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 1505 by gezelter, Sun Oct 3 22:18:59 2010 UTC vs.
trunk/src/nonbonded/Electrostatic.cpp (file contents), Revision 1921 by gezelter, Thu Aug 1 18:23:07 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_ *= 2.0;
294 >      selfMult2_ *= 2.0;
295 >      selfMult4_ *= 2.0;
296 >    } else {
297 >      selfMult1_ -= b0c;
298 >      selfMult2_ += (db0c_2 + 2.0*db0c_1*ric) /  3.0;
299 >      selfMult4_ -= (db0c_4 + 4.0*db0c_3*ric) / 15.0;
300 >    }
301 >
302 >    // working variables for the splines:
303 >    RealType ri, ri2;
304 >    RealType b0, b1, b2, b3, b4, b5;
305 >    RealType db0_1, db0_2, db0_3, db0_4, db0_5;
306 >    RealType f, fc, f0;
307 >    RealType g, gc, g0, g1, g2, g3, g4;
308 >    RealType h, hc, h1, h2, h3, h4;
309 >    RealType s, sc, s2, s3, s4;
310 >    RealType t, tc, t3, t4;
311 >    RealType u, uc, u4;
312 >
313 >    // working variables for Taylor expansion:
314 >    RealType rmRc, rmRc2, rmRc3, rmRc4;
315 >
316 >    // Approximate using splines using a maximum of 0.1 Angstroms
317 >    // between points.
318 >    int nptest = int((cutoffRadius_ + 2.0) / 0.1);
319 >    np_ = (np_ > nptest) ? np_ : nptest;
320 >  
321 >    // Add a 2 angstrom safety window to deal with cutoffGroups that
322 >    // have charged atoms longer than the cutoffRadius away from each
323 >    // other.  Splining is almost certainly the best choice here.
324 >    // Direct calls to erfc would be preferrable if it is a very fast
325 >    // implementation.
326 >
327 >    RealType dx = (cutoffRadius_ + 2.0) / RealType(np_);
328 >
329 >    // Storage vectors for the computed functions    
330 >    vector<RealType> rv;
331 >    vector<RealType> v01v;
332 >    vector<RealType> v11v;
333 >    vector<RealType> v21v, v22v;
334 >    vector<RealType> v31v, v32v;
335 >    vector<RealType> v41v, v42v, v43v;
336 >
337 >    for (int i = 1; i < np_ + 1; i++) {
338 >      r = RealType(i) * dx;
339 >      rv.push_back(r);
340 >
341 >      ri = 1.0 / r;
342 >      ri2 = ri * ri;
343 >
344 >      r2 = r * r;
345 >      expTerm = exp(-a2 * r2);
346 >
347 >      // Taylor expansion factors (no need for factorials this way):
348 >      rmRc = r - cutoffRadius_;
349 >      rmRc2 = rmRc  * rmRc / 2.0;
350 >      rmRc3 = rmRc2 * rmRc / 3.0;
351 >      rmRc4 = rmRc3 * rmRc / 4.0;
352 >
353 >      // values of Smith's B_l functions at r:
354 >      if (screeningMethod_ == DAMPED) {            
355 >        b0 = erfc(dampingAlpha_ * r) * ri;
356 >        b1 = (      b0 +     2.0*a2     * expTerm * invArootPi) * ri2;
357 >        b2 = (3.0 * b1 + pow(2.0*a2, 2) * expTerm * invArootPi) * ri2;
358 >        b3 = (5.0 * b2 + pow(2.0*a2, 3) * expTerm * invArootPi) * ri2;
359 >        b4 = (7.0 * b3 + pow(2.0*a2, 4) * expTerm * invArootPi) * ri2;
360 >        b5 = (9.0 * b4 + pow(2.0*a2, 5) * expTerm * invArootPi) * ri2;
361        } else {
362 <        sprintf( painCave.errMsg, "Electrostatic::initialize has no Dielectric"
363 <                 " value!\n");
364 <        painCave.severity = OPENMD_ERROR;
362 >        b0 = ri;
363 >        b1 = (      b0) * ri2;
364 >        b2 = (3.0 * b1) * ri2;
365 >        b3 = (5.0 * b2) * ri2;
366 >        b4 = (7.0 * b3) * ri2;
367 >        b5 = (9.0 * b4) * ri2;
368 >      }
369 >                
370 >      // higher derivatives of B_0 at r:
371 >      db0_1 = -r * b1;
372 >      db0_2 =     -b1 + r2 * b2;
373 >      db0_3 =          3.0*r*b2   - r2*r*b3;
374 >      db0_4 =          3.0*b2   - 6.0*r2*b3     + r2*r2*b4;
375 >      db0_5 =                    -15.0*r*b3 + 10.0*r2*r*b4 - r2*r2*r*b5;
376 >
377 >      f = b0;
378 >      fc = b0c;
379 >      f0 = f - fc - rmRc*db0c_1;
380 >
381 >      g = db0_1;        
382 >      gc = db0c_1;
383 >      g0 = g - gc;
384 >      g1 = g0 - rmRc *db0c_2;
385 >      g2 = g1 - rmRc2*db0c_3;
386 >      g3 = g2 - rmRc3*db0c_4;
387 >      g4 = g3 - rmRc4*db0c_5;
388 >
389 >      h = db0_2;      
390 >      hc = db0c_2;
391 >      h1 = h - hc;
392 >      h2 = h1 - rmRc *db0c_3;
393 >      h3 = h2 - rmRc2*db0c_4;
394 >      h4 = h3 - rmRc3*db0c_5;
395 >
396 >      s = db0_3;      
397 >      sc = db0c_3;
398 >      s2 = s - sc;
399 >      s3 = s2 - rmRc *db0c_4;
400 >      s4 = s3 - rmRc2*db0c_5;
401 >
402 >      t = db0_4;      
403 >      tc = db0c_4;
404 >      t3 = t - tc;
405 >      t4 = t3 - rmRc *db0c_5;
406 >      
407 >      u = db0_5;        
408 >      uc = db0c_5;
409 >      u4 = u - uc;
410 >
411 >      // in what follows below, the various v functions are used for
412 >      // potentials and torques, while the w functions show up in the
413 >      // forces.
414 >
415 >      switch (summationMethod_) {
416 >      case esm_SHIFTED_FORCE:
417 >                
418 >        v01 = f - fc - rmRc*gc;
419 >        v11 = g - gc - rmRc*hc;
420 >        v21 = g*ri - gc*ric - rmRc*(hc - gc*ric)*ric;
421 >        v22 = h - g*ri - (hc - gc*ric) - rmRc*(sc - (hc - gc*ric)*ric);
422 >        v31 = (h-g*ri)*ri - (hc-gc*ric)*ric - rmRc*(sc-2.0*(hc-gc*ric)*ric)*ric;
423 >        v32 = (s - 3.0*(h-g*ri)*ri) - (sc - 3.0*(hc-gc*ric)*ric)
424 >          - rmRc*(tc - 3.0*(sc-2.0*(hc-gc*ric)*ric)*ric);
425 >        v41 = (h - g*ri)*ri2 - (hc - gc*ric)*ric2
426 >          - rmRc*(sc - 3.0*(hc-gc*ric)*ric)*ric2;
427 >        v42 = (s-3.0*(h-g*ri)*ri)*ri - (sc-3.0*(hc-gc*ric)*ric)*ric
428 >          - rmRc*(tc - (4.0*sc - 9.0*(hc - gc*ric)*ric)*ric)*ric;
429 >        
430 >        v43 = (t - 3.0*(2.0*s - 5.0*(h - g*ri)*ri)*ri)
431 >          - (tc - 3.0*(2.0*sc - 5.0*(hc - gc*ric)*ric)*ric)
432 >          - rmRc*(uc-3.0*(2.0*tc - (7.0*sc - 15.0*(hc - gc*ric)*ric)*ric)*ric);
433 >
434 >        dv01 = g - gc;
435 >        dv11 = h - hc;
436 >        dv21 = (h - g*ri)*ri - (hc - gc*ric)*ric;
437 >        dv22 = (s - (h - g*ri)*ri) - (sc - (hc - gc*ric)*ric);        
438 >        dv31 = (s - 2.0*(h-g*ri)*ri)*ri - (sc - 2.0*(hc-gc*ric)*ric)*ric;
439 >        dv32 = (t - 3.0*(s-2.0*(h-g*ri)*ri)*ri)
440 >          - (tc - 3.0*(sc-2.0*(hc-gc*ric)*ric)*ric);
441 >        dv41 = (s - 3.0*(h - g*ri)*ri)*ri2 - (sc - 3.0*(hc - gc*ric)*ric)*ric2;
442 >        dv42 = (t - (4.0*s - 9.0*(h-g*ri)*ri)*ri)*ri
443 >          - (tc - (4.0*sc - 9.0*(hc-gc*ric)*ric)*ric)*ric;
444 >        dv43 = (u - 3.0*(2.0*t - (7.0*s - 15.0*(h - g*ri)*ri)*ri)*ri)
445 >          - (uc - 3.0*(2.0*tc - (7.0*sc - 15.0*(hc - gc*ric)*ric)*ric)*ric);
446 >        
447 >        break;
448 >
449 >      case esm_TAYLOR_SHIFTED:
450 >        
451 >        v01 = f0;
452 >        v11 = g1;
453 >        v21 = g2 * ri;
454 >        v22 = h2 - v21;
455 >        v31 = (h3 - g3 * ri) * ri;
456 >        v32 = s3 - 3.0*v31;
457 >        v41 = (h4 - g4 * ri) * ri2;
458 >        v42 = s4 * ri - 3.0*v41;
459 >        v43 = t4 - 6.0*v42 - 3.0*v41;
460 >
461 >        dv01 = g0;
462 >        dv11 = h1;
463 >        dv21 = (h2 - g2*ri)*ri;
464 >        dv22 = (s2 - (h2 - g2*ri)*ri);
465 >        dv31 = (s3 - 2.0*(h3-g3*ri)*ri)*ri;
466 >        dv32 = (t3 - 3.0*(s3-2.0*(h3-g3*ri)*ri)*ri);
467 >        dv41 = (s4 - 3.0*(h4 - g4*ri)*ri)*ri2;
468 >        dv42 = (t4 - (4.0*s4 - 9.0*(h4-g4*ri)*ri)*ri)*ri;
469 >        dv43 = (u4 - 3.0*(2.0*t4 - (7.0*s4 - 15.0*(h4 - g4*ri)*ri)*ri)*ri);
470 >
471 >        break;
472 >
473 >      case esm_SHIFTED_POTENTIAL:
474 >
475 >        v01 = f - fc;
476 >        v11 = g - gc;
477 >        v21 = g*ri - gc*ric;
478 >        v22 = h - g*ri - (hc - gc*ric);
479 >        v31 = (h-g*ri)*ri - (hc-gc*ric)*ric;
480 >        v32 = (s - 3.0*(h-g*ri)*ri) - (sc - 3.0*(hc-gc*ric)*ric);
481 >        v41 = (h - g*ri)*ri2 - (hc - gc*ric)*ric2;
482 >        v42 = (s-3.0*(h-g*ri)*ri)*ri - (sc-3.0*(hc-gc*ric)*ric)*ric;        
483 >        v43 = (t - 3.0*(2.0*s - 5.0*(h - g*ri)*ri)*ri)
484 >          - (tc - 3.0*(2.0*sc - 5.0*(hc - gc*ric)*ric)*ric);
485 >
486 >        dv01 = g;
487 >        dv11 = h;
488 >        dv21 = (h - g*ri)*ri;
489 >        dv22 = (s - (h - g*ri)*ri);
490 >        dv31 = (s - 2.0*(h-g*ri)*ri)*ri;
491 >        dv32 = (t - 3.0*(s-2.0*(h-g*ri)*ri)*ri);
492 >        dv41 = (s - 3.0*(h - g*ri)*ri)*ri2;
493 >        dv42 = (t - (4.0*s - 9.0*(h-g*ri)*ri)*ri)*ri;
494 >        dv43 = (u - 3.0*(2.0*t - (7.0*s - 15.0*(h - g*ri)*ri)*ri)*ri);
495 >
496 >        break;
497 >
498 >      case esm_SWITCHING_FUNCTION:
499 >      case esm_HARD:
500 >      case esm_EWALD_FULL:
501 >
502 >        v01 = f;
503 >        v11 = g;
504 >        v21 = g*ri;
505 >        v22 = h - g*ri;
506 >        v31 = (h-g*ri)*ri;
507 >        v32 = (s - 3.0*(h-g*ri)*ri);
508 >        v41 = (h - g*ri)*ri2;
509 >        v42 = (s-3.0*(h-g*ri)*ri)*ri;        
510 >        v43 = (t - 3.0*(2.0*s - 5.0*(h - g*ri)*ri)*ri);
511 >
512 >        dv01 = g;
513 >        dv11 = h;
514 >        dv21 = (h - g*ri)*ri;
515 >        dv22 = (s - (h - g*ri)*ri);
516 >        dv31 = (s - 2.0*(h-g*ri)*ri)*ri;
517 >        dv32 = (t - 3.0*(s-2.0*(h-g*ri)*ri)*ri);
518 >        dv41 = (s - 3.0*(h - g*ri)*ri)*ri2;
519 >        dv42 = (t - (4.0*s - 9.0*(h-g*ri)*ri)*ri)*ri;
520 >        dv43 = (u - 3.0*(2.0*t - (7.0*s - 15.0*(h - g*ri)*ri)*ri)*ri);
521 >
522 >        break;
523 >
524 >      case esm_REACTION_FIELD:
525 >        
526 >        // following DL_POLY's lead for shifting the image charge potential:
527 >        f = b0 + preRF_ * r2;
528 >        fc = b0c + preRF_ * cutoffRadius_ * cutoffRadius_;
529 >
530 >        g = db0_1 + preRF_ * 2.0 * r;        
531 >        gc = db0c_1 + preRF_ * 2.0 * cutoffRadius_;
532 >
533 >        h = db0_2 + preRF_ * 2.0;
534 >        hc = db0c_2 + preRF_ * 2.0;
535 >
536 >        v01 = f - fc;
537 >        v11 = g - gc;
538 >        v21 = g*ri - gc*ric;
539 >        v22 = h - g*ri - (hc - gc*ric);
540 >        v31 = (h-g*ri)*ri - (hc-gc*ric)*ric;
541 >        v32 = (s - 3.0*(h-g*ri)*ri) - (sc - 3.0*(hc-gc*ric)*ric);
542 >        v41 = (h - g*ri)*ri2 - (hc - gc*ric)*ric2;
543 >        v42 = (s-3.0*(h-g*ri)*ri)*ri - (sc-3.0*(hc-gc*ric)*ric)*ric;        
544 >        v43 = (t - 3.0*(2.0*s - 5.0*(h - g*ri)*ri)*ri)
545 >          - (tc - 3.0*(2.0*sc - 5.0*(hc - gc*ric)*ric)*ric);
546 >
547 >        dv01 = g;
548 >        dv11 = h;
549 >        dv21 = (h - g*ri)*ri;
550 >        dv22 = (s - (h - g*ri)*ri);
551 >        dv31 = (s - 2.0*(h-g*ri)*ri)*ri;
552 >        dv32 = (t - 3.0*(s-2.0*(h-g*ri)*ri)*ri);
553 >        dv41 = (s - 3.0*(h - g*ri)*ri)*ri2;
554 >        dv42 = (t - (4.0*s - 9.0*(h-g*ri)*ri)*ri)*ri;
555 >        dv43 = (u - 3.0*(2.0*t - (7.0*s - 15.0*(h - g*ri)*ri)*ri)*ri);
556 >
557 >        break;
558 >                
559 >      case esm_EWALD_PME:
560 >      case esm_EWALD_SPME:
561 >      default :
562 >        map<string, ElectrostaticSummationMethod>::iterator i;
563 >        std::string meth;
564 >        for (i = summationMap_.begin(); i != summationMap_.end(); ++i) {
565 >          if ((*i).second == summationMethod_) meth = (*i).first;
566 >        }
567 >        sprintf( painCave.errMsg,
568 >                 "Electrostatic::initialize: electrostaticSummationMethod %s \n"
569 >                 "\thas not been implemented yet. Please select one of:\n"
570 >                 "\t\"hard\", \"shifted_potential\", or \"shifted_force\"\n",
571 >                 meth.c_str() );
572          painCave.isFatal = 1;
573          simError();
574 +        break;      
575        }
576 +
577 +      // Add these computed values to the storage vectors for spline creation:
578 +      v01v.push_back(v01);
579 +      v11v.push_back(v11);
580 +      v21v.push_back(v21);
581 +      v22v.push_back(v22);
582 +      v31v.push_back(v31);
583 +      v32v.push_back(v32);      
584 +      v41v.push_back(v41);
585 +      v42v.push_back(v42);
586 +      v43v.push_back(v43);
587      }
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;
588  
589 +    // construct the spline structures and fill them with the values we've
590 +    // computed:
591 +
592 +    v01s = new CubicSpline();
593 +    v01s->addPoints(rv, v01v);
594 +    v11s = new CubicSpline();
595 +    v11s->addPoints(rv, v11v);
596 +    v21s = new CubicSpline();
597 +    v21s->addPoints(rv, v21v);
598 +    v22s = new CubicSpline();
599 +    v22s->addPoints(rv, v22v);
600 +    v31s = new CubicSpline();
601 +    v31s->addPoints(rv, v31v);
602 +    v32s = new CubicSpline();
603 +    v32s->addPoints(rv, v32v);
604 +    v41s = new CubicSpline();
605 +    v41s->addPoints(rv, v41v);
606 +    v42s = new CubicSpline();
607 +    v42s->addPoints(rv, v42v);
608 +    v43s = new CubicSpline();
609 +    v43s->addPoints(rv, v43v);
610 +
611 +    haveElectroSplines_ = true;
612 +
613      initialized_ = true;
614    }
615        
616    void Electrostatic::addType(AtomType* atomType){
617 <
617 >    
618      ElectrostaticAtomData electrostaticAtomData;
619      electrostaticAtomData.is_Charge = false;
620      electrostaticAtomData.is_Dipole = false;
186    electrostaticAtomData.is_SplitDipole = false;
621      electrostaticAtomData.is_Quadrupole = false;
622 +    electrostaticAtomData.is_Fluctuating = false;
623  
624 <    if (atomType->isCharge()) {
190 <      GenericData* data = atomType->getPropertyByName("Charge");
624 >    FixedChargeAdapter fca = FixedChargeAdapter(atomType);
625  
626 <      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 <      }
626 >    if (fca.isFixedCharge()) {
627        electrostaticAtomData.is_Charge = true;
628 <      electrostaticAtomData.charge = doubleData->getData();          
628 >      electrostaticAtomData.fixedCharge = fca.getCharge();
629      }
630  
631 <    if (atomType->isDirectional()) {
632 <      DirectionalAtomType* daType = dynamic_cast<DirectionalAtomType*>(atomType);
633 <      
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 <        }
631 >    MultipoleAdapter ma = MultipoleAdapter(atomType);
632 >    if (ma.isMultipole()) {
633 >      if (ma.isDipole()) {
634          electrostaticAtomData.is_Dipole = true;
635 <        electrostaticAtomData.dipole_moment = doubleData->getData();
635 >        electrostaticAtomData.dipole = ma.getDipole();
636        }
637 <
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 <        // Quadrupoles in OpenMD are set as the diagonal elements
287 <        // of the diagonalized traceless quadrupole moment tensor.
288 <        // The column vectors of the unitary matrix that diagonalizes
289 <        // the quadrupole moment tensor become the eFrame (or the
290 <        // electrostatic version of the body-fixed frame.
291 <
292 <        Vector3dGenericData* v3dData = dynamic_cast<Vector3dGenericData*>(data);
293 <        if (v3dData == NULL) {
294 <          sprintf( painCave.errMsg,
295 <                   "Electrostatic::addType could not convert GenericData to "
296 <                   "Quadrupole Moments for\n"
297 <                   "\tatom type %s\n", daType->getName().c_str());
298 <          painCave.severity = OPENMD_ERROR;
299 <          painCave.isFatal = 1;
300 <          simError();          
301 <        }
637 >      if (ma.isQuadrupole()) {
638          electrostaticAtomData.is_Quadrupole = true;
639 <        electrostaticAtomData.quadrupole_moments = v3dData->getData();
639 >        electrostaticAtomData.quadrupole = ma.getQuadrupole();
640        }
641      }
642      
643 <    AtomTypeProperties atp = atomType->getATP();    
643 >    FluctuatingChargeAdapter fqa = FluctuatingChargeAdapter(atomType);
644  
645 <    pair<map<int,AtomType*>::iterator,bool> ret;    
646 <    ret = ElectrostaticList.insert( pair<int,AtomType*>(atp.ident, atomType) );
645 >    if (fqa.isFluctuatingCharge()) {
646 >      electrostaticAtomData.is_Fluctuating = true;
647 >      electrostaticAtomData.electronegativity = fqa.getElectronegativity();
648 >      electrostaticAtomData.hardness = fqa.getHardness();
649 >      electrostaticAtomData.slaterN = fqa.getSlaterN();
650 >      electrostaticAtomData.slaterZeta = fqa.getSlaterZeta();
651 >    }
652 >
653 >    int atid = atomType->getIdent();
654 >    int etid = Etypes.size();
655 >    int fqtid = FQtypes.size();
656 >
657 >    pair<set<int>::iterator,bool> ret;    
658 >    ret = Etypes.insert( atid );
659      if (ret.second == false) {
660        sprintf( painCave.errMsg,
661                 "Electrostatic already had a previous entry with ident %d\n",
662 <               atp.ident);
662 >               atid);
663        painCave.severity = OPENMD_INFO;
664        painCave.isFatal = 0;
665        simError();        
666      }
667      
668 <    ElectrostaticMap[atomType] = electrostaticAtomData;    
668 >    Etids[ atid ] = etid;
669 >    ElectrostaticMap.push_back(electrostaticAtomData);
670 >
671 >    if (electrostaticAtomData.is_Fluctuating) {
672 >      ret = FQtypes.insert( atid );
673 >      if (ret.second == false) {
674 >        sprintf( painCave.errMsg,
675 >                 "Electrostatic already had a previous fluctuating charge entry with ident %d\n",
676 >                 atid );
677 >        painCave.severity = OPENMD_INFO;
678 >        painCave.isFatal = 0;
679 >        simError();        
680 >      }
681 >      FQtids[atid] = fqtid;
682 >      Jij[fqtid].resize(nFlucq_);
683 >
684 >      // Now, iterate over all known fluctuating and add to the
685 >      // coulomb integral map:
686 >      
687 >      std::set<int>::iterator it;
688 >      for( it = FQtypes.begin(); it != FQtypes.end(); ++it) {    
689 >        int etid2 = Etids[ (*it) ];
690 >        int fqtid2 = FQtids[ (*it) ];
691 >        ElectrostaticAtomData eaData2 = ElectrostaticMap[ etid2 ];
692 >        RealType a = electrostaticAtomData.slaterZeta;
693 >        RealType b = eaData2.slaterZeta;
694 >        int m = electrostaticAtomData.slaterN;
695 >        int n = eaData2.slaterN;
696 >        
697 >        // Create the spline of the coulombic integral for s-type
698 >        // Slater orbitals.  Add a 2 angstrom safety window to deal
699 >        // with cutoffGroups that have charged atoms longer than the
700 >        // cutoffRadius away from each other.
701 >        
702 >        RealType rval;
703 >        RealType dr = (cutoffRadius_ + 2.0) / RealType(np_ - 1);
704 >        vector<RealType> rvals;
705 >        vector<RealType> Jvals;
706 >        // don't start at i = 0, as rval = 0 is undefined for the
707 >        // slater overlap integrals.
708 >        for (int i = 1; i < np_+1; i++) {
709 >          rval = RealType(i) * dr;
710 >          rvals.push_back(rval);
711 >          Jvals.push_back(sSTOCoulInt( a, b, m, n, rval *
712 >                                       PhysicalConstants::angstromToBohr ) *
713 >                          PhysicalConstants::hartreeToKcal );
714 >        }
715 >        
716 >        CubicSpline* J = new CubicSpline();
717 >        J->addPoints(rvals, Jvals);
718 >        Jij[fqtid][fqtid2] = J;
719 >        Jij[fqtid2].resize( nFlucq_ );
720 >        Jij[fqtid2][fqtid] = J;
721 >      }      
722 >    }      
723      return;
724    }
725    
726 <  void Electrostatic::setElectrostaticCutoffRadius( RealType theECR,
727 <                                                    RealType theRSW ) {
728 <    defaultCutoff_ = theECR;
327 <    rrf_ = defaultCutoff_;
328 <    rt_ = theRSW;
329 <    haveDefaultCutoff_ = true;
726 >  void Electrostatic::setCutoffRadius( RealType rCut ) {
727 >    cutoffRadius_ = rCut;
728 >    haveCutoffRadius_ = true;
729    }
730 +
731    void Electrostatic::setElectrostaticSummationMethod( ElectrostaticSummationMethod esm ) {
732      summationMethod_ = esm;
733    }
# Line 343 | Line 743 | namespace OpenMD {
743      haveDielectric_ = true;
744    }
745  
746 <  void Electrostatic::calcForce(InteractionData idat) {
746 >  void Electrostatic::calcForce(InteractionData &idat) {
747  
348    // utility variables.  Should clean these up and use the Vector3d and
349    // Mat3x3d to replace as many as we can in future versions:
350
351    RealType q_i, q_j, mu_i, mu_j, d_i, d_j;
352    RealType qxx_i, qyy_i, qzz_i;
353    RealType qxx_j, qyy_j, qzz_j;
354    RealType cx_i, cy_i, cz_i;
355    RealType cx_j, cy_j, cz_j;
356    RealType cx2, cy2, cz2;
357    RealType ct_i, ct_j, ct_ij, a1;
358    RealType riji, ri, ri2, ri3, ri4;
359    RealType pref, vterm, epot, dudr;
360    RealType scale, sc2;
361    RealType pot_term, preVal, rfVal;
362    RealType c2ri, c3ri, c4rij, cti3, ctj3, ctidotj;
363    RealType preSw, preSwSc;
364    RealType c1, c2, c3, c4;
365    RealType erfcVal, derfcVal;
366    RealType BigR;
367
368    Vector3d Q_i, Q_j;
369    Vector3d ux_i, uy_i, uz_i;
370    Vector3d ux_j, uy_j, uz_j;
371    Vector3d dudux_i, duduy_i, duduz_i;
372    Vector3d dudux_j, duduy_j, duduz_j;
373    Vector3d rhatdot2, rhatc4;
374    Vector3d dVdr;
375
376    pair<RealType, RealType> res;
377    
748      if (!initialized_) initialize();
749      
750 <    ElectrostaticAtomData data1 = ElectrostaticMap[idat.atype1];
751 <    ElectrostaticAtomData data2 = ElectrostaticMap[idat.atype2];
750 >    data1 = ElectrostaticMap[Etids[idat.atid1]];
751 >    data2 = ElectrostaticMap[Etids[idat.atid2]];
752 >
753 >    U = 0.0;  // Potential
754 >    F.zero();  // Force
755 >    Ta.zero(); // Torque on site a
756 >    Tb.zero(); // Torque on site b
757 >    Ea.zero(); // Electric field at site a
758 >    Eb.zero(); // Electric field at site b
759 >    dUdCa = 0.0; // fluctuating charge force at site a
760 >    dUdCb = 0.0; // fluctuating charge force at site a
761      
762 <    // some variables we'll need independent of electrostatic type:
762 >    // Indirect interactions mediated by the reaction field.
763 >    indirect_Pot = 0.0;   // Potential
764 >    indirect_F.zero();    // Force
765 >    indirect_Ta.zero();   // Torque on site a
766 >    indirect_Tb.zero();   // Torque on site b
767  
768 <    riji = 1.0 / idat.rij;
769 <    Vector3d rhat = idat.d  * riji;
768 >    // Excluded potential that is still computed for fluctuating charges
769 >    excluded_Pot= 0.0;
770  
388    // logicals
771  
772 <    bool i_is_Charge = data1.is_Charge;
391 <    bool i_is_Dipole = data1.is_Dipole;
392 <    bool i_is_SplitDipole = data1.is_SplitDipole;
393 <    bool i_is_Quadrupole = data1.is_Quadrupole;
772 >    // some variables we'll need independent of electrostatic type:
773  
774 <    bool j_is_Charge = data2.is_Charge;
775 <    bool j_is_Dipole = data2.is_Dipole;
397 <    bool j_is_SplitDipole = data2.is_SplitDipole;
398 <    bool j_is_Quadrupole = data2.is_Quadrupole;
399 <    
400 <    if (i_is_Charge)
401 <      q_i = data1.charge;
402 <
403 <    if (i_is_Dipole) {
404 <      mu_i = data1.dipole_moment;
405 <      uz_i = idat.eFrame1.getColumn(2);
774 >    ri = 1.0 /  *(idat.rij);
775 >    rhat =  *(idat.d)  * ri;
776        
777 <      ct_i = dot(uz_i, rhat);
777 >    // logicals
778  
779 <      if (i_is_SplitDipole)
780 <        d_i = data1.split_dipole_distance;
781 <      
782 <      duduz_i = V3Zero;
413 <    }
414 <    
415 <    if (i_is_Quadrupole) {
416 <      Q_i = data1.quadrupole_moments;
417 <      qxx_i = Q_i.x();
418 <      qyy_i = Q_i.y();
419 <      qzz_i = Q_i.z();
420 <      
421 <      ux_i = idat.eFrame1.getColumn(0);
422 <      uy_i = idat.eFrame1.getColumn(1);
423 <      uz_i = idat.eFrame1.getColumn(2);
779 >    a_is_Charge = data1.is_Charge;
780 >    a_is_Dipole = data1.is_Dipole;
781 >    a_is_Quadrupole = data1.is_Quadrupole;
782 >    a_is_Fluctuating = data1.is_Fluctuating;
783  
784 <      cx_i = dot(ux_i, rhat);
785 <      cy_i = dot(uy_i, rhat);
786 <      cz_i = dot(uz_i, rhat);
784 >    b_is_Charge = data2.is_Charge;
785 >    b_is_Dipole = data2.is_Dipole;
786 >    b_is_Quadrupole = data2.is_Quadrupole;
787 >    b_is_Fluctuating = data2.is_Fluctuating;
788  
789 <      dudux_i = V3Zero;
790 <      duduy_i = V3Zero;
791 <      duduz_i = V3Zero;
789 >    // Obtain all of the required radial function values from the
790 >    // spline structures:
791 >    
792 >    // needed for fields (and forces):
793 >    if (a_is_Charge || b_is_Charge) {
794 >      v01s->getValueAndDerivativeAt( *(idat.rij), v01, dv01);
795      }
796 +    if (a_is_Dipole || b_is_Dipole) {
797 +      v11s->getValueAndDerivativeAt( *(idat.rij), v11, dv11);
798 +      v11or = ri * v11;
799 +    }
800 +    if (a_is_Quadrupole || b_is_Quadrupole ||  (a_is_Dipole && b_is_Dipole)) {
801 +      v21s->getValueAndDerivativeAt( *(idat.rij), v21, dv21);
802 +      v22s->getValueAndDerivativeAt( *(idat.rij), v22, dv22);
803 +      v22or = ri * v22;
804 +    }      
805  
806 <    if (j_is_Charge)
807 <      q_j = data2.charge;
806 >    // needed for potentials (and forces and torques):
807 >    if ((a_is_Dipole && b_is_Quadrupole) ||
808 >        (b_is_Dipole && a_is_Quadrupole)) {
809 >      v31s->getValueAndDerivativeAt( *(idat.rij), v31, dv31);
810 >      v32s->getValueAndDerivativeAt( *(idat.rij), v32, dv32);
811 >      v31or = v31 * ri;
812 >      v32or = v32 * ri;
813 >    }
814 >    if (a_is_Quadrupole && b_is_Quadrupole) {
815 >      v41s->getValueAndDerivativeAt( *(idat.rij), v41, dv41);
816 >      v42s->getValueAndDerivativeAt( *(idat.rij), v42, dv42);
817 >      v43s->getValueAndDerivativeAt( *(idat.rij), v43, dv43);
818 >      v42or = v42 * ri;
819 >      v43or = v43 * ri;
820 >    }
821  
822 <    if (j_is_Dipole) {
823 <      mu_j = data2.dipole_moment;
824 <      uz_j = idat.eFrame2.getColumn(2);
822 >    // calculate the single-site contributions (fields, etc).
823 >    
824 >    if (a_is_Charge) {
825 >      C_a = data1.fixedCharge;
826        
827 <      ct_j = dot(uz_j, rhat);
828 <
829 <      if (j_is_SplitDipole)
444 <        d_j = data2.split_dipole_distance;
827 >      if (a_is_Fluctuating) {
828 >        C_a += *(idat.flucQ1);
829 >      }
830        
831 <      duduz_j = V3Zero;
831 >      if (idat.excluded) {
832 >        *(idat.skippedCharge2) += C_a;
833 >      } else {
834 >        // only do the field if we're not excluded:
835 >        Eb -= C_a *  pre11_ * dv01 * rhat;
836 >      }
837      }
838      
839 <    if (j_is_Quadrupole) {
840 <      Q_j = data2.quadrupole_moments;
841 <      qxx_j = Q_j.x();
842 <      qyy_j = Q_j.y();
843 <      qzz_j = Q_j.z();
844 <      
455 <      ux_j = idat.eFrame2.getColumn(0);
456 <      uy_j = idat.eFrame2.getColumn(1);
457 <      uz_j = idat.eFrame2.getColumn(2);
458 <
459 <      cx_j = dot(ux_j, rhat);
460 <      cy_j = dot(uy_j, rhat);
461 <      cz_j = dot(uz_j, rhat);
462 <
463 <      dudux_j = V3Zero;
464 <      duduy_j = V3Zero;
465 <      duduz_j = V3Zero;
839 >    if (a_is_Dipole) {
840 >      D_a = *(idat.dipole1);
841 >      rdDa = dot(rhat, D_a);
842 >      rxDa = cross(rhat, D_a);
843 >      if (!idat.excluded)
844 >        Eb -=  pre12_ * ((dv11-v11or) * rdDa * rhat + v11or * D_a);
845      }
846      
847 <    epot = 0.0;
848 <    dVdr = V3Zero;
847 >    if (a_is_Quadrupole) {
848 >      Q_a = *(idat.quadrupole1);
849 >      trQa =  Q_a.trace();
850 >      Qar =   Q_a * rhat;
851 >      rQa = rhat * Q_a;
852 >      rdQar = dot(rhat, Qar);
853 >      rxQar = cross(rhat, Qar);
854 >      if (!idat.excluded)
855 >        Eb -= pre14_ * (trQa * rhat * dv21 + 2.0 * Qar * v22or
856 >                        + rdQar * rhat * (dv22 - 2.0*v22or));
857 >    }
858      
859 <    if (i_is_Charge) {
859 >    if (b_is_Charge) {
860 >      C_b = data2.fixedCharge;
861        
862 <      if (j_is_Charge) {
863 <        if (screeningMethod_ == DAMPED) {
864 <          // assemble the damping variables
865 <          res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
866 <          erfcVal = res.first;
867 <          derfcVal = res.second;
868 <          c1 = erfcVal * riji;
869 <          c2 = (-derfcVal + c1) * riji;
481 <        } else {
482 <          c1 = riji;
483 <          c2 = c1 * riji;
484 <        }
485 <
486 <        preVal = idat.electroMult * pre11_ * q_i * q_j;
487 <        
488 <        if (summationMethod_ == SHIFTED_POTENTIAL) {
489 <          vterm = preVal * (c1 - c1c_);
490 <          dudr  = -idat.sw * preVal * c2;
491 <
492 <        } else if (summationMethod_ == SHIFTED_FORCE)  {
493 <          vterm = preVal * ( c1 - c1c_ + c2c_*(idat.rij - defaultCutoff_) );
494 <          dudr  = idat.sw * preVal * (c2c_ - c2);
495 <
496 <        } else if (summationMethod_ == REACTION_FIELD) {
497 <          rfVal = idat.electroMult * preRF_ * idat.rij * idat.rij;
498 <          vterm = preVal * ( riji + rfVal );            
499 <          dudr  = idat.sw * preVal * ( 2.0 * rfVal - riji ) * riji;
500 <
501 <        } else {
502 <          vterm = preVal * riji * erfcVal;            
503 <
504 <          dudr  = - idat.sw * preVal * c2;
505 <
506 <        }
507 <
508 <        idat.vpair += vterm;
509 <        epot += idat.sw * vterm;
510 <
511 <        dVdr += dudr * rhat;      
862 >      if (b_is_Fluctuating)
863 >        C_b += *(idat.flucQ2);
864 >      
865 >      if (idat.excluded) {
866 >        *(idat.skippedCharge1) += C_b;
867 >      } else {
868 >        // only do the field if we're not excluded:
869 >        Ea += C_b *  pre11_ * dv01 * rhat;
870        }
871 <
514 <      if (j_is_Dipole) {
515 <        // pref is used by all the possible methods
516 <        pref = idat.electroMult * pre12_ * q_i * mu_j;
517 <        preSw = idat.sw * pref;
518 <
519 <        if (summationMethod_ == REACTION_FIELD) {
520 <          ri2 = riji * riji;
521 <          ri3 = ri2 * riji;
871 >    }
872      
873 <          vterm = - pref * ct_j * ( ri2 - preRF2_ * idat.rij );
874 <          idat.vpair += vterm;
875 <          epot += idat.sw * vterm;
873 >    if (b_is_Dipole) {
874 >      D_b = *(idat.dipole2);
875 >      rdDb = dot(rhat, D_b);
876 >      rxDb = cross(rhat, D_b);
877 >      if (!idat.excluded)
878 >        Ea += pre12_ * ((dv11-v11or) * rdDb * rhat + v11or * D_b);
879 >    }
880 >    
881 >    if (b_is_Quadrupole) {
882 >      Q_b = *(idat.quadrupole2);
883 >      trQb =  Q_b.trace();
884 >      Qbr =   Q_b * rhat;
885 >      rQb = rhat * Q_b;
886 >      rdQbr = dot(rhat, Qbr);
887 >      rxQbr = cross(rhat, Qbr);
888 >      if (!idat.excluded)
889 >        Ea += pre14_ * (trQb * rhat * dv21 + 2.0 * Qbr * v22or
890 >                        + rdQbr * rhat * (dv22 - 2.0*v22or));
891 >    }
892 >    
893 >    if ((a_is_Fluctuating || b_is_Fluctuating) && idat.excluded) {
894 >      J = Jij[FQtids[idat.atid1]][FQtids[idat.atid2]];
895 >    }    
896 >    
897 >    if (a_is_Charge) {    
898 >      
899 >      if (b_is_Charge) {
900 >        pref =  pre11_ * *(idat.electroMult);      
901 >        U  += C_a * C_b * pref * v01;
902 >        F  += C_a * C_b * pref * dv01 * rhat;
903 >        
904 >        // If this is an excluded pair, there are still indirect
905 >        // interactions via the reaction field we must worry about:
906  
907 <          dVdr +=  -preSw * (ri3 * (uz_j - 3.0 * ct_j * rhat) - preRF2_*uz_j);
908 <          duduz_j += -preSw * rhat * (ri2 - preRF2_ * idat.rij);  
907 >        if (summationMethod_ == esm_REACTION_FIELD && idat.excluded) {
908 >          rfContrib = preRF_ * pref * C_a * C_b * *(idat.r2);
909 >          indirect_Pot += rfContrib;
910 >          indirect_F   += rfContrib * 2.0 * ri * rhat;
911 >        }
912 >        
913 >        // Fluctuating charge forces are handled via Coulomb integrals
914 >        // for excluded pairs (i.e. those connected via bonds) and
915 >        // with the standard charge-charge interaction otherwise.
916  
917 +        if (idat.excluded) {          
918 +          if (a_is_Fluctuating || b_is_Fluctuating) {
919 +            coulInt = J->getValueAt( *(idat.rij) );
920 +            if (a_is_Fluctuating)  dUdCa += coulInt * C_b;
921 +            if (b_is_Fluctuating)  dUdCb += coulInt * C_a;
922 +            excluded_Pot += C_a * C_b * coulInt;
923 +          }          
924          } else {
925 <          // determine the inverse r used if we have split dipoles
926 <          if (j_is_SplitDipole) {
927 <            BigR = sqrt(idat.r2 + 0.25 * d_j * d_j);
928 <            ri = 1.0 / BigR;
535 <            scale = idat.rij * ri;
536 <          } else {
537 <            ri = riji;
538 <            scale = 1.0;
539 <          }
540 <          
541 <          sc2 = scale * scale;
925 >          if (a_is_Fluctuating) dUdCa += C_b * pref * v01;
926 >          if (a_is_Fluctuating) dUdCb += C_a * pref * v01;
927 >        }
928 >      }
929  
930 <          if (screeningMethod_ == DAMPED) {
931 <            // assemble the damping variables
932 <            res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
933 <            erfcVal = res.first;
934 <            derfcVal = res.second;
548 <            c1 = erfcVal * ri;
549 <            c2 = (-derfcVal + c1) * ri;
550 <            c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * ri;
551 <          } else {
552 <            c1 = ri;
553 <            c2 = c1 * ri;
554 <            c3 = 3.0 * c2 * ri;
555 <          }
556 <            
557 <          c2ri = c2 * ri;
930 >      if (b_is_Dipole) {
931 >        pref =  pre12_ * *(idat.electroMult);        
932 >        U  += C_a * pref * v11 * rdDb;
933 >        F  += C_a * pref * ((dv11 - v11or) * rdDb * rhat + v11or * D_b);
934 >        Tb += C_a * pref * v11 * rxDb;
935  
936 <          // calculate the potential
560 <          pot_term =  scale * c2;
561 <          vterm = -pref * ct_j * pot_term;
562 <          idat.vpair += vterm;
563 <          epot += idat.sw * vterm;
564 <            
565 <          // calculate derivatives for forces and torques
936 >        if (a_is_Fluctuating) dUdCa += pref * v11 * rdDb;
937  
938 <          dVdr += -preSw * (uz_j * c2ri - ct_j * rhat * sc2 * c3);
939 <          duduz_j += -preSw * pot_term * rhat;
938 >        // Even if we excluded this pair from direct interactions, we
939 >        // still have the reaction-field-mediated charge-dipole
940 >        // interaction:
941  
942 +        if (summationMethod_ == esm_REACTION_FIELD && idat.excluded) {
943 +          rfContrib = C_a * pref * preRF_ * 2.0 * *(idat.rij);
944 +          indirect_Pot += rfContrib * rdDb;
945 +          indirect_F   += rfContrib * D_b / (*idat.rij);
946 +          indirect_Tb  += C_a * pref * preRF_ * rxDb;
947          }
948        }
949  
950 <      if (j_is_Quadrupole) {
951 <        // first precalculate some necessary variables
952 <        cx2 = cx_j * cx_j;
953 <        cy2 = cy_j * cy_j;
954 <        cz2 = cz_j * cz_j;
955 <        pref =  idat.electroMult * pre14_ * q_i * one_third_;
579 <          
580 <        if (screeningMethod_ == DAMPED) {
581 <          // assemble the damping variables
582 <          res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
583 <          erfcVal = res.first;
584 <          derfcVal = res.second;
585 <          c1 = erfcVal * riji;
586 <          c2 = (-derfcVal + c1) * riji;
587 <          c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * riji;
588 <          c4 = -4.0 * derfcVal * alpha4_ + 5.0 * c3 * riji * riji;
589 <        } else {
590 <          c1 = riji;
591 <          c2 = c1 * riji;
592 <          c3 = 3.0 * c2 * riji;
593 <          c4 = 5.0 * c3 * riji * riji;
594 <        }
950 >      if (b_is_Quadrupole) {
951 >        pref = pre14_ * *(idat.electroMult);
952 >        U  +=  C_a * pref * (v21 * trQb + v22 * rdQbr);
953 >        F  +=  C_a * pref * (trQb * dv21 * rhat + 2.0 * Qbr * v22or);
954 >        F  +=  C_a * pref * rdQbr * rhat * (dv22 - 2.0*v22or);
955 >        Tb +=  C_a * pref * 2.0 * rxQbr * v22;
956  
957 <        // precompute variables for convenience
597 <        preSw = idat.sw * pref;
598 <        c2ri = c2 * riji;
599 <        c3ri = c3 * riji;
600 <        c4rij = c4 * idat.rij;
601 <        rhatdot2 = 2.0 * rhat * c3;
602 <        rhatc4 = rhat * c4rij;
603 <
604 <        // calculate the potential
605 <        pot_term = ( qxx_j * (cx2*c3 - c2ri) +
606 <                     qyy_j * (cy2*c3 - c2ri) +
607 <                     qzz_j * (cz2*c3 - c2ri) );
608 <        vterm = pref * pot_term;
609 <        idat.vpair += vterm;
610 <        epot += idat.sw * vterm;
611 <                
612 <        // calculate derivatives for the forces and torques
613 <
614 <        dVdr += -preSw * ( qxx_j* (cx2*rhatc4 - (2.0*cx_j*ux_j + rhat)*c3ri) +
615 <                           qyy_j* (cy2*rhatc4 - (2.0*cy_j*uy_j + rhat)*c3ri) +
616 <                           qzz_j* (cz2*rhatc4 - (2.0*cz_j*uz_j + rhat)*c3ri));
617 <                          
618 <        dudux_j += preSw * qxx_j * cx_j * rhatdot2;
619 <        duduy_j += preSw * qyy_j * cy_j * rhatdot2;
620 <        duduz_j += preSw * qzz_j * cz_j * rhatdot2;
957 >        if (a_is_Fluctuating) dUdCa += pref * (v21 * trQb + v22 * rdQbr);
958        }
959      }
623    
624    if (i_is_Dipole) {
960  
961 <      if (j_is_Charge) {
627 <        // variables used by all the methods
628 <        pref = idat.electroMult * pre12_ * q_j * mu_i;
629 <        preSw = idat.sw * pref;
961 >    if (a_is_Dipole) {
962  
963 <        if (summationMethod_ == REACTION_FIELD) {
963 >      if (b_is_Charge) {
964 >        pref = pre12_ * *(idat.electroMult);
965  
966 <          ri2 = riji * riji;
967 <          ri3 = ri2 * riji;
966 >        U  -= C_b * pref * v11 * rdDa;
967 >        F  -= C_b * pref * ((dv11-v11or) * rdDa * rhat + v11or * D_a);
968 >        Ta -= C_b * pref * v11 * rxDa;
969  
970 <          vterm = pref * ct_i * ( ri2 - preRF2_ * idat.rij );
637 <          idat.vpair += vterm;
638 <          epot += idat.sw * vterm;
639 <          
640 <          dVdr += preSw * (ri3 * (uz_i - 3.0 * ct_i * rhat) - preRF2_ * uz_i);
641 <          
642 <          duduz_i += preSw * rhat * (ri2 - preRF2_ * idat.rij);
643 <            
644 <        } else {
645 <          
646 <          // determine inverse r if we are using split dipoles
647 <          if (i_is_SplitDipole) {
648 <            BigR = sqrt(idat.r2 + 0.25 * d_i * d_i);
649 <            ri = 1.0 / BigR;
650 <            scale = idat.rij * ri;
651 <          } else {
652 <            ri = riji;
653 <            scale = 1.0;
654 <          }
655 <          
656 <          sc2 = scale * scale;
657 <            
658 <          if (screeningMethod_ == DAMPED) {
659 <            // assemble the damping variables
660 <            res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
661 <            erfcVal = res.first;
662 <            derfcVal = res.second;
663 <            c1 = erfcVal * ri;
664 <            c2 = (-derfcVal + c1) * ri;
665 <            c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * ri;
666 <          } else {
667 <            c1 = ri;
668 <            c2 = c1 * ri;
669 <            c3 = 3.0 * c2 * ri;
670 <          }
671 <          
672 <          c2ri = c2 * ri;
673 <              
674 <          // calculate the potential
675 <          pot_term = c2 * scale;
676 <          vterm = pref * ct_i * pot_term;
677 <          idat.vpair += vterm;
678 <          epot += idat.sw * vterm;
970 >        if (b_is_Fluctuating) dUdCb -= pref * v11 * rdDa;
971  
972 <          // calculate derivatives for the forces and torques
973 <          dVdr += preSw * (uz_i * c2ri - ct_i * rhat * sc2 * c3);
974 <          duduz_i += preSw * pot_term * rhat;
972 >        // Even if we excluded this pair from direct interactions,
973 >        // we still have the reaction-field-mediated charge-dipole
974 >        // interaction:
975 >        if (summationMethod_ == esm_REACTION_FIELD && idat.excluded) {
976 >          rfContrib = C_b * pref * preRF_ * 2.0 * *(idat.rij);
977 >          indirect_Pot -= rfContrib * rdDa;
978 >          indirect_F   -= rfContrib * D_a / (*idat.rij);
979 >          indirect_Ta  -= C_b * pref * preRF_ * rxDa;
980          }
981        }
982  
983 <      if (j_is_Dipole) {
984 <        // variables used by all methods
985 <        ct_ij = dot(uz_i, uz_j);
983 >      if (b_is_Dipole) {
984 >        pref = pre22_ * *(idat.electroMult);
985 >        DadDb = dot(D_a, D_b);
986 >        DaxDb = cross(D_a, D_b);
987  
988 <        pref = idat.electroMult * pre22_ * mu_i * mu_j;
989 <        preSw = idat.sw * pref;
988 >        U  -= pref * (DadDb * v21 + rdDa * rdDb * v22);
989 >        F  -= pref * (dv21 * DadDb * rhat + v22or * (rdDb * D_a + rdDa * D_b));
990 >        F  -= pref * (rdDa * rdDb) * (dv22 - 2.0*v22or) * rhat;
991 >        Ta += pref * ( v21 * DaxDb - v22 * rdDb * rxDa);
992 >        Tb += pref * (-v21 * DaxDb - v22 * rdDa * rxDb);
993  
994 <        if (summationMethod_ == REACTION_FIELD) {
995 <          ri2 = riji * riji;
996 <          ri3 = ri2 * riji;
997 <          ri4 = ri2 * ri2;
994 >        // Even if we excluded this pair from direct interactions, we
995 >        // still have the reaction-field-mediated dipole-dipole
996 >        // interaction:
997 >        if (summationMethod_ == esm_REACTION_FIELD && idat.excluded) {
998 >          rfContrib = -pref * preRF_ * 2.0;
999 >          indirect_Pot += rfContrib * DadDb;
1000 >          indirect_Ta  += rfContrib * DaxDb;
1001 >          indirect_Tb  -= rfContrib * DaxDb;
1002 >        }
1003 >      }
1004  
1005 <          vterm = pref * ( ri3 * (ct_ij - 3.0 * ct_i * ct_j) -
1006 <                           preRF2_ * ct_ij );
1007 <          idat.vpair += vterm;
1008 <          epot += idat.sw * vterm;
1009 <            
703 <          a1 = 5.0 * ct_i * ct_j - ct_ij;
704 <            
705 <          dVdr += preSw * 3.0 * ri4 * (a1 * rhat - ct_i * uz_j - ct_j * uz_i);
1005 >      if (b_is_Quadrupole) {
1006 >        pref = pre24_ * *(idat.electroMult);
1007 >        DadQb = D_a * Q_b;
1008 >        DadQbr = dot(D_a, Qbr);
1009 >        DaxQbr = cross(D_a, Qbr);
1010  
1011 <          duduz_i += preSw * (ri3 * (uz_j - 3.0 * ct_j * rhat) - preRF2_*uz_j);
1012 <          duduz_j += preSw * (ri3 * (uz_i - 3.0 * ct_i * rhat) - preRF2_*uz_i);
1011 >        U  -= pref * ((trQb*rdDa + 2.0*DadQbr)*v31 + rdDa*rdQbr*v32);
1012 >        F  -= pref * (trQb*D_a + 2.0*DadQb) * v31or;
1013 >        F  -= pref * (trQb*rdDa + 2.0*DadQbr) * (dv31-v31or) * rhat;
1014 >        F  -= pref * (D_a*rdQbr + 2.0*rdDa*rQb) * v32or;
1015 >        F  -= pref * (rdDa * rdQbr * rhat * (dv32-3.0*v32or));
1016 >        Ta += pref * ((-trQb*rxDa + 2.0 * DaxQbr)*v31 - rxDa*rdQbr*v32);
1017 >        Tb += pref * ((2.0*cross(DadQb, rhat) - 2.0*DaxQbr)*v31
1018 >                      - 2.0*rdDa*rxQbr*v32);
1019 >      }
1020 >    }
1021  
1022 <        } else {
1023 <          
1024 <          if (i_is_SplitDipole) {
1025 <            if (j_is_SplitDipole) {
1026 <              BigR = sqrt(idat.r2 + 0.25 * d_i * d_i + 0.25 * d_j * d_j);
1027 <            } else {
1028 <              BigR = sqrt(idat.r2 + 0.25 * d_i * d_i);
717 <            }
718 <            ri = 1.0 / BigR;
719 <            scale = idat.rij * ri;
720 <          } else {
721 <            if (j_is_SplitDipole) {
722 <              BigR = sqrt(idat.r2 + 0.25 * d_j * d_j);
723 <              ri = 1.0 / BigR;
724 <              scale = idat.rij * ri;
725 <            } else {
726 <              ri = riji;
727 <              scale = 1.0;
728 <            }
729 <          }
730 <          if (screeningMethod_ == DAMPED) {
731 <            // assemble damping variables
732 <            res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
733 <            erfcVal = res.first;
734 <            derfcVal = res.second;
735 <            c1 = erfcVal * ri;
736 <            c2 = (-derfcVal + c1) * ri;
737 <            c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * ri;
738 <            c4 = -4.0 * derfcVal * alpha4_ + 5.0 * c3 * ri * ri;
739 <          } else {
740 <            c1 = ri;
741 <            c2 = c1 * ri;
742 <            c3 = 3.0 * c2 * ri;
743 <            c4 = 5.0 * c3 * ri * ri;
744 <          }
1022 >    if (a_is_Quadrupole) {
1023 >      if (b_is_Charge) {
1024 >        pref = pre14_ * *(idat.electroMult);
1025 >        U  += C_b * pref * (v21 * trQa + v22 * rdQar);
1026 >        F  += C_b * pref * (trQa * rhat * dv21 + 2.0 * Qar * v22or);
1027 >        F  += C_b * pref * rdQar * rhat * (dv22 - 2.0*v22or);
1028 >        Ta += C_b * pref * 2.0 * rxQar * v22;
1029  
1030 <          // precompute variables for convenience
1031 <          sc2 = scale * scale;
1032 <          cti3 = ct_i * sc2 * c3;
1033 <          ctj3 = ct_j * sc2 * c3;
1034 <          ctidotj = ct_i * ct_j * sc2;
1035 <          preSwSc = preSw * scale;
1036 <          c2ri = c2 * ri;
753 <          c3ri = c3 * ri;
754 <          c4rij = c4 * idat.rij;
1030 >        if (b_is_Fluctuating) dUdCb += pref * (v21 * trQa + v22 * rdQar);
1031 >      }
1032 >      if (b_is_Dipole) {
1033 >        pref = pre24_ * *(idat.electroMult);
1034 >        DbdQa = D_b * Q_a;
1035 >        DbdQar = dot(D_b, Qar);
1036 >        DbxQar = cross(D_b, Qar);
1037  
1038 <          // calculate the potential
1039 <          pot_term = (ct_ij * c2ri - ctidotj * c3);
1040 <          vterm = pref * pot_term;
1041 <          idat.vpair += vterm;
1042 <          epot += idat.sw * vterm;
1043 <
1044 <          // calculate derivatives for the forces and torques
1045 <          dVdr += preSwSc * ( ctidotj * rhat * c4rij  -
764 <                              (ct_i*uz_j + ct_j*uz_i + ct_ij*rhat) * c3ri);
765 <          
766 <          duduz_i += preSw * (uz_j * c2ri - ctj3 * rhat);
767 <          duduz_j += preSw * (uz_i * c2ri - cti3 * rhat);
768 <        }
1038 >        U  += pref * ((trQa*rdDb + 2.0*DbdQar)*v31 + rdDb*rdQar*v32);
1039 >        F  += pref * (trQa*D_b + 2.0*DbdQa) * v31or;
1040 >        F  += pref * (trQa*rdDb + 2.0*DbdQar) * (dv31-v31or) * rhat;
1041 >        F  += pref * (D_b*rdQar + 2.0*rdDb*rQa) * v32or;
1042 >        F  += pref * (rdDb * rdQar * rhat * (dv32-3.0*v32or));
1043 >        Ta += pref * ((-2.0*cross(DbdQa, rhat) + 2.0*DbxQar)*v31
1044 >                      + 2.0*rdDb*rxQar*v32);
1045 >        Tb += pref * ((trQa*rxDb - 2.0 * DbxQar)*v31 + rxDb*rdQar*v32);
1046        }
1047 <    }
1047 >      if (b_is_Quadrupole) {
1048 >        pref = pre44_ * *(idat.electroMult);  // yes
1049 >        QaQb = Q_a * Q_b;
1050 >        trQaQb = QaQb.trace();
1051 >        rQaQb = rhat * QaQb;
1052 >        QaQbr = QaQb * rhat;
1053 >        QaxQb = cross(Q_a, Q_b);
1054 >        rQaQbr = dot(rQa, Qbr);
1055 >        rQaxQbr = cross(rQa, Qbr);
1056 >        
1057 >        U  += pref * (trQa * trQb + 2.0 * trQaQb) * v41;
1058 >        U  += pref * (trQa * rdQbr + trQb * rdQar  + 4.0 * rQaQbr) * v42;
1059 >        U  += pref * (rdQar * rdQbr) * v43;
1060  
1061 <    if (i_is_Quadrupole) {
1062 <      if (j_is_Charge) {
1063 <        // precompute some necessary variables
775 <        cx2 = cx_i * cx_i;
776 <        cy2 = cy_i * cy_i;
777 <        cz2 = cz_i * cz_i;
1061 >        F  += pref * rhat * (trQa * trQb + 2.0 * trQaQb)*dv41;
1062 >        F  += pref*rhat*(trQa*rdQbr + trQb*rdQar + 4.0*rQaQbr)*(dv42-2.0*v42or);
1063 >        F  += pref * rhat * (rdQar * rdQbr)*(dv43 - 4.0*v43or);
1064  
1065 <        pref = idat.electroMult * pre14_ * q_j * one_third_;
1065 >        F  += pref * 2.0 * (trQb*rQa + trQa*rQb) * v42or;
1066 >        F  += pref * 4.0 * (rQaQb + QaQbr) * v42or;
1067 >        F  += pref * 2.0 * (rQa*rdQbr + rdQar*rQb) * v43or;
1068  
1069 <        if (screeningMethod_ == DAMPED) {
1070 <          // assemble the damping variables
1071 <          res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
1072 <          erfcVal = res.first;
1073 <          derfcVal = res.second;
786 <          c1 = erfcVal * riji;
787 <          c2 = (-derfcVal + c1) * riji;
788 <          c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * riji;
789 <          c4 = -4.0 * derfcVal * alpha4_ + 5.0 * c3 * riji * riji;
790 <        } else {
791 <          c1 = riji;
792 <          c2 = c1 * riji;
793 <          c3 = 3.0 * c2 * riji;
794 <          c4 = 5.0 * c3 * riji * riji;
795 <        }
796 <          
797 <        // precompute some variables for convenience
798 <        preSw = idat.sw * pref;
799 <        c2ri = c2 * riji;
800 <        c3ri = c3 * riji;
801 <        c4rij = c4 * idat.rij;
802 <        rhatdot2 = 2.0 * rhat * c3;
803 <        rhatc4 = rhat * c4rij;
1069 >        Ta += pref * (- 4.0 * QaxQb * v41);
1070 >        Ta += pref * (- 2.0 * trQb * cross(rQa, rhat)
1071 >                      + 4.0 * cross(rhat, QaQbr)
1072 >                      - 4.0 * rQaxQbr) * v42;
1073 >        Ta += pref * 2.0 * cross(rhat,Qar) * rdQbr * v43;
1074  
805        // calculate the potential
806        pot_term = ( qxx_i * (cx2 * c3 - c2ri) +
807                     qyy_i * (cy2 * c3 - c2ri) +
808                     qzz_i * (cz2 * c3 - c2ri) );
809        
810        vterm = pref * pot_term;
811        idat.vpair += vterm;
812        epot += idat.sw * vterm;
1075  
1076 <        // calculate the derivatives for the forces and torques
1076 >        Tb += pref * (+ 4.0 * QaxQb * v41);
1077 >        Tb += pref * (- 2.0 * trQa * cross(rQb, rhat)
1078 >                      - 4.0 * cross(rQaQb, rhat)
1079 >                      + 4.0 * rQaxQbr) * v42;
1080 >        // Possible replacement for line 2 above:
1081 >        //             + 4.0 * cross(rhat, QbQar)
1082  
1083 <        dVdr += -preSw * (qxx_i* (cx2*rhatc4 - (2.0*cx_i*ux_i + rhat)*c3ri) +
817 <                          qyy_i* (cy2*rhatc4 - (2.0*cy_i*uy_i + rhat)*c3ri) +
818 <                          qzz_i* (cz2*rhatc4 - (2.0*cz_i*uz_i + rhat)*c3ri));
1083 >        Tb += pref * 2.0 * cross(rhat,Qbr) * rdQar * v43;
1084  
820        dudux_i += preSw * qxx_i * cx_i *  rhatdot2;
821        duduy_i += preSw * qyy_i * cy_i *  rhatdot2;
822        duduz_i += preSw * qzz_i * cz_i *  rhatdot2;
1085        }
1086      }
1087  
1088 <    idat.pot += epot;
1089 <    idat.f1 += dVdr;
1090 <
829 <    if (i_is_Dipole || i_is_Quadrupole)
830 <      idat.t1 -= cross(uz_i, duduz_i);
831 <    if (i_is_Quadrupole) {
832 <      idat.t1 -= cross(ux_i, dudux_i);
833 <      idat.t1 -= cross(uy_i, duduy_i);
1088 >    if (idat.doElectricField) {
1089 >      *(idat.eField1) += Ea * *(idat.electroMult);
1090 >      *(idat.eField2) += Eb * *(idat.electroMult);
1091      }
1092  
1093 <    if (j_is_Dipole || j_is_Quadrupole)
1094 <      idat.t2 -= cross(uz_j, duduz_j);
838 <    if (j_is_Quadrupole) {
839 <      idat.t2 -= cross(uz_j, dudux_j);
840 <      idat.t2 -= cross(uz_j, duduy_j);
841 <    }
1093 >    if (a_is_Fluctuating) *(idat.dVdFQ1) += dUdCa * *(idat.sw);
1094 >    if (b_is_Fluctuating) *(idat.dVdFQ2) += dUdCb * *(idat.sw);
1095  
1096 <    return;
1097 <  }  
1096 >    if (!idat.excluded) {
1097 >      
1098 >      *(idat.vpair) += U;
1099 >      (*(idat.pot))[ELECTROSTATIC_FAMILY] += U * *(idat.sw);
1100 >      *(idat.f1) += F * *(idat.sw);
1101 >      
1102 >      if (a_is_Dipole || a_is_Quadrupole)
1103 >        *(idat.t1) += Ta * *(idat.sw);
1104  
1105 <  void Electrostatic::calcSkipCorrection(SkipCorrectionData skdat) {
1105 >      if (b_is_Dipole || b_is_Quadrupole)
1106 >        *(idat.t2) += Tb * *(idat.sw);
1107 >      
1108 >    } else {
1109 >
1110 >      // only accumulate the forces and torques resulting from the
1111 >      // indirect reaction field terms.
1112  
1113 <    if (!initialized_) initialize();
1113 >      *(idat.vpair) += indirect_Pot;      
1114 >      (*(idat.excludedPot))[ELECTROSTATIC_FAMILY] +=  excluded_Pot;
1115 >      (*(idat.pot))[ELECTROSTATIC_FAMILY] += *(idat.sw) * indirect_Pot;
1116 >      *(idat.f1) += *(idat.sw) * indirect_F;
1117 >      
1118 >      if (a_is_Dipole || a_is_Quadrupole)
1119 >        *(idat.t1) += *(idat.sw) * indirect_Ta;
1120 >            
1121 >      if (b_is_Dipole || b_is_Quadrupole)
1122 >        *(idat.t2) += *(idat.sw) * indirect_Tb;
1123 >    }
1124 >    return;
1125 >  }
1126      
1127 <    ElectrostaticAtomData data1 = ElectrostaticMap[skdat.atype1];
851 <    ElectrostaticAtomData data2 = ElectrostaticMap[skdat.atype2];
852 <    
853 <    // logicals
1127 >  void Electrostatic::calcSelfCorrection(SelfData &sdat) {
1128  
1129 <    bool i_is_Charge = data1.is_Charge;
856 <    bool i_is_Dipole = data1.is_Dipole;
1129 >    if (!initialized_) initialize();
1130  
1131 <    bool j_is_Charge = data2.is_Charge;
859 <    bool j_is_Dipole = data2.is_Dipole;
860 <
861 <    RealType q_i, q_j;
1131 >    ElectrostaticAtomData data = ElectrostaticMap[Etids[sdat.atid]];
1132      
1133 <    // The skippedCharge computation is needed by the real-space cutoff methods
1134 <    // (i.e. shifted force and shifted potential)
1133 >    // logicals
1134 >    bool i_is_Charge = data.is_Charge;
1135 >    bool i_is_Dipole = data.is_Dipole;
1136 >    bool i_is_Quadrupole = data.is_Quadrupole;
1137 >    bool i_is_Fluctuating = data.is_Fluctuating;
1138 >    RealType C_a = data.fixedCharge;  
1139 >    RealType self(0.0), preVal, DdD, trQ, trQQ;
1140  
1141 <    if (i_is_Charge) {
1142 <      q_i = data1.charge;
868 <      skdat.skippedCharge2 += q_i;
1141 >    if (i_is_Dipole) {
1142 >      DdD = data.dipole.lengthSquare();
1143      }
1144 <
1145 <    if (j_is_Charge) {
1146 <      q_j = data2.charge;
1147 <      skdat.skippedCharge1 += q_j;
1144 >        
1145 >    if (i_is_Fluctuating) {
1146 >      C_a += *(sdat.flucQ);
1147 >      // dVdFQ is really a force, so this is negative the derivative
1148 >      *(sdat.dVdFQ) -=  *(sdat.flucQ) * data.hardness + data.electronegativity;
1149 >      (*(sdat.excludedPot))[ELECTROSTATIC_FAMILY] += (*sdat.flucQ) *
1150 >        (*(sdat.flucQ) * data.hardness * 0.5 + data.electronegativity);
1151      }
1152  
1153 <    // the rest of this function should only be necessary for reaction field.
1154 <
878 <    if (summationMethod_ == REACTION_FIELD) {
879 <      RealType riji, ri2, ri3;
880 <      RealType q_i, mu_i, ct_i;
881 <      RealType q_j, mu_j, ct_j;
882 <      RealType preVal, rfVal, vterm, dudr, pref, myPot;
883 <      Vector3d dVdr, uz_i, uz_j, duduz_i, duduz_j, rhat;
884 <
885 <      // some variables we'll need independent of electrostatic type:
1153 >    switch (summationMethod_) {
1154 >    case esm_REACTION_FIELD:
1155        
887      riji = 1.0 / skdat.rij;
888      rhat = skdat.d  * riji;
889
890      if (i_is_Dipole) {
891        mu_i = data1.dipole_moment;
892        uz_i = skdat.eFrame1.getColumn(2);      
893        ct_i = dot(uz_i, rhat);
894        duduz_i = V3Zero;
895      }
896            
897      if (j_is_Dipole) {
898        mu_j = data2.dipole_moment;
899        uz_j = skdat.eFrame2.getColumn(2);      
900        ct_j = dot(uz_j, rhat);
901        duduz_j = V3Zero;
902      }
903    
1156        if (i_is_Charge) {
1157 <        if (j_is_Charge) {
1158 <          preVal = skdat.electroMult * pre11_ * q_i * q_j;
1159 <          rfVal = preRF_ * skdat.rij * skdat.rij;
1160 <          vterm = preVal * rfVal;
1161 <          myPot += skdat.sw * vterm;        
910 <          dudr  = skdat.sw * preVal * 2.0 * rfVal * riji;        
911 <          dVdr += dudr * rhat;
912 <        }
913 <        
914 <        if (j_is_Dipole) {
915 <          ri2 = riji * riji;
916 <          ri3 = ri2 * riji;        
917 <          pref = skdat.electroMult * pre12_ * q_i * mu_j;
918 <          vterm = - pref * ct_j * ( ri2 - preRF2_ * skdat.rij );
919 <          myPot += skdat.sw * vterm;        
920 <          dVdr += -skdat.sw * pref * ( ri3 * ( uz_j - 3.0 * ct_j * rhat) - preRF2_ * uz_j);
921 <          duduz_j += -skdat.sw * pref * rhat * (ri2 - preRF2_ * skdat.rij);
922 <        }
1157 >        // Self potential [see Wang and Hermans, "Reaction Field
1158 >        // Molecular Dynamics Simulation with Friedman’s Image Charge
1159 >        // Method," J. Phys. Chem. 99, 12001-12007 (1995).]
1160 >        preVal = pre11_ * preRF_ * C_a * C_a;
1161 >        (*(sdat.pot))[ELECTROSTATIC_FAMILY] -= 0.5 * preVal / cutoffRadius_;
1162        }
1163 +
1164        if (i_is_Dipole) {
1165 <        if (j_is_Charge) {
926 <          ri2 = riji * riji;
927 <          ri3 = ri2 * riji;        
928 <          pref = skdat.electroMult * pre12_ * q_j * mu_i;
929 <          vterm = - pref * ct_i * ( ri2 - preRF2_ * skdat.rij );
930 <          myPot += skdat.sw * vterm;        
931 <          dVdr += skdat.sw * pref * ( ri3 * ( uz_i - 3.0 * ct_i * rhat) - preRF2_ * uz_i);      
932 <          duduz_i += skdat.sw * pref * rhat * (ri2 - preRF2_ * skdat.rij);
933 <        }
1165 >        (*(sdat.pot))[ELECTROSTATIC_FAMILY] -= pre22_ * preRF_ * DdD;
1166        }
1167        
1168 <      // accumulate the forces and torques resulting from the self term
937 <      skdat.pot += myPot;
938 <      skdat.f1 += dVdr;
1168 >      break;
1169        
1170 +    case esm_SHIFTED_FORCE:
1171 +    case esm_SHIFTED_POTENTIAL:
1172 +    case esm_TAYLOR_SHIFTED:
1173 +    case esm_EWALD_FULL:
1174 +      if (i_is_Charge)
1175 +        self += selfMult1_ * pre11_ * C_a * (C_a + *(sdat.skippedCharge));      
1176        if (i_is_Dipole)
1177 <        skdat.t1 -= cross(uz_i, duduz_i);
1178 <      if (j_is_Dipole)
1179 <        skdat.t2 -= cross(uz_j, duduz_j);
1177 >        self += selfMult2_ * pre22_ * DdD;      
1178 >      if (i_is_Quadrupole) {
1179 >        trQ = data.quadrupole.trace();
1180 >        trQQ = (data.quadrupole * data.quadrupole).trace();
1181 >        self += selfMult4_ * pre44_ * (2.0*trQQ + trQ*trQ);
1182 >        if (i_is_Charge)
1183 >          self -= selfMult2_ * pre14_ * 2.0 * C_a * trQ;
1184 >      }
1185 >      (*(sdat.pot))[ELECTROSTATIC_FAMILY] += self;      
1186 >      break;
1187 >    default:
1188 >      break;
1189      }
1190    }
1191 +  
1192 +  RealType Electrostatic::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) {
1193 +    // This seems to work moderately well as a default.  There's no
1194 +    // inherent scale for 1/r interactions that we can standardize.
1195 +    // 12 angstroms seems to be a reasonably good guess for most
1196 +    // cases.
1197 +    return 12.0;
1198 +  }
1199 +
1200 +
1201 +  void Electrostatic::ReciprocalSpaceSum(potVec& pot) {
1202      
1203 <  void Electrostatic::calcSelfCorrection(SelfCorrectionData scdat) {
1204 <    RealType mu1, preVal, chg1, self;
1203 >    RealType kPot = 0.0;
1204 >    RealType kVir = 0.0;
1205      
1206 <    if (!initialized_) initialize();
1206 >    const RealType mPoleConverter = 0.20819434; // converts from the
1207 >                                                // internal units of
1208 >                                                // Debye (for dipoles)
1209 >                                                // or Debye-angstroms
1210 >                                                // (for quadrupoles) to
1211 >                                                // electron angstroms or
1212 >                                                // electron-angstroms^2
1213      
1214 <    ElectrostaticAtomData data = ElectrostaticMap[scdat.atype];
1215 <  
1216 <    // logicals
1214 >    const RealType eConverter = 332.0637778; // convert the
1215 >                                             // Charge-Charge
1216 >                                             // electrostatic
1217 >                                             // interactions into kcal /
1218 >                                             // mol assuming distances
1219 >                                             // are measured in
1220 >                                             // angstroms.
1221  
1222 <    bool i_is_Charge = data.is_Charge;
1223 <    bool i_is_Dipole = data.is_Dipole;
1224 <
1225 <    if (summationMethod_ == REACTION_FIELD) {
1226 <      if (i_is_Dipole) {
1227 <        mu1 = data.dipole_moment;          
1228 <        preVal = pre22_ * preRF2_ * mu1 * mu1;
1229 <        scdat.pot -= 0.5 * preVal;
1222 >    Mat3x3d hmat = info_->getSnapshotManager()->getCurrentSnapshot()->getHmat();
1223 >    Vector3d box = hmat.diagonals();
1224 >    RealType boxMax = box.max();
1225 >    
1226 >    cerr << "da = " << dampingAlpha_ << " rc = " << cutoffRadius_ << "\n";
1227 >    cerr << "boxMax = " << boxMax << "\n";
1228 >    //int kMax = int(2.0 * M_PI / (pow(dampingAlpha_,2)*cutoffRadius_ * boxMax) );
1229 >    int kMax = 5;
1230 >    cerr << "kMax = " << kMax << "\n";
1231 >    int kSqMax = kMax*kMax + 2;
1232 >    
1233 >    int kLimit = kMax+1;
1234 >    int kLim2 = 2*kMax+1;
1235 >    int kSqLim = kSqMax;
1236 >    
1237 >    vector<RealType> AK(kSqLim+1, 0.0);
1238 >    RealType xcl = 2.0 * M_PI / box.x();
1239 >    RealType ycl = 2.0 * M_PI / box.y();
1240 >    RealType zcl = 2.0 * M_PI / box.z();
1241 >    RealType rcl = 2.0 * M_PI / boxMax;
1242 >    RealType rvol = 2.0 * M_PI /(box.x() * box.y() * box.z());
1243 >    
1244 >    if(dampingAlpha_ < 1.0e-12) return;
1245 >    
1246 >    RealType ralph = -0.25/pow(dampingAlpha_,2);
1247 >    
1248 >    // Calculate and store exponential factors  
1249 >    
1250 >    vector<vector<Vector3d> > eCos;
1251 >    vector<vector<Vector3d> > eSin;
1252 >    
1253 >    int nMax = info_->getNAtoms();
1254 >    
1255 >    eCos.resize(kLimit+1);
1256 >    eSin.resize(kLimit+1);
1257 >    for (int j = 0; j < kLimit+1; j++) {
1258 >      eCos[j].resize(nMax);
1259 >      eSin[j].resize(nMax);
1260 >    }
1261 >    
1262 >    Vector3d t( 2.0 * M_PI );
1263 >    t.Vdiv(t, box);
1264 >    
1265 >    SimInfo::MoleculeIterator mi;
1266 >    Molecule::AtomIterator ai;
1267 >    int i;
1268 >    Vector3d r;
1269 >    Vector3d tt;
1270 >    Vector3d w;
1271 >    Vector3d u;
1272 >    Vector3d a;
1273 >    Vector3d b;
1274 >    
1275 >    for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1276 >         mol = info_->nextMolecule(mi)) {
1277 >      for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1278 >          atom = mol->nextAtom(ai)) {  
1279          
1280 <        // The self-correction term adds into the reaction field vector
1281 <        Vector3d uz_i = scdat.eFrame.getColumn(2);
1282 <        Vector3d ei = preVal * uz_i;
1280 >        i = atom->getLocalIndex();
1281 >        r = atom->getPos();
1282 >        info_->getSnapshotManager()->getCurrentSnapshot()->wrapVector(r);
1283 >        
1284 >        // Shift so that all coordinates are in the range [0,L]:
1285  
1286 <        // This looks very wrong.  A vector crossed with itself is zero.
1287 <        scdat.t -= cross(uz_i, ei);
1286 >        r += box/2.0;
1287 >
1288 >        tt.Vmul(t, r);
1289 >
1290 >        //cerr << "tt = " << tt << "\n";
1291 >        
1292 >        eCos[1][i] = Vector3d(1.0, 1.0, 1.0);
1293 >        eSin[1][i] = Vector3d(0.0, 0.0, 0.0);
1294 >        eCos[2][i] = Vector3d(cos(tt.x()), cos(tt.y()), cos(tt.z()));
1295 >        eSin[2][i] = Vector3d(sin(tt.x()), sin(tt.y()), sin(tt.z()));
1296 >        u = eCos[2][i];
1297 >        w = eSin[2][i];
1298 >        
1299 >        for(int l = 3; l <= kLimit; l++) {
1300 >          a.Vmul(eCos[l-1][i], u);
1301 >          b.Vmul(eSin[l-1][i], w);
1302 >          eCos[l][i] = a - b;
1303 >          a.Vmul(eSin[l-1][i], u);
1304 >          b.Vmul(eCos[l-1][i], w);
1305 >          eSin[l][i] = a + b;
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 >                
1496 >                atom->addFrc( 4.0 * rvol * qfrc * kVec );
1497 >                
1498 >                if (data.is_Dipole) {
1499 >                  atom->addTrq( 4.0 * rvol * qtrq1 * dxk[i] );
1500 >                }
1501 >                if (data.is_Quadrupole) {
1502 >                  atom->addTrq( 4.0 * rvol * qtrq2 * qxk[i] );
1503 >                }
1504 >              }
1505 >            }
1506 >          }
1507 >        }
1508        }
1509      }
1510 +    cerr << "kPot = " << kPot << "\n";
1511 +    pot[ELECTROSTATIC_FAMILY] += kPot;  
1512    }
984
985  RealType Electrostatic::getSuggestedCutoffRadius(AtomType* at1, AtomType* at2) {
986    // This seems to work moderately well as a default.  There's no
987    // inherent scale for 1/r interactions that we can standardize.
988    // 12 angstroms seems to be a reasonably good guess for most
989    // cases.
990    return 12.0;
991  }
1513   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines