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 1535 by gezelter, Fri Dec 31 18:31:56 2010 UTC vs.
trunk/src/nonbonded/Electrostatic.cpp (file contents), Revision 1938 by gezelter, Thu Oct 31 15:32:17 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 + #ifdef IS_MPI
44 + #include <mpi.h>
45 + #endif
46 +
47   #include <stdio.h>
48   #include <string.h>
49  
50   #include <cmath>
51 + #include <numeric>
52   #include "nonbonded/Electrostatic.hpp"
53   #include "utils/simError.h"
54   #include "types/NonBondedInteractionType.hpp"
55 < #include "types/DirectionalAtomType.hpp"
55 > #include "types/FixedChargeAdapter.hpp"
56 > #include "types/FluctuatingChargeAdapter.hpp"
57 > #include "types/MultipoleAdapter.hpp"
58   #include "io/Globals.hpp"
59 + #include "nonbonded/SlaterIntegrals.hpp"
60 + #include "utils/PhysicalConstants.hpp"
61 + #include "math/erfc.hpp"
62 + #include "math/SquareMatrix.hpp"
63 + #include "primitives/Molecule.hpp"
64  
65   namespace OpenMD {
66    
67    Electrostatic::Electrostatic(): name_("Electrostatic"), initialized_(false),
68 <                                  forceField_(NULL) {}
68 >                                  forceField_(NULL), info_(NULL),
69 >                                  haveCutoffRadius_(false),
70 >                                  haveDampingAlpha_(false),
71 >                                  haveDielectric_(false),
72 >                                  haveElectroSplines_(false)
73 >  {}
74    
75    void Electrostatic::initialize() {
76 +    
77 +    Globals* simParams_ = info_->getSimParams();
78  
59    Globals* simParams_;
60
79      summationMap_["HARD"]               = esm_HARD;
80 +    summationMap_["NONE"]               = esm_HARD;
81      summationMap_["SWITCHING_FUNCTION"] = esm_SWITCHING_FUNCTION;
82      summationMap_["SHIFTED_POTENTIAL"]  = esm_SHIFTED_POTENTIAL;
83      summationMap_["SHIFTED_FORCE"]      = esm_SHIFTED_FORCE;    
84 +    summationMap_["TAYLOR_SHIFTED"]     = esm_TAYLOR_SHIFTED;    
85      summationMap_["REACTION_FIELD"]     = esm_REACTION_FIELD;    
86      summationMap_["EWALD_FULL"]         = esm_EWALD_FULL;        
87      summationMap_["EWALD_PME"]          = esm_EWALD_PME;        
# Line 76 | Line 96 | namespace OpenMD {
96      // Charge-Dipole, assuming charges are measured in electrons, and
97      // dipoles are measured in debyes
98      pre12_ = 69.13373;
99 <    // Dipole-Dipole, assuming dipoles are measured in debyes
99 >    // Dipole-Dipole, assuming dipoles are measured in Debye
100      pre22_ = 14.39325;
101      // Charge-Quadrupole, assuming charges are measured in electrons, and
102      // quadrupoles are measured in 10^-26 esu cm^2
103 <    // This unit is also known affectionately as an esu centi-barn.
103 >    // This unit is also known affectionately as an esu centibarn.
104      pre14_ = 69.13373;
105 <    
105 >    // Dipole-Quadrupole, assuming dipoles are measured in debyes and
106 >    // quadrupoles in esu centibarns:
107 >    pre24_ = 14.39325;
108 >    // Quadrupole-Quadrupole, assuming esu centibarns:
109 >    pre44_ = 14.39325;
110 >
111      // conversions for the simulation box dipole moment
112      chargeToC_ = 1.60217733e-19;
113      angstromToM_ = 1.0e-10;
114      debyeToCm_ = 3.33564095198e-30;
115      
116 <    // number of points for electrostatic splines
116 >    // Default number of points for electrostatic splines
117      np_ = 100;
118      
119      // variables to handle different summation methods for long-range
# Line 96 | Line 121 | namespace OpenMD {
121      summationMethod_ = esm_HARD;    
122      screeningMethod_ = UNDAMPED;
123      dielectric_ = 1.0;
99    one_third_ = 1.0 / 3.0;
100    haveCutoffRadius_ = false;
101    haveDampingAlpha_ = false;
102    haveDielectric_ = false;  
103    haveElectroSpline_ = false;
124    
125      // check the summation method:
126      if (simParams_->haveElectrostaticSummationMethod()) {
# Line 113 | Line 133 | namespace OpenMD {
133        } else {
134          // throw error
135          sprintf( painCave.errMsg,
136 <                 "SimInfo error: Unknown electrostaticSummationMethod.\n"
136 >                 "Electrostatic::initialize: Unknown electrostaticSummationMethod.\n"
137                   "\t(Input file specified %s .)\n"
138 <                 "\telectrostaticSummationMethod must be one of: \"none\",\n"
139 <                 "\t\"shifted_potential\", \"shifted_force\", or \n"
140 <                 "\t\"reaction_field\".\n", myMethod.c_str() );
138 >                 "\telectrostaticSummationMethod must be one of: \"hard\",\n"
139 >                 "\t\"shifted_potential\", \"shifted_force\",\n"
140 >                 "\t\"taylor_shifted\", or \"reaction_field\".\n",
141 >                 myMethod.c_str() );
142          painCave.isFatal = 1;
143          simError();
144        }
# Line 176 | Line 197 | namespace OpenMD {
197        simError();
198      }
199            
200 <    if (screeningMethod_ == DAMPED) {      
200 >    if (screeningMethod_ == DAMPED || summationMethod_ == esm_EWALD_FULL) {
201        if (!simParams_->haveDampingAlpha()) {
202          // first set a cutoff dependent alpha value
203          // we assume alpha depends linearly with rcut from 0 to 20.5 ang
204          dampingAlpha_ = 0.425 - cutoffRadius_* 0.02;
205 <        if (dampingAlpha_ < 0.0) dampingAlpha_ = 0.0;
185 <        
205 >        if (dampingAlpha_ < 0.0) dampingAlpha_ = 0.0;        
206          // throw warning
207          sprintf( painCave.errMsg,
208 <                 "Electrostatic::initialize: dampingAlpha was not specified in the input file.\n"
209 <                 "\tA default value of %f (1/ang) will be used for the cutoff of\n\t%f (ang).\n",
208 >                 "Electrostatic::initialize: dampingAlpha was not specified in the\n"
209 >                 "\tinput file.  A default value of %f (1/ang) will be used for the\n"
210 >                 "\tcutoff of %f (ang).\n",
211                   dampingAlpha_, cutoffRadius_);
212          painCave.severity = OPENMD_INFO;
213          painCave.isFatal = 0;
# Line 197 | Line 218 | namespace OpenMD {
218        haveDampingAlpha_ = true;
219      }
220  
200    // find all of the Electrostatic atom Types:
201    ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes();
202    ForceField::AtomTypeContainer::MapTypeIterator i;
203    AtomType* at;
204    
205    for (at = atomTypes->beginType(i); at != NULL;
206         at = atomTypes->nextType(i)) {
207      
208      if (at->isElectrostatic())
209        addType(at);
210    }
211    
221  
222 <    cutoffRadius2_ = cutoffRadius_ * cutoffRadius_;
223 <    rcuti_ = 1.0 / cutoffRadius_;
224 <    rcuti2_ = rcuti_ * rcuti_;
225 <    rcuti3_ = rcuti2_ * rcuti_;
226 <    rcuti4_ = rcuti2_ * rcuti2_;
222 >    Etypes.clear();
223 >    Etids.clear();
224 >    FQtypes.clear();
225 >    FQtids.clear();
226 >    ElectrostaticMap.clear();
227 >    Jij.clear();
228 >    nElectro_ = 0;
229 >    nFlucq_ = 0;
230  
231 <    if (screeningMethod_ == DAMPED) {
232 <      
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_;
231 >    Etids.resize( forceField_->getNAtomType(), -1);
232 >    FQtids.resize( forceField_->getNAtomType(), -1);
233  
234 <      c1c_ = erfc(dampingAlpha_ * cutoffRadius_) * rcuti_;
235 <      c2c_ = alphaPi_ * constEXP_ * rcuti_ + c1c_ * rcuti_;
236 <      c3c_ = 2.0 * alphaPi_ * alpha2_ + 3.0 * c2c_ * rcuti_;
237 <      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_;
234 >    set<AtomType*>::iterator at;
235 >    for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {    
236 >      if ((*at)->isElectrostatic()) nElectro_++;
237 >      if ((*at)->isFluctuatingCharge()) nFlucq_++;
238      }
239 <  
239 >    
240 >    Jij.resize(nFlucq_);
241 >
242 >    for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {
243 >      if ((*at)->isElectrostatic()) addType(*at);
244 >    }  
245 >    
246      if (summationMethod_ == esm_REACTION_FIELD) {
247        preRF_ = (dielectric_ - 1.0) /
248 <        ((2.0 * dielectric_ + 1.0) * cutoffRadius2_ * cutoffRadius_);
248 <      preRF2_ = 2.0 * preRF_;
248 >        ((2.0 * dielectric_ + 1.0) * pow(cutoffRadius_,3) );
249      }
250      
251 <    RealType dx = cutoffRadius_ / RealType(np_ - 1);
252 <    RealType rval;
253 <    vector<RealType> rvals;
254 <    vector<RealType> yvals;
255 <    for (int i = 0; i < np_; i++) {
256 <      rval = RealType(i) * dx;
257 <      rvals.push_back(rval);
258 <      yvals.push_back(erfc(dampingAlpha_ * rval));
251 >    RealType b0c, b1c, b2c, b3c, b4c, b5c;
252 >    RealType db0c_1, db0c_2, db0c_3, db0c_4, db0c_5;
253 >    RealType a2, expTerm, invArootPi;
254 >    
255 >    RealType r = cutoffRadius_;
256 >    RealType r2 = r * r;
257 >    RealType ric = 1.0 / r;
258 >    RealType ric2 = ric * ric;
259 >
260 >    if (screeningMethod_ == DAMPED) {      
261 >      a2 = dampingAlpha_ * dampingAlpha_;
262 >      invArootPi = 1.0 / (dampingAlpha_ * sqrt(M_PI));    
263 >      expTerm = exp(-a2 * r2);
264 >      // values of Smith's B_l functions at the cutoff radius:
265 >      b0c = erfc(dampingAlpha_ * r) / r;
266 >      b1c = (      b0c     + 2.0*a2     * expTerm * invArootPi) / r2;
267 >      b2c = (3.0 * b1c + pow(2.0*a2, 2) * expTerm * invArootPi) / r2;
268 >      b3c = (5.0 * b2c + pow(2.0*a2, 3) * expTerm * invArootPi) / r2;
269 >      b4c = (7.0 * b3c + pow(2.0*a2, 4) * expTerm * invArootPi) / r2;
270 >      b5c = (9.0 * b4c + pow(2.0*a2, 5) * expTerm * invArootPi) / r2;
271 >      // Half the Smith self piece:
272 >      selfMult1_ = - a2 * invArootPi;
273 >      selfMult2_ = - 2.0 * a2 * a2 * invArootPi / 3.0;
274 >      selfMult4_ = - 4.0 * a2 * a2 * a2 * invArootPi / 5.0;
275 >    } else {
276 >      a2 = 0.0;
277 >      b0c = 1.0 / r;
278 >      b1c = (      b0c) / r2;
279 >      b2c = (3.0 * b1c) / r2;
280 >      b3c = (5.0 * b2c) / r2;
281 >      b4c = (7.0 * b3c) / r2;
282 >      b5c = (9.0 * b4c) / r2;
283 >      selfMult1_ = 0.0;
284 >      selfMult2_ = 0.0;
285 >      selfMult4_ = 0.0;
286      }
260    erfcSpline_ = new CubicSpline();
261    erfcSpline_->addPoints(rvals, yvals);
262    haveElectroSpline_ = true;
287  
288 <    initialized_ = true;
289 <  }
290 <      
291 <  void Electrostatic::addType(AtomType* atomType){
288 >    // higher derivatives of B_0 at the cutoff radius:
289 >    db0c_1 = -r * b1c;
290 >    db0c_2 =     -b1c + r2 * b2c;
291 >    db0c_3 =          3.0*r*b2c  - r2*r*b3c;
292 >    db0c_4 =          3.0*b2c  - 6.0*r2*b3c     + r2*r2*b4c;
293 >    db0c_5 =                    -15.0*r*b3c + 10.0*r2*r*b4c - r2*r2*r*b5c;  
294  
295 <    ElectrostaticAtomData electrostaticAtomData;
296 <    electrostaticAtomData.is_Charge = false;
297 <    electrostaticAtomData.is_Dipole = false;
298 <    electrostaticAtomData.is_SplitDipole = false;
299 <    electrostaticAtomData.is_Quadrupole = false;
295 >    if (summationMethod_ != esm_EWALD_FULL) {
296 >      selfMult1_ -= b0c;
297 >      selfMult2_ += (db0c_2 + 2.0*db0c_1*ric) /  3.0;
298 >      selfMult4_ -= (db0c_4 + 4.0*db0c_3*ric) / 15.0;
299 >    }
300  
301 <    if (atomType->isCharge()) {
302 <      GenericData* data = atomType->getPropertyByName("Charge");
301 >    // working variables for the splines:
302 >    RealType ri, ri2;
303 >    RealType b0, b1, b2, b3, b4, b5;
304 >    RealType db0_1, db0_2, db0_3, db0_4, db0_5;
305 >    RealType f, fc, f0;
306 >    RealType g, gc, g0, g1, g2, g3, g4;
307 >    RealType h, hc, h1, h2, h3, h4;
308 >    RealType s, sc, s2, s3, s4;
309 >    RealType t, tc, t3, t4;
310 >    RealType u, uc, u4;
311  
312 <      if (data == NULL) {
313 <        sprintf( painCave.errMsg, "Electrostatic::addType could not find "
280 <                 "Charge\n"
281 <                 "\tparameters for atomType %s.\n",
282 <                 atomType->getName().c_str());
283 <        painCave.severity = OPENMD_ERROR;
284 <        painCave.isFatal = 1;
285 <        simError();                  
286 <      }
287 <      
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 <    }
312 >    // working variables for Taylor expansion:
313 >    RealType rmRc, rmRc2, rmRc3, rmRc4;
314  
315 <    if (atomType->isDirectional()) {
316 <      DirectionalAtomType* daType = dynamic_cast<DirectionalAtomType*>(atomType);
317 <      
318 <      if (daType->isDipole()) {
319 <        GenericData* data = daType->getPropertyByName("Dipole");
320 <        
321 <        if (data == NULL) {
322 <          sprintf( painCave.errMsg,
323 <                   "Electrostatic::addType could not find Dipole\n"
324 <                   "\tparameters for atomType %s.\n",
325 <                   daType->getName().c_str());
326 <          painCave.severity = OPENMD_ERROR;
327 <          painCave.isFatal = 1;
328 <          simError();                  
329 <        }
330 <      
331 <        DoubleGenericData* doubleData = dynamic_cast<DoubleGenericData*>(data);
332 <        if (doubleData == NULL) {
333 <          sprintf( painCave.errMsg,
334 <                   "Electrostatic::addType could not convert GenericData to "
335 <                   "Dipole Moment\n"
336 <                   "\tfor atom type %s\n", daType->getName().c_str());
337 <          painCave.severity = OPENMD_ERROR;
338 <          painCave.isFatal = 1;
339 <          simError();          
340 <        }
341 <        electrostaticAtomData.is_Dipole = true;
342 <        electrostaticAtomData.dipole_moment = doubleData->getData();
315 >    // Approximate using splines using a maximum of 0.1 Angstroms
316 >    // between points.
317 >    int nptest = int((cutoffRadius_ + 2.0) / 0.1);
318 >    np_ = (np_ > nptest) ? np_ : nptest;
319 >  
320 >    // Add a 2 angstrom safety window to deal with cutoffGroups that
321 >    // have charged atoms longer than the cutoffRadius away from each
322 >    // other.  Splining is almost certainly the best choice here.
323 >    // Direct calls to erfc would be preferrable if it is a very fast
324 >    // implementation.
325 >
326 >    RealType dx = (cutoffRadius_ + 2.0) / RealType(np_);
327 >
328 >    // Storage vectors for the computed functions    
329 >    vector<RealType> rv;
330 >    vector<RealType> v01v;
331 >    vector<RealType> v11v;
332 >    vector<RealType> v21v, v22v;
333 >    vector<RealType> v31v, v32v;
334 >    vector<RealType> v41v, v42v, v43v;
335 >
336 >    for (int i = 1; i < np_ + 1; i++) {
337 >      r = RealType(i) * dx;
338 >      rv.push_back(r);
339 >
340 >      ri = 1.0 / r;
341 >      ri2 = ri * ri;
342 >
343 >      r2 = r * r;
344 >      expTerm = exp(-a2 * r2);
345 >
346 >      // Taylor expansion factors (no need for factorials this way):
347 >      rmRc = r - cutoffRadius_;
348 >      rmRc2 = rmRc  * rmRc / 2.0;
349 >      rmRc3 = rmRc2 * rmRc / 3.0;
350 >      rmRc4 = rmRc3 * rmRc / 4.0;
351 >
352 >      // values of Smith's B_l functions at r:
353 >      if (screeningMethod_ == DAMPED) {            
354 >        b0 = erfc(dampingAlpha_ * r) * ri;
355 >        b1 = (      b0 +     2.0*a2     * expTerm * invArootPi) * ri2;
356 >        b2 = (3.0 * b1 + pow(2.0*a2, 2) * expTerm * invArootPi) * ri2;
357 >        b3 = (5.0 * b2 + pow(2.0*a2, 3) * expTerm * invArootPi) * ri2;
358 >        b4 = (7.0 * b3 + pow(2.0*a2, 4) * expTerm * invArootPi) * ri2;
359 >        b5 = (9.0 * b4 + pow(2.0*a2, 5) * expTerm * invArootPi) * ri2;
360 >      } else {
361 >        b0 = ri;
362 >        b1 = (      b0) * ri2;
363 >        b2 = (3.0 * b1) * ri2;
364 >        b3 = (5.0 * b2) * ri2;
365 >        b4 = (7.0 * b3) * ri2;
366 >        b5 = (9.0 * b4) * ri2;
367        }
368 +                
369 +      // higher derivatives of B_0 at r:
370 +      db0_1 = -r * b1;
371 +      db0_2 =     -b1 + r2 * b2;
372 +      db0_3 =          3.0*r*b2   - r2*r*b3;
373 +      db0_4 =          3.0*b2   - 6.0*r2*b3     + r2*r2*b4;
374 +      db0_5 =                    -15.0*r*b3 + 10.0*r2*r*b4 - r2*r2*r*b5;
375  
376 <      if (daType->isSplitDipole()) {
377 <        GenericData* data = daType->getPropertyByName("SplitDipoleDistance");
378 <        
379 <        if (data == NULL) {
380 <          sprintf(painCave.errMsg,
381 <                  "Electrostatic::addType could not find SplitDipoleDistance\n"
382 <                  "\tparameter for atomType %s.\n",
383 <                  daType->getName().c_str());
384 <          painCave.severity = OPENMD_ERROR;
385 <          painCave.isFatal = 1;
386 <          simError();                  
387 <        }
376 >      f = b0;
377 >      fc = b0c;
378 >      f0 = f - fc - rmRc*db0c_1;
379 >
380 >      g = db0_1;        
381 >      gc = db0c_1;
382 >      g0 = g - gc;
383 >      g1 = g0 - rmRc *db0c_2;
384 >      g2 = g1 - rmRc2*db0c_3;
385 >      g3 = g2 - rmRc3*db0c_4;
386 >      g4 = g3 - rmRc4*db0c_5;
387 >
388 >      h = db0_2;      
389 >      hc = db0c_2;
390 >      h1 = h - hc;
391 >      h2 = h1 - rmRc *db0c_3;
392 >      h3 = h2 - rmRc2*db0c_4;
393 >      h4 = h3 - rmRc3*db0c_5;
394 >
395 >      s = db0_3;      
396 >      sc = db0c_3;
397 >      s2 = s - sc;
398 >      s3 = s2 - rmRc *db0c_4;
399 >      s4 = s3 - rmRc2*db0c_5;
400 >
401 >      t = db0_4;      
402 >      tc = db0c_4;
403 >      t3 = t - tc;
404 >      t4 = t3 - rmRc *db0c_5;
405        
406 <        DoubleGenericData* doubleData = dynamic_cast<DoubleGenericData*>(data);
407 <        if (doubleData == NULL) {
408 <          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 <      }
406 >      u = db0_5;        
407 >      uc = db0c_5;
408 >      u4 = u - uc;
409  
410 <      if (daType->isQuadrupole()) {
411 <        GenericData* data = daType->getPropertyByName("QuadrupoleMoments");
410 >      // in what follows below, the various v functions are used for
411 >      // potentials and torques, while the w functions show up in the
412 >      // forces.
413 >
414 >      switch (summationMethod_) {
415 >      case esm_SHIFTED_FORCE:
416 >                
417 >        v01 = f - fc - rmRc*gc;
418 >        v11 = g - gc - rmRc*hc;
419 >        v21 = g*ri - gc*ric - rmRc*(hc - gc*ric)*ric;
420 >        v22 = h - g*ri - (hc - gc*ric) - rmRc*(sc - (hc - gc*ric)*ric);
421 >        v31 = (h-g*ri)*ri - (hc-gc*ric)*ric - rmRc*(sc-2.0*(hc-gc*ric)*ric)*ric;
422 >        v32 = (s - 3.0*(h-g*ri)*ri) - (sc - 3.0*(hc-gc*ric)*ric)
423 >          - rmRc*(tc - 3.0*(sc-2.0*(hc-gc*ric)*ric)*ric);
424 >        v41 = (h - g*ri)*ri2 - (hc - gc*ric)*ric2
425 >          - rmRc*(sc - 3.0*(hc-gc*ric)*ric)*ric2;
426 >        v42 = (s-3.0*(h-g*ri)*ri)*ri - (sc-3.0*(hc-gc*ric)*ric)*ric
427 >          - rmRc*(tc - (4.0*sc - 9.0*(hc - gc*ric)*ric)*ric)*ric;
428          
429 <        if (data == NULL) {
430 <          sprintf( painCave.errMsg,
431 <                   "Electrostatic::addType could not find QuadrupoleMoments\n"
432 <                   "\tparameter for atomType %s.\n",
433 <                   daType->getName().c_str());
434 <          painCave.severity = OPENMD_ERROR;
435 <          painCave.isFatal = 1;
436 <          simError();                  
437 <        }
429 >        v43 = (t - 3.0*(2.0*s - 5.0*(h - g*ri)*ri)*ri)
430 >          - (tc - 3.0*(2.0*sc - 5.0*(hc - gc*ric)*ric)*ric)
431 >          - rmRc*(uc-3.0*(2.0*tc - (7.0*sc - 15.0*(hc - gc*ric)*ric)*ric)*ric);
432 >
433 >        dv01 = g - gc;
434 >        dv11 = h - hc;
435 >        dv21 = (h - g*ri)*ri - (hc - gc*ric)*ric;
436 >        dv22 = (s - (h - g*ri)*ri) - (sc - (hc - gc*ric)*ric);        
437 >        dv31 = (s - 2.0*(h-g*ri)*ri)*ri - (sc - 2.0*(hc-gc*ric)*ric)*ric;
438 >        dv32 = (t - 3.0*(s-2.0*(h-g*ri)*ri)*ri)
439 >          - (tc - 3.0*(sc-2.0*(hc-gc*ric)*ric)*ric);
440 >        dv41 = (s - 3.0*(h - g*ri)*ri)*ri2 - (sc - 3.0*(hc - gc*ric)*ric)*ric2;
441 >        dv42 = (t - (4.0*s - 9.0*(h-g*ri)*ri)*ri)*ri
442 >          - (tc - (4.0*sc - 9.0*(hc-gc*ric)*ric)*ric)*ric;
443 >        dv43 = (u - 3.0*(2.0*t - (7.0*s - 15.0*(h - g*ri)*ri)*ri)*ri)
444 >          - (uc - 3.0*(2.0*tc - (7.0*sc - 15.0*(hc - gc*ric)*ric)*ric)*ric);
445          
446 <        // 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.
446 >        break;
447  
448 <        Vector3dGenericData* v3dData = dynamic_cast<Vector3dGenericData*>(data);
449 <        if (v3dData == NULL) {
450 <          sprintf( painCave.errMsg,
451 <                   "Electrostatic::addType could not convert GenericData to "
452 <                   "Quadrupole Moments for\n"
453 <                   "\tatom type %s\n", daType->getName().c_str());
454 <          painCave.severity = OPENMD_ERROR;
455 <          painCave.isFatal = 1;
456 <          simError();          
448 >      case esm_TAYLOR_SHIFTED:
449 >        
450 >        v01 = f0;
451 >        v11 = g1;
452 >        v21 = g2 * ri;
453 >        v22 = h2 - v21;
454 >        v31 = (h3 - g3 * ri) * ri;
455 >        v32 = s3 - 3.0*v31;
456 >        v41 = (h4 - g4 * ri) * ri2;
457 >        v42 = s4 * ri - 3.0*v41;
458 >        v43 = t4 - 6.0*v42 - 3.0*v41;
459 >
460 >        dv01 = g0;
461 >        dv11 = h1;
462 >        dv21 = (h2 - g2*ri)*ri;
463 >        dv22 = (s2 - (h2 - g2*ri)*ri);
464 >        dv31 = (s3 - 2.0*(h3-g3*ri)*ri)*ri;
465 >        dv32 = (t3 - 3.0*(s3-2.0*(h3-g3*ri)*ri)*ri);
466 >        dv41 = (s4 - 3.0*(h4 - g4*ri)*ri)*ri2;
467 >        dv42 = (t4 - (4.0*s4 - 9.0*(h4-g4*ri)*ri)*ri)*ri;
468 >        dv43 = (u4 - 3.0*(2.0*t4 - (7.0*s4 - 15.0*(h4 - g4*ri)*ri)*ri)*ri);
469 >
470 >        break;
471 >
472 >      case esm_SHIFTED_POTENTIAL:
473 >
474 >        v01 = f - fc;
475 >        v11 = g - gc;
476 >        v21 = g*ri - gc*ric;
477 >        v22 = h - g*ri - (hc - gc*ric);
478 >        v31 = (h-g*ri)*ri - (hc-gc*ric)*ric;
479 >        v32 = (s - 3.0*(h-g*ri)*ri) - (sc - 3.0*(hc-gc*ric)*ric);
480 >        v41 = (h - g*ri)*ri2 - (hc - gc*ric)*ric2;
481 >        v42 = (s-3.0*(h-g*ri)*ri)*ri - (sc-3.0*(hc-gc*ric)*ric)*ric;        
482 >        v43 = (t - 3.0*(2.0*s - 5.0*(h - g*ri)*ri)*ri)
483 >          - (tc - 3.0*(2.0*sc - 5.0*(hc - gc*ric)*ric)*ric);
484 >
485 >        dv01 = g;
486 >        dv11 = h;
487 >        dv21 = (h - g*ri)*ri;
488 >        dv22 = (s - (h - g*ri)*ri);
489 >        dv31 = (s - 2.0*(h-g*ri)*ri)*ri;
490 >        dv32 = (t - 3.0*(s-2.0*(h-g*ri)*ri)*ri);
491 >        dv41 = (s - 3.0*(h - g*ri)*ri)*ri2;
492 >        dv42 = (t - (4.0*s - 9.0*(h-g*ri)*ri)*ri)*ri;
493 >        dv43 = (u - 3.0*(2.0*t - (7.0*s - 15.0*(h - g*ri)*ri)*ri)*ri);
494 >
495 >        break;
496 >
497 >      case esm_SWITCHING_FUNCTION:
498 >      case esm_HARD:
499 >      case esm_EWALD_FULL:
500 >
501 >        v01 = f;
502 >        v11 = g;
503 >        v21 = g*ri;
504 >        v22 = h - g*ri;
505 >        v31 = (h-g*ri)*ri;
506 >        v32 = (s - 3.0*(h-g*ri)*ri);
507 >        v41 = (h - g*ri)*ri2;
508 >        v42 = (s-3.0*(h-g*ri)*ri)*ri;        
509 >        v43 = (t - 3.0*(2.0*s - 5.0*(h - g*ri)*ri)*ri);
510 >
511 >        dv01 = g;
512 >        dv11 = h;
513 >        dv21 = (h - g*ri)*ri;
514 >        dv22 = (s - (h - g*ri)*ri);
515 >        dv31 = (s - 2.0*(h-g*ri)*ri)*ri;
516 >        dv32 = (t - 3.0*(s-2.0*(h-g*ri)*ri)*ri);
517 >        dv41 = (s - 3.0*(h - g*ri)*ri)*ri2;
518 >        dv42 = (t - (4.0*s - 9.0*(h-g*ri)*ri)*ri)*ri;
519 >        dv43 = (u - 3.0*(2.0*t - (7.0*s - 15.0*(h - g*ri)*ri)*ri)*ri);
520 >
521 >        break;
522 >
523 >      case esm_REACTION_FIELD:
524 >        
525 >        // following DL_POLY's lead for shifting the image charge potential:
526 >        f = b0 + preRF_ * r2;
527 >        fc = b0c + preRF_ * cutoffRadius_ * cutoffRadius_;
528 >
529 >        g = db0_1 + preRF_ * 2.0 * r;        
530 >        gc = db0c_1 + preRF_ * 2.0 * cutoffRadius_;
531 >
532 >        h = db0_2 + preRF_ * 2.0;
533 >        hc = db0c_2 + preRF_ * 2.0;
534 >
535 >        v01 = f - fc;
536 >        v11 = g - gc;
537 >        v21 = g*ri - gc*ric;
538 >        v22 = h - g*ri - (hc - gc*ric);
539 >        v31 = (h-g*ri)*ri - (hc-gc*ric)*ric;
540 >        v32 = (s - 3.0*(h-g*ri)*ri) - (sc - 3.0*(hc-gc*ric)*ric);
541 >        v41 = (h - g*ri)*ri2 - (hc - gc*ric)*ric2;
542 >        v42 = (s-3.0*(h-g*ri)*ri)*ri - (sc-3.0*(hc-gc*ric)*ric)*ric;        
543 >        v43 = (t - 3.0*(2.0*s - 5.0*(h - g*ri)*ri)*ri)
544 >          - (tc - 3.0*(2.0*sc - 5.0*(hc - gc*ric)*ric)*ric);
545 >
546 >        dv01 = g;
547 >        dv11 = h;
548 >        dv21 = (h - g*ri)*ri;
549 >        dv22 = (s - (h - g*ri)*ri);
550 >        dv31 = (s - 2.0*(h-g*ri)*ri)*ri;
551 >        dv32 = (t - 3.0*(s-2.0*(h-g*ri)*ri)*ri);
552 >        dv41 = (s - 3.0*(h - g*ri)*ri)*ri2;
553 >        dv42 = (t - (4.0*s - 9.0*(h-g*ri)*ri)*ri)*ri;
554 >        dv43 = (u - 3.0*(2.0*t - (7.0*s - 15.0*(h - g*ri)*ri)*ri)*ri);
555 >
556 >        break;
557 >                
558 >      case esm_EWALD_PME:
559 >      case esm_EWALD_SPME:
560 >      default :
561 >        map<string, ElectrostaticSummationMethod>::iterator i;
562 >        std::string meth;
563 >        for (i = summationMap_.begin(); i != summationMap_.end(); ++i) {
564 >          if ((*i).second == summationMethod_) meth = (*i).first;
565          }
566 +        sprintf( painCave.errMsg,
567 +                 "Electrostatic::initialize: electrostaticSummationMethod %s \n"
568 +                 "\thas not been implemented yet. Please select one of:\n"
569 +                 "\t\"hard\", \"shifted_potential\", or \"shifted_force\"\n",
570 +                 meth.c_str() );
571 +        painCave.isFatal = 1;
572 +        simError();
573 +        break;      
574 +      }
575 +
576 +      // Add these computed values to the storage vectors for spline creation:
577 +      v01v.push_back(v01);
578 +      v11v.push_back(v11);
579 +      v21v.push_back(v21);
580 +      v22v.push_back(v22);
581 +      v31v.push_back(v31);
582 +      v32v.push_back(v32);      
583 +      v41v.push_back(v41);
584 +      v42v.push_back(v42);
585 +      v43v.push_back(v43);
586 +    }
587 +
588 +    // construct the spline structures and fill them with the values we've
589 +    // computed:
590 +
591 +    v01s = new CubicSpline();
592 +    v01s->addPoints(rv, v01v);
593 +    v11s = new CubicSpline();
594 +    v11s->addPoints(rv, v11v);
595 +    v21s = new CubicSpline();
596 +    v21s->addPoints(rv, v21v);
597 +    v22s = new CubicSpline();
598 +    v22s->addPoints(rv, v22v);
599 +    v31s = new CubicSpline();
600 +    v31s->addPoints(rv, v31v);
601 +    v32s = new CubicSpline();
602 +    v32s->addPoints(rv, v32v);
603 +    v41s = new CubicSpline();
604 +    v41s->addPoints(rv, v41v);
605 +    v42s = new CubicSpline();
606 +    v42s->addPoints(rv, v42v);
607 +    v43s = new CubicSpline();
608 +    v43s->addPoints(rv, v43v);
609 +
610 +    haveElectroSplines_ = true;
611 +
612 +    initialized_ = true;
613 +  }
614 +      
615 +  void Electrostatic::addType(AtomType* atomType){
616 +    
617 +    ElectrostaticAtomData electrostaticAtomData;
618 +    electrostaticAtomData.is_Charge = false;
619 +    electrostaticAtomData.is_Dipole = false;
620 +    electrostaticAtomData.is_Quadrupole = false;
621 +    electrostaticAtomData.is_Fluctuating = false;
622 +
623 +    FixedChargeAdapter fca = FixedChargeAdapter(atomType);
624 +
625 +    if (fca.isFixedCharge()) {
626 +      electrostaticAtomData.is_Charge = true;
627 +      electrostaticAtomData.fixedCharge = fca.getCharge();
628 +    }
629 +
630 +    MultipoleAdapter ma = MultipoleAdapter(atomType);
631 +    if (ma.isMultipole()) {
632 +      if (ma.isDipole()) {
633 +        electrostaticAtomData.is_Dipole = true;
634 +        electrostaticAtomData.dipole = ma.getDipole();
635 +      }
636 +      if (ma.isQuadrupole()) {
637          electrostaticAtomData.is_Quadrupole = true;
638 <        electrostaticAtomData.quadrupole_moments = v3dData->getData();
638 >        electrostaticAtomData.quadrupole = ma.getQuadrupole();
639        }
640      }
641      
642 <    AtomTypeProperties atp = atomType->getATP();    
642 >    FluctuatingChargeAdapter fqa = FluctuatingChargeAdapter(atomType);
643  
644 <    pair<map<int,AtomType*>::iterator,bool> ret;    
645 <    ret = ElectrostaticList.insert( pair<int,AtomType*>(atp.ident, atomType) );
644 >    if (fqa.isFluctuatingCharge()) {
645 >      electrostaticAtomData.is_Fluctuating = true;
646 >      electrostaticAtomData.electronegativity = fqa.getElectronegativity();
647 >      electrostaticAtomData.hardness = fqa.getHardness();
648 >      electrostaticAtomData.slaterN = fqa.getSlaterN();
649 >      electrostaticAtomData.slaterZeta = fqa.getSlaterZeta();
650 >    }
651 >
652 >    int atid = atomType->getIdent();
653 >    int etid = Etypes.size();
654 >    int fqtid = FQtypes.size();
655 >
656 >    pair<set<int>::iterator,bool> ret;    
657 >    ret = Etypes.insert( atid );
658      if (ret.second == false) {
659        sprintf( painCave.errMsg,
660                 "Electrostatic already had a previous entry with ident %d\n",
661 <               atp.ident);
661 >               atid);
662        painCave.severity = OPENMD_INFO;
663        painCave.isFatal = 0;
664        simError();        
665      }
666      
667 <    ElectrostaticMap[atomType] = electrostaticAtomData;    
667 >    Etids[ atid ] = etid;
668 >    ElectrostaticMap.push_back(electrostaticAtomData);
669 >
670 >    if (electrostaticAtomData.is_Fluctuating) {
671 >      ret = FQtypes.insert( atid );
672 >      if (ret.second == false) {
673 >        sprintf( painCave.errMsg,
674 >                 "Electrostatic already had a previous fluctuating charge entry with ident %d\n",
675 >                 atid );
676 >        painCave.severity = OPENMD_INFO;
677 >        painCave.isFatal = 0;
678 >        simError();        
679 >      }
680 >      FQtids[atid] = fqtid;
681 >      Jij[fqtid].resize(nFlucq_);
682 >
683 >      // Now, iterate over all known fluctuating and add to the
684 >      // coulomb integral map:
685 >      
686 >      std::set<int>::iterator it;
687 >      for( it = FQtypes.begin(); it != FQtypes.end(); ++it) {    
688 >        int etid2 = Etids[ (*it) ];
689 >        int fqtid2 = FQtids[ (*it) ];
690 >        ElectrostaticAtomData eaData2 = ElectrostaticMap[ etid2 ];
691 >        RealType a = electrostaticAtomData.slaterZeta;
692 >        RealType b = eaData2.slaterZeta;
693 >        int m = electrostaticAtomData.slaterN;
694 >        int n = eaData2.slaterN;
695 >        
696 >        // Create the spline of the coulombic integral for s-type
697 >        // Slater orbitals.  Add a 2 angstrom safety window to deal
698 >        // with cutoffGroups that have charged atoms longer than the
699 >        // cutoffRadius away from each other.
700 >        
701 >        RealType rval;
702 >        RealType dr = (cutoffRadius_ + 2.0) / RealType(np_ - 1);
703 >        vector<RealType> rvals;
704 >        vector<RealType> Jvals;
705 >        // don't start at i = 0, as rval = 0 is undefined for the
706 >        // slater overlap integrals.
707 >        for (int i = 1; i < np_+1; i++) {
708 >          rval = RealType(i) * dr;
709 >          rvals.push_back(rval);
710 >          Jvals.push_back(sSTOCoulInt( a, b, m, n, rval *
711 >                                       PhysicalConstants::angstromToBohr ) *
712 >                          PhysicalConstants::hartreeToKcal );
713 >        }
714 >        
715 >        CubicSpline* J = new CubicSpline();
716 >        J->addPoints(rvals, Jvals);
717 >        Jij[fqtid][fqtid2] = J;
718 >        Jij[fqtid2].resize( nFlucq_ );
719 >        Jij[fqtid2][fqtid] = J;
720 >      }      
721 >    }      
722      return;
723    }
724    
725 <  void Electrostatic::setElectrostaticCutoffRadius( RealType theECR,
726 <                                                    RealType theRSW ) {
412 <    cutoffRadius_ = theECR;
413 <    rrf_ = cutoffRadius_;
414 <    rt_ = theRSW;
725 >  void Electrostatic::setCutoffRadius( RealType rCut ) {
726 >    cutoffRadius_ = rCut;
727      haveCutoffRadius_ = true;
728    }
729 +
730    void Electrostatic::setElectrostaticSummationMethod( ElectrostaticSummationMethod esm ) {
731      summationMethod_ = esm;
732    }
# Line 429 | Line 742 | namespace OpenMD {
742      haveDielectric_ = true;
743    }
744  
745 <  void Electrostatic::calcForce(InteractionData idat) {
745 >  void Electrostatic::calcForce(InteractionData &idat) {
746  
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    
747      if (!initialized_) initialize();
748      
749 <    ElectrostaticAtomData data1 = ElectrostaticMap[idat.atype1];
750 <    ElectrostaticAtomData data2 = ElectrostaticMap[idat.atype2];
749 >    data1 = ElectrostaticMap[Etids[idat.atid1]];
750 >    data2 = ElectrostaticMap[Etids[idat.atid2]];
751 >
752 >    U = 0.0;  // Potential
753 >    F.zero();  // Force
754 >    Ta.zero(); // Torque on site a
755 >    Tb.zero(); // Torque on site b
756 >    Ea.zero(); // Electric field at site a
757 >    Eb.zero(); // Electric field at site b
758 >    dUdCa = 0.0; // fluctuating charge force at site a
759 >    dUdCb = 0.0; // fluctuating charge force at site a
760      
761 <    // some variables we'll need independent of electrostatic type:
761 >    // Indirect interactions mediated by the reaction field.
762 >    indirect_Pot = 0.0;   // Potential
763 >    indirect_F.zero();    // Force
764 >    indirect_Ta.zero();   // Torque on site a
765 >    indirect_Tb.zero();   // Torque on site b
766  
767 <    riji = 1.0 / idat.rij;
768 <    Vector3d rhat = idat.d  * riji;
767 >    // Excluded potential that is still computed for fluctuating charges
768 >    excluded_Pot= 0.0;
769  
770 <    // logicals
770 >    // some variables we'll need independent of electrostatic type:
771  
772 <    bool i_is_Charge = data1.is_Charge;
773 <    bool i_is_Dipole = data1.is_Dipole;
478 <    bool i_is_SplitDipole = data1.is_SplitDipole;
479 <    bool i_is_Quadrupole = data1.is_Quadrupole;
480 <
481 <    bool j_is_Charge = data2.is_Charge;
482 <    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);
772 >    ri = 1.0 /  *(idat.rij);
773 >    rhat =  *(idat.d)  * ri;
774        
775 <      ct_i = dot(uz_i, rhat);
775 >    // logicals
776  
777 <      if (i_is_SplitDipole)
778 <        d_i = data1.split_dipole_distance;
779 <      
780 <      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);
777 >    a_is_Charge = data1.is_Charge;
778 >    a_is_Dipole = data1.is_Dipole;
779 >    a_is_Quadrupole = data1.is_Quadrupole;
780 >    a_is_Fluctuating = data1.is_Fluctuating;
781  
782 <      cx_i = dot(ux_i, rhat);
783 <      cy_i = dot(uy_i, rhat);
784 <      cz_i = dot(uz_i, rhat);
782 >    b_is_Charge = data2.is_Charge;
783 >    b_is_Dipole = data2.is_Dipole;
784 >    b_is_Quadrupole = data2.is_Quadrupole;
785 >    b_is_Fluctuating = data2.is_Fluctuating;
786  
787 <      dudux_i = V3Zero;
788 <      duduy_i = V3Zero;
789 <      duduz_i = V3Zero;
787 >    // Obtain all of the required radial function values from the
788 >    // spline structures:
789 >    
790 >    // needed for fields (and forces):
791 >    if (a_is_Charge || b_is_Charge) {
792 >      v01s->getValueAndDerivativeAt( *(idat.rij), v01, dv01);
793      }
794 +    if (a_is_Dipole || b_is_Dipole) {
795 +      v11s->getValueAndDerivativeAt( *(idat.rij), v11, dv11);
796 +      v11or = ri * v11;
797 +    }
798 +    if (a_is_Quadrupole || b_is_Quadrupole ||  (a_is_Dipole && b_is_Dipole)) {
799 +      v21s->getValueAndDerivativeAt( *(idat.rij), v21, dv21);
800 +      v22s->getValueAndDerivativeAt( *(idat.rij), v22, dv22);
801 +      v22or = ri * v22;
802 +    }      
803  
804 <    if (j_is_Charge)
805 <      q_j = data2.charge;
804 >    // needed for potentials (and forces and torques):
805 >    if ((a_is_Dipole && b_is_Quadrupole) ||
806 >        (b_is_Dipole && a_is_Quadrupole)) {
807 >      v31s->getValueAndDerivativeAt( *(idat.rij), v31, dv31);
808 >      v32s->getValueAndDerivativeAt( *(idat.rij), v32, dv32);
809 >      v31or = v31 * ri;
810 >      v32or = v32 * ri;
811 >    }
812 >    if (a_is_Quadrupole && b_is_Quadrupole) {
813 >      v41s->getValueAndDerivativeAt( *(idat.rij), v41, dv41);
814 >      v42s->getValueAndDerivativeAt( *(idat.rij), v42, dv42);
815 >      v43s->getValueAndDerivativeAt( *(idat.rij), v43, dv43);
816 >      v42or = v42 * ri;
817 >      v43or = v43 * ri;
818 >    }
819  
820 <    if (j_is_Dipole) {
821 <      mu_j = data2.dipole_moment;
822 <      uz_j = idat.eFrame2.getColumn(2);
820 >    // calculate the single-site contributions (fields, etc).
821 >    
822 >    if (a_is_Charge) {
823 >      C_a = data1.fixedCharge;
824        
825 <      ct_j = dot(uz_j, rhat);
826 <
827 <      if (j_is_SplitDipole)
530 <        d_j = data2.split_dipole_distance;
825 >      if (a_is_Fluctuating) {
826 >        C_a += *(idat.flucQ1);
827 >      }
828        
829 <      duduz_j = V3Zero;
829 >      if (idat.excluded) {
830 >        *(idat.skippedCharge2) += C_a;
831 >      } else {
832 >        // only do the field if we're not excluded:
833 >        Eb -= C_a *  pre11_ * dv01 * rhat;
834 >      }
835      }
836      
837 <    if (j_is_Quadrupole) {
838 <      Q_j = data2.quadrupole_moments;
839 <      qxx_j = Q_j.x();
840 <      qyy_j = Q_j.y();
841 <      qzz_j = Q_j.z();
837 >    if (a_is_Dipole) {
838 >      D_a = *(idat.dipole1);
839 >      rdDa = dot(rhat, D_a);
840 >      rxDa = cross(rhat, D_a);
841 >      if (!idat.excluded)
842 >        Eb -=  pre12_ * ((dv11-v11or) * rdDa * rhat + v11or * D_a);
843 >    }
844 >    
845 >    if (a_is_Quadrupole) {
846 >      Q_a = *(idat.quadrupole1);
847 >      trQa =  Q_a.trace();
848 >      Qar =   Q_a * rhat;
849 >      rQa = rhat * Q_a;
850 >      rdQar = dot(rhat, Qar);
851 >      rxQar = cross(rhat, Qar);
852 >      if (!idat.excluded)
853 >        Eb -= pre14_ * (trQa * rhat * dv21 + 2.0 * Qar * v22or
854 >                        + rdQar * rhat * (dv22 - 2.0*v22or));
855 >    }
856 >    
857 >    if (b_is_Charge) {
858 >      C_b = data2.fixedCharge;
859        
860 <      ux_j = idat.eFrame2.getColumn(0);
861 <      uy_j = idat.eFrame2.getColumn(1);
862 <      uz_j = idat.eFrame2.getColumn(2);
863 <
864 <      cx_j = dot(ux_j, rhat);
865 <      cy_j = dot(uy_j, rhat);
866 <      cz_j = dot(uz_j, rhat);
867 <
868 <      dudux_j = V3Zero;
550 <      duduy_j = V3Zero;
551 <      duduz_j = V3Zero;
860 >      if (b_is_Fluctuating)
861 >        C_b += *(idat.flucQ2);
862 >      
863 >      if (idat.excluded) {
864 >        *(idat.skippedCharge1) += C_b;
865 >      } else {
866 >        // only do the field if we're not excluded:
867 >        Ea += C_b *  pre11_ * dv01 * rhat;
868 >      }
869      }
870      
871 <    epot = 0.0;
872 <    dVdr = V3Zero;
871 >    if (b_is_Dipole) {
872 >      D_b = *(idat.dipole2);
873 >      rdDb = dot(rhat, D_b);
874 >      rxDb = cross(rhat, D_b);
875 >      if (!idat.excluded)
876 >        Ea += pre12_ * ((dv11-v11or) * rdDb * rhat + v11or * D_b);
877 >    }
878      
879 <    if (i_is_Charge) {
879 >    if (b_is_Quadrupole) {
880 >      Q_b = *(idat.quadrupole2);
881 >      trQb =  Q_b.trace();
882 >      Qbr =   Q_b * rhat;
883 >      rQb = rhat * Q_b;
884 >      rdQbr = dot(rhat, Qbr);
885 >      rxQbr = cross(rhat, Qbr);
886 >      if (!idat.excluded)
887 >        Ea += pre14_ * (trQb * rhat * dv21 + 2.0 * Qbr * v22or
888 >                        + rdQbr * rhat * (dv22 - 2.0*v22or));
889 >    }
890 >        
891 >
892 >    if ((a_is_Fluctuating || b_is_Fluctuating) && idat.excluded) {
893 >      J = Jij[FQtids[idat.atid1]][FQtids[idat.atid2]];
894 >    }    
895 >
896 >    if (a_is_Charge) {    
897        
898 <      if (j_is_Charge) {
899 <        if (screeningMethod_ == DAMPED) {
900 <          // assemble the damping variables
901 <          res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
563 <          erfcVal = res.first;
564 <          derfcVal = res.second;
565 <          c1 = erfcVal * riji;
566 <          c2 = (-derfcVal + c1) * riji;
567 <        } else {
568 <          c1 = riji;
569 <          c2 = c1 * riji;
570 <        }
898 >      if (b_is_Charge) {
899 >        pref =  pre11_ * *(idat.electroMult);      
900 >        U  += C_a * C_b * pref * v01;
901 >        F  += C_a * C_b * pref * dv01 * rhat;
902  
903 <        preVal = idat.electroMult * pre11_ * q_i * q_j;
904 <        
574 <        if (summationMethod_ == esm_SHIFTED_POTENTIAL) {
575 <          vterm = preVal * (c1 - c1c_);
576 <          dudr  = -idat.sw * preVal * c2;
903 >        // If this is an excluded pair, there are still indirect
904 >        // interactions via the reaction field we must worry about:
905  
906 <        } else if (summationMethod_ == esm_SHIFTED_FORCE)  {
907 <          vterm = preVal * ( c1 - c1c_ + c2c_*(idat.rij - cutoffRadius_) );
908 <          dudr  = idat.sw * preVal * (c2c_ - c2);
906 >        if (summationMethod_ == esm_REACTION_FIELD && idat.excluded) {
907 >          rfContrib = preRF_ * pref * C_a * C_b * *(idat.r2);
908 >          indirect_Pot += rfContrib;
909 >          indirect_F   += rfContrib * 2.0 * ri * rhat;
910 >        }
911  
912 <        } else if (summationMethod_ == esm_REACTION_FIELD) {
913 <          rfVal = idat.electroMult * preRF_ * idat.rij * idat.rij;
914 <          vterm = preVal * ( riji + rfVal );            
585 <          dudr  = idat.sw * preVal * ( 2.0 * rfVal - riji ) * riji;
912 >        // Fluctuating charge forces are handled via Coulomb integrals
913 >        // for excluded pairs (i.e. those connected via bonds) and
914 >        // with the standard charge-charge interaction otherwise.
915  
916 +        if (idat.excluded) {
917 +          if (a_is_Fluctuating || b_is_Fluctuating) {
918 +            coulInt = J->getValueAt( *(idat.rij) );
919 +            if (a_is_Fluctuating) dUdCa += C_b * coulInt;
920 +            if (b_is_Fluctuating) dUdCb += C_a * coulInt;          
921 +          }
922          } else {
923 <          vterm = preVal * riji * erfcVal;            
923 >          if (a_is_Fluctuating) dUdCa += C_b * pref * v01;
924 >          if (b_is_Fluctuating) dUdCb += C_a * pref * v01;
925 >        }              
926 >      }
927  
928 <          dudr  = - idat.sw * preVal * c2;
928 >      if (b_is_Dipole) {
929 >        pref =  pre12_ * *(idat.electroMult);        
930 >        U  += C_a * pref * v11 * rdDb;
931 >        F  += C_a * pref * ((dv11 - v11or) * rdDb * rhat + v11or * D_b);
932 >        Tb += C_a * pref * v11 * rxDb;
933  
934 <        }
593 <
594 <        idat.vpair += vterm;
595 <        epot += idat.sw * vterm;
934 >        if (a_is_Fluctuating) dUdCa += pref * v11 * rdDb;
935  
936 <        dVdr += dudr * rhat;      
937 <      }
936 >        // Even if we excluded this pair from direct interactions, we
937 >        // still have the reaction-field-mediated charge-dipole
938 >        // interaction:
939  
940 <      if (j_is_Dipole) {
941 <        // pref is used by all the possible methods
942 <        pref = idat.electroMult * pre12_ * q_i * mu_j;
943 <        preSw = idat.sw * pref;
940 >        if (summationMethod_ == esm_REACTION_FIELD && idat.excluded) {
941 >          rfContrib = C_a * pref * preRF_ * 2.0 * *(idat.rij);
942 >          indirect_Pot += rfContrib * rdDb;
943 >          indirect_F   += rfContrib * D_b / (*idat.rij);
944 >          indirect_Tb  += C_a * pref * preRF_ * rxDb;
945 >        }
946 >      }
947  
948 <        if (summationMethod_ == esm_REACTION_FIELD) {
949 <          ri2 = riji * riji;
950 <          ri3 = ri2 * riji;
951 <    
952 <          vterm = - pref * ct_j * ( ri2 - preRF2_ * idat.rij );
953 <          idat.vpair += vterm;
611 <          epot += idat.sw * vterm;
948 >      if (b_is_Quadrupole) {
949 >        pref = pre14_ * *(idat.electroMult);
950 >        U  +=  C_a * pref * (v21 * trQb + v22 * rdQbr);
951 >        F  +=  C_a * pref * (trQb * dv21 * rhat + 2.0 * Qbr * v22or);
952 >        F  +=  C_a * pref * rdQbr * rhat * (dv22 - 2.0*v22or);
953 >        Tb +=  C_a * pref * 2.0 * rxQbr * v22;
954  
955 <          dVdr +=  -preSw * (ri3 * (uz_j - 3.0 * ct_j * rhat) - preRF2_*uz_j);
956 <          duduz_j += -preSw * rhat * (ri2 - preRF2_ * idat.rij);  
955 >        if (a_is_Fluctuating) dUdCa += pref * (v21 * trQb + v22 * rdQbr);
956 >      }
957 >    }
958  
959 <        } else {
617 <          // 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;
959 >    if (a_is_Dipole) {
960  
961 <          if (screeningMethod_ == DAMPED) {
962 <            // assemble the damping variables
631 <            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;
961 >      if (b_is_Charge) {
962 >        pref = pre12_ * *(idat.electroMult);
963  
964 <          // calculate the potential
965 <          pot_term =  scale * c2;
966 <          vterm = -pref * ct_j * pot_term;
648 <          idat.vpair += vterm;
649 <          epot += idat.sw * vterm;
650 <            
651 <          // calculate derivatives for forces and torques
964 >        U  -= C_b * pref * v11 * rdDa;
965 >        F  -= C_b * pref * ((dv11-v11or) * rdDa * rhat + v11or * D_a);
966 >        Ta -= C_b * pref * v11 * rxDa;
967  
968 <          dVdr += -preSw * (uz_j * c2ri - ct_j * rhat * sc2 * c3);
654 <          duduz_j += -preSw * pot_term * rhat;
968 >        if (b_is_Fluctuating) dUdCb -= pref * v11 * rdDa;
969  
970 +        // Even if we excluded this pair from direct interactions,
971 +        // we still have the reaction-field-mediated charge-dipole
972 +        // interaction:
973 +        if (summationMethod_ == esm_REACTION_FIELD && idat.excluded) {
974 +          rfContrib = C_b * pref * preRF_ * 2.0 * *(idat.rij);
975 +          indirect_Pot -= rfContrib * rdDa;
976 +          indirect_F   -= rfContrib * D_a / (*idat.rij);
977 +          indirect_Ta  -= C_b * pref * preRF_ * rxDa;
978          }
979        }
980  
981 <      if (j_is_Quadrupole) {
982 <        // first precalculate some necessary variables
983 <        cx2 = cx_j * cx_j;
984 <        cy2 = cy_j * cy_j;
985 <        cz2 = cz_j * cz_j;
986 <        pref =  idat.electroMult * pre14_ * q_i * one_third_;
987 <          
988 <        if (screeningMethod_ == DAMPED) {
989 <          // assemble the damping variables
990 <          res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
991 <          erfcVal = res.first;
992 <          derfcVal = res.second;
993 <          c1 = erfcVal * riji;
994 <          c2 = (-derfcVal + c1) * riji;
995 <          c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * riji;
996 <          c4 = -4.0 * derfcVal * alpha4_ + 5.0 * c3 * riji * riji;
997 <        } else {
998 <          c1 = riji;
677 <          c2 = c1 * riji;
678 <          c3 = 3.0 * c2 * riji;
679 <          c4 = 5.0 * c3 * riji * riji;
981 >      if (b_is_Dipole) {
982 >        pref = pre22_ * *(idat.electroMult);
983 >        DadDb = dot(D_a, D_b);
984 >        DaxDb = cross(D_a, D_b);
985 >
986 >        U  -= pref * (DadDb * v21 + rdDa * rdDb * v22);
987 >        F  -= pref * (dv21 * DadDb * rhat + v22or * (rdDb * D_a + rdDa * D_b));
988 >        F  -= pref * (rdDa * rdDb) * (dv22 - 2.0*v22or) * rhat;
989 >        Ta += pref * ( v21 * DaxDb - v22 * rdDb * rxDa);
990 >        Tb += pref * (-v21 * DaxDb - v22 * rdDa * rxDb);
991 >        // Even if we excluded this pair from direct interactions, we
992 >        // still have the reaction-field-mediated dipole-dipole
993 >        // interaction:
994 >        if (summationMethod_ == esm_REACTION_FIELD && idat.excluded) {
995 >          rfContrib = -pref * preRF_ * 2.0;
996 >          indirect_Pot += rfContrib * DadDb;
997 >          indirect_Ta  += rfContrib * DaxDb;
998 >          indirect_Tb  -= rfContrib * DaxDb;
999          }
1000 +      }
1001  
1002 <        // precompute variables for convenience
1003 <        preSw = idat.sw * pref;
1004 <        c2ri = c2 * riji;
1005 <        c3ri = c3 * riji;
1006 <        c4rij = c4 * idat.rij;
687 <        rhatdot2 = 2.0 * rhat * c3;
688 <        rhatc4 = rhat * c4rij;
1002 >      if (b_is_Quadrupole) {
1003 >        pref = pre24_ * *(idat.electroMult);
1004 >        DadQb = D_a * Q_b;
1005 >        DadQbr = dot(D_a, Qbr);
1006 >        DaxQbr = cross(D_a, Qbr);
1007  
1008 <        // calculate the potential
1009 <        pot_term = ( qxx_j * (cx2*c3 - c2ri) +
1010 <                     qyy_j * (cy2*c3 - c2ri) +
1011 <                     qzz_j * (cz2*c3 - c2ri) );
1012 <        vterm = pref * pot_term;
1013 <        idat.vpair += vterm;
1014 <        epot += idat.sw * vterm;
1015 <                
698 <        // calculate derivatives for the forces and torques
699 <
700 <        dVdr += -preSw * ( qxx_j* (cx2*rhatc4 - (2.0*cx_j*ux_j + rhat)*c3ri) +
701 <                           qyy_j* (cy2*rhatc4 - (2.0*cy_j*uy_j + rhat)*c3ri) +
702 <                           qzz_j* (cz2*rhatc4 - (2.0*cz_j*uz_j + rhat)*c3ri));
703 <                          
704 <        dudux_j += preSw * qxx_j * cx_j * rhatdot2;
705 <        duduy_j += preSw * qyy_j * cy_j * rhatdot2;
706 <        duduz_j += preSw * qzz_j * cz_j * rhatdot2;
1008 >        U  -= pref * ((trQb*rdDa + 2.0*DadQbr)*v31 + rdDa*rdQbr*v32);
1009 >        F  -= pref * (trQb*D_a + 2.0*DadQb) * v31or;
1010 >        F  -= pref * (trQb*rdDa + 2.0*DadQbr) * (dv31-v31or) * rhat;
1011 >        F  -= pref * (D_a*rdQbr + 2.0*rdDa*rQb) * v32or;
1012 >        F  -= pref * (rdDa * rdQbr * rhat * (dv32-3.0*v32or));
1013 >        Ta += pref * ((-trQb*rxDa + 2.0 * DaxQbr)*v31 - rxDa*rdQbr*v32);
1014 >        Tb += pref * ((2.0*cross(DadQb, rhat) - 2.0*DaxQbr)*v31
1015 >                      - 2.0*rdDa*rxQbr*v32);
1016        }
1017      }
709    
710    if (i_is_Dipole) {
1018  
1019 <      if (j_is_Charge) {
1020 <        // variables used by all the methods
1021 <        pref = idat.electroMult * pre12_ * q_j * mu_i;
1022 <        preSw = idat.sw * pref;
1019 >    if (a_is_Quadrupole) {
1020 >      if (b_is_Charge) {
1021 >        pref = pre14_ * *(idat.electroMult);
1022 >        U  += C_b * pref * (v21 * trQa + v22 * rdQar);
1023 >        F  += C_b * pref * (trQa * rhat * dv21 + 2.0 * Qar * v22or);
1024 >        F  += C_b * pref * rdQar * rhat * (dv22 - 2.0*v22or);
1025 >        Ta += C_b * pref * 2.0 * rxQar * v22;
1026  
1027 <        if (summationMethod_ == esm_REACTION_FIELD) {
1027 >        if (b_is_Fluctuating) dUdCb += pref * (v21 * trQa + v22 * rdQar);
1028 >      }
1029 >      if (b_is_Dipole) {
1030 >        pref = pre24_ * *(idat.electroMult);
1031 >        DbdQa = D_b * Q_a;
1032 >        DbdQar = dot(D_b, Qar);
1033 >        DbxQar = cross(D_b, Qar);
1034  
1035 <          ri2 = riji * riji;
1036 <          ri3 = ri2 * riji;
1035 >        U  += pref * ((trQa*rdDb + 2.0*DbdQar)*v31 + rdDb*rdQar*v32);
1036 >        F  += pref * (trQa*D_b + 2.0*DbdQa) * v31or;
1037 >        F  += pref * (trQa*rdDb + 2.0*DbdQar) * (dv31-v31or) * rhat;
1038 >        F  += pref * (D_b*rdQar + 2.0*rdDb*rQa) * v32or;
1039 >        F  += pref * (rdDb * rdQar * rhat * (dv32-3.0*v32or));
1040 >        Ta += pref * ((-2.0*cross(DbdQa, rhat) + 2.0*DbxQar)*v31
1041 >                      + 2.0*rdDb*rxQar*v32);
1042 >        Tb += pref * ((trQa*rxDb - 2.0 * DbxQar)*v31 + rxDb*rdQar*v32);
1043 >      }
1044 >      if (b_is_Quadrupole) {
1045 >        pref = pre44_ * *(idat.electroMult);  // yes
1046 >        QaQb = Q_a * Q_b;
1047 >        trQaQb = QaQb.trace();
1048 >        rQaQb = rhat * QaQb;
1049 >        QaQbr = QaQb * rhat;
1050 >        QaxQb = mCross(Q_a, Q_b);
1051 >        rQaQbr = dot(rQa, Qbr);
1052 >        rQaxQbr = cross(rQa, Qbr);
1053 >        
1054 >        U  += pref * (trQa * trQb + 2.0 * trQaQb) * v41;
1055 >        U  += pref * (trQa * rdQbr + trQb * rdQar  + 4.0 * rQaQbr) * v42;
1056 >        U  += pref * (rdQar * rdQbr) * v43;
1057  
1058 <          vterm = pref * ct_i * ( ri2 - preRF2_ * idat.rij );
1059 <          idat.vpair += vterm;
1060 <          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;
1058 >        F  += pref * rhat * (trQa * trQb + 2.0 * trQaQb)*dv41;
1059 >        F  += pref*rhat*(trQa*rdQbr + trQb*rdQar + 4.0*rQaQbr)*(dv42-2.0*v42or);
1060 >        F  += pref * rhat * (rdQar * rdQbr)*(dv43 - 4.0*v43or);
1061  
1062 <          // calculate derivatives for the forces and torques
1063 <          dVdr += preSw * (uz_i * c2ri - ct_i * rhat * sc2 * c3);
1064 <          duduz_i += preSw * pot_term * rhat;
769 <        }
770 <      }
1062 >        F  += pref * 2.0 * (trQb*rQa + trQa*rQb) * v42or;
1063 >        F  += pref * 4.0 * (rQaQb + QaQbr) * v42or;
1064 >        F  += pref * 2.0 * (rQa*rdQbr + rdQar*rQb) * v43or;
1065  
1066 <      if (j_is_Dipole) {
1067 <        // variables used by all methods
1068 <        ct_ij = dot(uz_i, uz_j);
1066 >        Ta += pref * (- 4.0 * QaxQb * v41);
1067 >        Ta += pref * (- 2.0 * trQb * cross(rQa, rhat)
1068 >                      + 4.0 * cross(rhat, QaQbr)
1069 >                      - 4.0 * rQaxQbr) * v42;
1070 >        Ta += pref * 2.0 * cross(rhat,Qar) * rdQbr * v43;
1071  
776        pref = idat.electroMult * pre22_ * mu_i * mu_j;
777        preSw = idat.sw * pref;
1072  
1073 <        if (summationMethod_ == esm_REACTION_FIELD) {
1074 <          ri2 = riji * riji;
1075 <          ri3 = ri2 * riji;
1076 <          ri4 = ri2 * ri2;
1073 >        Tb += pref * (+ 4.0 * QaxQb * v41);
1074 >        Tb += pref * (- 2.0 * trQa * cross(rQb, rhat)
1075 >                      - 4.0 * cross(rQaQb, rhat)
1076 >                      + 4.0 * rQaxQbr) * v42;
1077 >        // Possible replacement for line 2 above:
1078 >        //             + 4.0 * cross(rhat, QbQar)
1079  
1080 <          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);
792 <
793 <          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 <        }
1080 >        Tb += pref * 2.0 * cross(rhat,Qbr) * rdQar * v43;
1081        }
1082      }
1083  
1084 <    if (i_is_Quadrupole) {
1085 <      if (j_is_Charge) {
1086 <        // precompute some necessary variables
1087 <        cx2 = cx_i * cx_i;
862 <        cy2 = cy_i * cy_i;
863 <        cz2 = cz_i * cz_i;
1084 >    if (idat.doElectricField) {
1085 >      *(idat.eField1) += Ea * *(idat.electroMult);
1086 >      *(idat.eField2) += Eb * *(idat.electroMult);
1087 >    }
1088  
1089 <        pref = idat.electroMult * pre14_ * q_j * one_third_;
1089 >    if (a_is_Fluctuating) *(idat.dVdFQ1) += dUdCa * *(idat.sw);
1090 >    if (b_is_Fluctuating) *(idat.dVdFQ2) += dUdCb * *(idat.sw);
1091  
1092 <        if (screeningMethod_ == DAMPED) {
1093 <          // assemble the damping variables
1094 <          res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
1095 <          erfcVal = res.first;
1096 <          derfcVal = res.second;
1097 <          c1 = erfcVal * riji;
1098 <          c2 = (-derfcVal + c1) * riji;
1099 <          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;
1092 >    if (!idat.excluded) {
1093 >      
1094 >      *(idat.vpair) += U;
1095 >      (*(idat.pot))[ELECTROSTATIC_FAMILY] += U * *(idat.sw);
1096 >      *(idat.f1) += F * *(idat.sw);
1097 >      
1098 >      if (a_is_Dipole || a_is_Quadrupole)
1099 >        *(idat.t1) += Ta * *(idat.sw);
1100  
1101 <        // calculate the potential
1102 <        pot_term = ( qxx_i * (cx2 * c3 - c2ri) +
1103 <                     qyy_i * (cy2 * c3 - c2ri) +
1104 <                     qzz_i * (cz2 * c3 - c2ri) );
895 <        
896 <        vterm = pref * pot_term;
897 <        idat.vpair += vterm;
898 <        epot += idat.sw * vterm;
1101 >      if (b_is_Dipole || b_is_Quadrupole)
1102 >        *(idat.t2) += Tb * *(idat.sw);
1103 >      
1104 >    } else {
1105  
1106 <        // calculate the derivatives for the forces and torques
1106 >      // only accumulate the forces and torques resulting from the
1107 >      // indirect reaction field terms.
1108  
1109 <        dVdr += -preSw * (qxx_i* (cx2*rhatc4 - (2.0*cx_i*ux_i + rhat)*c3ri) +
1110 <                          qyy_i* (cy2*rhatc4 - (2.0*cy_i*uy_i + rhat)*c3ri) +
1111 <                          qzz_i* (cz2*rhatc4 - (2.0*cz_i*uz_i + rhat)*c3ri));
1112 <
1113 <        dudux_i += preSw * qxx_i * cx_i *  rhatdot2;
1114 <        duduy_i += preSw * qyy_i * cy_i *  rhatdot2;
1115 <        duduz_i += preSw * qzz_i * cz_i *  rhatdot2;
1116 <      }
1109 >      *(idat.vpair) += indirect_Pot;      
1110 >      (*(idat.excludedPot))[ELECTROSTATIC_FAMILY] +=  excluded_Pot;
1111 >      (*(idat.pot))[ELECTROSTATIC_FAMILY] += *(idat.sw) * indirect_Pot;
1112 >      *(idat.f1) += *(idat.sw) * indirect_F;
1113 >      
1114 >      if (a_is_Dipole || a_is_Quadrupole)
1115 >        *(idat.t1) += *(idat.sw) * indirect_Ta;
1116 >            
1117 >      if (b_is_Dipole || b_is_Quadrupole)
1118 >        *(idat.t2) += *(idat.sw) * indirect_Tb;
1119      }
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
1120      return;
1121 <  }  
1121 >  }
1122 >    
1123 >  void Electrostatic::calcSelfCorrection(SelfData &sdat) {
1124  
932  void Electrostatic::calcSkipCorrection(SkipCorrectionData skdat) {
933
1125      if (!initialized_) initialize();
1126 +
1127 +    ElectrostaticAtomData data = ElectrostaticMap[Etids[sdat.atid]];
1128      
936    ElectrostaticAtomData data1 = ElectrostaticMap[skdat.atype1];
937    ElectrostaticAtomData data2 = ElectrostaticMap[skdat.atype2];
938    
1129      // logicals
1130 +    bool i_is_Charge = data.is_Charge;
1131 +    bool i_is_Dipole = data.is_Dipole;
1132 +    bool i_is_Quadrupole = data.is_Quadrupole;
1133 +    bool i_is_Fluctuating = data.is_Fluctuating;
1134 +    RealType C_a = data.fixedCharge;  
1135 +    RealType self(0.0), preVal, DdD, trQ, trQQ;
1136  
1137 <    bool i_is_Charge = data1.is_Charge;
1138 <    bool i_is_Dipole = data1.is_Dipole;
943 <
944 <    bool j_is_Charge = data2.is_Charge;
945 <    bool j_is_Dipole = data2.is_Dipole;
946 <
947 <    RealType q_i, q_j;
948 <    
949 <    // The skippedCharge computation is needed by the real-space cutoff methods
950 <    // (i.e. shifted force and shifted potential)
951 <
952 <    if (i_is_Charge) {
953 <      q_i = data1.charge;
954 <      skdat.skippedCharge2 += q_i;
1137 >    if (i_is_Dipole) {
1138 >      DdD = data.dipole.lengthSquare();
1139      }
1140 <
1141 <    if (j_is_Charge) {
1142 <      q_j = data2.charge;
1143 <      skdat.skippedCharge1 += q_j;
1140 >        
1141 >    if (i_is_Fluctuating) {
1142 >      C_a += *(sdat.flucQ);
1143 >      *(sdat.flucQfrc) -=  *(sdat.flucQ) * data.hardness + data.electronegativity;
1144 >      (*(sdat.excludedPot))[ELECTROSTATIC_FAMILY] += (*sdat.flucQ) *
1145 >        (*(sdat.flucQ) * data.hardness * 0.5 + data.electronegativity);
1146      }
1147  
1148 <    // the rest of this function should only be necessary for reaction field.
1149 <
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:
1148 >    switch (summationMethod_) {
1149 >    case esm_REACTION_FIELD:
1150        
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    
1151        if (i_is_Charge) {
1152 <        if (j_is_Charge) {
1153 <          preVal = skdat.electroMult * pre11_ * q_i * q_j;
1154 <          rfVal = preRF_ * skdat.rij * skdat.rij;
1155 <          vterm = preVal * rfVal;
1156 <          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 <        }
1152 >        // Self potential [see Wang and Hermans, "Reaction Field
1153 >        // Molecular Dynamics Simulation with Friedman’s Image Charge
1154 >        // Method," J. Phys. Chem. 99, 12001-12007 (1995).]
1155 >        preVal = pre11_ * preRF_ * C_a * C_a;
1156 >        (*(sdat.pot))[ELECTROSTATIC_FAMILY] -= 0.5 * preVal / cutoffRadius_;
1157        }
1158 +
1159        if (i_is_Dipole) {
1160 <        if (j_is_Charge) {
1012 <          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 <        }
1160 >        (*(sdat.pot))[ELECTROSTATIC_FAMILY] -= pre22_ * preRF_ * DdD;
1161        }
1162        
1163 <      // accumulate the forces and torques resulting from the self term
1023 <      skdat.pot += myPot;
1024 <      skdat.f1 += dVdr;
1163 >      break;
1164        
1165 +    case esm_SHIFTED_FORCE:
1166 +    case esm_SHIFTED_POTENTIAL:
1167 +    case esm_TAYLOR_SHIFTED:
1168 +    case esm_EWALD_FULL:
1169 +      if (i_is_Charge)
1170 +        self += selfMult1_ * pre11_ * C_a * (C_a + *(sdat.skippedCharge));      
1171        if (i_is_Dipole)
1172 <        skdat.t1 -= cross(uz_i, duduz_i);
1173 <      if (j_is_Dipole)
1174 <        skdat.t2 -= cross(uz_j, duduz_j);
1172 >        self += selfMult2_ * pre22_ * DdD;      
1173 >      if (i_is_Quadrupole) {
1174 >        trQ = data.quadrupole.trace();
1175 >        trQQ = (data.quadrupole * data.quadrupole).trace();
1176 >        self += selfMult4_ * pre44_ * (2.0*trQQ + trQ*trQ);
1177 >        if (i_is_Charge)
1178 >          self -= selfMult2_ * pre14_ * 2.0 * C_a * trQ;
1179 >      }
1180 >      (*(sdat.pot))[ELECTROSTATIC_FAMILY] += self;      
1181 >      break;
1182 >    default:
1183 >      break;
1184      }
1185    }
1186 +  
1187 +  RealType Electrostatic::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) {
1188 +    // This seems to work moderately well as a default.  There's no
1189 +    // inherent scale for 1/r interactions that we can standardize.
1190 +    // 12 angstroms seems to be a reasonably good guess for most
1191 +    // cases.
1192 +    return 12.0;
1193 +  }
1194 +
1195 +
1196 +  void Electrostatic::ReciprocalSpaceSum(RealType& pot) {
1197      
1198 <  void Electrostatic::calcSelfCorrection(SelfCorrectionData scdat) {
1199 <    RealType mu1, preVal, chg1, self;
1198 >    RealType kPot = 0.0;
1199 >    RealType kVir = 0.0;
1200      
1201 <    if (!initialized_) initialize();
1201 >    const RealType mPoleConverter = 0.20819434; // converts from the
1202 >                                                // internal units of
1203 >                                                // Debye (for dipoles)
1204 >                                                // or Debye-angstroms
1205 >                                                // (for quadrupoles) to
1206 >                                                // electron angstroms or
1207 >                                                // electron-angstroms^2
1208      
1209 <    ElectrostaticAtomData data = ElectrostaticMap[scdat.atype];
1210 <  
1211 <    // logicals
1209 >    const RealType eConverter = 332.0637778; // convert the
1210 >                                             // Charge-Charge
1211 >                                             // electrostatic
1212 >                                             // interactions into kcal /
1213 >                                             // mol assuming distances
1214 >                                             // are measured in
1215 >                                             // angstroms.
1216  
1217 <    bool i_is_Charge = data.is_Charge;
1218 <    bool i_is_Dipole = data.is_Dipole;
1217 >    Mat3x3d hmat = info_->getSnapshotManager()->getCurrentSnapshot()->getHmat();
1218 >    Vector3d box = hmat.diagonals();
1219 >    RealType boxMax = box.max();
1220 >    
1221 >    //int kMax = int(2.0 * M_PI / (pow(dampingAlpha_,2)*cutoffRadius_ * boxMax) );
1222 >    int kMax = 7;
1223 >    int kSqMax = kMax*kMax + 2;
1224 >    
1225 >    int kLimit = kMax+1;
1226 >    int kLim2 = 2*kMax+1;
1227 >    int kSqLim = kSqMax;
1228 >    
1229 >    vector<RealType> AK(kSqLim+1, 0.0);
1230 >    RealType xcl = 2.0 * M_PI / box.x();
1231 >    RealType ycl = 2.0 * M_PI / box.y();
1232 >    RealType zcl = 2.0 * M_PI / box.z();
1233 >    RealType rcl = 2.0 * M_PI / boxMax;
1234 >    RealType rvol = 2.0 * M_PI /(box.x() * box.y() * box.z());
1235 >    
1236 >    if(dampingAlpha_ < 1.0e-12) return;
1237 >    
1238 >    RealType ralph = -0.25/pow(dampingAlpha_,2);
1239 >    
1240 >    // Calculate and store exponential factors  
1241 >    
1242 >    vector<vector<RealType> > elc;
1243 >    vector<vector<RealType> > emc;
1244 >    vector<vector<RealType> > enc;
1245 >    vector<vector<RealType> > els;
1246 >    vector<vector<RealType> > ems;
1247 >    vector<vector<RealType> > ens;
1248  
1249 <    if (summationMethod_ == esm_REACTION_FIELD) {
1250 <      if (i_is_Dipole) {
1251 <        mu1 = data.dipole_moment;          
1252 <        preVal = pre22_ * preRF2_ * mu1 * mu1;
1253 <        scdat.pot -= 0.5 * preVal;
1249 >    
1250 >    int nMax = info_->getNAtoms();
1251 >    
1252 >    elc.resize(kLimit+1);
1253 >    emc.resize(kLimit+1);
1254 >    enc.resize(kLimit+1);
1255 >    els.resize(kLimit+1);
1256 >    ems.resize(kLimit+1);
1257 >    ens.resize(kLimit+1);
1258 >
1259 >    for (int j = 0; j < kLimit+1; j++) {
1260 >      elc[j].resize(nMax);
1261 >      emc[j].resize(nMax);
1262 >      enc[j].resize(nMax);
1263 >      els[j].resize(nMax);
1264 >      ems[j].resize(nMax);
1265 >      ens[j].resize(nMax);
1266 >    }
1267 >    
1268 >    Vector3d t( 2.0 * M_PI );
1269 >    t.Vdiv(t, box);
1270 >
1271 >    
1272 >    SimInfo::MoleculeIterator mi;
1273 >    Molecule::AtomIterator ai;
1274 >    int i;
1275 >    Vector3d r;
1276 >    Vector3d tt;
1277 >    
1278 >    for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1279 >         mol = info_->nextMolecule(mi)) {
1280 >      for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1281 >          atom = mol->nextAtom(ai)) {  
1282          
1283 <        // The self-correction term adds into the reaction field vector
1284 <        Vector3d uz_i = scdat.eFrame.getColumn(2);
1285 <        Vector3d ei = preVal * uz_i;
1283 >        i = atom->getLocalIndex();
1284 >        r = atom->getPos();
1285 >        info_->getSnapshotManager()->getCurrentSnapshot()->wrapVector(r);
1286 >        
1287 >        tt.Vmul(t, r);
1288  
1289 <        // This looks very wrong.  A vector crossed with itself is zero.
1290 <        scdat.t -= cross(uz_i, ei);
1289 >        elc[1][i] = 1.0;
1290 >        emc[1][i] = 1.0;
1291 >        enc[1][i] = 1.0;
1292 >        els[1][i] = 0.0;
1293 >        ems[1][i] = 0.0;
1294 >        ens[1][i] = 0.0;
1295 >
1296 >        elc[2][i] = cos(tt.x());
1297 >        emc[2][i] = cos(tt.y());
1298 >        enc[2][i] = cos(tt.z());
1299 >        els[2][i] = sin(tt.x());
1300 >        ems[2][i] = sin(tt.y());
1301 >        ens[2][i] = sin(tt.z());
1302 >        
1303 >        for(int l = 3; l <= kLimit; l++) {
1304 >          elc[l][i]=elc[l-1][i]*elc[2][i]-els[l-1][i]*els[2][i];
1305 >          emc[l][i]=emc[l-1][i]*emc[2][i]-ems[l-1][i]*ems[2][i];
1306 >          enc[l][i]=enc[l-1][i]*enc[2][i]-ens[l-1][i]*ens[2][i];
1307 >          els[l][i]=els[l-1][i]*elc[2][i]+elc[l-1][i]*els[2][i];
1308 >          ems[l][i]=ems[l-1][i]*emc[2][i]+emc[l-1][i]*ems[2][i];
1309 >          ens[l][i]=ens[l-1][i]*enc[2][i]+enc[l-1][i]*ens[2][i];
1310 >        }
1311        }
1312 <    } else if (summationMethod_ == esm_SHIFTED_FORCE || summationMethod_ == esm_SHIFTED_POTENTIAL) {
1313 <      if (i_is_Charge) {        
1314 <        chg1 = data.charge;
1315 <        if (screeningMethod_ == DAMPED) {
1316 <          self = - 0.5 * (c1c_ + alphaPi_) * chg1 * (chg1 + scdat.skippedCharge) * pre11_;
1317 <        } else {        
1318 <          self = - 0.5 * rcuti_ * chg1 * (chg1 + scdat.skippedCharge) * pre11_;
1312 >    }
1313 >    
1314 >    // Calculate and store AK coefficients:
1315 >    
1316 >    RealType eksq = 1.0;
1317 >    RealType expf = 0.0;
1318 >    if (ralph < 0.0) expf = exp(ralph*rcl*rcl);
1319 >    for (i = 1; i <= kSqLim; i++) {
1320 >      RealType rksq = float(i)*rcl*rcl;
1321 >      eksq = expf*eksq;
1322 >      AK[i] = eConverter * eksq/rksq;
1323 >    }
1324 >    
1325 >    /*
1326 >     * Loop over all k vectors k = 2 pi (ll/Lx, mm/Ly, nn/Lz)
1327 >     * the values of ll, mm and nn are selected so that the symmetry of
1328 >     * reciprocal lattice is taken into account i.e. the following
1329 >     * rules apply.
1330 >     *
1331 >     * ll ranges over the values 0 to kMax only.
1332 >     *
1333 >     * mm ranges over 0 to kMax when ll=0 and over
1334 >     *            -kMax to kMax otherwise.
1335 >     * nn ranges over 1 to kMax when ll=mm=0 and over
1336 >     *            -kMax to kMax otherwise.
1337 >     *
1338 >     * Hence the result of the summation must be doubled at the end.    
1339 >     */
1340 >    
1341 >    std::vector<RealType> clm(nMax, 0.0);
1342 >    std::vector<RealType> slm(nMax, 0.0);
1343 >    std::vector<RealType> ckr(nMax, 0.0);
1344 >    std::vector<RealType> skr(nMax, 0.0);
1345 >    std::vector<RealType> ckc(nMax, 0.0);
1346 >    std::vector<RealType> cks(nMax, 0.0);
1347 >    std::vector<RealType> dkc(nMax, 0.0);
1348 >    std::vector<RealType> dks(nMax, 0.0);
1349 >    std::vector<RealType> qkc(nMax, 0.0);
1350 >    std::vector<RealType> qks(nMax, 0.0);
1351 >    std::vector<Vector3d> dxk(nMax, V3Zero);
1352 >    std::vector<Vector3d> qxk(nMax, V3Zero);
1353 >    RealType rl, rm, rn;
1354 >    Vector3d kVec;
1355 >    Vector3d Qk;
1356 >    Mat3x3d k2;
1357 >    RealType ckcs, ckss, dkcs, dkss, qkcs, qkss;
1358 >    int atid;
1359 >    ElectrostaticAtomData data;
1360 >    RealType C, dk, qk;
1361 >    Vector3d D;
1362 >    Mat3x3d  Q;
1363 >
1364 >    int mMin = kLimit;
1365 >    int nMin = kLimit + 1;
1366 >    for (int l = 1; l <= kLimit; l++) {
1367 >      int ll = l - 1;
1368 >      rl = xcl * float(ll);
1369 >      for (int mmm = mMin; mmm <= kLim2; mmm++) {
1370 >        int mm = mmm - kLimit;
1371 >        int m = abs(mm) + 1;
1372 >        rm = ycl * float(mm);
1373 >        // Set temporary products of exponential terms
1374 >        for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1375 >             mol = info_->nextMolecule(mi)) {
1376 >          for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1377 >              atom = mol->nextAtom(ai)) {
1378 >            
1379 >            i = atom->getLocalIndex();
1380 >            if(mm < 0) {
1381 >              clm[i]=elc[l][i]*emc[m][i]+els[l][i]*ems[m][i];
1382 >              slm[i]=els[l][i]*emc[m][i]-ems[m][i]*elc[l][i];
1383 >            } else {
1384 >              clm[i]=elc[l][i]*emc[m][i]-els[l][i]*ems[m][i];
1385 >              slm[i]=els[l][i]*emc[m][i]+ems[m][i]*elc[l][i];
1386 >            }
1387 >          }
1388          }
1389 <        scdat.pot += self;
1389 >        for (int nnn = nMin; nnn <= kLim2; nnn++) {
1390 >          int nn = nnn - kLimit;          
1391 >          int n = abs(nn) + 1;
1392 >          rn = zcl * float(nn);
1393 >          // Test on magnitude of k vector:
1394 >          int kk=ll*ll + mm*mm + nn*nn;
1395 >          if(kk <= kSqLim) {
1396 >            kVec = Vector3d(rl, rm, rn);
1397 >            k2 = outProduct(kVec, kVec);
1398 >            // Calculate exp(ikr) terms
1399 >            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1400 >                 mol = info_->nextMolecule(mi)) {
1401 >              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1402 >                  atom = mol->nextAtom(ai)) {
1403 >                i = atom->getLocalIndex();
1404 >                
1405 >                if (nn < 0) {
1406 >                  ckr[i]=clm[i]*enc[n][i]+slm[i]*ens[n][i];
1407 >                  skr[i]=slm[i]*enc[n][i]-clm[i]*ens[n][i];
1408 >
1409 >                } else {
1410 >                  ckr[i]=clm[i]*enc[n][i]-slm[i]*ens[n][i];
1411 >                  skr[i]=slm[i]*enc[n][i]+clm[i]*ens[n][i];
1412 >                }
1413 >              }
1414 >            }
1415 >            
1416 >            // Calculate scalar and vector products for each site:
1417 >            
1418 >            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1419 >                 mol = info_->nextMolecule(mi)) {
1420 >              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1421 >                  atom = mol->nextAtom(ai)) {
1422 >                i = atom->getLocalIndex();
1423 >                int atid = atom->getAtomType()->getIdent();
1424 >                data = ElectrostaticMap[Etids[atid]];
1425 >                              
1426 >                if (data.is_Charge) {
1427 >                  C = data.fixedCharge;
1428 >                  if (atom->isFluctuatingCharge()) C += atom->getFlucQPos();
1429 >                  ckc[i] = C * ckr[i];
1430 >                  cks[i] = C * skr[i];
1431 >                }
1432 >                
1433 >                if (data.is_Dipole) {
1434 >                  D = atom->getDipole() * mPoleConverter;
1435 >                  dk = dot(D, kVec);
1436 >                  dxk[i] = cross(D, kVec);
1437 >                  dkc[i] = dk * ckr[i];
1438 >                  dks[i] = dk * skr[i];
1439 >                }
1440 >                if (data.is_Quadrupole) {
1441 >                  Q = atom->getQuadrupole() * mPoleConverter;
1442 >                  Qk = Q * kVec;                  
1443 >                  qk = dot(kVec, Qk);
1444 >                  qxk[i] = -cross(kVec, Qk);
1445 >                  qkc[i] = qk * ckr[i];
1446 >                  qks[i] = qk * skr[i];
1447 >                }              
1448 >              }
1449 >            }
1450 >
1451 >            // calculate vector sums
1452 >            
1453 >            ckcs = std::accumulate(ckc.begin(),ckc.end(),0.0);
1454 >            ckss = std::accumulate(cks.begin(),cks.end(),0.0);
1455 >            dkcs = std::accumulate(dkc.begin(),dkc.end(),0.0);
1456 >            dkss = std::accumulate(dks.begin(),dks.end(),0.0);
1457 >            qkcs = std::accumulate(qkc.begin(),qkc.end(),0.0);
1458 >            qkss = std::accumulate(qks.begin(),qks.end(),0.0);
1459 >            
1460 > #ifdef IS_MPI
1461 >            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &ckcs, 1, MPI::REALTYPE,
1462 >                                      MPI::SUM);
1463 >            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &ckss, 1, MPI::REALTYPE,
1464 >                                      MPI::SUM);
1465 >            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &dkcs, 1, MPI::REALTYPE,
1466 >                                      MPI::SUM);
1467 >            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &dkss, 1, MPI::REALTYPE,
1468 >                                      MPI::SUM);
1469 >            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &qkcs, 1, MPI::REALTYPE,
1470 >                                      MPI::SUM);
1471 >            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &qkss, 1, MPI::REALTYPE,
1472 >                                      MPI::SUM);
1473 > #endif        
1474 >            
1475 >            // Accumulate potential energy and virial contribution:
1476 >
1477 >            kPot += 2.0 * rvol * AK[kk]*((ckss+dkcs-qkss)*(ckss+dkcs-qkss)
1478 >                                         + (ckcs-dkss-qkcs)*(ckcs-dkss-qkcs));
1479 >
1480 >            kVir += 2.0 * rvol  * AK[kk]*(ckcs*ckcs+ckss*ckss
1481 >                                          +4.0*(ckss*dkcs-ckcs*dkss)
1482 >                                          +3.0*(dkcs*dkcs+dkss*dkss)
1483 >                                          -6.0*(ckss*qkss+ckcs*qkcs)
1484 >                                          +8.0*(dkss*qkcs-dkcs*qkss)
1485 >                                          +5.0*(qkss*qkss+qkcs*qkcs));
1486 >            
1487 >            // Calculate force and torque for each site:
1488 >            
1489 >            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1490 >                 mol = info_->nextMolecule(mi)) {
1491 >              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1492 >                  atom = mol->nextAtom(ai)) {
1493 >                
1494 >                i = atom->getLocalIndex();
1495 >                atid = atom->getAtomType()->getIdent();
1496 >                data = ElectrostaticMap[Etids[atid]];
1497 >
1498 >                RealType qfrc = AK[kk]*((cks[i]+dkc[i]-qks[i])*(ckcs-dkss-qkcs)
1499 >                                     - (ckc[i]-dks[i]-qkc[i])*(ckss+dkcs-qkss));
1500 >                RealType qtrq1 = AK[kk]*(skr[i]*(ckcs-dkss-qkcs)
1501 >                                         -ckr[i]*(ckss+dkcs-qkss));
1502 >                RealType qtrq2 = 2.0*AK[kk]*(ckr[i]*(ckcs-dkss-qkcs)
1503 >                                            +skr[i]*(ckss+dkcs-qkss));
1504 >              
1505 >                atom->addFrc( 4.0 * rvol * qfrc * kVec );
1506 >                
1507 >                if (data.is_Dipole) {
1508 >                  atom->addTrq( 4.0 * rvol * qtrq1 * dxk[i] );
1509 >                }
1510 >                if (data.is_Quadrupole) {
1511 >                  atom->addTrq( 4.0 * rvol * qtrq2 * qxk[i] );
1512 >                }
1513 >              }
1514 >            }
1515 >          }
1516 >        }
1517 >        nMin = 1;
1518        }
1519 +      mMin = 1;
1520      }
1521 +    pot += kPot;  
1522    }
1070
1071  RealType Electrostatic::getSuggestedCutoffRadius(AtomType* at1, AtomType* at2) {
1072    // This seems to work moderately well as a default.  There's no
1073    // inherent scale for 1/r interactions that we can standardize.
1074    // 12 angstroms seems to be a reasonably good guess for most
1075    // cases.
1076    return 12.0;
1077  }
1523   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines