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 1504 by gezelter, Sat Oct 2 20:41:53 2010 UTC vs.
Revision 1616 by gezelter, Tue Aug 30 15:45:35 2011 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).          
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).                        
40   */
# Line 47 | Line 47
47   #include "utils/simError.h"
48   #include "types/NonBondedInteractionType.hpp"
49   #include "types/DirectionalAtomType.hpp"
50 + #include "io/Globals.hpp"
51  
51
52   namespace OpenMD {
53    
54    Electrostatic::Electrostatic(): name_("Electrostatic"), initialized_(false),
55 <                                  forceField_(NULL) {}
55 >                                  forceField_(NULL), info_(NULL),
56 >                                  haveCutoffRadius_(false),
57 >                                  haveDampingAlpha_(false),
58 >                                  haveDielectric_(false),
59 >                                  haveElectroSpline_(false)
60 >  {}
61    
62    void Electrostatic::initialize() {
63 +    
64 +    Globals* simParams_ = info_->getSimParams();
65 +
66 +    summationMap_["HARD"]               = esm_HARD;
67 +    summationMap_["NONE"]               = esm_HARD;
68 +    summationMap_["SWITCHING_FUNCTION"] = esm_SWITCHING_FUNCTION;
69 +    summationMap_["SHIFTED_POTENTIAL"]  = esm_SHIFTED_POTENTIAL;
70 +    summationMap_["SHIFTED_FORCE"]      = esm_SHIFTED_FORCE;    
71 +    summationMap_["REACTION_FIELD"]     = esm_REACTION_FIELD;    
72 +    summationMap_["EWALD_FULL"]         = esm_EWALD_FULL;        
73 +    summationMap_["EWALD_PME"]          = esm_EWALD_PME;        
74 +    summationMap_["EWALD_SPME"]         = esm_EWALD_SPME;        
75 +    screeningMap_["DAMPED"]             = DAMPED;
76 +    screeningMap_["UNDAMPED"]           = UNDAMPED;
77 +
78      // these prefactors convert the multipole interactions into kcal / mol
79      // all were computed assuming distances are measured in angstroms
80      // Charge-Charge, assuming charges are measured in electrons
# Line 79 | Line 99 | namespace OpenMD {
99      
100      // variables to handle different summation methods for long-range
101      // electrostatics:
102 <    summationMethod_ = NONE;    
102 >    summationMethod_ = esm_HARD;    
103      screeningMethod_ = UNDAMPED;
104      dielectric_ = 1.0;
105      one_third_ = 1.0 / 3.0;
86    haveDefaultCutoff_ = false;
87    haveDampingAlpha_ = false;
88    haveDielectric_ = false;  
89    haveElectroSpline_ = false;
106    
107 +    // check the summation method:
108 +    if (simParams_->haveElectrostaticSummationMethod()) {
109 +      string myMethod = simParams_->getElectrostaticSummationMethod();
110 +      toUpper(myMethod);
111 +      map<string, ElectrostaticSummationMethod>::iterator i;
112 +      i = summationMap_.find(myMethod);
113 +      if ( i != summationMap_.end() ) {
114 +        summationMethod_ = (*i).second;
115 +      } else {
116 +        // throw error
117 +        sprintf( painCave.errMsg,
118 +                 "Electrostatic::initialize: Unknown electrostaticSummationMethod.\n"
119 +                 "\t(Input file specified %s .)\n"
120 +                 "\telectrostaticSummationMethod must be one of: \"hard\",\n"
121 +                 "\t\"shifted_potential\", \"shifted_force\", or \n"
122 +                 "\t\"reaction_field\".\n", myMethod.c_str() );
123 +        painCave.isFatal = 1;
124 +        simError();
125 +      }
126 +    } else {
127 +      // set ElectrostaticSummationMethod to the cutoffMethod:
128 +      if (simParams_->haveCutoffMethod()){
129 +        string myMethod = simParams_->getCutoffMethod();
130 +        toUpper(myMethod);
131 +        map<string, ElectrostaticSummationMethod>::iterator i;
132 +        i = summationMap_.find(myMethod);
133 +        if ( i != summationMap_.end() ) {
134 +          summationMethod_ = (*i).second;
135 +        }
136 +      }
137 +    }
138 +    
139 +    if (summationMethod_ == esm_REACTION_FIELD) {        
140 +      if (!simParams_->haveDielectric()) {
141 +        // throw warning
142 +        sprintf( painCave.errMsg,
143 +                 "SimInfo warning: dielectric was not specified in the input file\n\tfor the reaction field correction method.\n"
144 +                 "\tA default value of %f will be used for the dielectric.\n", dielectric_);
145 +        painCave.isFatal = 0;
146 +        painCave.severity = OPENMD_INFO;
147 +        simError();
148 +      } else {
149 +        dielectric_ = simParams_->getDielectric();      
150 +      }
151 +      haveDielectric_ = true;
152 +    }
153 +    
154 +    if (simParams_->haveElectrostaticScreeningMethod()) {
155 +      string myScreen = simParams_->getElectrostaticScreeningMethod();
156 +      toUpper(myScreen);
157 +      map<string, ElectrostaticScreeningMethod>::iterator i;
158 +      i = screeningMap_.find(myScreen);
159 +      if ( i != screeningMap_.end()) {
160 +        screeningMethod_ = (*i).second;
161 +      } else {
162 +        sprintf( painCave.errMsg,
163 +                 "SimInfo error: Unknown electrostaticScreeningMethod.\n"
164 +                 "\t(Input file specified %s .)\n"
165 +                 "\telectrostaticScreeningMethod must be one of: \"undamped\"\n"
166 +                 "or \"damped\".\n", myScreen.c_str() );
167 +        painCave.isFatal = 1;
168 +        simError();
169 +      }
170 +    }
171 +
172 +    // check to make sure a cutoff value has been set:
173 +    if (!haveCutoffRadius_) {
174 +      sprintf( painCave.errMsg, "Electrostatic::initialize has no Default "
175 +               "Cutoff value!\n");
176 +      painCave.severity = OPENMD_ERROR;
177 +      painCave.isFatal = 1;
178 +      simError();
179 +    }
180 +          
181 +    if (screeningMethod_ == DAMPED) {      
182 +      if (!simParams_->haveDampingAlpha()) {
183 +        // first set a cutoff dependent alpha value
184 +        // we assume alpha depends linearly with rcut from 0 to 20.5 ang
185 +        dampingAlpha_ = 0.425 - cutoffRadius_* 0.02;
186 +        if (dampingAlpha_ < 0.0) dampingAlpha_ = 0.0;
187 +        
188 +        // throw warning
189 +        sprintf( painCave.errMsg,
190 +                 "Electrostatic::initialize: dampingAlpha was not specified in the input file.\n"
191 +                 "\tA default value of %f (1/ang) will be used for the cutoff of\n\t%f (ang).\n",
192 +                 dampingAlpha_, cutoffRadius_);
193 +        painCave.severity = OPENMD_INFO;
194 +        painCave.isFatal = 0;
195 +        simError();
196 +      } else {
197 +        dampingAlpha_ = simParams_->getDampingAlpha();
198 +      }
199 +      haveDampingAlpha_ = true;
200 +    }
201 +
202      // find all of the Electrostatic atom Types:
203      ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes();
204      ForceField::AtomTypeContainer::MapTypeIterator i;
205      AtomType* at;
206 <
206 >    
207      for (at = atomTypes->beginType(i); at != NULL;
208           at = atomTypes->nextType(i)) {
209        
# Line 100 | Line 211 | namespace OpenMD {
211          addType(at);
212      }
213      
103    // check to make sure a cutoff value has been set:
104    if (!haveDefaultCutoff_) {
105      sprintf( painCave.errMsg, "Electrostatic::initialize has no Default "
106               "Cutoff value!\n");
107      painCave.severity = OPENMD_ERROR;
108      painCave.isFatal = 1;
109      simError();
110    }
214  
215 <    defaultCutoff2_ = defaultCutoff_ * defaultCutoff_;
216 <    rcuti_ = 1.0 / defaultCutoff_;
215 >    cutoffRadius2_ = cutoffRadius_ * cutoffRadius_;
216 >    rcuti_ = 1.0 / cutoffRadius_;
217      rcuti2_ = rcuti_ * rcuti_;
218      rcuti3_ = rcuti2_ * rcuti_;
219      rcuti4_ = rcuti2_ * rcuti2_;
220  
221      if (screeningMethod_ == DAMPED) {
222 <      if (!haveDampingAlpha_) {
120 <        sprintf( painCave.errMsg, "Electrostatic::initialize has no "
121 <                 "DampingAlpha value!\n");
122 <        painCave.severity = OPENMD_ERROR;
123 <        painCave.isFatal = 1;
124 <        simError();
125 <      }
126 <
222 >      
223        alpha2_ = dampingAlpha_ * dampingAlpha_;
224        alpha4_ = alpha2_ * alpha2_;
225        alpha6_ = alpha4_ * alpha2_;
226        alpha8_ = alpha4_ * alpha4_;
227        
228 <      constEXP_ = exp(-alpha2_ * defaultCutoff2_);
228 >      constEXP_ = exp(-alpha2_ * cutoffRadius2_);
229        invRootPi_ = 0.56418958354775628695;
230        alphaPi_ = 2.0 * dampingAlpha_ * invRootPi_;
231  
232 <      c1c_ = erfc(dampingAlpha_ * defaultCutoff_) * rcuti_;
232 >      c1c_ = erfc(dampingAlpha_ * cutoffRadius_) * rcuti_;
233        c2c_ = alphaPi_ * constEXP_ * rcuti_ + c1c_ * rcuti_;
234        c3c_ = 2.0 * alphaPi_ * alpha2_ + 3.0 * c2c_ * rcuti_;
235        c4c_ = 4.0 * alphaPi_ * alpha4_ + 5.0 * c3c_ * rcuti2_;
# Line 148 | Line 244 | namespace OpenMD {
244        c6c_ = 9.0 * c5c_ * rcuti2_;
245      }
246    
247 <    if (summationMethod_ == REACTION_FIELD) {
248 <      if (haveDielectric_) {
249 <        preRF_ = (dielectric_ - 1.0) /
250 <            ((2.0 * dielectric_ + 1.0) * defaultCutoff2_ * defaultCutoff_);
155 <        preRF2_ = 2.0 * preRF_;
156 <      } else {
157 <        sprintf( painCave.errMsg, "Electrostatic::initialize has no Dielectric"
158 <                 " value!\n");
159 <        painCave.severity = OPENMD_ERROR;
160 <        painCave.isFatal = 1;
161 <        simError();
162 <      }
247 >    if (summationMethod_ == esm_REACTION_FIELD) {
248 >      preRF_ = (dielectric_ - 1.0) /
249 >        ((2.0 * dielectric_ + 1.0) * cutoffRadius2_ * cutoffRadius_);
250 >      preRF2_ = 2.0 * preRF_;
251      }
252 <                              
253 <    RealType dx = defaultCutoff_ / RealType(np_ - 1);
252 >    
253 >    // Add a 2 angstrom safety window to deal with cutoffGroups that
254 >    // have charged atoms longer than the cutoffRadius away from each
255 >    // other.  Splining may not be the best choice here.  Direct calls
256 >    // to erfc might be preferrable.
257 >
258 >    RealType dx = (cutoffRadius_ + 2.0) / RealType(np_ - 1);
259      RealType rval;
260      vector<RealType> rvals;
261      vector<RealType> yvals;
# Line 283 | Line 376 | namespace OpenMD {
376            simError();                  
377          }
378          
379 +        // Quadrupoles in OpenMD are set as the diagonal elements
380 +        // of the diagonalized traceless quadrupole moment tensor.
381 +        // The column vectors of the unitary matrix that diagonalizes
382 +        // the quadrupole moment tensor become the eFrame (or the
383 +        // electrostatic version of the body-fixed frame.
384 +
385          Vector3dGenericData* v3dData = dynamic_cast<Vector3dGenericData*>(data);
386          if (v3dData == NULL) {
387            sprintf( painCave.errMsg,
# Line 315 | Line 414 | namespace OpenMD {
414      return;
415    }
416    
417 <  void Electrostatic::setElectrostaticCutoffRadius( RealType theECR,
418 <                                                    RealType theRSW ) {
419 <    defaultCutoff_ = theECR;
420 <    rrf_ = defaultCutoff_;
322 <    rt_ = theRSW;
323 <    haveDefaultCutoff_ = true;
417 >  void Electrostatic::setCutoffRadius( RealType rCut ) {
418 >    cutoffRadius_ = rCut;
419 >    rrf_ = cutoffRadius_;
420 >    haveCutoffRadius_ = true;
421    }
422 +
423 +  void Electrostatic::setSwitchingRadius( RealType rSwitch ) {
424 +    rt_ = rSwitch;
425 +  }
426    void Electrostatic::setElectrostaticSummationMethod( ElectrostaticSummationMethod esm ) {
427      summationMethod_ = esm;
428    }
# Line 337 | Line 438 | namespace OpenMD {
438      haveDielectric_ = true;
439    }
440  
441 <  void Electrostatic::calcForce(InteractionData idat) {
441 >  void Electrostatic::calcForce(InteractionData &idat) {
442  
443      // utility variables.  Should clean these up and use the Vector3d and
444      // Mat3x3d to replace as many as we can in future versions:
# Line 351 | Line 452 | namespace OpenMD {
452      RealType ct_i, ct_j, ct_ij, a1;
453      RealType riji, ri, ri2, ri3, ri4;
454      RealType pref, vterm, epot, dudr;
455 +    RealType vpair(0.0);
456      RealType scale, sc2;
457      RealType pot_term, preVal, rfVal;
458      RealType c2ri, c3ri, c4rij, cti3, ctj3, ctidotj;
459      RealType preSw, preSwSc;
460      RealType c1, c2, c3, c4;
461 <    RealType erfcVal, derfcVal;
461 >    RealType erfcVal(1.0), derfcVal(0.0);
462      RealType BigR;
463  
464      Vector3d Q_i, Q_j;
# Line 367 | Line 469 | namespace OpenMD {
469      Vector3d rhatdot2, rhatc4;
470      Vector3d dVdr;
471  
472 +    // variables for indirect (reaction field) interactions for excluded pairs:
473 +    RealType indirect_Pot(0.0);
474 +    RealType indirect_vpair(0.0);
475 +    Vector3d indirect_dVdr(V3Zero);
476 +    Vector3d indirect_duduz_i(V3Zero), indirect_duduz_j(V3Zero);
477 +
478      pair<RealType, RealType> res;
479      
480      if (!initialized_) initialize();
481      
482 <    ElectrostaticAtomData data1 = ElectrostaticMap[idat.atype1];
483 <    ElectrostaticAtomData data2 = ElectrostaticMap[idat.atype2];
482 >    ElectrostaticAtomData data1 = ElectrostaticMap[idat.atypes.first];
483 >    ElectrostaticAtomData data2 = ElectrostaticMap[idat.atypes.second];
484      
485      // some variables we'll need independent of electrostatic type:
486  
487 <    riji = 1.0 / idat.rij;
488 <    Vector3d rhat = idat.d  * riji;
487 >    riji = 1.0 /  *(idat.rij) ;
488 >    Vector3d rhat =  *(idat.d)   * riji;
489  
490      // logicals
491  
# Line 391 | Line 499 | namespace OpenMD {
499      bool j_is_SplitDipole = data2.is_SplitDipole;
500      bool j_is_Quadrupole = data2.is_Quadrupole;
501      
502 <    if (i_is_Charge)
502 >    if (i_is_Charge) {
503        q_i = data1.charge;
504 +      if (idat.excluded) {
505 +        *(idat.skippedCharge2) += q_i;
506 +      }
507 +    }
508  
509      if (i_is_Dipole) {
510        mu_i = data1.dipole_moment;
511 <      uz_i = idat.eFrame1.getColumn(2);
511 >      uz_i = idat.eFrame1->getColumn(2);
512        
513        ct_i = dot(uz_i, rhat);
514  
# Line 412 | Line 524 | namespace OpenMD {
524        qyy_i = Q_i.y();
525        qzz_i = Q_i.z();
526        
527 <      ux_i = idat.eFrame1.getColumn(0);
528 <      uy_i = idat.eFrame1.getColumn(1);
529 <      uz_i = idat.eFrame1.getColumn(2);
527 >      ux_i = idat.eFrame1->getColumn(0);
528 >      uy_i = idat.eFrame1->getColumn(1);
529 >      uz_i = idat.eFrame1->getColumn(2);
530  
531        cx_i = dot(ux_i, rhat);
532        cy_i = dot(uy_i, rhat);
# Line 425 | Line 537 | namespace OpenMD {
537        duduz_i = V3Zero;
538      }
539  
540 <    if (j_is_Charge)
540 >    if (j_is_Charge) {
541        q_j = data2.charge;
542 +      if (idat.excluded) {
543 +        *(idat.skippedCharge1) += q_j;
544 +      }
545 +    }
546  
547 +
548      if (j_is_Dipole) {
549        mu_j = data2.dipole_moment;
550 <      uz_j = idat.eFrame2.getColumn(2);
550 >      uz_j = idat.eFrame2->getColumn(2);
551        
552        ct_j = dot(uz_j, rhat);
553  
# Line 446 | Line 563 | namespace OpenMD {
563        qyy_j = Q_j.y();
564        qzz_j = Q_j.z();
565        
566 <      ux_j = idat.eFrame2.getColumn(0);
567 <      uy_j = idat.eFrame2.getColumn(1);
568 <      uz_j = idat.eFrame2.getColumn(2);
566 >      ux_j = idat.eFrame2->getColumn(0);
567 >      uy_j = idat.eFrame2->getColumn(1);
568 >      uz_j = idat.eFrame2->getColumn(2);
569  
570        cx_j = dot(ux_j, rhat);
571        cy_j = dot(uy_j, rhat);
# Line 467 | Line 584 | namespace OpenMD {
584        if (j_is_Charge) {
585          if (screeningMethod_ == DAMPED) {
586            // assemble the damping variables
587 <          res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
588 <          erfcVal = res.first;
589 <          derfcVal = res.second;
587 >          //res = erfcSpline_->getValueAndDerivativeAt( *(idat.rij) );
588 >          //erfcVal = res.first;
589 >          //derfcVal = res.second;
590 >
591 >          erfcVal = erfc(dampingAlpha_ * *(idat.rij));
592 >          derfcVal = - alphaPi_ * exp(-alpha2_ * *(idat.r2));
593 >
594            c1 = erfcVal * riji;
595            c2 = (-derfcVal + c1) * riji;
596          } else {
# Line 477 | Line 598 | namespace OpenMD {
598            c2 = c1 * riji;
599          }
600  
601 <        preVal = idat.electroMult * pre11_ * q_i * q_j;
601 >        preVal =  *(idat.electroMult) * pre11_ * q_i * q_j;
602          
603 <        if (summationMethod_ == SHIFTED_POTENTIAL) {
603 >        if (summationMethod_ == esm_SHIFTED_POTENTIAL) {
604            vterm = preVal * (c1 - c1c_);
605 <          dudr  = -idat.sw * preVal * c2;
605 >          dudr  = - *(idat.sw)  * preVal * c2;
606  
607 <        } else if (summationMethod_ == SHIFTED_FORCE)  {
608 <          vterm = preVal * ( c1 - c1c_ + c2c_*(idat.rij - defaultCutoff_) );
609 <          dudr  = idat.sw * preVal * (c2c_ - c2);
607 >        } else if (summationMethod_ == esm_SHIFTED_FORCE)  {
608 >          vterm = preVal * ( c1 - c1c_ + c2c_*( *(idat.rij)  - cutoffRadius_) );
609 >          dudr  =  *(idat.sw)  * preVal * (c2c_ - c2);
610  
611 <        } else if (summationMethod_ == REACTION_FIELD) {
612 <          rfVal = idat.electroMult * preRF_ * idat.rij * idat.rij;
611 >        } else if (summationMethod_ == esm_REACTION_FIELD) {
612 >          rfVal = preRF_ *  *(idat.rij)  *  *(idat.rij);
613 >
614            vterm = preVal * ( riji + rfVal );            
615 <          dudr  = idat.sw * preVal * ( 2.0 * rfVal - riji ) * riji;
615 >          dudr  =  *(idat.sw)  * preVal * ( 2.0 * rfVal - riji ) * riji;
616 >          
617 >          // if this is an excluded pair, there are still indirect
618 >          // interactions via the reaction field we must worry about:
619  
620 +          if (idat.excluded) {
621 +            indirect_vpair += preVal * rfVal;
622 +            indirect_Pot += *(idat.sw) * preVal * rfVal;
623 +            indirect_dVdr += *(idat.sw)  * preVal * 2.0 * rfVal  * riji * rhat;
624 +          }
625 +          
626          } else {
496          vterm = preVal * riji * erfcVal;            
627  
628 <          dudr  = - idat.sw * preVal * c2;
628 >          vterm = preVal * riji * erfcVal;          
629 >          dudr  = -  *(idat.sw)  * preVal * c2;
630  
631          }
501
502        idat.vpair += vterm;
503        epot += idat.sw * vterm;
632  
633 <        dVdr += dudr * rhat;      
633 >        vpair += vterm;
634 >        epot +=  *(idat.sw)  * vterm;
635 >        dVdr += dudr * rhat;                
636        }
637  
638        if (j_is_Dipole) {
639          // pref is used by all the possible methods
640 <        pref = idat.electroMult * pre12_ * q_i * mu_j;
641 <        preSw = idat.sw * pref;
640 >        pref =  *(idat.electroMult) * pre12_ * q_i * mu_j;
641 >        preSw =  *(idat.sw)  * pref;
642  
643 <        if (summationMethod_ == REACTION_FIELD) {
643 >        if (summationMethod_ == esm_REACTION_FIELD) {
644            ri2 = riji * riji;
645            ri3 = ri2 * riji;
646      
647 <          vterm = - pref * ct_j * ( ri2 - preRF2_ * idat.rij );
648 <          idat.vpair += vterm;
649 <          epot += idat.sw * vterm;
647 >          vterm = - pref * ct_j * ( ri2 - preRF2_ *  *(idat.rij)  );
648 >          vpair += vterm;
649 >          epot +=  *(idat.sw)  * vterm;
650  
651            dVdr +=  -preSw * (ri3 * (uz_j - 3.0 * ct_j * rhat) - preRF2_*uz_j);
652 <          duduz_j += -preSw * rhat * (ri2 - preRF2_ * idat.rij);  
652 >          duduz_j += -preSw * rhat * (ri2 - preRF2_ *  *(idat.rij) );  
653  
654 +          // Even if we excluded this pair from direct interactions,
655 +          // we still have the reaction-field-mediated charge-dipole
656 +          // interaction:
657 +
658 +          if (idat.excluded) {
659 +            indirect_vpair += pref * ct_j * preRF2_ * *(idat.rij);
660 +            indirect_Pot += preSw * ct_j * preRF2_ * *(idat.rij);
661 +            indirect_dVdr += preSw * preRF2_ * uz_j;
662 +            indirect_duduz_j += preSw * rhat * preRF2_ *  *(idat.rij);
663 +          }
664 +                      
665          } else {
666            // determine the inverse r used if we have split dipoles
667            if (j_is_SplitDipole) {
668 <            BigR = sqrt(idat.r2 + 0.25 * d_j * d_j);
668 >            BigR = sqrt( *(idat.r2) + 0.25 * d_j * d_j);
669              ri = 1.0 / BigR;
670 <            scale = idat.rij * ri;
670 >            scale =  *(idat.rij)  * ri;
671            } else {
672              ri = riji;
673              scale = 1.0;
# Line 536 | Line 677 | namespace OpenMD {
677  
678            if (screeningMethod_ == DAMPED) {
679              // assemble the damping variables
680 <            res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
681 <            erfcVal = res.first;
682 <            derfcVal = res.second;
680 >            //res = erfcSpline_->getValueAndDerivativeAt( *(idat.rij) );
681 >            //erfcVal = res.first;
682 >            //derfcVal = res.second;
683 >            erfcVal = erfc(dampingAlpha_ * *(idat.rij));
684 >            derfcVal = - alphaPi_ * exp(-alpha2_ * *(idat.r2));
685              c1 = erfcVal * ri;
686              c2 = (-derfcVal + c1) * ri;
687              c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * ri;
# Line 553 | Line 696 | namespace OpenMD {
696            // calculate the potential
697            pot_term =  scale * c2;
698            vterm = -pref * ct_j * pot_term;
699 <          idat.vpair += vterm;
700 <          epot += idat.sw * vterm;
699 >          vpair += vterm;
700 >          epot +=  *(idat.sw)  * vterm;
701              
702            // calculate derivatives for forces and torques
703  
# Line 569 | Line 712 | namespace OpenMD {
712          cx2 = cx_j * cx_j;
713          cy2 = cy_j * cy_j;
714          cz2 = cz_j * cz_j;
715 <        pref =  idat.electroMult * pre14_ * q_i * one_third_;
715 >        pref =   *(idat.electroMult) * pre14_ * q_i * one_third_;
716            
717          if (screeningMethod_ == DAMPED) {
718            // assemble the damping variables
719 <          res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
720 <          erfcVal = res.first;
721 <          derfcVal = res.second;
719 >          //res = erfcSpline_->getValueAndDerivativeAt( *(idat.rij) );
720 >          //erfcVal = res.first;
721 >          //derfcVal = res.second;
722 >          erfcVal = erfc(dampingAlpha_ * *(idat.rij));
723 >          derfcVal = - alphaPi_ * exp(-alpha2_ * *(idat.r2));
724            c1 = erfcVal * riji;
725            c2 = (-derfcVal + c1) * riji;
726            c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * riji;
# Line 588 | Line 733 | namespace OpenMD {
733          }
734  
735          // precompute variables for convenience
736 <        preSw = idat.sw * pref;
736 >        preSw =  *(idat.sw)  * pref;
737          c2ri = c2 * riji;
738          c3ri = c3 * riji;
739 <        c4rij = c4 * idat.rij;
739 >        c4rij = c4 *  *(idat.rij) ;
740          rhatdot2 = 2.0 * rhat * c3;
741          rhatc4 = rhat * c4rij;
742  
# Line 600 | Line 745 | namespace OpenMD {
745                       qyy_j * (cy2*c3 - c2ri) +
746                       qzz_j * (cz2*c3 - c2ri) );
747          vterm = pref * pot_term;
748 <        idat.vpair += vterm;
749 <        epot += idat.sw * vterm;
748 >        vpair += vterm;
749 >        epot +=  *(idat.sw)  * vterm;
750                  
751          // calculate derivatives for the forces and torques
752  
# Line 619 | Line 764 | namespace OpenMD {
764  
765        if (j_is_Charge) {
766          // variables used by all the methods
767 <        pref = idat.electroMult * pre12_ * q_j * mu_i;
768 <        preSw = idat.sw * pref;
767 >        pref =  *(idat.electroMult) * pre12_ * q_j * mu_i;
768 >        preSw =  *(idat.sw)  * pref;
769  
770 <        if (summationMethod_ == REACTION_FIELD) {
770 >        if (summationMethod_ == esm_REACTION_FIELD) {
771  
772            ri2 = riji * riji;
773            ri3 = ri2 * riji;
774  
775 <          vterm = pref * ct_i * ( ri2 - preRF2_ * idat.rij );
776 <          idat.vpair += vterm;
777 <          epot += idat.sw * vterm;
775 >          vterm = pref * ct_i * ( ri2 - preRF2_ *  *(idat.rij)  );
776 >          vpair += vterm;
777 >          epot +=  *(idat.sw)  * vterm;
778            
779            dVdr += preSw * (ri3 * (uz_i - 3.0 * ct_i * rhat) - preRF2_ * uz_i);
780            
781 <          duduz_i += preSw * rhat * (ri2 - preRF2_ * idat.rij);
781 >          duduz_i += preSw * rhat * (ri2 - preRF2_ *  *(idat.rij) );
782 >
783 >          // Even if we excluded this pair from direct interactions,
784 >          // we still have the reaction-field-mediated charge-dipole
785 >          // interaction:
786 >
787 >          if (idat.excluded) {
788 >            indirect_vpair += -pref * ct_i * preRF2_ * *(idat.rij);
789 >            indirect_Pot += -preSw * ct_i * preRF2_ * *(idat.rij);
790 >            indirect_dVdr += -preSw * preRF2_ * uz_i;
791 >            indirect_duduz_i += -preSw * rhat * preRF2_ *  *(idat.rij);
792 >          }
793              
794          } else {
795            
796            // determine inverse r if we are using split dipoles
797            if (i_is_SplitDipole) {
798 <            BigR = sqrt(idat.r2 + 0.25 * d_i * d_i);
798 >            BigR = sqrt( *(idat.r2) + 0.25 * d_i * d_i);
799              ri = 1.0 / BigR;
800 <            scale = idat.rij * ri;
800 >            scale =  *(idat.rij)  * ri;
801            } else {
802              ri = riji;
803              scale = 1.0;
# Line 651 | Line 807 | namespace OpenMD {
807              
808            if (screeningMethod_ == DAMPED) {
809              // assemble the damping variables
810 <            res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
811 <            erfcVal = res.first;
812 <            derfcVal = res.second;
810 >            //res = erfcSpline_->getValueAndDerivativeAt( *(idat.rij) );
811 >            //erfcVal = res.first;
812 >            //derfcVal = res.second;
813 >            erfcVal = erfc(dampingAlpha_ * *(idat.rij));
814 >            derfcVal = - alphaPi_ * exp(-alpha2_ * *(idat.r2));
815              c1 = erfcVal * ri;
816              c2 = (-derfcVal + c1) * ri;
817              c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * ri;
# Line 668 | Line 826 | namespace OpenMD {
826            // calculate the potential
827            pot_term = c2 * scale;
828            vterm = pref * ct_i * pot_term;
829 <          idat.vpair += vterm;
830 <          epot += idat.sw * vterm;
829 >          vpair += vterm;
830 >          epot +=  *(idat.sw)  * vterm;
831  
832            // calculate derivatives for the forces and torques
833            dVdr += preSw * (uz_i * c2ri - ct_i * rhat * sc2 * c3);
# Line 681 | Line 839 | namespace OpenMD {
839          // variables used by all methods
840          ct_ij = dot(uz_i, uz_j);
841  
842 <        pref = idat.electroMult * pre22_ * mu_i * mu_j;
843 <        preSw = idat.sw * pref;
842 >        pref =  *(idat.electroMult) * pre22_ * mu_i * mu_j;
843 >        preSw =  *(idat.sw)  * pref;
844  
845 <        if (summationMethod_ == REACTION_FIELD) {
845 >        if (summationMethod_ == esm_REACTION_FIELD) {
846            ri2 = riji * riji;
847            ri3 = ri2 * riji;
848            ri4 = ri2 * ri2;
849  
850            vterm = pref * ( ri3 * (ct_ij - 3.0 * ct_i * ct_j) -
851                             preRF2_ * ct_ij );
852 <          idat.vpair += vterm;
853 <          epot += idat.sw * vterm;
852 >          vpair += vterm;
853 >          epot +=  *(idat.sw)  * vterm;
854              
855            a1 = 5.0 * ct_i * ct_j - ct_ij;
856              
# Line 700 | Line 858 | namespace OpenMD {
858  
859            duduz_i += preSw * (ri3 * (uz_j - 3.0 * ct_j * rhat) - preRF2_*uz_j);
860            duduz_j += preSw * (ri3 * (uz_i - 3.0 * ct_i * rhat) - preRF2_*uz_i);
861 +
862 +          if (idat.excluded) {
863 +            indirect_vpair +=  - pref * preRF2_ * ct_ij;
864 +            indirect_Pot +=    - preSw * preRF2_ * ct_ij;
865 +            indirect_duduz_i += -preSw * preRF2_ * uz_j;
866 +            indirect_duduz_j += -preSw * preRF2_ * uz_i;
867 +          }
868  
869          } else {
870            
871            if (i_is_SplitDipole) {
872              if (j_is_SplitDipole) {
873 <              BigR = sqrt(idat.r2 + 0.25 * d_i * d_i + 0.25 * d_j * d_j);
873 >              BigR = sqrt( *(idat.r2) + 0.25 * d_i * d_i + 0.25 * d_j * d_j);
874              } else {
875 <              BigR = sqrt(idat.r2 + 0.25 * d_i * d_i);
875 >              BigR = sqrt( *(idat.r2) + 0.25 * d_i * d_i);
876              }
877              ri = 1.0 / BigR;
878 <            scale = idat.rij * ri;
878 >            scale =  *(idat.rij)  * ri;
879            } else {
880              if (j_is_SplitDipole) {
881 <              BigR = sqrt(idat.r2 + 0.25 * d_j * d_j);
881 >              BigR = sqrt( *(idat.r2) + 0.25 * d_j * d_j);
882                ri = 1.0 / BigR;
883 <              scale = idat.rij * ri;
883 >              scale =  *(idat.rij)  * ri;
884              } else {
885                ri = riji;
886                scale = 1.0;
# Line 723 | Line 888 | namespace OpenMD {
888            }
889            if (screeningMethod_ == DAMPED) {
890              // assemble damping variables
891 <            res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
892 <            erfcVal = res.first;
893 <            derfcVal = res.second;
891 >            //res = erfcSpline_->getValueAndDerivativeAt( *(idat.rij) );
892 >            //erfcVal = res.first;
893 >            //derfcVal = res.second;
894 >            erfcVal = erfc(dampingAlpha_ * *(idat.rij));
895 >            derfcVal = - alphaPi_ * exp(-alpha2_ * *(idat.r2));
896              c1 = erfcVal * ri;
897              c2 = (-derfcVal + c1) * ri;
898              c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * ri;
# Line 745 | Line 912 | namespace OpenMD {
912            preSwSc = preSw * scale;
913            c2ri = c2 * ri;
914            c3ri = c3 * ri;
915 <          c4rij = c4 * idat.rij;
915 >          c4rij = c4 *  *(idat.rij) ;
916  
917            // calculate the potential
918            pot_term = (ct_ij * c2ri - ctidotj * c3);
919            vterm = pref * pot_term;
920 <          idat.vpair += vterm;
921 <          epot += idat.sw * vterm;
920 >          vpair += vterm;
921 >          epot +=  *(idat.sw)  * vterm;
922  
923            // calculate derivatives for the forces and torques
924            dVdr += preSwSc * ( ctidotj * rhat * c4rij  -
# Line 770 | Line 937 | namespace OpenMD {
937          cy2 = cy_i * cy_i;
938          cz2 = cz_i * cz_i;
939  
940 <        pref = idat.electroMult * pre14_ * q_j * one_third_;
940 >        pref =  *(idat.electroMult) * pre14_ * q_j * one_third_;
941  
942          if (screeningMethod_ == DAMPED) {
943            // assemble the damping variables
944 <          res = erfcSpline_->getValueAndDerivativeAt(idat.rij);
945 <          erfcVal = res.first;
946 <          derfcVal = res.second;
944 >          //res = erfcSpline_->getValueAndDerivativeAt( *(idat.rij) );
945 >          //erfcVal = res.first;
946 >          //derfcVal = res.second;
947 >          erfcVal = erfc(dampingAlpha_ * *(idat.rij));
948 >          derfcVal = - alphaPi_ * exp(-alpha2_ * *(idat.r2));
949            c1 = erfcVal * riji;
950            c2 = (-derfcVal + c1) * riji;
951            c3 = -2.0 * derfcVal * alpha2_ + 3.0 * c2 * riji;
# Line 789 | Line 958 | namespace OpenMD {
958          }
959            
960          // precompute some variables for convenience
961 <        preSw = idat.sw * pref;
961 >        preSw =  *(idat.sw)  * pref;
962          c2ri = c2 * riji;
963          c3ri = c3 * riji;
964 <        c4rij = c4 * idat.rij;
964 >        c4rij = c4 *  *(idat.rij) ;
965          rhatdot2 = 2.0 * rhat * c3;
966          rhatc4 = rhat * c4rij;
967  
# Line 802 | Line 971 | namespace OpenMD {
971                       qzz_i * (cz2 * c3 - c2ri) );
972          
973          vterm = pref * pot_term;
974 <        idat.vpair += vterm;
975 <        epot += idat.sw * vterm;
974 >        vpair += vterm;
975 >        epot +=  *(idat.sw)  * vterm;
976  
977          // calculate the derivatives for the forces and torques
978  
# Line 817 | Line 986 | namespace OpenMD {
986        }
987      }
988  
820    idat.pot += epot;
821    idat.f1 += dVdr;
989  
990 <    if (i_is_Dipole || i_is_Quadrupole)
991 <      idat.t1 -= cross(uz_i, duduz_i);
992 <    if (i_is_Quadrupole) {
993 <      idat.t1 -= cross(ux_i, dudux_i);
994 <      idat.t1 -= cross(uy_i, duduy_i);
995 <    }
990 >    if (!idat.excluded) {
991 >      *(idat.vpair) += vpair;
992 >      (*(idat.pot))[ELECTROSTATIC_FAMILY] += epot;
993 >      *(idat.f1) += dVdr;
994 >      
995 >      if (i_is_Dipole || i_is_Quadrupole)
996 >        *(idat.t1) -= cross(uz_i, duduz_i);
997 >      if (i_is_Quadrupole) {
998 >        *(idat.t1) -= cross(ux_i, dudux_i);
999 >        *(idat.t1) -= cross(uy_i, duduy_i);
1000 >      }
1001 >      
1002 >      if (j_is_Dipole || j_is_Quadrupole)
1003 >        *(idat.t2) -= cross(uz_j, duduz_j);
1004 >      if (j_is_Quadrupole) {
1005 >        *(idat.t2) -= cross(uz_j, dudux_j);
1006 >        *(idat.t2) -= cross(uz_j, duduy_j);
1007 >      }
1008 >
1009 >    } else {
1010 >
1011 >      // only accumulate the forces and torques resulting from the
1012 >      // indirect reaction field terms.
1013  
1014 <    if (j_is_Dipole || j_is_Quadrupole)
1015 <      idat.t2 -= cross(uz_j, duduz_j);
1016 <    if (j_is_Quadrupole) {
1017 <      idat.t2 -= cross(uz_j, dudux_j);
1018 <      idat.t2 -= cross(uz_j, duduy_j);
1014 >      *(idat.vpair) += indirect_vpair;
1015 >      (*(idat.pot))[ELECTROSTATIC_FAMILY] += indirect_Pot;
1016 >      *(idat.f1) += indirect_dVdr;
1017 >      
1018 >      if (i_is_Dipole)
1019 >        *(idat.t1) -= cross(uz_i, indirect_duduz_i);
1020 >      if (j_is_Dipole)
1021 >        *(idat.t2) -= cross(uz_j, indirect_duduz_j);
1022      }
1023  
1024 +
1025      return;
1026    }  
839
840  void Electrostatic::calcSkipCorrection(SkipCorrectionData skdat) {
841
842    if (!initialized_) initialize();
1027      
1028 <    ElectrostaticAtomData data1 = ElectrostaticMap[skdat.atype1];
845 <    ElectrostaticAtomData data2 = ElectrostaticMap[skdat.atype2];
846 <    
847 <    // logicals
848 <
849 <    bool i_is_Charge = data1.is_Charge;
850 <    bool i_is_Dipole = data1.is_Dipole;
851 <
852 <    bool j_is_Charge = data2.is_Charge;
853 <    bool j_is_Dipole = data2.is_Dipole;
854 <
855 <    RealType q_i, q_j;
856 <    
857 <    // The skippedCharge computation is needed by the real-space cutoff methods
858 <    // (i.e. shifted force and shifted potential)
859 <
860 <    if (i_is_Charge) {
861 <      q_i = data1.charge;
862 <      skdat.skippedCharge2 += q_i;
863 <    }
864 <
865 <    if (j_is_Charge) {
866 <      q_j = data2.charge;
867 <      skdat.skippedCharge1 += q_j;
868 <    }
869 <
870 <    // the rest of this function should only be necessary for reaction field.
871 <
872 <    if (summationMethod_ == REACTION_FIELD) {
873 <      RealType riji, ri2, ri3;
874 <      RealType q_i, mu_i, ct_i;
875 <      RealType q_j, mu_j, ct_j;
876 <      RealType preVal, rfVal, vterm, dudr, pref, myPot;
877 <      Vector3d dVdr, uz_i, uz_j, duduz_i, duduz_j, rhat;
878 <
879 <      // some variables we'll need independent of electrostatic type:
880 <      
881 <      riji = 1.0 / skdat.rij;
882 <      rhat = skdat.d  * riji;
883 <
884 <      if (i_is_Dipole) {
885 <        mu_i = data1.dipole_moment;
886 <        uz_i = skdat.eFrame1.getColumn(2);      
887 <        ct_i = dot(uz_i, rhat);
888 <        duduz_i = V3Zero;
889 <      }
890 <            
891 <      if (j_is_Dipole) {
892 <        mu_j = data2.dipole_moment;
893 <        uz_j = skdat.eFrame2.getColumn(2);      
894 <        ct_j = dot(uz_j, rhat);
895 <        duduz_j = V3Zero;
896 <      }
897 <    
898 <      if (i_is_Charge) {
899 <        if (j_is_Charge) {
900 <          preVal = skdat.electroMult * pre11_ * q_i * q_j;
901 <          rfVal = preRF_ * skdat.rij * skdat.rij;
902 <          vterm = preVal * rfVal;
903 <          myPot += skdat.sw * vterm;        
904 <          dudr  = skdat.sw * preVal * 2.0 * rfVal * riji;        
905 <          dVdr += dudr * rhat;
906 <        }
907 <        
908 <        if (j_is_Dipole) {
909 <          ri2 = riji * riji;
910 <          ri3 = ri2 * riji;        
911 <          pref = skdat.electroMult * pre12_ * q_i * mu_j;
912 <          vterm = - pref * ct_j * ( ri2 - preRF2_ * skdat.rij );
913 <          myPot += skdat.sw * vterm;        
914 <          dVdr += -skdat.sw * pref * ( ri3 * ( uz_j - 3.0 * ct_j * rhat) - preRF2_ * uz_j);
915 <          duduz_j += -skdat.sw * pref * rhat * (ri2 - preRF2_ * skdat.rij);
916 <        }
917 <      }
918 <      if (i_is_Dipole) {
919 <        if (j_is_Charge) {
920 <          ri2 = riji * riji;
921 <          ri3 = ri2 * riji;        
922 <          pref = skdat.electroMult * pre12_ * q_j * mu_i;
923 <          vterm = - pref * ct_i * ( ri2 - preRF2_ * skdat.rij );
924 <          myPot += skdat.sw * vterm;        
925 <          dVdr += skdat.sw * pref * ( ri3 * ( uz_i - 3.0 * ct_i * rhat) - preRF2_ * uz_i);      
926 <          duduz_i += skdat.sw * pref * rhat * (ri2 - preRF2_ * skdat.rij);
927 <        }
928 <      }
929 <      
930 <      // accumulate the forces and torques resulting from the self term
931 <      skdat.pot += myPot;
932 <      skdat.f1 += dVdr;
933 <      
934 <      if (i_is_Dipole)
935 <        skdat.t1 -= cross(uz_i, duduz_i);
936 <      if (j_is_Dipole)
937 <        skdat.t2 -= cross(uz_j, duduz_j);
938 <    }
939 <  }
940 <    
941 <  void Electrostatic::calcSelfCorrection(SelfCorrectionData scdat) {
1028 >  void Electrostatic::calcSelfCorrection(SelfData &sdat) {
1029      RealType mu1, preVal, chg1, self;
1030      
1031      if (!initialized_) initialize();
1032 <    
1033 <    ElectrostaticAtomData data = ElectrostaticMap[scdat.atype];
1032 >
1033 >    ElectrostaticAtomData data = ElectrostaticMap[sdat.atype];
1034    
1035      // logicals
949
1036      bool i_is_Charge = data.is_Charge;
1037      bool i_is_Dipole = data.is_Dipole;
1038  
1039 <    if (summationMethod_ == REACTION_FIELD) {
1039 >    if (summationMethod_ == esm_REACTION_FIELD) {
1040        if (i_is_Dipole) {
1041          mu1 = data.dipole_moment;          
1042          preVal = pre22_ * preRF2_ * mu1 * mu1;
1043 <        scdat.pot -= 0.5 * preVal;
1043 >        (*(sdat.pot))[ELECTROSTATIC_FAMILY] -= 0.5 * preVal;
1044          
1045          // The self-correction term adds into the reaction field vector
1046 <        Vector3d uz_i = scdat.eFrame.getColumn(2);
1046 >        Vector3d uz_i = sdat.eFrame->getColumn(2);
1047          Vector3d ei = preVal * uz_i;
1048  
1049          // This looks very wrong.  A vector crossed with itself is zero.
1050 <        scdat.t -= cross(uz_i, ei);
1050 >        *(sdat.t) -= cross(uz_i, ei);
1051        }
1052 <    } else if (summationMethod_ == SHIFTED_FORCE || summationMethod_ == SHIFTED_POTENTIAL) {
1052 >    } else if (summationMethod_ == esm_SHIFTED_FORCE || summationMethod_ == esm_SHIFTED_POTENTIAL) {
1053        if (i_is_Charge) {        
1054          chg1 = data.charge;
1055          if (screeningMethod_ == DAMPED) {
1056 <          self = - 0.5 * (c1c_ + alphaPi_) * chg1 * (chg1 + scdat.skippedCharge) * pre11_;
1056 >          self = - 0.5 * (c1c_ + alphaPi_) * chg1 * (chg1 + *(sdat.skippedCharge)) * pre11_;
1057          } else {        
1058 <          self = - 0.5 * rcuti_ * chg1 * (chg1 + scdat.skippedCharge) * pre11_;
1058 >          self = - 0.5 * rcuti_ * chg1 * (chg1 +  *(sdat.skippedCharge)) * pre11_;
1059          }
1060 <        scdat.pot += self;
1060 >        (*(sdat.pot))[ELECTROSTATIC_FAMILY] += self;
1061        }
1062      }
1063    }
1064 +
1065 +  RealType Electrostatic::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) {
1066 +    // This seems to work moderately well as a default.  There's no
1067 +    // inherent scale for 1/r interactions that we can standardize.
1068 +    // 12 angstroms seems to be a reasonably good guess for most
1069 +    // cases.
1070 +    return 12.0;
1071 +  }
1072   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines