--- branches/development/src/parallel/ForceMatrixDecomposition.cpp 2011/06/03 21:39:49 1575 +++ branches/development/src/parallel/ForceMatrixDecomposition.cpp 2011/06/09 20:26:29 1579 @@ -57,8 +57,9 @@ namespace OpenMD { storageLayout_ = sman_->getStorageLayout(); ff_ = info_->getForceField(); nLocal_ = snap_->getNumberOfAtoms(); - nGroups_ = snap_->getNumberOfCutoffGroups(); + nGroups_ = info_->getNLocalCutoffGroups(); + cerr << "in dId, nGroups = " << nGroups_ << "\n"; // gather the information for atomtype IDs (atids): identsLocal = info_->getIdentArray(); AtomLocalToGlobal = info_->getGlobalAtomIndices(); @@ -104,8 +105,8 @@ namespace OpenMD { cgColData.resize(nGroupsInCol_); cgColData.setStorageLayout(DataStorage::dslPosition); - identsRow.reserve(nAtomsInRow_); - identsCol.reserve(nAtomsInCol_); + identsRow.resize(nAtomsInRow_); + identsCol.resize(nAtomsInCol_); AtomCommIntRow->gather(identsLocal, identsRow); AtomCommIntColumn->gather(identsLocal, identsCol); @@ -120,7 +121,7 @@ namespace OpenMD { AtomCommRealColumn->gather(massFactorsLocal, massFactorsCol); groupListRow_.clear(); - groupListRow_.reserve(nGroupsInRow_); + groupListRow_.resize(nGroupsInRow_); for (int i = 0; i < nGroupsInRow_; i++) { int gid = cgRowToGlobal[i]; for (int j = 0; j < nAtomsInRow_; j++) { @@ -131,7 +132,7 @@ namespace OpenMD { } groupListCol_.clear(); - groupListCol_.reserve(nGroupsInCol_); + groupListCol_.resize(nGroupsInCol_); for (int i = 0; i < nGroupsInCol_; i++) { int gid = cgColToGlobal[i]; for (int j = 0; j < nAtomsInCol_; j++) { @@ -141,93 +142,273 @@ namespace OpenMD { } } - skipsForRowAtom.clear(); - skipsForRowAtom.reserve(nAtomsInRow_); + skipsForAtom.clear(); + skipsForAtom.resize(nAtomsInRow_); + toposForAtom.clear(); + toposForAtom.resize(nAtomsInRow_); + topoDist.clear(); + topoDist.resize(nAtomsInRow_); for (int i = 0; i < nAtomsInRow_; i++) { int iglob = AtomRowToGlobal[i]; - for (int j = 0; j < nAtomsInCol_; j++) { - int jglob = AtomColToGlobal[j]; - if (excludes.hasPair(iglob, jglob)) - skipsForRowAtom[i].push_back(j); - } - } - toposForRowAtom.clear(); - toposForRowAtom.reserve(nAtomsInRow_); - for (int i = 0; i < nAtomsInRow_; i++) { - int iglob = AtomRowToGlobal[i]; - int nTopos = 0; for (int j = 0; j < nAtomsInCol_; j++) { - int jglob = AtomColToGlobal[j]; + int jglob = AtomColToGlobal[j]; + + if (excludes.hasPair(iglob, jglob)) + skipsForAtom[i].push_back(j); + if (oneTwo.hasPair(iglob, jglob)) { - toposForRowAtom[i].push_back(j); - topoDistRow[i][nTopos] = 1; - nTopos++; + toposForAtom[i].push_back(j); + topoDist[i].push_back(1); + } else { + if (oneThree.hasPair(iglob, jglob)) { + toposForAtom[i].push_back(j); + topoDist[i].push_back(2); + } else { + if (oneFour.hasPair(iglob, jglob)) { + toposForAtom[i].push_back(j); + topoDist[i].push_back(3); + } + } } - if (oneThree.hasPair(iglob, jglob)) { - toposForRowAtom[i].push_back(j); - topoDistRow[i][nTopos] = 2; - nTopos++; - } - if (oneFour.hasPair(iglob, jglob)) { - toposForRowAtom[i].push_back(j); - topoDistRow[i][nTopos] = 3; - nTopos++; - } } } #endif groupList_.clear(); - groupList_.reserve(nGroups_); + groupList_.resize(nGroups_); for (int i = 0; i < nGroups_; i++) { int gid = cgLocalToGlobal[i]; for (int j = 0; j < nLocal_; j++) { int aid = AtomLocalToGlobal[j]; - if (globalGroupMembership[aid] == gid) + if (globalGroupMembership[aid] == gid) { groupList_[i].push_back(j); + } } } - skipsForLocalAtom.clear(); - skipsForLocalAtom.reserve(nLocal_); + skipsForAtom.clear(); + skipsForAtom.resize(nLocal_); + toposForAtom.clear(); + toposForAtom.resize(nLocal_); + topoDist.clear(); + topoDist.resize(nLocal_); for (int i = 0; i < nLocal_; i++) { int iglob = AtomLocalToGlobal[i]; + for (int j = 0; j < nLocal_; j++) { - int jglob = AtomLocalToGlobal[j]; + int jglob = AtomLocalToGlobal[j]; + if (excludes.hasPair(iglob, jglob)) - skipsForLocalAtom[i].push_back(j); + skipsForAtom[i].push_back(j); + + if (oneTwo.hasPair(iglob, jglob)) { + toposForAtom[i].push_back(j); + topoDist[i].push_back(1); + } else { + if (oneThree.hasPair(iglob, jglob)) { + toposForAtom[i].push_back(j); + topoDist[i].push_back(2); + } else { + if (oneFour.hasPair(iglob, jglob)) { + toposForAtom[i].push_back(j); + topoDist[i].push_back(3); + } + } + } } } + + createGtypeCutoffMap(); + } + + void ForceMatrixDecomposition::createGtypeCutoffMap() { - toposForLocalAtom.clear(); - toposForLocalAtom.reserve(nLocal_); - for (int i = 0; i < nLocal_; i++) { - int iglob = AtomLocalToGlobal[i]; - int nTopos = 0; - for (int j = 0; j < nLocal_; j++) { - int jglob = AtomLocalToGlobal[j]; - if (oneTwo.hasPair(iglob, jglob)) { - toposForLocalAtom[i].push_back(j); - topoDistLocal[i][nTopos] = 1; - nTopos++; + RealType tol = 1e-6; + RealType rc; + int atid; + set atypes = info_->getSimulatedAtomTypes(); + vector atypeCutoff; + atypeCutoff.resize( atypes.size() ); + + for (set::iterator at = atypes.begin(); + at != atypes.end(); ++at){ + rc = interactionMan_->getSuggestedCutoffRadius(*at); + atid = (*at)->getIdent(); + atypeCutoff[atid] = rc; + } + + vector gTypeCutoffs; + + // first we do a single loop over the cutoff groups to find the + // largest cutoff for any atypes present in this group. +#ifdef IS_MPI + vector groupCutoffRow(nGroupsInRow_, 0.0); + groupRowToGtype.resize(nGroupsInRow_); + for (int cg1 = 0; cg1 < nGroupsInRow_; cg1++) { + vector atomListRow = getAtomsInGroupRow(cg1); + for (vector::iterator ia = atomListRow.begin(); + ia != atomListRow.end(); ++ia) { + int atom1 = (*ia); + atid = identsRow[atom1]; + if (atypeCutoff[atid] > groupCutoffRow[cg1]) { + groupCutoffRow[cg1] = atypeCutoff[atid]; } - if (oneThree.hasPair(iglob, jglob)) { - toposForLocalAtom[i].push_back(j); - topoDistLocal[i][nTopos] = 2; - nTopos++; + } + + bool gTypeFound = false; + for (int gt = 0; gt < gTypeCutoffs.size(); gt++) { + if (abs(groupCutoffRow[cg1] - gTypeCutoffs[gt]) < tol) { + groupRowToGtype[cg1] = gt; + gTypeFound = true; + } + } + if (!gTypeFound) { + gTypeCutoffs.push_back( groupCutoffRow[cg1] ); + groupRowToGtype[cg1] = gTypeCutoffs.size() - 1; + } + + } + vector groupCutoffCol(nGroupsInCol_, 0.0); + groupColToGtype.resize(nGroupsInCol_); + for (int cg2 = 0; cg2 < nGroupsInCol_; cg2++) { + vector atomListCol = getAtomsInGroupColumn(cg2); + for (vector::iterator jb = atomListCol.begin(); + jb != atomListCol.end(); ++jb) { + int atom2 = (*jb); + atid = identsCol[atom2]; + if (atypeCutoff[atid] > groupCutoffCol[cg2]) { + groupCutoffCol[cg2] = atypeCutoff[atid]; } - if (oneFour.hasPair(iglob, jglob)) { - toposForLocalAtom[i].push_back(j); - topoDistLocal[i][nTopos] = 3; - nTopos++; + } + bool gTypeFound = false; + for (int gt = 0; gt < gTypeCutoffs.size(); gt++) { + if (abs(groupCutoffCol[cg2] - gTypeCutoffs[gt]) < tol) { + groupColToGtype[cg2] = gt; + gTypeFound = true; + } + } + if (!gTypeFound) { + gTypeCutoffs.push_back( groupCutoffCol[cg2] ); + groupColToGtype[cg2] = gTypeCutoffs.size() - 1; + } + } +#else + + vector groupCutoff(nGroups_, 0.0); + groupToGtype.resize(nGroups_); + + cerr << "nGroups = " << nGroups_ << "\n"; + for (int cg1 = 0; cg1 < nGroups_; cg1++) { + + groupCutoff[cg1] = 0.0; + vector atomList = getAtomsInGroupRow(cg1); + + for (vector::iterator ia = atomList.begin(); + ia != atomList.end(); ++ia) { + int atom1 = (*ia); + atid = identsLocal[atom1]; + if (atypeCutoff[atid] > groupCutoff[cg1]) { + groupCutoff[cg1] = atypeCutoff[atid]; } + } + + bool gTypeFound = false; + for (int gt = 0; gt < gTypeCutoffs.size(); gt++) { + if (abs(groupCutoff[cg1] - gTypeCutoffs[gt]) < tol) { + groupToGtype[cg1] = gt; + gTypeFound = true; + } + } + if (!gTypeFound) { + gTypeCutoffs.push_back( groupCutoff[cg1] ); + groupToGtype[cg1] = gTypeCutoffs.size() - 1; } } +#endif + + cerr << "gTypeCutoffs.size() = " << gTypeCutoffs.size() << "\n"; + // Now we find the maximum group cutoff value present in the simulation + + RealType groupMax = *max_element(gTypeCutoffs.begin(), gTypeCutoffs.end()); + +#ifdef IS_MPI + MPI::COMM_WORLD.Allreduce(&groupMax, &groupMax, 1, MPI::REALTYPE, MPI::MAX); +#endif + + RealType tradRcut = groupMax; + + for (int i = 0; i < gTypeCutoffs.size(); i++) { + for (int j = 0; j < gTypeCutoffs.size(); j++) { + RealType thisRcut; + switch(cutoffPolicy_) { + case TRADITIONAL: + thisRcut = tradRcut; + break; + case MIX: + thisRcut = 0.5 * (gTypeCutoffs[i] + gTypeCutoffs[j]); + break; + case MAX: + thisRcut = max(gTypeCutoffs[i], gTypeCutoffs[j]); + break; + default: + sprintf(painCave.errMsg, + "ForceMatrixDecomposition::createGtypeCutoffMap " + "hit an unknown cutoff policy!\n"); + painCave.severity = OPENMD_ERROR; + painCave.isFatal = 1; + simError(); + break; + } + + pair key = make_pair(i,j); + gTypeCutoffMap[key].first = thisRcut; + + if (thisRcut > largestRcut_) largestRcut_ = thisRcut; + + gTypeCutoffMap[key].second = thisRcut*thisRcut; + + gTypeCutoffMap[key].third = pow(thisRcut + skinThickness_, 2); + + // sanity check + + if (userChoseCutoff_) { + if (abs(gTypeCutoffMap[key].first - userCutoff_) > 0.0001) { + sprintf(painCave.errMsg, + "ForceMatrixDecomposition::createGtypeCutoffMap " + "user-specified rCut does not match computed group Cutoff\n"); + painCave.severity = OPENMD_ERROR; + painCave.isFatal = 1; + simError(); + } + } + } + } } - + + + groupCutoffs ForceMatrixDecomposition::getGroupCutoffs(int cg1, int cg2) { + int i, j; +#ifdef IS_MPI + i = groupRowToGtype[cg1]; + j = groupColToGtype[cg2]; +#else + i = groupToGtype[cg1]; + j = groupToGtype[cg2]; +#endif + return gTypeCutoffMap[make_pair(i,j)]; + } + + int ForceMatrixDecomposition::getTopologicalDistance(int atom1, int atom2) { + for (int j = 0; j < toposForAtom[atom1].size(); j++) { + if (toposForAtom[atom1][j] == atom2) + return topoDist[atom1][j]; + } + return 0; + } + void ForceMatrixDecomposition::zeroWorkArrays() { for (int j = 0; j < N_INTERACTION_FAMILIES; j++) { @@ -536,12 +717,8 @@ namespace OpenMD { return d; } - vector ForceMatrixDecomposition::getSkipsForRowAtom(int atom1) { -#ifdef IS_MPI - return skipsForRowAtom[atom1]; -#else - return skipsForLocalAtom[atom1]; -#endif + vector ForceMatrixDecomposition::getSkipsForAtom(int atom1) { + return skipsForAtom[atom1]; } /** @@ -574,35 +751,14 @@ namespace OpenMD { unique_id_2 = atom2; #endif -#ifdef IS_MPI - for (vector::iterator i = skipsForRowAtom[atom1].begin(); - i != skipsForRowAtom[atom1].end(); ++i) { - if ( (*i) == unique_id_2 ) return true; - } -#else - for (vector::iterator i = skipsForLocalAtom[atom1].begin(); - i != skipsForLocalAtom[atom1].end(); ++i) { + for (vector::iterator i = skipsForAtom[atom1].begin(); + i != skipsForAtom[atom1].end(); ++i) { if ( (*i) == unique_id_2 ) return true; } -#endif - } - int ForceMatrixDecomposition::getTopoDistance(int atom1, int atom2) { - -#ifdef IS_MPI - for (int i = 0; i < toposForRowAtom[atom1].size(); i++) { - if ( toposForRowAtom[atom1][i] == atom2 ) return topoDistRow[atom1][i]; - } -#else - for (int i = 0; i < toposForLocalAtom[atom1].size(); i++) { - if ( toposForLocalAtom[atom1][i] == atom2 ) return topoDistLocal[atom1][i]; - } -#endif - - // zero is default for unconnected (i.e. normal) pair interactions - return 0; } + void ForceMatrixDecomposition::addForceToAtomRow(int atom1, Vector3d fg){ #ifdef IS_MPI atomRowData.force[atom1] += fg; @@ -765,6 +921,7 @@ namespace OpenMD { vector > ForceMatrixDecomposition::buildNeighborList() { vector > neighborList; + groupCutoffs cuts; #ifdef IS_MPI cellListRow_.clear(); cellListCol_.clear(); @@ -772,10 +929,7 @@ namespace OpenMD { cellList_.clear(); #endif - // dangerous to not do error checking. - RealType rCut_; - - RealType rList_ = (rCut_ + skinThickness_); + RealType rList_ = (largestRcut_ + skinThickness_); RealType rl2 = rList_ * rList_; Snapshot* snap_ = sman_->getCurrentSnapshot(); Mat3x3d Hmat = snap_->getHmat(); @@ -791,8 +945,16 @@ namespace OpenMD { Vector3d rs, scaled, dr; Vector3i whichCell; int cellIndex; + int nCtot = nCells_.x() * nCells_.y() * nCells_.z(); #ifdef IS_MPI + cellListRow_.resize(nCtot); + cellListCol_.resize(nCtot); +#else + cellList_.resize(nCtot); +#endif + +#ifdef IS_MPI for (int i = 0; i < nGroupsInRow_; i++) { rs = cgRowData.position[i]; // scaled positions relative to the box vectors @@ -898,7 +1060,8 @@ namespace OpenMD { if (m2 != m1 || cgColToGlobal[(*j2)] < cgRowToGlobal[(*j1)]) { dr = cgColData.position[(*j2)] - cgRowData.position[(*j1)]; snap_->wrapVector(dr); - if (dr.lengthSquare() < rl2) { + cuts = getGroupCutoffs( (*j1), (*j2) ); + if (dr.lengthSquare() < cuts.third) { neighborList.push_back(make_pair((*j1), (*j2))); } } @@ -917,7 +1080,8 @@ namespace OpenMD { if (m2 != m1 || (*j2) < (*j1)) { dr = snap_->cgData.position[(*j2)] - snap_->cgData.position[(*j1)]; snap_->wrapVector(dr); - if (dr.lengthSquare() < rl2) { + cuts = getGroupCutoffs( (*j1), (*j2) ); + if (dr.lengthSquare() < cuts.third) { neighborList.push_back(make_pair((*j1), (*j2))); } }