OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
Morse.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/Morse.hpp"
49
50#include <cmath>
51#include <cstdio>
52#include <cstring>
53
54#include "types/MorseInteractionType.hpp"
55#include "utils/simError.h"
56
57using namespace std;
58
59namespace OpenMD {
60
61 Morse::Morse() : initialized_(false), forceField_(NULL), name_("Morse") {}
62
63 void Morse::initialize() {
64 Mtypes.clear();
65 Mtids.clear();
66 MixingMap.clear();
67 nM_ = 0;
68
69 Mtids.resize(forceField_->getNAtomType(), -1);
70
71 ForceField::NonBondedInteractionTypeContainer* nbiTypes =
72 forceField_->getNonBondedInteractionTypes();
73 ForceField::NonBondedInteractionTypeContainer::MapTypeIterator j;
74 ForceField::NonBondedInteractionTypeContainer::KeyType keys;
75 NonBondedInteractionType* nbt;
76
77 for (nbt = nbiTypes->beginType(j); nbt != NULL;
78 nbt = nbiTypes->nextType(j)) {
79 if (nbt->isMorse()) {
80 keys = nbiTypes->getKeys(j);
81 AtomType* at1 = forceField_->getAtomType(keys[0]);
82 if (at1 == NULL) {
83 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
84 "Morse::initialize could not find AtomType %s\n"
85 "\tto for for %s - %s interaction.\n",
86 keys[0].c_str(), keys[0].c_str(), keys[1].c_str());
87 painCave.severity = OPENMD_ERROR;
88 painCave.isFatal = 1;
89 simError();
90 }
91
92 AtomType* at2 = forceField_->getAtomType(keys[1]);
93 if (at2 == NULL) {
94 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
95 "Morse::initialize could not find AtomType %s\n"
96 "\tfor %s - %s nonbonded interaction.\n",
97 keys[1].c_str(), keys[0].c_str(), keys[1].c_str());
98 painCave.severity = OPENMD_ERROR;
99 painCave.isFatal = 1;
100 simError();
101 }
102
103 MorseInteractionType* mit = dynamic_cast<MorseInteractionType*>(nbt);
104
105 if (mit == NULL) {
106 snprintf(
107 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
108 "Morse::initialize could not convert NonBondedInteractionType\n"
109 "\tto MorseInteractionType for %s - %s interaction.\n",
110 at1->getName().c_str(), at2->getName().c_str());
111 painCave.severity = OPENMD_ERROR;
112 painCave.isFatal = 1;
113 simError();
114 }
115
116 RealType De = mit->getD();
117 RealType Re = mit->getR();
118 RealType beta = mit->getBeta();
119
120 MorseType variant = mit->getInteractionType();
121 addExplicitInteraction(at1, at2, De, Re, beta, variant);
122 }
123 }
124 initialized_ = true;
125 }
126
127 void Morse::addExplicitInteraction(AtomType* atype1, AtomType* atype2,
128 RealType De, RealType Re, RealType beta,
129 MorseType mt) {
130 MorseInteractionData mixer;
131 mixer.De = De;
132 mixer.Re = Re;
133 mixer.beta = beta;
134 mixer.variant = mt;
135
136 int atid1 = atype1->getIdent();
137 int atid2 = atype2->getIdent();
138
139 int mtid1, mtid2;
140
141 pair<set<int>::iterator, bool> ret;
142 ret = Mtypes.insert(atid1);
143 if (ret.second == false) {
144 // already had this type in the Mtypes list, just get the mtid:
145 mtid1 = Mtids[atid1];
146 } else {
147 // didn't already have it, so make a new one and assign it:
148 mtid1 = nM_;
149 Mtids[atid1] = nM_;
150 nM_++;
151 }
152 ret = Mtypes.insert(atid2);
153 if (ret.second == false) {
154 // already had this type in the Mtypes list, just get the mtid:
155 mtid2 = Mtids[atid2];
156 } else {
157 // didn't already have it, so make a new one and assign it:
158 mtid2 = nM_;
159 Mtids[atid2] = nM_;
160 nM_++;
161 }
162
163 MixingMap.resize(nM_);
164 MixingMap[mtid1].resize(nM_);
165 MixingMap[mtid1][mtid2] = mixer;
166 if (mtid2 != mtid1) {
167 MixingMap[mtid2].resize(nM_);
168 MixingMap[mtid2][mtid1] = mixer;
169 }
170 }
171
172 void Morse::calcForce(InteractionData& idat) {
173 if (!initialized_) initialize();
174
175 MorseInteractionData& mixer =
176 MixingMap[Mtids[idat.atid1]][Mtids[idat.atid2]];
177
178 RealType myPot = 0.0;
179 RealType myPotC = 0.0;
180 RealType myDeriv = 0.0;
181 RealType myDerivC = 0.0;
182
183 RealType De = mixer.De;
184 RealType Re = mixer.Re;
185 RealType beta = mixer.beta;
186 MorseType variant = mixer.variant;
187
188 // V(r) = D_e exp(-a(r-re)(exp(-a(r-re))-2)
189
190 RealType expt = -beta * (idat.rij - Re);
191 RealType expfnc = exp(expt);
192 RealType expfnc2 = expfnc * expfnc;
193
194 RealType exptC = 0.0;
195 RealType expfncC = 0.0;
196 RealType expfnc2C = 0.0;
197
198 if (idat.shiftedPot || idat.shiftedForce) {
199 exptC = -beta * (idat.rcut - Re);
200 expfncC = exp(exptC);
201 expfnc2C = expfncC * expfncC;
202 }
203
204 switch (variant) {
205 case mtShifted: {
206 myPot = De * (expfnc2 - 2.0 * expfnc);
207 myDeriv = 2.0 * De * beta * (expfnc - expfnc2);
208
209 if (idat.shiftedPot) {
210 myPotC = De * (expfnc2C - 2.0 * expfncC);
211 myDerivC = 0.0;
212 } else if (idat.shiftedForce) {
213 myPotC = De * (expfnc2C - 2.0 * expfncC);
214 myDerivC = 2.0 * De * beta * (expfncC - expfnc2C);
215 myPotC += myDerivC * (idat.rij - idat.rcut);
216 } else {
217 myPotC = 0.0;
218 myDerivC = 0.0;
219 }
220
221 break;
222 }
223 case mtRepulsive: {
224 myPot = De * expfnc2;
225 myDeriv = -2.0 * De * beta * expfnc2;
226
227 if (idat.shiftedPot) {
228 myPotC = De * expfnc2C;
229 myDerivC = 0.0;
230 } else if (idat.shiftedForce) {
231 myPotC = De * expfnc2C;
232 myDerivC = -2.0 * De * beta * expfnc2C;
233 myPotC += myDerivC * (idat.rij - idat.rcut);
234 } else {
235 myPotC = 0.0;
236 myDerivC = 0.0;
237 }
238
239 break;
240 }
241 case mtUnknown: {
242 // don't know what to do so don't do anything
243 break;
244 }
245 }
246
247 RealType pot_temp = idat.vdwMult * (myPot - myPotC);
248 idat.vpair += pot_temp;
249
250 RealType dudr = idat.sw * idat.vdwMult * (myDeriv - myDerivC);
251
252 idat.pot[VANDERWAALS_FAMILY] += idat.sw * pot_temp;
253 if (idat.isSelected) idat.selePot[VANDERWAALS_FAMILY] += idat.sw * pot_temp;
254
255 idat.f1 += idat.d * dudr / idat.rij;
256
257 return;
258 }
259
260 RealType Morse::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) {
261 if (!initialized_) initialize();
262
263 int atid1 = atypes.first->getIdent();
264 int atid2 = atypes.second->getIdent();
265 int mtid1 = Mtids[atid1];
266 int mtid2 = Mtids[atid2];
267
268 if (mtid1 == -1 || mtid2 == -1)
269 return 0.0;
270 else {
271 MorseInteractionData mixer = MixingMap[mtid1][mtid2];
272 RealType Re = mixer.Re;
273 RealType beta = mixer.beta;
274 // This value of the r corresponds to an energy about 1.48% of
275 // the energy at the bottom of the Morse well. For comparison, the
276 // Lennard-Jones function is about 1.63% of it's minimum value at
277 // a distance of 2.5 sigma.
278 return (4.9 + beta * Re) / beta;
279 }
280 }
281} // 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.