ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/nonbonded/Electrostatic.cpp
Revision: 1535
Committed: Fri Dec 31 18:31:56 2010 UTC (14 years, 4 months ago) by gezelter
File size: 36372 byte(s)
Log Message:
Well, it compiles and builds, but still has a bus error at runtime.

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] Vardeman & Gezelter, in progress (2009).
40 */
41
42 #include <stdio.h>
43 #include <string.h>
44
45 #include <cmath>
46 #include "nonbonded/Electrostatic.hpp"
47 #include "utils/simError.h"
48 #include "types/NonBondedInteractionType.hpp"
49 #include "types/DirectionalAtomType.hpp"
50 #include "io/Globals.hpp"
51
52 namespace OpenMD {
53
54 Electrostatic::Electrostatic(): name_("Electrostatic"), initialized_(false),
55 forceField_(NULL) {}
56
57 void Electrostatic::initialize() {
58
59 Globals* simParams_;
60
61 summationMap_["HARD"] = esm_HARD;
62 summationMap_["SWITCHING_FUNCTION"] = esm_SWITCHING_FUNCTION;
63 summationMap_["SHIFTED_POTENTIAL"] = esm_SHIFTED_POTENTIAL;
64 summationMap_["SHIFTED_FORCE"] = esm_SHIFTED_FORCE;
65 summationMap_["REACTION_FIELD"] = esm_REACTION_FIELD;
66 summationMap_["EWALD_FULL"] = esm_EWALD_FULL;
67 summationMap_["EWALD_PME"] = esm_EWALD_PME;
68 summationMap_["EWALD_SPME"] = esm_EWALD_SPME;
69 screeningMap_["DAMPED"] = DAMPED;
70 screeningMap_["UNDAMPED"] = UNDAMPED;
71
72 // these prefactors convert the multipole interactions into kcal / mol
73 // all were computed assuming distances are measured in angstroms
74 // Charge-Charge, assuming charges are measured in electrons
75 pre11_ = 332.0637778;
76 // Charge-Dipole, assuming charges are measured in electrons, and
77 // dipoles are measured in debyes
78 pre12_ = 69.13373;
79 // Dipole-Dipole, assuming dipoles are measured in debyes
80 pre22_ = 14.39325;
81 // Charge-Quadrupole, assuming charges are measured in electrons, and
82 // quadrupoles are measured in 10^-26 esu cm^2
83 // This unit is also known affectionately as an esu centi-barn.
84 pre14_ = 69.13373;
85
86 // conversions for the simulation box dipole moment
87 chargeToC_ = 1.60217733e-19;
88 angstromToM_ = 1.0e-10;
89 debyeToCm_ = 3.33564095198e-30;
90
91 // number of points for electrostatic splines
92 np_ = 100;
93
94 // variables to handle different summation methods for long-range
95 // electrostatics:
96 summationMethod_ = esm_HARD;
97 screeningMethod_ = UNDAMPED;
98 dielectric_ = 1.0;
99 one_third_ = 1.0 / 3.0;
100 haveCutoffRadius_ = false;
101 haveDampingAlpha_ = false;
102 haveDielectric_ = false;
103 haveElectroSpline_ = false;
104
105 // check the summation method:
106 if (simParams_->haveElectrostaticSummationMethod()) {
107 string myMethod = simParams_->getElectrostaticSummationMethod();
108 toUpper(myMethod);
109 map<string, ElectrostaticSummationMethod>::iterator i;
110 i = summationMap_.find(myMethod);
111 if ( i != summationMap_.end() ) {
112 summationMethod_ = (*i).second;
113 } else {
114 // throw error
115 sprintf( painCave.errMsg,
116 "SimInfo error: Unknown electrostaticSummationMethod.\n"
117 "\t(Input file specified %s .)\n"
118 "\telectrostaticSummationMethod must be one of: \"none\",\n"
119 "\t\"shifted_potential\", \"shifted_force\", or \n"
120 "\t\"reaction_field\".\n", myMethod.c_str() );
121 painCave.isFatal = 1;
122 simError();
123 }
124 } else {
125 // set ElectrostaticSummationMethod to the cutoffMethod:
126 if (simParams_->haveCutoffMethod()){
127 string myMethod = simParams_->getCutoffMethod();
128 toUpper(myMethod);
129 map<string, ElectrostaticSummationMethod>::iterator i;
130 i = summationMap_.find(myMethod);
131 if ( i != summationMap_.end() ) {
132 summationMethod_ = (*i).second;
133 }
134 }
135 }
136
137 if (summationMethod_ == esm_REACTION_FIELD) {
138 if (!simParams_->haveDielectric()) {
139 // throw warning
140 sprintf( painCave.errMsg,
141 "SimInfo warning: dielectric was not specified in the input file\n\tfor the reaction field correction method.\n"
142 "\tA default value of %f will be used for the dielectric.\n", dielectric_);
143 painCave.isFatal = 0;
144 painCave.severity = OPENMD_INFO;
145 simError();
146 } else {
147 dielectric_ = simParams_->getDielectric();
148 }
149 haveDielectric_ = true;
150 }
151
152 if (simParams_->haveElectrostaticScreeningMethod()) {
153 string myScreen = simParams_->getElectrostaticScreeningMethod();
154 toUpper(myScreen);
155 map<string, ElectrostaticScreeningMethod>::iterator i;
156 i = screeningMap_.find(myScreen);
157 if ( i != screeningMap_.end()) {
158 screeningMethod_ = (*i).second;
159 } else {
160 sprintf( painCave.errMsg,
161 "SimInfo error: Unknown electrostaticScreeningMethod.\n"
162 "\t(Input file specified %s .)\n"
163 "\telectrostaticScreeningMethod must be one of: \"undamped\"\n"
164 "or \"damped\".\n", myScreen.c_str() );
165 painCave.isFatal = 1;
166 simError();
167 }
168 }
169
170 // check to make sure a cutoff value has been set:
171 if (!haveCutoffRadius_) {
172 sprintf( painCave.errMsg, "Electrostatic::initialize has no Default "
173 "Cutoff value!\n");
174 painCave.severity = OPENMD_ERROR;
175 painCave.isFatal = 1;
176 simError();
177 }
178
179 if (screeningMethod_ == DAMPED) {
180 if (!simParams_->haveDampingAlpha()) {
181 // first set a cutoff dependent alpha value
182 // we assume alpha depends linearly with rcut from 0 to 20.5 ang
183 dampingAlpha_ = 0.425 - cutoffRadius_* 0.02;
184 if (dampingAlpha_ < 0.0) dampingAlpha_ = 0.0;
185
186 // throw warning
187 sprintf( painCave.errMsg,
188 "Electrostatic::initialize: dampingAlpha was not specified in the input file.\n"
189 "\tA default value of %f (1/ang) will be used for the cutoff of\n\t%f (ang).\n",
190 dampingAlpha_, cutoffRadius_);
191 painCave.severity = OPENMD_INFO;
192 painCave.isFatal = 0;
193 simError();
194 } else {
195 dampingAlpha_ = simParams_->getDampingAlpha();
196 }
197 haveDampingAlpha_ = true;
198 }
199
200 // find all of the Electrostatic atom Types:
201 ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes();
202 ForceField::AtomTypeContainer::MapTypeIterator i;
203 AtomType* at;
204
205 for (at = atomTypes->beginType(i); at != NULL;
206 at = atomTypes->nextType(i)) {
207
208 if (at->isElectrostatic())
209 addType(at);
210 }
211
212
213 cutoffRadius2_ = cutoffRadius_ * cutoffRadius_;
214 rcuti_ = 1.0 / cutoffRadius_;
215 rcuti2_ = rcuti_ * rcuti_;
216 rcuti3_ = rcuti2_ * rcuti_;
217 rcuti4_ = rcuti2_ * rcuti2_;
218
219 if (screeningMethod_ == DAMPED) {
220
221 alpha2_ = dampingAlpha_ * dampingAlpha_;
222 alpha4_ = alpha2_ * alpha2_;
223 alpha6_ = alpha4_ * alpha2_;
224 alpha8_ = alpha4_ * alpha4_;
225
226 constEXP_ = exp(-alpha2_ * cutoffRadius2_);
227 invRootPi_ = 0.56418958354775628695;
228 alphaPi_ = 2.0 * dampingAlpha_ * invRootPi_;
229
230 c1c_ = erfc(dampingAlpha_ * cutoffRadius_) * rcuti_;
231 c2c_ = alphaPi_ * constEXP_ * rcuti_ + c1c_ * rcuti_;
232 c3c_ = 2.0 * alphaPi_ * alpha2_ + 3.0 * c2c_ * rcuti_;
233 c4c_ = 4.0 * alphaPi_ * alpha4_ + 5.0 * c3c_ * rcuti2_;
234 c5c_ = 8.0 * alphaPi_ * alpha6_ + 7.0 * c4c_ * rcuti2_;
235 c6c_ = 16.0 * alphaPi_ * alpha8_ + 9.0 * c5c_ * rcuti2_;
236 } else {
237 c1c_ = rcuti_;
238 c2c_ = c1c_ * rcuti_;
239 c3c_ = 3.0 * c2c_ * rcuti_;
240 c4c_ = 5.0 * c3c_ * rcuti2_;
241 c5c_ = 7.0 * c4c_ * rcuti2_;
242 c6c_ = 9.0 * c5c_ * rcuti2_;
243 }
244
245 if (summationMethod_ == esm_REACTION_FIELD) {
246 preRF_ = (dielectric_ - 1.0) /
247 ((2.0 * dielectric_ + 1.0) * cutoffRadius2_ * cutoffRadius_);
248 preRF2_ = 2.0 * preRF_;
249 }
250
251 RealType dx = cutoffRadius_ / RealType(np_ - 1);
252 RealType rval;
253 vector<RealType> rvals;
254 vector<RealType> yvals;
255 for (int i = 0; i < np_; i++) {
256 rval = RealType(i) * dx;
257 rvals.push_back(rval);
258 yvals.push_back(erfc(dampingAlpha_ * rval));
259 }
260 erfcSpline_ = new CubicSpline();
261 erfcSpline_->addPoints(rvals, yvals);
262 haveElectroSpline_ = true;
263
264 initialized_ = true;
265 }
266
267 void Electrostatic::addType(AtomType* atomType){
268
269 ElectrostaticAtomData electrostaticAtomData;
270 electrostaticAtomData.is_Charge = false;
271 electrostaticAtomData.is_Dipole = false;
272 electrostaticAtomData.is_SplitDipole = false;
273 electrostaticAtomData.is_Quadrupole = false;
274
275 if (atomType->isCharge()) {
276 GenericData* data = atomType->getPropertyByName("Charge");
277
278 if (data == NULL) {
279 sprintf( painCave.errMsg, "Electrostatic::addType could not find "
280 "Charge\n"
281 "\tparameters for atomType %s.\n",
282 atomType->getName().c_str());
283 painCave.severity = OPENMD_ERROR;
284 painCave.isFatal = 1;
285 simError();
286 }
287
288 DoubleGenericData* doubleData = dynamic_cast<DoubleGenericData*>(data);
289 if (doubleData == NULL) {
290 sprintf( painCave.errMsg,
291 "Electrostatic::addType could not convert GenericData to "
292 "Charge for\n"
293 "\tatom type %s\n", atomType->getName().c_str());
294 painCave.severity = OPENMD_ERROR;
295 painCave.isFatal = 1;
296 simError();
297 }
298 electrostaticAtomData.is_Charge = true;
299 electrostaticAtomData.charge = doubleData->getData();
300 }
301
302 if (atomType->isDirectional()) {
303 DirectionalAtomType* daType = dynamic_cast<DirectionalAtomType*>(atomType);
304
305 if (daType->isDipole()) {
306 GenericData* data = daType->getPropertyByName("Dipole");
307
308 if (data == NULL) {
309 sprintf( painCave.errMsg,
310 "Electrostatic::addType could not find Dipole\n"
311 "\tparameters for atomType %s.\n",
312 daType->getName().c_str());
313 painCave.severity = OPENMD_ERROR;
314 painCave.isFatal = 1;
315 simError();
316 }
317
318 DoubleGenericData* doubleData = dynamic_cast<DoubleGenericData*>(data);
319 if (doubleData == NULL) {
320 sprintf( painCave.errMsg,
321 "Electrostatic::addType could not convert GenericData to "
322 "Dipole Moment\n"
323 "\tfor atom type %s\n", daType->getName().c_str());
324 painCave.severity = OPENMD_ERROR;
325 painCave.isFatal = 1;
326 simError();
327 }
328 electrostaticAtomData.is_Dipole = true;
329 electrostaticAtomData.dipole_moment = doubleData->getData();
330 }
331
332 if (daType->isSplitDipole()) {
333 GenericData* data = daType->getPropertyByName("SplitDipoleDistance");
334
335 if (data == NULL) {
336 sprintf(painCave.errMsg,
337 "Electrostatic::addType could not find SplitDipoleDistance\n"
338 "\tparameter for atomType %s.\n",
339 daType->getName().c_str());
340 painCave.severity = OPENMD_ERROR;
341 painCave.isFatal = 1;
342 simError();
343 }
344
345 DoubleGenericData* doubleData = dynamic_cast<DoubleGenericData*>(data);
346 if (doubleData == NULL) {
347 sprintf( painCave.errMsg,
348 "Electrostatic::addType could not convert GenericData to "
349 "SplitDipoleDistance for\n"
350 "\tatom type %s\n", daType->getName().c_str());
351 painCave.severity = OPENMD_ERROR;
352 painCave.isFatal = 1;
353 simError();
354 }
355 electrostaticAtomData.is_SplitDipole = true;
356 electrostaticAtomData.split_dipole_distance = doubleData->getData();
357 }
358
359 if (daType->isQuadrupole()) {
360 GenericData* data = daType->getPropertyByName("QuadrupoleMoments");
361
362 if (data == NULL) {
363 sprintf( painCave.errMsg,
364 "Electrostatic::addType could not find QuadrupoleMoments\n"
365 "\tparameter for atomType %s.\n",
366 daType->getName().c_str());
367 painCave.severity = OPENMD_ERROR;
368 painCave.isFatal = 1;
369 simError();
370 }
371
372 // Quadrupoles in OpenMD are set as the diagonal elements
373 // of the diagonalized traceless quadrupole moment tensor.
374 // The column vectors of the unitary matrix that diagonalizes
375 // the quadrupole moment tensor become the eFrame (or the
376 // electrostatic version of the body-fixed frame.
377
378 Vector3dGenericData* v3dData = dynamic_cast<Vector3dGenericData*>(data);
379 if (v3dData == NULL) {
380 sprintf( painCave.errMsg,
381 "Electrostatic::addType could not convert GenericData to "
382 "Quadrupole Moments for\n"
383 "\tatom type %s\n", daType->getName().c_str());
384 painCave.severity = OPENMD_ERROR;
385 painCave.isFatal = 1;
386 simError();
387 }
388 electrostaticAtomData.is_Quadrupole = true;
389 electrostaticAtomData.quadrupole_moments = v3dData->getData();
390 }
391 }
392
393 AtomTypeProperties atp = atomType->getATP();
394
395 pair<map<int,AtomType*>::iterator,bool> ret;
396 ret = ElectrostaticList.insert( pair<int,AtomType*>(atp.ident, atomType) );
397 if (ret.second == false) {
398 sprintf( painCave.errMsg,
399 "Electrostatic already had a previous entry with ident %d\n",
400 atp.ident);
401 painCave.severity = OPENMD_INFO;
402 painCave.isFatal = 0;
403 simError();
404 }
405
406 ElectrostaticMap[atomType] = electrostaticAtomData;
407 return;
408 }
409
410 void Electrostatic::setElectrostaticCutoffRadius( RealType theECR,
411 RealType theRSW ) {
412 cutoffRadius_ = theECR;
413 rrf_ = cutoffRadius_;
414 rt_ = theRSW;
415 haveCutoffRadius_ = true;
416 }
417 void Electrostatic::setElectrostaticSummationMethod( ElectrostaticSummationMethod esm ) {
418 summationMethod_ = esm;
419 }
420 void Electrostatic::setElectrostaticScreeningMethod( ElectrostaticScreeningMethod sm ) {
421 screeningMethod_ = sm;
422 }
423 void Electrostatic::setDampingAlpha( RealType alpha ) {
424 dampingAlpha_ = alpha;
425 haveDampingAlpha_ = true;
426 }
427 void Electrostatic::setReactionFieldDielectric( RealType dielectric ){
428 dielectric_ = dielectric;
429 haveDielectric_ = true;
430 }
431
432 void Electrostatic::calcForce(InteractionData idat) {
433
434 // utility variables. Should clean these up and use the Vector3d and
435 // Mat3x3d to replace as many as we can in future versions:
436
437 RealType q_i, q_j, mu_i, mu_j, d_i, d_j;
438 RealType qxx_i, qyy_i, qzz_i;
439 RealType qxx_j, qyy_j, qzz_j;
440 RealType cx_i, cy_i, cz_i;
441 RealType cx_j, cy_j, cz_j;
442 RealType cx2, cy2, cz2;
443 RealType ct_i, ct_j, ct_ij, a1;
444 RealType riji, ri, ri2, ri3, ri4;
445 RealType pref, vterm, epot, dudr;
446 RealType scale, sc2;
447 RealType pot_term, preVal, rfVal;
448 RealType c2ri, c3ri, c4rij, cti3, ctj3, ctidotj;
449 RealType preSw, preSwSc;
450 RealType c1, c2, c3, c4;
451 RealType erfcVal, derfcVal;
452 RealType BigR;
453
454 Vector3d Q_i, Q_j;
455 Vector3d ux_i, uy_i, uz_i;
456 Vector3d ux_j, uy_j, uz_j;
457 Vector3d dudux_i, duduy_i, duduz_i;
458 Vector3d dudux_j, duduy_j, duduz_j;
459 Vector3d rhatdot2, rhatc4;
460 Vector3d dVdr;
461
462 pair<RealType, RealType> res;
463
464 if (!initialized_) initialize();
465
466 ElectrostaticAtomData data1 = ElectrostaticMap[idat.atype1];
467 ElectrostaticAtomData data2 = ElectrostaticMap[idat.atype2];
468
469 // some variables we'll need independent of electrostatic type:
470
471 riji = 1.0 / idat.rij;
472 Vector3d rhat = idat.d * riji;
473
474 // logicals
475
476 bool i_is_Charge = data1.is_Charge;
477 bool i_is_Dipole = data1.is_Dipole;
478 bool i_is_SplitDipole = data1.is_SplitDipole;
479 bool i_is_Quadrupole = data1.is_Quadrupole;
480
481 bool j_is_Charge = data2.is_Charge;
482 bool j_is_Dipole = data2.is_Dipole;
483 bool j_is_SplitDipole = data2.is_SplitDipole;
484 bool j_is_Quadrupole = data2.is_Quadrupole;
485
486 if (i_is_Charge)
487 q_i = data1.charge;
488
489 if (i_is_Dipole) {
490 mu_i = data1.dipole_moment;
491 uz_i = idat.eFrame1.getColumn(2);
492
493 ct_i = dot(uz_i, rhat);
494
495 if (i_is_SplitDipole)
496 d_i = data1.split_dipole_distance;
497
498 duduz_i = V3Zero;
499 }
500
501 if (i_is_Quadrupole) {
502 Q_i = data1.quadrupole_moments;
503 qxx_i = Q_i.x();
504 qyy_i = Q_i.y();
505 qzz_i = Q_i.z();
506
507 ux_i = idat.eFrame1.getColumn(0);
508 uy_i = idat.eFrame1.getColumn(1);
509 uz_i = idat.eFrame1.getColumn(2);
510
511 cx_i = dot(ux_i, rhat);
512 cy_i = dot(uy_i, rhat);
513 cz_i = dot(uz_i, rhat);
514
515 dudux_i = V3Zero;
516 duduy_i = V3Zero;
517 duduz_i = V3Zero;
518 }
519
520 if (j_is_Charge)
521 q_j = data2.charge;
522
523 if (j_is_Dipole) {
524 mu_j = data2.dipole_moment;
525 uz_j = idat.eFrame2.getColumn(2);
526
527 ct_j = dot(uz_j, rhat);
528
529 if (j_is_SplitDipole)
530 d_j = data2.split_dipole_distance;
531
532 duduz_j = V3Zero;
533 }
534
535 if (j_is_Quadrupole) {
536 Q_j = data2.quadrupole_moments;
537 qxx_j = Q_j.x();
538 qyy_j = Q_j.y();
539 qzz_j = Q_j.z();
540
541 ux_j = idat.eFrame2.getColumn(0);
542 uy_j = idat.eFrame2.getColumn(1);
543 uz_j = idat.eFrame2.getColumn(2);
544
545 cx_j = dot(ux_j, rhat);
546 cy_j = dot(uy_j, rhat);
547 cz_j = dot(uz_j, rhat);
548
549 dudux_j = V3Zero;
550 duduy_j = V3Zero;
551 duduz_j = V3Zero;
552 }
553
554 epot = 0.0;
555 dVdr = V3Zero;
556
557 if (i_is_Charge) {
558
559 if (j_is_Charge) {
560 if (screeningMethod_ == DAMPED) {
561 // assemble the damping variables
562 res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
563 erfcVal = res.first;
564 derfcVal = res.second;
565 c1 = erfcVal * riji;
566 c2 = (-derfcVal + c1) * riji;
567 } else {
568 c1 = riji;
569 c2 = c1 * riji;
570 }
571
572 preVal = idat.electroMult * pre11_ * q_i * q_j;
573
574 if (summationMethod_ == esm_SHIFTED_POTENTIAL) {
575 vterm = preVal * (c1 - c1c_);
576 dudr = -idat.sw * preVal * c2;
577
578 } else if (summationMethod_ == esm_SHIFTED_FORCE) {
579 vterm = preVal * ( c1 - c1c_ + c2c_*(idat.rij - cutoffRadius_) );
580 dudr = idat.sw * preVal * (c2c_ - c2);
581
582 } else if (summationMethod_ == esm_REACTION_FIELD) {
583 rfVal = idat.electroMult * preRF_ * idat.rij * idat.rij;
584 vterm = preVal * ( riji + rfVal );
585 dudr = idat.sw * preVal * ( 2.0 * rfVal - riji ) * riji;
586
587 } else {
588 vterm = preVal * riji * erfcVal;
589
590 dudr = - idat.sw * preVal * c2;
591
592 }
593
594 idat.vpair += vterm;
595 epot += idat.sw * vterm;
596
597 dVdr += dudr * rhat;
598 }
599
600 if (j_is_Dipole) {
601 // pref is used by all the possible methods
602 pref = idat.electroMult * pre12_ * q_i * mu_j;
603 preSw = idat.sw * pref;
604
605 if (summationMethod_ == esm_REACTION_FIELD) {
606 ri2 = riji * riji;
607 ri3 = ri2 * riji;
608
609 vterm = - pref * ct_j * ( ri2 - preRF2_ * idat.rij );
610 idat.vpair += vterm;
611 epot += idat.sw * vterm;
612
613 dVdr += -preSw * (ri3 * (uz_j - 3.0 * ct_j * rhat) - preRF2_*uz_j);
614 duduz_j += -preSw * rhat * (ri2 - preRF2_ * idat.rij);
615
616 } else {
617 // determine the inverse r used if we have split dipoles
618 if (j_is_SplitDipole) {
619 BigR = sqrt(idat.r2 + 0.25 * d_j * d_j);
620 ri = 1.0 / BigR;
621 scale = idat.rij * ri;
622 } else {
623 ri = riji;
624 scale = 1.0;
625 }
626
627 sc2 = scale * scale;
628
629 if (screeningMethod_ == DAMPED) {
630 // assemble the damping variables
631 res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
632 erfcVal = res.first;
633 derfcVal = res.second;
634 c1 = erfcVal * ri;
635 c2 = (-derfcVal + c1) * ri;
636 c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * ri;
637 } else {
638 c1 = ri;
639 c2 = c1 * ri;
640 c3 = 3.0 * c2 * ri;
641 }
642
643 c2ri = c2 * ri;
644
645 // calculate the potential
646 pot_term = scale * c2;
647 vterm = -pref * ct_j * pot_term;
648 idat.vpair += vterm;
649 epot += idat.sw * vterm;
650
651 // calculate derivatives for forces and torques
652
653 dVdr += -preSw * (uz_j * c2ri - ct_j * rhat * sc2 * c3);
654 duduz_j += -preSw * pot_term * rhat;
655
656 }
657 }
658
659 if (j_is_Quadrupole) {
660 // first precalculate some necessary variables
661 cx2 = cx_j * cx_j;
662 cy2 = cy_j * cy_j;
663 cz2 = cz_j * cz_j;
664 pref = idat.electroMult * pre14_ * q_i * one_third_;
665
666 if (screeningMethod_ == DAMPED) {
667 // assemble the damping variables
668 res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
669 erfcVal = res.first;
670 derfcVal = res.second;
671 c1 = erfcVal * riji;
672 c2 = (-derfcVal + c1) * riji;
673 c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * riji;
674 c4 = -4.0 * derfcVal * alpha4_ + 5.0 * c3 * riji * riji;
675 } else {
676 c1 = riji;
677 c2 = c1 * riji;
678 c3 = 3.0 * c2 * riji;
679 c4 = 5.0 * c3 * riji * riji;
680 }
681
682 // precompute variables for convenience
683 preSw = idat.sw * pref;
684 c2ri = c2 * riji;
685 c3ri = c3 * riji;
686 c4rij = c4 * idat.rij;
687 rhatdot2 = 2.0 * rhat * c3;
688 rhatc4 = rhat * c4rij;
689
690 // calculate the potential
691 pot_term = ( qxx_j * (cx2*c3 - c2ri) +
692 qyy_j * (cy2*c3 - c2ri) +
693 qzz_j * (cz2*c3 - c2ri) );
694 vterm = pref * pot_term;
695 idat.vpair += vterm;
696 epot += idat.sw * vterm;
697
698 // calculate derivatives for the forces and torques
699
700 dVdr += -preSw * ( qxx_j* (cx2*rhatc4 - (2.0*cx_j*ux_j + rhat)*c3ri) +
701 qyy_j* (cy2*rhatc4 - (2.0*cy_j*uy_j + rhat)*c3ri) +
702 qzz_j* (cz2*rhatc4 - (2.0*cz_j*uz_j + rhat)*c3ri));
703
704 dudux_j += preSw * qxx_j * cx_j * rhatdot2;
705 duduy_j += preSw * qyy_j * cy_j * rhatdot2;
706 duduz_j += preSw * qzz_j * cz_j * rhatdot2;
707 }
708 }
709
710 if (i_is_Dipole) {
711
712 if (j_is_Charge) {
713 // variables used by all the methods
714 pref = idat.electroMult * pre12_ * q_j * mu_i;
715 preSw = idat.sw * pref;
716
717 if (summationMethod_ == esm_REACTION_FIELD) {
718
719 ri2 = riji * riji;
720 ri3 = ri2 * riji;
721
722 vterm = pref * ct_i * ( ri2 - preRF2_ * idat.rij );
723 idat.vpair += vterm;
724 epot += idat.sw * vterm;
725
726 dVdr += preSw * (ri3 * (uz_i - 3.0 * ct_i * rhat) - preRF2_ * uz_i);
727
728 duduz_i += preSw * rhat * (ri2 - preRF2_ * idat.rij);
729
730 } else {
731
732 // determine inverse r if we are using split dipoles
733 if (i_is_SplitDipole) {
734 BigR = sqrt(idat.r2 + 0.25 * d_i * d_i);
735 ri = 1.0 / BigR;
736 scale = idat.rij * ri;
737 } else {
738 ri = riji;
739 scale = 1.0;
740 }
741
742 sc2 = scale * scale;
743
744 if (screeningMethod_ == DAMPED) {
745 // assemble the damping variables
746 res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
747 erfcVal = res.first;
748 derfcVal = res.second;
749 c1 = erfcVal * ri;
750 c2 = (-derfcVal + c1) * ri;
751 c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * ri;
752 } else {
753 c1 = ri;
754 c2 = c1 * ri;
755 c3 = 3.0 * c2 * ri;
756 }
757
758 c2ri = c2 * ri;
759
760 // calculate the potential
761 pot_term = c2 * scale;
762 vterm = pref * ct_i * pot_term;
763 idat.vpair += vterm;
764 epot += idat.sw * vterm;
765
766 // calculate derivatives for the forces and torques
767 dVdr += preSw * (uz_i * c2ri - ct_i * rhat * sc2 * c3);
768 duduz_i += preSw * pot_term * rhat;
769 }
770 }
771
772 if (j_is_Dipole) {
773 // variables used by all methods
774 ct_ij = dot(uz_i, uz_j);
775
776 pref = idat.electroMult * pre22_ * mu_i * mu_j;
777 preSw = idat.sw * pref;
778
779 if (summationMethod_ == esm_REACTION_FIELD) {
780 ri2 = riji * riji;
781 ri3 = ri2 * riji;
782 ri4 = ri2 * ri2;
783
784 vterm = pref * ( ri3 * (ct_ij - 3.0 * ct_i * ct_j) -
785 preRF2_ * ct_ij );
786 idat.vpair += vterm;
787 epot += idat.sw * vterm;
788
789 a1 = 5.0 * ct_i * ct_j - ct_ij;
790
791 dVdr += preSw * 3.0 * ri4 * (a1 * rhat - ct_i * uz_j - ct_j * uz_i);
792
793 duduz_i += preSw * (ri3 * (uz_j - 3.0 * ct_j * rhat) - preRF2_*uz_j);
794 duduz_j += preSw * (ri3 * (uz_i - 3.0 * ct_i * rhat) - preRF2_*uz_i);
795
796 } else {
797
798 if (i_is_SplitDipole) {
799 if (j_is_SplitDipole) {
800 BigR = sqrt(idat.r2 + 0.25 * d_i * d_i + 0.25 * d_j * d_j);
801 } else {
802 BigR = sqrt(idat.r2 + 0.25 * d_i * d_i);
803 }
804 ri = 1.0 / BigR;
805 scale = idat.rij * ri;
806 } else {
807 if (j_is_SplitDipole) {
808 BigR = sqrt(idat.r2 + 0.25 * d_j * d_j);
809 ri = 1.0 / BigR;
810 scale = idat.rij * ri;
811 } else {
812 ri = riji;
813 scale = 1.0;
814 }
815 }
816 if (screeningMethod_ == DAMPED) {
817 // assemble damping variables
818 res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
819 erfcVal = res.first;
820 derfcVal = res.second;
821 c1 = erfcVal * ri;
822 c2 = (-derfcVal + c1) * ri;
823 c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * ri;
824 c4 = -4.0 * derfcVal * alpha4_ + 5.0 * c3 * ri * ri;
825 } else {
826 c1 = ri;
827 c2 = c1 * ri;
828 c3 = 3.0 * c2 * ri;
829 c4 = 5.0 * c3 * ri * ri;
830 }
831
832 // precompute variables for convenience
833 sc2 = scale * scale;
834 cti3 = ct_i * sc2 * c3;
835 ctj3 = ct_j * sc2 * c3;
836 ctidotj = ct_i * ct_j * sc2;
837 preSwSc = preSw * scale;
838 c2ri = c2 * ri;
839 c3ri = c3 * ri;
840 c4rij = c4 * idat.rij;
841
842 // calculate the potential
843 pot_term = (ct_ij * c2ri - ctidotj * c3);
844 vterm = pref * pot_term;
845 idat.vpair += vterm;
846 epot += idat.sw * vterm;
847
848 // calculate derivatives for the forces and torques
849 dVdr += preSwSc * ( ctidotj * rhat * c4rij -
850 (ct_i*uz_j + ct_j*uz_i + ct_ij*rhat) * c3ri);
851
852 duduz_i += preSw * (uz_j * c2ri - ctj3 * rhat);
853 duduz_j += preSw * (uz_i * c2ri - cti3 * rhat);
854 }
855 }
856 }
857
858 if (i_is_Quadrupole) {
859 if (j_is_Charge) {
860 // precompute some necessary variables
861 cx2 = cx_i * cx_i;
862 cy2 = cy_i * cy_i;
863 cz2 = cz_i * cz_i;
864
865 pref = idat.electroMult * pre14_ * q_j * one_third_;
866
867 if (screeningMethod_ == DAMPED) {
868 // assemble the damping variables
869 res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
870 erfcVal = res.first;
871 derfcVal = res.second;
872 c1 = erfcVal * riji;
873 c2 = (-derfcVal + c1) * riji;
874 c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * riji;
875 c4 = -4.0 * derfcVal * alpha4_ + 5.0 * c3 * riji * riji;
876 } else {
877 c1 = riji;
878 c2 = c1 * riji;
879 c3 = 3.0 * c2 * riji;
880 c4 = 5.0 * c3 * riji * riji;
881 }
882
883 // precompute some variables for convenience
884 preSw = idat.sw * pref;
885 c2ri = c2 * riji;
886 c3ri = c3 * riji;
887 c4rij = c4 * idat.rij;
888 rhatdot2 = 2.0 * rhat * c3;
889 rhatc4 = rhat * c4rij;
890
891 // calculate the potential
892 pot_term = ( qxx_i * (cx2 * c3 - c2ri) +
893 qyy_i * (cy2 * c3 - c2ri) +
894 qzz_i * (cz2 * c3 - c2ri) );
895
896 vterm = pref * pot_term;
897 idat.vpair += vterm;
898 epot += idat.sw * vterm;
899
900 // calculate the derivatives for the forces and torques
901
902 dVdr += -preSw * (qxx_i* (cx2*rhatc4 - (2.0*cx_i*ux_i + rhat)*c3ri) +
903 qyy_i* (cy2*rhatc4 - (2.0*cy_i*uy_i + rhat)*c3ri) +
904 qzz_i* (cz2*rhatc4 - (2.0*cz_i*uz_i + rhat)*c3ri));
905
906 dudux_i += preSw * qxx_i * cx_i * rhatdot2;
907 duduy_i += preSw * qyy_i * cy_i * rhatdot2;
908 duduz_i += preSw * qzz_i * cz_i * rhatdot2;
909 }
910 }
911
912 idat.pot += epot;
913 idat.f1 += dVdr;
914
915 if (i_is_Dipole || i_is_Quadrupole)
916 idat.t1 -= cross(uz_i, duduz_i);
917 if (i_is_Quadrupole) {
918 idat.t1 -= cross(ux_i, dudux_i);
919 idat.t1 -= cross(uy_i, duduy_i);
920 }
921
922 if (j_is_Dipole || j_is_Quadrupole)
923 idat.t2 -= cross(uz_j, duduz_j);
924 if (j_is_Quadrupole) {
925 idat.t2 -= cross(uz_j, dudux_j);
926 idat.t2 -= cross(uz_j, duduy_j);
927 }
928
929 return;
930 }
931
932 void Electrostatic::calcSkipCorrection(SkipCorrectionData skdat) {
933
934 if (!initialized_) initialize();
935
936 ElectrostaticAtomData data1 = ElectrostaticMap[skdat.atype1];
937 ElectrostaticAtomData data2 = ElectrostaticMap[skdat.atype2];
938
939 // logicals
940
941 bool i_is_Charge = data1.is_Charge;
942 bool i_is_Dipole = data1.is_Dipole;
943
944 bool j_is_Charge = data2.is_Charge;
945 bool j_is_Dipole = data2.is_Dipole;
946
947 RealType q_i, q_j;
948
949 // The skippedCharge computation is needed by the real-space cutoff methods
950 // (i.e. shifted force and shifted potential)
951
952 if (i_is_Charge) {
953 q_i = data1.charge;
954 skdat.skippedCharge2 += q_i;
955 }
956
957 if (j_is_Charge) {
958 q_j = data2.charge;
959 skdat.skippedCharge1 += q_j;
960 }
961
962 // the rest of this function should only be necessary for reaction field.
963
964 if (summationMethod_ == esm_REACTION_FIELD) {
965 RealType riji, ri2, ri3;
966 RealType q_i, mu_i, ct_i;
967 RealType q_j, mu_j, ct_j;
968 RealType preVal, rfVal, vterm, dudr, pref, myPot;
969 Vector3d dVdr, uz_i, uz_j, duduz_i, duduz_j, rhat;
970
971 // some variables we'll need independent of electrostatic type:
972
973 riji = 1.0 / skdat.rij;
974 rhat = skdat.d * riji;
975
976 if (i_is_Dipole) {
977 mu_i = data1.dipole_moment;
978 uz_i = skdat.eFrame1.getColumn(2);
979 ct_i = dot(uz_i, rhat);
980 duduz_i = V3Zero;
981 }
982
983 if (j_is_Dipole) {
984 mu_j = data2.dipole_moment;
985 uz_j = skdat.eFrame2.getColumn(2);
986 ct_j = dot(uz_j, rhat);
987 duduz_j = V3Zero;
988 }
989
990 if (i_is_Charge) {
991 if (j_is_Charge) {
992 preVal = skdat.electroMult * pre11_ * q_i * q_j;
993 rfVal = preRF_ * skdat.rij * skdat.rij;
994 vterm = preVal * rfVal;
995 myPot += skdat.sw * vterm;
996 dudr = skdat.sw * preVal * 2.0 * rfVal * riji;
997 dVdr += dudr * rhat;
998 }
999
1000 if (j_is_Dipole) {
1001 ri2 = riji * riji;
1002 ri3 = ri2 * riji;
1003 pref = skdat.electroMult * pre12_ * q_i * mu_j;
1004 vterm = - pref * ct_j * ( ri2 - preRF2_ * skdat.rij );
1005 myPot += skdat.sw * vterm;
1006 dVdr += -skdat.sw * pref * ( ri3 * ( uz_j - 3.0 * ct_j * rhat) - preRF2_ * uz_j);
1007 duduz_j += -skdat.sw * pref * rhat * (ri2 - preRF2_ * skdat.rij);
1008 }
1009 }
1010 if (i_is_Dipole) {
1011 if (j_is_Charge) {
1012 ri2 = riji * riji;
1013 ri3 = ri2 * riji;
1014 pref = skdat.electroMult * pre12_ * q_j * mu_i;
1015 vterm = - pref * ct_i * ( ri2 - preRF2_ * skdat.rij );
1016 myPot += skdat.sw * vterm;
1017 dVdr += skdat.sw * pref * ( ri3 * ( uz_i - 3.0 * ct_i * rhat) - preRF2_ * uz_i);
1018 duduz_i += skdat.sw * pref * rhat * (ri2 - preRF2_ * skdat.rij);
1019 }
1020 }
1021
1022 // accumulate the forces and torques resulting from the self term
1023 skdat.pot += myPot;
1024 skdat.f1 += dVdr;
1025
1026 if (i_is_Dipole)
1027 skdat.t1 -= cross(uz_i, duduz_i);
1028 if (j_is_Dipole)
1029 skdat.t2 -= cross(uz_j, duduz_j);
1030 }
1031 }
1032
1033 void Electrostatic::calcSelfCorrection(SelfCorrectionData scdat) {
1034 RealType mu1, preVal, chg1, self;
1035
1036 if (!initialized_) initialize();
1037
1038 ElectrostaticAtomData data = ElectrostaticMap[scdat.atype];
1039
1040 // logicals
1041
1042 bool i_is_Charge = data.is_Charge;
1043 bool i_is_Dipole = data.is_Dipole;
1044
1045 if (summationMethod_ == esm_REACTION_FIELD) {
1046 if (i_is_Dipole) {
1047 mu1 = data.dipole_moment;
1048 preVal = pre22_ * preRF2_ * mu1 * mu1;
1049 scdat.pot -= 0.5 * preVal;
1050
1051 // The self-correction term adds into the reaction field vector
1052 Vector3d uz_i = scdat.eFrame.getColumn(2);
1053 Vector3d ei = preVal * uz_i;
1054
1055 // This looks very wrong. A vector crossed with itself is zero.
1056 scdat.t -= cross(uz_i, ei);
1057 }
1058 } else if (summationMethod_ == esm_SHIFTED_FORCE || summationMethod_ == esm_SHIFTED_POTENTIAL) {
1059 if (i_is_Charge) {
1060 chg1 = data.charge;
1061 if (screeningMethod_ == DAMPED) {
1062 self = - 0.5 * (c1c_ + alphaPi_) * chg1 * (chg1 + scdat.skippedCharge) * pre11_;
1063 } else {
1064 self = - 0.5 * rcuti_ * chg1 * (chg1 + scdat.skippedCharge) * pre11_;
1065 }
1066 scdat.pot += self;
1067 }
1068 }
1069 }
1070
1071 RealType Electrostatic::getSuggestedCutoffRadius(AtomType* at1, AtomType* at2) {
1072 // This seems to work moderately well as a default. There's no
1073 // inherent scale for 1/r interactions that we can standardize.
1074 // 12 angstroms seems to be a reasonably good guess for most
1075 // cases.
1076 return 12.0;
1077 }
1078 }

Properties

Name Value
svn:eol-style native