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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines