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 1922 by gezelter, Mon Aug 5 13:41:15 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 + #ifdef IS_MPI
61 + #include <mpi.h>
62 + #endif
63  
61
64   namespace OpenMD {
65    
66    Electrostatic::Electrostatic(): name_("Electrostatic"), initialized_(false),
# Line 764 | Line 766 | namespace OpenMD {
766      // Excluded potential that is still computed for fluctuating charges
767      excluded_Pot= 0.0;
768  
767
769      // some variables we'll need independent of electrostatic type:
770  
771      ri = 1.0 /  *(idat.rij);
# Line 885 | 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 905 | 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;
919 <          }          
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 1140 | 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
1144 <      *(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 1194 | Line 1194 | namespace OpenMD {
1194    }
1195  
1196  
1197 <  void Electrostatic::ReciprocalSpaceSum(potVec& pot) {
1197 >  void Electrostatic::ReciprocalSpaceSum(RealType& pot) {
1198      
1199      RealType kPot = 0.0;
1200      RealType kVir = 0.0;
# Line 1240 | 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 );
# Line 1261 | Line 1275 | namespace OpenMD {
1275      int i;
1276      Vector3d r;
1277      Vector3d tt;
1264    Vector3d w;
1265    Vector3d u;
1266    Vector3d a;
1267    Vector3d b;
1278      
1279      for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1280           mol = info_->nextMolecule(mi)) {
# Line 1277 | Line 1287 | namespace OpenMD {
1287          
1288          tt.Vmul(t, r);
1289  
1290 <        
1291 <        eCos[1][i] = Vector3d(1.0, 1.0, 1.0);
1292 <        eSin[1][i] = Vector3d(0.0, 0.0, 0.0);
1293 <        eCos[2][i] = Vector3d(cos(tt.x()), cos(tt.y()), cos(tt.z()));
1294 <        eSin[2][i] = Vector3d(sin(tt.x()), sin(tt.y()), sin(tt.z()));
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 <        u = eCos[2][i];
1298 <        w = eSin[2][i];
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          
1304          for(int l = 3; l <= kLimit; l++) {
1305 <          eCos[l][i].x() = eCos[l-1][i].x()*eCos[2][i].x() - eSin[l-1][i].x()*eSin[2][i].x();
1306 <          eCos[l][i].y() = eCos[l-1][i].y()*eCos[2][i].y() - eSin[l-1][i].y()*eSin[2][i].y();
1307 <          eCos[l][i].z() = eCos[l-1][i].z()*eCos[2][i].z() - eSin[l-1][i].z()*eSin[2][i].z();
1308 <          
1309 <          eSin[l][i].x() = eSin[l-1][i].x()*eCos[2][i].x() + eCos[l-1][i].x()*eSin[2][i].x();
1310 <          eSin[l][i].y() = eSin[l-1][i].y()*eCos[2][i].y() + eCos[l-1][i].y()*eSin[2][i].z();
1296 <          eSin[l][i].z() = eSin[l-1][i].z()*eCos[2][i].z() + eCos[l-1][i].z()*eSin[2][i].y();
1297 <
1298 <
1299 <          // a.Vmul(eCos[l-1][i], u);
1300 <          // b.Vmul(eSin[l-1][i], w);
1301 <          // eCos[l][i] = a - b;
1302 <          // a.Vmul(eSin[l-1][i], u);
1303 <          // b.Vmul(eCos[l-1][i], w);
1304 <          // eSin[l][i] = a + b;
1305 <        
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 1346 | 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);
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 1364 | 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();
1369 <              slm[i] = eSin[l][i].x()*eCos[m][i].y()
1370 <                     - 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();
1374 <              slm[i] = eSin[l][i].x()*eCos[m][i].y()
1375 <                     + 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 1393 | 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 1410 | Line 1422 | namespace OpenMD {
1422                    atom = mol->nextAtom(ai)) {
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 * 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                  }              
# Line 1439 | Line 1451 | namespace OpenMD {
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);
1448 <
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 1462 | Line 1473 | namespace OpenMD {
1473                                        MPI::SUM);
1474   #endif        
1475              
1476 <            // Accumulate potential energy and virial contribution:          
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-qkss));
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));
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              
1488              // Calculate force and torque for each site:
1489              
# Line 1482 | 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));
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 <                
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 1508 | Line 1519 | namespace OpenMD {
1519        }
1520        mMin = 1;
1521      }
1522 <    cerr << "kPot = " << kPot << "\n";
1512 <    pot[ELECTROSTATIC_FAMILY] += kPot;  
1522 >    pot += kPot;  
1523    }
1524   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines