ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/nonbonded/LJ.cpp
Revision: 2017
Committed: Tue Sep 2 18:31:44 2014 UTC (10 years, 9 months ago) by gezelter
File size: 9771 byte(s)
Log Message:
Latest

File Contents

# User Rev Content
1 gezelter 1471 /*
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. Redistributions of source code must retain the above copyright
10     * notice, this list of conditions and the following disclaimer.
11     *
12     * 2. Redistributions in binary form must reproduce the above copyright
13     * notice, this list of conditions and the following disclaimer in the
14     * documentation and/or other materials provided with the
15     * distribution.
16     *
17     * This software is provided "AS IS," without a warranty of any
18     * kind. All express or implied conditions, representations and
19     * warranties, including any implied warranty of merchantability,
20     * fitness for a particular purpose or non-infringement, are hereby
21     * excluded. The University of Notre Dame and its licensors shall not
22     * be liable for any damages suffered by licensee as a result of
23     * using, modifying or distributing the software or its
24     * derivatives. In no event will the University of Notre Dame or its
25     * licensors be liable for any lost revenue, profit or data, or for
26     * direct, indirect, special, consequential, incidental or punitive
27     * damages, however caused and regardless of the theory of liability,
28     * arising out of the use of or inability to use software, even if the
29     * University of Notre Dame has been advised of the possibility of
30     * such damages.
31     *
32     * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
33     * research, please cite the appropriate papers when you publish your
34     * work. Good starting points are:
35     *
36     * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
37     * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
38 gezelter 1879 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
39 gezelter 1665 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40     * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 gezelter 1471 */
42    
43     #include <stdio.h>
44     #include <string.h>
45    
46     #include <cmath>
47     #include "nonbonded/LJ.hpp"
48     #include "utils/simError.h"
49 gezelter 1710 #include "types/LennardJonesAdapter.hpp"
50 jmichalk 1683 #include "types/LennardJonesInteractionType.hpp"
51 gezelter 1471
52     namespace OpenMD {
53    
54 gezelter 1583 LJ::LJ() : name_("LJ"), initialized_(false), forceField_(NULL) {}
55 gezelter 1471
56 gezelter 1710 RealType LJ::getSigma(AtomType* atomType1, AtomType* atomType2) {
57 gezelter 1471
58 gezelter 1710 LennardJonesAdapter lja1 = LennardJonesAdapter(atomType1);
59     LennardJonesAdapter lja2 = LennardJonesAdapter(atomType2);
60     RealType sigma1 = lja1.getSigma();
61     RealType sigma2 = lja2.getSigma();
62 gezelter 1471
63     ForceFieldOptions& fopts = forceField_->getForceFieldOptions();
64 gezelter 1502 string DistanceMix = fopts.getDistanceMixingRule();
65 gezelter 1471 toUpper(DistanceMix);
66    
67     if (DistanceMix == "GEOMETRIC")
68     return sqrt(sigma1 * sigma2);
69     else
70     return 0.5 * (sigma1 + sigma2);
71     }
72    
73 gezelter 1710 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 gezelter 1471 return sqrt(epsilon1 * epsilon2);
80     }
81    
82     void LJ::initialize() {
83    
84 gezelter 1895 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 gezelter 1471 }
95 gezelter 1895
96     MixingMap.resize(nLJ_);
97    
98     for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {
99     if ((*at)->isLennardJones()) addType(*at);
100     }
101    
102 gezelter 1476 ForceField::NonBondedInteractionTypeContainer* nbiTypes = forceField_->getNonBondedInteractionTypes();
103 gezelter 1471 ForceField::NonBondedInteractionTypeContainer::MapTypeIterator j;
104     NonBondedInteractionType* nbt;
105 jmichalk 1683 ForceField::NonBondedInteractionTypeContainer::KeyType keys;
106 gezelter 1471
107 gezelter 1476 for (nbt = nbiTypes->beginType(j); nbt != NULL;
108     nbt = nbiTypes->nextType(j)) {
109 jmichalk 1683
110 gezelter 1471 if (nbt->isLennardJones()) {
111 jmichalk 1683 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 gezelter 1471 sprintf( painCave.errMsg,
119 jmichalk 1683 "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 gezelter 1471 painCave.severity = OPENMD_ERROR;
124     painCave.isFatal = 1;
125     simError();
126     }
127    
128 jmichalk 1683 RealType sigma = ljit->getSigma();
129     RealType epsilon = ljit->getEpsilon();
130     addExplicitInteraction(at1, at2, sigma, epsilon);
131 gezelter 1471 }
132     }
133     initialized_ = true;
134     }
135    
136    
137     void LJ::addType(AtomType* atomType){
138 gezelter 1710 LennardJonesAdapter lja1 = LennardJonesAdapter(atomType);
139    
140     RealType sigma1 = lja1.getSigma();
141     RealType epsilon1 = lja1.getEpsilon();
142 gezelter 1502
143 gezelter 1471 // add it to the map:
144 gezelter 1895 int atid = atomType->getIdent();
145     int ljtid = LJtypes.size();
146    
147     pair<set<int>::iterator,bool> ret;
148     ret = LJtypes.insert( atid );
149 gezelter 1471 if (ret.second == false) {
150     sprintf( painCave.errMsg,
151     "LJ already had a previous entry with ident %d\n",
152 gezelter 1895 atid) ;
153 gezelter 1471 painCave.severity = OPENMD_INFO;
154     painCave.isFatal = 0;
155     simError();
156     }
157 gezelter 1895
158     LJtids[atid] = ljtid;
159     MixingMap[ljtid].resize( nLJ_ );
160    
161 gezelter 1471 // Now, iterate over all known types and add to the mixing map:
162    
163 gezelter 1895 std::set<int>::iterator it;
164     for( it = LJtypes.begin(); it != LJtypes.end(); ++it) {
165 gezelter 1471
166 gezelter 1895 int ljtid2 = LJtids[ (*it) ];
167     AtomType* atype2 = forceField_->getAtomType( (*it) );
168    
169 gezelter 1471 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 gezelter 1895 MixingMap[ljtid2].resize( nLJ_ );
175 gezelter 1471
176 gezelter 1895 MixingMap[ljtid][ljtid2] = mixer;
177     if (ljtid2 != ljtid) {
178     MixingMap[ljtid2][ljtid] = mixer;
179 gezelter 1471 }
180     }
181     }
182    
183     void LJ::addExplicitInteraction(AtomType* atype1, AtomType* atype2, RealType sigma, RealType epsilon){
184    
185     LJInteractionData mixer;
186     mixer.sigma = sigma;
187     mixer.epsilon = epsilon;
188     mixer.sigmai = 1.0 / mixer.sigma;
189     mixer.explicitlySet = true;
190    
191 gezelter 1895 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 gezelter 1471
219 gezelter 1895 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 gezelter 1471 }
227     }
228    
229 gezelter 1536 void LJ::calcForce(InteractionData &idat) {
230 gezelter 1471 if (!initialized_) initialize();
231    
232 gezelter 1895 LJInteractionData &mixer = MixingMap[LJtids[idat.atid1]][LJtids[idat.atid2]];
233 gezelter 1473
234 gezelter 1895 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;
243    
244     ros = *(idat.rij) * sigmai;
245    
246     getLJfunc(ros, myPot, myDeriv);
247    
248     if (idat.shiftedPot) {
249     rcos = *(idat.rcut) * sigmai;
250     getLJfunc(rcos, myPotC, myDerivC);
251     myDerivC = 0.0;
252     } else if (idat.shiftedForce) {
253     rcos = *(idat.rcut) * sigmai;
254     getLJfunc(rcos, myPotC, myDerivC);
255     myPotC = myPotC + myDerivC * (*(idat.rij) - *(idat.rcut)) * sigmai;
256     } else {
257     myPotC = 0.0;
258     myDerivC = 0.0;
259 gezelter 1471 }
260 gezelter 1895
261     RealType pot_temp = *(idat.vdwMult) * epsilon * (myPot - myPotC);
262     *(idat.vpair) += pot_temp;
263    
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 gezelter 1471 return;
270     }
271    
272 gezelter 1472 void LJ::getLJfunc(RealType r, RealType &pot, RealType &deriv) {
273    
274 gezelter 1471 RealType ri = 1.0 / r;
275     RealType ri2 = ri * ri;
276     RealType ri6 = ri2 * ri2 * ri2;
277     RealType ri7 = ri6 * ri;
278     RealType ri12 = ri6 * ri6;
279     RealType ri13 = ri12 * ri;
280 gezelter 1472
281 gezelter 1471 pot = 4.0 * (ri12 - ri6);
282     deriv = 24.0 * (ri7 - 2.0 * ri13);
283 gezelter 1472
284 gezelter 1471 return;
285     }
286 gezelter 1472
287 gezelter 1545 RealType LJ::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) {
288 gezelter 1505 if (!initialized_) initialize();
289 gezelter 1895
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 gezelter 1505 return 2.5 * mixer.sigma;
299     }
300     }
301 gezelter 1895
302 gezelter 1471 }

Properties

Name Value
svn:eol-style native