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

Comparing:
branches/development/src/nonbonded/LJ.cpp (file contents), Revision 1476 by gezelter, Wed Jul 21 18:31:05 2010 UTC vs.
trunk/src/nonbonded/LJ.cpp (file contents), Revision 2017 by gezelter, Tue Sep 2 18:31:44 2014 UTC

# Line 35 | Line 35
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).                        
38 > * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).          
39 > * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010).
40 > * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41   */
42  
43   #include <stdio.h>
# Line 45 | Line 46
46   #include <cmath>
47   #include "nonbonded/LJ.hpp"
48   #include "utils/simError.h"
49 + #include "types/LennardJonesAdapter.hpp"
50 + #include "types/LennardJonesInteractionType.hpp"
51  
49
52   namespace OpenMD {
53  
54 <  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;
54 >  LJ::LJ() : name_("LJ"), initialized_(false), forceField_(NULL) {}
55  
56 <  LJ* LJ::Instance() {
62 <    if (!_instance) {
63 <      _instance = new LJ();
64 <    }
65 <    return _instance;
66 <  }
56 >  RealType LJ::getSigma(AtomType* atomType1, AtomType* atomType2) {
57  
58 <  LJParam LJ::getLJParam(AtomType* atomType) {
58 >    LennardJonesAdapter lja1 = LennardJonesAdapter(atomType1);
59 >    LennardJonesAdapter lja2 = LennardJonesAdapter(atomType2);
60 >    RealType sigma1 = lja1.getSigma();
61 >    RealType sigma2 = lja2.getSigma();
62      
70    // Do sanity checking on the AtomType we were passed before
71    // building any data structures:
72    if (!atomType->isLennardJones()) {
73      sprintf( painCave.errMsg,
74               "LJ::getLJParam was passed an atomType (%s) that does not\n"
75               "\tappear to be a Lennard-Jones atom.\n",
76               atomType->getName().c_str());
77      painCave.severity = OPENMD_ERROR;
78      painCave.isFatal = 1;
79      simError();
80    }
81    
82    GenericData* data = atomType->getPropertyByName("LennardJones");
83    if (data == NULL) {
84      sprintf( painCave.errMsg, "LJ::getLJParam could not find Lennard-Jones\n"
85               "\tparameters for atomType %s.\n", atomType->getName().c_str());
86      painCave.severity = OPENMD_ERROR;
87      painCave.isFatal = 1;
88      simError();
89    }
90    
91    LJParamGenericData* ljData = dynamic_cast<LJParamGenericData*>(data);
92    if (ljData == NULL) {
93      sprintf( painCave.errMsg,
94               "LJ::getLJParam could not convert GenericData to LJParam for\n"
95               "\tatom type %s\n", atomType->getName().c_str());
96      painCave.severity = OPENMD_ERROR;
97      painCave.isFatal = 1;
98      simError();          
99    }
100    
101    return ljData->getData();
102  }
103
104  RealType LJ::getSigma(AtomType* atomType) {    
105    LJParam ljParam = getLJParam(atomType);
106    return ljParam.sigma;
107  }
108
109  RealType LJ::getSigma(int atid) {
110    if (!initialized_) initialize();
111    std::map<int, AtomType*> :: const_iterator it;
112    it = LJMap.find(atid);
113    if (it == LJMap.end()) {
114      sprintf( painCave.errMsg,
115               "LJ::getSigma could not find atid %d in LJMap\n",
116               (atid));
117      painCave.severity = OPENMD_ERROR;
118      painCave.isFatal = 1;
119      simError();          
120    }
121    AtomType* atype = it->second;
122
123    return getSigma(atype);    
124  }
125
126  RealType LJ::getSigma(AtomType* atomType1, AtomType* atomType2) {    
127    RealType sigma1 = getSigma(atomType1);
128    RealType sigma2 = getSigma(atomType2);
129    
63      ForceFieldOptions& fopts = forceField_->getForceFieldOptions();
64 <    std::string DistanceMix = fopts.getDistanceMixingRule();
64 >    string DistanceMix = fopts.getDistanceMixingRule();
65      toUpper(DistanceMix);
66  
67      if (DistanceMix == "GEOMETRIC")
# Line 137 | Line 70 | namespace OpenMD {
70        return 0.5 * (sigma1 + sigma2);
71    }
72  
73 <  RealType LJ::getEpsilon(AtomType* atomType) {    
74 <    LJParam ljParam = getLJParam(atomType);
75 <    return ljParam.epsilon;
73 >  RealType LJ::getEpsilon(AtomType* atomType1, AtomType* atomType2) {  
74 >    LennardJonesAdapter lja1 = LennardJonesAdapter(atomType1);
75 >    LennardJonesAdapter lja2 = LennardJonesAdapter(atomType2);
76 >  
77 >    RealType epsilon1 = lja1.getEpsilon();
78 >    RealType epsilon2 = lja2.getEpsilon();
79 >    return sqrt(epsilon1 * epsilon2);
80    }
81  
82 <  RealType LJ::getEpsilon(int atid) {    
146 <    if (!initialized_) initialize();
147 <    std::map<int, AtomType*> :: const_iterator it;
148 <    it = LJMap.find(atid);
149 <    if (it == LJMap.end()) {
150 <      sprintf( painCave.errMsg,
151 <               "LJ::getEpsilon could not find atid %d in LJMap\n",
152 <               (atid));
153 <      painCave.severity = OPENMD_ERROR;
154 <      painCave.isFatal = 1;
155 <      simError();          
156 <    }
157 <    AtomType* atype = it->second;
82 >  void LJ::initialize() {    
83  
84 <    return getEpsilon(atype);    
85 <  }
84 >    LJtypes.clear();
85 >    LJtids.clear();
86 >    MixingMap.clear();
87 >    nLJ_ = 0;
88  
89 <  RealType LJ::getEpsilon(AtomType* atomType1, AtomType* atomType2) {    
163 <    RealType epsilon1 = getEpsilon(atomType1);
164 <    RealType epsilon2 = getEpsilon(atomType2);
165 <    return sqrt(epsilon1 * epsilon2);
166 <  }
89 >    LJtids.resize( forceField_->getNAtomType(), -1);
90  
91 <  void LJ::initialize() {    
92 <    ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes();
93 <    ForceField::AtomTypeContainer::MapTypeIterator i;
94 <    AtomType* at;
91 >    set<AtomType*>::iterator at;
92 >    for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {
93 >      if ((*at)->isLennardJones()) nLJ_++;
94 >    }
95  
96 <    for (at = atomTypes->beginType(i); at != NULL;
97 <         at = atomTypes->nextType(i)) {
98 <      
99 <      if (at->isLennardJones())
177 <        addType(at);
96 >    MixingMap.resize(nLJ_);
97 >
98 >    for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {
99 >      if ((*at)->isLennardJones()) addType(*at);      
100      }
101  
102      ForceField::NonBondedInteractionTypeContainer* nbiTypes = forceField_->getNonBondedInteractionTypes();
103      ForceField::NonBondedInteractionTypeContainer::MapTypeIterator j;
104      NonBondedInteractionType* nbt;
105 +    ForceField::NonBondedInteractionTypeContainer::KeyType keys;
106  
107      for (nbt = nbiTypes->beginType(j); nbt != NULL;
108           nbt = nbiTypes->nextType(j)) {
109 <      
109 >
110        if (nbt->isLennardJones()) {
111 <        
112 <        std::pair<AtomType*, AtomType*> atypes = nbt->getAtomTypes();
113 <        
114 <        GenericData* data = nbt->getPropertyByName("LennardJones");
115 <        if (data == NULL) {
116 <          sprintf( painCave.errMsg, "LJ::rebuildMixingMap could not find\n"
117 <               "\tLennard-Jones parameters for %s - %s interaction.\n",
195 <                   atypes.first->getName().c_str(),
196 <                   atypes.second->getName().c_str());
197 <          painCave.severity = OPENMD_ERROR;
198 <          painCave.isFatal = 1;
199 <          simError();
200 <        }
201 <    
202 <        LJParamGenericData* ljData = dynamic_cast<LJParamGenericData*>(data);
203 <        if (ljData == NULL) {
111 >        keys = nbiTypes->getKeys(j);
112 >        AtomType* at1 = forceField_->getAtomType(keys[0]);
113 >        AtomType* at2 = forceField_->getAtomType(keys[1]);
114 >
115 >        LennardJonesInteractionType* ljit = dynamic_cast<LennardJonesInteractionType*>(nbt);
116 >
117 >        if (ljit == NULL) {
118            sprintf( painCave.errMsg,
119 <                   "LJ::rebuildMixingMap could not convert GenericData to\n"
120 <                   "\tLJParam for %s - %s interaction.\n",
121 <                   atypes.first->getName().c_str(),
122 <                   atypes.second->getName().c_str());
119 >                   "LJ::initialize could not convert NonBondedInteractionType\n"
120 >                   "\tto LennardJonesInteractionType for %s - %s interaction.\n",
121 >                   at1->getName().c_str(),
122 >                   at2->getName().c_str());
123            painCave.severity = OPENMD_ERROR;
124            painCave.isFatal = 1;
125            simError();          
126          }
213        
214        LJParam ljParam = ljData->getData();
127  
128 <        RealType sigma = ljParam.sigma;
129 <        RealType epsilon = ljParam.epsilon;
130 <
219 <        addExplicitInteraction(atypes.first, atypes.second, sigma, epsilon);
128 >        RealType sigma = ljit->getSigma();
129 >        RealType epsilon = ljit->getEpsilon();
130 >        addExplicitInteraction(at1, at2, sigma, epsilon);
131        }
132      }  
133      initialized_ = true;
134    }
224      
135  
136  
137    void LJ::addType(AtomType* atomType){
138 <    RealType sigma1 = getSigma(atomType);
229 <    RealType epsilon1 = getEpsilon(atomType);
138 >    LennardJonesAdapter lja1 = LennardJonesAdapter(atomType);
139  
140 +    RealType sigma1 = lja1.getSigma();
141 +    RealType epsilon1 = lja1.getEpsilon();
142 +    
143      // add it to the map:
144 <    AtomTypeProperties atp = atomType->getATP();    
145 <
146 <    std::pair<std::map<int,AtomType*>::iterator,bool> ret;    
147 <    ret = LJMap.insert( std::pair<int, AtomType*>(atp.ident, atomType) );
144 >    int atid = atomType->getIdent();
145 >    int ljtid = LJtypes.size();
146 >  
147 >    pair<set<int>::iterator,bool> ret;    
148 >    ret = LJtypes.insert( atid );
149      if (ret.second == false) {
150        sprintf( painCave.errMsg,
151                 "LJ already had a previous entry with ident %d\n",
152 <               atp.ident);
152 >               atid) ;
153        painCave.severity = OPENMD_INFO;
154        painCave.isFatal = 0;
155        simError();        
156      }
157 <    
157 >
158 >    LJtids[atid] = ljtid;
159 >    MixingMap[ljtid].resize( nLJ_ );
160 >
161      // Now, iterate over all known types and add to the mixing map:
162      
163 <    std::map<int, AtomType*>::iterator it;
164 <    for( it = LJMap.begin(); it != LJMap.end(); ++it) {
249 <      
250 <      AtomType* atype2 = (*it).second;
163 >    std::set<int>::iterator it;
164 >    for( it = LJtypes.begin(); it != LJtypes.end(); ++it) {
165  
166 +      int ljtid2 = LJtids[ (*it) ];
167 +      AtomType* atype2 = forceField_->getAtomType( (*it) );
168 +
169        LJInteractionData mixer;
170        mixer.sigma = getSigma(atomType, atype2);
171        mixer.epsilon = getEpsilon(atomType, atype2);
172        mixer.sigmai = 1.0 / mixer.sigma;
173        mixer.explicitlySet = false;
174 +      MixingMap[ljtid2].resize( nLJ_ );
175  
176 <      std::pair<AtomType*, AtomType*> key1, key2;
177 <      key1 = std::make_pair(atomType, atype2);
178 <      key2 = std::make_pair(atype2, atomType);
261 <      
262 <      MixingMap[key1] = mixer;
263 <      if (key2 != key1) {
264 <        MixingMap[key2] = mixer;
176 >      MixingMap[ljtid][ljtid2] = mixer;
177 >      if (ljtid2 != ljtid) {
178 >        MixingMap[ljtid2][ljtid] = mixer;
179        }
180      }      
181    }
182    
183    void LJ::addExplicitInteraction(AtomType* atype1, AtomType* atype2, RealType sigma, RealType epsilon){
184      
271    // in case these weren't already in the map
272    addType(atype1);
273    addType(atype2);
274
185      LJInteractionData mixer;
186      mixer.sigma = sigma;
187      mixer.epsilon = epsilon;
188      mixer.sigmai = 1.0 / mixer.sigma;
189      mixer.explicitlySet = true;
190  
191 <    std::pair<AtomType*, AtomType*> key1, key2;
192 <    key1 = std::make_pair(atype1, atype2);
193 <    key2 = std::make_pair(atype2, atype1);
191 >    int atid1 = atype1->getIdent();
192 >    int atid2 = atype2->getIdent();
193 >
194 >    int ljtid1, ljtid2;
195 >
196 >    pair<set<int>::iterator,bool> ret;    
197 >    ret = LJtypes.insert( atid1 );
198 >    if (ret.second == false) {
199 >      // already had this type in the LJMap, just get the ljtid:
200 >      ljtid1 = LJtids[ atid1 ];
201 >    } else {
202 >      // didn't already have it, so make a new one and assign it:
203 >      ljtid1 = nLJ_;
204 >      LJtids[atid1] = nLJ_;
205 >      nLJ_++;
206 >    }
207 >
208 >    ret = LJtypes.insert( atid2 );
209 >    if (ret.second == false) {
210 >      // already had this type in the LJMap, just get the ljtid:
211 >      ljtid2 = LJtids[ atid2 ];
212 >    } else {
213 >      // didn't already have it, so make a new one and assign it:
214 >      ljtid2 = nLJ_;
215 >      LJtids[atid2] = nLJ_;
216 >      nLJ_++;
217 >    }
218      
219 <    MixingMap[key1] = mixer;
220 <    if (key2 != key1) {
221 <      MixingMap[key2] = mixer;
219 >    MixingMap.resize(nLJ_);
220 >    MixingMap[ljtid1].resize(nLJ_);
221 >
222 >    MixingMap[ljtid1][ljtid2] = mixer;
223 >    if (ljtid2 != ljtid1) {
224 >      MixingMap[ljtid2].resize(nLJ_);
225 >      MixingMap[ljtid2][ljtid1] = mixer;
226      }    
227    }
228  
229 <  void LJ::calcForce(AtomType* at1, AtomType* at2, Vector3d d,
292 <                     RealType rij, RealType r2, RealType rcut, RealType sw,
293 <                     RealType vdwMult, RealType &vpair, RealType &pot,
294 <                     Vector3d &f1) {
295 <
229 >  void LJ::calcForce(InteractionData &idat) {
230      if (!initialized_) initialize();
231      
232 +    LJInteractionData &mixer = MixingMap[LJtids[idat.atid1]][LJtids[idat.atid2]];
233 +
234 +    RealType sigmai = mixer.sigmai;
235 +    RealType epsilon = mixer.epsilon;
236 +    
237      RealType ros;
238      RealType rcos;
239      RealType myPot = 0.0;
240      RealType myPotC = 0.0;
241      RealType myDeriv = 0.0;
242      RealType myDerivC = 0.0;
304
305    std::pair<AtomType*, AtomType*> key = std::make_pair(at1, at2);
306    LJInteractionData mixer = MixingMap[key];
307
308    RealType sigmai = mixer.sigmai;
309    RealType epsilon = mixer.epsilon;
243      
244 <
245 <    ros = rij * sigmai;
313 <
244 >    ros = *(idat.rij) * sigmai;    
245 >    
246      getLJfunc(ros, myPot, myDeriv);
247 <
248 <    if (shiftedPot_) {
249 <      rcos = rcut * sigmai;
247 >    
248 >    if (idat.shiftedPot) {
249 >      rcos = *(idat.rcut) * sigmai;
250        getLJfunc(rcos, myPotC, myDerivC);
251        myDerivC = 0.0;
252 <    } else if (LJ::shiftedFrc_) {
253 <      rcos = rcut * sigmai;
252 >    } else if (idat.shiftedForce) {
253 >      rcos = *(idat.rcut) * sigmai;
254        getLJfunc(rcos, myPotC, myDerivC);
255 <      myPotC = myPotC + myDerivC * (rij - rcut) * sigmai;
255 >      myPotC = myPotC + myDerivC * (*(idat.rij) - *(idat.rcut)) * sigmai;
256      } else {
257        myPotC = 0.0;
258 <      myDerivC = 0.0;
258 >      myDerivC = 0.0;        
259      }
328
329    RealType pot_temp = vdwMult * epsilon * (myPot - myPotC);
330    vpair += pot_temp;
331
332    RealType dudr = sw * vdwMult * epsilon * (myDeriv - myDerivC)*sigmai;
260      
261 <    pot += sw * pot_temp;
262 <    f1 = d * dudr / rij;
336 <
337 <    return;
338 <
339 <  }
340 <
341 <  void LJ::do_lj_pair(int *atid1, int *atid2, RealType *d, RealType *rij,
342 <                      RealType *r2, RealType *rcut, RealType *sw,
343 <                      RealType *vdwMult,
344 <                      RealType *vpair, RealType *pot, RealType *f1) {
345 <
346 <    if (!initialized_) initialize();
261 >    RealType pot_temp = *(idat.vdwMult) * epsilon * (myPot - myPotC);
262 >    *(idat.vpair) += pot_temp;
263      
264 <    AtomType* atype1 = LJMap[*atid1];
265 <    AtomType* atype2 = LJMap[*atid2];
264 >    RealType dudr = *(idat.sw) * *(idat.vdwMult) * epsilon * (myDeriv -
265 >                                                              myDerivC)*sigmai;      
266 >    (*(idat.pot))[VANDERWAALS_FAMILY] += *(idat.sw) * pot_temp;
267 >    *(idat.f1) += *(idat.d) * dudr / *(idat.rij);
268      
269 <    Vector3d disp(d[0], d[1], d[2]);
352 <    Vector3d frc(f1[0], f1[1], f1[2]);
353 <    
354 <    calcForce(atype1, atype2, disp, *rij, *r2, *rcut, *sw, *vdwMult, *vpair,
355 <              *pot, frc);
356 <      
357 <    f1[0] = frc.x();
358 <    f1[1] = frc.y();
359 <    f1[2] = frc.z();
360 <
361 <    return;    
269 >    return;
270    }
271    
272    void LJ::getLJfunc(RealType r, RealType &pot, RealType &deriv) {
# Line 376 | Line 284 | namespace OpenMD {
284      return;
285    }
286    
287 <
288 <  void LJ::setLJDefaultCutoff(RealType *thisRcut, int *sP, int *sF) {
381 <    shiftedPot_ = (bool)(*sP);
382 <    shiftedFrc_ = (bool)(*sF);
383 <  }
384 < }
385 <
386 < extern "C" {
387 <  
388 < #define fortranGetSigma FC_FUNC(getsigma, GETSIGMA)
389 < #define fortranGetEpsilon FC_FUNC(getepsilon, GETEPSILON)
390 < #define fortranSetLJCutoff FC_FUNC(setljdefaultcutoff, SETLJDEFAULTCUTOFF)
391 < #define fortranDoLJPair FC_FUNC(do_lj_pair, DO_LJ_PAIR)
392 <  
393 <  RealType fortranGetSigma(int* atid) {
394 <    return OpenMD::LJ::Instance()->getSigma(*atid);
395 <  }
396 <  RealType fortranGetEpsilon(int* atid) {  
397 <    return OpenMD::LJ::Instance()->getEpsilon(*atid);
398 <  }
399 <  void fortranSetLJCutoff(RealType *rcut, int *shiftedPot, int *shiftedFrc) {
400 <    return OpenMD::LJ::Instance()->setLJDefaultCutoff(rcut, shiftedPot,
401 <                                                      shiftedFrc);
402 <  }
403 <  void fortranDoLJPair(int *atid1, int *atid2, RealType *d, RealType *rij,
404 <                       RealType *r2, RealType *rcut, RealType *sw,
405 <                       RealType *vdwMult, RealType* vpair, RealType* pot,
406 <                       RealType *f1){
287 >  RealType LJ::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) {
288 >    if (!initialized_) initialize();  
289      
290 <    return OpenMD::LJ::Instance()->do_lj_pair(atid1, atid2, d, rij, r2, rcut,
291 <                                              sw, vdwMult, vpair, pot, f1);
290 >    int atid1 = atypes.first->getIdent();
291 >    int atid2 = atypes.second->getIdent();
292 >    int ljtid1 = LJtids[atid1];
293 >    int ljtid2 = LJtids[atid2];
294 >    
295 >    if (ljtid1 == -1 || ljtid2 == -1) return 0.0;
296 >    else {      
297 >      LJInteractionData mixer = MixingMap[ljtid1][ljtid2];
298 >      return 2.5 * mixer.sigma;
299 >    }
300    }
301 +  
302   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines