| 1 | tim | 895 | /* | 
| 2 |  |  | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 3 |  |  | * | 
| 4 |  |  | * The University of Notre Dame grants you ("Licensee") a | 
| 5 |  |  | * non-exclusive, royalty free, license to use, modify and | 
| 6 |  |  | * redistribute this software in source and binary code form, provided | 
| 7 |  |  | * that the following conditions are met: | 
| 8 |  |  | * | 
| 9 | gezelter | 1390 | * 1. Redistributions of source code must retain the above copyright | 
| 10 | tim | 895 | *    notice, this list of conditions and the following disclaimer. | 
| 11 |  |  | * | 
| 12 | gezelter | 1390 | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 | tim | 895 | *    notice, this list of conditions and the following disclaimer in the | 
| 14 |  |  | *    documentation and/or other materials provided with the | 
| 15 |  |  | *    distribution. | 
| 16 |  |  | * | 
| 17 |  |  | * This software is provided "AS IS," without a warranty of any | 
| 18 |  |  | * kind. All express or implied conditions, representations and | 
| 19 |  |  | * warranties, including any implied warranty of merchantability, | 
| 20 |  |  | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 |  |  | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 |  |  | * be liable for any damages suffered by licensee as a result of | 
| 23 |  |  | * using, modifying or distributing the software or its | 
| 24 |  |  | * derivatives. In no event will the University of Notre Dame or its | 
| 25 |  |  | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 |  |  | * direct, indirect, special, consequential, incidental or punitive | 
| 27 |  |  | * damages, however caused and regardless of the theory of liability, | 
| 28 |  |  | * arising out of the use of or inability to use software, even if the | 
| 29 |  |  | * University of Notre Dame has been advised of the possibility of | 
| 30 |  |  | * such damages. | 
| 31 | gezelter | 1390 | * | 
| 32 |  |  | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 |  |  | * research, please cite the appropriate papers when you publish your | 
| 34 |  |  | * work.  Good starting points are: | 
| 35 |  |  | * | 
| 36 |  |  | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 |  |  | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 |  |  | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). | 
| 39 |  |  | * [4]  Vardeman & Gezelter, in progress (2009). | 
| 40 | tim | 895 | */ | 
| 41 |  |  | #include <fstream> | 
| 42 | chuckv | 1120 | #include <iostream> | 
| 43 | tim | 895 | #include "integrators/LDForceManager.hpp" | 
| 44 |  |  | #include "math/CholeskyDecomposition.hpp" | 
| 45 | gezelter | 1390 | #include "utils/PhysicalConstants.hpp" | 
| 46 | gezelter | 956 | #include "hydrodynamics/Sphere.hpp" | 
| 47 |  |  | #include "hydrodynamics/Ellipsoid.hpp" | 
| 48 | gezelter | 1210 | #include "utils/ElementsTable.hpp" | 
| 49 | gezelter | 956 |  | 
| 50 | gezelter | 1390 | namespace OpenMD { | 
| 51 | tim | 895 |  | 
| 52 | gezelter | 1237 | LDForceManager::LDForceManager(SimInfo* info) : ForceManager(info), forceTolerance_(1e-6), maxIterNum_(4) { | 
| 53 | gezelter | 983 | simParams = info->getSimParams(); | 
| 54 |  |  | veloMunge = new Velocitizer(info); | 
| 55 |  |  |  | 
| 56 | gezelter | 945 | sphericalBoundaryConditions_ = false; | 
| 57 |  |  | if (simParams->getUseSphericalBoundaryConditions()) { | 
| 58 |  |  | sphericalBoundaryConditions_ = true; | 
| 59 |  |  | if (simParams->haveLangevinBufferRadius()) { | 
| 60 |  |  | langevinBufferRadius_ = simParams->getLangevinBufferRadius(); | 
| 61 |  |  | } else { | 
| 62 |  |  | sprintf( painCave.errMsg, | 
| 63 |  |  | "langevinBufferRadius must be specified " | 
| 64 |  |  | "when useSphericalBoundaryConditions is turned on.\n"); | 
| 65 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 66 | gezelter | 945 | painCave.isFatal = 1; | 
| 67 |  |  | simError(); | 
| 68 |  |  | } | 
| 69 |  |  |  | 
| 70 |  |  | if (simParams->haveFrozenBufferRadius()) { | 
| 71 |  |  | frozenBufferRadius_ = simParams->getFrozenBufferRadius(); | 
| 72 |  |  | } else { | 
| 73 |  |  | sprintf( painCave.errMsg, | 
| 74 |  |  | "frozenBufferRadius must be specified " | 
| 75 |  |  | "when useSphericalBoundaryConditions is turned on.\n"); | 
| 76 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 77 | gezelter | 945 | painCave.isFatal = 1; | 
| 78 |  |  | simError(); | 
| 79 |  |  | } | 
| 80 | tim | 895 |  | 
| 81 | gezelter | 945 | if (frozenBufferRadius_ < langevinBufferRadius_) { | 
| 82 |  |  | sprintf( painCave.errMsg, | 
| 83 |  |  | "frozenBufferRadius has been set smaller than the " | 
| 84 |  |  | "langevinBufferRadius.  This is probably an error.\n"); | 
| 85 | gezelter | 1390 | painCave.severity = OPENMD_WARNING; | 
| 86 | gezelter | 945 | painCave.isFatal = 0; | 
| 87 |  |  | simError(); | 
| 88 |  |  | } | 
| 89 |  |  | } | 
| 90 | gezelter | 956 |  | 
| 91 |  |  | // Build the hydroProp map: | 
| 92 | gezelter | 981 | std::map<std::string, HydroProp*> hydroPropMap; | 
| 93 | gezelter | 956 |  | 
| 94 | tim | 895 | Molecule* mol; | 
| 95 |  |  | StuntDouble* integrableObject; | 
| 96 | gezelter | 956 | SimInfo::MoleculeIterator i; | 
| 97 |  |  | Molecule::IntegrableObjectIterator  j; | 
| 98 |  |  | bool needHydroPropFile = false; | 
| 99 |  |  |  | 
| 100 |  |  | for (mol = info->beginMolecule(i); mol != NULL; | 
| 101 |  |  | mol = info->nextMolecule(i)) { | 
| 102 |  |  | for (integrableObject = mol->beginIntegrableObject(j); | 
| 103 |  |  | integrableObject != NULL; | 
| 104 | gezelter | 945 | integrableObject = mol->nextIntegrableObject(j)) { | 
| 105 | gezelter | 956 |  | 
| 106 |  |  | if (integrableObject->isRigidBody()) { | 
| 107 |  |  | RigidBody* rb = static_cast<RigidBody*>(integrableObject); | 
| 108 |  |  | if (rb->getNumAtoms() > 1) needHydroPropFile = true; | 
| 109 | gezelter | 945 | } | 
| 110 |  |  |  | 
| 111 |  |  | } | 
| 112 | tim | 895 | } | 
| 113 | gezelter | 956 |  | 
| 114 |  |  |  | 
| 115 |  |  | if (needHydroPropFile) { | 
| 116 |  |  | if (simParams->haveHydroPropFile()) { | 
| 117 |  |  | hydroPropMap = parseFrictionFile(simParams->getHydroPropFile()); | 
| 118 |  |  | } else { | 
| 119 |  |  | sprintf( painCave.errMsg, | 
| 120 | gezelter | 1237 | "HydroPropFile must be set to a file name if Langevin Dynamics\n" | 
| 121 |  |  | "\tis specified for rigidBodies which contain more than one atom\n" | 
| 122 |  |  | "\tTo create a HydroPropFile, run the \"Hydro\" program.\n"); | 
| 123 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 124 | gezelter | 956 | painCave.isFatal = 1; | 
| 125 |  |  | simError(); | 
| 126 |  |  | } | 
| 127 | tim | 971 |  | 
| 128 |  |  | for (mol = info->beginMolecule(i); mol != NULL; | 
| 129 |  |  | mol = info->nextMolecule(i)) { | 
| 130 |  |  | for (integrableObject = mol->beginIntegrableObject(j); | 
| 131 |  |  | integrableObject != NULL; | 
| 132 |  |  | integrableObject = mol->nextIntegrableObject(j)) { | 
| 133 |  |  |  | 
| 134 | gezelter | 981 | std::map<std::string, HydroProp*>::iterator iter = hydroPropMap.find(integrableObject->getType()); | 
| 135 | tim | 971 | if (iter != hydroPropMap.end()) { | 
| 136 |  |  | hydroProps_.push_back(iter->second); | 
| 137 |  |  | } else { | 
| 138 |  |  | sprintf( painCave.errMsg, | 
| 139 |  |  | "Can not find resistance tensor for atom [%s]\n", integrableObject->getType().c_str()); | 
| 140 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 141 | tim | 971 | painCave.isFatal = 1; | 
| 142 |  |  | simError(); | 
| 143 |  |  | } | 
| 144 |  |  | } | 
| 145 | gezelter | 956 | } | 
| 146 |  |  | } else { | 
| 147 | gezelter | 981 |  | 
| 148 |  |  | std::map<std::string, HydroProp*> hydroPropMap; | 
| 149 | gezelter | 956 | for (mol = info->beginMolecule(i); mol != NULL; | 
| 150 |  |  | mol = info->nextMolecule(i)) { | 
| 151 |  |  | for (integrableObject = mol->beginIntegrableObject(j); | 
| 152 |  |  | integrableObject != NULL; | 
| 153 |  |  | integrableObject = mol->nextIntegrableObject(j)) { | 
| 154 |  |  | Shape* currShape = NULL; | 
| 155 | xsun | 1185 |  | 
| 156 |  |  | if (integrableObject->isAtom()){ | 
| 157 |  |  | Atom* atom = static_cast<Atom*>(integrableObject); | 
| 158 |  |  | AtomType* atomType = atom->getAtomType(); | 
| 159 | gezelter | 956 | if (atomType->isGayBerne()) { | 
| 160 | xsun | 1185 | DirectionalAtomType* dAtomType = dynamic_cast<DirectionalAtomType*>(atomType); | 
| 161 | gezelter | 956 | GenericData* data = dAtomType->getPropertyByName("GayBerne"); | 
| 162 |  |  | if (data != NULL) { | 
| 163 |  |  | GayBerneParamGenericData* gayBerneData = dynamic_cast<GayBerneParamGenericData*>(data); | 
| 164 |  |  |  | 
| 165 |  |  | if (gayBerneData != NULL) { | 
| 166 |  |  | GayBerneParam gayBerneParam = gayBerneData->getData(); | 
| 167 | gezelter | 1610 | currShape = new Ellipsoid(V3Zero, | 
| 168 | xsun | 1185 | gayBerneParam.GB_l / 2.0, | 
| 169 | gezelter | 981 | gayBerneParam.GB_d / 2.0, | 
| 170 | gezelter | 956 | Mat3x3d::identity()); | 
| 171 |  |  | } else { | 
| 172 |  |  | sprintf( painCave.errMsg, | 
| 173 |  |  | "Can not cast GenericData to GayBerneParam\n"); | 
| 174 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 175 | gezelter | 956 | painCave.isFatal = 1; | 
| 176 |  |  | simError(); | 
| 177 |  |  | } | 
| 178 |  |  | } else { | 
| 179 |  |  | sprintf( painCave.errMsg, "Can not find Parameters for GayBerne\n"); | 
| 180 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 181 | gezelter | 956 | painCave.isFatal = 1; | 
| 182 |  |  | simError(); | 
| 183 |  |  | } | 
| 184 | xsun | 1185 | } else { | 
| 185 |  |  | if (atomType->isLennardJones()){ | 
| 186 |  |  | GenericData* data = atomType->getPropertyByName("LennardJones"); | 
| 187 |  |  | if (data != NULL) { | 
| 188 |  |  | LJParamGenericData* ljData = dynamic_cast<LJParamGenericData*>(data); | 
| 189 |  |  | if (ljData != NULL) { | 
| 190 |  |  | LJParam ljParam = ljData->getData(); | 
| 191 |  |  | currShape = new Sphere(atom->getPos(), ljParam.sigma/2.0); | 
| 192 |  |  | } else { | 
| 193 |  |  | sprintf( painCave.errMsg, | 
| 194 |  |  | "Can not cast GenericData to LJParam\n"); | 
| 195 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 196 | xsun | 1185 | painCave.isFatal = 1; | 
| 197 |  |  | simError(); | 
| 198 |  |  | } | 
| 199 |  |  | } | 
| 200 |  |  | } else { | 
| 201 | gezelter | 1237 | int aNum = etab.GetAtomicNum((atom->getType()).c_str()); | 
| 202 |  |  | if (aNum != 0) { | 
| 203 |  |  | currShape = new Sphere(atom->getPos(), etab.GetVdwRad(aNum)); | 
| 204 | gezelter | 956 | } else { | 
| 205 |  |  | sprintf( painCave.errMsg, | 
| 206 | xsun | 1185 | "Could not find atom type in default element.txt\n"); | 
| 207 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 208 | gezelter | 956 | painCave.isFatal = 1; | 
| 209 |  |  | simError(); | 
| 210 | xsun | 1185 | } | 
| 211 | gezelter | 956 | } | 
| 212 |  |  | } | 
| 213 |  |  | } | 
| 214 | chuckv | 1293 |  | 
| 215 |  |  | if (!simParams->haveTargetTemp()) { | 
| 216 |  |  | sprintf(painCave.errMsg, "You can't use LangevinDynamics without a targetTemp!\n"); | 
| 217 |  |  | painCave.isFatal = 1; | 
| 218 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 219 | chuckv | 1293 | simError(); | 
| 220 |  |  | } | 
| 221 |  |  |  | 
| 222 |  |  | if (!simParams->haveViscosity()) { | 
| 223 |  |  | sprintf(painCave.errMsg, "You can't use LangevinDynamics without a viscosity!\n"); | 
| 224 |  |  | painCave.isFatal = 1; | 
| 225 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 226 | chuckv | 1293 | simError(); | 
| 227 |  |  | } | 
| 228 |  |  |  | 
| 229 | gezelter | 1610 | HydroProp* currHydroProp = currShape->getHydroProp(simParams->getViscosity(),simParams->getTargetTemp()); | 
| 230 | chuckv | 1293 |  | 
| 231 | gezelter | 981 | std::map<std::string, HydroProp*>::iterator iter = hydroPropMap.find(integrableObject->getType()); | 
| 232 | gezelter | 956 | if (iter != hydroPropMap.end()) | 
| 233 |  |  | hydroProps_.push_back(iter->second); | 
| 234 |  |  | else { | 
| 235 | gezelter | 981 | currHydroProp->complete(); | 
| 236 |  |  | hydroPropMap.insert(std::map<std::string, HydroProp*>::value_type(integrableObject->getType(), currHydroProp)); | 
| 237 |  |  | hydroProps_.push_back(currHydroProp); | 
| 238 | gezelter | 956 | } | 
| 239 |  |  | } | 
| 240 |  |  | } | 
| 241 |  |  | } | 
| 242 | gezelter | 1390 | variance_ = 2.0 * PhysicalConstants::kb*simParams->getTargetTemp()/simParams->getDt(); | 
| 243 | gezelter | 981 | } | 
| 244 | gezelter | 956 |  | 
| 245 | gezelter | 981 | std::map<std::string, HydroProp*> LDForceManager::parseFrictionFile(const std::string& filename) { | 
| 246 |  |  | std::map<std::string, HydroProp*> props; | 
| 247 | tim | 895 | std::ifstream ifs(filename.c_str()); | 
| 248 |  |  | if (ifs.is_open()) { | 
| 249 | gezelter | 945 |  | 
| 250 | tim | 895 | } | 
| 251 | gezelter | 945 |  | 
| 252 | tim | 895 | const unsigned int BufferSize = 65535; | 
| 253 |  |  | char buffer[BufferSize]; | 
| 254 |  |  | while (ifs.getline(buffer, BufferSize)) { | 
| 255 | gezelter | 981 | HydroProp* currProp = new HydroProp(buffer); | 
| 256 |  |  | props.insert(std::map<std::string, HydroProp*>::value_type(currProp->getName(), currProp)); | 
| 257 | tim | 895 | } | 
| 258 | gezelter | 981 |  | 
| 259 | tim | 895 | return props; | 
| 260 |  |  | } | 
| 261 | gezelter | 981 |  | 
| 262 | gezelter | 1464 | void LDForceManager::postCalculation(){ | 
| 263 | tim | 895 | SimInfo::MoleculeIterator i; | 
| 264 |  |  | Molecule::IntegrableObjectIterator  j; | 
| 265 |  |  | Molecule* mol; | 
| 266 |  |  | StuntDouble* integrableObject; | 
| 267 | xsun | 1185 | RealType mass; | 
| 268 | tim | 895 | Vector3d pos; | 
| 269 |  |  | Vector3d frc; | 
| 270 |  |  | Mat3x3d A; | 
| 271 | tim | 904 | Mat3x3d Atrans; | 
| 272 | tim | 895 | Vector3d Tb; | 
| 273 |  |  | Vector3d ji; | 
| 274 |  |  | unsigned int index = 0; | 
| 275 | gezelter | 945 | bool doLangevinForces; | 
| 276 |  |  | bool freezeMolecule; | 
| 277 |  |  | int fdf; | 
| 278 | gezelter | 983 |  | 
| 279 | chuckv | 1120 | fdf = 0; | 
| 280 | gezelter | 983 |  | 
| 281 | tim | 895 | for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) { | 
| 282 | gezelter | 970 |  | 
| 283 |  |  | doLangevinForces = true; | 
| 284 |  |  | freezeMolecule = false; | 
| 285 |  |  |  | 
| 286 | gezelter | 945 | if (sphericalBoundaryConditions_) { | 
| 287 |  |  |  | 
| 288 |  |  | Vector3d molPos = mol->getCom(); | 
| 289 | tim | 963 | RealType molRad = molPos.length(); | 
| 290 | chuckv | 1120 |  | 
| 291 | gezelter | 945 | doLangevinForces = false; | 
| 292 |  |  |  | 
| 293 |  |  | if (molRad > langevinBufferRadius_) { | 
| 294 |  |  | doLangevinForces = true; | 
| 295 |  |  | freezeMolecule = false; | 
| 296 |  |  | } | 
| 297 |  |  | if (molRad > frozenBufferRadius_) { | 
| 298 |  |  | doLangevinForces = false; | 
| 299 |  |  | freezeMolecule = true; | 
| 300 |  |  | } | 
| 301 |  |  | } | 
| 302 |  |  |  | 
| 303 | gezelter | 956 | for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; | 
| 304 |  |  | integrableObject = mol->nextIntegrableObject(j)) { | 
| 305 | gezelter | 945 |  | 
| 306 | gezelter | 956 | if (freezeMolecule) | 
| 307 |  |  | fdf += integrableObject->freeze(); | 
| 308 |  |  |  | 
| 309 | chuckv | 1120 | if (doLangevinForces) { | 
| 310 | xsun | 1185 | mass = integrableObject->getMass(); | 
| 311 | tim | 895 | if (integrableObject->isDirectional()){ | 
| 312 | gezelter | 1237 |  | 
| 313 |  |  | // preliminaries for directional objects: | 
| 314 |  |  |  | 
| 315 | xsun | 1216 | A = integrableObject->getA(); | 
| 316 |  |  | Atrans = A.transpose(); | 
| 317 |  |  | Vector3d rcrLab = Atrans * hydroProps_[index]->getCOR(); | 
| 318 | xsun | 1185 |  | 
| 319 | gezelter | 1237 | //apply random force and torque at center of resistance | 
| 320 | xsun | 1185 |  | 
| 321 | gezelter | 945 | Vector3d randomForceBody; | 
| 322 |  |  | Vector3d randomTorqueBody; | 
| 323 |  |  | genRandomForceAndTorque(randomForceBody, randomTorqueBody, index, variance_); | 
| 324 | xsun | 1216 | Vector3d randomForceLab = Atrans * randomForceBody; | 
| 325 |  |  | Vector3d randomTorqueLab = Atrans * randomTorqueBody; | 
| 326 | gezelter | 1610 |  | 
| 327 | gezelter | 945 | integrableObject->addFrc(randomForceLab); | 
| 328 | xsun | 1216 | integrableObject->addTrq(randomTorqueLab + cross(rcrLab, randomForceLab )); | 
| 329 | gezelter | 1237 |  | 
| 330 |  |  | Mat3x3d I = integrableObject->getI(); | 
| 331 |  |  | Vector3d omegaBody; | 
| 332 |  |  |  | 
| 333 |  |  | // What remains contains velocity explicitly, but the velocity required | 
| 334 |  |  | // is at the full step: v(t + h), while we have initially the velocity | 
| 335 |  |  | // at the half step: v(t + h/2).  We need to iterate to converge the | 
| 336 |  |  | // friction force and friction torque vectors. | 
| 337 |  |  |  | 
| 338 |  |  | // this is the velocity at the half-step: | 
| 339 | gezelter | 945 |  | 
| 340 | gezelter | 1237 | Vector3d vel =integrableObject->getVel(); | 
| 341 |  |  | Vector3d angMom = integrableObject->getJ(); | 
| 342 |  |  |  | 
| 343 |  |  | //estimate velocity at full-step using everything but friction forces: | 
| 344 |  |  |  | 
| 345 |  |  | frc = integrableObject->getFrc(); | 
| 346 | gezelter | 1390 | Vector3d velStep = vel + (dt2_ /mass * PhysicalConstants::energyConvert) * frc; | 
| 347 | gezelter | 1237 |  | 
| 348 |  |  | Tb = integrableObject->lab2Body(integrableObject->getTrq()); | 
| 349 | gezelter | 1390 | Vector3d angMomStep = angMom + (dt2_ * PhysicalConstants::energyConvert) * Tb; | 
| 350 | gezelter | 1237 |  | 
| 351 |  |  | Vector3d omegaLab; | 
| 352 |  |  | Vector3d vcdLab; | 
| 353 |  |  | Vector3d vcdBody; | 
| 354 |  |  | Vector3d frictionForceBody; | 
| 355 |  |  | Vector3d frictionForceLab(0.0); | 
| 356 |  |  | Vector3d oldFFL;  // used to test for convergence | 
| 357 |  |  | Vector3d frictionTorqueBody(0.0); | 
| 358 |  |  | Vector3d oldFTB;  // used to test for convergence | 
| 359 |  |  | Vector3d frictionTorqueLab; | 
| 360 |  |  | RealType fdot; | 
| 361 |  |  | RealType tdot; | 
| 362 |  |  |  | 
| 363 |  |  | //iteration starts here: | 
| 364 |  |  |  | 
| 365 |  |  | for (int k = 0; k < maxIterNum_; k++) { | 
| 366 |  |  |  | 
| 367 |  |  | if (integrableObject->isLinear()) { | 
| 368 |  |  | int linearAxis = integrableObject->linearAxis(); | 
| 369 |  |  | int l = (linearAxis +1 )%3; | 
| 370 |  |  | int m = (linearAxis +2 )%3; | 
| 371 |  |  | omegaBody[l] = angMomStep[l] /I(l, l); | 
| 372 |  |  | omegaBody[m] = angMomStep[m] /I(m, m); | 
| 373 |  |  |  | 
| 374 |  |  | } else { | 
| 375 |  |  | omegaBody[0] = angMomStep[0] /I(0, 0); | 
| 376 |  |  | omegaBody[1] = angMomStep[1] /I(1, 1); | 
| 377 |  |  | omegaBody[2] = angMomStep[2] /I(2, 2); | 
| 378 |  |  | } | 
| 379 |  |  |  | 
| 380 |  |  | omegaLab = Atrans * omegaBody; | 
| 381 |  |  |  | 
| 382 |  |  | // apply friction force and torque at center of resistance | 
| 383 |  |  |  | 
| 384 |  |  | vcdLab = velStep + cross(omegaLab, rcrLab); | 
| 385 |  |  | vcdBody = A * vcdLab; | 
| 386 |  |  | frictionForceBody = -(hydroProps_[index]->getXitt() * vcdBody + hydroProps_[index]->getXirt() * omegaBody); | 
| 387 |  |  | oldFFL = frictionForceLab; | 
| 388 |  |  | frictionForceLab = Atrans * frictionForceBody; | 
| 389 |  |  | oldFTB = frictionTorqueBody; | 
| 390 |  |  | frictionTorqueBody = -(hydroProps_[index]->getXitr() * vcdBody + hydroProps_[index]->getXirr() * omegaBody); | 
| 391 |  |  | frictionTorqueLab = Atrans * frictionTorqueBody; | 
| 392 |  |  |  | 
| 393 |  |  | // re-estimate velocities at full-step using friction forces: | 
| 394 |  |  |  | 
| 395 | gezelter | 1390 | velStep = vel + (dt2_ / mass * PhysicalConstants::energyConvert) * (frc + frictionForceLab); | 
| 396 |  |  | angMomStep = angMom + (dt2_ * PhysicalConstants::energyConvert) * (Tb + frictionTorqueBody); | 
| 397 | gezelter | 1237 |  | 
| 398 |  |  | // check for convergence (if the vectors have converged, fdot and tdot will both be 1.0): | 
| 399 |  |  |  | 
| 400 |  |  | fdot = dot(frictionForceLab, oldFFL) / frictionForceLab.lengthSquare(); | 
| 401 |  |  | tdot = dot(frictionTorqueBody, oldFTB) / frictionTorqueBody.lengthSquare(); | 
| 402 |  |  |  | 
| 403 |  |  | if (fabs(1.0 - fdot) <= forceTolerance_ && fabs(1.0 - tdot) <= forceTolerance_) | 
| 404 |  |  | break; // iteration ends here | 
| 405 |  |  | } | 
| 406 |  |  |  | 
| 407 |  |  | integrableObject->addFrc(frictionForceLab); | 
| 408 |  |  | integrableObject->addTrq(frictionTorqueLab + cross(rcrLab, frictionForceLab)); | 
| 409 |  |  |  | 
| 410 |  |  |  | 
| 411 | tim | 895 | } else { | 
| 412 | gezelter | 945 | //spherical atom | 
| 413 | gezelter | 1237 |  | 
| 414 | gezelter | 945 | Vector3d randomForce; | 
| 415 |  |  | Vector3d randomTorque; | 
| 416 |  |  | genRandomForceAndTorque(randomForce, randomTorque, index, variance_); | 
| 417 | gezelter | 1237 | integrableObject->addFrc(randomForce); | 
| 418 |  |  |  | 
| 419 |  |  | // What remains contains velocity explicitly, but the velocity required | 
| 420 |  |  | // is at the full step: v(t + h), while we have initially the velocity | 
| 421 |  |  | // at the half step: v(t + h/2).  We need to iterate to converge the | 
| 422 |  |  | // friction force vector. | 
| 423 |  |  |  | 
| 424 |  |  | // this is the velocity at the half-step: | 
| 425 | gezelter | 945 |  | 
| 426 | gezelter | 1237 | Vector3d vel =integrableObject->getVel(); | 
| 427 |  |  |  | 
| 428 |  |  | //estimate velocity at full-step using everything but friction forces: | 
| 429 |  |  |  | 
| 430 |  |  | frc = integrableObject->getFrc(); | 
| 431 | gezelter | 1390 | Vector3d velStep = vel + (dt2_ / mass * PhysicalConstants::energyConvert) * frc; | 
| 432 | gezelter | 1237 |  | 
| 433 |  |  | Vector3d frictionForce(0.0); | 
| 434 |  |  | Vector3d oldFF;  // used to test for convergence | 
| 435 |  |  | RealType fdot; | 
| 436 |  |  |  | 
| 437 |  |  | //iteration starts here: | 
| 438 |  |  |  | 
| 439 |  |  | for (int k = 0; k < maxIterNum_; k++) { | 
| 440 |  |  |  | 
| 441 |  |  | oldFF = frictionForce; | 
| 442 |  |  | frictionForce = -hydroProps_[index]->getXitt() * velStep; | 
| 443 |  |  |  | 
| 444 |  |  | // re-estimate velocities at full-step using friction forces: | 
| 445 |  |  |  | 
| 446 | gezelter | 1390 | velStep = vel + (dt2_ / mass * PhysicalConstants::energyConvert) * (frc + frictionForce); | 
| 447 | gezelter | 1237 |  | 
| 448 |  |  | // check for convergence (if the vector has converged, fdot will be 1.0): | 
| 449 |  |  |  | 
| 450 |  |  | fdot = dot(frictionForce, oldFF) / frictionForce.lengthSquare(); | 
| 451 |  |  |  | 
| 452 |  |  | if (fabs(1.0 - fdot) <= forceTolerance_) | 
| 453 |  |  | break; // iteration ends here | 
| 454 |  |  | } | 
| 455 |  |  |  | 
| 456 |  |  | integrableObject->addFrc(frictionForce); | 
| 457 |  |  |  | 
| 458 | tim | 895 | } | 
| 459 | gezelter | 956 | } | 
| 460 | gezelter | 945 |  | 
| 461 | gezelter | 956 | ++index; | 
| 462 | tim | 895 |  | 
| 463 |  |  | } | 
| 464 | gezelter | 956 | } | 
| 465 | chuckv | 1120 |  | 
| 466 | gezelter | 945 | info_->setFdf(fdf); | 
| 467 | gezelter | 983 | veloMunge->removeComDrift(); | 
| 468 |  |  | // Remove angular drift if we are not using periodic boundary conditions. | 
| 469 |  |  | if(!simParams->getUsePeriodicBoundaryConditions()) | 
| 470 |  |  | veloMunge->removeAngularDrift(); | 
| 471 |  |  |  | 
| 472 | gezelter | 1464 | ForceManager::postCalculation(); | 
| 473 | tim | 895 | } | 
| 474 |  |  |  | 
| 475 | tim | 963 | void LDForceManager::genRandomForceAndTorque(Vector3d& force, Vector3d& torque, unsigned int index, RealType variance) { | 
| 476 | tim | 904 |  | 
| 477 | tim | 906 |  | 
| 478 | tim | 963 | Vector<RealType, 6> Z; | 
| 479 |  |  | Vector<RealType, 6> generalForce; | 
| 480 | tim | 904 |  | 
| 481 | tim | 895 | Z[0] = randNumGen_.randNorm(0, variance); | 
| 482 |  |  | Z[1] = randNumGen_.randNorm(0, variance); | 
| 483 |  |  | Z[2] = randNumGen_.randNorm(0, variance); | 
| 484 |  |  | Z[3] = randNumGen_.randNorm(0, variance); | 
| 485 |  |  | Z[4] = randNumGen_.randNorm(0, variance); | 
| 486 |  |  | Z[5] = randNumGen_.randNorm(0, variance); | 
| 487 | tim | 904 |  | 
| 488 | gezelter | 981 | generalForce = hydroProps_[index]->getS()*Z; | 
| 489 | tim | 904 |  | 
| 490 | tim | 895 | force[0] = generalForce[0]; | 
| 491 |  |  | force[1] = generalForce[1]; | 
| 492 |  |  | force[2] = generalForce[2]; | 
| 493 |  |  | torque[0] = generalForce[3]; | 
| 494 |  |  | torque[1] = generalForce[4]; | 
| 495 |  |  | torque[2] = generalForce[5]; | 
| 496 |  |  |  | 
| 497 | xsun | 1185 | } | 
| 498 | tim | 895 |  | 
| 499 |  |  | } |