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 1535 by gezelter, Fri Dec 31 18:31:56 2010 UTC vs.
Revision 1627 by gezelter, Tue Sep 13 22:05:04 2011 UTC

# Line 54 | Line 54
54   #include "math/Vector3.hpp"
55   #include "primitives/Molecule.hpp"
56   #include "primitives/StuntDouble.hpp"
57 #include "UseTheForce/DarkSide/neighborLists_interface.h"
57   #include "utils/MemoryUtils.hpp"
58   #include "utils/simError.h"
59   #include "selection/SelectionManager.hpp"
60   #include "io/ForceFieldOptions.hpp"
61   #include "UseTheForce/ForceField.hpp"
62   #include "nonbonded/SwitchingFunction.hpp"
64
63   #ifdef IS_MPI
64 < #include "UseTheForce/mpiComponentPlan.h"
65 < #include "UseTheForce/DarkSide/simParallel_interface.h"
68 < #endif
64 > #include <mpi.h>
65 > #endif
66  
67   using namespace std;
68   namespace OpenMD {
# Line 77 | Line 74 | namespace OpenMD {
74      nGlobalIntegrableObjects_(0), nGlobalRigidBodies_(0),
75      nAtoms_(0), nBonds_(0),  nBends_(0), nTorsions_(0), nInversions_(0),
76      nRigidBodies_(0), nIntegrableObjects_(0), nCutoffGroups_(0),
77 <    nConstraints_(0), sman_(NULL), fortranInitialized_(false),
77 >    nConstraints_(0), sman_(NULL), topologyDone_(false),
78      calcBoxDipole_(false), useAtomicVirial_(true) {    
79      
80      MoleculeStamp* molStamp;
# Line 131 | Line 128 | namespace OpenMD {
128      //equal to the total number of atoms minus number of atoms belong to
129      //cutoff group defined in meta-data file plus the number of cutoff
130      //groups defined in meta-data file
131 +
132      nGlobalCutoffGroups_ = nGlobalAtoms_ - nCutoffAtoms + nGroups;
133      
134      //every free atom (atom does not belong to rigid bodies) is an
# Line 274 | Line 272 | namespace OpenMD {
272   #endif
273      return fdf_;
274    }
275 +  
276 +  unsigned int SimInfo::getNLocalCutoffGroups(){
277 +    int nLocalCutoffAtoms = 0;
278 +    Molecule* mol;
279 +    MoleculeIterator mi;
280 +    CutoffGroup* cg;
281 +    Molecule::CutoffGroupIterator ci;
282      
283 +    for (mol = beginMolecule(mi); mol != NULL; mol  = nextMolecule(mi)) {
284 +      
285 +      for (cg = mol->beginCutoffGroup(ci); cg != NULL;
286 +           cg = mol->nextCutoffGroup(ci)) {
287 +        nLocalCutoffAtoms += cg->getNumAtom();
288 +        
289 +      }        
290 +    }
291 +    
292 +    return nAtoms_ - nLocalCutoffAtoms + nCutoffGroups_;
293 +  }
294 +    
295    void SimInfo::calcNdfRaw() {
296      int ndfRaw_local;
297  
# Line 680 | Line 697 | namespace OpenMD {
697      Atom* atom;
698      set<AtomType*> atomTypes;
699      
700 <    for(mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) {      
701 <      for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
700 >    for(mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) {
701 >      for(atom = mol->beginAtom(ai); atom != NULL;
702 >          atom = mol->nextAtom(ai)) {
703          atomTypes.insert(atom->getAtomType());
704        }      
705      }    
706 <
706 >    
707   #ifdef IS_MPI
708  
709      // loop over the found atom types on this processor, and add their
710      // numerical idents to a vector:
711 <
711 >    
712      vector<int> foundTypes;
713      set<AtomType*>::iterator i;
714      for (i = atomTypes.begin(); i != atomTypes.end(); ++i)
# Line 699 | Line 717 | namespace OpenMD {
717      // count_local holds the number of found types on this processor
718      int count_local = foundTypes.size();
719  
720 <    // count holds the total number of found types on all processors
703 <    // (some will be redundant with the ones found locally):
704 <    int count;
705 <    MPI::COMM_WORLD.Allreduce(&count_local, &count, 1, MPI::INT, MPI::SUM);
720 >    int nproc = MPI::COMM_WORLD.Get_size();
721  
722 <    // create a vector to hold the globally found types, and resize it:
723 <    vector<int> ftGlobal;
724 <    ftGlobal.resize(count);
725 <    vector<int> counts;
722 >    // we need arrays to hold the counts and displacement vectors for
723 >    // all processors
724 >    vector<int> counts(nproc, 0);
725 >    vector<int> disps(nproc, 0);
726  
727 <    int nproc = MPI::COMM_WORLD.Get_size();
728 <    counts.resize(nproc);
729 <    vector<int> disps;
730 <    disps.resize(nproc);
727 >    // fill the counts array
728 >    MPI::COMM_WORLD.Allgather(&count_local, 1, MPI::INT, &counts[0],
729 >                              1, MPI::INT);
730 >  
731 >    // use the processor counts to compute the displacement array
732 >    disps[0] = 0;    
733 >    int totalCount = counts[0];
734 >    for (int iproc = 1; iproc < nproc; iproc++) {
735 >      disps[iproc] = disps[iproc-1] + counts[iproc-1];
736 >      totalCount += counts[iproc];
737 >    }
738  
739 <    // now spray out the foundTypes to all the other processors:
739 >    // we need a (possibly redundant) set of all found types:
740 >    vector<int> ftGlobal(totalCount);
741      
742 +    // now spray out the foundTypes to all the other processors:    
743      MPI::COMM_WORLD.Allgatherv(&foundTypes[0], count_local, MPI::INT,
744 <                               &ftGlobal[0], &counts[0], &disps[0], MPI::INT);
744 >                               &ftGlobal[0], &counts[0], &disps[0],
745 >                               MPI::INT);
746 >
747 >    vector<int>::iterator j;
748  
749      // foundIdents is a stl set, so inserting an already found ident
750      // will have no effect.
751      set<int> foundIdents;
752 <    vector<int>::iterator j;
752 >
753      for (j = ftGlobal.begin(); j != ftGlobal.end(); ++j)
754        foundIdents.insert((*j));
755      
756      // now iterate over the foundIdents and get the actual atom types
757      // that correspond to these:
758      set<int>::iterator it;
759 <    for (it = foundIdents.begin(); it != foundIdents.end(); ++it)
759 >    for (it = foundIdents.begin(); it != foundIdents.end(); ++it)
760        atomTypes.insert( forceField_->getAtomType((*it)) );
761  
762   #endif
763 <    
763 >
764      return atomTypes;        
765    }
766  
# Line 745 | Line 772 | namespace OpenMD {
772        if ( simParams_->getAccumulateBoxDipole() ) {
773          calcBoxDipole_ = true;      
774        }
775 <
775 >    
776      set<AtomType*>::iterator i;
777      set<AtomType*> atomTypes;
778      atomTypes = getSimulatedAtomTypes();    
# Line 758 | Line 785 | namespace OpenMD {
785        usesMetallic |= (*i)->isMetal();
786        usesDirectional |= (*i)->isDirectional();
787      }
788 <
788 >    
789   #ifdef IS_MPI    
790      int temp;
791      temp = usesDirectional;
792      MPI_Allreduce(&temp, &usesDirectionalAtoms_, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);    
793 <
793 >    
794      temp = usesMetallic;
795      MPI_Allreduce(&temp, &usesMetallicAtoms_, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);    
796 <
796 >    
797      temp = usesElectrostatic;
798      MPI_Allreduce(&temp, &usesElectrostaticAtoms_, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);
799 + #else
800 +
801 +    usesDirectionalAtoms_ = usesDirectional;
802 +    usesMetallicAtoms_ = usesMetallic;
803 +    usesElectrostaticAtoms_ = usesElectrostatic;
804 +
805   #endif
806 <    fInfo_.SIM_uses_PBC = usesPeriodicBoundaries_;    
807 <    fInfo_.SIM_uses_DirectionalAtoms = usesDirectionalAtoms_;
808 <    fInfo_.SIM_uses_MetallicAtoms = usesMetallicAtoms_;
809 <    fInfo_.SIM_requires_SkipCorrection = usesElectrostaticAtoms_;
777 <    fInfo_.SIM_requires_SelfCorrection = usesElectrostaticAtoms_;
778 <    fInfo_.SIM_uses_AtomicVirial = usesAtomicVirial_;
806 >    
807 >    requiresPrepair_ = usesMetallicAtoms_ ? true : false;
808 >    requiresSkipCorrection_ = usesElectrostaticAtoms_ ? true : false;
809 >    requiresSelfCorrection_ = usesElectrostaticAtoms_ ? true : false;    
810    }
811  
812 <  void SimInfo::setupFortran() {
813 <    int isError;
814 <    int nExclude, nOneTwo, nOneThree, nOneFour;
815 <    vector<int> fortranGlobalGroupMembership;
812 >
813 >  vector<int> SimInfo::getGlobalAtomIndices() {
814 >    SimInfo::MoleculeIterator mi;
815 >    Molecule* mol;
816 >    Molecule::AtomIterator ai;
817 >    Atom* atom;
818 >
819 >    vector<int> GlobalAtomIndices(getNAtoms(), 0);
820      
821 <    isError = 0;
821 >    for (mol = beginMolecule(mi); mol != NULL; mol  = nextMolecule(mi)) {
822 >      
823 >      for (atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
824 >        GlobalAtomIndices[atom->getLocalIndex()] = atom->getGlobalIndex();
825 >      }
826 >    }
827 >    return GlobalAtomIndices;
828 >  }
829  
830 <    //globalGroupMembership_ is filled by SimCreator    
831 <    for (int i = 0; i < nGlobalAtoms_; i++) {
832 <      fortranGlobalGroupMembership.push_back(globalGroupMembership_[i] + 1);
830 >
831 >  vector<int> SimInfo::getGlobalGroupIndices() {
832 >    SimInfo::MoleculeIterator mi;
833 >    Molecule* mol;
834 >    Molecule::CutoffGroupIterator ci;
835 >    CutoffGroup* cg;
836 >
837 >    vector<int> GlobalGroupIndices;
838 >    
839 >    for (mol = beginMolecule(mi); mol != NULL; mol  = nextMolecule(mi)) {
840 >      
841 >      //local index of cutoff group is trivial, it only depends on the
842 >      //order of travesing
843 >      for (cg = mol->beginCutoffGroup(ci); cg != NULL;
844 >           cg = mol->nextCutoffGroup(ci)) {
845 >        GlobalGroupIndices.push_back(cg->getGlobalIndex());
846 >      }        
847      }
848 +    return GlobalGroupIndices;
849 +  }
850  
851 +
852 +  void SimInfo::prepareTopology() {
853 +    int nExclude, nOneTwo, nOneThree, nOneFour;
854 +
855      //calculate mass ratio of cutoff group
794    vector<RealType> mfact;
856      SimInfo::MoleculeIterator mi;
857      Molecule* mol;
858      Molecule::CutoffGroupIterator ci;
# Line 800 | Line 861 | namespace OpenMD {
861      Atom* atom;
862      RealType totalMass;
863  
864 <    //to avoid memory reallocation, reserve enough space for mfact
865 <    mfact.reserve(getNCutoffGroups());
864 >    /**
865 >     * The mass factor is the relative mass of an atom to the total
866 >     * mass of the cutoff group it belongs to.  By default, all atoms
867 >     * are their own cutoff groups, and therefore have mass factors of
868 >     * 1.  We need some special handling for massless atoms, which
869 >     * will be treated as carrying the entire mass of the cutoff
870 >     * group.
871 >     */
872 >    massFactors_.clear();
873 >    massFactors_.resize(getNAtoms(), 1.0);
874      
875      for(mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) {        
876 <      for (cg = mol->beginCutoffGroup(ci); cg != NULL; cg = mol->nextCutoffGroup(ci)) {
876 >      for (cg = mol->beginCutoffGroup(ci); cg != NULL;
877 >           cg = mol->nextCutoffGroup(ci)) {
878  
879          totalMass = cg->getMass();
880          for(atom = cg->beginAtom(ai); atom != NULL; atom = cg->nextAtom(ai)) {
881            // Check for massless groups - set mfact to 1 if true
882 <          if (totalMass != 0)
883 <            mfact.push_back(atom->getMass()/totalMass);
882 >          if (totalMass != 0)
883 >            massFactors_[atom->getLocalIndex()] = atom->getMass()/totalMass;
884            else
885 <            mfact.push_back( 1.0 );
885 >            massFactors_[atom->getLocalIndex()] = 1.0;
886          }
887        }      
888      }
889  
890 <    //fill ident array of local atoms (it is actually ident of
821 <    //AtomType, it is so confusing !!!)
822 <    vector<int> identArray;
890 >    // Build the identArray_
891  
892 <    //to avoid memory reallocation, reserve enough space identArray
893 <    identArray.reserve(getNAtoms());
826 <    
892 >    identArray_.clear();
893 >    identArray_.reserve(getNAtoms());    
894      for(mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) {        
895        for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
896 <        identArray.push_back(atom->getIdent());
896 >        identArray_.push_back(atom->getIdent());
897        }
898      }    
832
833    //fill molMembershipArray
834    //molMembershipArray is filled by SimCreator    
835    vector<int> molMembershipArray(nGlobalAtoms_);
836    for (int i = 0; i < nGlobalAtoms_; i++) {
837      molMembershipArray[i] = globalMolMembership_[i] + 1;
838    }
899      
900 <    //setup fortran simulation
900 >    //scan topology
901  
902      nExclude = excludedInteractions_.getSize();
903      nOneTwo = oneTwoInteractions_.getSize();
# Line 849 | Line 909 | namespace OpenMD {
909      int* oneThreeList = oneThreeInteractions_.getPairList();
910      int* oneFourList = oneFourInteractions_.getPairList();
911  
912 <    setFortranSim( &fInfo_, &nGlobalAtoms_, &nAtoms_, &identArray[0],
853 <                   &nExclude, excludeList,
854 <                   &nOneTwo, oneTwoList,
855 <                   &nOneThree, oneThreeList,
856 <                   &nOneFour, oneFourList,
857 <                   &molMembershipArray[0], &mfact[0], &nCutoffGroups_,
858 <                   &fortranGlobalGroupMembership[0], &isError);
859 <    
860 <    if( isError ){
861 <      
862 <      sprintf( painCave.errMsg,
863 <               "There was an error setting the simulation information in fortran.\n" );
864 <      painCave.isFatal = 1;
865 <      painCave.severity = OPENMD_ERROR;
866 <      simError();
867 <    }
868 <    
869 <    
870 <    sprintf( checkPointMsg,
871 <             "succesfully sent the simulation information to fortran.\n");
872 <    
873 <    errorCheckPoint();
874 <    
875 <    // Setup number of neighbors in neighbor list if present
876 <    if (simParams_->haveNeighborListNeighbors()) {
877 <      int nlistNeighbors = simParams_->getNeighborListNeighbors();
878 <      setNeighbors(&nlistNeighbors);
879 <    }
880 <  
881 < #ifdef IS_MPI    
882 <    //SimInfo is responsible for creating localToGlobalAtomIndex and
883 <    //localToGlobalGroupIndex
884 <    vector<int> localToGlobalAtomIndex(getNAtoms(), 0);
885 <    vector<int> localToGlobalCutoffGroupIndex;
886 <    mpiSimData parallelData;
887 <
888 <    for (mol = beginMolecule(mi); mol != NULL; mol  = nextMolecule(mi)) {
889 <
890 <      //local index(index in DataStorge) of atom is important
891 <      for (atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
892 <        localToGlobalAtomIndex[atom->getLocalIndex()] = atom->getGlobalIndex() + 1;
893 <      }
894 <
895 <      //local index of cutoff group is trivial, it only depends on the order of travesing
896 <      for (cg = mol->beginCutoffGroup(ci); cg != NULL; cg = mol->nextCutoffGroup(ci)) {
897 <        localToGlobalCutoffGroupIndex.push_back(cg->getGlobalIndex() + 1);
898 <      }        
899 <        
900 <    }
901 <
902 <    //fill up mpiSimData struct
903 <    parallelData.nMolGlobal = getNGlobalMolecules();
904 <    parallelData.nMolLocal = getNMolecules();
905 <    parallelData.nAtomsGlobal = getNGlobalAtoms();
906 <    parallelData.nAtomsLocal = getNAtoms();
907 <    parallelData.nGroupsGlobal = getNGlobalCutoffGroups();
908 <    parallelData.nGroupsLocal = getNCutoffGroups();
909 <    parallelData.myNode = worldRank;
910 <    MPI_Comm_size(MPI_COMM_WORLD, &(parallelData.nProcessors));
911 <
912 <    //pass mpiSimData struct and index arrays to fortran
913 <    setFsimParallel(&parallelData, &(parallelData.nAtomsLocal),
914 <                    &localToGlobalAtomIndex[0],  &(parallelData.nGroupsLocal),
915 <                    &localToGlobalCutoffGroupIndex[0], &isError);
916 <
917 <    if (isError) {
918 <      sprintf(painCave.errMsg,
919 <              "mpiRefresh errror: fortran didn't like something we gave it.\n");
920 <      painCave.isFatal = 1;
921 <      simError();
922 <    }
923 <
924 <    sprintf(checkPointMsg, " mpiRefresh successful.\n");
925 <    errorCheckPoint();
926 < #endif
927 <    fortranInitialized_ = true;
912 >    topologyDone_ = true;
913    }
914  
915    void SimInfo::addProperty(GenericData* genData) {
# Line 961 | Line 946 | namespace OpenMD {
946      Molecule* mol;
947      RigidBody* rb;
948      Atom* atom;
949 +    CutoffGroup* cg;
950      SimInfo::MoleculeIterator mi;
951      Molecule::RigidBodyIterator rbIter;
952 <    Molecule::AtomIterator atomIter;;
952 >    Molecule::AtomIterator atomIter;
953 >    Molecule::CutoffGroupIterator cgIter;
954  
955      for (mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) {
956          
# Line 973 | Line 960 | namespace OpenMD {
960          
961        for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) {
962          rb->setSnapshotManager(sman_);
963 +      }
964 +
965 +      for (cg = mol->beginCutoffGroup(cgIter); cg != NULL; cg = mol->nextCutoffGroup(cgIter)) {
966 +        cg->setSnapshotManager(sman_);
967        }
968      }    
969      

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines