ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/nonbonded/SHAPES.cpp
Revision: 1635
Committed: Thu Sep 15 16:24:03 2011 UTC (13 years, 7 months ago) by gezelter
File size: 14125 byte(s)
Log Message:
cleaning up the development branch a bit, removing cruft, etc.

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

Properties

Name Value
svn:eol-style native