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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines