ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/nonbonded/Electrostatic.cpp
Revision: 1601
Committed: Thu Aug 4 20:04:35 2011 UTC (13 years, 9 months ago) by gezelter
File size: 36236 byte(s)
Log Message:
removed spurious prints, fixed one bug, but there's still a parallel problem

File Contents

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

Properties

Name Value
svn:eol-style native