--- trunk/src/parallel/ForceMatrixDecomposition.cpp 2012/08/31 21:16:10 1793 +++ trunk/src/parallel/ForceMatrixDecomposition.cpp 2015/03/03 17:02:20 2064 @@ -35,7 +35,7 @@ * * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). - * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). + * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). */ @@ -50,10 +50,7 @@ namespace OpenMD { ForceMatrixDecomposition::ForceMatrixDecomposition(SimInfo* info, InteractionManager* iMan) : ForceDecomposition(info, iMan) { - // In a parallel computation, row and colum scans must visit all - // surrounding cells (not just the 14 upper triangular blocks that - // are used when the processor can see all pairs) -#ifdef IS_MPI + // Row and colum scans must visit all surrounding cells cellOffsets_.clear(); cellOffsets_.push_back( Vector3i(-1,-1,-1) ); cellOffsets_.push_back( Vector3i( 0,-1,-1) ); @@ -82,7 +79,6 @@ namespace OpenMD { cellOffsets_.push_back( Vector3i(-1, 1, 1) ); cellOffsets_.push_back( Vector3i( 0, 1, 1) ); cellOffsets_.push_back( Vector3i( 1, 1, 1) ); -#endif } @@ -99,6 +95,7 @@ namespace OpenMD { nGroups_ = info_->getNLocalCutoffGroups(); // gather the information for atomtype IDs (atids): idents = info_->getIdentArray(); + regions = info_->getRegions(); AtomLocalToGlobal = info_->getGlobalAtomIndices(); cgLocalToGlobal = info_->getGlobalGroupIndices(); vector globalGroupMembership = info_->getGlobalGroupMembership(); @@ -118,8 +115,8 @@ namespace OpenMD { #ifdef IS_MPI - MPI::Intracomm row = rowComm.getComm(); - MPI::Intracomm col = colComm.getComm(); + MPI_Comm row = rowComm.getComm(); + MPI_Comm col = colComm.getComm(); AtomPlanIntRow = new Plan(row, nLocal_); AtomPlanRealRow = new Plan(row, nLocal_); @@ -163,7 +160,13 @@ namespace OpenMD { AtomPlanIntRow->gather(idents, identsRow); AtomPlanIntColumn->gather(idents, identsCol); + + regionsRow.resize(nAtomsInRow_); + regionsCol.resize(nAtomsInCol_); + AtomPlanIntRow->gather(regions, regionsRow); + AtomPlanIntColumn->gather(regions, regionsCol); + // allocate memory for the parallel objects atypesRow.resize(nAtomsInRow_); atypesCol.resize(nAtomsInCol_); @@ -299,187 +302,14 @@ namespace OpenMD { groupList_[i].push_back(j); } } - } - - - createGtypeCutoffMap(); - + } } - - void ForceMatrixDecomposition::createGtypeCutoffMap() { - - RealType tol = 1e-6; - largestRcut_ = 0.0; - int atid; - set atypes = info_->getSimulatedAtomTypes(); - map atypeCutoff; - - for (set::iterator at = atypes.begin(); - at != atypes.end(); ++at){ - atid = (*at)->getIdent(); - if (userChoseCutoff_) - atypeCutoff[atid] = userCutoff_; - else - atypeCutoff[atid] = interactionMan_->getSuggestedCutoffRadius(*at); - } - - 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]; - } - } - - 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]; - } - } - 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_); - 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 = idents[atom1]; - if (atypeCutoff[atid] > groupCutoff[cg1]) - groupCutoff[cg1] = atypeCutoff[atid]; - } - - bool gTypeFound = false; - for (unsigned 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 - - // 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 (unsigned int i = 0; i < gTypeCutoffs.size(); i++) { - for (unsigned 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 (%lf) does not match computed group Cutoff\n", userCutoff_); - 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 (unsigned int j = 0; j < toposForAtom[atom1].size(); j++) { if (toposForAtom[atom1][j] == atom2) return topoDist[atom1][j]; - } + } return 0; } @@ -559,11 +389,11 @@ namespace OpenMD { atomColData.electricField.end(), V3Zero); } - if (storageLayout_ & DataStorage::dslFlucQForce) { - fill(atomRowData.flucQFrc.begin(), atomRowData.flucQFrc.end(), - 0.0); - fill(atomColData.flucQFrc.begin(), atomColData.flucQFrc.end(), - 0.0); + if (storageLayout_ & DataStorage::dslSitePotential) { + fill(atomRowData.sitePotential.begin(), + atomRowData.sitePotential.end(), 0.0); + fill(atomColData.sitePotential.begin(), + atomColData.sitePotential.end(), 0.0); } #endif @@ -598,14 +428,24 @@ namespace OpenMD { fill(snap_->atomData.electricField.begin(), snap_->atomData.electricField.end(), V3Zero); } + if (storageLayout_ & DataStorage::dslSitePotential) { + fill(snap_->atomData.sitePotential.begin(), + snap_->atomData.sitePotential.end(), 0.0); + } } void ForceMatrixDecomposition::distributeData() { + +#ifdef IS_MPI + snap_ = sman_->getCurrentSnapshot(); storageLayout_ = sman_->getStorageLayout(); -#ifdef IS_MPI + bool needsCG = true; + if(info_->getNCutoffGroups() != info_->getNAtoms()) + needsCG = false; + // gather up the atomic positions AtomPlanVectorRow->gather(snap_->atomData.position, atomRowData.position); @@ -614,21 +454,24 @@ namespace OpenMD { // gather up the cutoff group positions - cgPlanVectorRow->gather(snap_->cgData.position, - cgRowData.position); + if (needsCG) { + cgPlanVectorRow->gather(snap_->cgData.position, + cgRowData.position); + + cgPlanVectorColumn->gather(snap_->cgData.position, + cgColData.position); + } - cgPlanVectorColumn->gather(snap_->cgData.position, - cgColData.position); - - if (needVelocities_) { // gather up the atomic velocities AtomPlanVectorColumn->gather(snap_->atomData.velocity, atomColData.velocity); - - cgPlanVectorColumn->gather(snap_->cgData.velocity, - cgColData.velocity); + + if (needsCG) { + cgPlanVectorColumn->gather(snap_->cgData.velocity, + cgColData.velocity); + } } @@ -639,15 +482,22 @@ namespace OpenMD { AtomPlanMatrixColumn->gather(snap_->atomData.aMat, atomColData.aMat); } - - // if needed, gather the atomic eletrostatic frames - if (storageLayout_ & DataStorage::dslElectroFrame) { - AtomPlanMatrixRow->gather(snap_->atomData.electroFrame, - atomRowData.electroFrame); - AtomPlanMatrixColumn->gather(snap_->atomData.electroFrame, - atomColData.electroFrame); + + // if needed, gather the atomic eletrostatic information + if (storageLayout_ & DataStorage::dslDipole) { + AtomPlanVectorRow->gather(snap_->atomData.dipole, + atomRowData.dipole); + AtomPlanVectorColumn->gather(snap_->atomData.dipole, + atomColData.dipole); } + if (storageLayout_ & DataStorage::dslQuadrupole) { + AtomPlanMatrixRow->gather(snap_->atomData.quadrupole, + atomRowData.quadrupole); + AtomPlanMatrixColumn->gather(snap_->atomData.quadrupole, + atomColData.quadrupole); + } + // if needed, gather the atomic fluctuating charge values if (storageLayout_ & DataStorage::dslFlucQPosition) { AtomPlanRealRow->gather(snap_->atomData.flucQPos, @@ -663,10 +513,11 @@ namespace OpenMD { * data structures. */ void ForceMatrixDecomposition::collectIntermediateData() { +#ifdef IS_MPI + snap_ = sman_->getCurrentSnapshot(); storageLayout_ = sman_->getStorageLayout(); -#ifdef IS_MPI - + if (storageLayout_ & DataStorage::dslDensity) { AtomPlanRealRow->scatter(atomRowData.density, @@ -679,6 +530,8 @@ namespace OpenMD { snap_->atomData.density[i] += rho_tmp[i]; } + // this isn't necessary if we don't have polarizable atoms, but + // we'll leave it here for now. if (storageLayout_ & DataStorage::dslElectricField) { AtomPlanVectorRow->scatter(atomRowData.electricField, @@ -686,7 +539,8 @@ namespace OpenMD { int n = snap_->atomData.electricField.size(); vector field_tmp(n, V3Zero); - AtomPlanVectorColumn->scatter(atomColData.electricField, field_tmp); + AtomPlanVectorColumn->scatter(atomColData.electricField, + field_tmp); for (int i = 0; i < n; i++) snap_->atomData.electricField[i] += field_tmp[i]; } @@ -698,9 +552,10 @@ namespace OpenMD { * row and column-indexed data structures */ void ForceMatrixDecomposition::distributeIntermediateData() { +#ifdef IS_MPI snap_ = sman_->getCurrentSnapshot(); storageLayout_ = sman_->getStorageLayout(); -#ifdef IS_MPI + if (storageLayout_ & DataStorage::dslFunctional) { AtomPlanRealRow->gather(snap_->atomData.functional, atomRowData.functional); @@ -719,9 +574,10 @@ namespace OpenMD { void ForceMatrixDecomposition::collectData() { +#ifdef IS_MPI snap_ = sman_->getCurrentSnapshot(); storageLayout_ = sman_->getStorageLayout(); -#ifdef IS_MPI + int n = snap_->atomData.force.size(); vector frc_tmp(n, V3Zero); @@ -784,6 +640,38 @@ namespace OpenMD { for (int i = 0; i < nq; i++) snap_->atomData.flucQFrc[i] += fqfrc_tmp[i]; + } + + if (storageLayout_ & DataStorage::dslElectricField) { + + int nef = snap_->atomData.electricField.size(); + vector efield_tmp(nef, V3Zero); + + AtomPlanVectorRow->scatter(atomRowData.electricField, efield_tmp); + for (int i = 0; i < nef; i++) { + snap_->atomData.electricField[i] += efield_tmp[i]; + efield_tmp[i] = 0.0; + } + + AtomPlanVectorColumn->scatter(atomColData.electricField, efield_tmp); + for (int i = 0; i < nef; i++) + snap_->atomData.electricField[i] += efield_tmp[i]; + } + + if (storageLayout_ & DataStorage::dslSitePotential) { + + int nsp = snap_->atomData.sitePotential.size(); + vector sp_tmp(nsp, 0.0); + + AtomPlanRealRow->scatter(atomRowData.sitePotential, sp_tmp); + for (int i = 0; i < nsp; i++) { + snap_->atomData.sitePotential[i] += sp_tmp[i]; + sp_tmp[i] = 0.0; + } + + AtomPlanRealColumn->scatter(atomColData.sitePotential, sp_tmp); + for (int i = 0; i < nsp; i++) + snap_->atomData.sitePotential[i] += sp_tmp[i]; } nLocal_ = snap_->getNumberOfAtoms(); @@ -869,23 +757,23 @@ namespace OpenMD { for (int ii = 0; ii < N_INTERACTION_FAMILIES; ii++) { RealType ploc1 = pairwisePot[ii]; RealType ploc2 = 0.0; - MPI::COMM_WORLD.Allreduce(&ploc1, &ploc2, 1, MPI::REALTYPE, MPI::SUM); + MPI_Allreduce(&ploc1, &ploc2, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD); pairwisePot[ii] = ploc2; } for (int ii = 0; ii < N_INTERACTION_FAMILIES; ii++) { RealType ploc1 = excludedPot[ii]; RealType ploc2 = 0.0; - MPI::COMM_WORLD.Allreduce(&ploc1, &ploc2, 1, MPI::REALTYPE, MPI::SUM); + MPI_Allreduce(&ploc1, &ploc2, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD); excludedPot[ii] = ploc2; } // Here be dragons. - MPI::Intracomm col = colComm.getComm(); + MPI_Comm col = colComm.getComm(); - col.Allreduce(MPI::IN_PLACE, + MPI_Allreduce(MPI_IN_PLACE, &snap_->frameData.conductiveHeatFlux[0], 3, - MPI::REALTYPE, MPI::SUM); + MPI_REALTYPE, MPI_SUM, col); #endif @@ -897,29 +785,28 @@ namespace OpenMD { * functional) loops onto local data structures. */ void ForceMatrixDecomposition::collectSelfData() { + +#ifdef IS_MPI snap_ = sman_->getCurrentSnapshot(); storageLayout_ = sman_->getStorageLayout(); -#ifdef IS_MPI for (int ii = 0; ii < N_INTERACTION_FAMILIES; ii++) { RealType ploc1 = embeddingPot[ii]; RealType ploc2 = 0.0; - MPI::COMM_WORLD.Allreduce(&ploc1, &ploc2, 1, MPI::REALTYPE, MPI::SUM); + MPI_Allreduce(&ploc1, &ploc2, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD); embeddingPot[ii] = ploc2; } for (int ii = 0; ii < N_INTERACTION_FAMILIES; ii++) { RealType ploc1 = excludedSelfPot[ii]; RealType ploc2 = 0.0; - MPI::COMM_WORLD.Allreduce(&ploc1, &ploc2, 1, MPI::REALTYPE, MPI::SUM); + MPI_Allreduce(&ploc1, &ploc2, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD); excludedSelfPot[ii] = ploc2; } #endif } - - - int ForceMatrixDecomposition::getNAtomsInRow() { + int& ForceMatrixDecomposition::getNAtomsInRow() { #ifdef IS_MPI return nAtomsInRow_; #else @@ -930,7 +817,7 @@ namespace OpenMD { /** * returns the list of atoms belonging to this group. */ - vector ForceMatrixDecomposition::getAtomsInGroupRow(int cg1){ + vector& ForceMatrixDecomposition::getAtomsInGroupRow(int cg1){ #ifdef IS_MPI return groupListRow_[cg1]; #else @@ -938,7 +825,7 @@ namespace OpenMD { #endif } - vector ForceMatrixDecomposition::getAtomsInGroupColumn(int cg2){ + vector& ForceMatrixDecomposition::getAtomsInGroupColumn(int cg2){ #ifdef IS_MPI return groupListCol_[cg2]; #else @@ -946,20 +833,23 @@ namespace OpenMD { #endif } - Vector3d ForceMatrixDecomposition::getIntergroupVector(int cg1, int cg2){ + Vector3d ForceMatrixDecomposition::getIntergroupVector(int cg1, + int cg2){ + Vector3d d; - #ifdef IS_MPI d = cgColData.position[cg2] - cgRowData.position[cg1]; #else d = snap_->cgData.position[cg2] - snap_->cgData.position[cg1]; #endif - snap_->wrapVector(d); + if (usePeriodicBoundaryConditions_) { + snap_->wrapVector(d); + } return d; } - Vector3d ForceMatrixDecomposition::getGroupVelocityColumn(int cg2){ + Vector3d& ForceMatrixDecomposition::getGroupVelocityColumn(int cg2){ #ifdef IS_MPI return cgColData.velocity[cg2]; #else @@ -967,7 +857,7 @@ namespace OpenMD { #endif } - Vector3d ForceMatrixDecomposition::getAtomVelocityColumn(int atom2){ + Vector3d& ForceMatrixDecomposition::getAtomVelocityColumn(int atom2){ #ifdef IS_MPI return atomColData.velocity[atom2]; #else @@ -976,8 +866,8 @@ namespace OpenMD { } - Vector3d ForceMatrixDecomposition::getAtomToGroupVectorRow(int atom1, int cg1){ - + Vector3d ForceMatrixDecomposition::getAtomToGroupVectorRow(int atom1, + int cg1) { Vector3d d; #ifdef IS_MPI @@ -985,12 +875,14 @@ namespace OpenMD { #else d = snap_->cgData.position[cg1] - snap_->atomData.position[atom1]; #endif - - snap_->wrapVector(d); + if (usePeriodicBoundaryConditions_) { + snap_->wrapVector(d); + } return d; } - Vector3d ForceMatrixDecomposition::getAtomToGroupVectorColumn(int atom2, int cg2){ + Vector3d ForceMatrixDecomposition::getAtomToGroupVectorColumn(int atom2, + int cg2) { Vector3d d; #ifdef IS_MPI @@ -998,12 +890,13 @@ namespace OpenMD { #else d = snap_->cgData.position[cg2] - snap_->atomData.position[atom2]; #endif - - snap_->wrapVector(d); + if (usePeriodicBoundaryConditions_) { + snap_->wrapVector(d); + } return d; } - RealType ForceMatrixDecomposition::getMassFactorRow(int atom1) { + RealType& ForceMatrixDecomposition::getMassFactorRow(int atom1) { #ifdef IS_MPI return massFactorsRow[atom1]; #else @@ -1011,7 +904,7 @@ namespace OpenMD { #endif } - RealType ForceMatrixDecomposition::getMassFactorColumn(int atom2) { + RealType& ForceMatrixDecomposition::getMassFactorColumn(int atom2) { #ifdef IS_MPI return massFactorsCol[atom2]; #else @@ -1020,7 +913,8 @@ namespace OpenMD { } - Vector3d ForceMatrixDecomposition::getInteratomicVector(int atom1, int atom2){ + Vector3d ForceMatrixDecomposition::getInteratomicVector(int atom1, + int atom2){ Vector3d d; #ifdef IS_MPI @@ -1028,12 +922,13 @@ namespace OpenMD { #else d = snap_->atomData.position[atom2] - snap_->atomData.position[atom1]; #endif - - snap_->wrapVector(d); + if (usePeriodicBoundaryConditions_) { + snap_->wrapVector(d); + } return d; } - vector ForceMatrixDecomposition::getExcludesForAtom(int atom1) { + vector& ForceMatrixDecomposition::getExcludesForAtom(int atom1) { return excludesForAtom[atom1]; } @@ -1041,20 +936,21 @@ namespace OpenMD { * We need to exclude some overcounted interactions that result from * the parallel decomposition. */ - bool ForceMatrixDecomposition::skipAtomPair(int atom1, int atom2, int cg1, int cg2) { - int unique_id_1, unique_id_2, group1, group2; + bool ForceMatrixDecomposition::skipAtomPair(int atom1, int atom2, + int cg1, int cg2) { + int unique_id_1, unique_id_2; #ifdef IS_MPI // in MPI, we have to look up the unique IDs for each atom unique_id_1 = AtomRowToGlobal[atom1]; unique_id_2 = AtomColToGlobal[atom2]; - group1 = cgRowToGlobal[cg1]; - group2 = cgColToGlobal[cg2]; + // group1 = cgRowToGlobal[cg1]; + // group2 = cgColToGlobal[cg2]; #else unique_id_1 = AtomLocalToGlobal[atom1]; unique_id_2 = AtomLocalToGlobal[atom2]; - group1 = cgLocalToGlobal[cg1]; - group2 = cgLocalToGlobal[cg2]; + int group1 = cgLocalToGlobal[cg1]; + int group2 = cgLocalToGlobal[cg2]; #endif if (unique_id_1 == unique_id_2) return true; @@ -1118,114 +1014,241 @@ namespace OpenMD { // filling interaction blocks with pointers void ForceMatrixDecomposition::fillInteractionData(InteractionData &idat, - int atom1, int atom2) { + int atom1, int atom2, + bool newAtom1) { idat.excluded = excludeAtomPair(atom1, atom2); - + + if (newAtom1) { + #ifdef IS_MPI - idat.atypes = make_pair( atypesRow[atom1], atypesCol[atom2]); - //idat.atypes = make_pair( ff_->getAtomType(identsRow[atom1]), - // ff_->getAtomType(identsCol[atom2]) ); - + idat.atid1 = identsRow[atom1]; + idat.atid2 = identsCol[atom2]; + + if (regionsRow[atom1] >= 0 && regionsCol[atom2] >= 0) { + idat.sameRegion = (regionsRow[atom1] == regionsCol[atom2]); + } else { + idat.sameRegion = false; + } + + if (storageLayout_ & DataStorage::dslAmat) { + idat.A1 = &(atomRowData.aMat[atom1]); + idat.A2 = &(atomColData.aMat[atom2]); + } + + if (storageLayout_ & DataStorage::dslTorque) { + idat.t1 = &(atomRowData.torque[atom1]); + idat.t2 = &(atomColData.torque[atom2]); + } + + if (storageLayout_ & DataStorage::dslDipole) { + idat.dipole1 = &(atomRowData.dipole[atom1]); + idat.dipole2 = &(atomColData.dipole[atom2]); + } + + if (storageLayout_ & DataStorage::dslQuadrupole) { + idat.quadrupole1 = &(atomRowData.quadrupole[atom1]); + idat.quadrupole2 = &(atomColData.quadrupole[atom2]); + } + + if (storageLayout_ & DataStorage::dslDensity) { + idat.rho1 = &(atomRowData.density[atom1]); + idat.rho2 = &(atomColData.density[atom2]); + } + + if (storageLayout_ & DataStorage::dslFunctional) { + idat.frho1 = &(atomRowData.functional[atom1]); + idat.frho2 = &(atomColData.functional[atom2]); + } + + if (storageLayout_ & DataStorage::dslFunctionalDerivative) { + idat.dfrho1 = &(atomRowData.functionalDerivative[atom1]); + idat.dfrho2 = &(atomColData.functionalDerivative[atom2]); + } + + if (storageLayout_ & DataStorage::dslParticlePot) { + idat.particlePot1 = &(atomRowData.particlePot[atom1]); + idat.particlePot2 = &(atomColData.particlePot[atom2]); + } + + if (storageLayout_ & DataStorage::dslSkippedCharge) { + idat.skippedCharge1 = &(atomRowData.skippedCharge[atom1]); + idat.skippedCharge2 = &(atomColData.skippedCharge[atom2]); + } + + if (storageLayout_ & DataStorage::dslFlucQPosition) { + idat.flucQ1 = &(atomRowData.flucQPos[atom1]); + idat.flucQ2 = &(atomColData.flucQPos[atom2]); + } + +#else + + idat.atid1 = idents[atom1]; + idat.atid2 = idents[atom2]; + + if (regions[atom1] >= 0 && regions[atom2] >= 0) { + idat.sameRegion = (regions[atom1] == regions[atom2]); + } else { + idat.sameRegion = false; + } + + if (storageLayout_ & DataStorage::dslAmat) { + idat.A1 = &(snap_->atomData.aMat[atom1]); + idat.A2 = &(snap_->atomData.aMat[atom2]); + } + + if (storageLayout_ & DataStorage::dslTorque) { + idat.t1 = &(snap_->atomData.torque[atom1]); + idat.t2 = &(snap_->atomData.torque[atom2]); + } + + if (storageLayout_ & DataStorage::dslDipole) { + idat.dipole1 = &(snap_->atomData.dipole[atom1]); + idat.dipole2 = &(snap_->atomData.dipole[atom2]); + } + + if (storageLayout_ & DataStorage::dslQuadrupole) { + idat.quadrupole1 = &(snap_->atomData.quadrupole[atom1]); + idat.quadrupole2 = &(snap_->atomData.quadrupole[atom2]); + } + + if (storageLayout_ & DataStorage::dslDensity) { + idat.rho1 = &(snap_->atomData.density[atom1]); + idat.rho2 = &(snap_->atomData.density[atom2]); + } + + if (storageLayout_ & DataStorage::dslFunctional) { + idat.frho1 = &(snap_->atomData.functional[atom1]); + idat.frho2 = &(snap_->atomData.functional[atom2]); + } + + if (storageLayout_ & DataStorage::dslFunctionalDerivative) { + idat.dfrho1 = &(snap_->atomData.functionalDerivative[atom1]); + idat.dfrho2 = &(snap_->atomData.functionalDerivative[atom2]); + } + + if (storageLayout_ & DataStorage::dslParticlePot) { + idat.particlePot1 = &(snap_->atomData.particlePot[atom1]); + idat.particlePot2 = &(snap_->atomData.particlePot[atom2]); + } + + if (storageLayout_ & DataStorage::dslSkippedCharge) { + idat.skippedCharge1 = &(snap_->atomData.skippedCharge[atom1]); + idat.skippedCharge2 = &(snap_->atomData.skippedCharge[atom2]); + } + + if (storageLayout_ & DataStorage::dslFlucQPosition) { + idat.flucQ1 = &(snap_->atomData.flucQPos[atom1]); + idat.flucQ2 = &(snap_->atomData.flucQPos[atom2]); + } +#endif + + } else { + // atom1 is not new, so don't bother updating properties of that atom: +#ifdef IS_MPI + idat.atid2 = identsCol[atom2]; + + if (regionsRow[atom1] >= 0 && regionsCol[atom2] >= 0) { + idat.sameRegion = (regionsRow[atom1] == regionsCol[atom2]); + } else { + idat.sameRegion = false; + } + if (storageLayout_ & DataStorage::dslAmat) { - idat.A1 = &(atomRowData.aMat[atom1]); idat.A2 = &(atomColData.aMat[atom2]); } - if (storageLayout_ & DataStorage::dslElectroFrame) { - idat.eFrame1 = &(atomRowData.electroFrame[atom1]); - idat.eFrame2 = &(atomColData.electroFrame[atom2]); - } - if (storageLayout_ & DataStorage::dslTorque) { - idat.t1 = &(atomRowData.torque[atom1]); idat.t2 = &(atomColData.torque[atom2]); } + if (storageLayout_ & DataStorage::dslDipole) { + idat.dipole2 = &(atomColData.dipole[atom2]); + } + + if (storageLayout_ & DataStorage::dslQuadrupole) { + idat.quadrupole2 = &(atomColData.quadrupole[atom2]); + } + if (storageLayout_ & DataStorage::dslDensity) { - idat.rho1 = &(atomRowData.density[atom1]); idat.rho2 = &(atomColData.density[atom2]); } if (storageLayout_ & DataStorage::dslFunctional) { - idat.frho1 = &(atomRowData.functional[atom1]); idat.frho2 = &(atomColData.functional[atom2]); } if (storageLayout_ & DataStorage::dslFunctionalDerivative) { - idat.dfrho1 = &(atomRowData.functionalDerivative[atom1]); idat.dfrho2 = &(atomColData.functionalDerivative[atom2]); } if (storageLayout_ & DataStorage::dslParticlePot) { - idat.particlePot1 = &(atomRowData.particlePot[atom1]); idat.particlePot2 = &(atomColData.particlePot[atom2]); } if (storageLayout_ & DataStorage::dslSkippedCharge) { - idat.skippedCharge1 = &(atomRowData.skippedCharge[atom1]); idat.skippedCharge2 = &(atomColData.skippedCharge[atom2]); } - if (storageLayout_ & DataStorage::dslFlucQPosition) { - idat.flucQ1 = &(atomRowData.flucQPos[atom1]); + if (storageLayout_ & DataStorage::dslFlucQPosition) { idat.flucQ2 = &(atomColData.flucQPos[atom2]); } -#else - - idat.atypes = make_pair( atypesLocal[atom1], atypesLocal[atom2]); +#else + idat.atid2 = idents[atom2]; + if (regions[atom1] >= 0 && regions[atom2] >= 0) { + idat.sameRegion = (regions[atom1] == regions[atom2]); + } else { + idat.sameRegion = false; + } + if (storageLayout_ & DataStorage::dslAmat) { - idat.A1 = &(snap_->atomData.aMat[atom1]); idat.A2 = &(snap_->atomData.aMat[atom2]); } - if (storageLayout_ & DataStorage::dslElectroFrame) { - idat.eFrame1 = &(snap_->atomData.electroFrame[atom1]); - idat.eFrame2 = &(snap_->atomData.electroFrame[atom2]); - } - if (storageLayout_ & DataStorage::dslTorque) { - idat.t1 = &(snap_->atomData.torque[atom1]); idat.t2 = &(snap_->atomData.torque[atom2]); } + if (storageLayout_ & DataStorage::dslDipole) { + idat.dipole2 = &(snap_->atomData.dipole[atom2]); + } + + if (storageLayout_ & DataStorage::dslQuadrupole) { + idat.quadrupole2 = &(snap_->atomData.quadrupole[atom2]); + } + if (storageLayout_ & DataStorage::dslDensity) { - idat.rho1 = &(snap_->atomData.density[atom1]); idat.rho2 = &(snap_->atomData.density[atom2]); } if (storageLayout_ & DataStorage::dslFunctional) { - idat.frho1 = &(snap_->atomData.functional[atom1]); idat.frho2 = &(snap_->atomData.functional[atom2]); } if (storageLayout_ & DataStorage::dslFunctionalDerivative) { - idat.dfrho1 = &(snap_->atomData.functionalDerivative[atom1]); idat.dfrho2 = &(snap_->atomData.functionalDerivative[atom2]); } if (storageLayout_ & DataStorage::dslParticlePot) { - idat.particlePot1 = &(snap_->atomData.particlePot[atom1]); idat.particlePot2 = &(snap_->atomData.particlePot[atom2]); } if (storageLayout_ & DataStorage::dslSkippedCharge) { - idat.skippedCharge1 = &(snap_->atomData.skippedCharge[atom1]); idat.skippedCharge2 = &(snap_->atomData.skippedCharge[atom2]); } if (storageLayout_ & DataStorage::dslFlucQPosition) { - idat.flucQ1 = &(snap_->atomData.flucQPos[atom1]); idat.flucQ2 = &(snap_->atomData.flucQPos[atom2]); } #endif + } } - - void ForceMatrixDecomposition::unpackInteractionData(InteractionData &idat, int atom1, int atom2) { + void ForceMatrixDecomposition::unpackInteractionData(InteractionData &idat, + int atom1, int atom2) { #ifdef IS_MPI pot_row[atom1] += RealType(0.5) * *(idat.pot); pot_col[atom2] += RealType(0.5) * *(idat.pot); @@ -1245,6 +1268,11 @@ namespace OpenMD { atomColData.electricField[atom2] += *(idat.eField2); } + if (storageLayout_ & DataStorage::dslSitePotential) { + atomRowData.sitePotential[atom1] += *(idat.sPot1); + atomColData.sitePotential[atom2] += *(idat.sPot2); + } + #else pairwisePot += *(idat.pot); excludedPot += *(idat.excludedPot); @@ -1271,6 +1299,11 @@ namespace OpenMD { snap_->atomData.electricField[atom2] += *(idat.eField2); } + if (storageLayout_ & DataStorage::dslSitePotential) { + snap_->atomData.sitePotential[atom1] += *(idat.sPot1); + snap_->atomData.sitePotential[atom2] += *(idat.sPot2); + } + #endif } @@ -1278,60 +1311,94 @@ namespace OpenMD { /* * buildNeighborList * - * first element of pair is row-indexed CutoffGroup - * second element of pair is column-indexed CutoffGroup + * Constructs the Verlet neighbor list for a force-matrix + * decomposition. In this case, each processor is responsible for + * row-site interactions with column-sites. + * + * neighborList is returned as a packed array of neighboring + * column-ordered CutoffGroups. The starting position in + * neighborList for each row-ordered CutoffGroup is given by the + * returned vector point. */ - vector > ForceMatrixDecomposition::buildNeighborList() { - - vector > neighborList; - groupCutoffs cuts; + void ForceMatrixDecomposition::buildNeighborList(vector& neighborList, + vector& point) { + neighborList.clear(); + point.clear(); + int len = 0; + bool doAllPairs = false; + Snapshot* snap_ = sman_->getCurrentSnapshot(); + Mat3x3d box; + Mat3x3d invBox; + + Vector3d rs, scaled, dr; + Vector3i whichCell; + int cellIndex; + #ifdef IS_MPI cellListRow_.clear(); cellListCol_.clear(); + point.resize(nGroupsInRow_+1); #else cellList_.clear(); + point.resize(nGroups_+1); #endif + + if (!usePeriodicBoundaryConditions_) { + box = snap_->getBoundingBox(); + invBox = snap_->getInvBoundingBox(); + } else { + box = snap_->getHmat(); + invBox = snap_->getInvHmat(); + } + + Vector3d A = box.getColumn(0); + Vector3d B = box.getColumn(1); + Vector3d C = box.getColumn(2); - RealType rList_ = (largestRcut_ + skinThickness_); - Snapshot* snap_ = sman_->getCurrentSnapshot(); - Mat3x3d Hmat = snap_->getHmat(); - Vector3d Hx = Hmat.getColumn(0); - Vector3d Hy = Hmat.getColumn(1); - Vector3d Hz = Hmat.getColumn(2); + // Required for triclinic cells + Vector3d AxB = cross(A, B); + Vector3d BxC = cross(B, C); + Vector3d CxA = cross(C, A); - nCells_.x() = (int) ( Hx.length() )/ rList_; - nCells_.y() = (int) ( Hy.length() )/ rList_; - nCells_.z() = (int) ( Hz.length() )/ rList_; + // unit vectors perpendicular to the faces of the triclinic cell: + AxB.normalize(); + BxC.normalize(); + CxA.normalize(); - // handle small boxes where the cell offsets can end up repeating cells + // A set of perpendicular lengths in triclinic cells: + RealType Wa = abs(dot(A, BxC)); + RealType Wb = abs(dot(B, CxA)); + RealType Wc = abs(dot(C, AxB)); + nCells_.x() = int( Wa / rList_ ); + nCells_.y() = int( Wb / rList_ ); + nCells_.z() = int( Wc / rList_ ); + + // handle small boxes where the cell offsets can end up repeating cells if (nCells_.x() < 3) doAllPairs = true; if (nCells_.y() < 3) doAllPairs = true; if (nCells_.z() < 3) doAllPairs = true; - - Mat3x3d invHmat = snap_->getInvHmat(); - 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 - + if (!doAllPairs) { + #ifdef IS_MPI - + for (int i = 0; i < nGroupsInRow_; i++) { rs = cgRowData.position[i]; // scaled positions relative to the box vectors - scaled = invHmat * rs; + scaled = invBox * rs; // wrap the vector back into the unit box by subtracting integer box // numbers @@ -1359,7 +1426,7 @@ namespace OpenMD { rs = cgColData.position[i]; // scaled positions relative to the box vectors - scaled = invHmat * rs; + scaled = invBox * rs; // wrap the vector back into the unit box by subtracting integer box // numbers @@ -1383,13 +1450,13 @@ namespace OpenMD { // add this cutoff group to the list of groups in this cell; cellListCol_[cellIndex].push_back(i); } - + #else for (int i = 0; i < nGroups_; i++) { rs = snap_->cgData.position[i]; // scaled positions relative to the box vectors - scaled = invHmat * rs; + scaled = invBox * rs; // wrap the vector back into the unit box by subtracting integer box // numbers @@ -1403,9 +1470,9 @@ namespace OpenMD { } // find xyz-indices of cell that cutoffGroup is in. - whichCell.x() = nCells_.x() * scaled.x(); - whichCell.y() = nCells_.y() * scaled.y(); - whichCell.z() = nCells_.z() * scaled.z(); + whichCell.x() = int(nCells_.x() * scaled.x()); + whichCell.y() = int(nCells_.y() * scaled.y()); + whichCell.z() = int(nCells_.z() * scaled.z()); // find single index of this cell: cellIndex = Vlinear(whichCell, nCells_); @@ -1416,123 +1483,153 @@ namespace OpenMD { #endif - for (int m1z = 0; m1z < nCells_.z(); m1z++) { - for (int m1y = 0; m1y < nCells_.y(); m1y++) { - for (int m1x = 0; m1x < nCells_.x(); m1x++) { - Vector3i m1v(m1x, m1y, m1z); - int m1 = Vlinear(m1v, nCells_); - - for (vector::iterator os = cellOffsets_.begin(); - os != cellOffsets_.end(); ++os) { - - Vector3i m2v = m1v + (*os); - - - if (m2v.x() >= nCells_.x()) { - m2v.x() = 0; - } else if (m2v.x() < 0) { - m2v.x() = nCells_.x() - 1; - } - - if (m2v.y() >= nCells_.y()) { - m2v.y() = 0; - } else if (m2v.y() < 0) { - m2v.y() = nCells_.y() - 1; - } - - if (m2v.z() >= nCells_.z()) { - m2v.z() = 0; - } else if (m2v.z() < 0) { - m2v.z() = nCells_.z() - 1; - } - - int m2 = Vlinear (m2v, nCells_); - #ifdef IS_MPI - for (vector::iterator j1 = cellListRow_[m1].begin(); - j1 != cellListRow_[m1].end(); ++j1) { - for (vector::iterator j2 = cellListCol_[m2].begin(); - j2 != cellListCol_[m2].end(); ++j2) { - - // In parallel, we need to visit *all* pairs of row - // & column indicies and will divide labor in the - // force evaluation later. - dr = cgColData.position[(*j2)] - cgRowData.position[(*j1)]; - snap_->wrapVector(dr); - cuts = getGroupCutoffs( (*j1), (*j2) ); - if (dr.lengthSquare() < cuts.third) { - neighborList.push_back(make_pair((*j1), (*j2))); - } - } - } + for (int j1 = 0; j1 < nGroupsInRow_; j1++) { + rs = cgRowData.position[j1]; #else - for (vector::iterator j1 = cellList_[m1].begin(); - j1 != cellList_[m1].end(); ++j1) { - for (vector::iterator j2 = cellList_[m2].begin(); - j2 != cellList_[m2].end(); ++j2) { - - // Always do this if we're in different cells or if - // we're in the same cell and the global index of - // the j2 cutoff group is greater than or equal to - // the j1 cutoff group. Note that Rappaport's code - // has a "less than" conditional here, but that - // deals with atom-by-atom computation. OpenMD - // allows atoms within a single cutoff group to - // interact with each other. + for (int j1 = 0; j1 < nGroups_; j1++) { + rs = snap_->cgData.position[j1]; +#endif + point[j1] = len; + + // scaled positions relative to the box vectors + scaled = invBox * rs; + + // wrap the vector back into the unit box by subtracting integer box + // numbers + for (int j = 0; j < 3; j++) { + scaled[j] -= roundMe(scaled[j]); + scaled[j] += 0.5; + // Handle the special case when an object is exactly on the + // boundary (a scaled coordinate of 1.0 is the same as + // scaled coordinate of 0.0) + if (scaled[j] >= 1.0) scaled[j] -= 1.0; + } + + // find xyz-indices of cell that cutoffGroup is in. + whichCell.x() = nCells_.x() * scaled.x(); + whichCell.y() = nCells_.y() * scaled.y(); + whichCell.z() = nCells_.z() * scaled.z(); + + // find single index of this cell: + int m1 = Vlinear(whichCell, nCells_); + for (vector::iterator os = cellOffsets_.begin(); + os != cellOffsets_.end(); ++os) { + + Vector3i m2v = whichCell + (*os); - if (m2 != m1 || (*j2) >= (*j1) ) { - - dr = snap_->cgData.position[(*j2)] - snap_->cgData.position[(*j1)]; - snap_->wrapVector(dr); - cuts = getGroupCutoffs( (*j1), (*j2) ); - if (dr.lengthSquare() < cuts.third) { - neighborList.push_back(make_pair((*j1), (*j2))); - } - } - } + if (m2v.x() >= nCells_.x()) { + m2v.x() = 0; + } else if (m2v.x() < 0) { + m2v.x() = nCells_.x() - 1; + } + + if (m2v.y() >= nCells_.y()) { + m2v.y() = 0; + } else if (m2v.y() < 0) { + m2v.y() = nCells_.y() - 1; + } + + if (m2v.z() >= nCells_.z()) { + m2v.z() = 0; + } else if (m2v.z() < 0) { + m2v.z() = nCells_.z() - 1; + } + int m2 = Vlinear (m2v, nCells_); +#ifdef IS_MPI + for (vector::iterator j2 = cellListCol_[m2].begin(); + j2 != cellListCol_[m2].end(); ++j2) { + + // In parallel, we need to visit *all* pairs of row + // & column indicies and will divide labor in the + // force evaluation later. + dr = cgColData.position[(*j2)] - rs; + if (usePeriodicBoundaryConditions_) { + snap_->wrapVector(dr); + } + if (dr.lengthSquare() < rListSq_) { + neighborList.push_back( (*j2) ); + ++len; + } + } +#else + for (vector::iterator j2 = cellList_[m2].begin(); + j2 != cellList_[m2].end(); ++j2) { + + // Always do this if we're in different cells or if + // we're in the same cell and the global index of + // the j2 cutoff group is greater than or equal to + // the j1 cutoff group. Note that Rappaport's code + // has a "less than" conditional here, but that + // deals with atom-by-atom computation. OpenMD + // allows atoms within a single cutoff group to + // interact with each other. + + if ( (*j2) >= j1 ) { + + dr = snap_->cgData.position[(*j2)] - rs; + if (usePeriodicBoundaryConditions_) { + snap_->wrapVector(dr); } -#endif + if ( dr.lengthSquare() < rListSq_) { + neighborList.push_back( (*j2) ); + ++len; + } } - } + } +#endif } - } + } } else { // branch to do all cutoff group pairs #ifdef IS_MPI for (int j1 = 0; j1 < nGroupsInRow_; j1++) { + point[j1] = len; + rs = cgRowData.position[j1]; for (int j2 = 0; j2 < nGroupsInCol_; j2++) { - dr = cgColData.position[j2] - cgRowData.position[j1]; - snap_->wrapVector(dr); - cuts = getGroupCutoffs( j1, j2 ); - if (dr.lengthSquare() < cuts.third) { - neighborList.push_back(make_pair(j1, j2)); + dr = cgColData.position[j2] - rs; + if (usePeriodicBoundaryConditions_) { + snap_->wrapVector(dr); } + if (dr.lengthSquare() < rListSq_) { + neighborList.push_back( j2 ); + ++len; + } } } #else // include all groups here. for (int j1 = 0; j1 < nGroups_; j1++) { + point[j1] = len; + rs = snap_->cgData.position[j1]; // include self group interactions j2 == j1 for (int j2 = j1; j2 < nGroups_; j2++) { - dr = snap_->cgData.position[j2] - snap_->cgData.position[j1]; - snap_->wrapVector(dr); - cuts = getGroupCutoffs( j1, j2 ); - if (dr.lengthSquare() < cuts.third) { - neighborList.push_back(make_pair(j1, j2)); + dr = snap_->cgData.position[j2] - rs; + if (usePeriodicBoundaryConditions_) { + snap_->wrapVector(dr); } + if (dr.lengthSquare() < rListSq_) { + neighborList.push_back( j2 ); + ++len; + } } } #endif } - + +#ifdef IS_MPI + point[nGroupsInRow_] = len; +#else + point[nGroups_] = len; +#endif + // save the local cutoff group positions for the check that is // done on each loop: saved_CG_positions_.clear(); + saved_CG_positions_.reserve(nGroups_); for (int i = 0; i < nGroups_; i++) saved_CG_positions_.push_back(snap_->cgData.position[i]); - - return neighborList; } } //end namespace OpenMD