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 1502 by gezelter, Sat Oct 2 19:53:32 2010 UTC vs.
trunk/src/nonbonded/LJ.cpp (file contents), Revision 2071 by gezelter, Sat Mar 7 21:41:51 2015 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  
52   namespace OpenMD {
53  
54 <  LJ::LJ() : name_("LJ"), initialized_(false), shiftedPot_(false),
52 <             shiftedFrc_(false), forceField_(NULL) {}
54 >  LJ::LJ() : initialized_(false), forceField_(NULL), name_("LJ") {}
55  
56 <  LJParam LJ::getLJParam(AtomType* atomType) {
55 <    
56 <    // Do sanity checking on the AtomType we were passed before
57 <    // building any data structures:
58 <    if (!atomType->isLennardJones()) {
59 <      sprintf( painCave.errMsg,
60 <               "LJ::getLJParam was passed an atomType (%s) that does not\n"
61 <               "\tappear to be a Lennard-Jones atom.\n",
62 <               atomType->getName().c_str());
63 <      painCave.severity = OPENMD_ERROR;
64 <      painCave.isFatal = 1;
65 <      simError();
66 <    }
67 <    
68 <    GenericData* data = atomType->getPropertyByName("LennardJones");
69 <    if (data == NULL) {
70 <      sprintf( painCave.errMsg, "LJ::getLJParam could not find Lennard-Jones\n"
71 <               "\tparameters for atomType %s.\n", atomType->getName().c_str());
72 <      painCave.severity = OPENMD_ERROR;
73 <      painCave.isFatal = 1;
74 <      simError();
75 <    }
76 <    
77 <    LJParamGenericData* ljData = dynamic_cast<LJParamGenericData*>(data);
78 <    if (ljData == NULL) {
79 <      sprintf( painCave.errMsg,
80 <               "LJ::getLJParam could not convert GenericData to LJParam for\n"
81 <               "\tatom type %s\n", atomType->getName().c_str());
82 <      painCave.severity = OPENMD_ERROR;
83 <      painCave.isFatal = 1;
84 <      simError();          
85 <    }
86 <    
87 <    return ljData->getData();
88 <  }
56 >  RealType LJ::getSigma(AtomType* atomType1, AtomType* atomType2) {
57  
58 <  RealType LJ::getSigma(AtomType* atomType) {    
59 <    LJParam ljParam = getLJParam(atomType);
60 <    return ljParam.sigma;
61 <  }
94 <
95 <  RealType LJ::getSigma(AtomType* atomType1, AtomType* atomType2) {    
96 <    RealType sigma1 = getSigma(atomType1);
97 <    RealType sigma2 = getSigma(atomType2);
58 >    LennardJonesAdapter lja1 = LennardJonesAdapter(atomType1);
59 >    LennardJonesAdapter lja2 = LennardJonesAdapter(atomType2);
60 >    RealType sigma1 = lja1.getSigma();
61 >    RealType sigma2 = lja2.getSigma();
62      
63      ForceFieldOptions& fopts = forceField_->getForceFieldOptions();
64      string DistanceMix = fopts.getDistanceMixingRule();
# Line 106 | 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;
76 <  }
77 <
78 <  RealType LJ::getEpsilon(AtomType* atomType1, AtomType* atomType2) {    
115 <    RealType epsilon1 = getEpsilon(atomType1);
116 <    RealType epsilon2 = getEpsilon(atomType2);
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    void LJ::initialize() {    
121    ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes();
122    ForceField::AtomTypeContainer::MapTypeIterator i;
123    AtomType* at;
83  
84 <    for (at = atomTypes->beginType(i); at != NULL;
85 <         at = atomTypes->nextType(i)) {
86 <      
87 <      if (at->isLennardJones())
88 <        addType(at);
84 >    LJtypes.clear();
85 >    LJtids.clear();
86 >    MixingMap.clear();
87 >    nLJ_ = 0;
88 >
89 >    LJtids.resize( forceField_->getNAtomType(), -1);
90 >
91 >    set<AtomType*>::iterator at;
92 >    for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {
93 >      if ((*at)->isLennardJones()) nLJ_++;
94      }
95  
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 <        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",
147 <                   atypes.first->getName().c_str(),
148 <                   atypes.second->getName().c_str());
149 <          painCave.severity = OPENMD_ERROR;
150 <          painCave.isFatal = 1;
151 <          simError();
152 <        }
153 <    
154 <        LJParamGenericData* ljData = dynamic_cast<LJParamGenericData*>(data);
155 <        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          }
165        
166        LJParam ljParam = ljData->getData();
127  
128 <        RealType sigma = ljParam.sigma;
129 <        RealType epsilon = ljParam.epsilon;
130 <
171 <        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    }
176      
135  
136  
137    void LJ::addType(AtomType* atomType){
180    RealType sigma1 = getSigma(atomType);
181    RealType epsilon1 = getEpsilon(atomType);
138      
139      // add it to the map:
140 <    AtomTypeProperties atp = atomType->getATP();    
141 <
142 <    pair<map<int,AtomType*>::iterator,bool> ret;    
143 <    ret = LJMap.insert( pair<int, AtomType*>(atp.ident, atomType) );
140 >    int atid = atomType->getIdent();
141 >    int ljtid = LJtypes.size();
142 >  
143 >    pair<set<int>::iterator,bool> ret;    
144 >    ret = LJtypes.insert( atid );
145      if (ret.second == false) {
146        sprintf( painCave.errMsg,
147                 "LJ already had a previous entry with ident %d\n",
148 <               atp.ident);
148 >               atid) ;
149        painCave.severity = OPENMD_INFO;
150        painCave.isFatal = 0;
151        simError();        
152      }
153 <    
153 >
154 >    LJtids[atid] = ljtid;
155 >    MixingMap[ljtid].resize( nLJ_ );
156 >
157      // Now, iterate over all known types and add to the mixing map:
158      
159 <    std::map<int, AtomType*>::iterator it;
160 <    for( it = LJMap.begin(); it != LJMap.end(); ++it) {
201 <      
202 <      AtomType* atype2 = (*it).second;
159 >    std::set<int>::iterator it;
160 >    for( it = LJtypes.begin(); it != LJtypes.end(); ++it) {
161  
162 +      int ljtid2 = LJtids[ (*it) ];
163 +      AtomType* atype2 = forceField_->getAtomType( (*it) );
164 +
165        LJInteractionData mixer;
166        mixer.sigma = getSigma(atomType, atype2);
167        mixer.epsilon = getEpsilon(atomType, atype2);
168        mixer.sigmai = 1.0 / mixer.sigma;
169        mixer.explicitlySet = false;
170 +      MixingMap[ljtid2].resize( nLJ_ );
171  
172 <      std::pair<AtomType*, AtomType*> key1, key2;
173 <      key1 = std::make_pair(atomType, atype2);
174 <      key2 = std::make_pair(atype2, atomType);
213 <      
214 <      MixingMap[key1] = mixer;
215 <      if (key2 != key1) {
216 <        MixingMap[key2] = mixer;
172 >      MixingMap[ljtid][ljtid2] = mixer;
173 >      if (ljtid2 != ljtid) {
174 >        MixingMap[ljtid2][ljtid] = mixer;
175        }
176      }      
177    }
178    
179 <  void LJ::addExplicitInteraction(AtomType* atype1, AtomType* atype2, RealType sigma, RealType epsilon){
179 >  void LJ::addExplicitInteraction(AtomType* atype1, AtomType* atype2,
180 >                                  RealType sigma, RealType epsilon){
181      
223    // in case these weren't already in the map
224    addType(atype1);
225    addType(atype2);
226
182      LJInteractionData mixer;
183      mixer.sigma = sigma;
184      mixer.epsilon = epsilon;
185      mixer.sigmai = 1.0 / mixer.sigma;
186      mixer.explicitlySet = true;
187  
188 <    std::pair<AtomType*, AtomType*> key1, key2;
189 <    key1 = std::make_pair(atype1, atype2);
190 <    key2 = std::make_pair(atype2, atype1);
188 >    int atid1 = atype1->getIdent();
189 >    int atid2 = atype2->getIdent();
190 >
191 >    int ljtid1, ljtid2;
192 >
193 >    pair<set<int>::iterator,bool> ret;    
194 >    ret = LJtypes.insert( atid1 );
195 >    if (ret.second == false) {
196 >      // already had this type in the LJMap, just get the ljtid:
197 >      ljtid1 = LJtids[ atid1 ];
198 >    } else {
199 >      // didn't already have it, so make a new one and assign it:
200 >      ljtid1 = nLJ_;
201 >      LJtids[atid1] = nLJ_;
202 >      nLJ_++;
203 >    }
204 >
205 >    ret = LJtypes.insert( atid2 );
206 >    if (ret.second == false) {
207 >      // already had this type in the LJMap, just get the ljtid:
208 >      ljtid2 = LJtids[ atid2 ];
209 >    } else {
210 >      // didn't already have it, so make a new one and assign it:
211 >      ljtid2 = nLJ_;
212 >      LJtids[atid2] = nLJ_;
213 >      nLJ_++;
214 >    }
215      
216 <    MixingMap[key1] = mixer;
217 <    if (key2 != key1) {
218 <      MixingMap[key2] = mixer;
216 >    MixingMap.resize(nLJ_);
217 >    MixingMap[ljtid1].resize(nLJ_);
218 >
219 >    MixingMap[ljtid1][ljtid2] = mixer;
220 >    if (ljtid2 != ljtid1) {
221 >      MixingMap[ljtid2].resize(nLJ_);
222 >      MixingMap[ljtid2][ljtid1] = mixer;
223      }    
224    }
225  
226 <  void LJ::calcForce(InteractionData idat) {
244 <    
226 >  void LJ::calcForce(InteractionData &idat) {
227      if (!initialized_) initialize();
228      
229 +    LJInteractionData &mixer = MixingMap[LJtids[idat.atid1]][LJtids[idat.atid2]];
230 +
231 +    RealType sigmai = mixer.sigmai;
232 +    RealType epsilon = mixer.epsilon;
233 +    
234      RealType ros;
235      RealType rcos;
236      RealType myPot = 0.0;
237      RealType myPotC = 0.0;
238      RealType myDeriv = 0.0;
239      RealType myDerivC = 0.0;
253
254    std::pair<AtomType*, AtomType*> key = std::make_pair(idat.atype1,
255                                                         idat.atype2);
256    LJInteractionData mixer = MixingMap[key];
257
258    RealType sigmai = mixer.sigmai;
259    RealType epsilon = mixer.epsilon;
240      
241 <
242 <    ros = idat.rij * sigmai;
263 <
241 >    ros = *(idat.rij) * sigmai;    
242 >    
243      getLJfunc(ros, myPot, myDeriv);
244 <
245 <    if (shiftedPot_) {
246 <      rcos = idat.rcut * sigmai;
244 >    
245 >    if (idat.shiftedPot) {
246 >      rcos = *(idat.rcut) * sigmai;
247        getLJfunc(rcos, myPotC, myDerivC);
248        myDerivC = 0.0;
249 <    } else if (LJ::shiftedFrc_) {
250 <      rcos = idat.rcut * sigmai;
249 >    } else if (idat.shiftedForce) {
250 >      rcos = *(idat.rcut) * sigmai;
251        getLJfunc(rcos, myPotC, myDerivC);
252 <      myPotC = myPotC + myDerivC * (idat.rij - idat.rcut) * sigmai;
252 >      myPotC = myPotC + myDerivC * (*(idat.rij) - *(idat.rcut)) * sigmai;
253      } else {
254        myPotC = 0.0;
255 <      myDerivC = 0.0;
255 >      myDerivC = 0.0;        
256      }
278
279    RealType pot_temp = idat.vdwMult * epsilon * (myPot - myPotC);
280    idat.vpair += pot_temp;
281
282    RealType dudr = idat.sw * idat.vdwMult * epsilon * (myDeriv -
283                                                        myDerivC)*sigmai;
257      
258 <    idat.pot += idat.sw * pot_temp;
259 <    idat.f1 = idat.d * dudr / idat.rij;
260 <
258 >    RealType pot_temp = *(idat.vdwMult) * epsilon * (myPot - myPotC);
259 >    *(idat.vpair) += pot_temp;
260 >    
261 >    RealType dudr = *(idat.sw) * *(idat.vdwMult) * epsilon * (myDeriv -
262 >                                                              myDerivC)*sigmai;      
263 >    (*(idat.pot))[VANDERWAALS_FAMILY] += *(idat.sw) * pot_temp;
264 >    *(idat.f1) += *(idat.d) * dudr / *(idat.rij);
265 >    
266      return;
289
267    }
268    
269    void LJ::getLJfunc(RealType r, RealType &pot, RealType &deriv) {
# Line 304 | Line 281 | namespace OpenMD {
281      return;
282    }
283    
284 <
284 >  RealType LJ::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) {
285 >    if (!initialized_) initialize();  
286 >    
287 >    int atid1 = atypes.first->getIdent();
288 >    int atid2 = atypes.second->getIdent();
289 >    int ljtid1 = LJtids[atid1];
290 >    int ljtid2 = LJtids[atid2];
291 >    
292 >    if (ljtid1 == -1 || ljtid2 == -1) return 0.0;
293 >    else {      
294 >      LJInteractionData mixer = MixingMap[ljtid1][ljtid2];
295 >      return 2.5 * mixer.sigma;
296 >    }
297 >  }
298 >  
299   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines