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

Comparing branches/development/src/brains/SimInfo.cpp (file contents):
Revision 1583 by gezelter, Thu Jun 16 22:00:08 2011 UTC vs.
Revision 1764 by gezelter, Tue Jul 3 18:32:27 2012 UTC

# Line 36 | Line 36
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).          
39 < * [4]  Vardeman & Gezelter, in progress (2009).                        
39 > * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010).
40 > * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41   */
42  
43   /**
# Line 58 | Line 59
59   #include "utils/simError.h"
60   #include "selection/SelectionManager.hpp"
61   #include "io/ForceFieldOptions.hpp"
62 < #include "UseTheForce/ForceField.hpp"
62 > #include "brains/ForceField.hpp"
63   #include "nonbonded/SwitchingFunction.hpp"
64 + #ifdef IS_MPI
65 + #include <mpi.h>
66 + #endif
67  
68   using namespace std;
69   namespace OpenMD {
# Line 68 | Line 72 | namespace OpenMD {
72      forceField_(ff), simParams_(simParams),
73      ndf_(0), fdf_local(0), ndfRaw_(0), ndfTrans_(0), nZconstraint_(0),
74      nGlobalMols_(0), nGlobalAtoms_(0), nGlobalCutoffGroups_(0),
75 <    nGlobalIntegrableObjects_(0), nGlobalRigidBodies_(0),
75 >    nGlobalIntegrableObjects_(0), nGlobalRigidBodies_(0), nGlobalFluctuatingCharges_(0),
76      nAtoms_(0), nBonds_(0),  nBends_(0), nTorsions_(0), nInversions_(0),
77      nRigidBodies_(0), nIntegrableObjects_(0), nCutoffGroups_(0),
78 <    nConstraints_(0), sman_(NULL), topologyDone_(false),
78 >    nConstraints_(0), nFluctuatingCharges_(0), sman_(NULL), topologyDone_(false),
79      calcBoxDipole_(false), useAtomicVirial_(true) {    
80      
81      MoleculeStamp* molStamp;
# Line 221 | Line 225 | namespace OpenMD {
225  
226  
227    void SimInfo::calcNdf() {
228 <    int ndf_local;
228 >    int ndf_local, nfq_local;
229      MoleculeIterator i;
230      vector<StuntDouble*>::iterator j;
231 +    vector<Atom*>::iterator k;
232 +
233      Molecule* mol;
234      StuntDouble* integrableObject;
235 +    Atom* atom;
236  
237      ndf_local = 0;
238 +    nfq_local = 0;
239      
240      for (mol = beginMolecule(i); mol != NULL; mol = nextMolecule(i)) {
241        for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
# Line 242 | Line 250 | namespace OpenMD {
250              ndf_local += 3;
251            }
252          }
253 <            
253 >      }
254 >      for (atom = mol->beginFluctuatingCharge(k); atom != NULL;
255 >           atom = mol->nextFluctuatingCharge(k)) {
256 >        if (atom->isFluctuatingCharge()) {
257 >          nfq_local++;
258 >        }
259        }
260      }
261      
262 +    ndfLocal_ = ndf_local;
263 +
264      // n_constraints is local, so subtract them on each processor
265      ndf_local -= nConstraints_;
266  
267   #ifdef IS_MPI
268      MPI_Allreduce(&ndf_local,&ndf_,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD);
269 +    MPI_Allreduce(&nfq_local,&nGlobalFluctuatingCharges_,1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
270   #else
271      ndf_ = ndf_local;
272 +    nGlobalFluctuatingCharges_ = nfq_local;
273   #endif
274  
275      // nZconstraints_ is global, as are the 3 COM translations for the
# Line 694 | Line 711 | namespace OpenMD {
711      Atom* atom;
712      set<AtomType*> atomTypes;
713      
714 <    for(mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) {      
715 <      for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
714 >    for(mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) {
715 >      for(atom = mol->beginAtom(ai); atom != NULL;
716 >          atom = mol->nextAtom(ai)) {
717          atomTypes.insert(atom->getAtomType());
718        }      
719      }    
720 <
720 >    
721   #ifdef IS_MPI
722  
723      // loop over the found atom types on this processor, and add their
724      // numerical idents to a vector:
725 <
725 >    
726      vector<int> foundTypes;
727      set<AtomType*>::iterator i;
728      for (i = atomTypes.begin(); i != atomTypes.end(); ++i)
# Line 713 | Line 731 | namespace OpenMD {
731      // count_local holds the number of found types on this processor
732      int count_local = foundTypes.size();
733  
734 <    // count holds the total number of found types on all processors
717 <    // (some will be redundant with the ones found locally):
718 <    int count;
719 <    MPI::COMM_WORLD.Allreduce(&count_local, &count, 1, MPI::INT, MPI::SUM);
734 >    int nproc = MPI::COMM_WORLD.Get_size();
735  
736 <    // create a vector to hold the globally found types, and resize it:
737 <    vector<int> ftGlobal;
738 <    ftGlobal.resize(count);
739 <    vector<int> counts;
736 >    // we need arrays to hold the counts and displacement vectors for
737 >    // all processors
738 >    vector<int> counts(nproc, 0);
739 >    vector<int> disps(nproc, 0);
740  
741 <    int nproc = MPI::COMM_WORLD.Get_size();
742 <    counts.resize(nproc);
743 <    vector<int> disps;
744 <    disps.resize(nproc);
741 >    // fill the counts array
742 >    MPI::COMM_WORLD.Allgather(&count_local, 1, MPI::INT, &counts[0],
743 >                              1, MPI::INT);
744 >  
745 >    // use the processor counts to compute the displacement array
746 >    disps[0] = 0;    
747 >    int totalCount = counts[0];
748 >    for (int iproc = 1; iproc < nproc; iproc++) {
749 >      disps[iproc] = disps[iproc-1] + counts[iproc-1];
750 >      totalCount += counts[iproc];
751 >    }
752  
753 <    // now spray out the foundTypes to all the other processors:
753 >    // we need a (possibly redundant) set of all found types:
754 >    vector<int> ftGlobal(totalCount);
755      
756 +    // now spray out the foundTypes to all the other processors:    
757      MPI::COMM_WORLD.Allgatherv(&foundTypes[0], count_local, MPI::INT,
758 <                               &ftGlobal[0], &counts[0], &disps[0], MPI::INT);
758 >                               &ftGlobal[0], &counts[0], &disps[0],
759 >                               MPI::INT);
760  
761 +    vector<int>::iterator j;
762 +
763      // foundIdents is a stl set, so inserting an already found ident
764      // will have no effect.
765      set<int> foundIdents;
766 <    vector<int>::iterator j;
766 >
767      for (j = ftGlobal.begin(); j != ftGlobal.end(); ++j)
768        foundIdents.insert((*j));
769      
770      // now iterate over the foundIdents and get the actual atom types
771      // that correspond to these:
772      set<int>::iterator it;
773 <    for (it = foundIdents.begin(); it != foundIdents.end(); ++it)
773 >    for (it = foundIdents.begin(); it != foundIdents.end(); ++it)
774        atomTypes.insert( forceField_->getAtomType((*it)) );
775  
776   #endif
777 <    
777 >
778      return atomTypes;        
779    }
780  
# Line 766 | Line 793 | namespace OpenMD {
793      int usesElectrostatic = 0;
794      int usesMetallic = 0;
795      int usesDirectional = 0;
796 +    int usesFluctuatingCharges =  0;
797      //loop over all of the atom types
798      for (i = atomTypes.begin(); i != atomTypes.end(); ++i) {
799        usesElectrostatic |= (*i)->isElectrostatic();
800        usesMetallic |= (*i)->isMetal();
801        usesDirectional |= (*i)->isDirectional();
802 +      usesFluctuatingCharges |= (*i)->isFluctuatingCharge();
803      }
804 <    
804 >
805   #ifdef IS_MPI    
806      int temp;
807      temp = usesDirectional;
# Line 783 | Line 812 | namespace OpenMD {
812      
813      temp = usesElectrostatic;
814      MPI_Allreduce(&temp, &usesElectrostaticAtoms_, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);
815 +
816 +    temp = usesFluctuatingCharges;
817 +    MPI_Allreduce(&temp, &usesFluctuatingCharges_, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);
818   #else
819  
820      usesDirectionalAtoms_ = usesDirectional;
821      usesMetallicAtoms_ = usesMetallic;
822      usesElectrostaticAtoms_ = usesElectrostatic;
823 +    usesFluctuatingCharges_ = usesFluctuatingCharges;
824  
825   #endif
826      
# Line 956 | Line 989 | namespace OpenMD {
989      
990    }
991  
959  Vector3d SimInfo::getComVel(){
960    SimInfo::MoleculeIterator i;
961    Molecule* mol;
992  
963    Vector3d comVel(0.0);
964    RealType totalMass = 0.0;
965    
966
967    for (mol = beginMolecule(i); mol != NULL; mol = nextMolecule(i)) {
968      RealType mass = mol->getMass();
969      totalMass += mass;
970      comVel += mass * mol->getComVel();
971    }  
972
973 #ifdef IS_MPI
974    RealType tmpMass = totalMass;
975    Vector3d tmpComVel(comVel);    
976    MPI_Allreduce(&tmpMass,&totalMass,1,MPI_REALTYPE,MPI_SUM, MPI_COMM_WORLD);
977    MPI_Allreduce(tmpComVel.getArrayPointer(), comVel.getArrayPointer(),3,MPI_REALTYPE,MPI_SUM, MPI_COMM_WORLD);
978 #endif
979
980    comVel /= totalMass;
981
982    return comVel;
983  }
984
985  Vector3d SimInfo::getCom(){
986    SimInfo::MoleculeIterator i;
987    Molecule* mol;
988
989    Vector3d com(0.0);
990    RealType totalMass = 0.0;
991    
992    for (mol = beginMolecule(i); mol != NULL; mol = nextMolecule(i)) {
993      RealType mass = mol->getMass();
994      totalMass += mass;
995      com += mass * mol->getCom();
996    }  
997
998 #ifdef IS_MPI
999    RealType tmpMass = totalMass;
1000    Vector3d tmpCom(com);    
1001    MPI_Allreduce(&tmpMass,&totalMass,1,MPI_REALTYPE,MPI_SUM, MPI_COMM_WORLD);
1002    MPI_Allreduce(tmpCom.getArrayPointer(), com.getArrayPointer(),3,MPI_REALTYPE,MPI_SUM, MPI_COMM_WORLD);
1003 #endif
1004
1005    com /= totalMass;
1006
1007    return com;
1008
1009  }        
1010
993    ostream& operator <<(ostream& o, SimInfo& info) {
994  
995      return o;
996    }
997    
998 <  
1017 <   /*
1018 <   Returns center of mass and center of mass velocity in one function call.
1019 <   */
1020 <  
1021 <   void SimInfo::getComAll(Vector3d &com, Vector3d &comVel){
1022 <      SimInfo::MoleculeIterator i;
1023 <      Molecule* mol;
1024 <      
1025 <    
1026 <      RealType totalMass = 0.0;
1027 <    
1028 <
1029 <      for (mol = beginMolecule(i); mol != NULL; mol = nextMolecule(i)) {
1030 <         RealType mass = mol->getMass();
1031 <         totalMass += mass;
1032 <         com += mass * mol->getCom();
1033 <         comVel += mass * mol->getComVel();          
1034 <      }  
1035 <      
1036 < #ifdef IS_MPI
1037 <      RealType tmpMass = totalMass;
1038 <      Vector3d tmpCom(com);  
1039 <      Vector3d tmpComVel(comVel);
1040 <      MPI_Allreduce(&tmpMass,&totalMass,1,MPI_REALTYPE,MPI_SUM, MPI_COMM_WORLD);
1041 <      MPI_Allreduce(tmpCom.getArrayPointer(), com.getArrayPointer(),3,MPI_REALTYPE,MPI_SUM, MPI_COMM_WORLD);
1042 <      MPI_Allreduce(tmpComVel.getArrayPointer(), comVel.getArrayPointer(),3,MPI_REALTYPE,MPI_SUM, MPI_COMM_WORLD);
1043 < #endif
1044 <      
1045 <      com /= totalMass;
1046 <      comVel /= totalMass;
1047 <   }        
1048 <  
1049 <   /*
1050 <   Return intertia tensor for entire system and angular momentum Vector.
1051 <
1052 <
1053 <       [  Ixx -Ixy  -Ixz ]
1054 <    J =| -Iyx  Iyy  -Iyz |
1055 <       [ -Izx -Iyz   Izz ]
1056 <    */
1057 <
1058 <   void SimInfo::getInertiaTensor(Mat3x3d &inertiaTensor, Vector3d &angularMomentum){
1059 <      
1060 <
1061 <      RealType xx = 0.0;
1062 <      RealType yy = 0.0;
1063 <      RealType zz = 0.0;
1064 <      RealType xy = 0.0;
1065 <      RealType xz = 0.0;
1066 <      RealType yz = 0.0;
1067 <      Vector3d com(0.0);
1068 <      Vector3d comVel(0.0);
1069 <      
1070 <      getComAll(com, comVel);
1071 <      
1072 <      SimInfo::MoleculeIterator i;
1073 <      Molecule* mol;
1074 <      
1075 <      Vector3d thisq(0.0);
1076 <      Vector3d thisv(0.0);
1077 <
1078 <      RealType thisMass = 0.0;
1079 <    
1080 <      
1081 <      
1082 <  
1083 <      for (mol = beginMolecule(i); mol != NULL; mol = nextMolecule(i)) {
1084 <        
1085 <         thisq = mol->getCom()-com;
1086 <         thisv = mol->getComVel()-comVel;
1087 <         thisMass = mol->getMass();
1088 <         // Compute moment of intertia coefficients.
1089 <         xx += thisq[0]*thisq[0]*thisMass;
1090 <         yy += thisq[1]*thisq[1]*thisMass;
1091 <         zz += thisq[2]*thisq[2]*thisMass;
1092 <        
1093 <         // compute products of intertia
1094 <         xy += thisq[0]*thisq[1]*thisMass;
1095 <         xz += thisq[0]*thisq[2]*thisMass;
1096 <         yz += thisq[1]*thisq[2]*thisMass;
1097 <            
1098 <         angularMomentum += cross( thisq, thisv ) * thisMass;
1099 <            
1100 <      }  
1101 <      
1102 <      
1103 <      inertiaTensor(0,0) = yy + zz;
1104 <      inertiaTensor(0,1) = -xy;
1105 <      inertiaTensor(0,2) = -xz;
1106 <      inertiaTensor(1,0) = -xy;
1107 <      inertiaTensor(1,1) = xx + zz;
1108 <      inertiaTensor(1,2) = -yz;
1109 <      inertiaTensor(2,0) = -xz;
1110 <      inertiaTensor(2,1) = -yz;
1111 <      inertiaTensor(2,2) = xx + yy;
1112 <      
1113 < #ifdef IS_MPI
1114 <      Mat3x3d tmpI(inertiaTensor);
1115 <      Vector3d tmpAngMom;
1116 <      MPI_Allreduce(tmpI.getArrayPointer(), inertiaTensor.getArrayPointer(),9,MPI_REALTYPE,MPI_SUM, MPI_COMM_WORLD);
1117 <      MPI_Allreduce(tmpAngMom.getArrayPointer(), angularMomentum.getArrayPointer(),3,MPI_REALTYPE,MPI_SUM, MPI_COMM_WORLD);
1118 < #endif
1119 <              
1120 <      return;
1121 <   }
1122 <
1123 <   //Returns the angular momentum of the system
1124 <   Vector3d SimInfo::getAngularMomentum(){
1125 <      
1126 <      Vector3d com(0.0);
1127 <      Vector3d comVel(0.0);
1128 <      Vector3d angularMomentum(0.0);
1129 <      
1130 <      getComAll(com,comVel);
1131 <      
1132 <      SimInfo::MoleculeIterator i;
1133 <      Molecule* mol;
1134 <      
1135 <      Vector3d thisr(0.0);
1136 <      Vector3d thisp(0.0);
1137 <      
1138 <      RealType thisMass;
1139 <      
1140 <      for (mol = beginMolecule(i); mol != NULL; mol = nextMolecule(i)) {        
1141 <        thisMass = mol->getMass();
1142 <        thisr = mol->getCom()-com;
1143 <        thisp = (mol->getComVel()-comVel)*thisMass;
1144 <        
1145 <        angularMomentum += cross( thisr, thisp );
1146 <        
1147 <      }  
1148 <      
1149 < #ifdef IS_MPI
1150 <      Vector3d tmpAngMom;
1151 <      MPI_Allreduce(tmpAngMom.getArrayPointer(), angularMomentum.getArrayPointer(),3,MPI_REALTYPE,MPI_SUM, MPI_COMM_WORLD);
1152 < #endif
1153 <      
1154 <      return angularMomentum;
1155 <   }
1156 <  
998 >  
999    StuntDouble* SimInfo::getIOIndexToIntegrableObject(int index) {
1000      return IOIndexToIntegrableObject.at(index);
1001    }
# Line 1161 | Line 1003 | namespace OpenMD {
1003    void SimInfo::setIOIndexToIntegrableObject(const vector<StuntDouble*>& v) {
1004      IOIndexToIntegrableObject= v;
1005    }
1164
1165  /* Returns the Volume of the simulation based on a ellipsoid with semi-axes
1166     based on the radius of gyration V=4/3*Pi*R_1*R_2*R_3
1167     where R_i are related to the principle inertia moments R_i = sqrt(C*I_i/N), this reduces to
1168     V = 4/3*Pi*(C/N)^3/2*sqrt(det(I)). See S.E. Baltazar et. al. Comp. Mat. Sci. 37 (2006) 526-536.
1169  */
1170  void SimInfo::getGyrationalVolume(RealType &volume){
1171    Mat3x3d intTensor;
1172    RealType det;
1173    Vector3d dummyAngMom;
1174    RealType sysconstants;
1175    RealType geomCnst;
1176
1177    geomCnst = 3.0/2.0;
1178    /* Get the inertial tensor and angular momentum for free*/
1179    getInertiaTensor(intTensor,dummyAngMom);
1180    
1181    det = intTensor.determinant();
1182    sysconstants = geomCnst/(RealType)nGlobalIntegrableObjects_;
1183    volume = 4.0/3.0*NumericConstant::PI*pow(sysconstants,3.0/2.0)*sqrt(det);
1184    return;
1185  }
1186
1187  void SimInfo::getGyrationalVolume(RealType &volume, RealType &detI){
1188    Mat3x3d intTensor;
1189    Vector3d dummyAngMom;
1190    RealType sysconstants;
1191    RealType geomCnst;
1192
1193    geomCnst = 3.0/2.0;
1194    /* Get the inertial tensor and angular momentum for free*/
1195    getInertiaTensor(intTensor,dummyAngMom);
1196    
1197    detI = intTensor.determinant();
1198    sysconstants = geomCnst/(RealType)nGlobalIntegrableObjects_;
1199    volume = 4.0/3.0*NumericConstant::PI*pow(sysconstants,3.0/2.0)*sqrt(detI);
1200    return;
1201  }
1006   /*
1007     void SimInfo::setStuntDoubleFromGlobalIndex(vector<StuntDouble*> v) {
1008        assert( v.size() == nAtoms_ + nRigidBodies_);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines