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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines