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