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