OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
RepulsivePower.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/RepulsivePower.hpp"
49
50#include <cmath>
51#include <cstdio>
52#include <cstring>
53
54#include "types/RepulsivePowerInteractionType.hpp"
55#include "utils/simError.h"
56
57using namespace std;
58
59namespace OpenMD {
60
61 RepulsivePower::RepulsivePower() :
62 initialized_(false), forceField_(NULL), name_("RepulsivePower") {}
63
64 void RepulsivePower::initialize() {
65 RPtypes.clear();
66 RPtids.clear();
67 MixingMap.clear();
68 RPtids.resize(forceField_->getNAtomType(), -1);
69
70 ForceField::NonBondedInteractionTypeContainer* nbiTypes =
71 forceField_->getNonBondedInteractionTypes();
72 ForceField::NonBondedInteractionTypeContainer::MapTypeIterator j;
73 ForceField::NonBondedInteractionTypeContainer::KeyType keys;
74 NonBondedInteractionType* nbt;
75 int rptid1, rptid2;
76
77 for (nbt = nbiTypes->beginType(j); nbt != NULL;
78 nbt = nbiTypes->nextType(j)) {
79 if (nbt->isRepulsivePower()) {
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 "RepulsivePower::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 "RepulsivePower::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 int atid1 = at1->getIdent();
104 if (RPtids[atid1] == -1) {
105 rptid1 = RPtypes.size();
106 RPtypes.insert(atid1);
107 RPtids[atid1] = rptid1;
108 }
109 int atid2 = at2->getIdent();
110 if (RPtids[atid2] == -1) {
111 rptid2 = RPtypes.size();
112 RPtypes.insert(atid2);
113 RPtids[atid2] = rptid2;
114 }
115
116 RepulsivePowerInteractionType* rpit =
117 dynamic_cast<RepulsivePowerInteractionType*>(nbt);
118 if (rpit == NULL) {
119 snprintf(
120 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
121 "RepulsivePower::initialize could not convert "
122 "NonBondedInteractionType\n"
123 "\tto RepulsivePowerInteractionType for %s - %s interaction.\n",
124 at1->getName().c_str(), at2->getName().c_str());
125 painCave.severity = OPENMD_ERROR;
126 painCave.isFatal = 1;
127 simError();
128 }
129
130 RealType sigma = rpit->getSigma();
131 RealType epsilon = rpit->getEpsilon();
132 int nRep = rpit->getNrep();
133
134 addExplicitInteraction(at1, at2, sigma, epsilon, nRep);
135 }
136 }
137 initialized_ = true;
138 }
139
140 void RepulsivePower::addExplicitInteraction(AtomType* atype1,
141 AtomType* atype2, RealType sigma,
142 RealType epsilon, int nRep) {
143 RPInteractionData mixer;
144 mixer.sigma = sigma;
145 mixer.epsilon = epsilon;
146 mixer.sigmai = 1.0 / mixer.sigma;
147 mixer.nRep = nRep;
148
149 int nRP = RPtypes.size();
150 int atid1 = atype1->getIdent();
151 int atid2 = atype2->getIdent();
152 int rptid1, rptid2;
153
154 pair<set<int>::iterator, bool> ret;
155 ret = RPtypes.insert(atid1);
156 if (ret.second == false) {
157 // already had this type in the MieMap, just get the mietid:
158 rptid1 = RPtids[atid1];
159 } else {
160 // didn't already have it, so make a new one and assign it:
161 rptid1 = nRP;
162 RPtids[atid1] = nRP;
163 nRP++;
164 }
165
166 ret = RPtypes.insert(atid2);
167 if (ret.second == false) {
168 // already had this type in the MieMap, just get the mietid:
169 rptid2 = RPtids[atid2];
170 } else {
171 // didn't already have it, so make a new one and assign it:
172 rptid2 = nRP;
173 RPtids[atid2] = nRP;
174 nRP++;
175 }
176
177 MixingMap.resize(nRP);
178 MixingMap[rptid1].resize(nRP);
179
180 MixingMap[rptid1][rptid2] = mixer;
181 if (rptid2 != rptid1) {
182 MixingMap[rptid2].resize(nRP);
183 MixingMap[rptid2][rptid1] = mixer;
184 }
185 }
186
187 void RepulsivePower::calcForce(InteractionData& idat) {
188 if (!initialized_) initialize();
189
190 RPInteractionData& mixer =
191 MixingMap[RPtids[idat.atid1]][RPtids[idat.atid2]];
192 RealType sigmai = mixer.sigmai;
193 RealType epsilon = mixer.epsilon;
194 int nRep = mixer.nRep;
195
196 RealType ros;
197 RealType rcos;
198 RealType myPot = 0.0;
199 RealType myPotC = 0.0;
200 RealType myDeriv = 0.0;
201 RealType myDerivC = 0.0;
202
203 ros = idat.rij * sigmai;
204
205 getNRepulsionFunc(ros, nRep, myPot, myDeriv);
206
207 if (idat.shiftedPot) {
208 rcos = idat.rcut * sigmai;
209 getNRepulsionFunc(rcos, nRep, myPotC, myDerivC);
210 myDerivC = 0.0;
211 } else if (idat.shiftedForce) {
212 rcos = idat.rcut * sigmai;
213 getNRepulsionFunc(rcos, nRep, myPotC, myDerivC);
214 myPotC = myPotC + myDerivC * (idat.rij - idat.rcut) * sigmai;
215 } else {
216 myPotC = 0.0;
217 myDerivC = 0.0;
218 }
219
220 RealType pot_temp = idat.vdwMult * epsilon * (myPot - myPotC);
221 idat.vpair += pot_temp;
222
223 RealType dudr =
224 idat.sw * idat.vdwMult * epsilon * (myDeriv - myDerivC) * sigmai;
225
226 idat.pot[VANDERWAALS_FAMILY] += idat.sw * pot_temp;
227 if (idat.isSelected) idat.selePot[VANDERWAALS_FAMILY] += idat.sw * pot_temp;
228
229 idat.f1 += idat.d * dudr / idat.rij;
230
231 return;
232 }
233
234 void RepulsivePower::getNRepulsionFunc(const RealType& r, int& n,
235 RealType& pot, RealType& deriv) {
236 RealType ri = 1.0 / r;
237 RealType rin = pow(ri, n);
238 RealType rin1 = rin * ri;
239
240 pot = rin;
241 deriv = -n * rin1;
242
243 return;
244 }
245
246 RealType RepulsivePower::getSuggestedCutoffRadius(
247 pair<AtomType*, AtomType*> atypes) {
248 if (!initialized_) initialize();
249
250 int atid1 = atypes.first->getIdent();
251 int atid2 = atypes.second->getIdent();
252 int rptid1 = RPtids[atid1];
253 int rptid2 = RPtids[atid2];
254
255 if (rptid1 == -1 || rptid2 == -1)
256 return 0.0;
257 else {
258 RPInteractionData mixer = MixingMap[rptid1][rptid2];
259 return 2.5 * mixer.sigma;
260 }
261 }
262} // 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.