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 1921 by gezelter, Thu Aug 1 18:23:07 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  
61 +
62   namespace OpenMD {
63    
64    Electrostatic::Electrostatic(): name_("Electrostatic"), initialized_(false),
# Line 191 | Line 194 | namespace OpenMD {
194        simError();
195      }
196            
197 <    if (screeningMethod_ == DAMPED) {      
197 >    if (screeningMethod_ == DAMPED || summationMethod_ == esm_EWALD_FULL) {
198        if (!simParams_->haveDampingAlpha()) {
199          // first set a cutoff dependent alpha value
200          // we assume alpha depends linearly with rcut from 0 to 20.5 ang
201          dampingAlpha_ = 0.425 - cutoffRadius_* 0.02;
202 <        if (dampingAlpha_ < 0.0) dampingAlpha_ = 0.0;
200 <        
202 >        if (dampingAlpha_ < 0.0) dampingAlpha_ = 0.0;        
203          // throw warning
204          sprintf( painCave.errMsg,
205                   "Electrostatic::initialize: dampingAlpha was not specified in the\n"
# Line 213 | Line 215 | namespace OpenMD {
215        haveDampingAlpha_ = true;
216      }
217  
218 +
219      Etypes.clear();
220      Etids.clear();
221      FQtypes.clear();
# Line 262 | Line 265 | namespace OpenMD {
265        b3c = (5.0 * b2c + pow(2.0*a2, 3) * expTerm * invArootPi) / r2;
266        b4c = (7.0 * b3c + pow(2.0*a2, 4) * expTerm * invArootPi) / r2;
267        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;
268        // Half the Smith self piece:
269        selfMult1_ = - a2 * invArootPi;
270        selfMult2_ = - 2.0 * a2 * a2 * invArootPi / 3.0;
# Line 288 | Line 288 | namespace OpenMD {
288      db0c_3 =          3.0*r*b2c  - r2*r*b3c;
289      db0c_4 =          3.0*b2c  - 6.0*r2*b3c     + r2*r2*b4c;
290      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;
291 >
292 >    if (summationMethod_ == esm_EWALD_FULL) {
293 >      selfMult1_ *= 2.0;
294 >      selfMult2_ *= 2.0;
295 >      selfMult4_ *= 2.0;
296 >    } else {
297 >      selfMult1_ -= b0c;
298 >      selfMult2_ += (db0c_2 + 2.0*db0c_1*ric) /  3.0;
299 >      selfMult4_ -= (db0c_4 + 4.0*db0c_3*ric) / 15.0;
300 >    }
301  
302      // working variables for the splines:
303      RealType ri, ri2;
# Line 328 | Line 334 | namespace OpenMD {
334      vector<RealType> v31v, v32v;
335      vector<RealType> v41v, v42v, v43v;
336  
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
337      for (int i = 1; i < np_ + 1; i++) {
338        r = RealType(i) * dx;
339        rv.push_back(r);
# Line 499 | Line 497 | namespace OpenMD {
497  
498        case esm_SWITCHING_FUNCTION:
499        case esm_HARD:
500 +      case esm_EWALD_FULL:
501  
502          v01 = f;
503          v11 = g;
# Line 557 | Line 556 | namespace OpenMD {
556  
557          break;
558                  
560      case esm_EWALD_FULL:
559        case esm_EWALD_PME:
560        case esm_EWALD_SPME:
561        default :
# Line 586 | Line 584 | namespace OpenMD {
584        v41v.push_back(v41);
585        v42v.push_back(v42);
586        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      */
587      }
588  
589      // construct the spline structures and fill them with the values we've
# Line 620 | Line 607 | namespace OpenMD {
607      v42s->addPoints(rv, v42v);
608      v43s = new CubicSpline();
609      v43s->addPoints(rv, v43v);
623
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    */
610  
611      haveElectroSplines_ = true;
612  
# Line 715 | Line 681 | namespace OpenMD {
681        FQtids[atid] = fqtid;
682        Jij[fqtid].resize(nFlucq_);
683  
684 <      // Now, iterate over all known fluctuating and add to the coulomb integral map:
684 >      // Now, iterate over all known fluctuating and add to the
685 >      // coulomb integral map:
686        
687        std::set<int>::iterator it;
688        for( it = FQtypes.begin(); it != FQtypes.end(); ++it) {    
# Line 1202 | Line 1169 | namespace OpenMD {
1169        
1170      case esm_SHIFTED_FORCE:
1171      case esm_SHIFTED_POTENTIAL:
1172 +    case esm_TAYLOR_SHIFTED:
1173 +    case esm_EWALD_FULL:
1174        if (i_is_Charge)
1175          self += selfMult1_ * pre11_ * C_a * (C_a + *(sdat.skippedCharge));      
1176        if (i_is_Dipole)
# Line 1227 | Line 1196 | namespace OpenMD {
1196      // cases.
1197      return 12.0;
1198    }
1199 +
1200 +
1201 +  void Electrostatic::ReciprocalSpaceSum(potVec& pot) {
1202 +    
1203 +    RealType kPot = 0.0;
1204 +    RealType kVir = 0.0;
1205 +    
1206 +    const RealType mPoleConverter = 0.20819434; // converts from the
1207 +                                                // internal units of
1208 +                                                // Debye (for dipoles)
1209 +                                                // or Debye-angstroms
1210 +                                                // (for quadrupoles) to
1211 +                                                // electron angstroms or
1212 +                                                // electron-angstroms^2
1213 +    
1214 +    const RealType eConverter = 332.0637778; // convert the
1215 +                                             // Charge-Charge
1216 +                                             // electrostatic
1217 +                                             // interactions into kcal /
1218 +                                             // mol assuming distances
1219 +                                             // are measured in
1220 +                                             // angstroms.
1221 +
1222 +    Mat3x3d hmat = info_->getSnapshotManager()->getCurrentSnapshot()->getHmat();
1223 +    Vector3d box = hmat.diagonals();
1224 +    RealType boxMax = box.max();
1225 +    
1226 +    cerr << "da = " << dampingAlpha_ << " rc = " << cutoffRadius_ << "\n";
1227 +    cerr << "boxMax = " << boxMax << "\n";
1228 +    //int kMax = int(2.0 * M_PI / (pow(dampingAlpha_,2)*cutoffRadius_ * boxMax) );
1229 +    int kMax = 5;
1230 +    cerr << "kMax = " << kMax << "\n";
1231 +    int kSqMax = kMax*kMax + 2;
1232 +    
1233 +    int kLimit = kMax+1;
1234 +    int kLim2 = 2*kMax+1;
1235 +    int kSqLim = kSqMax;
1236 +    
1237 +    vector<RealType> AK(kSqLim+1, 0.0);
1238 +    RealType xcl = 2.0 * M_PI / box.x();
1239 +    RealType ycl = 2.0 * M_PI / box.y();
1240 +    RealType zcl = 2.0 * M_PI / box.z();
1241 +    RealType rcl = 2.0 * M_PI / boxMax;
1242 +    RealType rvol = 2.0 * M_PI /(box.x() * box.y() * box.z());
1243 +    
1244 +    if(dampingAlpha_ < 1.0e-12) return;
1245 +    
1246 +    RealType ralph = -0.25/pow(dampingAlpha_,2);
1247 +    
1248 +    // Calculate and store exponential factors  
1249 +    
1250 +    vector<vector<Vector3d> > eCos;
1251 +    vector<vector<Vector3d> > eSin;
1252 +    
1253 +    int nMax = info_->getNAtoms();
1254 +    
1255 +    eCos.resize(kLimit+1);
1256 +    eSin.resize(kLimit+1);
1257 +    for (int j = 0; j < kLimit+1; j++) {
1258 +      eCos[j].resize(nMax);
1259 +      eSin[j].resize(nMax);
1260 +    }
1261 +    
1262 +    Vector3d t( 2.0 * M_PI );
1263 +    t.Vdiv(t, box);
1264 +    
1265 +    SimInfo::MoleculeIterator mi;
1266 +    Molecule::AtomIterator ai;
1267 +    int i;
1268 +    Vector3d r;
1269 +    Vector3d tt;
1270 +    Vector3d w;
1271 +    Vector3d u;
1272 +    Vector3d a;
1273 +    Vector3d b;
1274 +    
1275 +    for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1276 +         mol = info_->nextMolecule(mi)) {
1277 +      for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1278 +          atom = mol->nextAtom(ai)) {  
1279 +        
1280 +        i = atom->getLocalIndex();
1281 +        r = atom->getPos();
1282 +        info_->getSnapshotManager()->getCurrentSnapshot()->wrapVector(r);
1283 +        
1284 +        // Shift so that all coordinates are in the range [0,L]:
1285 +
1286 +        r += box/2.0;
1287 +
1288 +        tt.Vmul(t, r);
1289 +
1290 +        //cerr << "tt = " << tt << "\n";
1291 +        
1292 +        eCos[1][i] = Vector3d(1.0, 1.0, 1.0);
1293 +        eSin[1][i] = Vector3d(0.0, 0.0, 0.0);
1294 +        eCos[2][i] = Vector3d(cos(tt.x()), cos(tt.y()), cos(tt.z()));
1295 +        eSin[2][i] = Vector3d(sin(tt.x()), sin(tt.y()), sin(tt.z()));
1296 +        u = eCos[2][i];
1297 +        w = eSin[2][i];
1298 +        
1299 +        for(int l = 3; l <= kLimit; l++) {
1300 +          a.Vmul(eCos[l-1][i], u);
1301 +          b.Vmul(eSin[l-1][i], w);
1302 +          eCos[l][i] = a - b;
1303 +          a.Vmul(eSin[l-1][i], u);
1304 +          b.Vmul(eCos[l-1][i], w);
1305 +          eSin[l][i] = a + b;
1306 +        }
1307 +      }
1308 +    }
1309 +    
1310 +    // Calculate and store AK coefficients:
1311 +    
1312 +    RealType eksq = 1.0;
1313 +    RealType expf = 0.0;
1314 +    if (ralph < 0.0) expf = exp(ralph*rcl*rcl);
1315 +    for (i = 1; i <= kSqLim; i++) {
1316 +      RealType rksq = float(i)*rcl*rcl;
1317 +      eksq = expf*eksq;
1318 +      AK[i] = eConverter * eksq/rksq;
1319 +    }
1320 +    
1321 +    /*
1322 +     * Loop over all k vectors k = 2 pi (ll/Lx, mm/Ly, nn/Lz)
1323 +     * the values of ll, mm and nn are selected so that the symmetry of
1324 +     * reciprocal lattice is taken into account i.e. the following
1325 +     * rules apply.
1326 +     *
1327 +     * ll ranges over the values 0 to kMax only.
1328 +     *
1329 +     * mm ranges over 0 to kMax when ll=0 and over
1330 +     *            -kMax to kMax otherwise.
1331 +     * nn ranges over 1 to kMax when ll=mm=0 and over
1332 +     *            -kMax to kMax otherwise.
1333 +     *
1334 +     * Hence the result of the summation must be doubled at the end.    
1335 +     */
1336 +    
1337 +    std::vector<RealType> clm(nMax, 0.0);
1338 +    std::vector<RealType> slm(nMax, 0.0);
1339 +    std::vector<RealType> ckr(nMax, 0.0);
1340 +    std::vector<RealType> skr(nMax, 0.0);
1341 +    std::vector<RealType> ckc(nMax, 0.0);
1342 +    std::vector<RealType> cks(nMax, 0.0);
1343 +    std::vector<RealType> dkc(nMax, 0.0);
1344 +    std::vector<RealType> dks(nMax, 0.0);
1345 +    std::vector<RealType> qkc(nMax, 0.0);
1346 +    std::vector<RealType> qks(nMax, 0.0);
1347 +    std::vector<Vector3d> dxk(nMax, V3Zero);
1348 +    std::vector<Vector3d> qxk(nMax, V3Zero);
1349 +    
1350 +    int mMin = kLimit;
1351 +    int nMin = kLimit + 1;
1352 +    for (int l = 1; l <= kLimit; l++) {
1353 +      int ll =l - 1;
1354 +      RealType rl = xcl * float(ll);
1355 +      for (int mmm = mMin; mmm <= kLim2; mmm++) {
1356 +        int mm = mmm - kLimit;
1357 +        int m = abs(mm) + 1;
1358 +        RealType rm = ycl * float(mm);
1359 +        // Set temporary products of exponential terms
1360 +        for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1361 +             mol = info_->nextMolecule(mi)) {
1362 +          for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1363 +              atom = mol->nextAtom(ai)) {
1364 +            
1365 +            i = atom->getLocalIndex();
1366 +            if(mm < 0) {
1367 +              clm[i] = eCos[l][i].x()*eCos[m][i].y()
1368 +                     + 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();
1371 +            } else {
1372 +              clm[i] = eCos[l][i].x()*eCos[m][i].y()
1373 +                     - 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();
1376 +            }
1377 +          }
1378 +        }
1379 +        for (int nnn = nMin; nnn <= kLim2; nnn++) {
1380 +          int nn = nnn - kLimit;          
1381 +          int n = abs(nn) + 1;
1382 +          RealType rn = zcl * float(nn);
1383 +          // Test on magnitude of k vector:
1384 +          int kk=ll*ll + mm*mm + nn*nn;
1385 +          if(kk <= kSqLim) {
1386 +            Vector3d kVec = Vector3d(rl, rm, rn);
1387 +            Mat3x3d  k2 = outProduct(kVec, kVec);
1388 +            // Calculate exp(ikr) terms
1389 +            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1390 +                 mol = info_->nextMolecule(mi)) {
1391 +              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1392 +                  atom = mol->nextAtom(ai)) {
1393 +                i = atom->getLocalIndex();
1394 +                
1395 +                if (nn < 0) {
1396 +                  ckr[i]=clm[i]*eCos[n][i].z()+slm[i]*eSin[n][i].z();
1397 +                  skr[i]=slm[i]*eCos[n][i].z()-clm[i]*eSin[n][i].z();
1398 +                } else {
1399 +                  ckr[i]=clm[i]*eCos[n][i].z()-slm[i]*eSin[n][i].z();
1400 +                  skr[i]=slm[i]*eCos[n][i].z()+clm[i]*eSin[n][i].z();
1401 +                }
1402 +              }
1403 +            }
1404 +            
1405 +            // Calculate scalar and vector products for each site:
1406 +            
1407 +            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1408 +                 mol = info_->nextMolecule(mi)) {
1409 +              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1410 +                  atom = mol->nextAtom(ai)) {
1411 +                i = atom->getLocalIndex();
1412 +                int atid = atom->getAtomType()->getIdent();
1413 +                ElectrostaticAtomData data = ElectrostaticMap[Etids[atid]];
1414 +                              
1415 +                if (data.is_Charge) {
1416 +                  RealType C = data.fixedCharge;
1417 +                  if (atom->isFluctuatingCharge()) C += atom->getFlucQPos();
1418 +                  ckc[i] = C * ckr[i];
1419 +                  cks[i] = C * skr[i];
1420 +                }
1421 +                
1422 +                if (data.is_Dipole) {
1423 +                  Vector3d D = atom->getDipole() * mPoleConverter;
1424 +                  RealType dk = dot(kVec, D);
1425 +                  dxk[i] = cross(kVec, D);
1426 +                  dkc[i] = dk * ckr[i];
1427 +                  dks[i] = dk * skr[i];
1428 +                }
1429 +                if (data.is_Quadrupole) {
1430 +                  Mat3x3d Q = atom->getQuadrupole();
1431 +                  Q *= mPoleConverter;
1432 +                  RealType qk = -( Q * k2 ).trace();
1433 +                  qxk[i] = -2.0 * cross(k2, Q);
1434 +                  qkc[i] = qk * ckr[i];
1435 +                  qks[i] = qk * skr[i];
1436 +                }              
1437 +              }
1438 +            }
1439 +
1440 +            // calculate vector sums
1441 +            
1442 +            RealType ckcs = std::accumulate(ckc.begin(),ckc.end(),0.0);
1443 +            RealType ckss = std::accumulate(cks.begin(),cks.end(),0.0);
1444 +            RealType dkcs = std::accumulate(dkc.begin(),dkc.end(),0.0);
1445 +            RealType dkss = std::accumulate(dks.begin(),dks.end(),0.0);
1446 +            RealType qkcs = std::accumulate(qkc.begin(),qkc.end(),0.0);
1447 +            RealType qkss = std::accumulate(qks.begin(),qks.end(),0.0);
1448 +
1449 +            
1450 + #ifdef IS_MPI
1451 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &ckcs, 1, MPI::REALTYPE,
1452 +                                      MPI::SUM);
1453 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &ckss, 1, MPI::REALTYPE,
1454 +                                      MPI::SUM);
1455 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &dkcs, 1, MPI::REALTYPE,
1456 +                                      MPI::SUM);
1457 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &dkss, 1, MPI::REALTYPE,
1458 +                                      MPI::SUM);
1459 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &qkcs, 1, MPI::REALTYPE,
1460 +                                      MPI::SUM);
1461 +            MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &qkss, 1, MPI::REALTYPE,
1462 +                                      MPI::SUM);
1463 + #endif        
1464 +            
1465 +            // Accumulate potential energy and virial contribution:          
1466 +
1467 +            kPot += 2.0 * rvol * AK[kk]*((ckss+dkcs-qkss)*(ckss+dkcs-qkss)
1468 +                                         + (ckcs-dkss-qkcs)*(ckcs-dkss-qkss));
1469 +
1470 +            kVir -= 2.0 * rvol * AK[kk]*(ckcs*ckcs+ckss*ckss
1471 +                                         +4.0*(ckss*dkcs-ckcs*dkss)
1472 +                                         +3.0*(dkcs*dkcs+dkss*dkss)
1473 +                                         -6.0*(ckss*qkss+ckcs*qkcs)
1474 +                                         +8.0*(dkss*qkcs-dkcs*qkss)
1475 +                                         +5.0*(qkss*qkss+qkcs*qkcs));
1476 +            
1477 +            // Calculate force and torque for each site:
1478 +            
1479 +            for (Molecule* mol = info_->beginMolecule(mi); mol != NULL;
1480 +                 mol = info_->nextMolecule(mi)) {
1481 +              for(Atom* atom = mol->beginAtom(ai); atom != NULL;
1482 +                  atom = mol->nextAtom(ai)) {
1483 +                
1484 +                i = atom->getLocalIndex();
1485 +                int atid = atom->getAtomType()->getIdent();
1486 +                ElectrostaticAtomData data = ElectrostaticMap[Etids[atid]];
1487 +                
1488 +                RealType qfrc = AK[kk]*((cks[i]+dkc[i]-qks[i])*(ckcs-dkss-qkcs)
1489 +                                        - (ckc[i]-dks[i]-qkc[i])*(ckss+dkcs-qkss));
1490 +                RealType qtrq1 = AK[kk]*(skr[i]*(ckcs-dkss-qkcs)
1491 +                                         -ckr[i]*(ckss+dkcs-qkss));
1492 +                RealType qtrq2 = 2.0*AK[kk]*(ckr[i]*(ckcs-dkss-qkcs)+
1493 +                                             skr[i]*(ckss+dkcs-qkss));
1494 +                
1495 +                
1496 +                atom->addFrc( 4.0 * rvol * qfrc * kVec );
1497 +                
1498 +                if (data.is_Dipole) {
1499 +                  atom->addTrq( 4.0 * rvol * qtrq1 * dxk[i] );
1500 +                }
1501 +                if (data.is_Quadrupole) {
1502 +                  atom->addTrq( 4.0 * rvol * qtrq2 * qxk[i] );
1503 +                }
1504 +              }
1505 +            }
1506 +          }
1507 +        }
1508 +      }
1509 +    }
1510 +    cerr << "kPot = " << kPot << "\n";
1511 +    pot[ELECTROSTATIC_FAMILY] += kPot;  
1512 +  }
1513   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines