ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/nonbonded/Electrostatic.cpp
(Generate patch)

Comparing branches/development/src/nonbonded/Electrostatic.cpp (file contents):
Revision 1586 by gezelter, Tue Jun 21 06:34:35 2011 UTC vs.
Revision 1787 by gezelter, Wed Aug 29 18:13:11 2012 UTC

# Line 34 | Line 34
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).          
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).                        
39 > * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010).
40 > * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41   */
42  
43   #include <stdio.h>
# Line 46 | Line 47
47   #include "nonbonded/Electrostatic.hpp"
48   #include "utils/simError.h"
49   #include "types/NonBondedInteractionType.hpp"
50 < #include "types/DirectionalAtomType.hpp"
50 > #include "types/FixedChargeAdapter.hpp"
51 > #include "types/FluctuatingChargeAdapter.hpp"
52 > #include "types/MultipoleAdapter.hpp"
53   #include "io/Globals.hpp"
54 + #include "nonbonded/SlaterIntegrals.hpp"
55 + #include "utils/PhysicalConstants.hpp"
56 + #include "math/erfc.hpp"
57 + #include "math/SquareMatrix.hpp"
58  
59   namespace OpenMD {
60    
61    Electrostatic::Electrostatic(): name_("Electrostatic"), initialized_(false),
62 <                                  forceField_(NULL), info_(NULL), haveCutoffRadius_(false),
63 <                                  haveDampingAlpha_(false), haveDielectric_(false),
64 <                                  haveElectroSpline_(false)
65 < {}
62 >                                  forceField_(NULL), info_(NULL),
63 >                                  haveCutoffRadius_(false),
64 >                                  haveDampingAlpha_(false),
65 >                                  haveDielectric_(false),
66 >                                  haveElectroSplines_(false)
67 >  {}
68    
69    void Electrostatic::initialize() {
70 <
70 >    
71      Globals* simParams_ = info_->getSimParams();
72  
73      summationMap_["HARD"]               = esm_HARD;
74 +    summationMap_["NONE"]               = esm_HARD;
75      summationMap_["SWITCHING_FUNCTION"] = esm_SWITCHING_FUNCTION;
76      summationMap_["SHIFTED_POTENTIAL"]  = esm_SHIFTED_POTENTIAL;
77      summationMap_["SHIFTED_FORCE"]      = esm_SHIFTED_FORCE;    
# Line 79 | Line 89 | namespace OpenMD {
89      // Charge-Dipole, assuming charges are measured in electrons, and
90      // dipoles are measured in debyes
91      pre12_ = 69.13373;
92 <    // Dipole-Dipole, assuming dipoles are measured in debyes
92 >    // Dipole-Dipole, assuming dipoles are measured in Debye
93      pre22_ = 14.39325;
94      // Charge-Quadrupole, assuming charges are measured in electrons, and
95      // quadrupoles are measured in 10^-26 esu cm^2
96 <    // This unit is also known affectionately as an esu centi-barn.
96 >    // This unit is also known affectionately as an esu centibarn.
97      pre14_ = 69.13373;
98 <    
98 >    // Dipole-Quadrupole, assuming dipoles are measured in debyes and
99 >    // quadrupoles in esu centibarns:
100 >    pre24_ = 14.39325;
101 >    // Quadrupole-Quadrupole, assuming esu centibarns:
102 >    pre44_ = 14.39325;
103 >
104      // conversions for the simulation box dipole moment
105      chargeToC_ = 1.60217733e-19;
106      angstromToM_ = 1.0e-10;
# Line 99 | Line 114 | namespace OpenMD {
114      summationMethod_ = esm_HARD;    
115      screeningMethod_ = UNDAMPED;
116      dielectric_ = 1.0;
102    one_third_ = 1.0 / 3.0;
117    
118      // check the summation method:
119      if (simParams_->haveElectrostaticSummationMethod()) {
# Line 114 | Line 128 | namespace OpenMD {
128          sprintf( painCave.errMsg,
129                   "Electrostatic::initialize: Unknown electrostaticSummationMethod.\n"
130                   "\t(Input file specified %s .)\n"
131 <                 "\telectrostaticSummationMethod must be one of: \"none\",\n"
131 >                 "\telectrostaticSummationMethod must be one of: \"hard\",\n"
132                   "\t\"shifted_potential\", \"shifted_force\", or \n"
133                   "\t\"reaction_field\".\n", myMethod.c_str() );
134          painCave.isFatal = 1;
# Line 184 | Line 198 | namespace OpenMD {
198          
199          // throw warning
200          sprintf( painCave.errMsg,
201 <                 "Electrostatic::initialize: dampingAlpha was not specified in the input file.\n"
202 <                 "\tA default value of %f (1/ang) will be used for the cutoff of\n\t%f (ang).\n",
201 >                 "Electrostatic::initialize: dampingAlpha was not specified in the\n"
202 >                 "\tinput file.  A default value of %f (1/ang) will be used for the\n"
203 >                 "\tcutoff of %f (ang).\n",
204                   dampingAlpha_, cutoffRadius_);
205          painCave.severity = OPENMD_INFO;
206          painCave.isFatal = 0;
# Line 206 | Line 221 | namespace OpenMD {
221        
222        if (at->isElectrostatic())
223          addType(at);
224 <    }
224 >    }  
225      
211
212    cutoffRadius2_ = cutoffRadius_ * cutoffRadius_;
213    rcuti_ = 1.0 / cutoffRadius_;
214    rcuti2_ = rcuti_ * rcuti_;
215    rcuti3_ = rcuti2_ * rcuti_;
216    rcuti4_ = rcuti2_ * rcuti2_;
217
218    if (screeningMethod_ == DAMPED) {
219      
220      alpha2_ = dampingAlpha_ * dampingAlpha_;
221      alpha4_ = alpha2_ * alpha2_;
222      alpha6_ = alpha4_ * alpha2_;
223      alpha8_ = alpha4_ * alpha4_;
224      
225      constEXP_ = exp(-alpha2_ * cutoffRadius2_);
226      invRootPi_ = 0.56418958354775628695;
227      alphaPi_ = 2.0 * dampingAlpha_ * invRootPi_;
228
229      c1c_ = erfc(dampingAlpha_ * cutoffRadius_) * rcuti_;
230      c2c_ = alphaPi_ * constEXP_ * rcuti_ + c1c_ * rcuti_;
231      c3c_ = 2.0 * alphaPi_ * alpha2_ + 3.0 * c2c_ * rcuti_;
232      c4c_ = 4.0 * alphaPi_ * alpha4_ + 5.0 * c3c_ * rcuti2_;
233      c5c_ = 8.0 * alphaPi_ * alpha6_ + 7.0 * c4c_ * rcuti2_;
234      c6c_ = 16.0 * alphaPi_ * alpha8_ + 9.0 * c5c_ * rcuti2_;
235    } else {
236      c1c_ = rcuti_;
237      c2c_ = c1c_ * rcuti_;
238      c3c_ = 3.0 * c2c_ * rcuti_;
239      c4c_ = 5.0 * c3c_ * rcuti2_;
240      c5c_ = 7.0 * c4c_ * rcuti2_;
241      c6c_ = 9.0 * c5c_ * rcuti2_;
242    }
243  
226      if (summationMethod_ == esm_REACTION_FIELD) {
227        preRF_ = (dielectric_ - 1.0) /
228 <        ((2.0 * dielectric_ + 1.0) * cutoffRadius2_ * cutoffRadius_);
247 <      preRF2_ = 2.0 * preRF_;
228 >        ((2.0 * dielectric_ + 1.0) * pow(cutoffRadius_,3) );
229      }
230      
231 <    RealType dx = cutoffRadius_ / RealType(np_ - 1);
232 <    RealType rval;
233 <    vector<RealType> rvals;
234 <    vector<RealType> yvals;
235 <    for (int i = 0; i < np_; i++) {
236 <      rval = RealType(i) * dx;
256 <      rvals.push_back(rval);
257 <      yvals.push_back(erfc(dampingAlpha_ * rval));
258 <    }
259 <    erfcSpline_ = new CubicSpline();
260 <    erfcSpline_->addPoints(rvals, yvals);
261 <    haveElectroSpline_ = true;
262 <
263 <    initialized_ = true;
264 <  }
265 <      
266 <  void Electrostatic::addType(AtomType* atomType){
267 <
268 <    ElectrostaticAtomData electrostaticAtomData;
269 <    electrostaticAtomData.is_Charge = false;
270 <    electrostaticAtomData.is_Dipole = false;
271 <    electrostaticAtomData.is_SplitDipole = false;
272 <    electrostaticAtomData.is_Quadrupole = false;
273 <
274 <    if (atomType->isCharge()) {
275 <      GenericData* data = atomType->getPropertyByName("Charge");
231 >    RealType b0c, b1c, b2c, b3c, b4c, b5c;
232 >    RealType db0c_1, db0c_2, db0c_3, db0c_4, db0c_5;
233 >    RealType a2, expTerm, invArootPi;
234 >    
235 >    RealType r = cutoffRadius_;
236 >    RealType r2 = r * r;
237  
238 <      if (data == NULL) {
239 <        sprintf( painCave.errMsg, "Electrostatic::addType could not find "
240 <                 "Charge\n"
241 <                 "\tparameters for atomType %s.\n",
242 <                 atomType->getName().c_str());
243 <        painCave.severity = OPENMD_ERROR;
244 <        painCave.isFatal = 1;
245 <        simError();                  
246 <      }
247 <      
248 <      DoubleGenericData* doubleData = dynamic_cast<DoubleGenericData*>(data);
249 <      if (doubleData == NULL) {
250 <        sprintf( painCave.errMsg,
251 <                 "Electrostatic::addType could not convert GenericData to "
252 <                 "Charge for\n"
253 <                 "\tatom type %s\n", atomType->getName().c_str());
254 <        painCave.severity = OPENMD_ERROR;
255 <        painCave.isFatal = 1;
256 <        simError();          
257 <      }
258 <      electrostaticAtomData.is_Charge = true;
298 <      electrostaticAtomData.charge = doubleData->getData();          
238 >    if (screeningMethod_ == DAMPED) {      
239 >      a2 = dampingAlpha_ * dampingAlpha_;
240 >      invArootPi = 1.0 / (dampingAlpha_ * sqrt(M_PI));    
241 >      expTerm = exp(-a2 * r2);
242 >      // values of Smith's B_l functions at the cutoff radius:
243 >      b0c = erfc(dampingAlpha_ * r) / r;
244 >      b1c = (      b0c     + 2.0*a2     * expTerm * invArootPi) / r2;
245 >      b2c = (3.0 * b1c + pow(2.0*a2, 2) * expTerm * invArootPi) / r2;
246 >      b3c = (5.0 * b2c + pow(2.0*a2, 3) * expTerm * invArootPi) / r2;
247 >      b4c = (7.0 * b3c + pow(2.0*a2, 4) * expTerm * invArootPi) / r2;
248 >      b5c = (9.0 * b4c + pow(2.0*a2, 5) * expTerm * invArootPi) / r2;
249 >      selfMult_ = b0c  +  2.0 * a2 * invArootPi;
250 >    } else {
251 >      a2 = 0.0;
252 >      b0c = 1.0 / r;
253 >      b1c = (      b0c) / r2;
254 >      b2c = (3.0 * b1c) / r2;
255 >      b3c = (5.0 * b2c) / r2;
256 >      b4c = (7.0 * b3c) / r2;
257 >      b5c = (9.0 * b4c) / r2;
258 >      selfMult_ = b0c;
259      }
260  
261 <    if (atomType->isDirectional()) {
262 <      DirectionalAtomType* daType = dynamic_cast<DirectionalAtomType*>(atomType);
263 <      
264 <      if (daType->isDipole()) {
265 <        GenericData* data = daType->getPropertyByName("Dipole");
266 <        
267 <        if (data == NULL) {
268 <          sprintf( painCave.errMsg,
269 <                   "Electrostatic::addType could not find Dipole\n"
270 <                   "\tparameters for atomType %s.\n",
271 <                   daType->getName().c_str());
272 <          painCave.severity = OPENMD_ERROR;
273 <          painCave.isFatal = 1;
274 <          simError();                  
275 <        }
276 <      
277 <        DoubleGenericData* doubleData = dynamic_cast<DoubleGenericData*>(data);
278 <        if (doubleData == NULL) {
279 <          sprintf( painCave.errMsg,
280 <                   "Electrostatic::addType could not convert GenericData to "
281 <                   "Dipole Moment\n"
282 <                   "\tfor atom type %s\n", daType->getName().c_str());
283 <          painCave.severity = OPENMD_ERROR;
284 <          painCave.isFatal = 1;
285 <          simError();          
286 <        }
287 <        electrostaticAtomData.is_Dipole = true;
288 <        electrostaticAtomData.dipole_moment = doubleData->getData();
261 >    // higher derivatives of B_0 at the cutoff radius:
262 >    db0c_1 = -r * b1c;
263 >    db0c_2 =     -b1c + r2 * b2c;
264 >    db0c_3 =          3.0*r*b2c  - r2*r*b3c;
265 >    db0c_4 =          3.0*b2c  - 6.0*r2*b3c     + r2*r2*b4c;
266 >    db0c_5 =                    -15.0*r*b3c + 10.0*r2*r*b4c - r2*r2*r*b5c;
267 >    
268 >    // working variables for the splines:
269 >    RealType ri, ri2;
270 >    RealType b0, b1, b2, b3, b4, b5;
271 >    RealType db0_1, db0_2, db0_3, db0_4, db0_5;
272 >    RealType f0;
273 >    RealType g0, g1, g2, g3, g4;
274 >    RealType h1, h2, h3, h4;
275 >    RealType s2, s3, s4;
276 >    RealType t3, t4;
277 >    RealType u4;
278 >
279 >    // working variables for Taylor expansion:
280 >    RealType rmRc, rmRc2, rmRc3, rmRc4;
281 >
282 >    // Add a 2 angstrom safety window to deal with cutoffGroups that
283 >    // have charged atoms longer than the cutoffRadius away from each
284 >    // other.  Splining is almost certainly the best choice here.
285 >    // Direct calls to erfc would be preferrable if it is a very fast
286 >    // implementation.
287 >
288 >    RealType dx = (cutoffRadius_ + 2.0) / RealType(np_);
289 >
290 >    // Storage vectors for the computed functions    
291 >    vector<RealType> rv;
292 >    vector<RealType> v01v, v02v;
293 >    vector<RealType> v11v, v12v, v13v;
294 >    vector<RealType> v21v, v22v, v23v, v24v;
295 >    vector<RealType> v31v, v32v, v33v, v34v, v35v;
296 >    vector<RealType> v41v, v42v, v43v, v44v, v45v, v46v;
297 >
298 >    for (int i = 1; i < np_ + 1; i++) {
299 >      r = RealType(i) * dx;
300 >      rv.push_back(r);
301 >
302 >      ri = 1.0 / r;
303 >      ri2 = ri * ri;
304 >
305 >      r2 = r * r;
306 >      expTerm = exp(-a2 * r2);
307 >
308 >      // Taylor expansion factors (no need for factorials this way):
309 >      rmRc = r - cutoffRadius_;
310 >      rmRc2 = rmRc  * rmRc / 2.0;
311 >      rmRc3 = rmRc2 * rmRc / 3.0;
312 >      rmRc4 = rmRc3 * rmRc / 4.0;
313 >
314 >      // values of Smith's B_l functions at r:
315 >      if (screeningMethod_ == DAMPED) {            
316 >        b0 = erfc(dampingAlpha_ * r) * ri;
317 >        b1 = (      b0 +     2.0*a2     * expTerm * invArootPi) * ri2;
318 >        b2 = (3.0 * b1 + pow(2.0*a2, 2) * expTerm * invArootPi) * ri2;
319 >        b3 = (5.0 * b2 + pow(2.0*a2, 3) * expTerm * invArootPi) * ri2;
320 >        b4 = (7.0 * b3 + pow(2.0*a2, 4) * expTerm * invArootPi) * ri2;
321 >        b5 = (9.0 * b4 + pow(2.0*a2, 5) * expTerm * invArootPi) * ri2;
322 >      } else {
323 >        b0 = ri;
324 >        b1 = (      b0) * ri2;
325 >        b2 = (3.0 * b1) * ri2;
326 >        b3 = (5.0 * b2) * ri2;
327 >        b4 = (7.0 * b3) * ri2;
328 >        b5 = (9.0 * b4) * ri2;
329        }
330 +                
331 +      // higher derivatives of B_0 at r:
332 +      db0_1 = -r * b1;
333 +      db0_2 =     -b1 + r2 * b2;
334 +      db0_3 =          3.0*r*b2   - r2*r*b3;
335 +      db0_4 =          3.0*b2   - 6.0*r2*b3     + r2*r2*b4;
336 +      db0_5 =                    -15.0*r*b3 + 10.0*r2*r*b4 - r2*r2*r*b5;
337  
338 <      if (daType->isSplitDipole()) {
339 <        GenericData* data = daType->getPropertyByName("SplitDipoleDistance");
338 >
339 >      switch (summationMethod_) {
340 >      case esm_SHIFTED_FORCE:
341 >        f0 = b0 - b0c - rmRc*db0c_1;
342          
343 <        if (data == NULL) {
344 <          sprintf(painCave.errMsg,
345 <                  "Electrostatic::addType could not find SplitDipoleDistance\n"
346 <                  "\tparameter for atomType %s.\n",
347 <                  daType->getName().c_str());
348 <          painCave.severity = OPENMD_ERROR;
349 <          painCave.isFatal = 1;
350 <          simError();                  
351 <        }
352 <      
353 <        DoubleGenericData* doubleData = dynamic_cast<DoubleGenericData*>(data);
354 <        if (doubleData == NULL) {
355 <          sprintf( painCave.errMsg,
356 <                   "Electrostatic::addType could not convert GenericData to "
357 <                   "SplitDipoleDistance for\n"
358 <                   "\tatom type %s\n", daType->getName().c_str());
359 <          painCave.severity = OPENMD_ERROR;
360 <          painCave.isFatal = 1;
361 <          simError();          
362 <        }
354 <        electrostaticAtomData.is_SplitDipole = true;
355 <        electrostaticAtomData.split_dipole_distance = doubleData->getData();
356 <      }
343 >        g0 = db0_1 - db0c_1;
344 >        g1 = g0 - rmRc *db0c_2;
345 >        g2 = g1 - rmRc2*db0c_3;
346 >        g3 = g2 - rmRc3*db0c_4;
347 >        g4 = g3 - rmRc4*db0c_5;
348 >        
349 >        h1 = db0_2 - db0c_2;
350 >        h2 = h1 - rmRc *db0c_3;
351 >        h3 = h2 - rmRc2*db0c_4;
352 >        h4 = h3 - rmRc3*db0c_5;
353 >        
354 >        s2 = db0_3 - db0c_3;
355 >        s3 = s2 - rmRc *db0c_4;
356 >        s4 = s3 - rmRc2*db0c_5;
357 >        
358 >        t3 = db0_4 - db0c_4;
359 >        t4 = t3 - rmRc *db0c_5;
360 >        
361 >        u4 = db0_5 - db0c_5;
362 >        break;
363  
364 <      if (daType->isQuadrupole()) {
365 <        GenericData* data = daType->getPropertyByName("QuadrupoleMoments");
364 >      case esm_SHIFTED_POTENTIAL:
365 >        f0 = b0 - b0c;
366          
367 <        if (data == NULL) {
368 <          sprintf( painCave.errMsg,
369 <                   "Electrostatic::addType could not find QuadrupoleMoments\n"
370 <                   "\tparameter for atomType %s.\n",
371 <                   daType->getName().c_str());
372 <          painCave.severity = OPENMD_ERROR;
373 <          painCave.isFatal = 1;
374 <          simError();                  
375 <        }
367 >        g0 = db0_1;
368 >        g1 = db0_1 - db0c_1;
369 >        g2 = g1 - rmRc *db0c_2;
370 >        g3 = g2 - rmRc2*db0c_3;
371 >        g4 = g3 - rmRc3*db0c_4;
372 >
373 >        h1 = db0_2;
374 >        h2 = db0_2 - db0c_2;
375 >        h3 = h2 - rmRc *db0c_3;
376 >        h4 = h3 - rmRc2*db0c_4;
377          
378 <        // Quadrupoles in OpenMD are set as the diagonal elements
379 <        // of the diagonalized traceless quadrupole moment tensor.
380 <        // The column vectors of the unitary matrix that diagonalizes
374 <        // the quadrupole moment tensor become the eFrame (or the
375 <        // electrostatic version of the body-fixed frame.
378 >        s2 = db0_3;
379 >        s3 = db0_3 - db0c_3;
380 >        s4 = s3 - rmRc *db0c_4;
381  
382 <        Vector3dGenericData* v3dData = dynamic_cast<Vector3dGenericData*>(data);
383 <        if (v3dData == NULL) {
384 <          sprintf( painCave.errMsg,
385 <                   "Electrostatic::addType could not convert GenericData to "
386 <                   "Quadrupole Moments for\n"
387 <                   "\tatom type %s\n", daType->getName().c_str());
388 <          painCave.severity = OPENMD_ERROR;
389 <          painCave.isFatal = 1;
390 <          simError();          
382 >        t3 = db0_4;
383 >        t4 = db0_4 - db0c_4;
384 >        
385 >        u4 = db0_5;
386 >        break;
387 >
388 >      case esm_SWITCHING_FUNCTION:
389 >      case esm_HARD:
390 >        f0 = b0;
391 >        
392 >        g0 = db0_1;
393 >        g1 = g0;
394 >        g2 = g1;
395 >        g3 = g2;
396 >        g4 = g3;
397 >        
398 >        h1 = db0_2;
399 >        h2 = h1;
400 >        h3 = h2;
401 >        h4 = h3;
402 >        
403 >        s2 = db0_3;
404 >        s3 = s2;
405 >        s4 = s3;
406 >        
407 >        t3 = db0_4;
408 >        t4 = t3;
409 >        
410 >        u4 = db0_5;
411 >        break;
412 >
413 >      case esm_REACTION_FIELD:
414 >
415 >        // following DL_POLY's lead for shifting the image charge potential:
416 >        f0 = b0  + preRF_ * r2
417 >          - (b0c + preRF_ * cutoffRadius_ * cutoffRadius_);
418 >
419 >        g0 = db0_1 + preRF_ * 2.0 * r;
420 >        g1 = g0;
421 >        g2 = g1;
422 >        g3 = g2;
423 >        g4 = g3;
424 >
425 >        h1 = db0_2 + preRF_ * 2.0;
426 >        h2 = h1;
427 >        h3 = h2;
428 >        h4 = h3;
429 >
430 >        s2 = db0_3;
431 >        s3 = s2;
432 >        s4 = s3;
433 >        
434 >        t3 = db0_4;
435 >        t4 = t3;
436 >        
437 >        u4 = db0_5;        
438 >        break;
439 >                
440 >      case esm_EWALD_FULL:
441 >      case esm_EWALD_PME:
442 >      case esm_EWALD_SPME:
443 >      default :
444 >        map<string, ElectrostaticSummationMethod>::iterator i;
445 >        std::string meth;
446 >        for (i = summationMap_.begin(); i != summationMap_.end(); ++i) {
447 >          if ((*i).second == summationMethod_) meth = (*i).first;
448          }
449 +        sprintf( painCave.errMsg,
450 +                 "Electrostatic::initialize: electrostaticSummationMethod %s \n"
451 +                 "\thas not been implemented yet. Please select one of:\n"
452 +                 "\t\"hard\", \"shifted_potential\", or \"shifted_force\"\n",
453 +                 meth.c_str() );
454 +        painCave.isFatal = 1;
455 +        simError();
456 +        break;      
457 +      }
458 +
459 +      v01 = f0;
460 +      v02 = g0;
461 +
462 +      v11 = g1;
463 +      v12 = g1 * ri;
464 +      v13 = h1 - v12;
465 +
466 +      v21 = g2 * ri;
467 +      v22 = h2 - v21;
468 +      v23 = v22 * ri;
469 +      v24 = s2 - 3.0*v23;        
470 +
471 +      v31 = (h3 - g3 * ri) * ri;
472 +      v32 = s3 - 3.0*v31;
473 +      v33 = v31 * ri;
474 +      v34 = v32 * ri;
475 +      v35 = t3 - 6.0*v34 - 3.0*v33;
476 +
477 +      v41 = (h4 - g4 * ri) * ri2;
478 +      v42 = s4 * ri - 3.0*v41;
479 +      v43 = t4 - 6.0*v42 - 3.0*v41;
480 +      v44 = v42 * ri;
481 +      v45 = v43 * ri;
482 +      v46 = u4 - 10.0*v45 - 15.0*v44;
483 +
484 +      // Add these computed values to the storage vectors for spline creation:
485 +      v01v.push_back(v01);
486 +      v02v.push_back(v02);
487 +
488 +      v11v.push_back(v11);
489 +      v12v.push_back(v12);
490 +      v13v.push_back(v13);
491 +
492 +      v21v.push_back(v21);
493 +      v22v.push_back(v22);
494 +      v23v.push_back(v23);
495 +      v24v.push_back(v24);
496 +
497 +      v31v.push_back(v31);
498 +      v32v.push_back(v32);
499 +      v33v.push_back(v33);
500 +      v34v.push_back(v34);
501 +      v35v.push_back(v35);
502 +
503 +      v41v.push_back(v41);
504 +      v42v.push_back(v42);
505 +      v43v.push_back(v43);
506 +      v44v.push_back(v44);
507 +      v45v.push_back(v45);
508 +      v46v.push_back(v46);
509 +    }
510 +
511 +    // construct the spline structures and fill them with the values we've
512 +    // computed:
513 +
514 +    v01s = new CubicSpline();
515 +    v01s->addPoints(rv, v01v);
516 +    v02s = new CubicSpline();
517 +    v02s->addPoints(rv, v02v);
518 +
519 +    v11s = new CubicSpline();
520 +    v11s->addPoints(rv, v11v);
521 +    v12s = new CubicSpline();
522 +    v12s->addPoints(rv, v12v);
523 +    v13s = new CubicSpline();
524 +    v13s->addPoints(rv, v13v);
525 +
526 +    v21s = new CubicSpline();
527 +    v21s->addPoints(rv, v21v);
528 +    v22s = new CubicSpline();
529 +    v22s->addPoints(rv, v22v);
530 +    v23s = new CubicSpline();
531 +    v23s->addPoints(rv, v23v);
532 +    v24s = new CubicSpline();
533 +    v24s->addPoints(rv, v24v);
534 +
535 +    v31s = new CubicSpline();
536 +    v31s->addPoints(rv, v31v);
537 +    v32s = new CubicSpline();
538 +    v32s->addPoints(rv, v32v);
539 +    v33s = new CubicSpline();
540 +    v33s->addPoints(rv, v33v);
541 +    v34s = new CubicSpline();
542 +    v34s->addPoints(rv, v34v);
543 +    v35s = new CubicSpline();
544 +    v35s->addPoints(rv, v35v);
545 +
546 +    v41s = new CubicSpline();
547 +    v41s->addPoints(rv, v41v);
548 +    v42s = new CubicSpline();
549 +    v42s->addPoints(rv, v42v);
550 +    v43s = new CubicSpline();
551 +    v43s->addPoints(rv, v43v);
552 +    v44s = new CubicSpline();
553 +    v44s->addPoints(rv, v44v);
554 +    v45s = new CubicSpline();
555 +    v45s->addPoints(rv, v45v);
556 +    v46s = new CubicSpline();
557 +    v46s->addPoints(rv, v46v);
558 +
559 +    haveElectroSplines_ = true;
560 +
561 +    initialized_ = true;
562 +  }
563 +      
564 +  void Electrostatic::addType(AtomType* atomType){
565 +
566 +    ElectrostaticAtomData electrostaticAtomData;
567 +    electrostaticAtomData.is_Charge = false;
568 +    electrostaticAtomData.is_Dipole = false;
569 +    electrostaticAtomData.is_Quadrupole = false;
570 +    electrostaticAtomData.is_Fluctuating = false;
571 +
572 +    FixedChargeAdapter fca = FixedChargeAdapter(atomType);
573 +
574 +    if (fca.isFixedCharge()) {
575 +      electrostaticAtomData.is_Charge = true;
576 +      electrostaticAtomData.fixedCharge = fca.getCharge();
577 +    }
578 +
579 +    MultipoleAdapter ma = MultipoleAdapter(atomType);
580 +    if (ma.isMultipole()) {
581 +      if (ma.isDipole()) {
582 +        electrostaticAtomData.is_Dipole = true;
583 +        electrostaticAtomData.dipole = ma.getDipole();
584 +      }
585 +      if (ma.isQuadrupole()) {
586          electrostaticAtomData.is_Quadrupole = true;
587 <        electrostaticAtomData.quadrupole_moments = v3dData->getData();
587 >        electrostaticAtomData.quadrupole = ma.getQuadrupole();
588        }
589      }
590      
591 <    AtomTypeProperties atp = atomType->getATP();    
591 >    FluctuatingChargeAdapter fqa = FluctuatingChargeAdapter(atomType);
592  
593 +    if (fqa.isFluctuatingCharge()) {
594 +      electrostaticAtomData.is_Fluctuating = true;
595 +      electrostaticAtomData.electronegativity = fqa.getElectronegativity();
596 +      electrostaticAtomData.hardness = fqa.getHardness();
597 +      electrostaticAtomData.slaterN = fqa.getSlaterN();
598 +      electrostaticAtomData.slaterZeta = fqa.getSlaterZeta();
599 +    }
600 +
601      pair<map<int,AtomType*>::iterator,bool> ret;    
602 <    ret = ElectrostaticList.insert( pair<int,AtomType*>(atp.ident, atomType) );
602 >    ret = ElectrostaticList.insert( pair<int,AtomType*>(atomType->getIdent(),
603 >                                                        atomType) );
604      if (ret.second == false) {
605        sprintf( painCave.errMsg,
606                 "Electrostatic already had a previous entry with ident %d\n",
607 <               atp.ident);
607 >               atomType->getIdent() );
608        painCave.severity = OPENMD_INFO;
609        painCave.isFatal = 0;
610        simError();        
611      }
612      
613 <    ElectrostaticMap[atomType] = electrostaticAtomData;    
613 >    ElectrostaticMap[atomType] = electrostaticAtomData;  
614 >
615 >    // Now, iterate over all known types and add to the mixing map:
616 >    
617 >    map<AtomType*, ElectrostaticAtomData>::iterator it;
618 >    for( it = ElectrostaticMap.begin(); it != ElectrostaticMap.end(); ++it) {
619 >      AtomType* atype2 = (*it).first;
620 >      ElectrostaticAtomData eaData2 = (*it).second;
621 >      if (eaData2.is_Fluctuating && electrostaticAtomData.is_Fluctuating) {
622 >        
623 >        RealType a = electrostaticAtomData.slaterZeta;
624 >        RealType b = eaData2.slaterZeta;
625 >        int m = electrostaticAtomData.slaterN;
626 >        int n = eaData2.slaterN;
627 >
628 >        // Create the spline of the coulombic integral for s-type
629 >        // Slater orbitals.  Add a 2 angstrom safety window to deal
630 >        // with cutoffGroups that have charged atoms longer than the
631 >        // cutoffRadius away from each other.
632 >
633 >        RealType rval;
634 >        RealType dr = (cutoffRadius_ + 2.0) / RealType(np_ - 1);
635 >        vector<RealType> rvals;
636 >        vector<RealType> Jvals;
637 >        // don't start at i = 0, as rval = 0 is undefined for the
638 >        // slater overlap integrals.
639 >        for (int i = 1; i < np_+1; i++) {
640 >          rval = RealType(i) * dr;
641 >          rvals.push_back(rval);
642 >          Jvals.push_back(sSTOCoulInt( a, b, m, n, rval *
643 >                                       PhysicalConstants::angstromToBohr ) *
644 >                          PhysicalConstants::hartreeToKcal );
645 >        }
646 >        
647 >        CubicSpline* J = new CubicSpline();
648 >        J->addPoints(rvals, Jvals);
649 >        
650 >        pair<AtomType*, AtomType*> key1, key2;
651 >        key1 = make_pair(atomType, atype2);
652 >        key2 = make_pair(atype2, atomType);
653 >        
654 >        Jij[key1] = J;
655 >        Jij[key2] = J;
656 >      }
657 >    }
658 >
659      return;
660    }
661    
662    void Electrostatic::setCutoffRadius( RealType rCut ) {
663      cutoffRadius_ = rCut;
411    rrf_ = cutoffRadius_;
664      haveCutoffRadius_ = true;
665    }
666  
415  void Electrostatic::setSwitchingRadius( RealType rSwitch ) {
416    rt_ = rSwitch;
417  }
667    void Electrostatic::setElectrostaticSummationMethod( ElectrostaticSummationMethod esm ) {
668      summationMethod_ = esm;
669    }
# Line 432 | Line 681 | namespace OpenMD {
681  
682    void Electrostatic::calcForce(InteractionData &idat) {
683  
684 <    // utility variables.  Should clean these up and use the Vector3d and
685 <    // Mat3x3d to replace as many as we can in future versions:
684 >    RealType C_a, C_b;  // Charges
685 >    Vector3d D_a, D_b;  // Dipoles (space-fixed)
686 >    Mat3x3d  Q_a, Q_b;  // Quadrupoles (space-fixed)
687  
688 <    RealType q_i, q_j, mu_i, mu_j, d_i, d_j;
689 <    RealType qxx_i, qyy_i, qzz_i;
690 <    RealType qxx_j, qyy_j, qzz_j;
691 <    RealType cx_i, cy_i, cz_i;
692 <    RealType cx_j, cy_j, cz_j;
693 <    RealType cx2, cy2, cz2;
444 <    RealType ct_i, ct_j, ct_ij, a1;
445 <    RealType riji, ri, ri2, ri3, ri4;
446 <    RealType pref, vterm, epot, dudr;
447 <    RealType scale, sc2;
448 <    RealType pot_term, preVal, rfVal;
449 <    RealType c2ri, c3ri, c4rij, cti3, ctj3, ctidotj;
450 <    RealType preSw, preSwSc;
451 <    RealType c1, c2, c3, c4;
452 <    RealType erfcVal, derfcVal;
453 <    RealType BigR;
688 >    RealType ri, ri2, ri3, ri4;                  // Distance utility scalars
689 >    RealType rdDa, rdDb;                         // Dipole utility scalars
690 >    Vector3d rxDa, rxDb;                         // Dipole utility vectors
691 >    RealType rdQar, rdQbr, trQa, trQb;           // Quadrupole utility scalars
692 >    Vector3d Qar, Qbr, rQa, rQb, rxQar, rxQbr;   // Quadrupole utility vectors
693 >    RealType pref;
694  
695 <    Vector3d Q_i, Q_j;
696 <    Vector3d ux_i, uy_i, uz_i;
697 <    Vector3d ux_j, uy_j, uz_j;
698 <    Vector3d dudux_i, duduy_i, duduz_i;
459 <    Vector3d dudux_j, duduy_j, duduz_j;
460 <    Vector3d rhatdot2, rhatc4;
461 <    Vector3d dVdr;
695 >    RealType DadDb, trQaQb, DadQbr, DbdQar;       // Cross-interaction scalars
696 >    Vector3d DaxDb, DadQb, DbdQa, DaxQbr, DbxQar; // Cross-interaction vectors
697 >    Vector3d rQaQb, QaQbr, QaxQb;
698 >    Mat3x3d  QaQb;                                // Cross-interaction matrices
699  
700 <    pair<RealType, RealType> res;
700 >    RealType U(0.0);  // Potential
701 >    Vector3d F(0.0);  // Force
702 >    Vector3d Ta(0.0); // Torque on site a
703 >    Vector3d Tb(0.0); // Torque on site b
704 >    Vector3d Ea(0.0); // Electric field at site a
705 >    Vector3d Eb(0.0); // Electric field at site b
706 >    RealType dUdCa(0.0); // fluctuating charge force at site a
707 >    RealType dUdCb(0.0); // fluctuating charge force at site a
708      
709 +    // Indirect interactions mediated by the reaction field.
710 +    RealType indirect_Pot(0.0);  // Potential
711 +    Vector3d indirect_F(0.0);    // Force
712 +    Vector3d indirect_Ta(0.0);   // Torque on site a
713 +    Vector3d indirect_Tb(0.0);   // Torque on site b
714 +
715 +    // Excluded potential that is still computed for fluctuating charges
716 +    RealType excluded_Pot(0.0);
717 +
718 +    RealType rfContrib, coulInt;
719 +    
720 +    // spline for coulomb integral
721 +    CubicSpline* J;
722 +
723      if (!initialized_) initialize();
724      
725      ElectrostaticAtomData data1 = ElectrostaticMap[idat.atypes.first];
# Line 469 | Line 727 | namespace OpenMD {
727      
728      // some variables we'll need independent of electrostatic type:
729  
730 <    riji = 1.0 /  *(idat.rij) ;
731 <    Vector3d rhat =  *(idat.d)   * riji;
732 <
730 >    ri = 1.0 /  *(idat.rij);
731 >    Vector3d rhat =  *(idat.d)  * ri;
732 >    ri2 = ri * ri;
733 >      
734      // logicals
735  
736 <    bool i_is_Charge = data1.is_Charge;
737 <    bool i_is_Dipole = data1.is_Dipole;
738 <    bool i_is_SplitDipole = data1.is_SplitDipole;
739 <    bool i_is_Quadrupole = data1.is_Quadrupole;
736 >    bool a_is_Charge = data1.is_Charge;
737 >    bool a_is_Dipole = data1.is_Dipole;
738 >    bool a_is_Quadrupole = data1.is_Quadrupole;
739 >    bool a_is_Fluctuating = data1.is_Fluctuating;
740  
741 <    bool j_is_Charge = data2.is_Charge;
742 <    bool j_is_Dipole = data2.is_Dipole;
743 <    bool j_is_SplitDipole = data2.is_SplitDipole;
744 <    bool j_is_Quadrupole = data2.is_Quadrupole;
486 <    
487 <    if (i_is_Charge)
488 <      q_i = data1.charge;
741 >    bool b_is_Charge = data2.is_Charge;
742 >    bool b_is_Dipole = data2.is_Dipole;
743 >    bool b_is_Quadrupole = data2.is_Quadrupole;
744 >    bool b_is_Fluctuating = data2.is_Fluctuating;
745  
746 <    if (i_is_Dipole) {
747 <      mu_i = data1.dipole_moment;
492 <      uz_i = idat.eFrame1->getColumn(2);
493 <      
494 <      ct_i = dot(uz_i, rhat);
495 <
496 <      if (i_is_SplitDipole)
497 <        d_i = data1.split_dipole_distance;
498 <      
499 <      duduz_i = V3Zero;
500 <    }
746 >    // Obtain all of the required radial function values from the
747 >    // spline structures:
748      
749 <    if (i_is_Quadrupole) {
750 <      Q_i = data1.quadrupole_moments;
751 <      qxx_i = Q_i.x();
505 <      qyy_i = Q_i.y();
506 <      qzz_i = Q_i.z();
507 <      
508 <      ux_i = idat.eFrame1->getColumn(0);
509 <      uy_i = idat.eFrame1->getColumn(1);
510 <      uz_i = idat.eFrame1->getColumn(2);
511 <
512 <      cx_i = dot(ux_i, rhat);
513 <      cy_i = dot(uy_i, rhat);
514 <      cz_i = dot(uz_i, rhat);
515 <
516 <      dudux_i = V3Zero;
517 <      duduy_i = V3Zero;
518 <      duduz_i = V3Zero;
749 >    if (a_is_Charge && b_is_Charge) {
750 >      v01 = v01s->getValueAt( *(idat.rij) );
751 >      v02 = v02s->getValueAt( *(idat.rij) );
752      }
753 +    if ((a_is_Charge && b_is_Dipole) || (b_is_Charge && a_is_Dipole)) {
754 +      v11 = v11s->getValueAt( *(idat.rij) );
755 +      v12 = v12s->getValueAt( *(idat.rij) );
756 +      v13 = v13s->getValueAt( *(idat.rij) );
757 +    }
758 +    if ((a_is_Charge && b_is_Quadrupole) ||
759 +        (b_is_Charge && a_is_Quadrupole) ||
760 +        (a_is_Dipole && b_is_Dipole)) {
761 +      v21 = v21s->getValueAt( *(idat.rij) );
762 +      v22 = v22s->getValueAt( *(idat.rij) );
763 +      v23 = v23s->getValueAt( *(idat.rij) );
764 +      v24 = v24s->getValueAt( *(idat.rij) );
765 +    }
766 +    if ((a_is_Dipole && b_is_Quadrupole) ||
767 +        (b_is_Dipole && a_is_Quadrupole)) {
768 +      v31 = v31s->getValueAt( *(idat.rij) );
769 +      v32 = v32s->getValueAt( *(idat.rij) );
770 +      v33 = v33s->getValueAt( *(idat.rij) );
771 +      v34 = v34s->getValueAt( *(idat.rij) );
772 +      v35 = v35s->getValueAt( *(idat.rij) );
773 +    }
774 +    if (a_is_Quadrupole && b_is_Quadrupole) {
775 +      v41 = v41s->getValueAt( *(idat.rij) );
776 +      v42 = v42s->getValueAt( *(idat.rij) );
777 +      v43 = v43s->getValueAt( *(idat.rij) );
778 +      v44 = v44s->getValueAt( *(idat.rij) );
779 +      v45 = v45s->getValueAt( *(idat.rij) );
780 +      v46 = v46s->getValueAt( *(idat.rij) );
781 +    }
782  
521    if (j_is_Charge)
522      q_j = data2.charge;
783  
784 <    if (j_is_Dipole) {
785 <      mu_j = data2.dipole_moment;
786 <      uz_j = idat.eFrame2->getColumn(2);
784 >    // calculate the single-site contributions (fields, etc).
785 >    
786 >    if (a_is_Charge) {
787 >      C_a = data1.fixedCharge;
788        
789 <      ct_j = dot(uz_j, rhat);
790 <
791 <      if (j_is_SplitDipole)
531 <        d_j = data2.split_dipole_distance;
789 >      if (a_is_Fluctuating) {
790 >        C_a += *(idat.flucQ1);
791 >      }
792        
793 <      duduz_j = V3Zero;
793 >      if (idat.excluded) {
794 >        *(idat.skippedCharge2) += C_a;
795 >      }
796 >      Eb -= C_a *  pre11_ * v02 * rhat;
797      }
798      
799 <    if (j_is_Quadrupole) {
800 <      Q_j = data2.quadrupole_moments;
801 <      qxx_j = Q_j.x();
802 <      qyy_j = Q_j.y();
803 <      qzz_j = Q_j.z();
541 <      
542 <      ux_j = idat.eFrame2->getColumn(0);
543 <      uy_j = idat.eFrame2->getColumn(1);
544 <      uz_j = idat.eFrame2->getColumn(2);
545 <
546 <      cx_j = dot(ux_j, rhat);
547 <      cy_j = dot(uy_j, rhat);
548 <      cz_j = dot(uz_j, rhat);
549 <
550 <      dudux_j = V3Zero;
551 <      duduy_j = V3Zero;
552 <      duduz_j = V3Zero;
799 >    if (a_is_Dipole) {
800 >      D_a = *(idat.dipole1);
801 >      rdDa = dot(rhat, D_a);
802 >      rxDa = cross(rhat, D_a);
803 >      Eb -=  pre12_ * (v13 * rdDa * rhat + v12 * D_a);
804      }
805      
806 <    epot = 0.0;
807 <    dVdr = V3Zero;
806 >    if (a_is_Quadrupole) {
807 >      Q_a = *(idat.quadrupole1);
808 >      trQa =  Q_a.trace();
809 >      Qar =   Q_a * rhat;
810 >      rQa = rhat * Q_a;
811 >      rdQar = dot(rhat, Qar);
812 >      rxQar = cross(rhat, Qar);
813 >      Eb -= pre14_ * ((trQa * rhat + 2.0 * Qar) * v23 + rdQar * rhat * v24);
814 >    }
815      
816 <    if (i_is_Charge) {
816 >    if (b_is_Charge) {
817 >      C_b = data2.fixedCharge;
818        
819 <      if (j_is_Charge) {
820 <        if (screeningMethod_ == DAMPED) {
821 <          // assemble the damping variables
822 <          res = erfcSpline_->getValueAndDerivativeAt( *(idat.rij) );
823 <          erfcVal = res.first;
824 <          derfcVal = res.second;
825 <          c1 = erfcVal * riji;
826 <          c2 = (-derfcVal + c1) * riji;
827 <        } else {
828 <          c1 = riji;
829 <          c2 = c1 * riji;
830 <        }
819 >      if (b_is_Fluctuating)
820 >        C_b += *(idat.flucQ2);
821 >      
822 >      if (idat.excluded) {
823 >        *(idat.skippedCharge1) += C_b;
824 >      }
825 >      Ea += C_b *  pre11_ * v02 * rhat;
826 >    }
827 >    
828 >    if (b_is_Dipole) {
829 >      D_b = *(idat.dipole2);
830 >      rdDb = dot(rhat, D_b);
831 >      rxDb = cross(rhat, D_b);
832 >      Ea += pre12_ * (v13 * rdDb * rhat + v12 * D_b);
833 >    }
834 >    
835 >    if (b_is_Quadrupole) {
836 >      Q_b = *(idat.quadrupole2);
837 >      trQb =  Q_b.trace();
838 >      Qbr =   Q_b * rhat;
839 >      rQb = rhat * Q_b;
840 >      rdQbr = dot(rhat, Qbr);
841 >      rxQbr = cross(rhat, Qbr);
842 >      Ea += pre14_ * ((trQb * rhat + 2.0 * Qbr) * v23 + rdQbr * rhat * v24);
843 >    }
844 >    
845 >    if ((a_is_Fluctuating || b_is_Fluctuating) && idat.excluded) {
846 >      J = Jij[idat.atypes];
847 >    }    
848 >    
849 >    if (a_is_Charge) {    
850 >      
851 >      if (b_is_Charge) {
852 >        pref =  pre11_ * *(idat.electroMult);          
853 >        U  += C_a * C_b * pref * v01;
854 >        F  += C_a * C_b * pref * v02 * rhat;
855 >        
856 >        // If this is an excluded pair, there are still indirect
857 >        // interactions via the reaction field we must worry about:
858  
859 <        preVal =  *(idat.electroMult) * pre11_ * q_i * q_j;
859 >        if (summationMethod_ == esm_REACTION_FIELD && idat.excluded) {
860 >          rfContrib = preRF_ * pref * C_a * C_b * *(idat.r2);
861 >          indirect_Pot += rfContrib;
862 >          indirect_F   += rfContrib * 2.0 * ri * rhat;
863 >        }
864          
865 <        if (summationMethod_ == esm_SHIFTED_POTENTIAL) {
866 <          vterm = preVal * (c1 - c1c_);
867 <          dudr  = - *(idat.sw)  * preVal * c2;
865 >        // Fluctuating charge forces are handled via Coulomb integrals
866 >        // for excluded pairs (i.e. those connected via bonds) and
867 >        // with the standard charge-charge interaction otherwise.
868  
869 <        } else if (summationMethod_ == esm_SHIFTED_FORCE)  {
870 <          vterm = preVal * ( c1 - c1c_ + c2c_*( *(idat.rij)  - cutoffRadius_) );
871 <          dudr  =  *(idat.sw)  * preVal * (c2c_ - c2);
869 >        if (idat.excluded) {          
870 >          if (a_is_Fluctuating || b_is_Fluctuating) {
871 >            coulInt = J->getValueAt( *(idat.rij) );
872 >            if (a_is_Fluctuating)  dUdCa += coulInt * C_b;
873 >            if (b_is_Fluctuating)  dUdCb += coulInt * C_a;
874 >            excluded_Pot += C_a * C_b * coulInt;
875 >          }          
876 >        } else {
877 >          if (a_is_Fluctuating) dUdCa += C_b * pref * v01;
878 >          if (a_is_Fluctuating) dUdCb += C_a * pref * v01;
879 >        }
880 >      }
881  
882 <        } else if (summationMethod_ == esm_REACTION_FIELD) {
883 <          rfVal =  *(idat.electroMult) * preRF_ *  *(idat.rij)  *  *(idat.rij) ;
884 <          vterm = preVal * ( riji + rfVal );            
885 <          dudr  =  *(idat.sw)  * preVal * ( 2.0 * rfVal - riji ) * riji;
882 >      if (b_is_Dipole) {
883 >        pref =  pre12_ * *(idat.electroMult);        
884 >        U  += C_a * pref * v11 * rdDb;
885 >        F  += C_a * pref * (v13 * rdDb * rhat + v12 * D_b);
886 >        Tb += C_a * pref * v11 * rxDb;
887  
888 <        } else {
589 <          vterm = preVal * riji * erfcVal;            
888 >        if (a_is_Fluctuating) dUdCa += pref * v11 * rdDb;
889  
890 <          dudr  = -  *(idat.sw)  * preVal * c2;
890 >        // Even if we excluded this pair from direct interactions, we
891 >        // still have the reaction-field-mediated charge-dipole
892 >        // interaction:
893  
894 +        if (summationMethod_ == esm_REACTION_FIELD && idat.excluded) {
895 +          rfContrib = C_a * pref * preRF_ * 2.0 * *(idat.rij);
896 +          indirect_Pot += rfContrib * rdDb;
897 +          indirect_F   += rfContrib * D_b / (*idat.rij);
898 +          indirect_Tb  += C_a * pref * preRF_ * rxDb;
899          }
594
595        *(idat.vpair) += vterm;
596        epot +=  *(idat.sw)  * vterm;
597
598        dVdr += dudr * rhat;      
900        }
901  
902 <      if (j_is_Dipole) {
903 <        // pref is used by all the possible methods
904 <        pref =  *(idat.electroMult) * pre12_ * q_i * mu_j;
905 <        preSw =  *(idat.sw)  * pref;
902 >      if (b_is_Quadrupole) {
903 >        pref = pre14_ * *(idat.electroMult);
904 >        U  +=  C_a * pref * (v21 * trQb + v22 * rdQbr);
905 >        F  +=  C_a * pref * (trQb * rhat + 2.0 * Qbr) * v23;
906 >        F  +=  C_a * pref * rdQbr * rhat * v24;
907 >        Tb +=  C_a * pref * 2.0 * rxQbr * v22;
908  
909 <        if (summationMethod_ == esm_REACTION_FIELD) {
910 <          ri2 = riji * riji;
911 <          ri3 = ri2 * riji;
609 <    
610 <          vterm = - pref * ct_j * ( ri2 - preRF2_ *  *(idat.rij)  );
611 <          *(idat.vpair) += vterm;
612 <          epot +=  *(idat.sw)  * vterm;
909 >        if (a_is_Fluctuating) dUdCa += pref * (v21 * trQb + v22 * rdQbr);
910 >      }
911 >    }
912  
913 <          dVdr +=  -preSw * (ri3 * (uz_j - 3.0 * ct_j * rhat) - preRF2_*uz_j);
615 <          duduz_j += -preSw * rhat * (ri2 - preRF2_ *  *(idat.rij) );  
913 >    if (a_is_Dipole) {
914  
915 <        } else {
916 <          // determine the inverse r used if we have split dipoles
619 <          if (j_is_SplitDipole) {
620 <            BigR = sqrt( *(idat.r2) + 0.25 * d_j * d_j);
621 <            ri = 1.0 / BigR;
622 <            scale =  *(idat.rij)  * ri;
623 <          } else {
624 <            ri = riji;
625 <            scale = 1.0;
626 <          }
627 <          
628 <          sc2 = scale * scale;
915 >      if (b_is_Charge) {
916 >        pref = pre12_ * *(idat.electroMult);
917  
918 <          if (screeningMethod_ == DAMPED) {
919 <            // assemble the damping variables
920 <            res = erfcSpline_->getValueAndDerivativeAt( *(idat.rij) );
633 <            erfcVal = res.first;
634 <            derfcVal = res.second;
635 <            c1 = erfcVal * ri;
636 <            c2 = (-derfcVal + c1) * ri;
637 <            c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * ri;
638 <          } else {
639 <            c1 = ri;
640 <            c2 = c1 * ri;
641 <            c3 = 3.0 * c2 * ri;
642 <          }
643 <            
644 <          c2ri = c2 * ri;
918 >        U  -= C_b * pref * v11 * rdDa;
919 >        F  -= C_b * pref * (v13 * rdDa * rhat + v12 * D_a);
920 >        Ta -= C_b * pref * v11 * rxDa;
921  
922 <          // calculate the potential
647 <          pot_term =  scale * c2;
648 <          vterm = -pref * ct_j * pot_term;
649 <          *(idat.vpair) += vterm;
650 <          epot +=  *(idat.sw)  * vterm;
651 <            
652 <          // calculate derivatives for forces and torques
922 >        if (b_is_Fluctuating) dUdCb -= pref * v11 * rdDa;
923  
924 <          dVdr += -preSw * (uz_j * c2ri - ct_j * rhat * sc2 * c3);
925 <          duduz_j += -preSw * pot_term * rhat;
926 <
924 >        // Even if we excluded this pair from direct interactions,
925 >        // we still have the reaction-field-mediated charge-dipole
926 >        // interaction:
927 >        if (summationMethod_ == esm_REACTION_FIELD && idat.excluded) {
928 >          rfContrib = C_b * pref * preRF_ * 2.0 * *(idat.rij);
929 >          indirect_Pot -= rfContrib * rdDa;
930 >          indirect_F   -= rfContrib * D_a / (*idat.rij);
931 >          indirect_Ta  -= C_b * pref * preRF_ * rxDa;
932          }
933        }
934  
935 <      if (j_is_Quadrupole) {
936 <        // first precalculate some necessary variables
937 <        cx2 = cx_j * cx_j;
938 <        cy2 = cy_j * cy_j;
939 <        cz2 = cz_j * cz_j;
940 <        pref =   *(idat.electroMult) * pre14_ * q_i * one_third_;
941 <          
942 <        if (screeningMethod_ == DAMPED) {
943 <          // assemble the damping variables
944 <          res = erfcSpline_->getValueAndDerivativeAt( *(idat.rij) );
945 <          erfcVal = res.first;
946 <          derfcVal = res.second;
947 <          c1 = erfcVal * riji;
948 <          c2 = (-derfcVal + c1) * riji;
949 <          c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * riji;
950 <          c4 = -4.0 * derfcVal * alpha4_ + 5.0 * c3 * riji * riji;
951 <        } else {
952 <          c1 = riji;
953 <          c2 = c1 * riji;
679 <          c3 = 3.0 * c2 * riji;
680 <          c4 = 5.0 * c3 * riji * riji;
935 >      if (b_is_Dipole) {
936 >        pref = pre22_ * *(idat.electroMult);
937 >        DadDb = dot(D_a, D_b);
938 >        DaxDb = cross(D_a, D_b);
939 >
940 >        U  -= pref * (DadDb * v21 + rdDa * rdDb * v22);
941 >        F  -= pref * (DadDb * rhat + rdDb * D_a + rdDa * D_b)*v23;
942 >        F  -= pref * (rdDa * rdDb) * v24 * rhat;
943 >        Ta += pref * ( v21 * DaxDb - v22 * rdDb * rxDa);
944 >        Tb += pref * (-v21 * DaxDb + v22 * rdDa * rxDb);
945 >
946 >        // Even if we excluded this pair from direct interactions, we
947 >        // still have the reaction-field-mediated dipole-dipole
948 >        // interaction:
949 >        if (summationMethod_ == esm_REACTION_FIELD && idat.excluded) {
950 >          rfContrib = -pref * preRF_ * 2.0;
951 >          indirect_Pot += rfContrib * DadDb;
952 >          indirect_Ta  += rfContrib * DaxDb;
953 >          indirect_Tb  -= rfContrib * DaxDb;
954          }
955  
956 <        // precompute variables for convenience
684 <        preSw =  *(idat.sw)  * pref;
685 <        c2ri = c2 * riji;
686 <        c3ri = c3 * riji;
687 <        c4rij = c4 *  *(idat.rij) ;
688 <        rhatdot2 = 2.0 * rhat * c3;
689 <        rhatc4 = rhat * c4rij;
956 >      }
957  
958 <        // calculate the potential
959 <        pot_term = ( qxx_j * (cx2*c3 - c2ri) +
960 <                     qyy_j * (cy2*c3 - c2ri) +
961 <                     qzz_j * (cz2*c3 - c2ri) );
962 <        vterm = pref * pot_term;
696 <        *(idat.vpair) += vterm;
697 <        epot +=  *(idat.sw)  * vterm;
698 <                
699 <        // calculate derivatives for the forces and torques
958 >      if (b_is_Quadrupole) {
959 >        pref = pre24_ * *(idat.electroMult);
960 >        DadQb = D_a * Q_b;
961 >        DadQbr = dot(D_a, Qbr);
962 >        DaxQbr = cross(D_a, Qbr);
963  
964 <        dVdr += -preSw * ( qxx_j* (cx2*rhatc4 - (2.0*cx_j*ux_j + rhat)*c3ri) +
965 <                           qyy_j* (cy2*rhatc4 - (2.0*cy_j*uy_j + rhat)*c3ri) +
966 <                           qzz_j* (cz2*rhatc4 - (2.0*cz_j*uz_j + rhat)*c3ri));
967 <                          
968 <        dudux_j += preSw * qxx_j * cx_j * rhatdot2;
969 <        duduy_j += preSw * qyy_j * cy_j * rhatdot2;
970 <        duduz_j += preSw * qzz_j * cz_j * rhatdot2;
964 >        U  -= pref * ((trQb*rdDa + 2.0*DadQbr)*v31 + rdDa*rdQbr*v32);
965 >        F  -= pref * (trQb*D_a + 2.0*DadQb) * v33;
966 >        F  -= pref * (trQb*rdDa*rhat + 2.0*DadQbr*rhat + D_a*rdQbr
967 >                      + 2.0*rdDa*rQb)*v34;
968 >        F  -= pref * (rdDa * rdQbr * rhat * v35);
969 >        Ta += pref * ((-trQb*rxDa + 2.0 * DaxQbr)*v31 - rxDa*rdQbr*v32);
970 >        Tb += pref * ((2.0*cross(DadQb, rhat) - 2.0*DaxQbr)*v31
971 >                      - 2.0*rdDa*rxQbr*v32);
972        }
973      }
710    
711    if (i_is_Dipole) {
974  
975 <      if (j_is_Charge) {
976 <        // variables used by all the methods
977 <        pref =  *(idat.electroMult) * pre12_ * q_j * mu_i;
978 <        preSw =  *(idat.sw)  * pref;
975 >    if (a_is_Quadrupole) {
976 >      if (b_is_Charge) {
977 >        pref = pre14_ * *(idat.electroMult);
978 >        U  += C_b * pref * (v21 * trQa + v22 * rdQar);
979 >        F  += C_b * pref * (trQa * rhat + 2.0 * Qar) * v23;
980 >        F  += C_b * pref * rdQar * rhat * v24;
981 >        Ta += C_b * pref * 2.0 * rxQar * v22;
982  
983 <        if (summationMethod_ == esm_REACTION_FIELD) {
983 >        if (b_is_Fluctuating) dUdCb += pref * (v21 * trQa + v22 * rdQar);
984 >      }
985 >      if (b_is_Dipole) {
986 >        pref = pre24_ * *(idat.electroMult);
987 >        DbdQa = D_b * Q_a;
988 >        DbdQar = dot(D_b, Qar);
989 >        DbxQar = cross(D_b, Qar);
990  
991 <          ri2 = riji * riji;
992 <          ri3 = ri2 * riji;
993 <
994 <          vterm = pref * ct_i * ( ri2 - preRF2_ *  *(idat.rij)  );
995 <          *(idat.vpair) += vterm;
996 <          epot +=  *(idat.sw)  * vterm;
997 <          
998 <          dVdr += preSw * (ri3 * (uz_i - 3.0 * ct_i * rhat) - preRF2_ * uz_i);
728 <          
729 <          duduz_i += preSw * rhat * (ri2 - preRF2_ *  *(idat.rij) );
730 <            
731 <        } else {
732 <          
733 <          // determine inverse r if we are using split dipoles
734 <          if (i_is_SplitDipole) {
735 <            BigR = sqrt( *(idat.r2) + 0.25 * d_i * d_i);
736 <            ri = 1.0 / BigR;
737 <            scale =  *(idat.rij)  * ri;
738 <          } else {
739 <            ri = riji;
740 <            scale = 1.0;
741 <          }
742 <          
743 <          sc2 = scale * scale;
744 <            
745 <          if (screeningMethod_ == DAMPED) {
746 <            // assemble the damping variables
747 <            res = erfcSpline_->getValueAndDerivativeAt( *(idat.rij) );
748 <            erfcVal = res.first;
749 <            derfcVal = res.second;
750 <            c1 = erfcVal * ri;
751 <            c2 = (-derfcVal + c1) * ri;
752 <            c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * ri;
753 <          } else {
754 <            c1 = ri;
755 <            c2 = c1 * ri;
756 <            c3 = 3.0 * c2 * ri;
757 <          }
758 <          
759 <          c2ri = c2 * ri;
760 <              
761 <          // calculate the potential
762 <          pot_term = c2 * scale;
763 <          vterm = pref * ct_i * pot_term;
764 <          *(idat.vpair) += vterm;
765 <          epot +=  *(idat.sw)  * vterm;
766 <
767 <          // calculate derivatives for the forces and torques
768 <          dVdr += preSw * (uz_i * c2ri - ct_i * rhat * sc2 * c3);
769 <          duduz_i += preSw * pot_term * rhat;
770 <        }
991 >        U  += pref * ((trQa*rdDb + 2.0*DbdQar)*v31 + rdDb*rdQar*v32);
992 >        F  += pref * (trQa*D_b + 2.0*DbdQa) * v33;
993 >        F  += pref * (trQa*rdDb*rhat + 2.0*DbdQar*rhat + D_b*rdQar
994 >                      + 2.0*rdDb*rQa)*v34;
995 >        F  += pref * (rdDb * rdQar * rhat * v35);
996 >        Ta += pref * ((-2.0*cross(DbdQa, rhat) + 2.0*DbxQar)*v31
997 >                      + 2.0*rdDb*rxQar*v32);
998 >        Tb += pref * ((trQa*rxDb + 2.0 * DbxQar)*v31 + rxDb*rdQar*v32);
999        }
1000 +      if (b_is_Quadrupole) {
1001 +        pref = pre44_ * *(idat.electroMult);
1002 +        QaQb = Q_a * Q_b;
1003 +        trQaQb = QaQb.trace();
1004 +        rQaQb = rhat * QaQb;
1005 +        QaQbr = QaQb * rhat;        
1006 +        QaxQb = cross(Q_a, Q_b);
1007  
1008 <      if (j_is_Dipole) {
1009 <        // variables used by all methods
1010 <        ct_ij = dot(uz_i, uz_j);
1008 >        U  += pref * (trQa * trQb + 2.0*trQaQb) * v41;
1009 >        U  += pref * (trQa*rdQbr + trQb*rdQar  + 4.0*dot(rQa, Qbr)) * v42;
1010 >        U  += pref * (rdQar * rdQbr) * v43;
1011  
1012 <        pref =  *(idat.electroMult) * pre22_ * mu_i * mu_j;
1013 <        preSw =  *(idat.sw)  * pref;
1012 >        F  += pref * (trQa*trQb*rhat + 2.0*trQaQb*rhat)*v44;
1013 >        F  += pref * (2.0*trQb*rQa + 2.0*trQa*rQb)*v44;
1014 >        F  += pref * (4.0* QaQb * rhat + 4.0 * rhat * QaQb)*v44;
1015 >        F  += pref * (trQa*rdQbr*rhat + trQb*rdQar*rhat
1016 >                      + 4.0*dot(rQa, Qbr)*rhat)*v45;
1017 >        F  += pref * (2.0*rQa*rdQbr + 2.0*rdQar*rQb)*v45;
1018 >        F  += pref * (rdQar*rdQbr*rhat) * v46;
1019  
1020 <        if (summationMethod_ == esm_REACTION_FIELD) {
1021 <          ri2 = riji * riji;
1022 <          ri3 = ri2 * riji;
1023 <          ri4 = ri2 * ri2;
1020 >        Ta += pref * (-4.0 * QaxQb  * v41);
1021 >        Ta += pref * (-2.0*trQb*cross(rQa, rhat)
1022 >                      + 4.0*cross(rhat, QaQbr)
1023 >                      - 4.0*cross(rQa, Qbr)) * v42;
1024 >        Ta += pref * 2.0 * cross(rhat,Qar) * rdQbr * v43;
1025  
1026 <          vterm = pref * ( ri3 * (ct_ij - 3.0 * ct_i * ct_j) -
1027 <                           preRF2_ * ct_ij );
1028 <          *(idat.vpair) += vterm;
1029 <          epot +=  *(idat.sw)  * vterm;
1030 <            
790 <          a1 = 5.0 * ct_i * ct_j - ct_ij;
791 <            
792 <          dVdr += preSw * 3.0 * ri4 * (a1 * rhat - ct_i * uz_j - ct_j * uz_i);
1026 >        Tb += pref * (4.0 * QaxQb * v41);
1027 >        Tb += pref * (-2.0*trQa*cross(rQb, rhat)
1028 >                      - 4.0*cross(rQaQb, rhat)
1029 >                      + 4.0*cross(rQa, Qbr))*v42;
1030 >        Tb += pref * 2.0 * cross(rhat,Qbr) * rdQar * v43;        
1031  
794          duduz_i += preSw * (ri3 * (uz_j - 3.0 * ct_j * rhat) - preRF2_*uz_j);
795          duduz_j += preSw * (ri3 * (uz_i - 3.0 * ct_i * rhat) - preRF2_*uz_i);
796
797        } else {
798          
799          if (i_is_SplitDipole) {
800            if (j_is_SplitDipole) {
801              BigR = sqrt( *(idat.r2) + 0.25 * d_i * d_i + 0.25 * d_j * d_j);
802            } else {
803              BigR = sqrt( *(idat.r2) + 0.25 * d_i * d_i);
804            }
805            ri = 1.0 / BigR;
806            scale =  *(idat.rij)  * ri;
807          } else {
808            if (j_is_SplitDipole) {
809              BigR = sqrt( *(idat.r2) + 0.25 * d_j * d_j);
810              ri = 1.0 / BigR;
811              scale =  *(idat.rij)  * ri;
812            } else {
813              ri = riji;
814              scale = 1.0;
815            }
816          }
817          if (screeningMethod_ == DAMPED) {
818            // assemble damping variables
819            res = erfcSpline_->getValueAndDerivativeAt( *(idat.rij) );
820            erfcVal = res.first;
821            derfcVal = res.second;
822            c1 = erfcVal * ri;
823            c2 = (-derfcVal + c1) * ri;
824            c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * ri;
825            c4 = -4.0 * derfcVal * alpha4_ + 5.0 * c3 * ri * ri;
826          } else {
827            c1 = ri;
828            c2 = c1 * ri;
829            c3 = 3.0 * c2 * ri;
830            c4 = 5.0 * c3 * ri * ri;
831          }
832
833          // precompute variables for convenience
834          sc2 = scale * scale;
835          cti3 = ct_i * sc2 * c3;
836          ctj3 = ct_j * sc2 * c3;
837          ctidotj = ct_i * ct_j * sc2;
838          preSwSc = preSw * scale;
839          c2ri = c2 * ri;
840          c3ri = c3 * ri;
841          c4rij = c4 *  *(idat.rij) ;
842
843          // calculate the potential
844          pot_term = (ct_ij * c2ri - ctidotj * c3);
845          vterm = pref * pot_term;
846          *(idat.vpair) += vterm;
847          epot +=  *(idat.sw)  * vterm;
848
849          // calculate derivatives for the forces and torques
850          dVdr += preSwSc * ( ctidotj * rhat * c4rij  -
851                              (ct_i*uz_j + ct_j*uz_i + ct_ij*rhat) * c3ri);
852          
853          duduz_i += preSw * (uz_j * c2ri - ctj3 * rhat);
854          duduz_j += preSw * (uz_i * c2ri - cti3 * rhat);
855        }
1032        }
1033      }
1034  
1035 <    if (i_is_Quadrupole) {
1036 <      if (j_is_Charge) {
1037 <        // precompute some necessary variables
1038 <        cx2 = cx_i * cx_i;
863 <        cy2 = cy_i * cy_i;
864 <        cz2 = cz_i * cz_i;
1035 >    if (idat.doElectricField) {
1036 >      *(idat.eField1) += Ea * *(idat.electroMult);
1037 >      *(idat.eField2) += Eb * *(idat.electroMult);
1038 >    }
1039  
1040 <        pref =  *(idat.electroMult) * pre14_ * q_j * one_third_;
1040 >    if (a_is_Fluctuating) *(idat.dVdFQ1) += dUdCa * *(idat.sw);
1041 >    if (b_is_Fluctuating) *(idat.dVdFQ2) += dUdCb * *(idat.sw);
1042  
1043 <        if (screeningMethod_ == DAMPED) {
1044 <          // assemble the damping variables
1045 <          res = erfcSpline_->getValueAndDerivativeAt( *(idat.rij) );
1046 <          erfcVal = res.first;
1047 <          derfcVal = res.second;
1048 <          c1 = erfcVal * riji;
1049 <          c2 = (-derfcVal + c1) * riji;
1050 <          c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * riji;
876 <          c4 = -4.0 * derfcVal * alpha4_ + 5.0 * c3 * riji * riji;
877 <        } else {
878 <          c1 = riji;
879 <          c2 = c1 * riji;
880 <          c3 = 3.0 * c2 * riji;
881 <          c4 = 5.0 * c3 * riji * riji;
882 <        }
883 <          
884 <        // precompute some variables for convenience
885 <        preSw =  *(idat.sw)  * pref;
886 <        c2ri = c2 * riji;
887 <        c3ri = c3 * riji;
888 <        c4rij = c4 *  *(idat.rij) ;
889 <        rhatdot2 = 2.0 * rhat * c3;
890 <        rhatc4 = rhat * c4rij;
1043 >    if (!idat.excluded) {
1044 >      
1045 >      *(idat.vpair) += U;
1046 >      (*(idat.pot))[ELECTROSTATIC_FAMILY] += U * *(idat.sw);
1047 >      *(idat.f1) += F * *(idat.sw);
1048 >      
1049 >      if (a_is_Dipole || a_is_Quadrupole)
1050 >        *(idat.t1) += Ta * *(idat.sw);
1051  
1052 <        // calculate the potential
1053 <        pot_term = ( qxx_i * (cx2 * c3 - c2ri) +
1054 <                     qyy_i * (cy2 * c3 - c2ri) +
1055 <                     qzz_i * (cz2 * c3 - c2ri) );
896 <        
897 <        vterm = pref * pot_term;
898 <        *(idat.vpair) += vterm;
899 <        epot +=  *(idat.sw)  * vterm;
1052 >      if (b_is_Dipole || b_is_Quadrupole)
1053 >        *(idat.t2) += Tb * *(idat.sw);
1054 >      
1055 >    } else {
1056  
1057 <        // calculate the derivatives for the forces and torques
1057 >      // only accumulate the forces and torques resulting from the
1058 >      // indirect reaction field terms.
1059  
1060 <        dVdr += -preSw * (qxx_i* (cx2*rhatc4 - (2.0*cx_i*ux_i + rhat)*c3ri) +
1061 <                          qyy_i* (cy2*rhatc4 - (2.0*cy_i*uy_i + rhat)*c3ri) +
1062 <                          qzz_i* (cz2*rhatc4 - (2.0*cz_i*uz_i + rhat)*c3ri));
1063 <
1064 <        dudux_i += preSw * qxx_i * cx_i *  rhatdot2;
1065 <        duduy_i += preSw * qyy_i * cy_i *  rhatdot2;
1066 <        duduz_i += preSw * qzz_i * cz_i *  rhatdot2;
1067 <      }
1060 >      *(idat.vpair) += indirect_Pot;      
1061 >      (*(idat.excludedPot))[ELECTROSTATIC_FAMILY] +=  excluded_Pot;
1062 >      (*(idat.pot))[ELECTROSTATIC_FAMILY] += *(idat.sw) * indirect_Pot;
1063 >      *(idat.f1) += *(idat.sw) * indirect_F;
1064 >      
1065 >      if (a_is_Dipole || a_is_Quadrupole)
1066 >        *(idat.t1) += *(idat.sw) * indirect_Ta;
1067 >            
1068 >      if (b_is_Dipole || b_is_Quadrupole)
1069 >        *(idat.t2) += *(idat.sw) * indirect_Tb;
1070      }
912
913    (*(idat.pot))[ELECTROSTATIC_FAMILY] += epot;
914    *(idat.f1) += dVdr;
915
916    if (i_is_Dipole || i_is_Quadrupole)
917      *(idat.t1) -= cross(uz_i, duduz_i);
918    if (i_is_Quadrupole) {
919      *(idat.t1) -= cross(ux_i, dudux_i);
920      *(idat.t1) -= cross(uy_i, duduy_i);
921    }
922    
923    if (j_is_Dipole || j_is_Quadrupole)
924      *(idat.t2) -= cross(uz_j, duduz_j);
925    if (j_is_Quadrupole) {
926      *(idat.t2) -= cross(uz_j, dudux_j);
927      *(idat.t2) -= cross(uz_j, duduy_j);
928    }
929
1071      return;
1072    }  
1073 +    
1074 +  void Electrostatic::calcSelfCorrection(SelfData &sdat) {
1075  
933  void Electrostatic::calcSkipCorrection(InteractionData &idat) {
934
1076      if (!initialized_) initialize();
1077 +
1078 +    ElectrostaticAtomData data = ElectrostaticMap[sdat.atype];
1079      
937    ElectrostaticAtomData data1 = ElectrostaticMap[idat.atypes.first];
938    ElectrostaticAtomData data2 = ElectrostaticMap[idat.atypes.second];
939    
1080      // logicals
1081 <
1082 <    bool i_is_Charge = data1.is_Charge;
1083 <    bool i_is_Dipole = data1.is_Dipole;
1084 <
1085 <    bool j_is_Charge = data2.is_Charge;
946 <    bool j_is_Dipole = data2.is_Dipole;
947 <
948 <    RealType q_i, q_j;
1081 >    bool i_is_Charge = data.is_Charge;
1082 >    bool i_is_Dipole = data.is_Dipole;
1083 >    bool i_is_Fluctuating = data.is_Fluctuating;
1084 >    RealType C_a = data.fixedCharge;  
1085 >    RealType self, preVal, DadDa;
1086      
1087 <    // The skippedCharge computation is needed by the real-space cutoff methods
1088 <    // (i.e. shifted force and shifted potential)
1089 <
1090 <    if (i_is_Charge) {
1091 <      q_i = data1.charge;
1092 <      *(idat.skippedCharge2) += q_i;
1087 >    if (i_is_Fluctuating) {
1088 >      C_a += *(sdat.flucQ);
1089 >      // dVdFQ is really a force, so this is negative the derivative
1090 >      *(sdat.dVdFQ) -=  *(sdat.flucQ) * data.hardness + data.electronegativity;
1091 >      (*(sdat.excludedPot))[ELECTROSTATIC_FAMILY] += (*sdat.flucQ) *
1092 >        (*(sdat.flucQ) * data.hardness * 0.5 + data.electronegativity);
1093      }
1094  
1095 <    if (j_is_Charge) {
1096 <      q_j = data2.charge;
960 <      *(idat.skippedCharge1) += q_j;
961 <    }
962 <
963 <    // the rest of this function should only be necessary for reaction field.
964 <
965 <    if (summationMethod_ == esm_REACTION_FIELD) {
966 <      RealType riji, ri2, ri3;
967 <      RealType mu_i, ct_i;
968 <      RealType mu_j, ct_j;
969 <      RealType preVal, rfVal, vterm, dudr, pref, myPot(0.0);
970 <      Vector3d dVdr, uz_i, uz_j, duduz_i, duduz_j, rhat;
971 <
972 <      // some variables we'll need independent of electrostatic type:
1095 >    switch (summationMethod_) {
1096 >    case esm_REACTION_FIELD:
1097        
974      riji = 1.0 /  *(idat.rij) ;
975      rhat =  *(idat.d)  * riji;
976
977      if (i_is_Dipole) {
978        mu_i = data1.dipole_moment;
979        uz_i = idat.eFrame1->getColumn(2);      
980        ct_i = dot(uz_i, rhat);
981        duduz_i = V3Zero;
982      }
983            
984      if (j_is_Dipole) {
985        mu_j = data2.dipole_moment;
986        uz_j = idat.eFrame2->getColumn(2);      
987        ct_j = dot(uz_j, rhat);
988        duduz_j = V3Zero;
989      }
990    
1098        if (i_is_Charge) {
1099 <        if (j_is_Charge) {
1100 <          preVal =  *(idat.electroMult) * pre11_ * q_i * q_j;
1101 <          rfVal = preRF_ *  *(idat.rij)  *  *(idat.rij) ;
1102 <          vterm = preVal * rfVal;
1103 <          myPot +=  *(idat.sw)  * vterm;        
997 <          dudr  =  *(idat.sw)  * preVal * 2.0 * rfVal * riji;        
998 <          dVdr += dudr * rhat;
999 <        }
1000 <        
1001 <        if (j_is_Dipole) {
1002 <          ri2 = riji * riji;
1003 <          ri3 = ri2 * riji;        
1004 <          pref =  *(idat.electroMult) * pre12_ * q_i * mu_j;
1005 <          vterm = - pref * ct_j * ( ri2 - preRF2_ *  *(idat.rij)  );
1006 <          myPot +=  *(idat.sw)  * vterm;        
1007 <          dVdr += - *(idat.sw)  * pref * ( ri3 * ( uz_j - 3.0 * ct_j * rhat) - preRF2_ * uz_j);
1008 <          duduz_j += - *(idat.sw)  * pref * rhat * (ri2 - preRF2_ *  *(idat.rij) );
1009 <        }
1099 >        // Self potential [see Wang and Hermans, "Reaction Field
1100 >        // Molecular Dynamics Simulation with Friedman’s Image Charge
1101 >        // Method," J. Phys. Chem. 99, 12001-12007 (1995).]
1102 >        preVal = pre11_ * preRF_ * C_a * C_a;
1103 >        (*(sdat.pot))[ELECTROSTATIC_FAMILY] -= 0.5 * preVal / cutoffRadius_;
1104        }
1105 +
1106        if (i_is_Dipole) {
1107 <        if (j_is_Charge) {
1108 <          ri2 = riji * riji;
1014 <          ri3 = ri2 * riji;        
1015 <          pref =  *(idat.electroMult) * pre12_ * q_j * mu_i;
1016 <          vterm = - pref * ct_i * ( ri2 - preRF2_ *  *(idat.rij)  );
1017 <          myPot +=  *(idat.sw)  * vterm;        
1018 <          dVdr +=  *(idat.sw)  * pref * ( ri3 * ( uz_i - 3.0 * ct_i * rhat) - preRF2_ * uz_i);      
1019 <          duduz_i +=  *(idat.sw)  * pref * rhat * (ri2 - preRF2_ *  *(idat.rij));
1020 <        }
1107 >        DadDa = data.dipole.lengthSquare();
1108 >        (*(sdat.pot))[ELECTROSTATIC_FAMILY] -= pre22_ * preRF_ * DadDa;
1109        }
1110        
1111 <      // accumulate the forces and torques resulting from the self term
1024 <      (*(idat.pot))[ELECTROSTATIC_FAMILY] += myPot;
1025 <      *(idat.f1) += dVdr;
1111 >      break;
1112        
1113 <      if (i_is_Dipole)
1114 <        *(idat.t1) -= cross(uz_i, duduz_i);
1029 <      if (j_is_Dipole)
1030 <        *(idat.t2) -= cross(uz_j, duduz_j);
1031 <    }
1032 <  }
1033 <    
1034 <  void Electrostatic::calcSelfCorrection(SelfData &sdat) {
1035 <    RealType mu1, preVal, chg1, self;
1036 <    
1037 <    if (!initialized_) initialize();
1038 <
1039 <    ElectrostaticAtomData data = ElectrostaticMap[sdat.atype];
1040 <  
1041 <    // logicals
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 <        (*(sdat.pot))[ELECTROSTATIC_FAMILY] -= 0.5 * preVal;
1050 <        
1051 <        // The self-correction term adds into the reaction field vector
1052 <        Vector3d uz_i = sdat.eFrame->getColumn(2);
1053 <        Vector3d ei = preVal * uz_i;
1054 <
1055 <        // This looks very wrong.  A vector crossed with itself is zero.
1056 <        *(sdat.t) -= cross(uz_i, ei);
1057 <      }
1058 <    } else if (summationMethod_ == esm_SHIFTED_FORCE || summationMethod_ == esm_SHIFTED_POTENTIAL) {
1113 >    case esm_SHIFTED_FORCE:
1114 >    case esm_SHIFTED_POTENTIAL:
1115        if (i_is_Charge) {        
1116 <        chg1 = data.charge;
1061 <        if (screeningMethod_ == DAMPED) {
1062 <          self = - 0.5 * (c1c_ + alphaPi_) * chg1 * (chg1 + *(sdat.skippedCharge)) * pre11_;
1063 <        } else {        
1064 <          self = - 0.5 * rcuti_ * chg1 * (chg1 +  *(sdat.skippedCharge)) * pre11_;
1065 <        }
1116 >        self = -0.5 * selfMult_ * C_a * (C_a + *(sdat.skippedCharge)) * pre11_;
1117          (*(sdat.pot))[ELECTROSTATIC_FAMILY] += self;
1118        }
1119 +      break;
1120 +    default:
1121 +      break;
1122      }
1123    }
1124 <
1124 >  
1125    RealType Electrostatic::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) {
1126      // This seems to work moderately well as a default.  There's no
1127      // inherent scale for 1/r interactions that we can standardize.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines