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 1536 by gezelter, Wed Jan 5 14:49:05 2011 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"
58 #include "UseTheForce/doForces_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"
65
63   #ifdef IS_MPI
64 < #include "UseTheForce/mpiComponentPlan.h"
65 < #include "UseTheForce/DarkSide/simParallel_interface.h"
69 < #endif
64 > #include <mpi.h>
65 > #endif
66  
67   using namespace std;
68   namespace OpenMD {
# Line 78 | 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 132 | 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 275 | 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 681 | 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 700 | 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
704 <    // (some will be redundant with the ones found locally):
705 <    int count;
706 <    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 746 | 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 759 | 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_;
778 <    fInfo_.SIM_requires_SelfCorrection = usesElectrostaticAtoms_;
779 <    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
795    vector<RealType> mfact;
856      SimInfo::MoleculeIterator mi;
857      Molecule* mol;
858      Molecule::CutoffGroupIterator ci;
# Line 801 | 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
822 <    //AtomType, it is so confusing !!!)
823 <    vector<int> identArray;
890 >    // Build the identArray_
891  
892 <    //to avoid memory reallocation, reserve enough space identArray
893 <    identArray.reserve(getNAtoms());
827 <    
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      }    
833
834    //fill molMembershipArray
835    //molMembershipArray is filled by SimCreator    
836    vector<int> molMembershipArray(nGlobalAtoms_);
837    for (int i = 0; i < nGlobalAtoms_; i++) {
838      molMembershipArray[i] = globalMolMembership_[i] + 1;
839    }
899      
900 <    //setup fortran simulation
900 >    //scan topology
901  
902      nExclude = excludedInteractions_.getSize();
903      nOneTwo = oneTwoInteractions_.getSize();
# Line 850 | Line 909 | namespace OpenMD {
909      int* oneThreeList = oneThreeInteractions_.getPairList();
910      int* oneFourList = oneFourInteractions_.getPairList();
911  
912 <    setFortranSim( &fInfo_, &nGlobalAtoms_, &nAtoms_, &identArray[0],
854 <                   &nExclude, excludeList,
855 <                   &nOneTwo, oneTwoList,
856 <                   &nOneThree, oneThreeList,
857 <                   &nOneFour, oneFourList,
858 <                   &molMembershipArray[0], &mfact[0], &nCutoffGroups_,
859 <                   &fortranGlobalGroupMembership[0], &isError);
860 <    
861 <    if( isError ){
862 <      
863 <      sprintf( painCave.errMsg,
864 <               "There was an error setting the simulation information in fortran.\n" );
865 <      painCave.isFatal = 1;
866 <      painCave.severity = OPENMD_ERROR;
867 <      simError();
868 <    }
869 <    
870 <    
871 <    sprintf( checkPointMsg,
872 <             "succesfully sent the simulation information to fortran.\n");
873 <    
874 <    errorCheckPoint();
875 <    
876 <    // Setup number of neighbors in neighbor list if present
877 <    if (simParams_->haveNeighborListNeighbors()) {
878 <      int nlistNeighbors = simParams_->getNeighborListNeighbors();
879 <      setNeighbors(&nlistNeighbors);
880 <    }
881 <  
882 < #ifdef IS_MPI    
883 <    //SimInfo is responsible for creating localToGlobalAtomIndex and
884 <    //localToGlobalGroupIndex
885 <    vector<int> localToGlobalAtomIndex(getNAtoms(), 0);
886 <    vector<int> localToGlobalCutoffGroupIndex;
887 <    mpiSimData parallelData;
888 <
889 <    for (mol = beginMolecule(mi); mol != NULL; mol  = nextMolecule(mi)) {
890 <
891 <      //local index(index in DataStorge) of atom is important
892 <      for (atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
893 <        localToGlobalAtomIndex[atom->getLocalIndex()] = atom->getGlobalIndex() + 1;
894 <      }
895 <
896 <      //local index of cutoff group is trivial, it only depends on the order of travesing
897 <      for (cg = mol->beginCutoffGroup(ci); cg != NULL; cg = mol->nextCutoffGroup(ci)) {
898 <        localToGlobalCutoffGroupIndex.push_back(cg->getGlobalIndex() + 1);
899 <      }        
900 <        
901 <    }
902 <
903 <    //fill up mpiSimData struct
904 <    parallelData.nMolGlobal = getNGlobalMolecules();
905 <    parallelData.nMolLocal = getNMolecules();
906 <    parallelData.nAtomsGlobal = getNGlobalAtoms();
907 <    parallelData.nAtomsLocal = getNAtoms();
908 <    parallelData.nGroupsGlobal = getNGlobalCutoffGroups();
909 <    parallelData.nGroupsLocal = getNCutoffGroups();
910 <    parallelData.myNode = worldRank;
911 <    MPI_Comm_size(MPI_COMM_WORLD, &(parallelData.nProcessors));
912 <
913 <    //pass mpiSimData struct and index arrays to fortran
914 <    setFsimParallel(&parallelData, &(parallelData.nAtomsLocal),
915 <                    &localToGlobalAtomIndex[0],  &(parallelData.nGroupsLocal),
916 <                    &localToGlobalCutoffGroupIndex[0], &isError);
917 <
918 <    if (isError) {
919 <      sprintf(painCave.errMsg,
920 <              "mpiRefresh errror: fortran didn't like something we gave it.\n");
921 <      painCave.isFatal = 1;
922 <      simError();
923 <    }
924 <
925 <    sprintf(checkPointMsg, " mpiRefresh successful.\n");
926 <    errorCheckPoint();
927 < #endif
928 <
929 <    initFortranFF(&isError);
930 <    if (isError) {
931 <      sprintf(painCave.errMsg,
932 <              "initFortranFF errror: fortran didn't like something we gave it.\n");
933 <      painCave.isFatal = 1;
934 <      simError();
935 <    }
936 <    fortranInitialized_ = true;
912 >    topologyDone_ = true;
913    }
914  
915    void SimInfo::addProperty(GenericData* genData) {
# Line 970 | 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 982 | 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