OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
LJ.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45#include "nonbonded/LJ.hpp"
46
47#include <cmath>
48#include <cstdio>
49#include <cstring>
50
51#include "types/LennardJonesAdapter.hpp"
52#include "types/LennardJonesInteractionType.hpp"
53#include "utils/simError.h"
54
55namespace OpenMD {
56
57 LJ::LJ() : initialized_(false), forceField_(NULL), name_("LJ") {}
58
59 RealType LJ::getSigma(AtomType* atomType1, AtomType* atomType2) {
60 LennardJonesAdapter lja1 = LennardJonesAdapter(atomType1);
61 LennardJonesAdapter lja2 = LennardJonesAdapter(atomType2);
62 RealType sigma1 = lja1.getSigma();
63 RealType sigma2 = lja2.getSigma();
64
65 ForceFieldOptions& fopts = forceField_->getForceFieldOptions();
66 string DistanceMix = fopts.getDistanceMixingRule();
67 toUpper(DistanceMix);
68
69 if (DistanceMix == "GEOMETRIC")
70 return sqrt(sigma1 * sigma2);
71 else
72 return 0.5 * (sigma1 + sigma2);
73 }
74
75 RealType LJ::getEpsilon(AtomType* atomType1, AtomType* atomType2) {
76 LennardJonesAdapter lja1 = LennardJonesAdapter(atomType1);
77 LennardJonesAdapter lja2 = LennardJonesAdapter(atomType2);
78
79 RealType epsilon1 = lja1.getEpsilon();
80 RealType epsilon2 = lja2.getEpsilon();
81 return sqrt(epsilon1 * epsilon2);
82 }
83
84 void LJ::initialize() {
85 LJtypes.clear();
86 LJtids.clear();
87 MixingMap.clear();
88 nLJ_ = 0;
89
90 LJtids.resize(forceField_->getNAtomType(), -1);
91
92 AtomTypeSet::iterator at;
93 for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {
94 if ((*at)->isLennardJones()) nLJ_++;
95 }
96
97 MixingMap.resize(nLJ_);
98
99 for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {
100 if ((*at)->isLennardJones()) addType(*at);
101 }
102
103 ForceField::NonBondedInteractionTypeContainer* nbiTypes =
104 forceField_->getNonBondedInteractionTypes();
105 ForceField::NonBondedInteractionTypeContainer::MapTypeIterator j;
106 NonBondedInteractionType* nbt;
107 ForceField::NonBondedInteractionTypeContainer::KeyType keys;
108
109 for (nbt = nbiTypes->beginType(j); nbt != NULL;
110 nbt = nbiTypes->nextType(j)) {
111 if (nbt->isLennardJones()) {
112 keys = nbiTypes->getKeys(j);
113 keys = nbiTypes->getKeys(j);
114 AtomType* at1 = forceField_->getAtomType(keys[0]);
115 if (at1 == NULL) {
116 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
117 "LennardJones::initialize could not find AtomType %s\n"
118 "\tto for for %s - %s interaction.\n",
119 keys[0].c_str(), keys[0].c_str(), keys[1].c_str());
120 painCave.severity = OPENMD_ERROR;
121 painCave.isFatal = 1;
122 simError();
123 }
124
125 AtomType* at2 = forceField_->getAtomType(keys[1]);
126 if (at2 == NULL) {
127 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
128 "LennardJones::initialize could not find AtomType %s\n"
129 "\tfor %s - %s nonbonded interaction.\n",
130 keys[1].c_str(), keys[0].c_str(), keys[1].c_str());
131 painCave.severity = OPENMD_ERROR;
132 painCave.isFatal = 1;
133 simError();
134 }
135
136 LennardJonesInteractionType* ljit =
137 dynamic_cast<LennardJonesInteractionType*>(nbt);
138
139 if (ljit == NULL) {
140 snprintf(
141 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
142 "LJ::initialize could not convert NonBondedInteractionType\n"
143 "\tto LennardJonesInteractionType for %s - %s interaction.\n",
144 at1->getName().c_str(), at2->getName().c_str());
145 painCave.severity = OPENMD_ERROR;
146 painCave.isFatal = 1;
147 simError();
148 }
149
150 RealType sigma = ljit->getSigma();
151 RealType epsilon = ljit->getEpsilon();
152 addExplicitInteraction(at1, at2, sigma, epsilon);
153 }
154 }
155 initialized_ = true;
156 }
157
158 void LJ::addType(AtomType* atomType) {
159 // add it to the map:
160 int atid = atomType->getIdent();
161 int ljtid = LJtypes.size();
162
163 pair<set<int>::iterator, bool> ret;
164 ret = LJtypes.insert(atid);
165 if (ret.second == false) {
166 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
167 "LJ already had a previous entry with ident %d\n", atid);
168 painCave.severity = OPENMD_INFO;
169 painCave.isFatal = 0;
170 simError();
171 }
172
173 // Check to make sure the 1/sigma won't cause problems later:
174 RealType s = getSigma(atomType, atomType);
175 if (fabs(s) < std::numeric_limits<RealType>::epsilon()) {
176 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
177 "Lennard-Jones atom %s was defined with a sigma value (%f)\n"
178 "\tthat was too close to zero.",
179 atomType->getName().c_str(), s);
180 painCave.severity = OPENMD_ERROR;
181 painCave.isFatal = 1;
182 simError();
183 }
184
185 LJtids[atid] = ljtid;
186 MixingMap[ljtid].resize(nLJ_);
187
188 // Now, iterate over all known types and add to the mixing map:
189
190 std::set<int>::iterator it;
191 for (it = LJtypes.begin(); it != LJtypes.end(); ++it) {
192 int ljtid2 = LJtids[(*it)];
193 AtomType* atype2 = forceField_->getAtomType((*it));
194
195 LJInteractionData mixer;
196 mixer.sigma = getSigma(atomType, atype2);
197 mixer.epsilon = getEpsilon(atomType, atype2);
198 mixer.sigmai = 1.0 / mixer.sigma;
199 mixer.explicitlySet = false;
200 MixingMap[ljtid2].resize(nLJ_);
201
202 MixingMap[ljtid][ljtid2] = mixer;
203 if (ljtid2 != ljtid) { MixingMap[ljtid2][ljtid] = mixer; }
204 }
205 }
206
207 void LJ::addExplicitInteraction(AtomType* atype1, AtomType* atype2,
208 RealType sigma, RealType epsilon) {
209 LJInteractionData mixer;
210 mixer.sigma = sigma;
211 mixer.epsilon = epsilon;
212 mixer.sigmai = 1.0 / mixer.sigma;
213 mixer.explicitlySet = true;
214
215 int atid1 = atype1->getIdent();
216 int atid2 = atype2->getIdent();
217
218 int ljtid1, ljtid2;
219
220 pair<set<int>::iterator, bool> ret;
221 ret = LJtypes.insert(atid1);
222 if (ret.second == false) {
223 // already had this type in the LJMap, just get the ljtid:
224 ljtid1 = LJtids[atid1];
225 } else {
226 // didn't already have it, so make a new one and assign it:
227 ljtid1 = nLJ_;
228 LJtids[atid1] = nLJ_;
229 nLJ_++;
230 }
231
232 ret = LJtypes.insert(atid2);
233 if (ret.second == false) {
234 // already had this type in the LJMap, just get the ljtid:
235 ljtid2 = LJtids[atid2];
236 } else {
237 // didn't already have it, so make a new one and assign it:
238 ljtid2 = nLJ_;
239 LJtids[atid2] = nLJ_;
240 nLJ_++;
241 }
242
243 MixingMap.resize(nLJ_);
244 MixingMap[ljtid1].resize(nLJ_);
245
246 MixingMap[ljtid1][ljtid2] = mixer;
247 if (ljtid2 != ljtid1) {
248 MixingMap[ljtid2].resize(nLJ_);
249 MixingMap[ljtid2][ljtid1] = mixer;
250 }
251 }
252
253 void LJ::calcForce(InteractionData& idat) {
254 if (!initialized_) initialize();
255
256 LJInteractionData& mixer =
257 MixingMap[LJtids[idat.atid1]][LJtids[idat.atid2]];
258
259 RealType sigmai = mixer.sigmai;
260 RealType epsilon = mixer.epsilon;
261
262 RealType ros;
263 RealType rcos;
264 RealType myPot = 0.0;
265 RealType myPotC = 0.0;
266 RealType myDeriv = 0.0;
267 RealType myDerivC = 0.0;
268
269 ros = idat.rij * sigmai;
270
271 getLJfunc(ros, myPot, myDeriv);
272
273 if (idat.shiftedPot) {
274 rcos = idat.rcut * sigmai;
275 getLJfunc(rcos, myPotC, myDerivC);
276 myDerivC = 0.0;
277 } else if (idat.shiftedForce) {
278 rcos = idat.rcut * sigmai;
279 getLJfunc(rcos, myPotC, myDerivC);
280 myPotC = myPotC + myDerivC * (idat.rij - idat.rcut) * sigmai;
281 } else {
282 myPotC = 0.0;
283 myDerivC = 0.0;
284 }
285
286 RealType pot_temp = idat.vdwMult * epsilon * (myPot - myPotC);
287 idat.vpair += pot_temp;
288
289 RealType dudr =
290 idat.sw * idat.vdwMult * epsilon * (myDeriv - myDerivC) * sigmai;
291 idat.pot[VANDERWAALS_FAMILY] += idat.sw * pot_temp;
292
293 if (idat.isSelected) idat.selePot[VANDERWAALS_FAMILY] += idat.sw * pot_temp;
294
295 idat.f1 += idat.d * dudr / idat.rij;
296
297 return;
298 }
299
300 void LJ::getLJfunc(RealType r, RealType& pot, RealType& deriv) {
301 RealType ri = 1.0 / r;
302 RealType ri2 = ri * ri;
303 RealType ri6 = ri2 * ri2 * ri2;
304 RealType ri7 = ri6 * ri;
305 RealType ri12 = ri6 * ri6;
306 RealType ri13 = ri12 * ri;
307
308 pot = 4.0 * (ri12 - ri6);
309 deriv = 24.0 * (ri7 - 2.0 * ri13);
310
311 return;
312 }
313
314 RealType LJ::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) {
315 if (!initialized_) initialize();
316
317 int atid1 = atypes.first->getIdent();
318 int atid2 = atypes.second->getIdent();
319 int ljtid1 = LJtids[atid1];
320 int ljtid2 = LJtids[atid2];
321
322 if (ljtid1 == -1 || ljtid2 == -1)
323 return 0.0;
324 else {
325 LJInteractionData mixer = MixingMap[ljtid1][ljtid2];
326 return 2.5 * mixer.sigma;
327 }
328 }
329
330} // namespace OpenMD
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
@ VANDERWAALS_FAMILY
Long-range dispersion and short-range pauli repulsion.