OpenMD 3.2
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 following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48#include "nonbonded/LJ.hpp"
49
50#include <cmath>
51#include <cstdio>
52#include <cstring>
53
54#include "types/LennardJonesAdapter.hpp"
55#include "types/LennardJonesInteractionType.hpp"
56#include "utils/simError.h"
57
58namespace OpenMD {
59
60 LJ::LJ() : initialized_(false), forceField_(NULL), name_("LJ") {}
61
62 RealType LJ::getSigma(AtomType* atomType1, AtomType* atomType2) {
63 LennardJonesAdapter lja1 = LennardJonesAdapter(atomType1);
64 LennardJonesAdapter lja2 = LennardJonesAdapter(atomType2);
65 RealType sigma1 = lja1.getSigma();
66 RealType sigma2 = lja2.getSigma();
67
68 ForceFieldOptions& fopts = forceField_->getForceFieldOptions();
69 string DistanceMix = fopts.getDistanceMixingRule();
70 toUpper(DistanceMix);
71
72 if (DistanceMix == "GEOMETRIC")
73 return sqrt(sigma1 * sigma2);
74 else
75 return 0.5 * (sigma1 + sigma2);
76 }
77
78 RealType LJ::getEpsilon(AtomType* atomType1, AtomType* atomType2) {
79 LennardJonesAdapter lja1 = LennardJonesAdapter(atomType1);
80 LennardJonesAdapter lja2 = LennardJonesAdapter(atomType2);
81
82 RealType epsilon1 = lja1.getEpsilon();
83 RealType epsilon2 = lja2.getEpsilon();
84 return sqrt(epsilon1 * epsilon2);
85 }
86
87 void LJ::initialize() {
88 LJtypes.clear();
89 LJtids.clear();
90 MixingMap.clear();
91 nLJ_ = 0;
92
93 LJtids.resize(forceField_->getNAtomType(), -1);
94
95 AtomTypeSet::iterator at;
96 for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {
97 if ((*at)->isLennardJones()) nLJ_++;
98 }
99
100 MixingMap.resize(nLJ_);
101
102 for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {
103 if ((*at)->isLennardJones()) addType(*at);
104 }
105
106 ForceField::NonBondedInteractionTypeContainer* nbiTypes =
107 forceField_->getNonBondedInteractionTypes();
108 ForceField::NonBondedInteractionTypeContainer::MapTypeIterator j;
109 NonBondedInteractionType* nbt;
110 ForceField::NonBondedInteractionTypeContainer::KeyType keys;
111
112 for (nbt = nbiTypes->beginType(j); nbt != NULL;
113 nbt = nbiTypes->nextType(j)) {
114 if (nbt->isLennardJones()) {
115 keys = nbiTypes->getKeys(j);
116 AtomType* at1 = forceField_->getAtomType(keys[0]);
117 if (at1 == NULL) {
118 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
119 "LennardJones::initialize could not find AtomType %s\n"
120 "\tto for for %s - %s interaction.\n",
121 keys[0].c_str(), keys[0].c_str(), keys[1].c_str());
122 painCave.severity = OPENMD_ERROR;
123 painCave.isFatal = 1;
124 simError();
125 }
126
127 AtomType* at2 = forceField_->getAtomType(keys[1]);
128 if (at2 == NULL) {
129 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
130 "LennardJones::initialize could not find AtomType %s\n"
131 "\tfor %s - %s nonbonded interaction.\n",
132 keys[1].c_str(), keys[0].c_str(), keys[1].c_str());
133 painCave.severity = OPENMD_ERROR;
134 painCave.isFatal = 1;
135 simError();
136 }
137
138 LennardJonesInteractionType* ljit =
139 dynamic_cast<LennardJonesInteractionType*>(nbt);
140
141 if (ljit == NULL) {
142 snprintf(
143 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
144 "LJ::initialize could not convert NonBondedInteractionType\n"
145 "\tto LennardJonesInteractionType for %s - %s interaction.\n",
146 at1->getName().c_str(), at2->getName().c_str());
147 painCave.severity = OPENMD_ERROR;
148 painCave.isFatal = 1;
149 simError();
150 }
151
152 RealType sigma = ljit->getSigma();
153 RealType epsilon = ljit->getEpsilon();
154 addExplicitInteraction(at1, at2, sigma, epsilon);
155 }
156 }
157 initialized_ = true;
158 }
159
160 void LJ::addType(AtomType* atomType) {
161 // add it to the map:
162 int atid = atomType->getIdent();
163 int ljtid = LJtypes.size();
164
165 pair<set<int>::iterator, bool> ret;
166 ret = LJtypes.insert(atid);
167 if (ret.second == false) {
168 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
169 "LJ already had a previous entry with ident %d\n", atid);
170 painCave.severity = OPENMD_INFO;
171 painCave.isFatal = 0;
172 simError();
173 }
174
175 // Check to make sure the 1/sigma won't cause problems later:
176 RealType s = getSigma(atomType, atomType);
177 if (fabs(s) < std::numeric_limits<RealType>::epsilon()) {
178 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
179 "Lennard-Jones atom %s was defined with a sigma value (%f)\n"
180 "\tthat was too close to zero.",
181 atomType->getName().c_str(), s);
182 painCave.severity = OPENMD_ERROR;
183 painCave.isFatal = 1;
184 simError();
185 }
186
187 LJtids[atid] = ljtid;
188 MixingMap[ljtid].resize(nLJ_);
189
190 // Now, iterate over all known types and add to the mixing map:
191
192 std::set<int>::iterator it;
193 for (it = LJtypes.begin(); it != LJtypes.end(); ++it) {
194 int ljtid2 = LJtids[(*it)];
195 AtomType* atype2 = forceField_->getAtomType((*it));
196
197 LJInteractionData mixer;
198 mixer.sigma = getSigma(atomType, atype2);
199 mixer.epsilon = getEpsilon(atomType, atype2);
200 mixer.sigmai = 1.0 / mixer.sigma;
201
202 mixer.explicitlySet = false;
203 MixingMap[ljtid2].resize(nLJ_);
204
205 MixingMap[ljtid][ljtid2] = mixer;
206 if (ljtid2 != ljtid) { MixingMap[ljtid2][ljtid] = mixer; }
207 }
208 }
209
210 void LJ::addExplicitInteraction(AtomType* atype1, AtomType* atype2,
211 RealType sigma, RealType epsilon) {
212 LJInteractionData mixer;
213 mixer.sigma = sigma;
214 mixer.epsilon = epsilon;
215 mixer.sigmai = 1.0 / mixer.sigma;
216 mixer.explicitlySet = true;
217
218 int atid1 = atype1->getIdent();
219 int atid2 = atype2->getIdent();
220
221 int ljtid1, ljtid2;
222
223 pair<set<int>::iterator, bool> ret;
224 ret = LJtypes.insert(atid1);
225 if (ret.second == false) {
226 // already had this type in the LJMap, just get the ljtid:
227 ljtid1 = LJtids[atid1];
228 } else {
229 // didn't already have it, so make a new one and assign it:
230 ljtid1 = nLJ_;
231 LJtids[atid1] = nLJ_;
232 nLJ_++;
233 }
234
235 ret = LJtypes.insert(atid2);
236 if (ret.second == false) {
237 // already had this type in the LJMap, just get the ljtid:
238 ljtid2 = LJtids[atid2];
239 } else {
240 // didn't already have it, so make a new one and assign it:
241 ljtid2 = nLJ_;
242 LJtids[atid2] = nLJ_;
243 nLJ_++;
244 }
245
246 MixingMap.resize(nLJ_);
247 MixingMap[ljtid1].resize(nLJ_);
248
249 MixingMap[ljtid1][ljtid2] = mixer;
250 if (ljtid2 != ljtid1) {
251 MixingMap[ljtid2].resize(nLJ_);
252 MixingMap[ljtid2][ljtid1] = mixer;
253 }
254 }
255
256 void LJ::calcForce(InteractionData& idat) {
257 if (!initialized_) initialize();
258
259 LJInteractionData& mixer =
260 MixingMap[LJtids[idat.atid1]][LJtids[idat.atid2]];
261
262 RealType sigmai = mixer.sigmai;
263 RealType epsilon = mixer.epsilon;
264
265 // if (idat.topoDist == 3) {
266 // idat.atid1;
267 // }
268
269 RealType ros;
270 RealType rcos;
271 RealType myPot = 0.0;
272 RealType myPotC = 0.0;
273 RealType myDeriv = 0.0;
274 RealType myDerivC = 0.0;
275
276 ros = idat.rij * sigmai;
277
278 getLJfunc(ros, myPot, myDeriv);
279
280 if (idat.shiftedPot) {
281 rcos = idat.rcut * sigmai;
282 getLJfunc(rcos, myPotC, myDerivC);
283 myDerivC = 0.0;
284 } else if (idat.shiftedForce) {
285 rcos = idat.rcut * sigmai;
286 getLJfunc(rcos, myPotC, myDerivC);
287 myPotC = myPotC + myDerivC * (idat.rij - idat.rcut) * sigmai;
288 } else {
289 myPotC = 0.0;
290 myDerivC = 0.0;
291 }
292
293 RealType pot_temp = idat.vdwMult * epsilon * (myPot - myPotC);
294 idat.vpair += pot_temp;
295
296 RealType dudr =
297 idat.sw * idat.vdwMult * epsilon * (myDeriv - myDerivC) * sigmai;
298 idat.pot[VANDERWAALS_FAMILY] += idat.sw * pot_temp;
299
300 if (idat.isSelected) idat.selePot[VANDERWAALS_FAMILY] += idat.sw * pot_temp;
301
302 idat.f1 += idat.d * dudr / idat.rij;
303
304 return;
305 }
306
307 void LJ::getLJfunc(RealType r, RealType& pot, RealType& deriv) {
308 RealType ri = 1.0 / r;
309 RealType ri2 = ri * ri;
310 RealType ri6 = ri2 * ri2 * ri2;
311 RealType ri7 = ri6 * ri;
312 RealType ri12 = ri6 * ri6;
313 RealType ri13 = ri12 * ri;
314
315 pot = 4.0 * (ri12 - ri6);
316 deriv = 24.0 * (ri7 - 2.0 * ri13);
317
318 return;
319 }
320
321 RealType LJ::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) {
322 if (!initialized_) initialize();
323
324 int atid1 = atypes.first->getIdent();
325 int atid2 = atypes.second->getIdent();
326 int ljtid1 = LJtids[atid1];
327 int ljtid2 = LJtids[atid2];
328
329 if (ljtid1 == -1 || ljtid2 == -1)
330 return 0.0;
331 else {
332 LJInteractionData mixer = MixingMap[ljtid1][ljtid2];
333 return 2.5 * mixer.sigma;
334 }
335 }
336
337} // 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.