--- branches/development/src/parallel/ForceMatrixDecomposition.cpp 2011/06/09 20:26:29 1579 +++ branches/development/src/parallel/ForceMatrixDecomposition.cpp 2011/07/11 01:39:49 1590 @@ -57,20 +57,21 @@ namespace OpenMD { storageLayout_ = sman_->getStorageLayout(); ff_ = info_->getForceField(); nLocal_ = snap_->getNumberOfAtoms(); - + nGroups_ = info_->getNLocalCutoffGroups(); - cerr << "in dId, nGroups = " << nGroups_ << "\n"; // gather the information for atomtype IDs (atids): - identsLocal = info_->getIdentArray(); + idents = info_->getIdentArray(); AtomLocalToGlobal = info_->getGlobalAtomIndices(); cgLocalToGlobal = info_->getGlobalGroupIndices(); vector globalGroupMembership = info_->getGlobalGroupMembership(); - vector massFactorsLocal = info_->getMassFactors(); - PairList excludes = info_->getExcludedInteractions(); - PairList oneTwo = info_->getOneTwoInteractions(); - PairList oneThree = info_->getOneThreeInteractions(); - PairList oneFour = info_->getOneFourInteractions(); + massFactors = info_->getMassFactors(); + + PairList* excludes = info_->getExcludedInteractions(); + PairList* oneTwo = info_->getOneTwoInteractions(); + PairList* oneThree = info_->getOneThreeInteractions(); + PairList* oneFour = info_->getOneFourInteractions(); + #ifdef IS_MPI AtomCommIntRow = new Communicator(nLocal_); @@ -108,17 +109,27 @@ namespace OpenMD { identsRow.resize(nAtomsInRow_); identsCol.resize(nAtomsInCol_); - AtomCommIntRow->gather(identsLocal, identsRow); - AtomCommIntColumn->gather(identsLocal, identsCol); + AtomCommIntRow->gather(idents, identsRow); + AtomCommIntColumn->gather(idents, identsCol); + // allocate memory for the parallel objects + AtomRowToGlobal.resize(nAtomsInRow_); + AtomColToGlobal.resize(nAtomsInCol_); + cgRowToGlobal.resize(nGroupsInRow_); + cgColToGlobal.resize(nGroupsInCol_); + massFactorsRow.resize(nAtomsInRow_); + massFactorsCol.resize(nAtomsInCol_); + pot_row.resize(nAtomsInRow_); + pot_col.resize(nAtomsInCol_); + AtomCommIntRow->gather(AtomLocalToGlobal, AtomRowToGlobal); AtomCommIntColumn->gather(AtomLocalToGlobal, AtomColToGlobal); cgCommIntRow->gather(cgLocalToGlobal, cgRowToGlobal); cgCommIntColumn->gather(cgLocalToGlobal, cgColToGlobal); - AtomCommRealRow->gather(massFactorsLocal, massFactorsRow); - AtomCommRealColumn->gather(massFactorsLocal, massFactorsCol); + AtomCommRealRow->gather(massFactors, massFactorsRow); + AtomCommRealColumn->gather(massFactors, massFactorsCol); groupListRow_.clear(); groupListRow_.resize(nGroupsInRow_); @@ -142,8 +153,8 @@ namespace OpenMD { } } - skipsForAtom.clear(); - skipsForAtom.resize(nAtomsInRow_); + excludesForAtom.clear(); + excludesForAtom.resize(nAtomsInRow_); toposForAtom.clear(); toposForAtom.resize(nAtomsInRow_); topoDist.clear(); @@ -154,18 +165,18 @@ namespace OpenMD { for (int j = 0; j < nAtomsInCol_; j++) { int jglob = AtomColToGlobal[j]; - if (excludes.hasPair(iglob, jglob)) - skipsForAtom[i].push_back(j); + if (excludes->hasPair(iglob, jglob)) + excludesForAtom[i].push_back(j); - if (oneTwo.hasPair(iglob, jglob)) { + if (oneTwo->hasPair(iglob, jglob)) { toposForAtom[i].push_back(j); topoDist[i].push_back(1); } else { - if (oneThree.hasPair(iglob, jglob)) { + if (oneThree->hasPair(iglob, jglob)) { toposForAtom[i].push_back(j); topoDist[i].push_back(2); } else { - if (oneFour.hasPair(iglob, jglob)) { + if (oneFour->hasPair(iglob, jglob)) { toposForAtom[i].push_back(j); topoDist[i].push_back(3); } @@ -188,8 +199,8 @@ namespace OpenMD { } } - skipsForAtom.clear(); - skipsForAtom.resize(nLocal_); + excludesForAtom.clear(); + excludesForAtom.resize(nLocal_); toposForAtom.clear(); toposForAtom.resize(nLocal_); topoDist.clear(); @@ -201,18 +212,18 @@ namespace OpenMD { for (int j = 0; j < nLocal_; j++) { int jglob = AtomLocalToGlobal[j]; - if (excludes.hasPair(iglob, jglob)) - skipsForAtom[i].push_back(j); + if (excludes->hasPair(iglob, jglob)) + excludesForAtom[i].push_back(j); - if (oneTwo.hasPair(iglob, jglob)) { + if (oneTwo->hasPair(iglob, jglob)) { toposForAtom[i].push_back(j); topoDist[i].push_back(1); } else { - if (oneThree.hasPair(iglob, jglob)) { + if (oneThree->hasPair(iglob, jglob)) { toposForAtom[i].push_back(j); topoDist[i].push_back(2); } else { - if (oneFour.hasPair(iglob, jglob)) { + if (oneFour->hasPair(iglob, jglob)) { toposForAtom[i].push_back(j); topoDist[i].push_back(3); } @@ -222,26 +233,27 @@ namespace OpenMD { } createGtypeCutoffMap(); + } void ForceMatrixDecomposition::createGtypeCutoffMap() { - + RealType tol = 1e-6; RealType rc; int atid; set atypes = info_->getSimulatedAtomTypes(); - vector atypeCutoff; - atypeCutoff.resize( atypes.size() ); - + map atypeCutoff; + for (set::iterator at = atypes.begin(); at != atypes.end(); ++at){ - rc = interactionMan_->getSuggestedCutoffRadius(*at); atid = (*at)->getIdent(); - atypeCutoff[atid] = rc; + 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 @@ -299,8 +311,6 @@ namespace OpenMD { vector groupCutoff(nGroups_, 0.0); groupToGtype.resize(nGroups_); - - cerr << "nGroups = " << nGroups_ << "\n"; for (int cg1 = 0; cg1 < nGroups_; cg1++) { groupCutoff[cg1] = 0.0; @@ -309,7 +319,7 @@ namespace OpenMD { for (vector::iterator ia = atomList.begin(); ia != atomList.end(); ++ia) { int atom1 = (*ia); - atid = identsLocal[atom1]; + atid = idents[atom1]; if (atypeCutoff[atid] > groupCutoff[cg1]) { groupCutoff[cg1] = atypeCutoff[atid]; } @@ -329,13 +339,14 @@ namespace OpenMD { } #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()); + RealType groupMax = *max_element(gTypeCutoffs.begin(), + gTypeCutoffs.end()); #ifdef IS_MPI - MPI::COMM_WORLD.Allreduce(&groupMax, &groupMax, 1, MPI::REALTYPE, MPI::MAX); + MPI::COMM_WORLD.Allreduce(&groupMax, &groupMax, 1, MPI::REALTYPE, + MPI::MAX); #endif RealType tradRcut = groupMax; @@ -378,7 +389,7 @@ namespace OpenMD { if (abs(gTypeCutoffMap[key].first - userCutoff_) > 0.0001) { sprintf(painCave.errMsg, "ForceMatrixDecomposition::createGtypeCutoffMap " - "user-specified rCut does not match computed group Cutoff\n"); + "user-specified rCut (%lf) does not match computed group Cutoff\n", userCutoff_); painCave.severity = OPENMD_ERROR; painCave.isFatal = 1; simError(); @@ -410,11 +421,9 @@ namespace OpenMD { } void ForceMatrixDecomposition::zeroWorkArrays() { + pairwisePot = 0.0; + embeddingPot = 0.0; - for (int j = 0; j < N_INTERACTION_FAMILIES; j++) { - longRangePot_[j] = 0.0; - } - #ifdef IS_MPI if (storageLayout_ & DataStorage::dslForce) { fill(atomRowData.force.begin(), atomRowData.force.end(), V3Zero); @@ -430,13 +439,13 @@ namespace OpenMD { Vector (0.0)); fill(pot_col.begin(), pot_col.end(), - Vector (0.0)); - - pot_local = Vector(0.0); + Vector (0.0)); if (storageLayout_ & DataStorage::dslParticlePot) { - fill(atomRowData.particlePot.begin(), atomRowData.particlePot.end(), 0.0); - fill(atomColData.particlePot.begin(), atomColData.particlePot.end(), 0.0); + fill(atomRowData.particlePot.begin(), atomRowData.particlePot.end(), + 0.0); + fill(atomColData.particlePot.begin(), atomColData.particlePot.end(), + 0.0); } if (storageLayout_ & DataStorage::dslDensity) { @@ -445,8 +454,10 @@ namespace OpenMD { } if (storageLayout_ & DataStorage::dslFunctional) { - fill(atomRowData.functional.begin(), atomRowData.functional.end(), 0.0); - fill(atomColData.functional.begin(), atomColData.functional.end(), 0.0); + fill(atomRowData.functional.begin(), atomRowData.functional.end(), + 0.0); + fill(atomColData.functional.begin(), atomColData.functional.end(), + 0.0); } if (storageLayout_ & DataStorage::dslFunctionalDerivative) { @@ -456,8 +467,16 @@ namespace OpenMD { atomColData.functionalDerivative.end(), 0.0); } -#else - + if (storageLayout_ & DataStorage::dslSkippedCharge) { + fill(atomRowData.skippedCharge.begin(), + atomRowData.skippedCharge.end(), 0.0); + fill(atomColData.skippedCharge.begin(), + atomColData.skippedCharge.end(), 0.0); + } + +#endif + // even in parallel, we need to zero out the local arrays: + if (storageLayout_ & DataStorage::dslParticlePot) { fill(snap_->atomData.particlePot.begin(), snap_->atomData.particlePot.end(), 0.0); @@ -475,7 +494,10 @@ namespace OpenMD { fill(snap_->atomData.functionalDerivative.begin(), snap_->atomData.functionalDerivative.end(), 0.0); } -#endif + if (storageLayout_ & DataStorage::dslSkippedCharge) { + fill(snap_->atomData.skippedCharge.begin(), + snap_->atomData.skippedCharge.end(), 0.0); + } } @@ -512,6 +534,7 @@ namespace OpenMD { AtomCommMatrixColumn->gather(snap_->atomData.electroFrame, atomColData.electroFrame); } + #endif } @@ -578,22 +601,37 @@ namespace OpenMD { AtomCommVectorColumn->scatter(atomColData.force, frc_tmp); for (int i = 0; i < n; i++) snap_->atomData.force[i] += frc_tmp[i]; - - + if (storageLayout_ & DataStorage::dslTorque) { - int nt = snap_->atomData.force.size(); + int nt = snap_->atomData.torque.size(); vector trq_tmp(nt, V3Zero); AtomCommVectorRow->scatter(atomRowData.torque, trq_tmp); - for (int i = 0; i < n; i++) { + for (int i = 0; i < nt; i++) { snap_->atomData.torque[i] += trq_tmp[i]; trq_tmp[i] = 0.0; } AtomCommVectorColumn->scatter(atomColData.torque, trq_tmp); - for (int i = 0; i < n; i++) + for (int i = 0; i < nt; i++) snap_->atomData.torque[i] += trq_tmp[i]; + } + + if (storageLayout_ & DataStorage::dslSkippedCharge) { + + int ns = snap_->atomData.skippedCharge.size(); + vector skch_tmp(ns, 0.0); + + AtomCommRealRow->scatter(atomRowData.skippedCharge, skch_tmp); + for (int i = 0; i < ns; i++) { + snap_->atomData.skippedCharge[i] += skch_tmp[i]; + skch_tmp[i] = 0.0; + } + + AtomCommRealColumn->scatter(atomColData.skippedCharge, skch_tmp); + for (int i = 0; i < ns; i++) + snap_->atomData.skippedCharge[i] += skch_tmp[i]; } nLocal_ = snap_->getNumberOfAtoms(); @@ -606,7 +644,7 @@ namespace OpenMD { AtomCommPotRow->scatter(pot_row, pot_temp); for (int ii = 0; ii < pot_temp.size(); ii++ ) - pot_local += pot_temp[ii]; + pairwisePot += pot_temp[ii]; fill(pot_temp.begin(), pot_temp.end(), Vector (0.0)); @@ -614,9 +652,9 @@ namespace OpenMD { AtomCommPotColumn->scatter(pot_col, pot_temp); for (int ii = 0; ii < pot_temp.size(); ii++ ) - pot_local += pot_temp[ii]; - + pairwisePot += pot_temp[ii]; #endif + } int ForceMatrixDecomposition::getNAtomsInRow() { @@ -691,7 +729,7 @@ namespace OpenMD { #ifdef IS_MPI return massFactorsRow[atom1]; #else - return massFactorsLocal[atom1]; + return massFactors[atom1]; #endif } @@ -699,7 +737,7 @@ namespace OpenMD { #ifdef IS_MPI return massFactorsCol[atom2]; #else - return massFactorsLocal[atom2]; + return massFactors[atom2]; #endif } @@ -717,15 +755,12 @@ namespace OpenMD { return d; } - vector ForceMatrixDecomposition::getSkipsForAtom(int atom1) { - return skipsForAtom[atom1]; + vector ForceMatrixDecomposition::getExcludesForAtom(int atom1) { + return excludesForAtom[atom1]; } /** - * There are a number of reasons to skip a pair or a - * particle. Mostly we do this to exclude atoms who are involved in - * short range interactions (bonds, bends, torsions), but we also - * need to exclude some overcounted interactions that result from + * We need to exclude some overcounted interactions that result from * the parallel decomposition. */ bool ForceMatrixDecomposition::skipAtomPair(int atom1, int atom2) { @@ -745,17 +780,36 @@ namespace OpenMD { } else { if ((unique_id_1 + unique_id_2) % 2 == 1) return true; } +#endif + return false; + } + + /** + * We need to handle the interactions for atoms who are involved in + * the same rigid body as well as some short range interactions + * (bonds, bends, torsions) differently from other interactions. + * We'll still visit the pairwise routines, but with a flag that + * tells those routines to exclude the pair from direct long range + * interactions. Some indirect interactions (notably reaction + * field) must still be handled for these pairs. + */ + bool ForceMatrixDecomposition::excludeAtomPair(int atom1, int atom2) { + int unique_id_2; + +#ifdef IS_MPI + // in MPI, we have to look up the unique IDs for the row atom. + unique_id_2 = AtomColToGlobal[atom2]; #else // in the normal loop, the atom numbers are unique - unique_id_1 = atom1; unique_id_2 = atom2; #endif - for (vector::iterator i = skipsForAtom[atom1].begin(); - i != skipsForAtom[atom1].end(); ++i) { + for (vector::iterator i = excludesForAtom[atom1].begin(); + i != excludesForAtom[atom1].end(); ++i) { if ( (*i) == unique_id_2 ) return true; - } + } + return false; } @@ -776,14 +830,15 @@ namespace OpenMD { } // filling interaction blocks with pointers - InteractionData ForceMatrixDecomposition::fillInteractionData(int atom1, int atom2) { - InteractionData idat; + void ForceMatrixDecomposition::fillInteractionData(InteractionData &idat, + int atom1, int atom2) { + idat.excluded = excludeAtomPair(atom1, atom2); + #ifdef IS_MPI idat.atypes = make_pair( ff_->getAtomType(identsRow[atom1]), ff_->getAtomType(identsCol[atom2]) ); - if (storageLayout_ & DataStorage::dslAmat) { idat.A1 = &(atomRowData.aMat[atom1]); @@ -820,10 +875,15 @@ namespace OpenMD { idat.particlePot2 = &(atomColData.particlePot[atom2]); } + if (storageLayout_ & DataStorage::dslSkippedCharge) { + idat.skippedCharge1 = &(atomRowData.skippedCharge[atom1]); + idat.skippedCharge2 = &(atomColData.skippedCharge[atom2]); + } + #else - idat.atypes = make_pair( ff_->getAtomType(identsLocal[atom1]), - ff_->getAtomType(identsLocal[atom2]) ); + idat.atypes = make_pair( ff_->getAtomType(idents[atom1]), + ff_->getAtomType(idents[atom2]) ); if (storageLayout_ & DataStorage::dslAmat) { idat.A1 = &(snap_->atomData.aMat[atom1]); @@ -840,7 +900,7 @@ namespace OpenMD { idat.t2 = &(snap_->atomData.torque[atom2]); } - if (storageLayout_ & DataStorage::dslDensity) { + if (storageLayout_ & DataStorage::dslDensity) { idat.rho1 = &(snap_->atomData.density[atom1]); idat.rho2 = &(snap_->atomData.density[atom2]); } @@ -860,12 +920,15 @@ namespace OpenMD { idat.particlePot2 = &(snap_->atomData.particlePot[atom2]); } + if (storageLayout_ & DataStorage::dslSkippedCharge) { + idat.skippedCharge1 = &(snap_->atomData.skippedCharge[atom1]); + idat.skippedCharge2 = &(snap_->atomData.skippedCharge[atom2]); + } #endif - return idat; } - void ForceMatrixDecomposition::unpackInteractionData(InteractionData idat, int atom1, int atom2) { + void ForceMatrixDecomposition::unpackInteractionData(InteractionData &idat, int atom1, int atom2) { #ifdef IS_MPI pot_row[atom1] += 0.5 * *(idat.pot); pot_col[atom2] += 0.5 * *(idat.pot); @@ -873,45 +936,14 @@ namespace OpenMD { atomRowData.force[atom1] += *(idat.f1); atomColData.force[atom2] -= *(idat.f1); #else - longRangePot_ += *(idat.pot); - + pairwisePot += *(idat.pot); + snap_->atomData.force[atom1] += *(idat.f1); snap_->atomData.force[atom2] -= *(idat.f1); #endif - + } - - InteractionData ForceMatrixDecomposition::fillSkipData(int atom1, int atom2){ - - InteractionData idat; -#ifdef IS_MPI - idat.atypes = make_pair( ff_->getAtomType(identsRow[atom1]), - ff_->getAtomType(identsCol[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]); - } -#else - idat.atypes = make_pair( ff_->getAtomType(identsLocal[atom1]), - ff_->getAtomType(identsLocal[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]); - } -#endif - } - /* * buildNeighborList * @@ -922,6 +954,8 @@ namespace OpenMD { vector > neighborList; groupCutoffs cuts; + bool doAllPairs = false; + #ifdef IS_MPI cellListRow_.clear(); cellListCol_.clear(); @@ -941,6 +975,12 @@ namespace OpenMD { nCells_.y() = (int) ( Hy.length() )/ rList_; nCells_.z() = (int) ( Hz.length() )/ 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; @@ -954,151 +994,195 @@ namespace OpenMD { 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; - // 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]); - - // 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: - cellIndex = Vlinear(whichCell, nCells_); - // add this cutoff group to the list of groups in this cell; - cellListRow_[cellIndex].push_back(i); - } - - for (int i = 0; i < nGroupsInCol_; i++) { - rs = cgColData.position[i]; - // scaled positions relative to the box vectors - scaled = invHmat * 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]); - - // 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: - cellIndex = Vlinear(whichCell, nCells_); - // add this cutoff group to the list of groups in this cell; - cellListCol_[cellIndex].push_back(i); - } + + for (int i = 0; i < nGroupsInRow_; i++) { + rs = cgRowData.position[i]; + + // scaled positions relative to the box vectors + scaled = invHmat * 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; + } + + // 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: + cellIndex = Vlinear(whichCell, nCells_); + + // add this cutoff group to the list of groups in this cell; + cellListRow_[cellIndex].push_back(i); + } + + for (int i = 0; i < nGroupsInCol_; i++) { + rs = cgColData.position[i]; + + // scaled positions relative to the box vectors + scaled = invHmat * 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; + } + + // 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: + cellIndex = Vlinear(whichCell, nCells_); + + // 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; - // 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]); - - // 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: - cellIndex = Vlinear(whichCell, nCells_); - // add this cutoff group to the list of groups in this cell; - cellList_[cellIndex].push_back(i); - } + for (int i = 0; i < nGroups_; i++) { + rs = snap_->cgData.position[i]; + + // scaled positions relative to the box vectors + scaled = invHmat * 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; + } + + // 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: + cellIndex = Vlinear(whichCell, nCells_); + + // add this cutoff group to the list of groups in this cell; + cellList_[cellIndex].push_back(i); + } #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) { + 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_); - 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_); - + 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) { - - // 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 less than the j1 cutoff group - - if (m2 != m1 || cgColToGlobal[(*j2)] < cgRowToGlobal[(*j1)]) { - 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 (vector::iterator j1 = cellListRow_[m1].begin(); + j1 != cellListRow_[m1].end(); ++j1) { + for (vector::iterator j2 = cellListCol_[m2].begin(); + j2 != cellListCol_[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 less than the j1 cutoff group + + if (m2 != m1 || cgColToGlobal[(*j2)] < cgRowToGlobal[(*j1)]) { + 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))); + } } } } - } #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 less than the j1 cutoff group - - 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))); + + 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 less than the j1 cutoff group + + 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))); + } } } } - } #endif + } } } } + } else { + // branch to do all cutoff group pairs +#ifdef IS_MPI + for (int j1 = 0; j1 < nGroupsInRow_; 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)); + } + } + } +#else + for (int j1 = 0; j1 < nGroups_ - 1; j1++) { + for (int j2 = j1 + 1; 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)); + } + } + } +#endif } - + // save the local cutoff group positions for the check that is // done on each loop: saved_CG_positions_.clear(); for (int i = 0; i < nGroups_; i++) saved_CG_positions_.push_back(snap_->cgData.position[i]); - + return neighborList; } } //end namespace OpenMD