ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/integrators/LDForceManager.cpp
(Generate patch)

Comparing trunk/src/integrators/LDForceManager.cpp (file contents):
Revision 908 by tim, Mon Mar 20 19:12:14 2006 UTC vs.
Revision 983 by gezelter, Tue Jun 6 17:43:28 2006 UTC

# Line 42 | Line 42
42   #include "integrators/LDForceManager.hpp"
43   #include "math/CholeskyDecomposition.hpp"
44   #include "utils/OOPSEConstant.hpp"
45 + #include "hydrodynamics/Sphere.hpp"
46 + #include "hydrodynamics/Ellipsoid.hpp"
47 + #include "openbabel/mol.hpp"
48 +
49 + using namespace OpenBabel;
50   namespace oopse {
51  
52    LDForceManager::LDForceManager(SimInfo* info) : ForceManager(info){
53 <    Globals* simParams = info->getSimParams();
54 <    std::map<std::string, HydroProp> hydroPropMap;
55 <    if (simParams->haveHydroPropFile()) {
56 <        hydroPropMap = parseFrictionFile(simParams->getHydroPropFile());
57 <    } else {
58 <        //error
53 >    simParams = info->getSimParams();
54 >    veloMunge = new Velocitizer(info);
55 >
56 >    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 >
81 >      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  
91 <    SimInfo::MoleculeIterator i;
92 <    Molecule::IntegrableObjectIterator  j;
91 >    // Build the hydroProp map:
92 >    std::map<std::string, HydroProp*> hydroPropMap;
93 >
94      Molecule* mol;
95      StuntDouble* integrableObject;
96 <    for (mol = info->beginMolecule(i); mol != NULL; mol = info->nextMolecule(i)) {
97 <      for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
98 <              integrableObject = mol->nextIntegrableObject(j)) {
99 <            std::map<std::string, HydroProp>::iterator iter = hydroPropMap.find(integrableObject->getType());
100 <            if (iter != hydroPropMap.end()) {
101 <                hydroProps_.push_back(iter->second);
96 >    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 >           integrableObject = mol->nextIntegrableObject(j)) {
105 >        
106 >        if (integrableObject->isRigidBody()) {
107 >          RigidBody* rb = static_cast<RigidBody*>(integrableObject);
108 >          if (rb->getNumAtoms() > 1) needHydroPropFile = true;
109 >        }
110 >        
111 >      }
112 >    }
113 >        
114 >
115 >    if (needHydroPropFile) {              
116 >      if (simParams->haveHydroPropFile()) {
117 >        hydroPropMap = parseFrictionFile(simParams->getHydroPropFile());
118 >      } else {              
119 >        sprintf( painCave.errMsg,
120 >                 "HydroPropFile must be set to a file name if Langevin\n"
121 >                 "\tDynamics is specified for rigidBodies which contain more\n"
122 >                 "\tthan one atom.  To create a HydroPropFile, run \"Hydro\".\n");
123 >        painCave.severity = OOPSE_ERROR;
124 >        painCave.isFatal = 1;
125 >        simError();  
126 >      }      
127 >
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 >          std::map<std::string, HydroProp*>::iterator iter = hydroPropMap.find(integrableObject->getType());
135 >          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 >      }
146 >    } else {
147 >      
148 >      std::map<std::string, HydroProp*> hydroPropMap;
149 >      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 >          if (integrableObject->isDirectionalAtom()) {
156 >            DirectionalAtom* dAtom = static_cast<DirectionalAtom*>(integrableObject);
157 >            AtomType* atomType = dAtom->getAtomType();
158 >            if (atomType->isGayBerne()) {
159 >              DirectionalAtomType* dAtomType = dynamic_cast<DirectionalAtomType*>(atomType);
160 >              
161 >              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 >                                            gayBerneParam.GB_d / 2.0,
169 >                                            gayBerneParam.GB_l / 2.0,
170 >                                            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 >            }
185 >          } else {
186 >            Atom* atom = static_cast<Atom*>(integrableObject);
187 >            AtomType* atomType = atom->getAtomType();
188 >            if (atomType->isLennardJones()){
189 >              GenericData* data = atomType->getPropertyByName("LennardJones");
190 >              if (data != NULL) {
191 >                LJParamGenericData* ljData = dynamic_cast<LJParamGenericData*>(data);
192 >                
193 >                if (ljData != NULL) {
194 >                  LJParam ljParam = ljData->getData();
195 >                  currShape = new Sphere(atom->getPos(), ljParam.sigma/2.0);
196 >                } else {
197 >                  sprintf( painCave.errMsg,
198 >                           "Can not cast GenericData to LJParam\n");
199 >                  painCave.severity = OOPSE_ERROR;
200 >                  painCave.isFatal = 1;
201 >                  simError();          
202 >                }      
203 >              }
204              } else {
205 <                //error
205 >              int obanum = etab.GetAtomicNum((atom->getType()).c_str());
206 >              if (obanum != 0) {
207 >                currShape = new Sphere(atom->getPos(), etab.GetVdwRad(obanum));
208 >              } else {
209 >                sprintf( painCave.errMsg,
210 >                         "Could not find atom type in default element.txt\n");
211 >                painCave.severity = OOPSE_ERROR;
212 >                painCave.isFatal = 1;
213 >                simError();          
214 >              }
215              }
216 <            
217 <           }
216 >          }
217 >          HydroProp* currHydroProp = currShape->getHydroProp(simParams->getViscosity(),simParams->getTargetTemp());
218 >          std::map<std::string, HydroProp*>::iterator iter = hydroPropMap.find(integrableObject->getType());
219 >          if (iter != hydroPropMap.end())
220 >            hydroProps_.push_back(iter->second);
221 >          else {
222 >            currHydroProp->complete();
223 >            hydroPropMap.insert(std::map<std::string, HydroProp*>::value_type(integrableObject->getType(), currHydroProp));
224 >            hydroProps_.push_back(currHydroProp);
225 >          }
226 >        }
227 >      }
228      }
229      variance_ = 2.0 * OOPSEConstant::kb*simParams->getTargetTemp()/simParams->getDt();
230 <  }
231 <  std::map<std::string, HydroProp> LDForceManager::parseFrictionFile(const std::string& filename) {
232 <    std::map<std::string, HydroProp> props;
230 >  }  
231 >
232 >  std::map<std::string, HydroProp*> LDForceManager::parseFrictionFile(const std::string& filename) {
233 >    std::map<std::string, HydroProp*> props;
234      std::ifstream ifs(filename.c_str());
235      if (ifs.is_open()) {
236 <
236 >      
237      }
238 <
238 >    
239      const unsigned int BufferSize = 65535;
240      char buffer[BufferSize];  
241      while (ifs.getline(buffer, BufferSize)) {
242 <        StringTokenizer tokenizer(buffer);
243 <        HydroProp currProp;
86 <        if (tokenizer.countTokens() >= 40) {
87 <            std::string atomName = tokenizer.nextToken();
88 <            currProp.cor[0] = tokenizer.nextTokenAsDouble();
89 <            currProp.cor[1] = tokenizer.nextTokenAsDouble();
90 <            currProp.cor[2] = tokenizer.nextTokenAsDouble();
91 <            
92 <            currProp.Xirtt(0,0) = tokenizer.nextTokenAsDouble();
93 <            currProp.Xirtt(0,1) = tokenizer.nextTokenAsDouble();
94 <            currProp.Xirtt(0,2) = tokenizer.nextTokenAsDouble();
95 <            currProp.Xirtt(1,0) = tokenizer.nextTokenAsDouble();
96 <            currProp.Xirtt(1,1) = tokenizer.nextTokenAsDouble();
97 <            currProp.Xirtt(1,2) = tokenizer.nextTokenAsDouble();
98 <            currProp.Xirtt(2,0) = tokenizer.nextTokenAsDouble();
99 <            currProp.Xirtt(2,1) = tokenizer.nextTokenAsDouble();
100 <            currProp.Xirtt(2,2) = tokenizer.nextTokenAsDouble();
101 <
102 <            currProp.Xirrt(0,0) = tokenizer.nextTokenAsDouble();
103 <            currProp.Xirrt(0,1) = tokenizer.nextTokenAsDouble();
104 <            currProp.Xirrt(0,2) = tokenizer.nextTokenAsDouble();
105 <            currProp.Xirrt(1,0) = tokenizer.nextTokenAsDouble();
106 <            currProp.Xirrt(1,1) = tokenizer.nextTokenAsDouble();
107 <            currProp.Xirrt(1,2) = tokenizer.nextTokenAsDouble();
108 <            currProp.Xirrt(2,0) = tokenizer.nextTokenAsDouble();
109 <            currProp.Xirrt(2,1) = tokenizer.nextTokenAsDouble();
110 <            currProp.Xirrt(2,2) = tokenizer.nextTokenAsDouble();
111 <        
112 <            currProp.Xirtr(0,0) = tokenizer.nextTokenAsDouble();
113 <            currProp.Xirtr(0,1) = tokenizer.nextTokenAsDouble();
114 <            currProp.Xirtr(0,2) = tokenizer.nextTokenAsDouble();
115 <            currProp.Xirtr(1,0) = tokenizer.nextTokenAsDouble();
116 <            currProp.Xirtr(1,1) = tokenizer.nextTokenAsDouble();
117 <            currProp.Xirtr(1,2) = tokenizer.nextTokenAsDouble();
118 <            currProp.Xirtr(2,0) = tokenizer.nextTokenAsDouble();
119 <            currProp.Xirtr(2,1) = tokenizer.nextTokenAsDouble();
120 <            currProp.Xirtr(2,2) = tokenizer.nextTokenAsDouble();
121 <
122 <            currProp.Xirrr(0,0) = tokenizer.nextTokenAsDouble();
123 <            currProp.Xirrr(0,1) = tokenizer.nextTokenAsDouble();
124 <            currProp.Xirrr(0,2) = tokenizer.nextTokenAsDouble();
125 <            currProp.Xirrr(1,0) = tokenizer.nextTokenAsDouble();
126 <            currProp.Xirrr(1,1) = tokenizer.nextTokenAsDouble();
127 <            currProp.Xirrr(1,2) = tokenizer.nextTokenAsDouble();
128 <            currProp.Xirrr(2,0) = tokenizer.nextTokenAsDouble();
129 <            currProp.Xirrr(2,1) = tokenizer.nextTokenAsDouble();
130 <            currProp.Xirrr(2,2) = tokenizer.nextTokenAsDouble();
131 <
132 <            SquareMatrix<double, 6> Xir;
133 <            Xir.setSubMatrix(0, 0, currProp.Xirtt);
134 <            Xir.setSubMatrix(0, 3, currProp.Xirrt);
135 <            Xir.setSubMatrix(3, 0, currProp.Xirtr);
136 <            Xir.setSubMatrix(3, 3, currProp.Xirrr);
137 <            CholeskyDecomposition(Xir, currProp.S);            
138 <
139 <            props.insert(std::map<std::string, HydroProp>::value_type(atomName, currProp));
140 <        }
242 >      HydroProp* currProp = new HydroProp(buffer);
243 >      props.insert(std::map<std::string, HydroProp*>::value_type(currProp->getName(), currProp));
244      }
245  
246      return props;
247    }
248 <  
248 >  
249    void LDForceManager::postCalculation() {
250      SimInfo::MoleculeIterator i;
251      Molecule::IntegrableObjectIterator  j;
# Line 155 | Line 258 | namespace oopse {
258      Mat3x3d Atrans;
259      Vector3d Tb;
260      Vector3d ji;
261 <    double mass;
261 >    RealType mass;
262      unsigned int index = 0;
263 <    for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
264 <      for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
265 <           integrableObject = mol->nextIntegrableObject(j)) {
263 >    bool doLangevinForces;
264 >    bool freezeMolecule;
265 >    int fdf;
266  
164          vel =integrableObject->getVel();
165          if (integrableObject->isDirectional()){
166             //calculate angular velocity in lab frame
167             Mat3x3d I = integrableObject->getI();
168             Vector3d angMom = integrableObject->getJ();
169             Vector3d omega;
267  
268 <             if (integrableObject->isLinear()) {
269 <                int linearAxis = integrableObject->linearAxis();
173 <                int l = (linearAxis +1 )%3;
174 <                int m = (linearAxis +2 )%3;
175 <                omega[l] = angMom[l] /I(l, l);
176 <                omega[m] = angMom[m] /I(m, m);
177 <                
178 <             } else {
179 <                 omega[0] = angMom[0] /I(0, 0);
180 <                 omega[1] = angMom[1] /I(1, 1);
181 <                 omega[2] = angMom[2] /I(2, 2);
182 <             }
268 >    fdf = 0;
269 >    for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
270  
271 <             //apply friction force and torque at center of resistance
272 <             A = integrableObject->getA();
186 <             Atrans = A.transpose();
187 <             Vector3d rcr = Atrans * hydroProps_[index].cor;  
188 <             Vector3d vcdLab = vel + cross(omega, rcr);
189 <             Vector3d vcdBody = A* vcdLab;
190 <             Vector3d frictionForceBody = -(hydroProps_[index].Xirtt * vcdBody + hydroProps_[index].Xirrt * omega);
191 <             Vector3d frictionForceLab = Atrans*frictionForceBody;
192 <             integrableObject->addFrc(frictionForceLab);
193 <             Vector3d frictionTorqueBody = - (hydroProps_[index].Xirtr * vcdBody + hydroProps_[index].Xirrr * omega);
194 <             Vector3d frictionTorqueLab = Atrans*frictionTorqueBody;
195 <             integrableObject->addTrq(frictionTorqueLab+ cross(rcr, frictionForceLab));
271 >      doLangevinForces = true;          
272 >      freezeMolecule = false;
273  
274 <             //apply random force and torque at center of resistance
275 <             Vector3d randomForceBody;
276 <             Vector3d randomTorqueBody;
277 <             genRandomForceAndTorque(randomForceBody, randomTorqueBody, index, variance_);
278 <             Vector3d randomForceLab = Atrans*randomForceBody;
279 <             Vector3d randomTorqueLab = Atrans* randomTorqueBody;
280 <             integrableObject->addFrc(randomForceLab);            
281 <             integrableObject->addTrq(randomTorqueLab + cross(rcr, randomForceLab ));            
282 <
274 >      if (sphericalBoundaryConditions_) {
275 >        
276 >        Vector3d molPos = mol->getCom();
277 >        RealType molRad = molPos.length();
278 >        
279 >        doLangevinForces = false;
280 >        
281 >        if (molRad > langevinBufferRadius_) {
282 >          doLangevinForces = true;
283 >          freezeMolecule = false;
284 >        }
285 >        if (molRad > frozenBufferRadius_) {
286 >          doLangevinForces = false;
287 >          freezeMolecule = true;
288 >        }
289 >      }
290 >      
291 >      for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
292 >           integrableObject = mol->nextIntegrableObject(j)) {
293 >          
294 >        if (freezeMolecule)
295 >          fdf += integrableObject->freeze();
296 >        
297 >        if (doLangevinForces) {          
298 >          vel =integrableObject->getVel();
299 >          if (integrableObject->isDirectional()){
300 >            //calculate angular velocity in lab frame
301 >            Mat3x3d I = integrableObject->getI();
302 >            Vector3d angMom = integrableObject->getJ();
303 >            Vector3d omega;
304 >            
305 >            if (integrableObject->isLinear()) {
306 >              int linearAxis = integrableObject->linearAxis();
307 >              int l = (linearAxis +1 )%3;
308 >              int m = (linearAxis +2 )%3;
309 >              omega[l] = angMom[l] /I(l, l);
310 >              omega[m] = angMom[m] /I(m, m);
311 >              
312 >            } else {
313 >              omega[0] = angMom[0] /I(0, 0);
314 >              omega[1] = angMom[1] /I(1, 1);
315 >              omega[2] = angMom[2] /I(2, 2);
316 >            }
317 >            
318 >            //apply friction force and torque at center of resistance
319 >            A = integrableObject->getA();
320 >            Atrans = A.transpose();
321 >            Vector3d rcr = Atrans * hydroProps_[index]->getCOR();  
322 >            Vector3d vcdLab = vel + cross(omega, rcr);
323 >            Vector3d vcdBody = A* vcdLab;
324 >            Vector3d frictionForceBody = -(hydroProps_[index]->getXitt() * vcdBody + hydroProps_[index]->getXirt() * omega);
325 >            Vector3d frictionForceLab = Atrans*frictionForceBody;
326 >            integrableObject->addFrc(frictionForceLab);
327 >            Vector3d frictionTorqueBody = - (hydroProps_[index]->getXitr() * vcdBody + hydroProps_[index]->getXirr() * omega);
328 >            Vector3d frictionTorqueLab = Atrans*frictionTorqueBody;
329 >            integrableObject->addTrq(frictionTorqueLab+ cross(rcr, frictionForceLab));
330 >            
331 >            //apply random force and torque at center of resistance
332 >            Vector3d randomForceBody;
333 >            Vector3d randomTorqueBody;
334 >            genRandomForceAndTorque(randomForceBody, randomTorqueBody, index, variance_);
335 >            Vector3d randomForceLab = Atrans*randomForceBody;
336 >            Vector3d randomTorqueLab = Atrans* randomTorqueBody;
337 >            integrableObject->addFrc(randomForceLab);            
338 >            integrableObject->addTrq(randomTorqueLab + cross(rcr, randomForceLab ));            
339 >            
340            } else {
341 <             //spheric atom
342 <             Vector3d frictionForce = -(hydroProps_[index].Xirtt *vel);    
343 <             Vector3d randomForce;
344 <             Vector3d randomTorque;
345 <             genRandomForceAndTorque(randomForce, randomTorque, index, variance_);
346 <
347 <             integrableObject->addFrc(frictionForce+randomForce);            
341 >            //spherical atom
342 >            Vector3d frictionForce = -(hydroProps_[index]->getXitt() * vel);
343 >            Vector3d randomForce;
344 >            Vector3d randomTorque;
345 >            genRandomForceAndTorque(randomForce, randomTorque, index, variance_);
346 >            
347 >            integrableObject->addFrc(frictionForce+randomForce);            
348            }
349 <
349 >        }
350 >          
351          ++index;
352      
353        }
354      }    
355 +    info_->setFdf(fdf);
356  
357 <    ForceManager::postCalculation();
357 >    veloMunge->removeComDrift();
358 >    // Remove angular drift if we are not using periodic boundary conditions.
359 >    if(!simParams->getUsePeriodicBoundaryConditions())
360 >      veloMunge->removeAngularDrift();
361  
362 <
224 <
362 >    ForceManager::postCalculation();  
363    }
364  
365 < void LDForceManager::genRandomForceAndTorque(Vector3d& force, Vector3d& torque, unsigned int index, double variance) {
365 > void LDForceManager::genRandomForceAndTorque(Vector3d& force, Vector3d& torque, unsigned int index, RealType variance) {
366  
367  
368 <    Vector<double, 6> Z;
369 <    Vector<double, 6> generalForce;
368 >    Vector<RealType, 6> Z;
369 >    Vector<RealType, 6> generalForce;
370  
371          
372      Z[0] = randNumGen_.randNorm(0, variance);
# Line 239 | Line 377 | void LDForceManager::genRandomForceAndTorque(Vector3d&
377      Z[5] = randNumGen_.randNorm(0, variance);
378      
379  
380 <    generalForce = hydroProps_[index].S*Z;
380 >    generalForce = hydroProps_[index]->getS()*Z;
381      
382      force[0] = generalForce[0];
383      force[1] = generalForce[1];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines