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 1907 by gezelter, Fri Jul 19 18:18:27 2013 UTC vs.
Revision 1938 by gezelter, Thu Oct 31 15:32:17 2013 UTC

# Line 40 | Line 40
40   * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41   */
42  
43 + #ifdef IS_MPI
44 + #include <mpi.h>
45 + #endif
46 +
47   #include <stdio.h>
48   #include <string.h>
49  
50   #include <cmath>
51 + #include <numeric>
52   #include "nonbonded/Electrostatic.hpp"
53   #include "utils/simError.h"
54   #include "types/NonBondedInteractionType.hpp"
# Line 55 | Line 60
60   #include "utils/PhysicalConstants.hpp"
61   #include "math/erfc.hpp"
62   #include "math/SquareMatrix.hpp"
63 + #include "primitives/Molecule.hpp"
64  
65   namespace OpenMD {
66    
# Line 191 | Line 197 | namespace OpenMD {
197        simError();
198      }
199            
200 <    if (screeningMethod_ == DAMPED) {      
200 >    if (screeningMethod_ == DAMPED || summationMethod_ == esm_EWALD_FULL) {
201        if (!simParams_->haveDampingAlpha()) {
202          // first set a cutoff dependent alpha value
203          // we assume alpha depends linearly with rcut from 0 to 20.5 ang
204          dampingAlpha_ = 0.425 - cutoffRadius_* 0.02;
205 <        if (dampingAlpha_ < 0.0) dampingAlpha_ = 0.0;
200 <        
205 >        if (dampingAlpha_ < 0.0) dampingAlpha_ = 0.0;        
206          // throw warning
207          sprintf( painCave.errMsg,
208                   "Electrostatic::initialize: dampingAlpha was not specified in the\n"
# Line 213 | Line 218 | namespace OpenMD {
218        haveDampingAlpha_ = true;
219      }
220  
221 +
222      Etypes.clear();
223      Etids.clear();
224      FQtypes.clear();
# Line 262 | Line 268 | namespace OpenMD {
268        b3c = (5.0 * b2c + pow(2.0*a2, 3) * expTerm * invArootPi) / r2;
269        b4c = (7.0 * b3c + pow(2.0*a2, 4) * expTerm * invArootPi) / r2;
270        b5c = (9.0 * b4c + pow(2.0*a2, 5) * expTerm * invArootPi) / r2;
265      //selfMult1_ = - 2.0 * a2 * invArootPi;
266      //selfMult2_ = - 4.0 * a2 * a2 * invArootPi / 3.0;
267      //selfMult4_ = - 8.0 * a2 * a2 * a2 * invArootPi / 5.0;
271        // Half the Smith self piece:
272        selfMult1_ = - a2 * invArootPi;
273        selfMult2_ = - 2.0 * a2 * a2 * invArootPi / 3.0;
# Line 288 | Line 291 | namespace OpenMD {
291      db0c_3 =          3.0*r*b2c  - r2*r*b3c;
292      db0c_4 =          3.0*b2c  - 6.0*r2*b3c     + r2*r2*b4c;
293      db0c_5 =                    -15.0*r*b3c + 10.0*r2*r*b4c - r2*r2*r*b5c;  
291    
292    selfMult1_ -= b0c;
293    selfMult2_ += (db0c_2 + 2.0*db0c_1*ric) /  3.0;
294    selfMult4_ -= (db0c_4 + 4.0*db0c_3*ric) / 15.0;
294  
295 +    if (summationMethod_ != esm_EWALD_FULL) {
296 +      selfMult1_ -= b0c;
297 +      selfMult2_ += (db0c_2 + 2.0*db0c_1*ric) /  3.0;
298 +      selfMult4_ -= (db0c_4 + 4.0*db0c_3*ric) / 15.0;
299 +    }
300 +
301      // working variables for the splines:
302      RealType ri, ri2;
303      RealType b0, b1, b2, b3, b4, b5;
# Line 328 | Line 333 | namespace OpenMD {
333      vector<RealType> v31v, v32v;
334      vector<RealType> v41v, v42v, v43v;
335  
331    /*
332    vector<RealType> dv01v;
333    vector<RealType> dv11v;
334    vector<RealType> dv21v, dv22v;
335    vector<RealType> dv31v, dv32v;
336    vector<RealType> dv41v, dv42v, dv43v;
337    */
338
336      for (int i = 1; i < np_ + 1; i++) {
337        r = RealType(i) * dx;
338        rv.push_back(r);
# Line 499 | Line 496 | namespace OpenMD {
496  
497        case esm_SWITCHING_FUNCTION:
498        case esm_HARD:
499 +      case esm_EWALD_FULL:
500  
501          v01 = f;
502          v11 = g;
# Line 557 | Line 555 | namespace OpenMD {
555  
556          break;
557                  
560      case esm_EWALD_FULL:
558        case esm_EWALD_PME:
559        case esm_EWALD_SPME:
560        default :
# Line 586 | Line 583 | namespace OpenMD {
583        v41v.push_back(v41);
584        v42v.push_back(v42);
585        v43v.push_back(v43);
589      /*
590      dv01v.push_back(dv01);
591      dv11v.push_back(dv11);
592      dv21v.push_back(dv21);
593      dv22v.push_back(dv22);
594      dv31v.push_back(dv31);
595      dv32v.push_back(dv32);      
596      dv41v.push_back(dv41);
597      dv42v.push_back(dv42);
598      dv43v.push_back(dv43);
599      */
586      }
587  
588      // construct the spline structures and fill them with the values we've
# Line 621 | Line 607 | namespace OpenMD {
607      v43s = new CubicSpline();
608      v43s->addPoints(rv, v43v);
609  
624    /*
625    dv01s = new CubicSpline();
626    dv01s->addPoints(rv, dv01v);
627    dv11s = new CubicSpline();
628    dv11s->addPoints(rv, dv11v);
629    dv21s = new CubicSpline();
630    dv21s->addPoints(rv, dv21v);
631    dv22s = new CubicSpline();
632    dv22s->addPoints(rv, dv22v);
633    dv31s = new CubicSpline();
634    dv31s->addPoints(rv, dv31v);
635    dv32s = new CubicSpline();
636    dv32s->addPoints(rv, dv32v);
637    dv41s = new CubicSpline();
638    dv41s->addPoints(rv, dv41v);
639    dv42s = new CubicSpline();
640    dv42s->addPoints(rv, dv42v);
641    dv43s = new CubicSpline();
642    dv43s->addPoints(rv, dv43v);
643    */
644
610      haveElectroSplines_ = true;
611  
612      initialized_ = true;
# Line 715 | Line 680 | namespace OpenMD {
680        FQtids[atid] = fqtid;
681        Jij[fqtid].resize(nFlucq_);
682  
683 <      // Now, iterate over all known fluctuating and add to the coulomb integral map:
683 >      // Now, iterate over all known fluctuating and add to the
684 >      // coulomb integral map:
685        
686        std::set<int>::iterator it;
687        for( it = FQtypes.begin(); it != FQtypes.end(); ++it) {    
# Line 801 | Line 767 | namespace OpenMD {
767      // Excluded potential that is still computed for fluctuating charges
768      excluded_Pot= 0.0;
769  
804
770      // some variables we'll need independent of electrostatic type:
771  
772      ri = 1.0 /  *(idat.rij);
# Line 922 | Line 887 | namespace OpenMD {
887          Ea += pre14_ * (trQb * rhat * dv21 + 2.0 * Qbr * v22or
888                          + rdQbr * rhat * (dv22 - 2.0*v22or));
889      }
890 <    
890 >        
891 >
892      if ((a_is_Fluctuating || b_is_Fluctuating) && idat.excluded) {
893        J = Jij[FQtids[idat.atid1]][FQtids[idat.atid2]];
894      }    
895 <    
895 >
896      if (a_is_Charge) {    
897        
898        if (b_is_Charge) {
899          pref =  pre11_ * *(idat.electroMult);      
900          U  += C_a * C_b * pref * v01;
901          F  += C_a * C_b * pref * dv01 * rhat;
902 <        
902 >
903          // If this is an excluded pair, there are still indirect
904          // interactions via the reaction field we must worry about:
905  
# Line 942 | Line 908 | namespace OpenMD {
908            indirect_Pot += rfContrib;
909            indirect_F   += rfContrib * 2.0 * ri * rhat;
910          }
911 <        
911 >
912          // Fluctuating charge forces are handled via Coulomb integrals
913          // for excluded pairs (i.e. those connected via bonds) and
914          // with the standard charge-charge interaction otherwise.
915  
916 <        if (idat.excluded) {          
916 >        if (idat.excluded) {
917            if (a_is_Fluctuating || b_is_Fluctuating) {
918              coulInt = J->getValueAt( *(idat.rij) );
919 <            if (a_is_Fluctuating)  dUdCa += coulInt * C_b;
920 <            if (b_is_Fluctuating)  dUdCb += coulInt * C_a;
921 <            excluded_Pot += C_a * C_b * coulInt;
956 <          }          
919 >            if (a_is_Fluctuating) dUdCa += C_b * coulInt;
920 >            if (b_is_Fluctuating) dUdCb += C_a * coulInt;          
921 >          }
922          } else {
923            if (a_is_Fluctuating) dUdCa += C_b * pref * v01;
924 <          if (a_is_Fluctuating) dUdCb += C_a * pref * v01;
925 <        }
924 >          if (b_is_Fluctuating) dUdCb += C_a * pref * v01;
925 >        }              
926        }
927  
928        if (b_is_Dipole) {
# Line 1023 | Line 988 | namespace OpenMD {
988          F  -= pref * (rdDa * rdDb) * (dv22 - 2.0*v22or) * rhat;
989          Ta += pref * ( v21 * DaxDb - v22 * rdDb * rxDa);
990          Tb += pref * (-v21 * DaxDb - v22 * rdDa * rxDb);
1026
991          // Even if we excluded this pair from direct interactions, we
992          // still have the reaction-field-mediated dipole-dipole
993          // interaction:
# Line 1083 | Line 1047 | namespace OpenMD {
1047          trQaQb = QaQb.trace();
1048          rQaQb = rhat * QaQb;
1049          QaQbr = QaQb * rhat;
1050 <        QaxQb = cross(Q_a, Q_b);
1050 >        QaxQb = mCross(Q_a, Q_b);
1051          rQaQbr = dot(rQa, Qbr);
1052          rQaxQbr = cross(rQa, Qbr);
1053          
# Line 1114 | Line 1078 | namespace OpenMD {
1078          //             + 4.0 * cross(rhat, QbQar)
1079  
1080          Tb += pref * 2.0 * cross(rhat,Qbr) * rdQar * v43;
1117
1081        }
1082      }
1083  
# Line 1177 | Line 1140 | namespace OpenMD {
1140          
1141      if (i_is_Fluctuating) {
1142        C_a += *(sdat.flucQ);
1143 <      // dVdFQ is really a force, so this is negative the derivative
1181 <      *(sdat.dVdFQ) -=  *(sdat.flucQ) * data.hardness + data.electronegativity;
1143 >      *(sdat.flucQfrc) -=  *(sdat.flucQ) * data.hardness + data.electronegativity;
1144        (*(sdat.excludedPot))[ELECTROSTATIC_FAMILY] += (*sdat.flucQ) *
1145          (*(sdat.flucQ) * data.hardness * 0.5 + data.electronegativity);
1146      }
# Line 1203 | Line 1165 | namespace OpenMD {
1165      case esm_SHIFTED_FORCE:
1166      case esm_SHIFTED_POTENTIAL:
1167      case esm_TAYLOR_SHIFTED:
1168 +    case esm_EWALD_FULL:
1169        if (i_is_Charge)
1170          self += selfMult1_ * pre11_ * C_a * (C_a + *(sdat.skippedCharge));      
1171        if (i_is_Dipole)
# Line 1227 | Line 1190 | namespace OpenMD {
1190      // 12 angstroms seems to be a reasonably good guess for most
1191      // cases.
1192      return 12.0;
1193 +  }
1194 +
1195 +
1196 +  void Electrostatic::ReciprocalSpaceSum(RealType& pot) {
1197 +    
1198 +    RealType kPot = 0.0;
1199 +    RealType kVir = 0.0;
1200 +    
1201 +    const RealType mPoleConverter = 0.20819434; // converts from the
1202 +                                                // internal units of
1203 +                                                // Debye (for dipoles)
1204 +                                                // or Debye-angstroms
1205 +                                                // (for quadrupoles) to
1206 +                                                // electron angstroms or
1207 +                                                // electron-angstroms^2
1208 +    
1209 +    const RealType eConverter = 332.0637778; // convert the
1210 +                                             // Charge-Charge
1211 +                                             // electrostatic
1212 +                                             // interactions into kcal /
1213 +                                             // mol assuming distances
1214 +                                             // are measured in
1215 +                                             // angstroms.
1216 +
1217 +    Mat3x3d hmat = info_->getSnapshotManager()->getCurrentSnapshot()->getHmat();
1218 +    Vector3d box = hmat.diagonals();
1219 +    RealType boxMax = box.max();
1220 +    
1221 +    //int kMax = int(2.0 * M_PI / (pow(dampingAlpha_,2)*cutoffRadius_ * boxMax) );
1222 +    int kMax = 7;
1223 +    int kSqMax = kMax*kMax + 2;
1224 +    
1225 +    int kLimit = kMax+1;
1226 +    int kLim2 = 2*kMax+1;
1227 +    int kSqLim = kSqMax;
1228 +    
1229 +    vector<RealType> AK(kSqLim+1, 0.0);
1230 +    RealType xcl = 2.0 * M_PI / box.x();
1231 +    RealType ycl = 2.0 * M_PI / box.y();
1232 +    RealType zcl = 2.0 * M_PI / box.z();
1233 +    RealType rcl = 2.0 * M_PI / boxMax;
1234 +    RealType rvol = 2.0 * M_PI /(box.x() * box.y() * box.z());
1235 +    
1236 +    if(dampingAlpha_ < 1.0e-12) return;
1237 +    
1238 +    RealType ralph = -0.25/pow(dampingAlpha_,2);
1239 +    
1240 +    // Calculate and store exponential factors  
1241 +    
1242 +    vector<vector<RealType> > elc;
1243 +    vector<vector<RealType> > emc;
1244 +    vector<vector<RealType> > enc;
1245 +    vector<vector<RealType> > els;
1246 +    vector<vector<RealType> > ems;
1247 +    vector<vector<RealType> > ens;
1248 +
1249 +    
1250 +    int nMax = info_->getNAtoms();
1251 +    
1252 +    elc.resize(kLimit+1);
1253 +    emc.resize(kLimit+1);
1254 +    enc.resize(kLimit+1);
1255 +    els.resize(kLimit+1);
1256 +    ems.resize(kLimit+1);
1257 +    ens.resize(kLimit+1);
1258 +
1259 +    for (int j = 0; j < kLimit+1; j++) {
1260 +      elc[j].resize(nMax);
1261 +      emc[j].resize(nMax);
1262 +      enc[j].resize(nMax);
1263 +      els[j].resize(nMax);
1264 +      ems[j].resize(nMax);
1265 +      ens[j].resize(nMax);
1266 +    }
1267 +    
1268 +    Vector3d t( 2.0 * M_PI );
1269 +    t.Vdiv(t, box);
1270 +
1271 +    
1272 +    SimInfo::MoleculeIterator mi;
1273 +    Molecule::AtomIterator ai;
1274 +    int i;
1275 +    Vector3d r;
1276 +    Vector3d tt;
1277 +    
1278 +    for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1279 +         mol = info_->nextMolecule(mi)) {
1280 +      for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1281 +          atom = mol->nextAtom(ai)) {  
1282 +        
1283 +        i = atom->getLocalIndex();
1284 +        r = atom->getPos();
1285 +        info_->getSnapshotManager()->getCurrentSnapshot()->wrapVector(r);
1286 +        
1287 +        tt.Vmul(t, r);
1288 +
1289 +        elc[1][i] = 1.0;
1290 +        emc[1][i] = 1.0;
1291 +        enc[1][i] = 1.0;
1292 +        els[1][i] = 0.0;
1293 +        ems[1][i] = 0.0;
1294 +        ens[1][i] = 0.0;
1295 +
1296 +        elc[2][i] = cos(tt.x());
1297 +        emc[2][i] = cos(tt.y());
1298 +        enc[2][i] = cos(tt.z());
1299 +        els[2][i] = sin(tt.x());
1300 +        ems[2][i] = sin(tt.y());
1301 +        ens[2][i] = sin(tt.z());
1302 +        
1303 +        for(int l = 3; l <= kLimit; l++) {
1304 +          elc[l][i]=elc[l-1][i]*elc[2][i]-els[l-1][i]*els[2][i];
1305 +          emc[l][i]=emc[l-1][i]*emc[2][i]-ems[l-1][i]*ems[2][i];
1306 +          enc[l][i]=enc[l-1][i]*enc[2][i]-ens[l-1][i]*ens[2][i];
1307 +          els[l][i]=els[l-1][i]*elc[2][i]+elc[l-1][i]*els[2][i];
1308 +          ems[l][i]=ems[l-1][i]*emc[2][i]+emc[l-1][i]*ems[2][i];
1309 +          ens[l][i]=ens[l-1][i]*enc[2][i]+enc[l-1][i]*ens[2][i];
1310 +        }
1311 +      }
1312 +    }
1313 +    
1314 +    // Calculate and store AK coefficients:
1315 +    
1316 +    RealType eksq = 1.0;
1317 +    RealType expf = 0.0;
1318 +    if (ralph < 0.0) expf = exp(ralph*rcl*rcl);
1319 +    for (i = 1; i <= kSqLim; i++) {
1320 +      RealType rksq = float(i)*rcl*rcl;
1321 +      eksq = expf*eksq;
1322 +      AK[i] = eConverter * eksq/rksq;
1323 +    }
1324 +    
1325 +    /*
1326 +     * Loop over all k vectors k = 2 pi (ll/Lx, mm/Ly, nn/Lz)
1327 +     * the values of ll, mm and nn are selected so that the symmetry of
1328 +     * reciprocal lattice is taken into account i.e. the following
1329 +     * rules apply.
1330 +     *
1331 +     * ll ranges over the values 0 to kMax only.
1332 +     *
1333 +     * mm ranges over 0 to kMax when ll=0 and over
1334 +     *            -kMax to kMax otherwise.
1335 +     * nn ranges over 1 to kMax when ll=mm=0 and over
1336 +     *            -kMax to kMax otherwise.
1337 +     *
1338 +     * Hence the result of the summation must be doubled at the end.    
1339 +     */
1340 +    
1341 +    std::vector<RealType> clm(nMax, 0.0);
1342 +    std::vector<RealType> slm(nMax, 0.0);
1343 +    std::vector<RealType> ckr(nMax, 0.0);
1344 +    std::vector<RealType> skr(nMax, 0.0);
1345 +    std::vector<RealType> ckc(nMax, 0.0);
1346 +    std::vector<RealType> cks(nMax, 0.0);
1347 +    std::vector<RealType> dkc(nMax, 0.0);
1348 +    std::vector<RealType> dks(nMax, 0.0);
1349 +    std::vector<RealType> qkc(nMax, 0.0);
1350 +    std::vector<RealType> qks(nMax, 0.0);
1351 +    std::vector<Vector3d> dxk(nMax, V3Zero);
1352 +    std::vector<Vector3d> qxk(nMax, V3Zero);
1353 +    RealType rl, rm, rn;
1354 +    Vector3d kVec;
1355 +    Vector3d Qk;
1356 +    Mat3x3d k2;
1357 +    RealType ckcs, ckss, dkcs, dkss, qkcs, qkss;
1358 +    int atid;
1359 +    ElectrostaticAtomData data;
1360 +    RealType C, dk, qk;
1361 +    Vector3d D;
1362 +    Mat3x3d  Q;
1363 +
1364 +    int mMin = kLimit;
1365 +    int nMin = kLimit + 1;
1366 +    for (int l = 1; l <= kLimit; l++) {
1367 +      int ll = l - 1;
1368 +      rl = xcl * float(ll);
1369 +      for (int mmm = mMin; mmm <= kLim2; mmm++) {
1370 +        int mm = mmm - kLimit;
1371 +        int m = abs(mm) + 1;
1372 +        rm = ycl * float(mm);
1373 +        // Set temporary products of exponential terms
1374 +        for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1375 +             mol = info_->nextMolecule(mi)) {
1376 +          for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1377 +              atom = mol->nextAtom(ai)) {
1378 +            
1379 +            i = atom->getLocalIndex();
1380 +            if(mm < 0) {
1381 +              clm[i]=elc[l][i]*emc[m][i]+els[l][i]*ems[m][i];
1382 +              slm[i]=els[l][i]*emc[m][i]-ems[m][i]*elc[l][i];
1383 +            } else {
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 +            }
1387 +          }
1388 +        }
1389 +        for (int nnn = nMin; nnn <= kLim2; nnn++) {
1390 +          int nn = nnn - kLimit;          
1391 +          int n = abs(nn) + 1;
1392 +          rn = zcl * float(nn);
1393 +          // Test on magnitude of k vector:
1394 +          int kk=ll*ll + mm*mm + nn*nn;
1395 +          if(kk <= kSqLim) {
1396 +            kVec = Vector3d(rl, rm, rn);
1397 +            k2 = outProduct(kVec, kVec);
1398 +            // Calculate exp(ikr) terms
1399 +            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1400 +                 mol = info_->nextMolecule(mi)) {
1401 +              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1402 +                  atom = mol->nextAtom(ai)) {
1403 +                i = atom->getLocalIndex();
1404 +                
1405 +                if (nn < 0) {
1406 +                  ckr[i]=clm[i]*enc[n][i]+slm[i]*ens[n][i];
1407 +                  skr[i]=slm[i]*enc[n][i]-clm[i]*ens[n][i];
1408 +
1409 +                } else {
1410 +                  ckr[i]=clm[i]*enc[n][i]-slm[i]*ens[n][i];
1411 +                  skr[i]=slm[i]*enc[n][i]+clm[i]*ens[n][i];
1412 +                }
1413 +              }
1414 +            }
1415 +            
1416 +            // Calculate scalar and vector products for each site:
1417 +            
1418 +            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1419 +                 mol = info_->nextMolecule(mi)) {
1420 +              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1421 +                  atom = mol->nextAtom(ai)) {
1422 +                i = atom->getLocalIndex();
1423 +                int atid = atom->getAtomType()->getIdent();
1424 +                data = ElectrostaticMap[Etids[atid]];
1425 +                              
1426 +                if (data.is_Charge) {
1427 +                  C = data.fixedCharge;
1428 +                  if (atom->isFluctuatingCharge()) C += atom->getFlucQPos();
1429 +                  ckc[i] = C * ckr[i];
1430 +                  cks[i] = C * skr[i];
1431 +                }
1432 +                
1433 +                if (data.is_Dipole) {
1434 +                  D = atom->getDipole() * mPoleConverter;
1435 +                  dk = dot(D, kVec);
1436 +                  dxk[i] = cross(D, kVec);
1437 +                  dkc[i] = dk * ckr[i];
1438 +                  dks[i] = dk * skr[i];
1439 +                }
1440 +                if (data.is_Quadrupole) {
1441 +                  Q = atom->getQuadrupole() * mPoleConverter;
1442 +                  Qk = Q * kVec;                  
1443 +                  qk = dot(kVec, Qk);
1444 +                  qxk[i] = -cross(kVec, Qk);
1445 +                  qkc[i] = qk * ckr[i];
1446 +                  qks[i] = qk * skr[i];
1447 +                }              
1448 +              }
1449 +            }
1450 +
1451 +            // calculate vector sums
1452 +            
1453 +            ckcs = std::accumulate(ckc.begin(),ckc.end(),0.0);
1454 +            ckss = std::accumulate(cks.begin(),cks.end(),0.0);
1455 +            dkcs = std::accumulate(dkc.begin(),dkc.end(),0.0);
1456 +            dkss = std::accumulate(dks.begin(),dks.end(),0.0);
1457 +            qkcs = std::accumulate(qkc.begin(),qkc.end(),0.0);
1458 +            qkss = std::accumulate(qks.begin(),qks.end(),0.0);
1459 +            
1460 + #ifdef IS_MPI
1461 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &ckcs, 1, MPI::REALTYPE,
1462 +                                      MPI::SUM);
1463 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &ckss, 1, MPI::REALTYPE,
1464 +                                      MPI::SUM);
1465 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &dkcs, 1, MPI::REALTYPE,
1466 +                                      MPI::SUM);
1467 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &dkss, 1, MPI::REALTYPE,
1468 +                                      MPI::SUM);
1469 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &qkcs, 1, MPI::REALTYPE,
1470 +                                      MPI::SUM);
1471 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &qkss, 1, MPI::REALTYPE,
1472 +                                      MPI::SUM);
1473 + #endif        
1474 +            
1475 +            // Accumulate potential energy and virial contribution:
1476 +
1477 +            kPot += 2.0 * rvol * AK[kk]*((ckss+dkcs-qkss)*(ckss+dkcs-qkss)
1478 +                                         + (ckcs-dkss-qkcs)*(ckcs-dkss-qkcs));
1479 +
1480 +            kVir += 2.0 * rvol  * AK[kk]*(ckcs*ckcs+ckss*ckss
1481 +                                          +4.0*(ckss*dkcs-ckcs*dkss)
1482 +                                          +3.0*(dkcs*dkcs+dkss*dkss)
1483 +                                          -6.0*(ckss*qkss+ckcs*qkcs)
1484 +                                          +8.0*(dkss*qkcs-dkcs*qkss)
1485 +                                          +5.0*(qkss*qkss+qkcs*qkcs));
1486 +            
1487 +            // Calculate force and torque for each site:
1488 +            
1489 +            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1490 +                 mol = info_->nextMolecule(mi)) {
1491 +              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1492 +                  atom = mol->nextAtom(ai)) {
1493 +                
1494 +                i = atom->getLocalIndex();
1495 +                atid = atom->getAtomType()->getIdent();
1496 +                data = ElectrostaticMap[Etids[atid]];
1497 +
1498 +                RealType qfrc = AK[kk]*((cks[i]+dkc[i]-qks[i])*(ckcs-dkss-qkcs)
1499 +                                     - (ckc[i]-dks[i]-qkc[i])*(ckss+dkcs-qkss));
1500 +                RealType qtrq1 = AK[kk]*(skr[i]*(ckcs-dkss-qkcs)
1501 +                                         -ckr[i]*(ckss+dkcs-qkss));
1502 +                RealType qtrq2 = 2.0*AK[kk]*(ckr[i]*(ckcs-dkss-qkcs)
1503 +                                            +skr[i]*(ckss+dkcs-qkss));
1504 +              
1505 +                atom->addFrc( 4.0 * rvol * qfrc * kVec );
1506 +                
1507 +                if (data.is_Dipole) {
1508 +                  atom->addTrq( 4.0 * rvol * qtrq1 * dxk[i] );
1509 +                }
1510 +                if (data.is_Quadrupole) {
1511 +                  atom->addTrq( 4.0 * rvol * qtrq2 * qxk[i] );
1512 +                }
1513 +              }
1514 +            }
1515 +          }
1516 +        }
1517 +        nMin = 1;
1518 +      }
1519 +      mMin = 1;
1520 +    }
1521 +    pot += kPot;  
1522    }
1523   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines