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 1921 by gezelter, Thu Aug 1 18:23:07 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;
291 <    
290 >    db0c_5 =                    -15.0*r*b3c + 10.0*r2*r*b4c - r2*r2*r*b5c;  
291 >
292 >    if (summationMethod_ == esm_EWALD_FULL) {
293 >      selfMult1_ *= 2.0;
294 >      selfMult2_ *= 2.0;
295 >      selfMult4_ *= 2.0;
296 >    } else {
297 >      selfMult1_ -= b0c;
298 >      selfMult2_ += (db0c_2 + 2.0*db0c_1*ric) /  3.0;
299 >      selfMult4_ -= (db0c_4 + 4.0*db0c_3*ric) / 15.0;
300 >    }
301  
302      // working variables for the splines:
303      RealType ri, ri2;
# Line 304 | Line 333 | namespace OpenMD {
333      vector<RealType> v21v, v22v;
334      vector<RealType> v31v, v32v;
335      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    */
336  
337      for (int i = 1; i < np_ + 1; i++) {
338        r = RealType(i) * dx;
# Line 398 | Line 419 | namespace OpenMD {
419          v11 = g - gc - rmRc*hc;
420          v21 = g*ri - gc*ric - rmRc*(hc - gc*ric)*ric;
421          v22 = h - g*ri - (hc - gc*ric) - rmRc*(sc - (hc - gc*ric)*ric);
422 <        v31 = (h-g*ri)*ri - (hc-g*ric)*ric - rmRc*(sc-2.0*(hc-gc*ric)*ric)*ric;
422 >        v31 = (h-g*ri)*ri - (hc-gc*ric)*ric - rmRc*(sc-2.0*(hc-gc*ric)*ric)*ric;
423          v32 = (s - 3.0*(h-g*ri)*ri) - (sc - 3.0*(hc-gc*ric)*ric)
424            - rmRc*(tc - 3.0*(sc-2.0*(hc-gc*ric)*ric)*ric);
425          v41 = (h - g*ri)*ri2 - (hc - gc*ric)*ric2
# Line 455 | Line 476 | namespace OpenMD {
476          v11 = g - gc;
477          v21 = g*ri - gc*ric;
478          v22 = h - g*ri - (hc - gc*ric);
479 <        v31 = (h-g*ri)*ri - (hc-g*ric)*ric;
479 >        v31 = (h-g*ri)*ri - (hc-gc*ric)*ric;
480          v32 = (s - 3.0*(h-g*ri)*ri) - (sc - 3.0*(hc-gc*ric)*ric);
481          v41 = (h - g*ri)*ri2 - (hc - gc*ric)*ric2;
482          v42 = (s-3.0*(h-g*ri)*ri)*ri - (sc-3.0*(hc-gc*ric)*ric)*ric;        
# Line 476 | Line 497 | namespace OpenMD {
497  
498        case esm_SWITCHING_FUNCTION:
499        case esm_HARD:
500 +      case esm_EWALD_FULL:
501  
502          v01 = f;
503          v11 = g;
# Line 515 | Line 537 | namespace OpenMD {
537          v11 = g - gc;
538          v21 = g*ri - gc*ric;
539          v22 = h - g*ri - (hc - gc*ric);
540 <        v31 = (h-g*ri)*ri - (hc-g*ric)*ric;
540 >        v31 = (h-g*ri)*ri - (hc-gc*ric)*ric;
541          v32 = (s - 3.0*(h-g*ri)*ri) - (sc - 3.0*(hc-gc*ric)*ric);
542          v41 = (h - g*ri)*ri2 - (hc - gc*ric)*ric2;
543          v42 = (s-3.0*(h-g*ri)*ri)*ri - (sc-3.0*(hc-gc*ric)*ric)*ric;        
# Line 534 | Line 556 | namespace OpenMD {
556  
557          break;
558                  
537      case esm_EWALD_FULL:
559        case esm_EWALD_PME:
560        case esm_EWALD_SPME:
561        default :
# Line 563 | Line 584 | namespace OpenMD {
584        v41v.push_back(v41);
585        v42v.push_back(v42);
586        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      */
587      }
588  
589      // construct the spline structures and fill them with the values we've
# Line 598 | Line 608 | namespace OpenMD {
608      v43s = new CubicSpline();
609      v43s->addPoints(rv, v43v);
610  
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
611      haveElectroSplines_ = true;
612  
613      initialized_ = true;
614    }
615        
616    void Electrostatic::addType(AtomType* atomType){
617 <
617 >    
618      ElectrostaticAtomData electrostaticAtomData;
619      electrostaticAtomData.is_Charge = false;
620      electrostaticAtomData.is_Dipole = false;
# Line 661 | Line 650 | namespace OpenMD {
650        electrostaticAtomData.slaterZeta = fqa.getSlaterZeta();
651      }
652  
653 <    pair<map<int,AtomType*>::iterator,bool> ret;    
654 <    ret = ElectrostaticList.insert( pair<int,AtomType*>(atomType->getIdent(),
655 <                                                        atomType) );
653 >    int atid = atomType->getIdent();
654 >    int etid = Etypes.size();
655 >    int fqtid = FQtypes.size();
656 >
657 >    pair<set<int>::iterator,bool> ret;    
658 >    ret = Etypes.insert( atid );
659      if (ret.second == false) {
660        sprintf( painCave.errMsg,
661                 "Electrostatic already had a previous entry with ident %d\n",
662 <               atomType->getIdent() );
662 >               atid);
663        painCave.severity = OPENMD_INFO;
664        painCave.isFatal = 0;
665        simError();        
666      }
667      
668 <    ElectrostaticMap[atomType] = electrostaticAtomData;  
668 >    Etids[ atid ] = etid;
669 >    ElectrostaticMap.push_back(electrostaticAtomData);
670  
671 <    // Now, iterate over all known types and add to the mixing map:
672 <    
673 <    map<AtomType*, ElectrostaticAtomData>::iterator it;
674 <    for( it = ElectrostaticMap.begin(); it != ElectrostaticMap.end(); ++it) {
675 <      AtomType* atype2 = (*it).first;
676 <      ElectrostaticAtomData eaData2 = (*it).second;
677 <      if (eaData2.is_Fluctuating && electrostaticAtomData.is_Fluctuating) {
678 <        
671 >    if (electrostaticAtomData.is_Fluctuating) {
672 >      ret = FQtypes.insert( atid );
673 >      if (ret.second == false) {
674 >        sprintf( painCave.errMsg,
675 >                 "Electrostatic already had a previous fluctuating charge entry with ident %d\n",
676 >                 atid );
677 >        painCave.severity = OPENMD_INFO;
678 >        painCave.isFatal = 0;
679 >        simError();        
680 >      }
681 >      FQtids[atid] = fqtid;
682 >      Jij[fqtid].resize(nFlucq_);
683 >
684 >      // Now, iterate over all known fluctuating and add to the
685 >      // coulomb integral map:
686 >      
687 >      std::set<int>::iterator it;
688 >      for( it = FQtypes.begin(); it != FQtypes.end(); ++it) {    
689 >        int etid2 = Etids[ (*it) ];
690 >        int fqtid2 = FQtids[ (*it) ];
691 >        ElectrostaticAtomData eaData2 = ElectrostaticMap[ etid2 ];
692          RealType a = electrostaticAtomData.slaterZeta;
693          RealType b = eaData2.slaterZeta;
694          int m = electrostaticAtomData.slaterN;
695          int n = eaData2.slaterN;
696 <
696 >        
697          // Create the spline of the coulombic integral for s-type
698          // Slater orbitals.  Add a 2 angstrom safety window to deal
699          // with cutoffGroups that have charged atoms longer than the
700          // cutoffRadius away from each other.
701 <
701 >        
702          RealType rval;
703          RealType dr = (cutoffRadius_ + 2.0) / RealType(np_ - 1);
704          vector<RealType> rvals;
# Line 709 | Line 715 | namespace OpenMD {
715          
716          CubicSpline* J = new CubicSpline();
717          J->addPoints(rvals, Jvals);
718 <        
719 <        pair<AtomType*, AtomType*> key1, key2;
720 <        key1 = make_pair(atomType, atype2);
721 <        key2 = make_pair(atype2, atomType);
722 <        
717 <        Jij[key1] = J;
718 <        Jij[key2] = J;
719 <      }
720 <    }
721 <
718 >        Jij[fqtid][fqtid2] = J;
719 >        Jij[fqtid2].resize( nFlucq_ );
720 >        Jij[fqtid2][fqtid] = J;
721 >      }      
722 >    }      
723      return;
724    }
725    
# Line 744 | Line 745 | namespace OpenMD {
745  
746    void Electrostatic::calcForce(InteractionData &idat) {
747  
748 <    RealType C_a, C_b;  // Charges
749 <    Vector3d D_a, D_b;  // Dipoles (space-fixed)
750 <    Mat3x3d  Q_a, Q_b;  // Quadrupoles (space-fixed)
748 >    if (!initialized_) initialize();
749 >    
750 >    data1 = ElectrostaticMap[Etids[idat.atid1]];
751 >    data2 = ElectrostaticMap[Etids[idat.atid2]];
752  
753 <    RealType ri;                                 // Distance utility scalar
754 <    RealType rdDa, rdDb;                         // Dipole utility scalars
755 <    Vector3d rxDa, rxDb;                         // Dipole utility vectors
756 <    RealType rdQar, rdQbr, trQa, trQb;           // Quadrupole utility scalars
757 <    Vector3d Qar, Qbr, rQa, rQb, rxQar, rxQbr;   // Quadrupole utility vectors
758 <    RealType pref;
759 <
760 <    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
753 >    U = 0.0;  // Potential
754 >    F.zero();  // Force
755 >    Ta.zero(); // Torque on site a
756 >    Tb.zero(); // Torque on site b
757 >    Ea.zero(); // Electric field at site a
758 >    Eb.zero(); // Electric field at site b
759 >    dUdCa = 0.0; // fluctuating charge force at site a
760 >    dUdCb = 0.0; // fluctuating charge force at site a
761      
762      // Indirect interactions mediated by the reaction field.
763 <    RealType indirect_Pot(0.0);  // Potential
764 <    Vector3d indirect_F(0.0);    // Force
765 <    Vector3d indirect_Ta(0.0);   // Torque on site a
766 <    Vector3d indirect_Tb(0.0);   // Torque on site b
763 >    indirect_Pot = 0.0;   // Potential
764 >    indirect_F.zero();    // Force
765 >    indirect_Ta.zero();   // Torque on site a
766 >    indirect_Tb.zero();   // Torque on site b
767  
768      // Excluded potential that is still computed for fluctuating charges
769 <    RealType excluded_Pot(0.0);
769 >    excluded_Pot= 0.0;
770  
782    RealType rfContrib, coulInt;
783    
784    // spline for coulomb integral
785    CubicSpline* J;
771  
787    if (!initialized_) initialize();
788    
789    ElectrostaticAtomData data1 = ElectrostaticMap[idat.atypes.first];
790    ElectrostaticAtomData data2 = ElectrostaticMap[idat.atypes.second];
791    
772      // some variables we'll need independent of electrostatic type:
773  
774      ri = 1.0 /  *(idat.rij);
775 <    Vector3d rhat =  *(idat.d)  * ri;
775 >    rhat =  *(idat.d)  * ri;
776        
777      // logicals
778  
779 <    bool a_is_Charge = data1.is_Charge;
780 <    bool a_is_Dipole = data1.is_Dipole;
781 <    bool a_is_Quadrupole = data1.is_Quadrupole;
782 <    bool a_is_Fluctuating = data1.is_Fluctuating;
779 >    a_is_Charge = data1.is_Charge;
780 >    a_is_Dipole = data1.is_Dipole;
781 >    a_is_Quadrupole = data1.is_Quadrupole;
782 >    a_is_Fluctuating = data1.is_Fluctuating;
783  
784 <    bool b_is_Charge = data2.is_Charge;
785 <    bool b_is_Dipole = data2.is_Dipole;
786 <    bool b_is_Quadrupole = data2.is_Quadrupole;
787 <    bool b_is_Fluctuating = data2.is_Fluctuating;
784 >    b_is_Charge = data2.is_Charge;
785 >    b_is_Dipole = data2.is_Dipole;
786 >    b_is_Quadrupole = data2.is_Quadrupole;
787 >    b_is_Fluctuating = data2.is_Fluctuating;
788  
789      // Obtain all of the required radial function values from the
790      // spline structures:
# Line 911 | Line 891 | namespace OpenMD {
891      }
892      
893      if ((a_is_Fluctuating || b_is_Fluctuating) && idat.excluded) {
894 <      J = Jij[idat.atypes];
894 >      J = Jij[FQtids[idat.atid1]][FQtids[idat.atid2]];
895      }    
896      
897      if (a_is_Charge) {    
# Line 1102 | Line 1082 | namespace OpenMD {
1082  
1083          Tb += pref * 2.0 * cross(rhat,Qbr) * rdQar * v43;
1084  
1105        //  cerr << " tsum = " << Ta + Tb - cross(  *(idat.d) , F ) << "\n";
1085        }
1086      }
1087  
# Line 1149 | Line 1128 | namespace OpenMD {
1128  
1129      if (!initialized_) initialize();
1130  
1131 <    ElectrostaticAtomData data = ElectrostaticMap[sdat.atype];
1131 >    ElectrostaticAtomData data = ElectrostaticMap[Etids[sdat.atid]];
1132      
1133      // logicals
1134      bool i_is_Charge = data.is_Charge;
1135      bool i_is_Dipole = data.is_Dipole;
1136 +    bool i_is_Quadrupole = data.is_Quadrupole;
1137      bool i_is_Fluctuating = data.is_Fluctuating;
1138      RealType C_a = data.fixedCharge;  
1139 <    RealType self, preVal, DadDa;
1140 <    
1139 >    RealType self(0.0), preVal, DdD, trQ, trQQ;
1140 >
1141 >    if (i_is_Dipole) {
1142 >      DdD = data.dipole.lengthSquare();
1143 >    }
1144 >        
1145      if (i_is_Fluctuating) {
1146        C_a += *(sdat.flucQ);
1147        // dVdFQ is really a force, so this is negative the derivative
# Line 1178 | Line 1162 | namespace OpenMD {
1162        }
1163  
1164        if (i_is_Dipole) {
1165 <        DadDa = data.dipole.lengthSquare();
1182 <        (*(sdat.pot))[ELECTROSTATIC_FAMILY] -= pre22_ * preRF_ * DadDa;
1165 >        (*(sdat.pot))[ELECTROSTATIC_FAMILY] -= pre22_ * preRF_ * DdD;
1166        }
1167        
1168        break;
1169        
1170      case esm_SHIFTED_FORCE:
1171      case esm_SHIFTED_POTENTIAL:
1172 <      if (i_is_Charge) {
1173 <        self = - selfMult_ * C_a * (C_a + *(sdat.skippedCharge)) * pre11_;
1174 <        (*(sdat.pot))[ELECTROSTATIC_FAMILY] += self;
1172 >    case esm_TAYLOR_SHIFTED:
1173 >    case esm_EWALD_FULL:
1174 >      if (i_is_Charge)
1175 >        self += selfMult1_ * pre11_ * C_a * (C_a + *(sdat.skippedCharge));      
1176 >      if (i_is_Dipole)
1177 >        self += selfMult2_ * pre22_ * DdD;      
1178 >      if (i_is_Quadrupole) {
1179 >        trQ = data.quadrupole.trace();
1180 >        trQQ = (data.quadrupole * data.quadrupole).trace();
1181 >        self += selfMult4_ * pre44_ * (2.0*trQQ + trQ*trQ);
1182 >        if (i_is_Charge)
1183 >          self -= selfMult2_ * pre14_ * 2.0 * C_a * trQ;
1184        }
1185 +      (*(sdat.pot))[ELECTROSTATIC_FAMILY] += self;      
1186        break;
1187      default:
1188        break;
# Line 1203 | Line 1196 | namespace OpenMD {
1196      // cases.
1197      return 12.0;
1198    }
1199 +
1200 +
1201 +  void Electrostatic::ReciprocalSpaceSum(potVec& pot) {
1202 +    
1203 +    RealType kPot = 0.0;
1204 +    RealType kVir = 0.0;
1205 +    
1206 +    const RealType mPoleConverter = 0.20819434; // converts from the
1207 +                                                // internal units of
1208 +                                                // Debye (for dipoles)
1209 +                                                // or Debye-angstroms
1210 +                                                // (for quadrupoles) to
1211 +                                                // electron angstroms or
1212 +                                                // electron-angstroms^2
1213 +    
1214 +    const RealType eConverter = 332.0637778; // convert the
1215 +                                             // Charge-Charge
1216 +                                             // electrostatic
1217 +                                             // interactions into kcal /
1218 +                                             // mol assuming distances
1219 +                                             // are measured in
1220 +                                             // angstroms.
1221 +
1222 +    Mat3x3d hmat = info_->getSnapshotManager()->getCurrentSnapshot()->getHmat();
1223 +    Vector3d box = hmat.diagonals();
1224 +    RealType boxMax = box.max();
1225 +    
1226 +    cerr << "da = " << dampingAlpha_ << " rc = " << cutoffRadius_ << "\n";
1227 +    cerr << "boxMax = " << boxMax << "\n";
1228 +    //int kMax = int(2.0 * M_PI / (pow(dampingAlpha_,2)*cutoffRadius_ * boxMax) );
1229 +    int kMax = 5;
1230 +    cerr << "kMax = " << kMax << "\n";
1231 +    int kSqMax = kMax*kMax + 2;
1232 +    
1233 +    int kLimit = kMax+1;
1234 +    int kLim2 = 2*kMax+1;
1235 +    int kSqLim = kSqMax;
1236 +    
1237 +    vector<RealType> AK(kSqLim+1, 0.0);
1238 +    RealType xcl = 2.0 * M_PI / box.x();
1239 +    RealType ycl = 2.0 * M_PI / box.y();
1240 +    RealType zcl = 2.0 * M_PI / box.z();
1241 +    RealType rcl = 2.0 * M_PI / boxMax;
1242 +    RealType rvol = 2.0 * M_PI /(box.x() * box.y() * box.z());
1243 +    
1244 +    if(dampingAlpha_ < 1.0e-12) return;
1245 +    
1246 +    RealType ralph = -0.25/pow(dampingAlpha_,2);
1247 +    
1248 +    // Calculate and store exponential factors  
1249 +    
1250 +    vector<vector<Vector3d> > eCos;
1251 +    vector<vector<Vector3d> > eSin;
1252 +    
1253 +    int nMax = info_->getNAtoms();
1254 +    
1255 +    eCos.resize(kLimit+1);
1256 +    eSin.resize(kLimit+1);
1257 +    for (int j = 0; j < kLimit+1; j++) {
1258 +      eCos[j].resize(nMax);
1259 +      eSin[j].resize(nMax);
1260 +    }
1261 +    
1262 +    Vector3d t( 2.0 * M_PI );
1263 +    t.Vdiv(t, box);
1264 +    
1265 +    SimInfo::MoleculeIterator mi;
1266 +    Molecule::AtomIterator ai;
1267 +    int i;
1268 +    Vector3d r;
1269 +    Vector3d tt;
1270 +    Vector3d w;
1271 +    Vector3d u;
1272 +    Vector3d a;
1273 +    Vector3d b;
1274 +    
1275 +    for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1276 +         mol = info_->nextMolecule(mi)) {
1277 +      for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1278 +          atom = mol->nextAtom(ai)) {  
1279 +        
1280 +        i = atom->getLocalIndex();
1281 +        r = atom->getPos();
1282 +        info_->getSnapshotManager()->getCurrentSnapshot()->wrapVector(r);
1283 +        
1284 +        // Shift so that all coordinates are in the range [0,L]:
1285 +
1286 +        r += box/2.0;
1287 +
1288 +        tt.Vmul(t, r);
1289 +
1290 +        //cerr << "tt = " << tt << "\n";
1291 +        
1292 +        eCos[1][i] = Vector3d(1.0, 1.0, 1.0);
1293 +        eSin[1][i] = Vector3d(0.0, 0.0, 0.0);
1294 +        eCos[2][i] = Vector3d(cos(tt.x()), cos(tt.y()), cos(tt.z()));
1295 +        eSin[2][i] = Vector3d(sin(tt.x()), sin(tt.y()), sin(tt.z()));
1296 +        u = eCos[2][i];
1297 +        w = eSin[2][i];
1298 +        
1299 +        for(int l = 3; l <= kLimit; l++) {
1300 +          a.Vmul(eCos[l-1][i], u);
1301 +          b.Vmul(eSin[l-1][i], w);
1302 +          eCos[l][i] = a - b;
1303 +          a.Vmul(eSin[l-1][i], u);
1304 +          b.Vmul(eCos[l-1][i], w);
1305 +          eSin[l][i] = a + b;
1306 +        }
1307 +      }
1308 +    }
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(kVec, D);
1425 +                  dxk[i] = cross(kVec, D);
1426 +                  dkc[i] = dk * ckr[i];
1427 +                  dks[i] = dk * skr[i];
1428 +                }
1429 +                if (data.is_Quadrupole) {
1430 +                  Mat3x3d Q = atom->getQuadrupole();
1431 +                  Q *= mPoleConverter;
1432 +                  RealType qk = -( Q * k2 ).trace();
1433 +                  qxk[i] = -2.0 * cross(k2, Q);
1434 +                  qkc[i] = qk * ckr[i];
1435 +                  qks[i] = qk * skr[i];
1436 +                }              
1437 +              }
1438 +            }
1439 +
1440 +            // calculate vector sums
1441 +            
1442 +            RealType ckcs = std::accumulate(ckc.begin(),ckc.end(),0.0);
1443 +            RealType ckss = std::accumulate(cks.begin(),cks.end(),0.0);
1444 +            RealType dkcs = std::accumulate(dkc.begin(),dkc.end(),0.0);
1445 +            RealType dkss = std::accumulate(dks.begin(),dks.end(),0.0);
1446 +            RealType qkcs = std::accumulate(qkc.begin(),qkc.end(),0.0);
1447 +            RealType qkss = std::accumulate(qks.begin(),qks.end(),0.0);
1448 +
1449 +            
1450 + #ifdef IS_MPI
1451 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &ckcs, 1, MPI::REALTYPE,
1452 +                                      MPI::SUM);
1453 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &ckss, 1, MPI::REALTYPE,
1454 +                                      MPI::SUM);
1455 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &dkcs, 1, MPI::REALTYPE,
1456 +                                      MPI::SUM);
1457 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &dkss, 1, MPI::REALTYPE,
1458 +                                      MPI::SUM);
1459 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &qkcs, 1, MPI::REALTYPE,
1460 +                                      MPI::SUM);
1461 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &qkss, 1, MPI::REALTYPE,
1462 +                                      MPI::SUM);
1463 + #endif        
1464 +            
1465 +            // Accumulate potential energy and virial contribution:          
1466 +
1467 +            kPot += 2.0 * rvol * AK[kk]*((ckss+dkcs-qkss)*(ckss+dkcs-qkss)
1468 +                                         + (ckcs-dkss-qkcs)*(ckcs-dkss-qkss));
1469 +
1470 +            kVir -= 2.0 * rvol * AK[kk]*(ckcs*ckcs+ckss*ckss
1471 +                                         +4.0*(ckss*dkcs-ckcs*dkss)
1472 +                                         +3.0*(dkcs*dkcs+dkss*dkss)
1473 +                                         -6.0*(ckss*qkss+ckcs*qkcs)
1474 +                                         +8.0*(dkss*qkcs-dkcs*qkss)
1475 +                                         +5.0*(qkss*qkss+qkcs*qkcs));
1476 +            
1477 +            // Calculate force and torque for each site:
1478 +            
1479 +            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1480 +                 mol = info_->nextMolecule(mi)) {
1481 +              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1482 +                  atom = mol->nextAtom(ai)) {
1483 +                
1484 +                i = atom->getLocalIndex();
1485 +                int atid = atom->getAtomType()->getIdent();
1486 +                ElectrostaticAtomData data = ElectrostaticMap[Etids[atid]];
1487 +                
1488 +                RealType qfrc = AK[kk]*((cks[i]+dkc[i]-qks[i])*(ckcs-dkss-qkcs)
1489 +                                        - (ckc[i]-dks[i]-qkc[i])*(ckss+dkcs-qkss));
1490 +                RealType qtrq1 = AK[kk]*(skr[i]*(ckcs-dkss-qkcs)
1491 +                                         -ckr[i]*(ckss+dkcs-qkss));
1492 +                RealType qtrq2 = 2.0*AK[kk]*(ckr[i]*(ckcs-dkss-qkcs)+
1493 +                                             skr[i]*(ckss+dkcs-qkss));
1494 +                
1495 +                
1496 +                atom->addFrc( 4.0 * rvol * qfrc * kVec );
1497 +                
1498 +                if (data.is_Dipole) {
1499 +                  atom->addTrq( 4.0 * rvol * qtrq1 * dxk[i] );
1500 +                }
1501 +                if (data.is_Quadrupole) {
1502 +                  atom->addTrq( 4.0 * rvol * qtrq2 * qxk[i] );
1503 +                }
1504 +              }
1505 +            }
1506 +          }
1507 +        }
1508 +      }
1509 +    }
1510 +    cerr << "kPot = " << kPot << "\n";
1511 +    pot[ELECTROSTATIC_FAMILY] += kPot;  
1512 +  }
1513   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines