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 1915 by gezelter, Mon Jul 29 17:55:17 2013 UTC vs.
Revision 1932 by gezelter, Wed Aug 21 16:52:56 2013 UTC

# Line 57 | Line 57
57   #include "math/erfc.hpp"
58   #include "math/SquareMatrix.hpp"
59   #include "primitives/Molecule.hpp"
60 <
60 > #ifdef IS_MPI
61 > #include <mpi.h>
62 > #endif
63  
64   namespace OpenMD {
65    
# Line 677 | 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 763 | Line 766 | namespace OpenMD {
766      // Excluded potential that is still computed for fluctuating charges
767      excluded_Pot= 0.0;
768  
766
769      // some variables we'll need independent of electrostatic type:
770  
771      ri = 1.0 /  *(idat.rij);
# Line 884 | 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 904 | 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;
918 <          }          
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 1139 | Line 1141 | namespace OpenMD {
1141          
1142      if (i_is_Fluctuating) {
1143        C_a += *(sdat.flucQ);
1144 <      // dVdFQ is really a force, so this is negative the derivative
1143 <      *(sdat.dVdFQ) -=  *(sdat.flucQ) * data.hardness + data.electronegativity;
1144 >      *(sdat.flucQfrc) -=  *(sdat.flucQ) * data.hardness + data.electronegativity;
1145        (*(sdat.excludedPot))[ELECTROSTATIC_FAMILY] += (*sdat.flucQ) *
1146          (*(sdat.flucQ) * data.hardness * 0.5 + data.electronegativity);
1147      }
# Line 1165 | Line 1166 | namespace OpenMD {
1166      case esm_SHIFTED_FORCE:
1167      case esm_SHIFTED_POTENTIAL:
1168      case esm_TAYLOR_SHIFTED:
1169 +    case esm_EWALD_FULL:
1170        if (i_is_Charge)
1171          self += selfMult1_ * pre11_ * C_a * (C_a + *(sdat.skippedCharge));      
1172        if (i_is_Dipole)
# Line 1192 | Line 1194 | namespace OpenMD {
1194    }
1195  
1196  
1197 <  void Electrostatic::ReciprocalSpaceSum () {
1197 >  void Electrostatic::ReciprocalSpaceSum(RealType& pot) {
1198      
1199      RealType kPot = 0.0;
1200      RealType kVir = 0.0;
# Line 1217 | Line 1219 | namespace OpenMD {
1219      Vector3d box = hmat.diagonals();
1220      RealType boxMax = box.max();
1221      
1222 <    //int kMax = int(pow(dampingAlpha_,2)*cutoffRadius_ * boxMax / M_PI);
1223 <    const int kMax = 5;
1222 >    //int kMax = int(2.0 * M_PI / (pow(dampingAlpha_,2)*cutoffRadius_ * boxMax) );
1223 >    int kMax = 7;
1224      int kSqMax = kMax*kMax + 2;
1225      
1226      int kLimit = kMax+1;
# Line 1238 | Line 1240 | namespace OpenMD {
1240      
1241      // Calculate and store exponential factors  
1242      
1243 <    vector<vector<Vector3d> > eCos;
1244 <    vector<vector<Vector3d> > eSin;
1243 >    vector<vector<RealType> > elc;
1244 >    vector<vector<RealType> > emc;
1245 >    vector<vector<RealType> > enc;
1246 >    vector<vector<RealType> > els;
1247 >    vector<vector<RealType> > ems;
1248 >    vector<vector<RealType> > ens;
1249 >
1250      
1251      int nMax = info_->getNAtoms();
1252      
1253 <    eCos.resize(kLimit+1);
1254 <    eSin.resize(kLimit+1);
1253 >    elc.resize(kLimit+1);
1254 >    emc.resize(kLimit+1);
1255 >    enc.resize(kLimit+1);
1256 >    els.resize(kLimit+1);
1257 >    ems.resize(kLimit+1);
1258 >    ens.resize(kLimit+1);
1259 >
1260      for (int j = 0; j < kLimit+1; j++) {
1261 <      eCos[j].resize(nMax);
1262 <      eSin[j].resize(nMax);
1261 >      elc[j].resize(nMax);
1262 >      emc[j].resize(nMax);
1263 >      enc[j].resize(nMax);
1264 >      els[j].resize(nMax);
1265 >      ems[j].resize(nMax);
1266 >      ens[j].resize(nMax);
1267      }
1268      
1269      Vector3d t( 2.0 * M_PI );
1270      t.Vdiv(t, box);
1271 +
1272      
1273      SimInfo::MoleculeIterator mi;
1274      Molecule::AtomIterator ai;
1275      int i;
1276      Vector3d r;
1277      Vector3d tt;
1261    Vector3d w;
1262    Vector3d u;
1278      
1279      for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1280           mol = info_->nextMolecule(mi)) {
# Line 1271 | Line 1286 | namespace OpenMD {
1286          info_->getSnapshotManager()->getCurrentSnapshot()->wrapVector(r);
1287          
1288          tt.Vmul(t, r);
1289 +
1290 +        elc[1][i] = 1.0;
1291 +        emc[1][i] = 1.0;
1292 +        enc[1][i] = 1.0;
1293 +        els[1][i] = 0.0;
1294 +        ems[1][i] = 0.0;
1295 +        ens[1][i] = 0.0;
1296 +
1297 +        elc[2][i] = cos(tt.x());
1298 +        emc[2][i] = cos(tt.y());
1299 +        enc[2][i] = cos(tt.z());
1300 +        els[2][i] = sin(tt.x());
1301 +        ems[2][i] = sin(tt.y());
1302 +        ens[2][i] = sin(tt.z());
1303          
1275        eCos[1][i] = Vector3d(1.0, 1.0, 1.0);
1276        eSin[1][i] = Vector3d(0.0, 0.0, 0.0);
1277        eCos[2][i] = Vector3d(cos(tt.x()), cos(tt.y()), cos(tt.z()));
1278        eSin[2][i] = Vector3d(sin(tt.x()), sin(tt.y()), sin(tt.z()));
1279        u = 2.0 * eCos[1][i];
1280        eCos[3][i].Vmul(u, eCos[2][i]);
1281        eSin[3][i].Vmul(u, eSin[2][i]);      
1282        
1304          for(int l = 3; l <= kLimit; l++) {
1305 <          w.Vmul(u, eCos[l-1][i]);
1306 <          eCos[l][i] = w - eCos[l-2][i];
1307 <          w.Vmul(u, eSin[l-1][i]);
1308 <          eSin[l][i] = w - eSin[l-2][i];
1305 >          elc[l][i]=elc[l-1][i]*elc[2][i]-els[l-1][i]*els[2][i];
1306 >          emc[l][i]=emc[l-1][i]*emc[2][i]-ems[l-1][i]*ems[2][i];
1307 >          enc[l][i]=enc[l-1][i]*enc[2][i]-ens[l-1][i]*ens[2][i];
1308 >          els[l][i]=els[l-1][i]*elc[2][i]+elc[l-1][i]*els[2][i];
1309 >          ems[l][i]=ems[l-1][i]*emc[2][i]+emc[l-1][i]*ems[2][i];
1310 >          ens[l][i]=ens[l-1][i]*enc[2][i]+enc[l-1][i]*ens[2][i];
1311          }
1312        }
1313      }
# Line 1328 | Line 1351 | namespace OpenMD {
1351      std::vector<RealType> qks(nMax, 0.0);
1352      std::vector<Vector3d> dxk(nMax, V3Zero);
1353      std::vector<Vector3d> qxk(nMax, V3Zero);
1354 <    
1354 >    RealType rl, rm, rn;
1355 >    Vector3d kVec;
1356 >    Vector3d Qk;
1357 >    Mat3x3d k2;
1358 >    RealType ckcs, ckss, dkcs, dkss, qkcs, qkss;
1359 >    int atid;
1360 >    ElectrostaticAtomData data;
1361 >    RealType C, dk, qk;
1362 >    Vector3d D;
1363 >    Mat3x3d  Q;
1364 >
1365      int mMin = kLimit;
1366      int nMin = kLimit + 1;
1367      for (int l = 1; l <= kLimit; l++) {
1368 <      int ll =l - 1;
1369 <      RealType rl = xcl * float(ll);
1368 >      int ll = l - 1;
1369 >      rl = xcl * float(ll);
1370        for (int mmm = mMin; mmm <= kLim2; mmm++) {
1371          int mm = mmm - kLimit;
1372          int m = abs(mm) + 1;
1373 <        RealType rm = ycl * float(mm);
1373 >        rm = ycl * float(mm);
1374          // Set temporary products of exponential terms
1375          for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1376               mol = info_->nextMolecule(mi)) {
# Line 1346 | Line 1379 | namespace OpenMD {
1379              
1380              i = atom->getLocalIndex();
1381              if(mm < 0) {
1382 <              clm[i] = eCos[l][i].x()*eCos[m][i].y()
1383 <                     + eSin[l][i].x()*eSin[m][i].y();
1351 <              slm[i] = eCos[l][i].x()*eCos[m][i].y()
1352 <                     - eSin[m][i].y()*eCos[l][i].x();
1382 >              clm[i]=elc[l][i]*emc[m][i]+els[l][i]*ems[m][i];
1383 >              slm[i]=els[l][i]*emc[m][i]-ems[m][i]*elc[l][i];
1384              } else {
1385 <              clm[i] = eCos[l][i].x()*eCos[m][i].y()
1386 <                     - eSin[l][i].x()*eSin[m][i].y();
1356 <              slm[i] = eSin[l][i].x()*eCos[m][i].y()
1357 <                     + eSin[m][i].y()*eCos[l][i].x();
1385 >              clm[i]=elc[l][i]*emc[m][i]-els[l][i]*ems[m][i];
1386 >              slm[i]=els[l][i]*emc[m][i]+ems[m][i]*elc[l][i];
1387              }
1388            }
1389          }
1390          for (int nnn = nMin; nnn <= kLim2; nnn++) {
1391            int nn = nnn - kLimit;          
1392            int n = abs(nn) + 1;
1393 <          RealType rn = zcl * float(nn);
1393 >          rn = zcl * float(nn);
1394            // Test on magnitude of k vector:
1395            int kk=ll*ll + mm*mm + nn*nn;
1396            if(kk <= kSqLim) {
1397 <            Vector3d kVec = Vector3d(rl, rm, rn);
1398 <            Mat3x3d  k2 = outProduct(kVec, kVec);
1397 >            kVec = Vector3d(rl, rm, rn);
1398 >            k2 = outProduct(kVec, kVec);
1399              // Calculate exp(ikr) terms
1400              for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1401                   mol = info_->nextMolecule(mi)) {
# Line 1375 | Line 1404 | namespace OpenMD {
1404                  i = atom->getLocalIndex();
1405                  
1406                  if (nn < 0) {
1407 <                  ckr[i]=clm[i]*eCos[n][i].z()+slm[i]*eSin[n][i].z();
1408 <                  skr[i]=slm[i]*eCos[n][i].z()-clm[i]*eSin[n][i].z();
1407 >                  ckr[i]=clm[i]*enc[n][i]+slm[i]*ens[n][i];
1408 >                  skr[i]=slm[i]*enc[n][i]-clm[i]*ens[n][i];
1409 >
1410                  } else {
1411 <                  ckr[i]=clm[i]*eCos[n][i].z()-slm[i]*eSin[n][i].z();
1412 <                  skr[i]=slm[i]*eCos[n][i].z()+clm[i]*eSin[n][i].z();
1411 >                  ckr[i]=clm[i]*enc[n][i]-slm[i]*ens[n][i];
1412 >                  skr[i]=slm[i]*enc[n][i]+clm[i]*ens[n][i];
1413                  }
1414                }
1415              }
# Line 1390 | Line 1420 | namespace OpenMD {
1420                   mol = info_->nextMolecule(mi)) {
1421                for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1422                    atom = mol->nextAtom(ai)) {
1423 <                i = atom->getGlobalIndex();
1423 >                i = atom->getLocalIndex();
1424                  int atid = atom->getAtomType()->getIdent();
1425 <                ElectrostaticAtomData data = ElectrostaticMap[Etids[atid]];
1425 >                data = ElectrostaticMap[Etids[atid]];
1426                                
1427                  if (data.is_Charge) {
1428 <                  RealType C = data.fixedCharge;
1428 >                  C = data.fixedCharge;
1429                    if (atom->isFluctuatingCharge()) C += atom->getFlucQPos();
1430                    ckc[i] = C * ckr[i];
1431 <                  cks[i] = C * cks[i];
1431 >                  cks[i] = C * skr[i];
1432                  }
1433                  
1434                  if (data.is_Dipole) {
1435 <                  Vector3d D = atom->getDipole() * mPoleConverter;
1436 <                  RealType dk = dot(kVec, D);
1437 <                  dxk[i] = cross(kVec, D);
1435 >                  D = atom->getDipole() * mPoleConverter;
1436 >                  dk = dot(D, kVec);
1437 >                  dxk[i] = cross(D, kVec);
1438                    dkc[i] = dk * ckr[i];
1439                    dks[i] = dk * skr[i];
1440                  }
1441                  if (data.is_Quadrupole) {
1442 <                  Mat3x3d Q = atom->getQuadrupole();
1443 <                  Q *= mPoleConverter;
1444 <                  RealType qk = -( Q * k2 ).trace();
1445 <                  qxk[i] = -2.0 * cross(k2, Q);
1442 >                  Q = atom->getQuadrupole() * mPoleConverter;
1443 >                  Qk = Q * kVec;                  
1444 >                  qk = dot(Qk, kVec);
1445 >                  qxk[i] = cross(Qk, kVec);
1446                    qkc[i] = qk * ckr[i];
1447                    qks[i] = qk * skr[i];
1448                  }              
1449                }
1450              }
1451 <            
1451 >
1452              // calculate vector sums
1453              
1454 <            RealType ckcs = std::accumulate(ckc.begin(),ckc.end(),0.0);
1455 <            RealType ckss = std::accumulate(cks.begin(),cks.end(),0.0);
1456 <            RealType dkcs = std::accumulate(dkc.begin(),dkc.end(),0.0);
1457 <            RealType dkss = std::accumulate(dks.begin(),dks.end(),0.0);
1458 <            RealType qkcs = std::accumulate(qkc.begin(),qkc.end(),0.0);
1459 <            RealType qkss = std::accumulate(qks.begin(),qks.end(),0.0);
1454 >            ckcs = std::accumulate(ckc.begin(),ckc.end(),0.0);
1455 >            ckss = std::accumulate(cks.begin(),cks.end(),0.0);
1456 >            dkcs = std::accumulate(dkc.begin(),dkc.end(),0.0);
1457 >            dkss = std::accumulate(dks.begin(),dks.end(),0.0);
1458 >            qkcs = std::accumulate(qkc.begin(),qkc.end(),0.0);
1459 >            qkss = std::accumulate(qks.begin(),qks.end(),0.0);
1460              
1461   #ifdef IS_MPI
1462              MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &ckcs, 1, MPI::REALTYPE,
# Line 1444 | Line 1474 | namespace OpenMD {
1474   #endif        
1475              
1476              // Accumulate potential energy and virial contribution:
1477 +
1478 +            kPot += 2.0 * rvol * AK[kk]*((ckss+dkcs-qkss)*(ckss+dkcs-qkss)
1479 +                                         + (ckcs-dkss-qkcs)*(ckcs-dkss-qkcs));
1480 +
1481 +            kVir += 2.0 * rvol  * AK[kk]*(ckcs*ckcs+ckss*ckss
1482 +                                          +4.0*(ckss*dkcs-ckcs*dkss)
1483 +                                          +3.0*(dkcs*dkcs+dkss*dkss)
1484 +                                          -6.0*(ckss*qkss+ckcs*qkcs)
1485 +                                          +8.0*(dkss*qkcs-dkcs*qkss)
1486 +                                          +5.0*(qkss*qkss+qkcs*qkcs));
1487              
1448            //cerr << "l, m, n = " << l << " " << m << " " << n << "\n";
1449            cerr << "kVec = " << kVec << "\n";
1450            cerr << "ckss = " << ckss << " ckcs = " << ckcs << "\n";
1451            kPot += 2.0 * rvol * AK[kk]*((ckss+dkcs-qkss)*(ckss+dkcs-qkss)
1452                                         + (ckcs-dkss-qkcs)*(ckcs-dkss-qkss));
1453            //cerr << "kspace pot = " << kPot << "\n";
1454            kVir -= 2.0 * rvol * AK[kk]*(ckcs*ckcs+ckss*ckss
1455                                         +4.0*(ckss*dkcs-ckcs*dkss)
1456                                         +3.0*(dkcs*dkcs+dkss*dkss)
1457                                         -6.0*(ckss*qkss+ckcs*qkcs)
1458                                         +8.0*(dkss*qkcs-dkcs*qkss)
1459                                         +5.0*(qkss*qkss+qkcs*qkcs));
1460            
1488              // Calculate force and torque for each site:
1489              
1490              for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
# Line 1466 | Line 1493 | namespace OpenMD {
1493                    atom = mol->nextAtom(ai)) {
1494                  
1495                  i = atom->getLocalIndex();
1496 <                int atid = atom->getAtomType()->getIdent();
1497 <                ElectrostaticAtomData data = ElectrostaticMap[Etids[atid]];
1498 <                
1496 >                atid = atom->getAtomType()->getIdent();
1497 >                data = ElectrostaticMap[Etids[atid]];
1498 >
1499                  RealType qfrc = AK[kk]*((cks[i]+dkc[i]-qks[i])*(ckcs-dkss-qkcs)
1500 <                                        - (ckc[i]-dks[i]-qkc[i])*(ckss+dkcs-qkss));
1500 >                                     - (ckc[i]-dks[i]-qkc[i])*(ckss+dkcs-qkss));
1501                  RealType qtrq1 = AK[kk]*(skr[i]*(ckcs-dkss-qkcs)
1502                                           -ckr[i]*(ckss+dkcs-qkss));
1503 <                RealType qtrq2 = 2.0*AK[kk]*(ckr[i]*(ckcs-dkss-qkcs)+
1504 <                                             skr[i]*(ckss+dkcs-qkss));
1505 <                
1479 <                
1503 >                RealType qtrq2 = 2.0*AK[kk]*(ckr[i]*(ckcs-dkss-qkcs)
1504 >                                             +skr[i]*(ckss+dkcs-qkss));
1505 >              
1506                  atom->addFrc( 4.0 * rvol * qfrc * kVec );
1507                  
1508                  if (data.is_Dipole) {
# Line 1489 | Line 1515 | namespace OpenMD {
1515              }
1516            }
1517          }
1518 +        nMin = 1;
1519        }
1520 +      mMin = 1;
1521      }
1522 +    pot += kPot;  
1523    }
1524   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines