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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines