ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/nonbonded/Electrostatic.cpp
Revision: 1502
Committed: Sat Oct 2 19:53:32 2010 UTC (14 years, 7 months ago) by gezelter
File size: 32139 byte(s)
Log Message:
Many changes, and we're not quite done with this phase yet.

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

Properties

Name Value
svn:eol-style native