1 |
/* |
2 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
* |
4 |
* The University of Notre Dame grants you ("Licensee") a |
5 |
* non-exclusive, royalty free, license to use, modify and |
6 |
* redistribute this software in source and binary code form, provided |
7 |
* that the following conditions are met: |
8 |
* |
9 |
* 1. Redistributions of source code must retain the above copyright |
10 |
* notice, this list of conditions and the following disclaimer. |
11 |
* |
12 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
* notice, this list of conditions and the following disclaimer in the |
14 |
* documentation and/or other materials provided with the |
15 |
* distribution. |
16 |
* |
17 |
* This software is provided "AS IS," without a warranty of any |
18 |
* kind. All express or implied conditions, representations and |
19 |
* warranties, including any implied warranty of merchantability, |
20 |
* fitness for a particular purpose or non-infringement, are hereby |
21 |
* excluded. The University of Notre Dame and its licensors shall not |
22 |
* be liable for any damages suffered by licensee as a result of |
23 |
* using, modifying or distributing the software or its |
24 |
* derivatives. In no event will the University of Notre Dame or its |
25 |
* licensors be liable for any lost revenue, profit or data, or for |
26 |
* direct, indirect, special, consequential, incidental or punitive |
27 |
* damages, however caused and regardless of the theory of liability, |
28 |
* arising out of the use of or inability to use software, even if the |
29 |
* University of Notre Dame has been advised of the possibility of |
30 |
* such damages. |
31 |
* |
32 |
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
* research, please cite the appropriate papers when you publish your |
34 |
* work. Good starting points are: |
35 |
* |
36 |
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
* [4] Vardeman & Gezelter, in progress (2009). |
40 |
*/ |
41 |
|
42 |
#include <stdio.h> |
43 |
#include <string.h> |
44 |
|
45 |
#include <cmath> |
46 |
#include "nonbonded/SHAPES.hpp" |
47 |
#include "nonbonded/LJ.hpp" |
48 |
#include "utils/simError.h" |
49 |
|
50 |
using namespace std; |
51 |
namespace OpenMD { |
52 |
|
53 |
bool SHAPES::initialized_ = false; |
54 |
int SHAPES::lMax_ = 64; |
55 |
int SHAPES::mMax_ = 64; |
56 |
ForceField* SHAPES::forceField_ = NULL; |
57 |
map<int, AtomType*> SHAPES::ShapesMap; |
58 |
map<pair<AtomType*, AtomType*>, SHAPESInteractionData> SHAPES::MixingMap; |
59 |
|
60 |
SHAPES* SHAPES::_instance = NULL; |
61 |
|
62 |
SHAPES* SHAPES::Instance() { |
63 |
if (!_instance) { |
64 |
_instance = new SHAPES(); |
65 |
} |
66 |
return _instance; |
67 |
} |
68 |
|
69 |
void SHAPES::initialize() { |
70 |
|
71 |
ForceFieldOptions& fopts = forceField_->getForceFieldOptions(); |
72 |
ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes(); |
73 |
ForceField::AtomTypeContainer::MapTypeIterator i; |
74 |
AtomType* at; |
75 |
|
76 |
// SHAPES handles all of the SHAPES-SHAPES interactions as well as |
77 |
// SHAPES-LJ cross interactions: |
78 |
|
79 |
for (at = atomTypes->beginType(i); at != NULL; |
80 |
at = atomTypes->nextType(i)) { |
81 |
|
82 |
if (at->isShape() || at->isLennardJones()) |
83 |
addType(at); |
84 |
} |
85 |
|
86 |
initialized_ = true; |
87 |
} |
88 |
|
89 |
void SHAPES::addType(AtomType* atomType){ |
90 |
// add it to the map: |
91 |
AtomTypeProperties atp = atomType->getATP(); |
92 |
|
93 |
pair<map<int,AtomType*>::iterator,bool> ret; |
94 |
ret = ShapesMap.insert( pair<int, AtomType*>(atp.ident, atomType) ); |
95 |
if (ret.second == false) { |
96 |
sprintf( painCave.errMsg, |
97 |
"SHAPES already had a previous entry with ident %d\n", |
98 |
atp.ident); |
99 |
painCave.severity = OPENMD_INFO; |
100 |
painCave.isFatal = 0; |
101 |
simError(); |
102 |
} |
103 |
|
104 |
if (atomType->isShape()) { |
105 |
ShapeAtomType* sAtomType = dynamic_cast<ShapeAtomType*>(atomType); |
106 |
if (sAtomType == NULL) { |
107 |
sprintf(painCave.errMsg, |
108 |
"SHAPES:: Can't cast to ShapeAtomType"); |
109 |
painCave.severity = OPENMD_ERROR; |
110 |
painCave.isFatal = 1; |
111 |
simError(); |
112 |
} |
113 |
ShapesMap.insert( pair<int, ShapeAtomType*>(atp.ident, sAtomType) ); |
114 |
|
115 |
} else if (atomType->isLennardJones()) { |
116 |
d1 = LJ::Instance()->getSigma(atomType) / sqrt(2.0); |
117 |
e1 = LJ::Instance()->getEpsilon(atomType); |
118 |
} else { |
119 |
sprintf( painCave.errMsg, |
120 |
"SHAPES::addType was passed an atomType (%s) that does not\n" |
121 |
"\tappear to be a Gay-Berne or Lennard-Jones atom.\n", |
122 |
atomType->getName().c_str()); |
123 |
painCave.severity = OPENMD_ERROR; |
124 |
painCave.isFatal = 1; |
125 |
simError(); |
126 |
} |
127 |
|
128 |
|
129 |
// Now, iterate over all known types and add to the mixing map: |
130 |
|
131 |
map<int, AtomType*>::iterator it; |
132 |
for( it = ShapesMap.begin(); it != SHAPESMap.end(); ++it) { |
133 |
|
134 |
AtomType* atype2 = (*it).second; |
135 |
|
136 |
RealType d2, l2, e2, er2, dw2; |
137 |
|
138 |
if (atype2->isGayBerne()) { |
139 |
GayBerneParam gb2 = getGayBerneParam(atype2); |
140 |
d2 = gb2.SHAPES_d; |
141 |
l2 = gb2.SHAPES_l; |
142 |
e2 = gb2.SHAPES_eps; |
143 |
er2 = gb2.SHAPES_eps_ratio; |
144 |
dw2 = gb2.SHAPES_dw; |
145 |
} else if (atype2->isLennardJones()) { |
146 |
d2 = LJ::Instance()->getSigma(atype2) / sqrt(2.0); |
147 |
e2 = LJ::Instance()->getEpsilon(atype2); |
148 |
l2 = d2; |
149 |
er2 = 1.0; |
150 |
dw2 = 1.0; |
151 |
} |
152 |
|
153 |
SHAPESInteractionData mixer; |
154 |
|
155 |
// Cleaver paper uses sqrt of squares to get sigma0 for |
156 |
// mixed interactions. |
157 |
|
158 |
mixer.sigma0 = sqrt(d1*d1 + d2*d2); |
159 |
mixer.xa2 = (l1*l1 - d1*d1)/(l1*l1 + d2*d2); |
160 |
mixer.xai2 = (l2*l2 - d2*d2)/(l2*l2 + d1*d1); |
161 |
mixer.x2 = (l1*l1 - d1*d1) * (l2*l2 - d2*d2) / |
162 |
((l2*l2 + d1*d1) * (l1*l1 + d2*d2)); |
163 |
|
164 |
// assumed LB mixing rules for now: |
165 |
|
166 |
mixer.dw = 0.5 * (dw1 + dw2); |
167 |
mixer.eps0 = sqrt(e1 * e2); |
168 |
|
169 |
RealType er = sqrt(er1 * er2); |
170 |
RealType ermu = pow(er,(1.0 / mu_)); |
171 |
RealType xp = (1.0 - ermu) / (1.0 + ermu); |
172 |
RealType ap2 = 1.0 / (1.0 + ermu); |
173 |
|
174 |
mixer.xp2 = xp * xp; |
175 |
mixer.xpap2 = xp * ap2; |
176 |
mixer.xpapi2 = xp / ap2; |
177 |
|
178 |
// only add this pairing if at least one of the atoms is a Gay-Berne atom |
179 |
|
180 |
if (atomType->isGayBerne() || atype2->isGayBerne()) { |
181 |
|
182 |
pair<AtomType*, AtomType*> key1, key2; |
183 |
key1 = make_pair(atomType, atype2); |
184 |
key2 = make_pair(atype2, atomType); |
185 |
|
186 |
MixingMap[key1] = mixer; |
187 |
if (key2 != key1) { |
188 |
MixingMap[key2] = mixer; |
189 |
} |
190 |
} |
191 |
} |
192 |
} |
193 |
|
194 |
|
195 |
RealType SHAPES::getGayBerneCut(int atid) { |
196 |
if (!initialized_) initialize(); |
197 |
std::map<int, AtomType*> :: const_iterator it; |
198 |
it = SHAPESMap.find(atid); |
199 |
if (it == SHAPESMap.end()) { |
200 |
sprintf( painCave.errMsg, |
201 |
"SHAPES::getGayBerneCut could not find atid %d in SHAPESMap\n", |
202 |
(atid)); |
203 |
painCave.severity = OPENMD_ERROR; |
204 |
painCave.isFatal = 1; |
205 |
simError(); |
206 |
} |
207 |
|
208 |
AtomType* atype = it->second; |
209 |
|
210 |
RealType gbCut; |
211 |
|
212 |
if (atype->isGayBerne()) { |
213 |
GayBerneParam gb = getGayBerneParam(atype); |
214 |
|
215 |
// sigma is actually sqrt(2) * l for prolate ellipsoids |
216 |
gbCut = 2.5 * sqrt(2.0) * max(gb.SHAPES_l, gb.SHAPES_d); |
217 |
|
218 |
} else if (atype->isLennardJones()) { |
219 |
gbCut = 2.5 * LJ::Instance()->getSigma(atype); |
220 |
} |
221 |
|
222 |
return gbCut; |
223 |
} |
224 |
|
225 |
|
226 |
void SHAPES::calcForce(AtomType* at1, AtomType* at2, Vector3d d, |
227 |
RealType r, RealType r2, RealType sw, |
228 |
RealType &vpair, RealType &pot, |
229 |
RotMat3x3d A1, RotMat3x3d A2, Vector3d &f1, |
230 |
Vector3d &t1, Vector3d &t2) { |
231 |
|
232 |
if (!initialized_) initialize(); |
233 |
|
234 |
pair<AtomType*, AtomType*> key = make_pair(at1, at2); |
235 |
SHAPESInteractionData mixer = MixingMap[key]; |
236 |
|
237 |
RealType r3 = r2 * r; |
238 |
RealType r5 = r3 * r2; |
239 |
|
240 |
Vector3d drdi = -d / r; |
241 |
Vector3d drdui = V3Zero; |
242 |
Vector3d drdj = d / r; |
243 |
Vector3d drduj = V3Zero; |
244 |
|
245 |
bool i_is_LJ = at1->isLennardJones(); |
246 |
bool j_is_LJ = at2->isLennardJones(); |
247 |
|
248 |
RealType sigma_i; |
249 |
RealType s_i; |
250 |
RealType eps_i; |
251 |
Vector3d dsigmaidr; |
252 |
Vector3d disgmaidu; |
253 |
Vector3d dsidr; |
254 |
Vector3d dsidu; |
255 |
Vector3d depsidr; |
256 |
Vector3d depsidu; |
257 |
|
258 |
if (i_is_LJ) { |
259 |
sigma_i = LJ::Instance()->getSigma(at1); |
260 |
s_i = sigma_i; |
261 |
epsilon_i = LJ::Instance()->getEpsilon(at1); |
262 |
dsigmaidr = V3Zero; |
263 |
dsigmaidu = V3Zero; |
264 |
dsidr = V3Zero; |
265 |
dsidu = V3Zero; |
266 |
depsidr = V3Zero; |
267 |
depsidu = V3Zero; |
268 |
} else { |
269 |
|
270 |
// rotate the inter-particle separation into the two different |
271 |
// body-fixed coordinate systems: |
272 |
|
273 |
Vector3d ri = A1 * d; |
274 |
|
275 |
RealType xi = ri.x() / r; |
276 |
RealType yi = ri.y() / r; |
277 |
RealType zi = ri.z() / r; |
278 |
RealType xi2 = xi * xi; |
279 |
RealType yi2 = yi * yi; |
280 |
RealType zi2 = zi * zi; |
281 |
RealType cti = zi / r; |
282 |
|
283 |
if (cti > 1.0) cti = 1.0; |
284 |
if (cti < -1.0_dp) cti = -1.0; |
285 |
|
286 |
Vector3d dctidr(-zi * xi / r3, |
287 |
-zi * yi / r3, |
288 |
1.0 / r - zi2 / r3); |
289 |
|
290 |
Vector3d dctidu(yi / r, |
291 |
-zi / r, |
292 |
0.0); |
293 |
|
294 |
// this is an attempt to try to truncate the singularity when |
295 |
// sin(theta) is near 0.0: |
296 |
|
297 |
RealType sti2 = 1.0 - cti*cti; |
298 |
if (fabs(sti2) < 1.0e-12) { |
299 |
RealType proji = sqrt(r * 1.0e-12); |
300 |
Vector3d dcpidx(1.0 / proji, |
301 |
0.0, |
302 |
|
303 |
dcpidx = 1.0_dp / proji |
304 |
dcpidy = 0.0_dp |
305 |
dcpidux = xi / proji |
306 |
dcpiduy = 0.0_dp |
307 |
dspidx = 0.0_dp |
308 |
dspidy = 1.0_dp / proji |
309 |
dspidux = 0.0_dp |
310 |
dspiduy = yi / proji |
311 |
else |
312 |
proji = sqrt(xi2 + yi2) |
313 |
proji3 = proji*proji*proji |
314 |
dcpidx = 1.0_dp / proji - xi2 / proji3 |
315 |
dcpidy = - xi * yi / proji3 |
316 |
dcpidux = xi / proji - (xi2 * xi) / proji3 |
317 |
dcpiduy = - (xi * yi2) / proji3 |
318 |
dspidx = - xi * yi / proji3 |
319 |
dspidy = 1.0_dp / proji - yi2 / proji3 |
320 |
dspidux = - (yi * xi2) / proji3 |
321 |
dspiduy = yi / proji - (yi2 * yi) / proji3 |
322 |
endif |
323 |
|
324 |
cpi = xi / proji |
325 |
dcpidz = 0.0_dp |
326 |
dcpiduz = 0.0_dp |
327 |
|
328 |
spi = yi / proji |
329 |
dspidz = 0.0_dp |
330 |
dspiduz = 0.0_dp |
331 |
|
332 |
|
333 |
|
334 |
|
335 |
RealType sigma0 = mixer.sigma0; |
336 |
RealType dw = mixer.dw; |
337 |
RealType eps0 = mixer.eps0; |
338 |
RealType x2 = mixer.x2; |
339 |
RealType xa2 = mixer.xa2; |
340 |
RealType xai2 = mixer.xai2; |
341 |
RealType xp2 = mixer.xp2; |
342 |
RealType xpap2 = mixer.xpap2; |
343 |
RealType xpapi2 = mixer.xpapi2; |
344 |
|
345 |
Vector3d ul1 = A1.getRow(2); |
346 |
Vector3d ul2 = A2.getRow(2); |
347 |
|
348 |
RealType a, b, g; |
349 |
|
350 |
|
351 |
if (i_is_LJ) { |
352 |
a = 0.0; |
353 |
ul1 = V3Zero; |
354 |
} else { |
355 |
a = dot(d, ul1); |
356 |
} |
357 |
|
358 |
if (j_is_LJ) { |
359 |
b = 0.0; |
360 |
ul2 = V3Zero; |
361 |
} else { |
362 |
b = dot(d, ul2); |
363 |
} |
364 |
|
365 |
if (i_is_LJ || j_is_LJ) |
366 |
g = 0.0; |
367 |
else |
368 |
g = dot(ul1, ul2); |
369 |
|
370 |
RealType au = a / r; |
371 |
RealType bu = b / r; |
372 |
|
373 |
RealType au2 = au * au; |
374 |
RealType bu2 = bu * bu; |
375 |
RealType g2 = g * g; |
376 |
|
377 |
RealType H = (xa2 * au2 + xai2 * bu2 - 2.0*x2*au*bu*g) / (1.0 - x2*g2); |
378 |
RealType Hp = (xpap2*au2 + xpapi2*bu2 - 2.0*xp2*au*bu*g) / (1.0 - xp2*g2); |
379 |
|
380 |
RealType sigma = sigma0 / sqrt(1.0 - H); |
381 |
RealType e1 = 1.0 / sqrt(1.0 - x2*g2); |
382 |
RealType e2 = 1.0 - Hp; |
383 |
RealType eps = eps0 * pow(e1,nu_) * pow(e2,mu_); |
384 |
RealType BigR = dw*sigma0 / (r - sigma + dw*sigma0); |
385 |
|
386 |
RealType R3 = BigR*BigR*BigR; |
387 |
RealType R6 = R3*R3; |
388 |
RealType R7 = R6 * BigR; |
389 |
RealType R12 = R6*R6; |
390 |
RealType R13 = R6*R7; |
391 |
|
392 |
RealType U = vdwMult * 4.0 * eps * (R12 - R6); |
393 |
|
394 |
RealType s3 = sigma*sigma*sigma; |
395 |
RealType s03 = sigma0*sigma0*sigma0; |
396 |
|
397 |
RealType pref1 = - vdwMult * 8.0 * eps * mu_ * (R12 - R6) / (e2 * r); |
398 |
|
399 |
RealType pref2 = vdwMult * 8.0 * eps * s3 * (6.0*R13 - 3.0*R7) /(dw*r*s03); |
400 |
|
401 |
RealType dUdr = - (pref1 * Hp + pref2 * (sigma0*sigma0*r/s3 + H)); |
402 |
|
403 |
RealType dUda = pref1 * (xpap2*au - xp2*bu*g) / (1.0 - xp2 * g2) |
404 |
+ pref2 * (xa2 * au - x2 *bu*g) / (1.0 - x2 * g2); |
405 |
|
406 |
RealType dUdb = pref1 * (xpapi2*bu - xp2*au*g) / (1.0 - xp2 * g2) |
407 |
+ pref2 * (xai2 * bu - x2 *au*g) / (1.0 - x2 * g2); |
408 |
|
409 |
RealType dUdg = 4.0 * eps * nu_ * (R12 - R6) * x2 * g / (1.0 - x2*g2) |
410 |
+ 8.0 * eps * mu_ * (R12 - R6) * (xp2*au*bu - Hp*xp2*g) / |
411 |
(1.0 - xp2 * g2) / e2 + 8.0 * eps * s3 * (3.0 * R7 - 6.0 * R13) * |
412 |
(x2 * au * bu - H * x2 * g) / (1.0 - x2 * g2) / (dw * s03); |
413 |
|
414 |
|
415 |
Vector3d rhat = d / r; |
416 |
Vector3d rxu1 = cross(d, ul1); |
417 |
Vector3d rxu2 = cross(d, ul2); |
418 |
Vector3d uxu = cross(ul1, ul2); |
419 |
|
420 |
pot += U*sw; |
421 |
f1 += dUdr * rhat + dUda * ul1 + dUdb * ul2; |
422 |
t1 += dUda * rxu1 - dUdg * uxu; |
423 |
t2 += dUdb * rxu2 - dUdg * uxu; |
424 |
vpair += U*sw; |
425 |
|
426 |
return; |
427 |
|
428 |
} |
429 |
|
430 |
void SHAPES::do_gb_pair(int *atid1, int *atid2, RealType *d, RealType *r, |
431 |
RealType *r2, RealType *sw, RealType *vdwMult, |
432 |
RealType *vpair, RealType *pot, RealType *A1, |
433 |
RealType *A2, RealType *f1, RealType *t1, RealType *t2) { |
434 |
|
435 |
if (!initialized_) initialize(); |
436 |
|
437 |
AtomType* atype1 = SHAPESMap[*atid1]; |
438 |
AtomType* atype2 = SHAPESMap[*atid2]; |
439 |
|
440 |
Vector3d disp(d); |
441 |
Vector3d frc(f1); |
442 |
Vector3d trq1(t1); |
443 |
Vector3d trq2(t2); |
444 |
RotMat3x3d Ai(A1); |
445 |
RotMat3x3d Aj(A2); |
446 |
|
447 |
// Fortran has the opposite matrix ordering from c++, so we'll use |
448 |
// transpose here. When we finish the conversion to C++, this wrapper |
449 |
// will disappear, as will the transpose below: |
450 |
|
451 |
calcForce(atype1, atype2, disp, *r, *r2, *sw, *vdwMult, *vpair, *pot, |
452 |
Ai, Aj, frc, trq1, trq1); |
453 |
|
454 |
f1[0] = frc.x(); |
455 |
f1[1] = frc.y(); |
456 |
f1[2] = frc.z(); |
457 |
|
458 |
t1[0] = trq1.x(); |
459 |
t1[1] = trq1.y(); |
460 |
t1[2] = trq1.z(); |
461 |
|
462 |
t2[0] = trq2.x(); |
463 |
t2[1] = trq2.y(); |
464 |
t2[2] = trq2.z(); |
465 |
|
466 |
return; |
467 |
} |
468 |
} |
469 |
|
470 |
extern "C" { |
471 |
|
472 |
#define fortranGetGayBerneCut FC_FUNC(getgaybernecut, GETGAYBERNECUT) |
473 |
#define fortranDoSHAPESPair FC_FUNC(do_gb_pair, DO_SHAPES_PAIR) |
474 |
|
475 |
RealType fortranGetGayBerneCut(int* atid) { |
476 |
return OpenMD::SHAPES::Instance()->getGayBerneCut(*atid); |
477 |
} |
478 |
|
479 |
void fortranDoSHAPESPair(int *atid1, int *atid2, RealType *d, RealType *r, |
480 |
RealType *r2, RealType *sw, RealType *vdwMult, |
481 |
RealType *vpair, RealType *pot, RealType *A1, |
482 |
RealType *A2, RealType *f1, RealType *t1, RealType *t2){ |
483 |
|
484 |
return OpenMD::SHAPES::Instance()->do_gb_pair(atid1, atid2, d, r, r2, sw, |
485 |
vdwMult, vpair, pot, A1, A2, f1, |
486 |
t1, t2); |
487 |
} |
488 |
} |