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