ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/brains/ForceManager.cpp
(Generate patch)

Comparing branches/development/src/brains/ForceManager.cpp (file contents):
Revision 1709 by gezelter, Tue May 15 13:04:08 2012 UTC vs.
Revision 1850 by gezelter, Wed Feb 20 15:39:39 2013 UTC

# Line 35 | Line 35
35   *                                                                      
36   * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).            
37   * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).          
38 < * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008).          
38 > * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).          
39   * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010).
40   * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41   */
# Line 44 | Line 44
44   * @file ForceManager.cpp
45   * @author tlin
46   * @date 11/09/2004
47 * @time 10:39am
47   * @version 1.0
48   */
49  
# Line 58 | Line 57
57   #include "primitives/Torsion.hpp"
58   #include "primitives/Inversion.hpp"
59   #include "nonbonded/NonBondedInteraction.hpp"
60 + #include "perturbations/ElectricField.hpp"
61   #include "parallel/ForceMatrixDecomposition.hpp"
62  
63   #include <cstdio>
# Line 110 | Line 110 | namespace OpenMD {
110      Globals* simParams_ = info_->getSimParams();
111      ForceFieldOptions& forceFieldOptions_ = forceField_->getForceFieldOptions();
112      int mdFileVersion;
113 +    rCut_ = 0.0; //Needs a value for a later max() call;  
114      
115      if (simParams_->haveMDfileVersion())
116        mdFileVersion = simParams_->getMDfileVersion();
117      else
118        mdFileVersion = 0;
119    
120 +    // We need the list of simulated atom types to figure out cutoffs
121 +    // as well as long range corrections.
122 +
123 +    set<AtomType*>::iterator i;
124 +    set<AtomType*> atomTypes_;
125 +    atomTypes_ = info_->getSimulatedAtomTypes();
126 +
127      if (simParams_->haveCutoffRadius()) {
128        rCut_ = simParams_->getCutoffRadius();
129      } else {      
# Line 130 | Line 138 | namespace OpenMD {
138          rCut_ = 12.0;
139        } else {
140          RealType thisCut;
141 <        set<AtomType*>::iterator i;
134 <        set<AtomType*> atomTypes;
135 <        atomTypes = info_->getSimulatedAtomTypes();        
136 <        for (i = atomTypes.begin(); i != atomTypes.end(); ++i) {
141 >        for (i = atomTypes_.begin(); i != atomTypes_.end(); ++i) {
142            thisCut = interactionMan_->getSuggestedCutoffRadius((*i));
143            rCut_ = max(thisCut, rCut_);
144          }
# Line 201 | Line 206 | namespace OpenMD {
206          // electrostaticSummationMethod keyword.
207          
208          if (simParams_->haveElectrostaticSummationMethod()) {
209 <          std::string myMethod = simParams_->getElectrostaticSummationMethod();
209 >          string myMethod = simParams_->getElectrostaticSummationMethod();
210            toUpper(myMethod);
211          
212            if (myMethod == "SHIFTED_POTENTIAL") {
# Line 256 | Line 261 | namespace OpenMD {
261      stringToCutoffPolicy["MAX"] = MAX;
262      stringToCutoffPolicy["TRADITIONAL"] = TRADITIONAL;    
263  
264 <    std::string cutPolicy;
264 >    string cutPolicy;
265      if (forceFieldOptions_.haveCutoffPolicy()){
266        cutPolicy = forceFieldOptions_.getCutoffPolicy();
267      }else if (simParams_->haveCutoffPolicy()) {
# Line 368 | Line 373 | namespace OpenMD {
373      }
374      switcher_->setSwitchType(sft_);
375      switcher_->setSwitch(rSwitch_, rCut_);
371    interactionMan_->setSwitchingRadius(rSwitch_);
376    }
377  
378  
# Line 388 | Line 392 | namespace OpenMD {
392        setupCutoffs();
393  
394        info_->prepareTopology();      
395 +
396 +      doParticlePot_ = info_->getSimParams()->getOutputParticlePotential();
397 +      doHeatFlux_ = info_->getSimParams()->getPrintHeatFlux();
398 +      if (doHeatFlux_) doParticlePot_ = true;
399 +
400 +      doElectricField_ = info_->getSimParams()->getOutputElectricField();
401 +  
402      }
403  
404      ForceFieldOptions& fopts = forceField_->getForceFieldOptions();
# Line 417 | Line 428 | namespace OpenMD {
428      electrostaticScale_[2] = fopts.getelectrostatic13scale();
429      electrostaticScale_[3] = fopts.getelectrostatic14scale();    
430      
431 +    if (info_->getSimParams()->haveElectricField()) {
432 +      ElectricField* eField = new ElectricField(info_);
433 +      perturbations_.push_back(eField);
434 +    }
435 +
436      fDecomp_->distributeInitialData();
437  
438      initialized_ = true;
# Line 443 | Line 459 | namespace OpenMD {
459      Molecule::CutoffGroupIterator ci;
460      CutoffGroup* cg;
461      
462 <    // forces are zeroed here, before any are accumulated.
462 >    // forces and potentials are zeroed here, before any are
463 >    // accumulated.
464      
465 +    Snapshot* snap = info_->getSnapshotManager()->getCurrentSnapshot();
466 +
467 +    snap->setBondPotential(0.0);
468 +    snap->setBendPotential(0.0);
469 +    snap->setTorsionPotential(0.0);
470 +    snap->setInversionPotential(0.0);
471 +
472 +    potVec zeroPot(0.0);
473 +    snap->setLongRangePotential(zeroPot);
474 +    snap->setExcludedPotentials(zeroPot);
475 +
476 +    snap->setRestraintPotential(0.0);
477 +    snap->setRawPotential(0.0);
478 +
479      for (mol = info_->beginMolecule(mi); mol != NULL;
480           mol = info_->nextMolecule(mi)) {
481        for(atom = mol->beginAtom(ai); atom != NULL;
# Line 468 | Line 499 | namespace OpenMD {
499      }
500      
501      // Zero out the stress tensor
502 <    tau *= 0.0;
503 <    
502 >    stressTensor *= 0.0;
503 >    // Zero out the heatFlux
504 >    fDecomp_->setHeatFlux( Vector3d(0.0) );    
505    }
506    
507    void ForceManager::shortRangeInteractions() {
# Line 502 | Line 534 | namespace OpenMD {
534  
535        for (bond = mol->beginBond(bondIter); bond != NULL;
536             bond = mol->nextBond(bondIter)) {
537 <        bond->calcForce();
537 >        bond->calcForce(doParticlePot_);
538          bondPotential += bond->getPotential();
539        }
540  
# Line 510 | Line 542 | namespace OpenMD {
542             bend = mol->nextBend(bendIter)) {
543          
544          RealType angle;
545 <        bend->calcForce(angle);
545 >        bend->calcForce(angle, doParticlePot_);
546          RealType currBendPot = bend->getPotential();          
547          
548          bendPotential += bend->getPotential();
# Line 535 | Line 567 | namespace OpenMD {
567        for (torsion = mol->beginTorsion(torsionIter); torsion != NULL;
568             torsion = mol->nextTorsion(torsionIter)) {
569          RealType angle;
570 <        torsion->calcForce(angle);
570 >        torsion->calcForce(angle, doParticlePot_);
571          RealType currTorsionPot = torsion->getPotential();
572          torsionPotential += torsion->getPotential();
573          map<Torsion*, TorsionDataSet>::iterator i = torsionDataSets.find(torsion);
# Line 559 | Line 591 | namespace OpenMD {
591             inversion != NULL;
592             inversion = mol->nextInversion(inversionIter)) {
593          RealType angle;
594 <        inversion->calcForce(angle);
594 >        inversion->calcForce(angle, doParticlePot_);
595          RealType currInversionPot = inversion->getPotential();
596          inversionPotential += inversion->getPotential();
597          map<Inversion*, InversionDataSet>::iterator i = inversionDataSets.find(inversion);
# Line 579 | Line 611 | namespace OpenMD {
611          }      
612        }      
613      }
614 <    
615 <    RealType  shortRangePotential = bondPotential + bendPotential +
616 <      torsionPotential +  inversionPotential;    
614 >
615 > #ifdef IS_MPI
616 >    // Collect from all nodes.  This should eventually be moved into a
617 >    // SystemDecomposition, but this is a better place than in
618 >    // Thermo to do the collection.
619 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &bondPotential, 1, MPI::REALTYPE,
620 >                              MPI::SUM);
621 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &bendPotential, 1, MPI::REALTYPE,
622 >                              MPI::SUM);
623 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &torsionPotential, 1,
624 >                              MPI::REALTYPE, MPI::SUM);
625 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &inversionPotential, 1,
626 >                              MPI::REALTYPE, MPI::SUM);
627 > #endif
628 >
629      Snapshot* curSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
630 <    curSnapshot->statData[Stats::SHORT_RANGE_POTENTIAL] = shortRangePotential;
631 <    curSnapshot->statData[Stats::BOND_POTENTIAL] = bondPotential;
632 <    curSnapshot->statData[Stats::BEND_POTENTIAL] = bendPotential;
633 <    curSnapshot->statData[Stats::DIHEDRAL_POTENTIAL] = torsionPotential;
634 <    curSnapshot->statData[Stats::INVERSION_POTENTIAL] = inversionPotential;    
630 >
631 >    curSnapshot->setBondPotential(bondPotential);
632 >    curSnapshot->setBendPotential(bendPotential);
633 >    curSnapshot->setTorsionPotential(torsionPotential);
634 >    curSnapshot->setInversionPotential(inversionPotential);
635 >    
636 >    // RealType shortRangePotential = bondPotential + bendPotential +
637 >    //   torsionPotential +  inversionPotential;    
638 >
639 >    // curSnapshot->setShortRangePotential(shortRangePotential);
640    }
641    
642    void ForceManager::longRangeInteractions() {
643  
644 +
645      Snapshot* curSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
646      DataStorage* config = &(curSnapshot->atomData);
647      DataStorage* cgConfig = &(curSnapshot->cgData);
# Line 615 | Line 665 | namespace OpenMD {
665        // center of mass of the group is the same as position of the atom  
666        // if cutoff group does not exist
667        cgConfig->position = config->position;
668 +      cgConfig->velocity = config->velocity;
669      }
670  
671      fDecomp_->zeroWorkArrays();
672      fDecomp_->distributeData();
673      
674      int cg1, cg2, atom1, atom2, topoDist;
675 <    Vector3d d_grp, dag, d;
675 >    Vector3d d_grp, dag, d, gvel2, vel2;
676      RealType rgrpsq, rgrp, r2, r;
677      RealType electroMult, vdwMult;
678      RealType vij;
# Line 634 | Line 685 | namespace OpenMD {
685      InteractionData idat;
686      SelfData sdat;
687      RealType mf;
637    RealType lrPot;
688      RealType vpair;
689 +    RealType dVdFQ1(0.0);
690 +    RealType dVdFQ2(0.0);
691      potVec longRangePotential(0.0);
692      potVec workPot(0.0);
693 +    potVec exPot(0.0);
694 +    Vector3d eField1(0.0);
695 +    Vector3d eField2(0.0);
696 +    vector<int>::iterator ia, jb;
697  
698      int loopStart, loopEnd;
699  
700      idat.vdwMult = &vdwMult;
701      idat.electroMult = &electroMult;
702      idat.pot = &workPot;
703 +    idat.excludedPot = &exPot;
704      sdat.pot = fDecomp_->getEmbeddingPotential();
705 +    sdat.excludedPot = fDecomp_->getExcludedSelfPotential();
706      idat.vpair = &vpair;
707 +    idat.dVdFQ1 = &dVdFQ1;
708 +    idat.dVdFQ2 = &dVdFQ2;
709 +    idat.eField1 = &eField1;
710 +    idat.eField2 = &eField2;  
711      idat.f1 = &f1;
712      idat.sw = &sw;
713      idat.shiftedPot = (cutoffMethod_ == SHIFTED_POTENTIAL) ? true : false;
714      idat.shiftedForce = (cutoffMethod_ == SHIFTED_FORCE) ? true : false;
715 +    idat.doParticlePot = doParticlePot_;
716 +    idat.doElectricField = doElectricField_;
717 +    sdat.doParticlePot = doParticlePot_;
718      
719      loopEnd = PAIR_LOOP;
720      if (info_->requiresPrepair() ) {
# Line 657 | Line 722 | namespace OpenMD {
722      } else {
723        loopStart = PAIR_LOOP;
724      }
660  
725      for (int iLoop = loopStart; iLoop <= loopEnd; iLoop++) {
726      
727        if (iLoop == loopStart) {
# Line 685 | Line 749 | namespace OpenMD {
749            if (iLoop == PAIR_LOOP) {
750              vij = 0.0;
751              fij = V3Zero;
752 +            eField1 = V3Zero;
753 +            eField2 = V3Zero;
754            }
755            
756            in_switching_region = switcher_->getSwitch(rgrpsq, sw, dswdr,
757                                                       rgrp);
758 <          
758 >
759            atomListRow = fDecomp_->getAtomsInGroupRow(cg1);
760            atomListColumn = fDecomp_->getAtomsInGroupColumn(cg2);
761  
762 <          for (vector<int>::iterator ia = atomListRow.begin();
762 >          if (doHeatFlux_)
763 >            gvel2 = fDecomp_->getGroupVelocityColumn(cg2);
764 >
765 >          for (ia = atomListRow.begin();
766                 ia != atomListRow.end(); ++ia) {            
767              atom1 = (*ia);
768 <            
769 <            for (vector<int>::iterator jb = atomListColumn.begin();
768 >
769 >            for (jb = atomListColumn.begin();
770                   jb != atomListColumn.end(); ++jb) {              
771                atom2 = (*jb);
772  
773 <              if (!fDecomp_->skipAtomPair(atom1, atom2)) {
773 >              if (!fDecomp_->skipAtomPair(atom1, atom2, cg1, cg2)) {
774 >
775                  vpair = 0.0;
776                  workPot = 0.0;
777 +                exPot = 0.0;
778                  f1 = V3Zero;
779 +                dVdFQ1 = 0.0;
780 +                dVdFQ2 = 0.0;
781  
782                  fDecomp_->fillInteractionData(idat, atom1, atom2);
783 <                
783 >
784                  topoDist = fDecomp_->getTopologicalDistance(atom1, atom2);
785                  vdwMult = vdwScale_[topoDist];
786                  electroMult = electrostaticScale_[topoDist];
# Line 715 | Line 788 | namespace OpenMD {
788                  if (atomListRow.size() == 1 && atomListColumn.size() == 1) {
789                    idat.d = &d_grp;
790                    idat.r2 = &rgrpsq;
791 +                  if (doHeatFlux_)
792 +                    vel2 = gvel2;
793                  } else {
794                    d = fDecomp_->getInteratomicVector(atom1, atom2);
795                    curSnapshot->wrapVector( d );
796                    r2 = d.lengthSquare();
797                    idat.d = &d;
798                    idat.r2 = &r2;
799 +                  if (doHeatFlux_)
800 +                    vel2 = fDecomp_->getAtomVelocityColumn(atom2);
801                  }
802                
803                  r = sqrt( *(idat.r2) );
# Line 733 | Line 810 | namespace OpenMD {
810                    fDecomp_->unpackInteractionData(idat, atom1, atom2);
811                    vij += vpair;
812                    fij += f1;
813 <                  tau -= outProduct( *(idat.d), f1);
813 >                  stressTensor -= outProduct( *(idat.d), f1);
814 >                  if (doHeatFlux_)
815 >                    fDecomp_->addToHeatFlux(*(idat.d) * dot(f1, vel2));
816                  }
817                }
818              }
# Line 746 | Line 825 | namespace OpenMD {
825                fij += fg;
826  
827                if (atomListRow.size() == 1 && atomListColumn.size() == 1) {
828 <                tau -= outProduct( *(idat.d), fg);
828 >                if (!fDecomp_->skipAtomPair(atomListRow[0],
829 >                                            atomListColumn[0],
830 >                                            cg1, cg2)) {
831 >                  stressTensor -= outProduct( *(idat.d), fg);
832 >                  if (doHeatFlux_)
833 >                    fDecomp_->addToHeatFlux(*(idat.d) * dot(fg, vel2));
834 >                }                
835                }
836            
837 <              for (vector<int>::iterator ia = atomListRow.begin();
837 >              for (ia = atomListRow.begin();
838                     ia != atomListRow.end(); ++ia) {            
839                  atom1 = (*ia);                
840                  mf = fDecomp_->getMassFactorRow(atom1);
# Line 762 | Line 847 | namespace OpenMD {
847                      // find the distance between the atom
848                      // and the center of the cutoff group:
849                      dag = fDecomp_->getAtomToGroupVectorRow(atom1, cg1);
850 <                    tau -= outProduct(dag, fg);
850 >                    stressTensor -= outProduct(dag, fg);
851 >                    if (doHeatFlux_)
852 >                      fDecomp_->addToHeatFlux( dag * dot(fg, vel2));
853                    }
854                  }
855                }
856 <              for (vector<int>::iterator jb = atomListColumn.begin();
856 >              for (jb = atomListColumn.begin();
857                     jb != atomListColumn.end(); ++jb) {              
858                  atom2 = (*jb);
859                  mf = fDecomp_->getMassFactorColumn(atom2);
# Line 780 | Line 867 | namespace OpenMD {
867                      // find the distance between the atom
868                      // and the center of the cutoff group:
869                      dag = fDecomp_->getAtomToGroupVectorColumn(atom2, cg2);
870 <                    tau -= outProduct(dag, fg);
870 >                    stressTensor -= outProduct(dag, fg);
871 >                    if (doHeatFlux_)
872 >                      fDecomp_->addToHeatFlux( dag * dot(fg, vel2));
873                    }
874                  }
875                }
876              }
877              //if (!info_->usesAtomicVirial()) {
878 <            //  tau -= outProduct(d_grp, fij);
878 >            //  stressTensor -= outProduct(d_grp, fij);
879 >            //  if (doHeatFlux_)
880 >            //     fDecomp_->addToHeatFlux( d_grp * dot(fij, vel2));
881              //}
882            }
883          }
# Line 797 | Line 888 | namespace OpenMD {
888  
889            fDecomp_->collectIntermediateData();
890  
891 <          for (int atom1 = 0; atom1 < info_->getNAtoms(); atom1++) {
891 >          for (unsigned int atom1 = 0; atom1 < info_->getNAtoms(); atom1++) {
892              fDecomp_->fillSelfData(sdat, atom1);
893              interactionMan_->doPreForce(sdat);
894            }
# Line 808 | Line 899 | namespace OpenMD {
899        }
900      }
901      
902 +    // collects pairwise information
903      fDecomp_->collectData();
904          
905      if (info_->requiresSelfCorrection()) {
906 <
815 <      for (int atom1 = 0; atom1 < info_->getNAtoms(); atom1++) {          
906 >      for (unsigned int atom1 = 0; atom1 < info_->getNAtoms(); atom1++) {
907          fDecomp_->fillSelfData(sdat, atom1);
908          interactionMan_->doSelfCorrection(sdat);
909        }
819
910      }
911  
912 +    // collects single-atom information
913 +    fDecomp_->collectSelfData();
914 +
915      longRangePotential = *(fDecomp_->getEmbeddingPotential()) +
916        *(fDecomp_->getPairwisePotential());
917  
918 <    lrPot = longRangePotential.sum();
918 >    curSnapshot->setLongRangePotential(longRangePotential);
919 >    
920 >    curSnapshot->setExcludedPotentials(*(fDecomp_->getExcludedSelfPotential()) +
921 >                                         *(fDecomp_->getExcludedPotential()));
922  
827    //store the tau and long range potential    
828    curSnapshot->statData[Stats::LONG_RANGE_POTENTIAL] = lrPot;
829    curSnapshot->statData[Stats::VANDERWAALS_POTENTIAL] = longRangePotential[VANDERWAALS_FAMILY];
830    curSnapshot->statData[Stats::ELECTROSTATIC_POTENTIAL] = longRangePotential[ELECTROSTATIC_FAMILY];
923    }
924  
925    
926    void ForceManager::postCalculation() {
927 +
928 +    vector<Perturbation*>::iterator pi;
929 +    for (pi = perturbations_.begin(); pi != perturbations_.end(); ++pi) {
930 +      (*pi)->applyPerturbation();
931 +    }
932 +
933      SimInfo::MoleculeIterator mi;
934      Molecule* mol;
935      Molecule::RigidBodyIterator rbIter;
936      RigidBody* rb;
937      Snapshot* curSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
938 <    
938 >  
939      // collect the atomic forces onto rigid bodies
940      
941      for (mol = info_->beginMolecule(mi); mol != NULL;
# Line 845 | Line 943 | namespace OpenMD {
943        for (rb = mol->beginRigidBody(rbIter); rb != NULL;
944             rb = mol->nextRigidBody(rbIter)) {
945          Mat3x3d rbTau = rb->calcForcesAndTorquesAndVirial();
946 <        tau += rbTau;
946 >        stressTensor += rbTau;
947        }
948      }
949      
950   #ifdef IS_MPI
951 <    Mat3x3d tmpTau(tau);
952 <    MPI_Allreduce(tmpTau.getArrayPointer(), tau.getArrayPointer(),
855 <                  9, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
951 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, stressTensor.getArrayPointer(), 9,
952 >                              MPI::REALTYPE, MPI::SUM);
953   #endif
954 <    curSnapshot->setTau(tau);
955 <  }
954 >    curSnapshot->setStressTensor(stressTensor);
955 >    
956 >    if (info_->getSimParams()->getUseLongRangeCorrections()) {
957 >      /*
958 >      RealType vol = curSnapshot->getVolume();
959 >      RealType Elrc(0.0);
960 >      RealType Wlrc(0.0);
961  
962 < } //end namespace OpenMD
962 >      set<AtomType*>::iterator i;
963 >      set<AtomType*>::iterator j;
964 >    
965 >      RealType n_i, n_j;
966 >      RealType rho_i, rho_j;
967 >      pair<RealType, RealType> LRI;
968 >      
969 >      for (i = atomTypes_.begin(); i != atomTypes_.end(); ++i) {
970 >        n_i = RealType(info_->getGlobalCountOfType(*i));
971 >        rho_i = n_i /  vol;
972 >        for (j = atomTypes_.begin(); j != atomTypes_.end(); ++j) {
973 >          n_j = RealType(info_->getGlobalCountOfType(*j));
974 >          rho_j = n_j / vol;
975 >          
976 >          LRI = interactionMan_->getLongRangeIntegrals( (*i), (*j) );
977 >
978 >          Elrc += n_i   * rho_j * LRI.first;
979 >          Wlrc -= rho_i * rho_j * LRI.second;
980 >        }
981 >      }
982 >      Elrc *= 2.0 * NumericConstant::PI;
983 >      Wlrc *= 2.0 * NumericConstant::PI;
984 >
985 >      RealType lrp = curSnapshot->getLongRangePotential();
986 >      curSnapshot->setLongRangePotential(lrp + Elrc);
987 >      stressTensor += Wlrc * SquareMatrix3<RealType>::identity();
988 >      curSnapshot->setStressTensor(stressTensor);
989 >      */
990 >    
991 >    }
992 >  }
993 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines