--- trunk/src/parallel/ForceMatrixDecomposition.cpp 2013/07/01 21:09:37 1895 +++ trunk/src/parallel/ForceMatrixDecomposition.cpp 2015/03/09 17:10:26 2077 @@ -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,182 +302,9 @@ 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) @@ -559,6 +389,13 @@ namespace OpenMD { atomColData.electricField.end(), V3Zero); } + if (storageLayout_ & DataStorage::dslSitePotential) { + fill(atomRowData.sitePotential.begin(), + atomRowData.sitePotential.end(), 0.0); + fill(atomColData.sitePotential.begin(), + atomColData.sitePotential.end(), 0.0); + } + #endif // even in parallel, we need to zero out the local arrays: @@ -590,15 +427,25 @@ namespace OpenMD { if (storageLayout_ & DataStorage::dslElectricField) { 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); @@ -607,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); + } } @@ -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, @@ -701,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); @@ -722,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); @@ -805,6 +658,21 @@ namespace OpenMD { 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(); @@ -818,12 +686,12 @@ namespace OpenMD { AtomPlanPotRow->scatter(pot_row, pot_temp); AtomPlanPotRow->scatter(expot_row, expot_temp); - for (int ii = 0; ii < pot_temp.size(); ii++ ) + for (std::size_t ii = 0; ii < pot_temp.size(); ii++ ) pairwisePot += pot_temp[ii]; - for (int ii = 0; ii < expot_temp.size(); ii++ ) + for (std::size_t ii = 0; ii < expot_temp.size(); ii++ ) excludedPot += expot_temp[ii]; - + if (storageLayout_ & DataStorage::dslParticlePot) { // This is the pairwise contribution to the particle pot. The // embedding contribution is added in each of the low level @@ -846,12 +714,12 @@ namespace OpenMD { AtomPlanPotColumn->scatter(pot_col, pot_temp); AtomPlanPotColumn->scatter(expot_col, expot_temp); - for (int ii = 0; ii < pot_temp.size(); ii++ ) + for (std::size_t ii = 0; ii < pot_temp.size(); ii++ ) pairwisePot += pot_temp[ii]; - for (int ii = 0; ii < expot_temp.size(); ii++ ) + for (std::size_t ii = 0; ii < expot_temp.size(); ii++ ) excludedPot += expot_temp[ii]; - + if (storageLayout_ & DataStorage::dslParticlePot) { // This is the pairwise contribution to the particle pot. The // embedding contribution is added in each of the low level @@ -889,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 @@ -917,28 +785,27 @@ 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() { #ifdef IS_MPI return nAtomsInRow_; @@ -966,9 +833,10 @@ 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 @@ -998,8 +866,8 @@ namespace OpenMD { } - Vector3d ForceMatrixDecomposition::getAtomToGroupVectorRow(int atom1, int cg1){ - + Vector3d ForceMatrixDecomposition::getAtomToGroupVectorRow(int atom1, + int cg1) { Vector3d d; #ifdef IS_MPI @@ -1013,7 +881,8 @@ namespace OpenMD { return d; } - Vector3d ForceMatrixDecomposition::getAtomToGroupVectorColumn(int atom2, int cg2){ + Vector3d ForceMatrixDecomposition::getAtomToGroupVectorColumn(int atom2, + int cg2) { Vector3d d; #ifdef IS_MPI @@ -1044,7 +913,8 @@ namespace OpenMD { } - Vector3d ForceMatrixDecomposition::getInteratomicVector(int atom1, int atom2){ + Vector3d ForceMatrixDecomposition::getInteratomicVector(int atom1, + int atom2){ Vector3d d; #ifdef IS_MPI @@ -1066,7 +936,8 @@ 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) { + bool ForceMatrixDecomposition::skipAtomPair(int atom1, int atom2, + int cg1, int cg2) { int unique_id_1, unique_id_2; #ifdef IS_MPI @@ -1143,128 +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.atid1 = identsRow[atom1]; + 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]; - //idat.atypes = make_pair( ff_->getAtomType(identsRow[atom1]), - // ff_->getAtomType(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]); + if (storageLayout_ & DataStorage::dslFlucQPosition) { idat.flucQ2 = &(atomColData.flucQPos[atom2]); } -#else - - idat.atypes = make_pair( atypesLocal[atom1], atypesLocal[atom2]); - idat.atid1 = idents[atom1]; +#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::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 + } } - - 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); @@ -1284,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); @@ -1310,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 } @@ -1317,16 +1311,23 @@ 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. */ - void ForceMatrixDecomposition::buildNeighborList(vector >& neighborList) { - + void ForceMatrixDecomposition::buildNeighborList(vector& neighborList, + vector& point) { neighborList.clear(); - groupCutoffs cuts; + point.clear(); + int len = 0; + bool doAllPairs = false; - RealType rList_ = (largestRcut_ + skinThickness_); Snapshot* snap_ = sman_->getCurrentSnapshot(); Mat3x3d box; Mat3x3d invBox; @@ -1338,8 +1339,10 @@ namespace OpenMD { #ifdef IS_MPI cellListRow_.clear(); cellListCol_.clear(); + point.resize(nGroupsInRow_+1); #else cellList_.clear(); + point.resize(nGroups_+1); #endif if (!usePeriodicBoundaryConditions_) { @@ -1350,16 +1353,30 @@ namespace OpenMD { invBox = snap_->getInvHmat(); } - Vector3d boxX = box.getColumn(0); - Vector3d boxY = box.getColumn(1); - Vector3d boxZ = box.getColumn(2); + Vector3d A = box.getColumn(0); + Vector3d B = box.getColumn(1); + Vector3d C = box.getColumn(2); + + // Required for triclinic cells + Vector3d AxB = cross(A, B); + Vector3d BxC = cross(B, C); + Vector3d CxA = cross(C, A); + + // unit vectors perpendicular to the faces of the triclinic cell: + AxB.normalize(); + BxC.normalize(); + CxA.normalize(); + + // 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) ( boxX.length() )/ rList_; - nCells_.y() = (int) ( boxY.length() )/ rList_; - nCells_.z() = (int) ( boxZ.length() )/ rList_; + 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; @@ -1374,6 +1391,7 @@ namespace OpenMD { #endif if (!doAllPairs) { + #ifdef IS_MPI for (int i = 0; i < nGroupsInRow_; i++) { @@ -1432,7 +1450,7 @@ 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]; @@ -1452,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_); @@ -1465,126 +1483,149 @@ 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; - } +#ifdef IS_MPI + for (int j1 = 0; j1 < nGroupsInRow_; j1++) { + rs = cgRowData.position[j1]; +#else - int m2 = Vlinear (m2v, nCells_); + 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() = int(nCells_.x() * scaled.x()); + whichCell.y() = int(nCells_.y() * scaled.y()); + whichCell.z() = int(nCells_.z() * scaled.z()); + + for (vector::iterator os = cellOffsets_.begin(); + os != cellOffsets_.end(); ++os) { + Vector3i m2v = whichCell + (*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)]; - if (usePeriodicBoundaryConditions_) { - snap_->wrapVector(dr); - } - cuts = getGroupCutoffs( (*j1), (*j2) ); - if (dr.lengthSquare() < cuts.third) { - neighborList.push_back(make_pair((*j1), (*j2))); - } - } - } + 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 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. - - if (m2 != m1 || (*j2) >= (*j1) ) { - - dr = snap_->cgData.position[(*j2)] - snap_->cgData.position[(*j1)]; - if (usePeriodicBoundaryConditions_) { - snap_->wrapVector(dr); - } - cuts = getGroupCutoffs( (*j1), (*j2) ); - if (dr.lengthSquare() < cuts.third) { - neighborList.push_back(make_pair((*j1), (*j2))); - } - } - } + 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]; + dr = cgColData.position[j2] - rs; if (usePeriodicBoundaryConditions_) { snap_->wrapVector(dr); } - cuts = getGroupCutoffs( j1, j2 ); - if (dr.lengthSquare() < cuts.third) { - neighborList.push_back(make_pair(j1, j2)); + 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]; + dr = snap_->cgData.position[j2] - rs; if (usePeriodicBoundaryConditions_) { snap_->wrapVector(dr); } - cuts = getGroupCutoffs( j1, j2 ); - if (dr.lengthSquare() < cuts.third) { - neighborList.push_back(make_pair(j1, j2)); + 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]); }