OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
InversePowerSeries.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/InversePowerSeries.hpp"
49
50#include <cmath>
51#include <cstdio>
52#include <cstring>
53
54#include "types/InversePowerSeriesInteractionType.hpp"
55#include "utils/simError.h"
56
57using namespace std;
58
59namespace OpenMD {
60
61 InversePowerSeries::InversePowerSeries() :
62 initialized_(false), forceField_(NULL), name_("InversePowerSeries") {}
63
64 void InversePowerSeries::initialize() {
65 InversePowerSeriesTypes.clear();
66 InversePowerSeriesTids.clear();
67 MixingMap.clear();
68 InversePowerSeriesTids.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 ipstid1, ipstid2;
76
77 for (nbt = nbiTypes->beginType(j); nbt != NULL;
78 nbt = nbiTypes->nextType(j)) {
79 if (nbt->isInversePowerSeries()) {
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 "InversePowerSeries::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 "InversePowerSeries::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 (InversePowerSeriesTids[atid1] == -1) {
105 ipstid1 = InversePowerSeriesTypes.size();
106 InversePowerSeriesTypes.insert(atid1);
107 InversePowerSeriesTids[atid1] = ipstid1;
108 }
109 int atid2 = at2->getIdent();
110 if (InversePowerSeriesTids[atid2] == -1) {
111 ipstid2 = InversePowerSeriesTypes.size();
112 InversePowerSeriesTypes.insert(atid2);
113 InversePowerSeriesTids[atid2] = ipstid2;
114 }
115
116 InversePowerSeriesInteractionType* ipsit =
117 dynamic_cast<InversePowerSeriesInteractionType*>(nbt);
118 if (ipsit == NULL) {
119 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
120 "InversePowerSeries::initialize could not convert "
121 "NonBondedInteractionType\n"
122 "\tto InversePowerSeriesInteractionType for %s - %s "
123 "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 std::vector<int> powers = ipsit->getPowers();
131 std::vector<RealType> coefficients = ipsit->getCoefficients();
132
133 addExplicitInteraction(at1, at2, powers, coefficients);
134 }
135 }
136 initialized_ = true;
137 }
138
139 void InversePowerSeries::addExplicitInteraction(
140 AtomType* atype1, AtomType* atype2, std::vector<int> powers,
141 std::vector<RealType> coefficients) {
142 InversePowerSeriesInteractionData mixer;
143 mixer.powers = powers;
144 mixer.coefficients = coefficients;
145
146 int ipstid1 = InversePowerSeriesTids[atype1->getIdent()];
147 int ipstid2 = InversePowerSeriesTids[atype2->getIdent()];
148 int nInversePowerSeries = InversePowerSeriesTypes.size();
149
150 MixingMap.resize(nInversePowerSeries);
151 MixingMap[ipstid1].resize(nInversePowerSeries);
152
153 MixingMap[ipstid1][ipstid2] = mixer;
154 if (ipstid2 != ipstid1) {
155 MixingMap[ipstid2].resize(nInversePowerSeries);
156 MixingMap[ipstid2][ipstid1] = mixer;
157 }
158 }
159
160 void InversePowerSeries::calcForce(InteractionData& idat) {
161 if (!initialized_) initialize();
162
163 InversePowerSeriesInteractionData& mixer =
164 MixingMap[InversePowerSeriesTids[idat.atid1]]
165 [InversePowerSeriesTids[idat.atid2]];
166 std::vector<int> powers = mixer.powers;
167 std::vector<RealType> coefficients = mixer.coefficients;
168
169 RealType myPot = 0.0;
170 RealType myPotC = 0.0;
171 RealType myDeriv = 0.0;
172 RealType myDerivC = 0.0;
173
174 RealType ri = 1.0 / idat.rij;
175 RealType ric = 1.0 / idat.rcut;
176
177 RealType fn, fnc;
178
179 for (unsigned int i = 0; i < powers.size(); i++) {
180 fn = coefficients[i] * pow(ri, powers[i]);
181 fnc = coefficients[i] * pow(ric, powers[i]);
182 myPot += fn;
183 myPotC += fnc;
184 myDeriv -= powers[i] * fn * ri;
185 myDerivC -= powers[i] * fnc * ric;
186 }
187
188 if (idat.shiftedPot) {
189 myDerivC = 0.0;
190 } else if (idat.shiftedForce) {
191 myPotC = myPotC + myDerivC * (idat.rij - idat.rcut);
192 } else {
193 myPotC = 0.0;
194 myDerivC = 0.0;
195 }
196
197 RealType pot_temp = idat.vdwMult * (myPot - myPotC);
198 idat.vpair += pot_temp;
199
200 RealType dudr = idat.sw * idat.vdwMult * (myDeriv - myDerivC);
201
202 idat.pot[VANDERWAALS_FAMILY] += idat.sw * pot_temp;
203 if (idat.isSelected) idat.selePot[VANDERWAALS_FAMILY] += idat.sw * pot_temp;
204
205 idat.f1 += idat.d * dudr / idat.rij;
206
207 return;
208 }
209
210 RealType InversePowerSeries::getSuggestedCutoffRadius(
211 pair<AtomType*, AtomType*> atypes) {
212 if (!initialized_) initialize();
213
214 int atid1 = atypes.first->getIdent();
215 int atid2 = atypes.second->getIdent();
216 int ipstid1 = InversePowerSeriesTids[atid1];
217 int ipstid2 = InversePowerSeriesTids[atid2];
218
219 if (ipstid1 == -1 || ipstid2 == -1)
220 return 0.0;
221 else {
222 // This seems to work moderately well as a default. There's no
223 // inherent scale for 1/r^n interactions that we can standardize.
224 // 12 angstroms seems to be a reasonably good guess for most
225 // cases.
226 return 12.0;
227 }
228 }
229} // 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.