ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/nonbonded/GB.cpp
Revision: 1483
Committed: Tue Jul 27 21:17:31 2010 UTC (14 years, 10 months ago) by gezelter
File size: 13619 byte(s)
Log Message:
Added GB module to the C++ side, got rid of it on the fortran side.

File Contents

# User Rev Content
1 gezelter 1483 /*
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/GB.hpp"
47     #include "nonbonded/LJ.hpp"
48     #include "utils/simError.h"
49    
50     using namespace std;
51     namespace OpenMD {
52    
53     bool GB::initialized_ = false;
54     RealType GB::mu_ = 2.0;
55     RealType GB::nu_ = 1.0;
56     ForceField* GB::forceField_ = NULL;
57     map<int, AtomType*> GB::GBMap;
58     map<pair<AtomType*, AtomType*>, GBInteractionData> GB::MixingMap;
59    
60     GB* GB::_instance = NULL;
61    
62     GB* GB::Instance() {
63     if (!_instance) {
64     _instance = new GB();
65     }
66     return _instance;
67     }
68    
69     GayBerneParam GB::getGayBerneParam(AtomType* atomType) {
70    
71     // Do sanity checking on the AtomType we were passed before
72     // building any data structures:
73     if (!atomType->isGayBerne()) {
74     sprintf( painCave.errMsg,
75     "GB::getGayBerneParam was passed an atomType (%s) that does\n"
76     "\tnot appear to be a Gay-Berne atom.\n",
77     atomType->getName().c_str());
78     painCave.severity = OPENMD_ERROR;
79     painCave.isFatal = 1;
80     simError();
81     }
82    
83     DirectionalAtomType* daType = dynamic_cast<DirectionalAtomType*>(atomType);
84     GenericData* data = daType->getPropertyByName("GayBerne");
85     if (data == NULL) {
86     sprintf( painCave.errMsg, "GB::getGayBerneParam could not find\n"
87     "\tGay-Berne parameters for atomType %s.\n",
88     daType->getName().c_str());
89     painCave.severity = OPENMD_ERROR;
90     painCave.isFatal = 1;
91     simError();
92     }
93    
94     GayBerneParamGenericData* gbData = dynamic_cast<GayBerneParamGenericData*>(data);
95     if (gbData == NULL) {
96     sprintf( painCave.errMsg,
97     "GB::getGayBerneParam could not convert GenericData to\n"
98     "\tGayBerneParamGenericData for atom type %s\n",
99     daType->getName().c_str());
100     painCave.severity = OPENMD_ERROR;
101     painCave.isFatal = 1;
102     simError();
103     }
104    
105     return gbData->getData();
106     }
107    
108     void GB::initialize() {
109     ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes();
110     ForceField::AtomTypeContainer::MapTypeIterator i;
111     AtomType* at;
112    
113     // GB handles all of the GB-GB interactions as well as GB-LJ cross
114     // interactions:
115    
116     for (at = atomTypes->beginType(i); at != NULL;
117     at = atomTypes->nextType(i)) {
118    
119     if (at->isGayBerne() || at->isLennardJones())
120     addType(at);
121     }
122    
123     initialized_ = true;
124     }
125    
126     void GB::addType(AtomType* atomType){
127     // add it to the map:
128     AtomTypeProperties atp = atomType->getATP();
129    
130     pair<map<int,AtomType*>::iterator,bool> ret;
131     ret = GBMap.insert( pair<int, AtomType*>(atp.ident, atomType) );
132     if (ret.second == false) {
133     sprintf( painCave.errMsg,
134     "GB already had a previous entry with ident %d\n",
135     atp.ident);
136     painCave.severity = OPENMD_INFO;
137     painCave.isFatal = 0;
138     simError();
139     }
140    
141     RealType d1, l1, e1, er1, dw1;
142    
143     if (atomType->isGayBerne()) {
144     GayBerneParam gb1 = getGayBerneParam(atomType);
145     d1 = gb1.GB_d;
146     l1 = gb1.GB_l;
147     e1 = gb1.GB_eps;
148     er1 = gb1.GB_eps_ratio;
149     dw1 = gb1.GB_dw;
150     } else if (atomType->isLennardJones()) {
151     d1 = LJ::Instance()->getSigma(atomType) / sqrt(2.0);
152     e1 = LJ::Instance()->getEpsilon(atomType);
153     l1 = d1;
154     er1 = 1.0;
155     dw1 = 1.0;
156     } else {
157     sprintf( painCave.errMsg,
158     "GB::addType was passed an atomType (%s) that does not\n"
159     "\tappear to be a Gay-Berne or Lennard-Jones atom.\n",
160     atomType->getName().c_str());
161     painCave.severity = OPENMD_ERROR;
162     painCave.isFatal = 1;
163     simError();
164     }
165    
166    
167     // Now, iterate over all known types and add to the mixing map:
168    
169     map<int, AtomType*>::iterator it;
170     for( it = GBMap.begin(); it != GBMap.end(); ++it) {
171    
172     AtomType* atype2 = (*it).second;
173    
174     RealType d2, l2, e2, er2, dw2;
175    
176     if (atype2->isGayBerne()) {
177     GayBerneParam gb2 = getGayBerneParam(atype2);
178     d2 = gb2.GB_d;
179     l2 = gb2.GB_l;
180     e2 = gb2.GB_eps;
181     er2 = gb2.GB_eps_ratio;
182     dw2 = gb2.GB_dw;
183     } else if (atype2->isLennardJones()) {
184     d2 = LJ::Instance()->getSigma(atype2) / sqrt(2.0);
185     e2 = LJ::Instance()->getEpsilon(atype2);
186     l2 = d2;
187     er2 = 1.0;
188     dw2 = 1.0;
189     }
190    
191     GBInteractionData mixer;
192    
193     // Cleaver paper uses sqrt of squares to get sigma0 for
194     // mixed interactions.
195    
196     mixer.sigma0 = sqrt(d1*d1 + d2*d2);
197     mixer.xa2 = (l1*l1 - d1*d1)/(l1*l1 + d2*d2);
198     mixer.xai2 = (l2*l2 - d2*d2)/(l2*l2 + d1*d1);
199     mixer.x2 = (l1*l1 - d1*d1) * (l2*l2 - d2*d2) /
200     ((l2*l2 + d1*d1) * (l1*l1 + d2*d2));
201    
202     // assumed LB mixing rules for now:
203    
204     mixer.dw = 0.5 * (dw1 + dw2);
205     mixer.eps0 = sqrt(e1 * e2);
206    
207     RealType er = sqrt(er1 * er2);
208     RealType ermu = pow(er,(1.0 / mu_));
209     RealType xp = (1.0 - ermu) / (1.0 + ermu);
210     RealType ap2 = 1.0 / (1.0 + ermu);
211    
212     mixer.xp2 = xp * xp;
213     mixer.xpap2 = xp * ap2;
214     mixer.xpapi2 = xp / ap2;
215    
216     // only add this pairing if at least one of the atoms is a Gay-Berne atom
217    
218     if (atomType->isGayBerne() || atype2->isGayBerne()) {
219    
220     pair<AtomType*, AtomType*> key1, key2;
221     key1 = make_pair(atomType, atype2);
222     key2 = make_pair(atype2, atomType);
223    
224     MixingMap[key1] = mixer;
225     if (key2 != key1) {
226     MixingMap[key2] = mixer;
227     }
228     }
229     }
230     }
231    
232    
233     RealType GB::getGayBerneCut(int atid) {
234     if (!initialized_) initialize();
235     std::map<int, AtomType*> :: const_iterator it;
236     it = GBMap.find(atid);
237     if (it == GBMap.end()) {
238     sprintf( painCave.errMsg,
239     "GB::getGayBerneCut could not find atid %d in GBMap\n",
240     (atid));
241     painCave.severity = OPENMD_ERROR;
242     painCave.isFatal = 1;
243     simError();
244     }
245    
246     AtomType* atype = it->second;
247    
248     RealType gbCut;
249    
250     if (atype->isGayBerne()) {
251     GayBerneParam gb = getGayBerneParam(atype);
252    
253     // sigma is actually sqrt(2) * l for prolate ellipsoids
254     gbCut = 2.5 * sqrt(2.0) * max(gb.GB_l, gb.GB_d);
255    
256     } else if (atype->isLennardJones()) {
257     gbCut = 2.5 * LJ::Instance()->getSigma(atype);
258     }
259    
260     return gbCut;
261     }
262    
263    
264     void GB::calcForce(AtomType* at1, AtomType* at2, Vector3d d,
265     RealType r, RealType r2, RealType sw,
266     RealType vdwMult, RealType &vpair, RealType &pot,
267     RotMat3x3d A1, RotMat3x3d A2, Vector3d &f1,
268     Vector3d &t1, Vector3d &t2) {
269    
270     if (!initialized_) initialize();
271    
272     pair<AtomType*, AtomType*> key = make_pair(at1, at2);
273     GBInteractionData mixer = MixingMap[key];
274    
275     RealType sigma0 = mixer.sigma0;
276     RealType dw = mixer.dw;
277     RealType eps0 = mixer.eps0;
278     RealType x2 = mixer.x2;
279     RealType xa2 = mixer.xa2;
280     RealType xai2 = mixer.xai2;
281     RealType xp2 = mixer.xp2;
282     RealType xpap2 = mixer.xpap2;
283     RealType xpapi2 = mixer.xpapi2;
284    
285     Vector3d ul1 = A1.getColumn(2);
286     Vector3d ul2 = A2.getColumn(2);
287    
288     RealType a, b, g;
289    
290     bool i_is_LJ = at1->isLennardJones();
291     bool j_is_LJ = at2->isLennardJones();
292    
293     if (i_is_LJ) {
294     a = 0.0;
295     ul1 = V3Zero;
296     } else {
297     a = dot(d, ul1);
298     }
299    
300     if (j_is_LJ) {
301     b = 0.0;
302     ul2 = V3Zero;
303     } else {
304     b = dot(d, ul2);
305     }
306    
307     if (i_is_LJ || j_is_LJ)
308     g = 0.0;
309     else
310     g = dot(ul1, ul2);
311    
312     RealType au = a / r;
313     RealType bu = b / r;
314    
315     RealType au2 = au * au;
316     RealType bu2 = bu * bu;
317     RealType g2 = g * g;
318    
319     RealType H = (xa2 * au2 + xai2 * bu2 - 2.0*x2*au*bu*g) / (1.0 - x2*g2);
320     RealType Hp = (xpap2*au2 + xpapi2*bu2 - 2.0*xp2*au*bu*g) / (1.0 - xp2*g2);
321    
322     RealType sigma = sigma0 / sqrt(1.0 - H);
323     RealType e1 = 1.0 / sqrt(1.0 - x2*g2);
324     RealType e2 = 1.0 - Hp;
325     RealType eps = eps0 * pow(e1,nu_) * pow(e2,mu_);
326     RealType BigR = dw*sigma0 / (r - sigma + dw*sigma0);
327    
328     RealType R3 = BigR*BigR*BigR;
329     RealType R6 = R3*R3;
330     RealType R7 = R6 * BigR;
331     RealType R12 = R6*R6;
332     RealType R13 = R6*R7;
333    
334     RealType U = vdwMult * 4.0 * eps * (R12 - R6);
335    
336     RealType s3 = sigma*sigma*sigma;
337     RealType s03 = sigma0*sigma0*sigma0;
338    
339     RealType pref1 = - vdwMult * 8.0 * eps * mu_ * (R12 - R6) / (e2 * r);
340    
341     RealType pref2 = vdwMult * 8.0 * eps * s3 * (6.0*R13 - 3.0*R7) /(dw*r*s03);
342    
343     RealType dUdr = - (pref1 * Hp + pref2 * (sigma0*sigma0*r/s3 + H));
344    
345     RealType dUda = pref1 * (xpap2*au - xp2*bu*g) / (1.0 - xp2 * g2)
346     + pref2 * (xa2 * au - x2 *bu*g) / (1.0 - x2 * g2);
347    
348     RealType dUdb = pref1 * (xpapi2*bu - xp2*au*g) / (1.0 - xp2 * g2)
349     + pref2 * (xai2 * bu - x2 *au*g) / (1.0 - x2 * g2);
350    
351     RealType dUdg = 4.0 * eps * nu_ * (R12 - R6) * x2 * g / (1.0 - x2*g2)
352     + 8.0 * eps * mu_ * (R12 - R6) * (xp2*au*bu - Hp*xp2*g) /
353     (1.0 - xp2 * g2) / e2 + 8.0 * eps * s3 * (3.0 * R7 - 6.0 * R13) *
354     (x2 * au * bu - H * x2 * g) / (1.0 - x2 * g2) / (dw * s03);
355    
356    
357     Vector3d rhat = d / r;
358     Vector3d rxu1 = cross(d, ul1);
359     Vector3d rxu2 = cross(d, ul2);
360     Vector3d uxu = cross(ul1, ul2);
361    
362     pot += U*sw;
363     f1 += dUdr * rhat + dUda * ul1 + dUdb * ul2;
364     t1 += dUda * rxu1 - dUdg * uxu;
365     t2 += dUdb * rxu2 - dUdg * uxu;
366     vpair += U*sw;
367    
368     return;
369    
370     }
371    
372     void GB::do_gb_pair(int *atid1, int *atid2, RealType *d, RealType *r,
373     RealType *r2, RealType *sw, RealType *vdwMult,
374     RealType *vpair, RealType *pot, RealType *A1,
375     RealType *A2, RealType *f1, RealType *t1, RealType *t2) {
376    
377     if (!initialized_) initialize();
378    
379     AtomType* atype1 = GBMap[*atid1];
380     AtomType* atype2 = GBMap[*atid2];
381    
382     Vector3d disp(d);
383     Vector3d frc(f1);
384     Vector3d trq1(t1);
385     Vector3d trq2(t2);
386     RotMat3x3d Ai(A1);
387     RotMat3x3d Aj(A2);
388    
389     // Fortran has the opposite matrix ordering from c++, so we'll use
390     // transpose here. When we finish the conversion to C++, this wrapper
391     // will disappear, as will the transpose below:
392    
393     calcForce(atype1, atype2, disp, *r, *r2, *sw, *vdwMult, *vpair, *pot,
394     Ai.transpose(), Aj.transpose(), frc, trq1, trq1);
395    
396     f1[0] = frc.x();
397     f1[1] = frc.y();
398     f1[2] = frc.z();
399    
400     t1[0] = trq1.x();
401     t1[1] = trq1.y();
402     t1[2] = trq1.z();
403    
404     t2[0] = trq2.x();
405     t2[1] = trq2.y();
406     t2[2] = trq2.z();
407    
408     return;
409     }
410     }
411    
412     extern "C" {
413    
414     #define fortranGetGayBerneCut FC_FUNC(getgaybernecut, GETGAYBERNECUT)
415     #define fortranDoGBPair FC_FUNC(do_gb_pair, DO_GB_PAIR)
416    
417     RealType fortranGetGayBerneCut(int* atid) {
418     return OpenMD::GB::Instance()->getGayBerneCut(*atid);
419     }
420    
421     void fortranDoGBPair(int *atid1, int *atid2, RealType *d, RealType *r,
422     RealType *r2, RealType *sw, RealType *vdwMult,
423     RealType *vpair, RealType *pot, RealType *A1,
424     RealType *A2, RealType *f1, RealType *t1, RealType *t2){
425    
426     return OpenMD::GB::Instance()->do_gb_pair(atid1, atid2, d, r, r2, sw,
427     vdwMult, vpair, pot, A1, A2, f1,
428     t1, t2);
429     }
430     }

Properties

Name Value
svn:eol-style native