ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/nonbonded/SHAPES.cpp
Revision: 1874
Committed: Wed May 15 15:09:35 2013 UTC (12 years ago) by gezelter
File size: 14242 byte(s)
Log Message:
Fixed a bunch of cppcheck warnings.

File Contents

# User Rev Content
1 gezelter 1501 /*
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 gezelter 1850 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
39 gezelter 1665 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40     * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 gezelter 1501 */
42    
43     #include <stdio.h>
44     #include <string.h>
45    
46     #include <cmath>
47     #include "nonbonded/SHAPES.hpp"
48     #include "nonbonded/LJ.hpp"
49     #include "utils/simError.h"
50    
51     using namespace std;
52     namespace OpenMD {
53 gezelter 1505
54 gezelter 1502 SHAPES::SHAPES() {
55     initialized_ = false;
56     lMax_ = 64;
57     mMax_ = 64;
58     forceField_ = NULL;
59 gezelter 1501 }
60 gezelter 1502
61 gezelter 1501 void SHAPES::initialize() {
62    
63     ForceFieldOptions& fopts = forceField_->getForceFieldOptions();
64     ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes();
65     ForceField::AtomTypeContainer::MapTypeIterator i;
66     AtomType* at;
67    
68     // SHAPES handles all of the SHAPES-SHAPES interactions as well as
69     // SHAPES-LJ cross interactions:
70 gezelter 1502
71 gezelter 1501 for (at = atomTypes->beginType(i); at != NULL;
72     at = atomTypes->nextType(i)) {
73    
74 gezelter 1505 if (at->isShape())
75     addShape(dynamic_cast<ShapeAtomType*>(at));
76    
77     if (at->isLennardJones())
78     addLJ(at);
79    
80 gezelter 1501 }
81 gezelter 1502
82 gezelter 1501 initialized_ = true;
83     }
84 gezelter 1502
85 gezelter 1505 void SHAPES::addShape(ShapeAtomType* atomType){
86 gezelter 1501 // add it to the map:
87     AtomTypeProperties atp = atomType->getATP();
88 gezelter 1505
89 gezelter 1635 if (atomType->isShape() ) {
90     pair<map<int,ShapeAtomType*>::iterator, bool> ret;
91     ret = ShapesMap.insert( pair<int, ShapeAtomType*>(atp.ident, atomType));
92     if (ret.second == false) {
93     sprintf( painCave.errMsg,
94     "SHAPES already had a previous entry with ident %d\n",
95     atp.ident);
96     painCave.severity = OPENMD_INFO;
97     painCave.isFatal = 0;
98     simError();
99     }
100    
101     ShapesMap.insert( pair<int, ShapeAtomType*>(atp.ident, static_cast<ShapeAtomType*>(atomType)) );
102    
103     } else if (atomType->isLennardJones()) {
104     RealType d1 = getLJSigma(atomType) / sqrt(2.0);
105     RealType e1 = getLJEpsilon(atomType);
106 gezelter 1501 } else {
107     sprintf( painCave.errMsg,
108     "SHAPES::addType was passed an atomType (%s) that does not\n"
109 gezelter 1502 "\tappear to be a SHAPES or Lennard-Jones atom.\n",
110 gezelter 1501 atomType->getName().c_str());
111     painCave.severity = OPENMD_ERROR;
112     painCave.isFatal = 1;
113     simError();
114 gezelter 1505 }
115 gezelter 1501 }
116    
117    
118 gezelter 1502 LJParam SHAPES::getLJParam(AtomType* atomType) {
119    
120     // Do sanity checking on the AtomType we were passed before
121     // building any data structures:
122     if (!atomType->isLennardJones()) {
123     sprintf( painCave.errMsg,
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     sprintf( painCave.errMsg, "SHAPES::getLJParam could not find Lennard-Jones\n"
135     "\tparameters for atomType %s.\n", atomType->getName().c_str());
136     painCave.severity = OPENMD_ERROR;
137     painCave.isFatal = 1;
138     simError();
139     }
140    
141     LJParamGenericData* ljData = dynamic_cast<LJParamGenericData*>(data);
142     if (ljData == NULL) {
143     sprintf( painCave.errMsg,
144     "SHAPES::getLJParam could not convert GenericData to LJParam for\n"
145     "\tatom type %s\n", atomType->getName().c_str());
146     painCave.severity = OPENMD_ERROR;
147     painCave.isFatal = 1;
148     simError();
149     }
150    
151     return ljData->getData();
152     }
153    
154     RealType SHAPES::getLJEpsilon(AtomType* atomType) {
155     LJParam ljParam = getLJParam(atomType);
156     return ljParam.epsilon;
157     }
158     RealType SHAPES::getLJSigma(AtomType* atomType) {
159     LJParam ljParam = getLJParam(atomType);
160     return ljParam.sigma;
161     }
162    
163 gezelter 1501 RealType SHAPES::getGayBerneCut(int atid) {
164     if (!initialized_) initialize();
165     std::map<int, AtomType*> :: const_iterator it;
166     it = SHAPESMap.find(atid);
167     if (it == SHAPESMap.end()) {
168     sprintf( painCave.errMsg,
169     "SHAPES::getGayBerneCut could not find atid %d in SHAPESMap\n",
170     (atid));
171     painCave.severity = OPENMD_ERROR;
172     painCave.isFatal = 1;
173     simError();
174     }
175    
176     AtomType* atype = it->second;
177    
178     RealType gbCut;
179    
180     if (atype->isGayBerne()) {
181     GayBerneParam gb = getGayBerneParam(atype);
182    
183     // sigma is actually sqrt(2) * l for prolate ellipsoids
184     gbCut = 2.5 * sqrt(2.0) * max(gb.SHAPES_l, gb.SHAPES_d);
185    
186     } else if (atype->isLennardJones()) {
187     gbCut = 2.5 * LJ::Instance()->getSigma(atype);
188     }
189    
190     return gbCut;
191     }
192    
193    
194     void SHAPES::calcForce(AtomType* at1, AtomType* at2, Vector3d d,
195     RealType r, RealType r2, RealType sw,
196     RealType &vpair, RealType &pot,
197     RotMat3x3d A1, RotMat3x3d A2, Vector3d &f1,
198     Vector3d &t1, Vector3d &t2) {
199    
200     if (!initialized_) initialize();
201    
202     pair<AtomType*, AtomType*> key = make_pair(at1, at2);
203     SHAPESInteractionData mixer = MixingMap[key];
204    
205     RealType r3 = r2 * r;
206     RealType r5 = r3 * r2;
207    
208     Vector3d drdi = -d / r;
209     Vector3d drdui = V3Zero;
210     Vector3d drdj = d / r;
211     Vector3d drduj = V3Zero;
212    
213     bool i_is_LJ = at1->isLennardJones();
214     bool j_is_LJ = at2->isLennardJones();
215    
216     RealType sigma_i;
217     RealType s_i;
218     RealType eps_i;
219     Vector3d dsigmaidr;
220     Vector3d disgmaidu;
221     Vector3d dsidr;
222     Vector3d dsidu;
223     Vector3d depsidr;
224     Vector3d depsidu;
225    
226     if (i_is_LJ) {
227     sigma_i = LJ::Instance()->getSigma(at1);
228     s_i = sigma_i;
229     epsilon_i = LJ::Instance()->getEpsilon(at1);
230     dsigmaidr = V3Zero;
231     dsigmaidu = V3Zero;
232     dsidr = V3Zero;
233     dsidu = V3Zero;
234     depsidr = V3Zero;
235     depsidu = V3Zero;
236     } else {
237    
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,
255     -zi * yi / r3,
256     1.0 / r - zi2 / r3);
257    
258     Vector3d dctidu(yi / r,
259     -zi / r,
260     0.0);
261    
262     // this is an attempt to try to truncate the singularity when
263     // sin(theta) is near 0.0:
264    
265     RealType sti2 = 1.0 - cti*cti;
266     if (fabs(sti2) < 1.0e-12) {
267     RealType proji = sqrt(r * 1.0e-12);
268     Vector3d dcpidx(1.0 / proji,
269     0.0,
270 gezelter 1874
271     // pickup the ball here!
272 gezelter 1501
273     dcpidx = 1.0_dp / proji
274     dcpidy = 0.0_dp
275     dcpidux = xi / proji
276     dcpiduy = 0.0_dp
277     dspidx = 0.0_dp
278     dspidy = 1.0_dp / proji
279     dspidux = 0.0_dp
280     dspiduy = yi / proji
281     else
282     proji = sqrt(xi2 + yi2)
283     proji3 = proji*proji*proji
284     dcpidx = 1.0_dp / proji - xi2 / proji3
285     dcpidy = - xi * yi / proji3
286     dcpidux = xi / proji - (xi2 * xi) / proji3
287     dcpiduy = - (xi * yi2) / proji3
288     dspidx = - xi * yi / proji3
289     dspidy = 1.0_dp / proji - yi2 / proji3
290     dspidux = - (yi * xi2) / proji3
291     dspiduy = yi / proji - (yi2 * yi) / proji3
292     endif
293    
294     cpi = xi / proji
295     dcpidz = 0.0_dp
296     dcpiduz = 0.0_dp
297    
298     spi = yi / proji
299     dspidz = 0.0_dp
300     dspiduz = 0.0_dp
301    
302    
303    
304    
305     RealType sigma0 = mixer.sigma0;
306     RealType dw = mixer.dw;
307     RealType eps0 = mixer.eps0;
308     RealType x2 = mixer.x2;
309     RealType xa2 = mixer.xa2;
310     RealType xai2 = mixer.xai2;
311     RealType xp2 = mixer.xp2;
312     RealType xpap2 = mixer.xpap2;
313     RealType xpapi2 = mixer.xpapi2;
314    
315     Vector3d ul1 = A1.getRow(2);
316     Vector3d ul2 = A2.getRow(2);
317    
318     RealType a, b, g;
319    
320    
321     if (i_is_LJ) {
322     a = 0.0;
323     ul1 = V3Zero;
324     } else {
325     a = dot(d, ul1);
326     }
327    
328     if (j_is_LJ) {
329     b = 0.0;
330     ul2 = V3Zero;
331     } else {
332     b = dot(d, ul2);
333     }
334    
335     if (i_is_LJ || j_is_LJ)
336     g = 0.0;
337     else
338     g = dot(ul1, ul2);
339    
340     RealType au = a / r;
341     RealType bu = b / r;
342    
343     RealType au2 = au * au;
344     RealType bu2 = bu * bu;
345     RealType g2 = g * g;
346    
347     RealType H = (xa2 * au2 + xai2 * bu2 - 2.0*x2*au*bu*g) / (1.0 - x2*g2);
348     RealType Hp = (xpap2*au2 + xpapi2*bu2 - 2.0*xp2*au*bu*g) / (1.0 - xp2*g2);
349    
350     RealType sigma = sigma0 / sqrt(1.0 - H);
351     RealType e1 = 1.0 / sqrt(1.0 - x2*g2);
352     RealType e2 = 1.0 - Hp;
353     RealType eps = eps0 * pow(e1,nu_) * pow(e2,mu_);
354     RealType BigR = dw*sigma0 / (r - sigma + dw*sigma0);
355    
356     RealType R3 = BigR*BigR*BigR;
357     RealType R6 = R3*R3;
358     RealType R7 = R6 * BigR;
359     RealType R12 = R6*R6;
360     RealType R13 = R6*R7;
361    
362     RealType U = vdwMult * 4.0 * eps * (R12 - R6);
363    
364     RealType s3 = sigma*sigma*sigma;
365     RealType s03 = sigma0*sigma0*sigma0;
366    
367     RealType pref1 = - vdwMult * 8.0 * eps * mu_ * (R12 - R6) / (e2 * r);
368    
369     RealType pref2 = vdwMult * 8.0 * eps * s3 * (6.0*R13 - 3.0*R7) /(dw*r*s03);
370    
371     RealType dUdr = - (pref1 * Hp + pref2 * (sigma0*sigma0*r/s3 + H));
372    
373     RealType dUda = pref1 * (xpap2*au - xp2*bu*g) / (1.0 - xp2 * g2)
374     + pref2 * (xa2 * au - x2 *bu*g) / (1.0 - x2 * g2);
375    
376     RealType dUdb = pref1 * (xpapi2*bu - xp2*au*g) / (1.0 - xp2 * g2)
377     + pref2 * (xai2 * bu - x2 *au*g) / (1.0 - x2 * g2);
378    
379     RealType dUdg = 4.0 * eps * nu_ * (R12 - R6) * x2 * g / (1.0 - x2*g2)
380     + 8.0 * eps * mu_ * (R12 - R6) * (xp2*au*bu - Hp*xp2*g) /
381     (1.0 - xp2 * g2) / e2 + 8.0 * eps * s3 * (3.0 * R7 - 6.0 * R13) *
382     (x2 * au * bu - H * x2 * g) / (1.0 - x2 * g2) / (dw * s03);
383    
384    
385     Vector3d rhat = d / r;
386     Vector3d rxu1 = cross(d, ul1);
387     Vector3d rxu2 = cross(d, ul2);
388     Vector3d uxu = cross(ul1, ul2);
389    
390     pot += U*sw;
391     f1 += dUdr * rhat + dUda * ul1 + dUdb * ul2;
392     t1 += dUda * rxu1 - dUdg * uxu;
393     t2 += dUdb * rxu2 - dUdg * uxu;
394     vpair += U*sw;
395    
396     return;
397    
398     }
399    
400     void SHAPES::do_gb_pair(int *atid1, int *atid2, RealType *d, RealType *r,
401     RealType *r2, RealType *sw, RealType *vdwMult,
402     RealType *vpair, RealType *pot, RealType *A1,
403     RealType *A2, RealType *f1, RealType *t1, RealType *t2) {
404    
405     if (!initialized_) initialize();
406    
407     AtomType* atype1 = SHAPESMap[*atid1];
408     AtomType* atype2 = SHAPESMap[*atid2];
409    
410     Vector3d disp(d);
411     Vector3d frc(f1);
412     Vector3d trq1(t1);
413     Vector3d trq2(t2);
414     RotMat3x3d Ai(A1);
415     RotMat3x3d Aj(A2);
416    
417     // Fortran has the opposite matrix ordering from c++, so we'll use
418     // transpose here. When we finish the conversion to C++, this wrapper
419     // will disappear, as will the transpose below:
420    
421     calcForce(atype1, atype2, disp, *r, *r2, *sw, *vdwMult, *vpair, *pot,
422     Ai, Aj, frc, trq1, trq1);
423    
424     f1[0] = frc.x();
425     f1[1] = frc.y();
426     f1[2] = frc.z();
427    
428     t1[0] = trq1.x();
429     t1[1] = trq1.y();
430     t1[2] = trq1.z();
431    
432     t2[0] = trq2.x();
433     t2[1] = trq2.y();
434     t2[2] = trq2.z();
435    
436     return;
437     }
438     }
439    
440     extern "C" {
441    
442     #define fortranGetGayBerneCut FC_FUNC(getgaybernecut, GETGAYBERNECUT)
443     #define fortranDoSHAPESPair FC_FUNC(do_gb_pair, DO_SHAPES_PAIR)
444    
445     RealType fortranGetGayBerneCut(int* atid) {
446     return OpenMD::SHAPES::Instance()->getGayBerneCut(*atid);
447     }
448    
449     void fortranDoSHAPESPair(int *atid1, int *atid2, RealType *d, RealType *r,
450     RealType *r2, RealType *sw, RealType *vdwMult,
451     RealType *vpair, RealType *pot, RealType *A1,
452     RealType *A2, RealType *f1, RealType *t1, RealType *t2){
453    
454     return OpenMD::SHAPES::Instance()->do_gb_pair(atid1, atid2, d, r, r2, sw,
455     vdwMult, vpair, pot, A1, A2, f1,
456     t1, t2);
457     }
458     }

Properties

Name Value
svn:eol-style native