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

Comparing trunk/src/nonbonded/Electrostatic.cpp (file contents):
Revision 1879 by gezelter, Sun Jun 16 15:15:42 2013 UTC vs.
Revision 1924 by gezelter, Mon Aug 5 21:46:11 2013 UTC

# Line 44 | Line 44
44   #include <string.h>
45  
46   #include <cmath>
47 + #include <numeric>
48   #include "nonbonded/Electrostatic.hpp"
49   #include "utils/simError.h"
50   #include "types/NonBondedInteractionType.hpp"
# Line 55 | Line 56
56   #include "utils/PhysicalConstants.hpp"
57   #include "math/erfc.hpp"
58   #include "math/SquareMatrix.hpp"
59 + #include "primitives/Molecule.hpp"
60  
61 +
62   namespace OpenMD {
63    
64    Electrostatic::Electrostatic(): name_("Electrostatic"), initialized_(false),
# Line 191 | Line 194 | namespace OpenMD {
194        simError();
195      }
196            
197 <    if (screeningMethod_ == DAMPED) {      
197 >    if (screeningMethod_ == DAMPED || summationMethod_ == esm_EWALD_FULL) {
198        if (!simParams_->haveDampingAlpha()) {
199          // first set a cutoff dependent alpha value
200          // we assume alpha depends linearly with rcut from 0 to 20.5 ang
201          dampingAlpha_ = 0.425 - cutoffRadius_* 0.02;
202 <        if (dampingAlpha_ < 0.0) dampingAlpha_ = 0.0;
200 <        
202 >        if (dampingAlpha_ < 0.0) dampingAlpha_ = 0.0;        
203          // throw warning
204          sprintf( painCave.errMsg,
205                   "Electrostatic::initialize: dampingAlpha was not specified in the\n"
# Line 213 | Line 215 | namespace OpenMD {
215        haveDampingAlpha_ = true;
216      }
217  
218 <    // find all of the Electrostatic atom Types:
219 <    ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes();
220 <    ForceField::AtomTypeContainer::MapTypeIterator i;
221 <    AtomType* at;
218 >
219 >    Etypes.clear();
220 >    Etids.clear();
221 >    FQtypes.clear();
222 >    FQtids.clear();
223 >    ElectrostaticMap.clear();
224 >    Jij.clear();
225 >    nElectro_ = 0;
226 >    nFlucq_ = 0;
227 >
228 >    Etids.resize( forceField_->getNAtomType(), -1);
229 >    FQtids.resize( forceField_->getNAtomType(), -1);
230 >
231 >    set<AtomType*>::iterator at;
232 >    for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {    
233 >      if ((*at)->isElectrostatic()) nElectro_++;
234 >      if ((*at)->isFluctuatingCharge()) nFlucq_++;
235 >    }
236      
237 <    for (at = atomTypes->beginType(i); at != NULL;
238 <         at = atomTypes->nextType(i)) {
239 <      
240 <      if (at->isElectrostatic())
225 <        addType(at);
237 >    Jij.resize(nFlucq_);
238 >
239 >    for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {
240 >      if ((*at)->isElectrostatic()) addType(*at);
241      }  
242      
243      if (summationMethod_ == esm_REACTION_FIELD) {
# Line 250 | Line 265 | namespace OpenMD {
265        b3c = (5.0 * b2c + pow(2.0*a2, 3) * expTerm * invArootPi) / r2;
266        b4c = (7.0 * b3c + pow(2.0*a2, 4) * expTerm * invArootPi) / r2;
267        b5c = (9.0 * b4c + pow(2.0*a2, 5) * expTerm * invArootPi) / r2;
268 <      selfMult_ = b0c + a2 * invArootPi;
268 >      // Half the Smith self piece:
269 >      selfMult1_ = - a2 * invArootPi;
270 >      selfMult2_ = - 2.0 * a2 * a2 * invArootPi / 3.0;
271 >      selfMult4_ = - 4.0 * a2 * a2 * a2 * invArootPi / 5.0;
272      } else {
273        a2 = 0.0;
274        b0c = 1.0 / r;
# Line 259 | Line 277 | namespace OpenMD {
277        b3c = (5.0 * b2c) / r2;
278        b4c = (7.0 * b3c) / r2;
279        b5c = (9.0 * b4c) / r2;
280 <      selfMult_ = b0c;
280 >      selfMult1_ = 0.0;
281 >      selfMult2_ = 0.0;
282 >      selfMult4_ = 0.0;
283      }
284  
285      // higher derivatives of B_0 at the cutoff radius:
# Line 267 | Line 287 | namespace OpenMD {
287      db0c_2 =     -b1c + r2 * b2c;
288      db0c_3 =          3.0*r*b2c  - r2*r*b3c;
289      db0c_4 =          3.0*b2c  - 6.0*r2*b3c     + r2*r2*b4c;
290 <    db0c_5 =                    -15.0*r*b3c + 10.0*r2*r*b4c - r2*r2*r*b5c;
271 <    
290 >    db0c_5 =                    -15.0*r*b3c + 10.0*r2*r*b4c - r2*r2*r*b5c;  
291  
292 +    if (summationMethod_ != esm_EWALD_FULL) {
293 +      selfMult1_ -= b0c;
294 +      selfMult2_ += (db0c_2 + 2.0*db0c_1*ric) /  3.0;
295 +      selfMult4_ -= (db0c_4 + 4.0*db0c_3*ric) / 15.0;
296 +    }
297 +
298      // working variables for the splines:
299      RealType ri, ri2;
300      RealType b0, b1, b2, b3, b4, b5;
# Line 304 | Line 329 | namespace OpenMD {
329      vector<RealType> v21v, v22v;
330      vector<RealType> v31v, v32v;
331      vector<RealType> v41v, v42v, v43v;
307
308    /*
309    vector<RealType> dv01v;
310    vector<RealType> dv11v;
311    vector<RealType> dv21v, dv22v;
312    vector<RealType> dv31v, dv32v;
313    vector<RealType> dv41v, dv42v, dv43v;
314    */
332  
333      for (int i = 1; i < np_ + 1; i++) {
334        r = RealType(i) * dx;
# Line 398 | Line 415 | namespace OpenMD {
415          v11 = g - gc - rmRc*hc;
416          v21 = g*ri - gc*ric - rmRc*(hc - gc*ric)*ric;
417          v22 = h - g*ri - (hc - gc*ric) - rmRc*(sc - (hc - gc*ric)*ric);
418 <        v31 = (h-g*ri)*ri - (hc-g*ric)*ric - rmRc*(sc-2.0*(hc-gc*ric)*ric)*ric;
418 >        v31 = (h-g*ri)*ri - (hc-gc*ric)*ric - rmRc*(sc-2.0*(hc-gc*ric)*ric)*ric;
419          v32 = (s - 3.0*(h-g*ri)*ri) - (sc - 3.0*(hc-gc*ric)*ric)
420            - rmRc*(tc - 3.0*(sc-2.0*(hc-gc*ric)*ric)*ric);
421          v41 = (h - g*ri)*ri2 - (hc - gc*ric)*ric2
# Line 455 | Line 472 | namespace OpenMD {
472          v11 = g - gc;
473          v21 = g*ri - gc*ric;
474          v22 = h - g*ri - (hc - gc*ric);
475 <        v31 = (h-g*ri)*ri - (hc-g*ric)*ric;
475 >        v31 = (h-g*ri)*ri - (hc-gc*ric)*ric;
476          v32 = (s - 3.0*(h-g*ri)*ri) - (sc - 3.0*(hc-gc*ric)*ric);
477          v41 = (h - g*ri)*ri2 - (hc - gc*ric)*ric2;
478          v42 = (s-3.0*(h-g*ri)*ri)*ri - (sc-3.0*(hc-gc*ric)*ric)*ric;        
# Line 476 | Line 493 | namespace OpenMD {
493  
494        case esm_SWITCHING_FUNCTION:
495        case esm_HARD:
496 +      case esm_EWALD_FULL:
497  
498          v01 = f;
499          v11 = g;
# Line 515 | Line 533 | namespace OpenMD {
533          v11 = g - gc;
534          v21 = g*ri - gc*ric;
535          v22 = h - g*ri - (hc - gc*ric);
536 <        v31 = (h-g*ri)*ri - (hc-g*ric)*ric;
536 >        v31 = (h-g*ri)*ri - (hc-gc*ric)*ric;
537          v32 = (s - 3.0*(h-g*ri)*ri) - (sc - 3.0*(hc-gc*ric)*ric);
538          v41 = (h - g*ri)*ri2 - (hc - gc*ric)*ric2;
539          v42 = (s-3.0*(h-g*ri)*ri)*ri - (sc-3.0*(hc-gc*ric)*ric)*ric;        
# Line 534 | Line 552 | namespace OpenMD {
552  
553          break;
554                  
537      case esm_EWALD_FULL:
555        case esm_EWALD_PME:
556        case esm_EWALD_SPME:
557        default :
# Line 563 | Line 580 | namespace OpenMD {
580        v41v.push_back(v41);
581        v42v.push_back(v42);
582        v43v.push_back(v43);
566      /*
567      dv01v.push_back(dv01);
568      dv11v.push_back(dv11);
569      dv21v.push_back(dv21);
570      dv22v.push_back(dv22);
571      dv31v.push_back(dv31);
572      dv32v.push_back(dv32);      
573      dv41v.push_back(dv41);
574      dv42v.push_back(dv42);
575      dv43v.push_back(dv43);
576      */
583      }
584  
585      // construct the spline structures and fill them with the values we've
# Line 598 | Line 604 | namespace OpenMD {
604      v43s = new CubicSpline();
605      v43s->addPoints(rv, v43v);
606  
601    /*
602    dv01s = new CubicSpline();
603    dv01s->addPoints(rv, dv01v);
604    dv11s = new CubicSpline();
605    dv11s->addPoints(rv, dv11v);
606    dv21s = new CubicSpline();
607    dv21s->addPoints(rv, dv21v);
608    dv22s = new CubicSpline();
609    dv22s->addPoints(rv, dv22v);
610    dv31s = new CubicSpline();
611    dv31s->addPoints(rv, dv31v);
612    dv32s = new CubicSpline();
613    dv32s->addPoints(rv, dv32v);
614    dv41s = new CubicSpline();
615    dv41s->addPoints(rv, dv41v);
616    dv42s = new CubicSpline();
617    dv42s->addPoints(rv, dv42v);
618    dv43s = new CubicSpline();
619    dv43s->addPoints(rv, dv43v);
620    */
621
607      haveElectroSplines_ = true;
608  
609      initialized_ = true;
610    }
611        
612    void Electrostatic::addType(AtomType* atomType){
613 <
613 >    
614      ElectrostaticAtomData electrostaticAtomData;
615      electrostaticAtomData.is_Charge = false;
616      electrostaticAtomData.is_Dipole = false;
# Line 661 | Line 646 | namespace OpenMD {
646        electrostaticAtomData.slaterZeta = fqa.getSlaterZeta();
647      }
648  
649 <    pair<map<int,AtomType*>::iterator,bool> ret;    
650 <    ret = ElectrostaticList.insert( pair<int,AtomType*>(atomType->getIdent(),
651 <                                                        atomType) );
649 >    int atid = atomType->getIdent();
650 >    int etid = Etypes.size();
651 >    int fqtid = FQtypes.size();
652 >
653 >    pair<set<int>::iterator,bool> ret;    
654 >    ret = Etypes.insert( atid );
655      if (ret.second == false) {
656        sprintf( painCave.errMsg,
657                 "Electrostatic already had a previous entry with ident %d\n",
658 <               atomType->getIdent() );
658 >               atid);
659        painCave.severity = OPENMD_INFO;
660        painCave.isFatal = 0;
661        simError();        
662      }
663      
664 <    ElectrostaticMap[atomType] = electrostaticAtomData;  
664 >    Etids[ atid ] = etid;
665 >    ElectrostaticMap.push_back(electrostaticAtomData);
666  
667 <    // Now, iterate over all known types and add to the mixing map:
668 <    
669 <    map<AtomType*, ElectrostaticAtomData>::iterator it;
670 <    for( it = ElectrostaticMap.begin(); it != ElectrostaticMap.end(); ++it) {
671 <      AtomType* atype2 = (*it).first;
672 <      ElectrostaticAtomData eaData2 = (*it).second;
673 <      if (eaData2.is_Fluctuating && electrostaticAtomData.is_Fluctuating) {
674 <        
667 >    if (electrostaticAtomData.is_Fluctuating) {
668 >      ret = FQtypes.insert( atid );
669 >      if (ret.second == false) {
670 >        sprintf( painCave.errMsg,
671 >                 "Electrostatic already had a previous fluctuating charge entry with ident %d\n",
672 >                 atid );
673 >        painCave.severity = OPENMD_INFO;
674 >        painCave.isFatal = 0;
675 >        simError();        
676 >      }
677 >      FQtids[atid] = fqtid;
678 >      Jij[fqtid].resize(nFlucq_);
679 >
680 >      // Now, iterate over all known fluctuating and add to the
681 >      // coulomb integral map:
682 >      
683 >      std::set<int>::iterator it;
684 >      for( it = FQtypes.begin(); it != FQtypes.end(); ++it) {    
685 >        int etid2 = Etids[ (*it) ];
686 >        int fqtid2 = FQtids[ (*it) ];
687 >        ElectrostaticAtomData eaData2 = ElectrostaticMap[ etid2 ];
688          RealType a = electrostaticAtomData.slaterZeta;
689          RealType b = eaData2.slaterZeta;
690          int m = electrostaticAtomData.slaterN;
691          int n = eaData2.slaterN;
692 <
692 >        
693          // Create the spline of the coulombic integral for s-type
694          // Slater orbitals.  Add a 2 angstrom safety window to deal
695          // with cutoffGroups that have charged atoms longer than the
696          // cutoffRadius away from each other.
697 <
697 >        
698          RealType rval;
699          RealType dr = (cutoffRadius_ + 2.0) / RealType(np_ - 1);
700          vector<RealType> rvals;
# Line 709 | Line 711 | namespace OpenMD {
711          
712          CubicSpline* J = new CubicSpline();
713          J->addPoints(rvals, Jvals);
714 <        
715 <        pair<AtomType*, AtomType*> key1, key2;
716 <        key1 = make_pair(atomType, atype2);
717 <        key2 = make_pair(atype2, atomType);
718 <        
717 <        Jij[key1] = J;
718 <        Jij[key2] = J;
719 <      }
720 <    }
721 <
714 >        Jij[fqtid][fqtid2] = J;
715 >        Jij[fqtid2].resize( nFlucq_ );
716 >        Jij[fqtid2][fqtid] = J;
717 >      }      
718 >    }      
719      return;
720    }
721    
# Line 744 | Line 741 | namespace OpenMD {
741  
742    void Electrostatic::calcForce(InteractionData &idat) {
743  
744 <    RealType C_a, C_b;  // Charges
745 <    Vector3d D_a, D_b;  // Dipoles (space-fixed)
746 <    Mat3x3d  Q_a, Q_b;  // Quadrupoles (space-fixed)
744 >    if (!initialized_) initialize();
745 >    
746 >    data1 = ElectrostaticMap[Etids[idat.atid1]];
747 >    data2 = ElectrostaticMap[Etids[idat.atid2]];
748  
749 <    RealType ri;                                 // Distance utility scalar
750 <    RealType rdDa, rdDb;                         // Dipole utility scalars
751 <    Vector3d rxDa, rxDb;                         // Dipole utility vectors
752 <    RealType rdQar, rdQbr, trQa, trQb;           // Quadrupole utility scalars
753 <    Vector3d Qar, Qbr, rQa, rQb, rxQar, rxQbr;   // Quadrupole utility vectors
754 <    RealType pref;
755 <
756 <    RealType DadDb, trQaQb, DadQbr, DbdQar;       // Cross-interaction scalars
759 <    RealType rQaQbr;
760 <    Vector3d DaxDb, DadQb, DbdQa, DaxQbr, DbxQar; // Cross-interaction vectors
761 <    Vector3d rQaQb, QaQbr, QaxQb, rQaxQbr;
762 <    Mat3x3d  QaQb;                                // Cross-interaction matrices
763 <
764 <    RealType U(0.0);  // Potential
765 <    Vector3d F(0.0);  // Force
766 <    Vector3d Ta(0.0); // Torque on site a
767 <    Vector3d Tb(0.0); // Torque on site b
768 <    Vector3d Ea(0.0); // Electric field at site a
769 <    Vector3d Eb(0.0); // Electric field at site b
770 <    RealType dUdCa(0.0); // fluctuating charge force at site a
771 <    RealType dUdCb(0.0); // fluctuating charge force at site a
749 >    U = 0.0;  // Potential
750 >    F.zero();  // Force
751 >    Ta.zero(); // Torque on site a
752 >    Tb.zero(); // Torque on site b
753 >    Ea.zero(); // Electric field at site a
754 >    Eb.zero(); // Electric field at site b
755 >    dUdCa = 0.0; // fluctuating charge force at site a
756 >    dUdCb = 0.0; // fluctuating charge force at site a
757      
758      // Indirect interactions mediated by the reaction field.
759 <    RealType indirect_Pot(0.0);  // Potential
760 <    Vector3d indirect_F(0.0);    // Force
761 <    Vector3d indirect_Ta(0.0);   // Torque on site a
762 <    Vector3d indirect_Tb(0.0);   // Torque on site b
759 >    indirect_Pot = 0.0;   // Potential
760 >    indirect_F.zero();    // Force
761 >    indirect_Ta.zero();   // Torque on site a
762 >    indirect_Tb.zero();   // Torque on site b
763  
764      // Excluded potential that is still computed for fluctuating charges
765 <    RealType excluded_Pot(0.0);
765 >    excluded_Pot= 0.0;
766  
782    RealType rfContrib, coulInt;
783    
784    // spline for coulomb integral
785    CubicSpline* J;
767  
787    if (!initialized_) initialize();
788    
789    ElectrostaticAtomData data1 = ElectrostaticMap[idat.atypes.first];
790    ElectrostaticAtomData data2 = ElectrostaticMap[idat.atypes.second];
791    
768      // some variables we'll need independent of electrostatic type:
769  
770      ri = 1.0 /  *(idat.rij);
771 <    Vector3d rhat =  *(idat.d)  * ri;
771 >    rhat =  *(idat.d)  * ri;
772        
773      // logicals
774  
775 <    bool a_is_Charge = data1.is_Charge;
776 <    bool a_is_Dipole = data1.is_Dipole;
777 <    bool a_is_Quadrupole = data1.is_Quadrupole;
778 <    bool a_is_Fluctuating = data1.is_Fluctuating;
775 >    a_is_Charge = data1.is_Charge;
776 >    a_is_Dipole = data1.is_Dipole;
777 >    a_is_Quadrupole = data1.is_Quadrupole;
778 >    a_is_Fluctuating = data1.is_Fluctuating;
779  
780 <    bool b_is_Charge = data2.is_Charge;
781 <    bool b_is_Dipole = data2.is_Dipole;
782 <    bool b_is_Quadrupole = data2.is_Quadrupole;
783 <    bool b_is_Fluctuating = data2.is_Fluctuating;
780 >    b_is_Charge = data2.is_Charge;
781 >    b_is_Dipole = data2.is_Dipole;
782 >    b_is_Quadrupole = data2.is_Quadrupole;
783 >    b_is_Fluctuating = data2.is_Fluctuating;
784  
785      // Obtain all of the required radial function values from the
786      // spline structures:
# Line 911 | Line 887 | namespace OpenMD {
887      }
888      
889      if ((a_is_Fluctuating || b_is_Fluctuating) && idat.excluded) {
890 <      J = Jij[idat.atypes];
890 >      J = Jij[FQtids[idat.atid1]][FQtids[idat.atid2]];
891      }    
892      
893      if (a_is_Charge) {    
# Line 1102 | Line 1078 | namespace OpenMD {
1078  
1079          Tb += pref * 2.0 * cross(rhat,Qbr) * rdQar * v43;
1080  
1105        //  cerr << " tsum = " << Ta + Tb - cross(  *(idat.d) , F ) << "\n";
1081        }
1082      }
1083  
# Line 1149 | Line 1124 | namespace OpenMD {
1124  
1125      if (!initialized_) initialize();
1126  
1127 <    ElectrostaticAtomData data = ElectrostaticMap[sdat.atype];
1127 >    ElectrostaticAtomData data = ElectrostaticMap[Etids[sdat.atid]];
1128      
1129      // logicals
1130      bool i_is_Charge = data.is_Charge;
1131      bool i_is_Dipole = data.is_Dipole;
1132 +    bool i_is_Quadrupole = data.is_Quadrupole;
1133      bool i_is_Fluctuating = data.is_Fluctuating;
1134      RealType C_a = data.fixedCharge;  
1135 <    RealType self, preVal, DadDa;
1136 <    
1135 >    RealType self(0.0), preVal, DdD, trQ, trQQ;
1136 >
1137 >    if (i_is_Dipole) {
1138 >      DdD = data.dipole.lengthSquare();
1139 >    }
1140 >        
1141      if (i_is_Fluctuating) {
1142        C_a += *(sdat.flucQ);
1143        // dVdFQ is really a force, so this is negative the derivative
# Line 1178 | Line 1158 | namespace OpenMD {
1158        }
1159  
1160        if (i_is_Dipole) {
1161 <        DadDa = data.dipole.lengthSquare();
1182 <        (*(sdat.pot))[ELECTROSTATIC_FAMILY] -= pre22_ * preRF_ * DadDa;
1161 >        (*(sdat.pot))[ELECTROSTATIC_FAMILY] -= pre22_ * preRF_ * DdD;
1162        }
1163        
1164        break;
1165        
1166      case esm_SHIFTED_FORCE:
1167      case esm_SHIFTED_POTENTIAL:
1168 <      if (i_is_Charge) {
1169 <        self = - selfMult_ * C_a * (C_a + *(sdat.skippedCharge)) * pre11_;
1170 <        (*(sdat.pot))[ELECTROSTATIC_FAMILY] += self;
1168 >    case esm_TAYLOR_SHIFTED:
1169 >    case esm_EWALD_FULL:
1170 >      if (i_is_Charge)
1171 >        self += selfMult1_ * pre11_ * C_a * (C_a + *(sdat.skippedCharge));      
1172 >      if (i_is_Dipole)
1173 >        self += selfMult2_ * pre22_ * DdD;      
1174 >      if (i_is_Quadrupole) {
1175 >        trQ = data.quadrupole.trace();
1176 >        trQQ = (data.quadrupole * data.quadrupole).trace();
1177 >        self += selfMult4_ * pre44_ * (2.0*trQQ + trQ*trQ);
1178 >        if (i_is_Charge)
1179 >          self -= selfMult2_ * pre14_ * 2.0 * C_a * trQ;
1180        }
1181 +      (*(sdat.pot))[ELECTROSTATIC_FAMILY] += self;      
1182        break;
1183      default:
1184        break;
# Line 1203 | Line 1192 | namespace OpenMD {
1192      // cases.
1193      return 12.0;
1194    }
1195 +
1196 +
1197 +  void Electrostatic::ReciprocalSpaceSum(potVec& pot) {
1198 +    
1199 +    RealType kPot = 0.0;
1200 +    RealType kVir = 0.0;
1201 +    
1202 +    const RealType mPoleConverter = 0.20819434; // converts from the
1203 +                                                // internal units of
1204 +                                                // Debye (for dipoles)
1205 +                                                // or Debye-angstroms
1206 +                                                // (for quadrupoles) to
1207 +                                                // electron angstroms or
1208 +                                                // electron-angstroms^2
1209 +    
1210 +    const RealType eConverter = 332.0637778; // convert the
1211 +                                             // Charge-Charge
1212 +                                             // electrostatic
1213 +                                             // interactions into kcal /
1214 +                                             // mol assuming distances
1215 +                                             // are measured in
1216 +                                             // angstroms.
1217 +
1218 +    Mat3x3d hmat = info_->getSnapshotManager()->getCurrentSnapshot()->getHmat();
1219 +    Vector3d box = hmat.diagonals();
1220 +    RealType boxMax = box.max();
1221 +    
1222 +    //int kMax = int(2.0 * M_PI / (pow(dampingAlpha_,2)*cutoffRadius_ * boxMax) );
1223 +    int kMax = 7;
1224 +    int kSqMax = kMax*kMax + 2;
1225 +    
1226 +    int kLimit = kMax+1;
1227 +    int kLim2 = 2*kMax+1;
1228 +    int kSqLim = kSqMax;
1229 +    
1230 +    vector<RealType> AK(kSqLim+1, 0.0);
1231 +    RealType xcl = 2.0 * M_PI / box.x();
1232 +    RealType ycl = 2.0 * M_PI / box.y();
1233 +    RealType zcl = 2.0 * M_PI / box.z();
1234 +    RealType rcl = 2.0 * M_PI / boxMax;
1235 +    RealType rvol = 2.0 * M_PI /(box.x() * box.y() * box.z());
1236 +    
1237 +    if(dampingAlpha_ < 1.0e-12) return;
1238 +    
1239 +    RealType ralph = -0.25/pow(dampingAlpha_,2);
1240 +    
1241 +    // Calculate and store exponential factors  
1242 +    
1243 +    vector<vector<Vector3d> > eCos;
1244 +    vector<vector<Vector3d> > eSin;
1245 +    
1246 +    int nMax = info_->getNAtoms();
1247 +    
1248 +    eCos.resize(kLimit+1);
1249 +    eSin.resize(kLimit+1);
1250 +    for (int j = 0; j < kLimit+1; j++) {
1251 +      eCos[j].resize(nMax);
1252 +      eSin[j].resize(nMax);
1253 +    }
1254 +    
1255 +    Vector3d t( 2.0 * M_PI );
1256 +    t.Vdiv(t, box);
1257 +
1258 +    
1259 +    SimInfo::MoleculeIterator mi;
1260 +    Molecule::AtomIterator ai;
1261 +    int i;
1262 +    Vector3d r;
1263 +    Vector3d tt;
1264 +    Vector3d w;
1265 +    Vector3d u;
1266 +    Vector3d a;
1267 +    Vector3d b;
1268 +    
1269 +    for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1270 +         mol = info_->nextMolecule(mi)) {
1271 +      for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1272 +          atom = mol->nextAtom(ai)) {  
1273 +        
1274 +        i = atom->getLocalIndex();
1275 +        r = atom->getPos();
1276 +        info_->getSnapshotManager()->getCurrentSnapshot()->wrapVector(r);
1277 +        
1278 +        tt.Vmul(t, r);
1279 +
1280 +        
1281 +        eCos[1][i] = Vector3d(1.0, 1.0, 1.0);
1282 +        eSin[1][i] = Vector3d(0.0, 0.0, 0.0);
1283 +        eCos[2][i] = Vector3d(cos(tt.x()), cos(tt.y()), cos(tt.z()));
1284 +        eSin[2][i] = Vector3d(sin(tt.x()), sin(tt.y()), sin(tt.z()));
1285 +
1286 +        u = eCos[2][i];
1287 +        w = eSin[2][i];
1288 +        
1289 +        for(int l = 3; l <= kLimit; l++) {
1290 +          eCos[l][i].x() = eCos[l-1][i].x()*eCos[2][i].x() - eSin[l-1][i].x()*eSin[2][i].x();
1291 +          eCos[l][i].y() = eCos[l-1][i].y()*eCos[2][i].y() - eSin[l-1][i].y()*eSin[2][i].y();
1292 +          eCos[l][i].z() = eCos[l-1][i].z()*eCos[2][i].z() - eSin[l-1][i].z()*eSin[2][i].z();
1293 +          
1294 +          eSin[l][i].x() = eSin[l-1][i].x()*eCos[2][i].x() + eCos[l-1][i].x()*eSin[2][i].x();
1295 +          eSin[l][i].y() = eSin[l-1][i].y()*eCos[2][i].y() + eCos[l-1][i].y()*eSin[2][i].y();
1296 +          eSin[l][i].z() = eSin[l-1][i].z()*eCos[2][i].z() + eCos[l-1][i].z()*eSin[2][i].z();
1297 +
1298 +
1299 +          // a.Vmul(eCos[l-1][i], u);
1300 +          // b.Vmul(eSin[l-1][i], w);
1301 +          // eCos[l][i] = a - b;
1302 +          // a.Vmul(eSin[l-1][i], u);
1303 +          // b.Vmul(eCos[l-1][i], w);
1304 +          // eSin[l][i] = a + b;
1305 +        
1306 +        }
1307 +      }
1308 +    }
1309 +    
1310 +    // Calculate and store AK coefficients:
1311 +    
1312 +    RealType eksq = 1.0;
1313 +    RealType expf = 0.0;
1314 +    if (ralph < 0.0) expf = exp(ralph*rcl*rcl);
1315 +    for (i = 1; i <= kSqLim; i++) {
1316 +      RealType rksq = float(i)*rcl*rcl;
1317 +      eksq = expf*eksq;
1318 +      AK[i] = eConverter * eksq/rksq;
1319 +    }
1320 +    
1321 +    /*
1322 +     * Loop over all k vectors k = 2 pi (ll/Lx, mm/Ly, nn/Lz)
1323 +     * the values of ll, mm and nn are selected so that the symmetry of
1324 +     * reciprocal lattice is taken into account i.e. the following
1325 +     * rules apply.
1326 +     *
1327 +     * ll ranges over the values 0 to kMax only.
1328 +     *
1329 +     * mm ranges over 0 to kMax when ll=0 and over
1330 +     *            -kMax to kMax otherwise.
1331 +     * nn ranges over 1 to kMax when ll=mm=0 and over
1332 +     *            -kMax to kMax otherwise.
1333 +     *
1334 +     * Hence the result of the summation must be doubled at the end.    
1335 +     */
1336 +    
1337 +    std::vector<RealType> clm(nMax, 0.0);
1338 +    std::vector<RealType> slm(nMax, 0.0);
1339 +    std::vector<RealType> ckr(nMax, 0.0);
1340 +    std::vector<RealType> skr(nMax, 0.0);
1341 +    std::vector<RealType> ckc(nMax, 0.0);
1342 +    std::vector<RealType> cks(nMax, 0.0);
1343 +    std::vector<RealType> dkc(nMax, 0.0);
1344 +    std::vector<RealType> dks(nMax, 0.0);
1345 +    std::vector<RealType> qkc(nMax, 0.0);
1346 +    std::vector<RealType> qks(nMax, 0.0);
1347 +    std::vector<Vector3d> dxk(nMax, V3Zero);
1348 +    std::vector<Vector3d> qxk(nMax, V3Zero);
1349 +    
1350 +    int mMin = kLimit;
1351 +    int nMin = kLimit + 1;
1352 +    for (int l = 1; l <= kLimit; l++) {
1353 +      int ll = l - 1;
1354 +      RealType rl = xcl * float(ll);
1355 +      for (int mmm = mMin; mmm <= kLim2; mmm++) {
1356 +        int mm = mmm - kLimit;
1357 +        int m = abs(mm) + 1;
1358 +        RealType rm = ycl * float(mm);
1359 +        // Set temporary products of exponential terms
1360 +        for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1361 +             mol = info_->nextMolecule(mi)) {
1362 +          for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1363 +              atom = mol->nextAtom(ai)) {
1364 +            
1365 +            i = atom->getLocalIndex();
1366 +            if(mm < 0) {
1367 +              clm[i] = eCos[l][i].x()*eCos[m][i].y()
1368 +                     + eSin[l][i].x()*eSin[m][i].y();
1369 +              slm[i] = eSin[l][i].x()*eCos[m][i].y()
1370 +                     - eSin[m][i].y()*eCos[l][i].x();
1371 +            } else {
1372 +              clm[i] = eCos[l][i].x()*eCos[m][i].y()
1373 +                     - eSin[l][i].x()*eSin[m][i].y();
1374 +              slm[i] = eSin[l][i].x()*eCos[m][i].y()
1375 +                     + eSin[m][i].y()*eCos[l][i].x();              
1376 +            }
1377 +          }
1378 +        }
1379 +        for (int nnn = nMin; nnn <= kLim2; nnn++) {
1380 +          int nn = nnn - kLimit;          
1381 +          int n = abs(nn) + 1;
1382 +          RealType rn = zcl * float(nn);
1383 +          // Test on magnitude of k vector:
1384 +          int kk=ll*ll + mm*mm + nn*nn;
1385 +          if(kk <= kSqLim) {
1386 +            Vector3d kVec = Vector3d(rl, rm, rn);
1387 +            Mat3x3d  k2 = outProduct(kVec, kVec);
1388 +            // Calculate exp(ikr) terms
1389 +            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1390 +                 mol = info_->nextMolecule(mi)) {
1391 +              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1392 +                  atom = mol->nextAtom(ai)) {
1393 +                i = atom->getLocalIndex();
1394 +                
1395 +                if (nn < 0) {
1396 +                  ckr[i]=clm[i]*eCos[n][i].z()+slm[i]*eSin[n][i].z();
1397 +                  skr[i]=slm[i]*eCos[n][i].z()-clm[i]*eSin[n][i].z();
1398 +                } else {
1399 +                  ckr[i]=clm[i]*eCos[n][i].z()-slm[i]*eSin[n][i].z();
1400 +                  skr[i]=slm[i]*eCos[n][i].z()+clm[i]*eSin[n][i].z();
1401 +                }
1402 +              }
1403 +            }
1404 +            
1405 +            // Calculate scalar and vector products for each site:
1406 +            
1407 +            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1408 +                 mol = info_->nextMolecule(mi)) {
1409 +              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1410 +                  atom = mol->nextAtom(ai)) {
1411 +                i = atom->getLocalIndex();
1412 +                int atid = atom->getAtomType()->getIdent();
1413 +                ElectrostaticAtomData data = ElectrostaticMap[Etids[atid]];
1414 +                              
1415 +                if (data.is_Charge) {
1416 +                  RealType C = data.fixedCharge;
1417 +                  if (atom->isFluctuatingCharge()) C += atom->getFlucQPos();
1418 +                  ckc[i] = C * ckr[i];
1419 +                  cks[i] = C * skr[i];
1420 +                }
1421 +                
1422 +                if (data.is_Dipole) {
1423 +                  Vector3d D = atom->getDipole() * mPoleConverter;
1424 +                  RealType dk = dot(D, kVec);
1425 +                  dxk[i] = cross(D, kVec);
1426 +                  dkc[i] = dk * ckr[i];
1427 +                  dks[i] = dk * skr[i];
1428 +                }
1429 +                if (data.is_Quadrupole) {
1430 +                  Mat3x3d Q = atom->getQuadrupole();
1431 +                  Q *= mPoleConverter;
1432 +                  RealType qk = - doubleDot(Q, k2);
1433 +                  // RealType qk = -( Q * k2 ).trace();
1434 +                  qxk[i] = -2.0 * cross(k2, Q);
1435 +                  qkc[i] = qk * ckr[i];
1436 +                  qks[i] = qk * skr[i];
1437 +                }              
1438 +              }
1439 +            }
1440 +
1441 +            // calculate vector sums
1442 +            
1443 +            RealType ckcs = std::accumulate(ckc.begin(),ckc.end(),0.0);
1444 +            RealType ckss = std::accumulate(cks.begin(),cks.end(),0.0);
1445 +            RealType dkcs = std::accumulate(dkc.begin(),dkc.end(),0.0);
1446 +            RealType dkss = std::accumulate(dks.begin(),dks.end(),0.0);
1447 +            RealType qkcs = std::accumulate(qkc.begin(),qkc.end(),0.0);
1448 +            RealType qkss = std::accumulate(qks.begin(),qks.end(),0.0);
1449 +
1450 +            
1451 + #ifdef IS_MPI
1452 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &ckcs, 1, MPI::REALTYPE,
1453 +                                      MPI::SUM);
1454 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &ckss, 1, MPI::REALTYPE,
1455 +                                      MPI::SUM);
1456 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &dkcs, 1, MPI::REALTYPE,
1457 +                                      MPI::SUM);
1458 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &dkss, 1, MPI::REALTYPE,
1459 +                                      MPI::SUM);
1460 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &qkcs, 1, MPI::REALTYPE,
1461 +                                      MPI::SUM);
1462 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &qkss, 1, MPI::REALTYPE,
1463 +                                      MPI::SUM);
1464 + #endif        
1465 +            
1466 +            // Accumulate potential energy and virial contribution:
1467 +
1468 +            kPot += 2.0 * rvol * AK[kk]*((ckss+dkcs-qkss)*(ckss+dkcs-qkss)
1469 +                                         + (ckcs-dkss-qkcs)*(ckcs-dkss-qkss));
1470 +
1471 +            kVir -= 2.0 * rvol * AK[kk]*(ckcs*ckcs+ckss*ckss
1472 +                                         +4.0*(ckss*dkcs-ckcs*dkss)
1473 +                                         +3.0*(dkcs*dkcs+dkss*dkss)
1474 +                                         -6.0*(ckss*qkss+ckcs*qkcs)
1475 +                                         +8.0*(dkss*qkcs-dkcs*qkss)
1476 +                                         +5.0*(qkss*qkss+qkcs*qkcs));
1477 +            
1478 +            // Calculate force and torque for each site:
1479 +            
1480 +            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1481 +                 mol = info_->nextMolecule(mi)) {
1482 +              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1483 +                  atom = mol->nextAtom(ai)) {
1484 +                
1485 +                i = atom->getLocalIndex();
1486 +                int atid = atom->getAtomType()->getIdent();
1487 +                ElectrostaticAtomData data = ElectrostaticMap[Etids[atid]];
1488 +                
1489 +                RealType qfrc = AK[kk]*((cks[i]+dkc[i]-qks[i])*(ckcs-dkss-qkcs)
1490 +                                     - (ckc[i]-dks[i]-qkc[i])*(ckss+dkcs-qkss));
1491 +                RealType qtrq1 = AK[kk]*(skr[i]*(ckcs-dkss-qkcs)
1492 +                                         -ckr[i]*(ckss+dkcs-qkss));
1493 +                RealType qtrq2 = 2.0*AK[kk]*(ckr[i]*(ckcs-dkss-qkcs)+
1494 +                                             skr[i]*(ckss+dkcs-qkss));
1495 +              
1496 +                atom->addFrc( 4.0 * rvol * qfrc * kVec );
1497 +                
1498 +                if (data.is_Dipole) {
1499 +                  atom->addTrq( 4.0 * rvol * qtrq1 * dxk[i] );
1500 +                }
1501 +                if (data.is_Quadrupole) {
1502 +                  atom->addTrq( 4.0 * rvol * qtrq2 * qxk[i] );
1503 +                }
1504 +              }
1505 +            }
1506 +          }
1507 +        }
1508 +        nMin = 1;
1509 +      }
1510 +      mMin = 1;
1511 +    }
1512 +    cerr << "kPot = " << kPot << "\n";
1513 +    pot[ELECTROSTATIC_FAMILY] += kPot;  
1514 +  }
1515   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines