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

Comparing branches/development/src/nonbonded/LJ.cpp (file contents):
Revision 1472 by gezelter, Mon Jul 19 20:42:55 2010 UTC vs.
Revision 1629 by gezelter, Wed Sep 14 21:15:17 2011 UTC

# Line 46 | Line 46
46   #include "nonbonded/LJ.hpp"
47   #include "utils/simError.h"
48  
49
49   namespace OpenMD {
50  
51 <  bool LJ::initialized_ = false;
53 <  bool LJ::shiftedPot_ = false;
54 <  bool LJ::shiftedFrc_ = false;
55 <  ForceField* LJ::forceField_ = NULL;
56 <  std::map<int, AtomType*> LJ::LJMap;
57 <  std::map<std::pair<AtomType*, AtomType*>, LJInteractionData> LJ::MixingMap;
58 <  
59 <  LJ* LJ::_instance = NULL;
51 >  LJ::LJ() : name_("LJ"), initialized_(false), forceField_(NULL) {}
52  
61  LJ* LJ::Instance() {
62    if (!_instance) {
63      _instance = new LJ();
64    }
65    return _instance;
66  }
67
53    LJParam LJ::getLJParam(AtomType* atomType) {
54      
55      // Do sanity checking on the AtomType we were passed before
# Line 106 | Line 91 | namespace OpenMD {
91      return ljParam.sigma;
92    }
93  
109  RealType LJ::getSigma(int atid) {
110    std::map<int, AtomType*> :: const_iterator it;
111    it = LJMap.find(atid);
112    if (it == LJMap.end()) {
113      sprintf( painCave.errMsg,
114               "LJ::getSigma could not find atid %d in LJMap\n",
115               (atid));
116      painCave.severity = OPENMD_ERROR;
117      painCave.isFatal = 1;
118      simError();          
119    }
120    AtomType* atype = it->second;
121
122    return getSigma(atype);    
123  }
124
94    RealType LJ::getSigma(AtomType* atomType1, AtomType* atomType2) {    
95      RealType sigma1 = getSigma(atomType1);
96      RealType sigma2 = getSigma(atomType2);
97      
98      ForceFieldOptions& fopts = forceField_->getForceFieldOptions();
99 <    std::string DistanceMix = fopts.getDistanceMixingRule();
99 >    string DistanceMix = fopts.getDistanceMixingRule();
100      toUpper(DistanceMix);
101  
102      if (DistanceMix == "GEOMETRIC")
# Line 141 | Line 110 | namespace OpenMD {
110      return ljParam.epsilon;
111    }
112  
144  RealType LJ::getEpsilon(int atid) {    
145    std::map<int, AtomType*> :: const_iterator it;
146    it = LJMap.find(atid);
147    if (it == LJMap.end()) {
148      sprintf( painCave.errMsg,
149               "LJ::getEpsilon could not find atid %d in LJMap\n",
150               (atid));
151      painCave.severity = OPENMD_ERROR;
152      painCave.isFatal = 1;
153      simError();          
154    }
155    AtomType* atype = it->second;
156
157    return getEpsilon(atype);    
158  }
159
113    RealType LJ::getEpsilon(AtomType* atomType1, AtomType* atomType2) {    
114      RealType epsilon1 = getEpsilon(atomType1);
115      RealType epsilon2 = getEpsilon(atomType2);
# Line 164 | Line 117 | namespace OpenMD {
117    }
118  
119    void LJ::initialize() {    
120 <    ForceField::AtomTypeContainer atomTypes = forceField_->getAtomTypes();
120 >    ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes();
121      ForceField::AtomTypeContainer::MapTypeIterator i;
122      AtomType* at;
123  
124 <    for (at = atomTypes.beginType(i); at != NULL;
125 <         at = atomTypes.nextType(i)) {
124 >    for (at = atomTypes->beginType(i); at != NULL;
125 >         at = atomTypes->nextType(i)) {
126        
127        if (at->isLennardJones())
128          addType(at);
129      }
130  
131 <    ForceField::NonBondedInteractionTypeContainer nbiTypes = forceField_->getNonBondedInteractionTypes();
131 >    ForceField::NonBondedInteractionTypeContainer* nbiTypes = forceField_->getNonBondedInteractionTypes();
132      ForceField::NonBondedInteractionTypeContainer::MapTypeIterator j;
133      NonBondedInteractionType* nbt;
134  
135 <    for (nbt = nbiTypes.beginType(j); nbt != NULL;
136 <         nbt = nbiTypes.nextType(j)) {
135 >    for (nbt = nbiTypes->beginType(j); nbt != NULL;
136 >         nbt = nbiTypes->nextType(j)) {
137        
138        if (nbt->isLennardJones()) {
139          
140 <        std::pair<AtomType*, AtomType*> atypes = nbt->getAtomTypes();
140 >        pair<AtomType*, AtomType*> atypes = nbt->getAtomTypes();
141          
142          GenericData* data = nbt->getPropertyByName("LennardJones");
143          if (data == NULL) {
# Line 225 | Line 178 | namespace OpenMD {
178    void LJ::addType(AtomType* atomType){
179      RealType sigma1 = getSigma(atomType);
180      RealType epsilon1 = getEpsilon(atomType);
181 <
181 >    
182      // add it to the map:
183      AtomTypeProperties atp = atomType->getATP();    
184 <    std::pair<std::map<int,AtomType*>::iterator,bool> ret;
185 <    ret = LJMap.insert( std::pair<int, AtomType*>(atp.ident, atomType) );
184 >
185 >    pair<map<int,AtomType*>::iterator,bool> ret;    
186 >    ret = LJMap.insert( pair<int, AtomType*>(atp.ident, atomType) );
187      if (ret.second == false) {
188        sprintf( painCave.errMsg,
189                 "LJ already had a previous entry with ident %d\n",
# Line 285 | Line 239 | namespace OpenMD {
239      }    
240    }
241  
242 <  void LJ::calcForce(AtomType* at1, AtomType* at2, Vector3d d,
289 <                     RealType rij, RealType r2, RealType rcut, RealType sw,
290 <                     RealType vdwMult, RealType &vpair, RealType &pot,
291 <                     Vector3d &f1) {
292 <
293 <    if (!initialized_) initialize();
242 >  void LJ::calcForce(InteractionData &idat) {
243      
244 <    RealType ros;
296 <    RealType rcos;
297 <    RealType myPot = 0.0;
298 <    RealType myPotC = 0.0;
299 <    RealType myDeriv = 0.0;
300 <    RealType myDerivC = 0.0;
244 >    if (!initialized_) initialize();
245  
246 <    std::pair<AtomType*, AtomType*> key = std::make_pair(at1, at2);
247 <    LJInteractionData mixer = MixingMap[key];
304 <
305 <    RealType sigmai = mixer.sigmai;
306 <    RealType epsilon = mixer.epsilon;
246 >    map<pair<AtomType*, AtomType*>, LJInteractionData>::iterator it;
247 >    it = MixingMap.find( idat.atypes );
248      
249 <    ros = rij * sigmai;
250 <
251 <    getLJfunc(ros, myPot, myDeriv);
249 >    if (it != MixingMap.end())  {
250 >      
251 >      LJInteractionData mixer = (*it).second;
252 >      
253 >      RealType sigmai = mixer.sigmai;
254 >      RealType epsilon = mixer.epsilon;
255 >      
256 >      RealType ros;
257 >      RealType rcos;
258 >      RealType myPot = 0.0;
259 >      RealType myPotC = 0.0;
260 >      RealType myDeriv = 0.0;
261 >      RealType myDerivC = 0.0;
262 >    
263 >      ros = *(idat.rij) * sigmai;    
264 >      
265 >      getLJfunc(ros, myPot, myDeriv);
266 >      
267 >      if (idat.shiftedPot) {
268 >        rcos = *(idat.rcut) * sigmai;
269 >        getLJfunc(rcos, myPotC, myDerivC);
270 >        myDerivC = 0.0;
271 >      } else if (idat.shiftedForce) {
272 >        rcos = *(idat.rcut) * sigmai;
273 >        getLJfunc(rcos, myPotC, myDerivC);
274 >        myPotC = myPotC + myDerivC * (*(idat.rij) - *(idat.rcut)) * sigmai;
275 >      } else {
276 >        myPotC = 0.0;
277 >        myDerivC = 0.0;        
278 >      }
279  
280 <    if (shiftedPot_) {
281 <      rcos = rcut * sigmai;
282 <      getLJfunc(rcos, myPotC, myDerivC);
283 <      myDerivC = 0.0;
284 <    } else if (LJ::shiftedFrc_) {
317 <      rcos = rcut * sigmai;
318 <      getLJfunc(rcos, myPotC, myDerivC);
319 <      myPotC = myPotC + myDerivC * (rij - rcut) * sigmai;
320 <    } else {
321 <      myPotC = 0.0;
322 <      myDerivC = 0.0;
323 <    }
280 >      RealType pot_temp = *(idat.vdwMult) * epsilon * (myPot - myPotC);
281 >      *(idat.vpair) += pot_temp;
282 >      
283 >      RealType dudr = *(idat.sw) * *(idat.vdwMult) * epsilon * (myDeriv -
284 >                                                                myDerivC)*sigmai;      
285  
286 <    RealType pot_temp = vdwMult * epsilon * (myPot - myPotC);
287 <    vpair += pot_temp;
288 <
328 <    RealType dudr = sw * vdwMult * epsilon * (myDeriv - myDerivC)*sigmai;
329 <    
330 <    pot += sw * pot_temp;
331 <    f1 = d * dudr / rij;
332 <
286 >      (*(idat.pot))[VANDERWAALS_FAMILY] += *(idat.sw) * pot_temp;
287 >      *(idat.f1) += *(idat.d) * dudr / *(idat.rij);
288 >    }
289      return;
290    }
335
336  void LJ::do_lj_pair(int *atid1, int *atid2, RealType *d, RealType *rij,
337                      RealType *r2, RealType *rcut, RealType *sw,
338                      RealType *vdwMult,
339                      RealType *vpair, RealType *pot, RealType *f1) {
340
341    if (!initialized_) initialize();
342    
343    AtomType* atype1 = LJMap[*atid1];
344    AtomType* atype2 = LJMap[*atid2];
345    
346    Vector3d disp(d[0], d[1], d[2]);
347    Vector3d frc(f1[0], f1[1], f1[2]);
348    
349    calcForce(atype1, atype2, disp, *rij, *r2, *rcut, *sw, *vdwMult, *vpair,
350              *pot, frc);
351      
352    f1[0] = frc.x();
353    f1[1] = frc.y();
354    f1[2] = frc.z();
355    
356    return;    
357  }
291    
292    void LJ::getLJfunc(RealType r, RealType &pot, RealType &deriv) {
293  
# Line 371 | Line 304 | namespace OpenMD {
304      return;
305    }
306    
307 <
308 <  void LJ::setLJDefaultCutoff(RealType *thisRcut, int *sP, int *sF) {
309 <    shiftedPot_ = (bool)(*sP);
310 <    shiftedFrc_ = (bool)(*sF);
307 >  RealType LJ::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) {
308 >    if (!initialized_) initialize();  
309 >    map<pair<AtomType*, AtomType*>, LJInteractionData>::iterator it;
310 >    it = MixingMap.find(atypes);
311 >    if (it == MixingMap.end())
312 >      return 0.0;
313 >    else  {
314 >      LJInteractionData mixer = (*it).second;
315 >      return 2.5 * mixer.sigma;
316 >    }
317    }
379 }
318  
381 extern "C" {
382  
383 #define fortranGetSigma FC_FUNC(getsigma, GETSIGMA)
384 #define fortranGetEpsilon FC_FUNC(getepsilon, GETEPSILON)
385 #define fortranSetLJCutoff FC_FUNC(setljdefaultcutoff, SETLJDEFAULTCUTOFF)
386 #define fortranDoLJPair FC_FUNC(do_lj_pair, DO_LJ_PAIR)
387  
388  RealType fortranGetSigma(int* atid) {
389    return OpenMD::LJ::Instance()->getSigma(*atid);
390  }
391  RealType fortranGetEpsilon(int* atid) {  
392    return OpenMD::LJ::Instance()->getEpsilon(*atid);
393  }
394  void fortranSetLJCutoff(RealType *rcut, int *shiftedPot, int *shiftedFrc) {
395    return OpenMD::LJ::Instance()->setLJDefaultCutoff(rcut, shiftedPot,
396                                                      shiftedFrc);
397  }
398  void fortranDoLJPair(int *atid1, int *atid2, RealType *d, RealType *rij,
399                       RealType *r2, RealType *rcut, RealType *sw,
400                       RealType *vdwMult, RealType* vpair, RealType* pot,
401                       RealType *f1){
402    
403    return OpenMD::LJ::Instance()->do_lj_pair(atid1, atid2, d, rij, r2, rcut,
404                                              sw, vdwMult, vpair, pot, f1);
405  }
319   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines