OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
SHAPES.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/SHAPES.hpp"
49
50#include <cmath>
51#include <cstdio>
52#include <cstring>
53
54#include "nonbonded/LJ.hpp"
55#include "utils/simError.h"
56
57using namespace std;
58namespace OpenMD {
59
60 SHAPES::SHAPES() {
61 initialized_ = false;
62 lMax_ = 64;
63 mMax_ = 64;
64 forceField_ = NULL;
65 }
66
67 void SHAPES::initialize() {
68 ForceFieldOptions& fopts = forceField_->getForceFieldOptions();
69 ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes();
70 ForceField::AtomTypeContainer::MapTypeIterator i;
71 AtomType* at;
72
73 // SHAPES handles all of the SHAPES-SHAPES interactions as well as
74 // SHAPES-LJ cross interactions:
75
76 for (at = atomTypes->beginType(i); at != NULL;
77 at = atomTypes->nextType(i)) {
78 if (at->isShape()) addShape(dynamic_cast<ShapeAtomType*>(at));
79
80 if (at->isLennardJones()) addLJ(at);
81 }
82
83 initialized_ = true;
84 }
85
86 void SHAPES::addShape(ShapeAtomType* atomType) {
87 // add it to the map:
88 AtomTypeProperties atp = atomType->getATP();
89
90 if (atomType->isShape()) {
91 pair<map<int, ShapeAtomType*>::iterator, bool> ret;
92 ret = ShapesMap.insert(pair<int, ShapeAtomType*>(atp.ident, atomType));
93 if (ret.second == false) {
94 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
95 "SHAPES already had a previous entry with ident %d\n",
96 atp.ident);
97 painCave.severity = OPENMD_INFO;
98 painCave.isFatal = 0;
99 simError();
100 }
101
102 ShapesMap.insert(pair<int, ShapeAtomType*>(
103 atp.ident, static_cast<ShapeAtomType*>(atomType)));
104
105 } else if (atomType->isLennardJones()) {
106 RealType d1 = getLJSigma(atomType) / sqrt(2.0);
107 RealType e1 = getLJEpsilon(atomType);
108 } else {
109 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
110 "SHAPES::addType was passed an atomType (%s) that does not\n"
111 "\tappear to be a SHAPES or Lennard-Jones atom.\n",
112 atomType->getName().c_str());
113 painCave.severity = OPENMD_ERROR;
114 painCave.isFatal = 1;
115 simError();
116 }
117 }
118
119 LJParam SHAPES::getLJParam(AtomType* atomType) {
120 // Do sanity checking on the AtomType we were passed before
121 // building any data structures:
122 if (!atomType->isLennardJones()) {
123 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
124 "SHAPES::getLJParam was passed an atomType (%s) that does not\n"
125 "\tappear to be a Lennard-Jones atom.\n",
126 atomType->getName().c_str());
127 painCave.severity = OPENMD_ERROR;
128 painCave.isFatal = 1;
129 simError();
130 }
131
132 GenericData* data = atomType->getPropertyByName("LennardJones");
133 if (data == NULL) {
134 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
135 "SHAPES::getLJParam could not find Lennard-Jones\n"
136 "\tparameters for atomType %s.\n",
137 atomType->getName().c_str());
138 painCave.severity = OPENMD_ERROR;
139 painCave.isFatal = 1;
140 simError();
141 }
142
143 LJParamGenericData* ljData = dynamic_cast<LJParamGenericData*>(data);
144 if (ljData == NULL) {
145 snprintf(
146 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
147 "SHAPES::getLJParam could not convert GenericData to LJParam for\n"
148 "\tatom type %s\n",
149 atomType->getName().c_str());
150 painCave.severity = OPENMD_ERROR;
151 painCave.isFatal = 1;
152 simError();
153 }
154
155 return ljData->getData();
156 }
157
158 RealType SHAPES::getLJEpsilon(AtomType* atomType) {
159 LJParam ljParam = getLJParam(atomType);
160 return ljParam.epsilon;
161 }
162 RealType SHAPES::getLJSigma(AtomType* atomType) {
163 LJParam ljParam = getLJParam(atomType);
164 return ljParam.sigma;
165 }
166
167 RealType SHAPES::getGayBerneCut(int atid) {
168 if (!initialized_) initialize();
169 std::map<int, AtomType*>::const_iterator it;
170 it = SHAPESMap.find(atid);
171 if (it == SHAPESMap.end()) {
172 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
173 "SHAPES::getGayBerneCut could not find atid %d in SHAPESMap\n",
174 (atid));
175 painCave.severity = OPENMD_ERROR;
176 painCave.isFatal = 1;
177 simError();
178 }
179
180 AtomType* atype = it->second;
181
182 RealType gbCut;
183
184 if (atype->isGayBerne()) {
185 GayBerneParam gb = getGayBerneParam(atype);
186
187 // sigma is actually sqrt(2) * l for prolate ellipsoids
188 gbCut = 2.5 * sqrt(2.0) * max(gb.SHAPES_l, gb.SHAPES_d);
189
190 } else if (atype->isLennardJones()) {
191 gbCut = 2.5 * LJ::Instance()->getSigma(atype);
192 }
193
194 return gbCut;
195 }
196
197 void SHAPES::calcForce(AtomType* at1, AtomType* at2, Vector3d d, RealType r,
198 RealType r2, RealType sw, RealType& vpair,
199 RealType& pot, RotMat3x3d A1, RotMat3x3d A2,
200 Vector3d& f1, Vector3d& t1, Vector3d& t2) {
201 if (!initialized_) initialize();
202
203 pair<AtomType*, AtomType*> key = make_pair(at1, at2);
204 SHAPESInteractionData mixer = MixingMap[key];
205
206 RealType r3 = r2 * r;
207 RealType r5 = r3 * r2;
208
209 Vector3d drdi = -d / r;
210 Vector3d drdui = V3Zero;
211 Vector3d drdj = d / r;
212 Vector3d drduj = V3Zero;
213
214 bool i_is_LJ = at1->isLennardJones();
215 bool j_is_LJ = at2->isLennardJones();
216
217 RealType sigma_i;
218 RealType s_i;
219 RealType eps_i;
220 Vector3d dsigmaidr;
221 Vector3d disgmaidu;
222 Vector3d dsidr;
223 Vector3d dsidu;
224 Vector3d depsidr;
225 Vector3d depsidu;
226
227 if (i_is_LJ) {
228 sigma_i = LJ::Instance()->getSigma(at1);
229 s_i = sigma_i;
230 epsilon_i = LJ::Instance()->getEpsilon(at1);
231 dsigmaidr = V3Zero;
232 dsigmaidu = V3Zero;
233 dsidr = V3Zero;
234 dsidu = V3Zero;
235 depsidr = V3Zero;
236 depsidu = V3Zero;
237 } else {
238 // rotate the inter-particle separation into the two different
239 // body-fixed coordinate systems:
240
241 Vector3d ri = A1 * d;
242
243 RealType xi = ri.x() / r;
244 RealType yi = ri.y() / r;
245 RealType zi = ri.z() / r;
246 RealType xi2 = xi * xi;
247 RealType yi2 = yi * yi;
248 RealType zi2 = zi * zi;
249 RealType cti = zi / r;
250
251 if (cti > 1.0) cti = 1.0;
252 if (cti < -1.0_dp) cti = -1.0;
253
254 Vector3d dctidr(-zi * xi / r3, -zi * yi / r3, 1.0 / r - zi2 / r3);
255
256 Vector3d dctidu(yi / r, -zi / r, 0.0);
257
258 // this is an attempt to try to truncate the singularity when
259 // sin(theta) is near 0.0:
260
261 RealType sti2 = 1.0 - cti * cti;
262 RealType proji;
263 Vector3d dcpidr, dcpidu, dspidr, dspidu;
264 if (fabs(sti2) < 1.0e-12) {
265 proji = sqrt(r * 1.0e-12);
266 dcpidr = Vector3d(1.0 / proji, 0.0, 0.0);
267 dcpidu = Vector3d(xi / proji, 0.0, 0.0);
268 dspidr = Vector3d(0.0, 1.0 / proji, 0.0);
269 dspidu = Vector3d(0.0, yi / proji, 0.0);
270 } else {
271 proji = sqrt(xi2 + yi2);
272 RealType proji3 = proji * proji * proji;
273 dcpidr =
274 Vector3d(1.0_dp / proji - xi2 / proji3, -xi * yi / proji3, 0.0);
275 dcpidu = Vector3d(xi / proji - (xi2 * xi) / proji3,
276 -(xi * yi2) / proji3, 0.0);
277 dspidr =
278 Vector3d(-xi * yi / proji3, 1.0_dp / proji - yi2 / proji3, 0.0);
279 dspidu = Vector3d(-(yi * xi2) / proji3,
280 yi / proji - (yi2 * yi) / proji3, 0.0);
281 }
282
283 cpi = xi / proji;
284 dcpidr.z() = 0.0;
285 dcpidu.z() = 0.0;
286
287 spi = yi / proji;
288 dspidr.z() = 0.0;
289 dspidu.z() = 0.0;
290
291 RealType sigma0 = mixer.sigma0;
292 RealType dw = mixer.dw;
293 RealType eps0 = mixer.eps0;
294 RealType x2 = mixer.x2;
295 RealType xa2 = mixer.xa2;
296 RealType xai2 = mixer.xai2;
297 RealType xp2 = mixer.xp2;
298 RealType xpap2 = mixer.xpap2;
299 RealType xpapi2 = mixer.xpapi2;
300
301 Vector3d ul1 = A1.getRow(2);
302 Vector3d ul2 = A2.getRow(2);
303
304 RealType a, b, g;
305
306 if (i_is_LJ) {
307 a = 0.0;
308 ul1 = V3Zero;
309 } else {
310 a = dot(d, ul1);
311 }
312
313 if (j_is_LJ) {
314 b = 0.0;
315 ul2 = V3Zero;
316 } else {
317 b = dot(d, ul2);
318 }
319
320 if (i_is_LJ || j_is_LJ)
321 g = 0.0;
322 else
323 g = dot(ul1, ul2);
324
325 RealType au = a / r;
326 RealType bu = b / r;
327
328 RealType au2 = au * au;
329 RealType bu2 = bu * bu;
330 RealType g2 = g * g;
331
332 RealType H =
333 (xa2 * au2 + xai2 * bu2 - 2.0 * x2 * au * bu * g) / (1.0 - x2 * g2);
334 RealType Hp = (xpap2 * au2 + xpapi2 * bu2 - 2.0 * xp2 * au * bu * g) /
335 (1.0 - xp2 * g2);
336
337 RealType sigma = sigma0 / sqrt(1.0 - H);
338 RealType e1 = 1.0 / sqrt(1.0 - x2 * g2);
339 RealType e2 = 1.0 - Hp;
340 RealType eps = eps0 * pow(e1, nu_) * pow(e2, mu_);
341 RealType BigR = dw * sigma0 / (r - sigma + dw * sigma0);
342
343 RealType R3 = BigR * BigR * BigR;
344 RealType R6 = R3 * R3;
345 RealType R7 = R6 * BigR;
346 RealType R12 = R6 * R6;
347 RealType R13 = R6 * R7;
348
349 RealType U = vdwMult * 4.0 * eps * (R12 - R6);
350
351 RealType s3 = sigma * sigma * sigma;
352 RealType s03 = sigma0 * sigma0 * sigma0;
353
354 RealType pref1 = -vdwMult * 8.0 * eps * mu_ * (R12 - R6) / (e2 * r);
355
356 RealType pref2 =
357 vdwMult * 8.0 * eps * s3 * (6.0 * R13 - 3.0 * R7) / (dw * r * s03);
358
359 RealType dUdr = -(pref1 * Hp + pref2 * (sigma0 * sigma0 * r / s3 + H));
360
361 RealType dUda = pref1 * (xpap2 * au - xp2 * bu * g) / (1.0 - xp2 * g2) +
362 pref2 * (xa2 * au - x2 * bu * g) / (1.0 - x2 * g2);
363
364 RealType dUdb = pref1 * (xpapi2 * bu - xp2 * au * g) / (1.0 - xp2 * g2) +
365 pref2 * (xai2 * bu - x2 * au * g) / (1.0 - x2 * g2);
366
367 RealType dUdg =
368 4.0 * eps * nu_ * (R12 - R6) * x2 * g / (1.0 - x2 * g2) +
369 8.0 * eps * mu_ * (R12 - R6) * (xp2 * au * bu - Hp * xp2 * g) /
370 (1.0 - xp2 * g2) / e2 +
371 8.0 * eps * s3 * (3.0 * R7 - 6.0 * R13) *
372 (x2 * au * bu - H * x2 * g) / (1.0 - x2 * g2) / (dw * s03);
373
374 Vector3d rhat = d / r;
375 Vector3d rxu1 = cross(d, ul1);
376 Vector3d rxu2 = cross(d, ul2);
377 Vector3d uxu = cross(ul1, ul2);
378
379 pot += U * sw;
380 f1 += dUdr * rhat + dUda * ul1 + dUdb * ul2;
381 t1 += dUda * rxu1 - dUdg * uxu;
382 t2 += dUdb * rxu2 - dUdg * uxu;
383 vpair += U * sw;
384
385 return;
386 }
387 }
388} // namespace OpenMD
AtomType is what OpenMD looks to for unchanging data about an atom.
Definition AtomType.hpp:69
Real & z()
Returns reference of the third element of Vector3.
Definition Vector3.hpp:123
Real & x()
Returns reference of the first element of Vector3.
Definition Vector3.hpp:99
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
Vector3< Real > cross(const Vector3< Real > &v1, const Vector3< Real > &v2)
Returns the cross product of two Vectors.
Definition Vector3.hpp:139
Real dot(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the dot product of two DynamicVectors.