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 1900 by gezelter, Fri Jul 12 17:38:06 2013 UTC vs.
Revision 1933 by gezelter, Fri Aug 23 15:59:23 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 +
221      Etypes.clear();
222      Etids.clear();
223      FQtypes.clear();
# Line 262 | 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;
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;
270        // Half the Smith self piece:
271        selfMult1_ = - a2 * invArootPi;
272        selfMult2_ = - 2.0 * a2 * a2 * invArootPi / 3.0;
# Line 288 | Line 290 | namespace OpenMD {
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;  
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;
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;
302      RealType b0, b1, b2, b3, b4, b5;
# Line 328 | Line 332 | namespace OpenMD {
332      vector<RealType> v31v, v32v;
333      vector<RealType> v41v, v42v, v43v;
334  
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
335      for (int i = 1; i < np_ + 1; i++) {
336        r = RealType(i) * dx;
337        rv.push_back(r);
# Line 499 | 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 557 | Line 554 | namespace OpenMD {
554  
555          break;
556                  
560      case esm_EWALD_FULL:
557        case esm_EWALD_PME:
558        case esm_EWALD_SPME:
559        default :
# Line 586 | Line 582 | namespace OpenMD {
582        v41v.push_back(v41);
583        v42v.push_back(v42);
584        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      */
585      }
586  
587      // construct the spline structures and fill them with the values we've
# Line 621 | Line 606 | namespace OpenMD {
606      v43s = new CubicSpline();
607      v43s->addPoints(rv, v43v);
608  
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
609      haveElectroSplines_ = true;
610  
611      initialized_ = true;
# Line 715 | Line 679 | namespace OpenMD {
679        FQtids[atid] = fqtid;
680        Jij[fqtid].resize(nFlucq_);
681  
682 <      // Now, iterate over all known fluctuating and add to the coulomb integral map:
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) {    
# Line 801 | Line 766 | namespace OpenMD {
766      // Excluded potential that is still computed for fluctuating charges
767      excluded_Pot= 0.0;
768  
804
769      // some variables we'll need independent of electrostatic type:
770  
771      ri = 1.0 /  *(idat.rij);
# Line 922 | Line 886 | namespace OpenMD {
886          Ea += pre14_ * (trQb * rhat * dv21 + 2.0 * Qbr * v22or
887                          + rdQbr * rhat * (dv22 - 2.0*v22or));
888      }
889 <    
889 >        
890 >
891      if ((a_is_Fluctuating || b_is_Fluctuating) && idat.excluded) {
892        J = Jij[FQtids[idat.atid1]][FQtids[idat.atid2]];
893      }    
894 <    
894 >
895      if (a_is_Charge) {    
896        
897        if (b_is_Charge) {
898          pref =  pre11_ * *(idat.electroMult);      
899          U  += C_a * C_b * pref * v01;
900          F  += C_a * C_b * pref * dv01 * rhat;
901 <        
901 >
902          // If this is an excluded pair, there are still indirect
903          // interactions via the reaction field we must worry about:
904  
# Line 942 | Line 907 | namespace OpenMD {
907            indirect_Pot += rfContrib;
908            indirect_F   += rfContrib * 2.0 * ri * rhat;
909          }
910 <        
910 >
911          // Fluctuating charge forces are handled via Coulomb integrals
912          // for excluded pairs (i.e. those connected via bonds) and
913          // with the standard charge-charge interaction otherwise.
914  
915 <        if (idat.excluded) {          
915 >        if (idat.excluded) {
916            if (a_is_Fluctuating || b_is_Fluctuating) {
917              coulInt = J->getValueAt( *(idat.rij) );
918 <            if (a_is_Fluctuating)  dUdCa += coulInt * C_b;
919 <            if (b_is_Fluctuating)  dUdCb += coulInt * C_a;
920 <            excluded_Pot += C_a * C_b * coulInt;
956 <          }          
918 >            if (a_is_Fluctuating) dUdCa += C_b * coulInt;
919 >            if (b_is_Fluctuating) dUdCb += C_a * coulInt;          
920 >          }
921          } else {
922            if (a_is_Fluctuating) dUdCa += C_b * pref * v01;
923 <          if (a_is_Fluctuating) dUdCb += C_a * pref * v01;
924 <        }
923 >          if (b_is_Fluctuating) dUdCb += C_a * pref * v01;
924 >        }              
925        }
926  
927        if (b_is_Dipole) {
# Line 1023 | Line 987 | namespace OpenMD {
987          F  -= pref * (rdDa * rdDb) * (dv22 - 2.0*v22or) * rhat;
988          Ta += pref * ( v21 * DaxDb - v22 * rdDb * rxDa);
989          Tb += pref * (-v21 * DaxDb - v22 * rdDa * rxDb);
1026
990          // Even if we excluded this pair from direct interactions, we
991          // still have the reaction-field-mediated dipole-dipole
992          // interaction:
# Line 1083 | Line 1046 | namespace OpenMD {
1046          trQaQb = QaQb.trace();
1047          rQaQb = rhat * QaQb;
1048          QaQbr = QaQb * rhat;
1049 <        QaxQb = cross(Q_a, Q_b);
1049 >        QaxQb = mCross(Q_a, Q_b);
1050          rQaQbr = dot(rQa, Qbr);
1051          rQaxQbr = cross(rQa, Qbr);
1052          
# Line 1114 | Line 1077 | namespace OpenMD {
1077          //             + 4.0 * cross(rhat, QbQar)
1078  
1079          Tb += pref * 2.0 * cross(rhat,Qbr) * rdQar * v43;
1117
1080        }
1081      }
1082  
# Line 1177 | Line 1139 | namespace OpenMD {
1139          
1140      if (i_is_Fluctuating) {
1141        C_a += *(sdat.flucQ);
1142 <      // dVdFQ is really a force, so this is negative the derivative
1181 <      *(sdat.dVdFQ) -=  *(sdat.flucQ) * data.hardness + data.electronegativity;
1142 >      *(sdat.flucQfrc) -=  *(sdat.flucQ) * data.hardness + data.electronegativity;
1143        (*(sdat.excludedPot))[ELECTROSTATIC_FAMILY] += (*sdat.flucQ) *
1144          (*(sdat.flucQ) * data.hardness * 0.5 + data.electronegativity);
1145      }
# Line 1202 | Line 1163 | namespace OpenMD {
1163        
1164      case esm_SHIFTED_FORCE:
1165      case esm_SHIFTED_POTENTIAL:
1166 +    case esm_TAYLOR_SHIFTED:
1167 +    case esm_EWALD_FULL:
1168        if (i_is_Charge)
1169          self += selfMult1_ * pre11_ * C_a * (C_a + *(sdat.skippedCharge));      
1170        if (i_is_Dipole)
# Line 1226 | Line 1189 | namespace OpenMD {
1189      // 12 angstroms seems to be a reasonably good guess for most
1190      // cases.
1191      return 12.0;
1192 +  }
1193 +
1194 +
1195 +  void Electrostatic::ReciprocalSpaceSum(RealType& pot) {
1196 +    
1197 +    RealType kPot = 0.0;
1198 +    RealType kVir = 0.0;
1199 +    
1200 +    const RealType mPoleConverter = 0.20819434; // converts from the
1201 +                                                // internal units of
1202 +                                                // Debye (for dipoles)
1203 +                                                // or Debye-angstroms
1204 +                                                // (for quadrupoles) to
1205 +                                                // electron angstroms or
1206 +                                                // electron-angstroms^2
1207 +    
1208 +    const RealType eConverter = 332.0637778; // convert the
1209 +                                             // Charge-Charge
1210 +                                             // electrostatic
1211 +                                             // interactions into kcal /
1212 +                                             // mol assuming distances
1213 +                                             // are measured in
1214 +                                             // angstroms.
1215 +
1216 +    Mat3x3d hmat = info_->getSnapshotManager()->getCurrentSnapshot()->getHmat();
1217 +    Vector3d box = hmat.diagonals();
1218 +    RealType boxMax = box.max();
1219 +    
1220 +    //int kMax = int(2.0 * M_PI / (pow(dampingAlpha_,2)*cutoffRadius_ * boxMax) );
1221 +    int kMax = 7;
1222 +    int kSqMax = kMax*kMax + 2;
1223 +    
1224 +    int kLimit = kMax+1;
1225 +    int kLim2 = 2*kMax+1;
1226 +    int kSqLim = kSqMax;
1227 +    
1228 +    vector<RealType> AK(kSqLim+1, 0.0);
1229 +    RealType xcl = 2.0 * M_PI / box.x();
1230 +    RealType ycl = 2.0 * M_PI / box.y();
1231 +    RealType zcl = 2.0 * M_PI / box.z();
1232 +    RealType rcl = 2.0 * M_PI / boxMax;
1233 +    RealType rvol = 2.0 * M_PI /(box.x() * box.y() * box.z());
1234 +    
1235 +    if(dampingAlpha_ < 1.0e-12) return;
1236 +    
1237 +    RealType ralph = -0.25/pow(dampingAlpha_,2);
1238 +    
1239 +    // Calculate and store exponential factors  
1240 +    
1241 +    vector<vector<RealType> > elc;
1242 +    vector<vector<RealType> > emc;
1243 +    vector<vector<RealType> > enc;
1244 +    vector<vector<RealType> > els;
1245 +    vector<vector<RealType> > ems;
1246 +    vector<vector<RealType> > ens;
1247 +
1248 +    
1249 +    int nMax = info_->getNAtoms();
1250 +    
1251 +    elc.resize(kLimit+1);
1252 +    emc.resize(kLimit+1);
1253 +    enc.resize(kLimit+1);
1254 +    els.resize(kLimit+1);
1255 +    ems.resize(kLimit+1);
1256 +    ens.resize(kLimit+1);
1257 +
1258 +    for (int j = 0; j < kLimit+1; j++) {
1259 +      elc[j].resize(nMax);
1260 +      emc[j].resize(nMax);
1261 +      enc[j].resize(nMax);
1262 +      els[j].resize(nMax);
1263 +      ems[j].resize(nMax);
1264 +      ens[j].resize(nMax);
1265 +    }
1266 +    
1267 +    Vector3d t( 2.0 * M_PI );
1268 +    t.Vdiv(t, box);
1269 +
1270 +    
1271 +    SimInfo::MoleculeIterator mi;
1272 +    Molecule::AtomIterator ai;
1273 +    int i;
1274 +    Vector3d r;
1275 +    Vector3d tt;
1276 +    
1277 +    for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1278 +         mol = info_->nextMolecule(mi)) {
1279 +      for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1280 +          atom = mol->nextAtom(ai)) {  
1281 +        
1282 +        i = atom->getLocalIndex();
1283 +        r = atom->getPos();
1284 +        info_->getSnapshotManager()->getCurrentSnapshot()->wrapVector(r);
1285 +        
1286 +        tt.Vmul(t, r);
1287 +
1288 +        elc[1][i] = 1.0;
1289 +        emc[1][i] = 1.0;
1290 +        enc[1][i] = 1.0;
1291 +        els[1][i] = 0.0;
1292 +        ems[1][i] = 0.0;
1293 +        ens[1][i] = 0.0;
1294 +
1295 +        elc[2][i] = cos(tt.x());
1296 +        emc[2][i] = cos(tt.y());
1297 +        enc[2][i] = cos(tt.z());
1298 +        els[2][i] = sin(tt.x());
1299 +        ems[2][i] = sin(tt.y());
1300 +        ens[2][i] = sin(tt.z());
1301 +        
1302 +        for(int l = 3; l <= kLimit; l++) {
1303 +          elc[l][i]=elc[l-1][i]*elc[2][i]-els[l-1][i]*els[2][i];
1304 +          emc[l][i]=emc[l-1][i]*emc[2][i]-ems[l-1][i]*ems[2][i];
1305 +          enc[l][i]=enc[l-1][i]*enc[2][i]-ens[l-1][i]*ens[2][i];
1306 +          els[l][i]=els[l-1][i]*elc[2][i]+elc[l-1][i]*els[2][i];
1307 +          ems[l][i]=ems[l-1][i]*emc[2][i]+emc[l-1][i]*ems[2][i];
1308 +          ens[l][i]=ens[l-1][i]*enc[2][i]+enc[l-1][i]*ens[2][i];
1309 +        }
1310 +      }
1311 +    }
1312 +    
1313 +    // Calculate and store AK coefficients:
1314 +    
1315 +    RealType eksq = 1.0;
1316 +    RealType expf = 0.0;
1317 +    if (ralph < 0.0) expf = exp(ralph*rcl*rcl);
1318 +    for (i = 1; i <= kSqLim; i++) {
1319 +      RealType rksq = float(i)*rcl*rcl;
1320 +      eksq = expf*eksq;
1321 +      AK[i] = eConverter * eksq/rksq;
1322 +    }
1323 +    
1324 +    /*
1325 +     * Loop over all k vectors k = 2 pi (ll/Lx, mm/Ly, nn/Lz)
1326 +     * the values of ll, mm and nn are selected so that the symmetry of
1327 +     * reciprocal lattice is taken into account i.e. the following
1328 +     * rules apply.
1329 +     *
1330 +     * ll ranges over the values 0 to kMax only.
1331 +     *
1332 +     * mm ranges over 0 to kMax when ll=0 and over
1333 +     *            -kMax to kMax otherwise.
1334 +     * nn ranges over 1 to kMax when ll=mm=0 and over
1335 +     *            -kMax to kMax otherwise.
1336 +     *
1337 +     * Hence the result of the summation must be doubled at the end.    
1338 +     */
1339 +    
1340 +    std::vector<RealType> clm(nMax, 0.0);
1341 +    std::vector<RealType> slm(nMax, 0.0);
1342 +    std::vector<RealType> ckr(nMax, 0.0);
1343 +    std::vector<RealType> skr(nMax, 0.0);
1344 +    std::vector<RealType> ckc(nMax, 0.0);
1345 +    std::vector<RealType> cks(nMax, 0.0);
1346 +    std::vector<RealType> dkc(nMax, 0.0);
1347 +    std::vector<RealType> dks(nMax, 0.0);
1348 +    std::vector<RealType> qkc(nMax, 0.0);
1349 +    std::vector<RealType> qks(nMax, 0.0);
1350 +    std::vector<Vector3d> dxk(nMax, V3Zero);
1351 +    std::vector<Vector3d> qxk(nMax, V3Zero);
1352 +    RealType rl, rm, rn;
1353 +    Vector3d kVec;
1354 +    Vector3d Qk;
1355 +    Mat3x3d k2;
1356 +    RealType ckcs, ckss, dkcs, dkss, qkcs, qkss;
1357 +    int atid;
1358 +    ElectrostaticAtomData data;
1359 +    RealType C, dk, qk;
1360 +    Vector3d D;
1361 +    Mat3x3d  Q;
1362 +
1363 +    int mMin = kLimit;
1364 +    int nMin = kLimit + 1;
1365 +    for (int l = 1; l <= kLimit; l++) {
1366 +      int ll = l - 1;
1367 +      rl = xcl * float(ll);
1368 +      for (int mmm = mMin; mmm <= kLim2; mmm++) {
1369 +        int mm = mmm - kLimit;
1370 +        int m = abs(mm) + 1;
1371 +        rm = ycl * float(mm);
1372 +        // Set temporary products of exponential terms
1373 +        for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1374 +             mol = info_->nextMolecule(mi)) {
1375 +          for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1376 +              atom = mol->nextAtom(ai)) {
1377 +            
1378 +            i = atom->getLocalIndex();
1379 +            if(mm < 0) {
1380 +              clm[i]=elc[l][i]*emc[m][i]+els[l][i]*ems[m][i];
1381 +              slm[i]=els[l][i]*emc[m][i]-ems[m][i]*elc[l][i];
1382 +            } else {
1383 +              clm[i]=elc[l][i]*emc[m][i]-els[l][i]*ems[m][i];
1384 +              slm[i]=els[l][i]*emc[m][i]+ems[m][i]*elc[l][i];
1385 +            }
1386 +          }
1387 +        }
1388 +        for (int nnn = nMin; nnn <= kLim2; nnn++) {
1389 +          int nn = nnn - kLimit;          
1390 +          int n = abs(nn) + 1;
1391 +          rn = zcl * float(nn);
1392 +          // Test on magnitude of k vector:
1393 +          int kk=ll*ll + mm*mm + nn*nn;
1394 +          if(kk <= kSqLim) {
1395 +            kVec = Vector3d(rl, rm, rn);
1396 +            k2 = outProduct(kVec, kVec);
1397 +            // Calculate exp(ikr) terms
1398 +            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1399 +                 mol = info_->nextMolecule(mi)) {
1400 +              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1401 +                  atom = mol->nextAtom(ai)) {
1402 +                i = atom->getLocalIndex();
1403 +                
1404 +                if (nn < 0) {
1405 +                  ckr[i]=clm[i]*enc[n][i]+slm[i]*ens[n][i];
1406 +                  skr[i]=slm[i]*enc[n][i]-clm[i]*ens[n][i];
1407 +
1408 +                } else {
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 +              }
1413 +            }
1414 +            
1415 +            // Calculate scalar and vector products for each site:
1416 +            
1417 +            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1418 +                 mol = info_->nextMolecule(mi)) {
1419 +              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1420 +                  atom = mol->nextAtom(ai)) {
1421 +                i = atom->getLocalIndex();
1422 +                int atid = atom->getAtomType()->getIdent();
1423 +                data = ElectrostaticMap[Etids[atid]];
1424 +                              
1425 +                if (data.is_Charge) {
1426 +                  C = data.fixedCharge;
1427 +                  if (atom->isFluctuatingCharge()) C += atom->getFlucQPos();
1428 +                  ckc[i] = C * ckr[i];
1429 +                  cks[i] = C * skr[i];
1430 +                }
1431 +                
1432 +                if (data.is_Dipole) {
1433 +                  D = atom->getDipole() * mPoleConverter;
1434 +                  dk = dot(D, kVec);
1435 +                  dxk[i] = cross(D, kVec);
1436 +                  dkc[i] = dk * ckr[i];
1437 +                  dks[i] = dk * skr[i];
1438 +                }
1439 +                if (data.is_Quadrupole) {
1440 +                  Q = atom->getQuadrupole() * mPoleConverter;
1441 +                  Qk = Q * kVec;                  
1442 +                  qk = dot(kVec, Qk);
1443 +                  qxk[i] = cross(kVec, Qk);
1444 +                  qkc[i] = qk * ckr[i];
1445 +                  qks[i] = qk * skr[i];
1446 +                }              
1447 +              }
1448 +            }
1449 +
1450 +            // calculate vector sums
1451 +            
1452 +            ckcs = std::accumulate(ckc.begin(),ckc.end(),0.0);
1453 +            ckss = std::accumulate(cks.begin(),cks.end(),0.0);
1454 +            dkcs = std::accumulate(dkc.begin(),dkc.end(),0.0);
1455 +            dkss = std::accumulate(dks.begin(),dks.end(),0.0);
1456 +            qkcs = std::accumulate(qkc.begin(),qkc.end(),0.0);
1457 +            qkss = std::accumulate(qks.begin(),qks.end(),0.0);
1458 +            
1459 + #ifdef IS_MPI
1460 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &ckcs, 1, MPI::REALTYPE,
1461 +                                      MPI::SUM);
1462 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &ckss, 1, MPI::REALTYPE,
1463 +                                      MPI::SUM);
1464 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &dkcs, 1, MPI::REALTYPE,
1465 +                                      MPI::SUM);
1466 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &dkss, 1, MPI::REALTYPE,
1467 +                                      MPI::SUM);
1468 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &qkcs, 1, MPI::REALTYPE,
1469 +                                      MPI::SUM);
1470 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &qkss, 1, MPI::REALTYPE,
1471 +                                      MPI::SUM);
1472 + #endif        
1473 +            
1474 +            // Accumulate potential energy and virial contribution:
1475 +
1476 +            kPot += 2.0 * rvol * AK[kk]*((ckss+dkcs-qkss)*(ckss+dkcs-qkss)
1477 +                                         + (ckcs-dkss-qkcs)*(ckcs-dkss-qkcs));
1478 +
1479 +            kVir += 2.0 * rvol  * AK[kk]*(ckcs*ckcs+ckss*ckss
1480 +                                          +4.0*(ckss*dkcs-ckcs*dkss)
1481 +                                          +3.0*(dkcs*dkcs+dkss*dkss)
1482 +                                          -6.0*(ckss*qkss+ckcs*qkcs)
1483 +                                          +8.0*(dkss*qkcs-dkcs*qkss)
1484 +                                          +5.0*(qkss*qkss+qkcs*qkcs));
1485 +            
1486 +            // Calculate force and torque for each site:
1487 +            
1488 +            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1489 +                 mol = info_->nextMolecule(mi)) {
1490 +              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1491 +                  atom = mol->nextAtom(ai)) {
1492 +                
1493 +                i = atom->getLocalIndex();
1494 +                atid = atom->getAtomType()->getIdent();
1495 +                data = ElectrostaticMap[Etids[atid]];
1496 +
1497 +                RealType qfrc = AK[kk]*((cks[i]+dkc[i]-qks[i])*(ckcs-dkss-qkcs)
1498 +                                     - (ckc[i]-dks[i]-qkc[i])*(ckss+dkcs-qkss));
1499 +                RealType qtrq1 = AK[kk]*(skr[i]*(ckcs-dkss-qkcs)
1500 +                                         -ckr[i]*(ckss+dkcs-qkss));
1501 +                RealType qtrq2 = 2.0*AK[kk]*(ckr[i]*(ckcs-dkss-qkcs)
1502 +                                            +skr[i]*(ckss+dkcs-qkss));
1503 +              
1504 +                atom->addFrc( 4.0 * rvol * qfrc * kVec );
1505 +                
1506 +                if (data.is_Dipole) {
1507 +                  atom->addTrq( 4.0 * rvol * qtrq1 * dxk[i] );
1508 +                }
1509 +                if (data.is_Quadrupole) {
1510 +                  atom->addTrq( 4.0 * rvol * qtrq2 * qxk[i] );
1511 +                }
1512 +              }
1513 +            }
1514 +          }
1515 +        }
1516 +        nMin = 1;
1517 +      }
1518 +      mMin = 1;
1519 +    }
1520 +    pot += kPot;  
1521    }
1522   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines