ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/nonbonded/GB.cpp
Revision: 1668
Committed: Fri Jan 6 19:03:05 2012 UTC (13 years, 4 months ago) by gezelter
File size: 14139 byte(s)
Log Message:
Some fixes for CMake and single precision builds

File Contents

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

Properties

Name Value
svn:eol-style native