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

Comparing branches/development/src/nonbonded/Electrostatic.cpp (file contents):
Revision 1723 by gezelter, Thu May 24 20:59:54 2012 UTC vs.
Revision 1850 by gezelter, Wed Feb 20 15:39:39 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines