ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/integrators/LDForceManager.cpp
Revision: 1237
Committed: Fri Apr 18 16:55:15 2008 UTC (17 years ago) by gezelter
Original Path: trunk/src/integrators/LDForceManager.cpp
File size: 19093 byte(s)
Log Message:
Adding iterative looping to get better estimates of friction forces
from full-step velocities

File Contents

# User Rev Content
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     * 1. Acknowledgement of the program authors must be made in any
10     * publication of scientific results based in part on use of the
11     * program. An acceptable form of acknowledgement is citation of
12     * the article in which the program was described (Matthew
13     * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14     * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15     * Parallel Simulation Engine for Molecular Dynamics,"
16     * J. Comput. Chem. 26, pp. 252-271 (2005))
17     *
18     * 2. Redistributions of source code must retain the above copyright
19     * notice, this list of conditions and the following disclaimer.
20     *
21     * 3. Redistributions in binary form must reproduce the above copyright
22     * notice, this list of conditions and the following disclaimer in the
23     * documentation and/or other materials provided with the
24     * distribution.
25     *
26     * This software is provided "AS IS," without a warranty of any
27     * kind. All express or implied conditions, representations and
28     * warranties, including any implied warranty of merchantability,
29     * fitness for a particular purpose or non-infringement, are hereby
30     * excluded. The University of Notre Dame and its licensors shall not
31     * be liable for any damages suffered by licensee as a result of
32     * using, modifying or distributing the software or its
33     * derivatives. In no event will the University of Notre Dame or its
34     * licensors be liable for any lost revenue, profit or data, or for
35     * direct, indirect, special, consequential, incidental or punitive
36     * damages, however caused and regardless of the theory of liability,
37     * arising out of the use of or inability to use software, even if the
38     * University of Notre Dame has been advised of the possibility of
39     * such damages.
40     */
41     #include <fstream>
42 chuckv 1120 #include <iostream>
43 tim 895 #include "integrators/LDForceManager.hpp"
44     #include "math/CholeskyDecomposition.hpp"
45 tim 904 #include "utils/OOPSEConstant.hpp"
46 gezelter 956 #include "hydrodynamics/Sphere.hpp"
47     #include "hydrodynamics/Ellipsoid.hpp"
48 gezelter 1210 #include "utils/ElementsTable.hpp"
49 gezelter 956
50 tim 895 namespace oopse {
51    
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     painCave.severity = OOPSE_ERROR;
66     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     painCave.severity = OOPSE_ERROR;
77     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     painCave.severity = OOPSE_WARNING;
86     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 956 painCave.severity = OOPSE_ERROR;
124     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     painCave.severity = OOPSE_ERROR;
141     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     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     painCave.severity = OOPSE_ERROR;
175     painCave.isFatal = 1;
176     simError();
177     }
178     } else {
179     sprintf( painCave.errMsg, "Can not find Parameters for GayBerne\n");
180     painCave.severity = OOPSE_ERROR;
181     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     painCave.severity = OOPSE_ERROR;
196     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 956 painCave.severity = OOPSE_ERROR;
208     painCave.isFatal = 1;
209     simError();
210 xsun 1185 }
211 gezelter 956 }
212     }
213     }
214 gezelter 981 HydroProp* currHydroProp = currShape->getHydroProp(simParams->getViscosity(),simParams->getTargetTemp());
215     std::map<std::string, HydroProp*>::iterator iter = hydroPropMap.find(integrableObject->getType());
216 gezelter 956 if (iter != hydroPropMap.end())
217     hydroProps_.push_back(iter->second);
218     else {
219 gezelter 981 currHydroProp->complete();
220     hydroPropMap.insert(std::map<std::string, HydroProp*>::value_type(integrableObject->getType(), currHydroProp));
221     hydroProps_.push_back(currHydroProp);
222 gezelter 956 }
223     }
224     }
225     }
226 tim 904 variance_ = 2.0 * OOPSEConstant::kb*simParams->getTargetTemp()/simParams->getDt();
227 gezelter 981 }
228 gezelter 956
229 gezelter 981 std::map<std::string, HydroProp*> LDForceManager::parseFrictionFile(const std::string& filename) {
230     std::map<std::string, HydroProp*> props;
231 tim 895 std::ifstream ifs(filename.c_str());
232     if (ifs.is_open()) {
233 gezelter 945
234 tim 895 }
235 gezelter 945
236 tim 895 const unsigned int BufferSize = 65535;
237     char buffer[BufferSize];
238     while (ifs.getline(buffer, BufferSize)) {
239 gezelter 981 HydroProp* currProp = new HydroProp(buffer);
240     props.insert(std::map<std::string, HydroProp*>::value_type(currProp->getName(), currProp));
241 tim 895 }
242 gezelter 981
243 tim 895 return props;
244     }
245 gezelter 981
246 gezelter 1126 void LDForceManager::postCalculation(bool needStress){
247 tim 895 SimInfo::MoleculeIterator i;
248     Molecule::IntegrableObjectIterator j;
249     Molecule* mol;
250     StuntDouble* integrableObject;
251 xsun 1185 RealType mass;
252 tim 895 Vector3d pos;
253     Vector3d frc;
254     Mat3x3d A;
255 tim 904 Mat3x3d Atrans;
256 tim 895 Vector3d Tb;
257     Vector3d ji;
258     unsigned int index = 0;
259 gezelter 945 bool doLangevinForces;
260     bool freezeMolecule;
261     int fdf;
262 gezelter 983
263 chuckv 1120 fdf = 0;
264 gezelter 983
265 tim 895 for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
266 gezelter 970
267     doLangevinForces = true;
268     freezeMolecule = false;
269    
270 gezelter 945 if (sphericalBoundaryConditions_) {
271    
272     Vector3d molPos = mol->getCom();
273 tim 963 RealType molRad = molPos.length();
274 chuckv 1120
275 gezelter 945 doLangevinForces = false;
276    
277     if (molRad > langevinBufferRadius_) {
278     doLangevinForces = true;
279     freezeMolecule = false;
280     }
281     if (molRad > frozenBufferRadius_) {
282     doLangevinForces = false;
283     freezeMolecule = true;
284     }
285     }
286    
287 gezelter 956 for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
288     integrableObject = mol->nextIntegrableObject(j)) {
289 gezelter 945
290 gezelter 956 if (freezeMolecule)
291     fdf += integrableObject->freeze();
292    
293 chuckv 1120 if (doLangevinForces) {
294 xsun 1185 mass = integrableObject->getMass();
295 tim 895 if (integrableObject->isDirectional()){
296 gezelter 1237
297     // preliminaries for directional objects:
298    
299 xsun 1216 A = integrableObject->getA();
300     Atrans = A.transpose();
301     Vector3d rcrLab = Atrans * hydroProps_[index]->getCOR();
302 xsun 1185
303 gezelter 1237 //apply random force and torque at center of resistance
304 xsun 1185
305 gezelter 945 Vector3d randomForceBody;
306     Vector3d randomTorqueBody;
307     genRandomForceAndTorque(randomForceBody, randomTorqueBody, index, variance_);
308 xsun 1216 Vector3d randomForceLab = Atrans * randomForceBody;
309     Vector3d randomTorqueLab = Atrans * randomTorqueBody;
310 gezelter 945 integrableObject->addFrc(randomForceLab);
311 xsun 1216 integrableObject->addTrq(randomTorqueLab + cross(rcrLab, randomForceLab ));
312 gezelter 1237
313     Mat3x3d I = integrableObject->getI();
314     Vector3d omegaBody;
315    
316     // What remains contains velocity explicitly, but the velocity required
317     // is at the full step: v(t + h), while we have initially the velocity
318     // at the half step: v(t + h/2). We need to iterate to converge the
319     // friction force and friction torque vectors.
320    
321     // this is the velocity at the half-step:
322 gezelter 945
323 gezelter 1237 Vector3d vel =integrableObject->getVel();
324     Vector3d angMom = integrableObject->getJ();
325    
326     //estimate velocity at full-step using everything but friction forces:
327    
328     frc = integrableObject->getFrc();
329     Vector3d velStep = vel + (dt2_ /mass * OOPSEConstant::energyConvert) * frc;
330    
331     Tb = integrableObject->lab2Body(integrableObject->getTrq());
332     Vector3d angMomStep = angMom + (dt2_ * OOPSEConstant::energyConvert) * Tb;
333    
334     Vector3d omegaLab;
335     Vector3d vcdLab;
336     Vector3d vcdBody;
337     Vector3d frictionForceBody;
338     Vector3d frictionForceLab(0.0);
339     Vector3d oldFFL; // used to test for convergence
340     Vector3d frictionTorqueBody(0.0);
341     Vector3d oldFTB; // used to test for convergence
342     Vector3d frictionTorqueLab;
343     RealType fdot;
344     RealType tdot;
345    
346     //iteration starts here:
347    
348     for (int k = 0; k < maxIterNum_; k++) {
349    
350     if (integrableObject->isLinear()) {
351     int linearAxis = integrableObject->linearAxis();
352     int l = (linearAxis +1 )%3;
353     int m = (linearAxis +2 )%3;
354     omegaBody[l] = angMomStep[l] /I(l, l);
355     omegaBody[m] = angMomStep[m] /I(m, m);
356    
357     } else {
358     omegaBody[0] = angMomStep[0] /I(0, 0);
359     omegaBody[1] = angMomStep[1] /I(1, 1);
360     omegaBody[2] = angMomStep[2] /I(2, 2);
361     }
362    
363     omegaLab = Atrans * omegaBody;
364    
365     // apply friction force and torque at center of resistance
366    
367     vcdLab = velStep + cross(omegaLab, rcrLab);
368     vcdBody = A * vcdLab;
369     frictionForceBody = -(hydroProps_[index]->getXitt() * vcdBody + hydroProps_[index]->getXirt() * omegaBody);
370     oldFFL = frictionForceLab;
371     frictionForceLab = Atrans * frictionForceBody;
372     oldFTB = frictionTorqueBody;
373     frictionTorqueBody = -(hydroProps_[index]->getXitr() * vcdBody + hydroProps_[index]->getXirr() * omegaBody);
374     frictionTorqueLab = Atrans * frictionTorqueBody;
375    
376     // re-estimate velocities at full-step using friction forces:
377    
378     velStep = vel + (dt2_ / mass * OOPSEConstant::energyConvert) * (frc + frictionForceLab);
379     angMomStep = angMom + (dt2_ * OOPSEConstant::energyConvert) * (Tb + frictionTorqueBody);
380    
381     // check for convergence (if the vectors have converged, fdot and tdot will both be 1.0):
382    
383     fdot = dot(frictionForceLab, oldFFL) / frictionForceLab.lengthSquare();
384     tdot = dot(frictionTorqueBody, oldFTB) / frictionTorqueBody.lengthSquare();
385    
386     if (fabs(1.0 - fdot) <= forceTolerance_ && fabs(1.0 - tdot) <= forceTolerance_)
387     break; // iteration ends here
388     }
389    
390     integrableObject->addFrc(frictionForceLab);
391     integrableObject->addTrq(frictionTorqueLab + cross(rcrLab, frictionForceLab));
392    
393    
394 tim 895 } else {
395 gezelter 945 //spherical atom
396 gezelter 1237
397 gezelter 945 Vector3d randomForce;
398     Vector3d randomTorque;
399     genRandomForceAndTorque(randomForce, randomTorque, index, variance_);
400 gezelter 1237 integrableObject->addFrc(randomForce);
401    
402     // What remains contains velocity explicitly, but the velocity required
403     // is at the full step: v(t + h), while we have initially the velocity
404     // at the half step: v(t + h/2). We need to iterate to converge the
405     // friction force vector.
406    
407     // this is the velocity at the half-step:
408 gezelter 945
409 gezelter 1237 Vector3d vel =integrableObject->getVel();
410    
411     //estimate velocity at full-step using everything but friction forces:
412    
413     frc = integrableObject->getFrc();
414     Vector3d velStep = vel + (dt2_ / mass * OOPSEConstant::energyConvert) * frc;
415    
416     Vector3d frictionForce(0.0);
417     Vector3d oldFF; // used to test for convergence
418     RealType fdot;
419    
420     //iteration starts here:
421    
422     for (int k = 0; k < maxIterNum_; k++) {
423    
424     oldFF = frictionForce;
425     frictionForce = -hydroProps_[index]->getXitt() * velStep;
426    
427     // re-estimate velocities at full-step using friction forces:
428    
429     velStep = vel + (dt2_ / mass * OOPSEConstant::energyConvert) * (frc + frictionForce);
430    
431     // check for convergence (if the vector has converged, fdot will be 1.0):
432    
433     fdot = dot(frictionForce, oldFF) / frictionForce.lengthSquare();
434    
435     if (fabs(1.0 - fdot) <= forceTolerance_)
436     break; // iteration ends here
437     }
438    
439     integrableObject->addFrc(frictionForce);
440    
441 tim 895 }
442 gezelter 956 }
443 gezelter 945
444 gezelter 956 ++index;
445 tim 895
446     }
447 gezelter 956 }
448 chuckv 1120
449 gezelter 945 info_->setFdf(fdf);
450 gezelter 983 veloMunge->removeComDrift();
451     // Remove angular drift if we are not using periodic boundary conditions.
452     if(!simParams->getUsePeriodicBoundaryConditions())
453     veloMunge->removeAngularDrift();
454    
455 gezelter 1126 ForceManager::postCalculation(needStress);
456 tim 895 }
457    
458 tim 963 void LDForceManager::genRandomForceAndTorque(Vector3d& force, Vector3d& torque, unsigned int index, RealType variance) {
459 tim 904
460 tim 906
461 tim 963 Vector<RealType, 6> Z;
462     Vector<RealType, 6> generalForce;
463 tim 904
464 tim 895 Z[0] = randNumGen_.randNorm(0, variance);
465     Z[1] = randNumGen_.randNorm(0, variance);
466     Z[2] = randNumGen_.randNorm(0, variance);
467     Z[3] = randNumGen_.randNorm(0, variance);
468     Z[4] = randNumGen_.randNorm(0, variance);
469     Z[5] = randNumGen_.randNorm(0, variance);
470 tim 904
471 gezelter 981 generalForce = hydroProps_[index]->getS()*Z;
472 tim 904
473 tim 895 force[0] = generalForce[0];
474     force[1] = generalForce[1];
475     force[2] = generalForce[2];
476     torque[0] = generalForce[3];
477     torque[1] = generalForce[4];
478     torque[2] = generalForce[5];
479    
480 xsun 1185 }
481 tim 895
482     }

Properties

Name Value
svn:executable *