--- branches/development/src/parallel/ForceDecomposition.cpp 2011/01/17 21:34:36 1540 +++ branches/development/src/parallel/ForceMatrixDecomposition.cpp 2011/05/27 16:45:44 1571 @@ -38,140 +38,790 @@ * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). * [4] Vardeman & Gezelter, in progress (2009). */ -#include "parallel/ForceDecomposition.hpp" -#include "parallel/Communicator.hpp" +#include "parallel/ForceMatrixDecomposition.hpp" #include "math/SquareMatrix3.hpp" +#include "nonbonded/NonBondedInteraction.hpp" +#include "brains/SnapshotManager.hpp" +#include "brains/PairList.hpp" +using namespace std; namespace OpenMD { - void ForceDecomposition::distributeInitialData() { + /** + * distributeInitialData is essentially a copy of the older fortran + * SimulationSetup + */ + + void ForceMatrixDecomposition::distributeInitialData() { + snap_ = sman_->getCurrentSnapshot(); + storageLayout_ = sman_->getStorageLayout(); + ff_ = info_->getForceField(); + nLocal_ = snap_->getNumberOfAtoms(); + nGroups_ = snap_->getNumberOfCutoffGroups(); + + // gather the information for atomtype IDs (atids): + identsLocal = 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(); + vector pot_local(N_INTERACTION_FAMILIES, 0.0); + #ifdef IS_MPI + + AtomCommIntRow = new Communicator(nLocal_); + AtomCommRealRow = new Communicator(nLocal_); + AtomCommVectorRow = new Communicator(nLocal_); + AtomCommMatrixRow = new Communicator(nLocal_); - int nAtoms; - int nGroups; + AtomCommIntColumn = new Communicator(nLocal_); + AtomCommRealColumn = new Communicator(nLocal_); + AtomCommVectorColumn = new Communicator(nLocal_); + AtomCommMatrixColumn = new Communicator(nLocal_); - AtomCommRealI = new Communicator(nAtoms); - AtomCommVectorI = new Communicator(nAtoms); - AtomCommMatrixI = new Communicator(nAtoms); + cgCommIntRow = new Communicator(nGroups_); + cgCommVectorRow = new Communicator(nGroups_); + cgCommIntColumn = new Communicator(nGroups_); + cgCommVectorColumn = new Communicator(nGroups_); - AtomCommRealJ = new Communicator(nAtoms); - AtomCommVectorJ = new Communicator(nAtoms); - AtomCommMatrixJ = new Communicator(nAtoms); + nAtomsInRow_ = AtomCommIntRow->getSize(); + nAtomsInCol_ = AtomCommIntColumn->getSize(); + nGroupsInRow_ = cgCommIntRow->getSize(); + nGroupsInCol_ = cgCommIntColumn->getSize(); - cgCommVectorI = new Communicator(nGroups); - cgCommVectorJ = new Communicator(nGroups); - // more to come -#endif - } + // Modify the data storage objects with the correct layouts and sizes: + atomRowData.resize(nAtomsInRow_); + atomRowData.setStorageLayout(storageLayout_); + atomColData.resize(nAtomsInCol_); + atomColData.setStorageLayout(storageLayout_); + cgRowData.resize(nGroupsInRow_); + cgRowData.setStorageLayout(DataStorage::dslPosition); + cgColData.resize(nGroupsInCol_); + cgColData.setStorageLayout(DataStorage::dslPosition); + vector > pot_row(N_INTERACTION_FAMILIES, + vector (nAtomsInRow_, 0.0)); + vector > pot_col(N_INTERACTION_FAMILIES, + vector (nAtomsInCol_, 0.0)); + + identsRow.reserve(nAtomsInRow_); + identsCol.reserve(nAtomsInCol_); + + AtomCommIntRow->gather(identsLocal, identsRow); + AtomCommIntColumn->gather(identsLocal, identsCol); + + AtomCommIntRow->gather(AtomLocalToGlobal, AtomRowToGlobal); + AtomCommIntColumn->gather(AtomLocalToGlobal, AtomColToGlobal); + + cgCommIntRow->gather(cgLocalToGlobal, cgRowToGlobal); + cgCommIntColumn->gather(cgLocalToGlobal, cgColToGlobal); + AtomCommRealRow->gather(massFactorsLocal, massFactorsRow); + AtomCommRealColumn->gather(massFactorsLocal, massFactorsCol); - void ForceDecomposition::distributeData() { + groupListRow_.clear(); + groupListRow_.reserve(nGroupsInRow_); + for (int i = 0; i < nGroupsInRow_; i++) { + int gid = cgRowToGlobal[i]; + for (int j = 0; j < nAtomsInRow_; j++) { + int aid = AtomRowToGlobal[j]; + if (globalGroupMembership[aid] == gid) + groupListRow_[i].push_back(j); + } + } + + groupListCol_.clear(); + groupListCol_.reserve(nGroupsInCol_); + for (int i = 0; i < nGroupsInCol_; i++) { + int gid = cgColToGlobal[i]; + for (int j = 0; j < nAtomsInCol_; j++) { + int aid = AtomColToGlobal[j]; + if (globalGroupMembership[aid] == gid) + groupListCol_[i].push_back(j); + } + } + + skipsForRowAtom.clear(); + skipsForRowAtom.reserve(nAtomsInRow_); + for (int i = 0; i < nAtomsInRow_; i++) { + int iglob = AtomRowToGlobal[i]; + for (int j = 0; j < nAtomsInCol_; j++) { + int jglob = AtomColToGlobal[j]; + if (excludes.hasPair(iglob, jglob)) + skipsForRowAtom[i].push_back(j); + } + } + + toposForRowAtom.clear(); + toposForRowAtom.reserve(nAtomsInRow_); + for (int i = 0; i < nAtomsInRow_; i++) { + int iglob = AtomRowToGlobal[i]; + int nTopos = 0; + for (int j = 0; j < nAtomsInCol_; j++) { + int jglob = AtomColToGlobal[j]; + if (oneTwo.hasPair(iglob, jglob)) { + toposForRowAtom[i].push_back(j); + topoDistRow[i][nTopos] = 1; + nTopos++; + } + if (oneThree.hasPair(iglob, jglob)) { + toposForRowAtom[i].push_back(j); + topoDistRow[i][nTopos] = 2; + nTopos++; + } + if (oneFour.hasPair(iglob, jglob)) { + toposForRowAtom[i].push_back(j); + topoDistRow[i][nTopos] = 3; + nTopos++; + } + } + } + +#endif + + groupList_.clear(); + groupList_.reserve(nGroups_); + for (int i = 0; i < nGroups_; i++) { + int gid = cgLocalToGlobal[i]; + for (int j = 0; j < nLocal_; j++) { + int aid = AtomLocalToGlobal[j]; + if (globalGroupMembership[aid] == gid) + groupList_[i].push_back(j); + } + } + + skipsForLocalAtom.clear(); + skipsForLocalAtom.reserve(nLocal_); + + for (int i = 0; i < nLocal_; i++) { + int iglob = AtomLocalToGlobal[i]; + for (int j = 0; j < nLocal_; j++) { + int jglob = AtomLocalToGlobal[j]; + if (excludes.hasPair(iglob, jglob)) + skipsForLocalAtom[i].push_back(j); + } + } + + toposForLocalAtom.clear(); + toposForLocalAtom.reserve(nLocal_); + for (int i = 0; i < nLocal_; i++) { + int iglob = AtomLocalToGlobal[i]; + int nTopos = 0; + for (int j = 0; j < nLocal_; j++) { + int jglob = AtomLocalToGlobal[j]; + if (oneTwo.hasPair(iglob, jglob)) { + toposForLocalAtom[i].push_back(j); + topoDistLocal[i][nTopos] = 1; + nTopos++; + } + if (oneThree.hasPair(iglob, jglob)) { + toposForLocalAtom[i].push_back(j); + topoDistLocal[i][nTopos] = 2; + nTopos++; + } + if (oneFour.hasPair(iglob, jglob)) { + toposForLocalAtom[i].push_back(j); + topoDistLocal[i][nTopos] = 3; + nTopos++; + } + } + } + } + + void ForceMatrixDecomposition::distributeData() { + snap_ = sman_->getCurrentSnapshot(); + storageLayout_ = sman_->getStorageLayout(); #ifdef IS_MPI - Snapshot* snap = sman_->getCurrentSnapshot(); // gather up the atomic positions - AtomCommVectorI->gather(snap->atomData.position, - snap->atomIData.position); - AtomCommVectorJ->gather(snap->atomData.position, - snap->atomJData.position); + AtomCommVectorRow->gather(snap_->atomData.position, + atomRowData.position); + AtomCommVectorColumn->gather(snap_->atomData.position, + atomColData.position); // gather up the cutoff group positions - cgCommVectorI->gather(snap->cgData.position, - snap->cgIData.position); - cgCommVectorJ->gather(snap->cgData.position, - snap->cgJData.position); + cgCommVectorRow->gather(snap_->cgData.position, + cgRowData.position); + cgCommVectorColumn->gather(snap_->cgData.position, + cgColData.position); // if needed, gather the atomic rotation matrices - if (snap->atomData.getStorageLayout() & DataStorage::dslAmat) { - AtomCommMatrixI->gather(snap->atomData.aMat, - snap->atomIData.aMat); - AtomCommMatrixJ->gather(snap->atomData.aMat, - snap->atomJData.aMat); + if (storageLayout_ & DataStorage::dslAmat) { + AtomCommMatrixRow->gather(snap_->atomData.aMat, + atomRowData.aMat); + AtomCommMatrixColumn->gather(snap_->atomData.aMat, + atomColData.aMat); } // if needed, gather the atomic eletrostatic frames - if (snap->atomData.getStorageLayout() & DataStorage::dslElectroFrame) { - AtomCommMatrixI->gather(snap->atomData.electroFrame, - snap->atomIData.electroFrame); - AtomCommMatrixJ->gather(snap->atomData.electroFrame, - snap->atomJData.electroFrame); + if (storageLayout_ & DataStorage::dslElectroFrame) { + AtomCommMatrixRow->gather(snap_->atomData.electroFrame, + atomRowData.electroFrame); + AtomCommMatrixColumn->gather(snap_->atomData.electroFrame, + atomColData.electroFrame); } #endif } - void ForceDecomposition::collectIntermediateData() { + void ForceMatrixDecomposition::collectIntermediateData() { + snap_ = sman_->getCurrentSnapshot(); + storageLayout_ = sman_->getStorageLayout(); #ifdef IS_MPI - Snapshot* snap = sman_->getCurrentSnapshot(); - if (snap->atomData.getStorageLayout() & DataStorage::dslDensity) { - AtomCommRealI->scatter(snap->atomIData.density, - snap->atomData.density); - std::vector rho_tmp; - int n = snap->getNumberOfAtoms(); - rho_tmp.reserve( n ); - AtomCommRealJ->scatter(snap->atomJData.density, rho_tmp); + if (storageLayout_ & DataStorage::dslDensity) { + + AtomCommRealRow->scatter(atomRowData.density, + snap_->atomData.density); + + int n = snap_->atomData.density.size(); + std::vector rho_tmp(n, 0.0); + AtomCommRealColumn->scatter(atomColData.density, rho_tmp); for (int i = 0; i < n; i++) - snap->atomData.density[i] += rho_tmp[i]; + snap_->atomData.density[i] += rho_tmp[i]; } #endif } - void ForceDecomposition::distributeIntermediateData() { + void ForceMatrixDecomposition::distributeIntermediateData() { + snap_ = sman_->getCurrentSnapshot(); + storageLayout_ = sman_->getStorageLayout(); #ifdef IS_MPI - Snapshot* snap = sman_->getCurrentSnapshot(); - if (snap->atomData.getStorageLayout() & DataStorage::dslFunctional) { - AtomCommRealI->gather(snap->atomData.functional, - snap->atomIData.functional); - AtomCommRealJ->gather(snap->atomData.functional, - snap->atomJData.functional); + if (storageLayout_ & DataStorage::dslFunctional) { + AtomCommRealRow->gather(snap_->atomData.functional, + atomRowData.functional); + AtomCommRealColumn->gather(snap_->atomData.functional, + atomColData.functional); } - if (snap->atomData.getStorageLayout() & DataStorage::dslFunctionalDerivative) { - AtomCommRealI->gather(snap->atomData.functionalDerivative, - snap->atomIData.functionalDerivative); - AtomCommRealJ->gather(snap->atomData.functionalDerivative, - snap->atomJData.functionalDerivative); + if (storageLayout_ & DataStorage::dslFunctionalDerivative) { + AtomCommRealRow->gather(snap_->atomData.functionalDerivative, + atomRowData.functionalDerivative); + AtomCommRealColumn->gather(snap_->atomData.functionalDerivative, + atomColData.functionalDerivative); } #endif } - void ForceDecomposition::collectData() { -#ifdef IS_MPI - Snapshot* snap = sman_->getCurrentSnapshot(); - int n = snap->getNumberOfAtoms(); - - std::vector frc_tmp; - frc_tmp.reserve( n ); + void ForceMatrixDecomposition::collectData() { + snap_ = sman_->getCurrentSnapshot(); + storageLayout_ = sman_->getStorageLayout(); +#ifdef IS_MPI + int n = snap_->atomData.force.size(); + vector frc_tmp(n, V3Zero); - AtomCommVectorI->scatter(snap->atomIData.force, frc_tmp); - for (int i = 0; i < n; i++) - snap->atomData.force[i] += frc_tmp[i]; + AtomCommVectorRow->scatter(atomRowData.force, frc_tmp); + for (int i = 0; i < n; i++) { + snap_->atomData.force[i] += frc_tmp[i]; + frc_tmp[i] = 0.0; + } - AtomCommVectorJ->scatter(snap->atomJData.force, frc_tmp); + AtomCommVectorColumn->scatter(atomColData.force, frc_tmp); for (int i = 0; i < n; i++) - snap->atomData.force[i] += frc_tmp[i]; + snap_->atomData.force[i] += frc_tmp[i]; - if (snap->atomData.getStorageLayout() & DataStorage::dslTorque) { - std::vector trq_tmp; - trq_tmp.reserve( n ); + if (storageLayout_ & DataStorage::dslTorque) { + + int nt = snap_->atomData.force.size(); + vector trq_tmp(nt, V3Zero); + + AtomCommVectorRow->scatter(atomRowData.torque, trq_tmp); + for (int i = 0; i < n; i++) { + snap_->atomData.torque[i] += trq_tmp[i]; + trq_tmp[i] = 0.0; + } - AtomCommVectorI->scatter(snap->atomIData.torque, trq_tmp); + AtomCommVectorColumn->scatter(atomColData.torque, trq_tmp); for (int i = 0; i < n; i++) - snap->atomData.torque[i] += trq_tmp[i]; - - AtomCommVectorJ->scatter(snap->atomJData.torque, trq_tmp); - for (int i = 0; i < n; i++) - snap->atomData.torque[i] += trq_tmp[i]; + snap_->atomData.torque[i] += trq_tmp[i]; } - // Still need pot! + nLocal_ = snap_->getNumberOfAtoms(); + + vector > pot_temp(N_INTERACTION_FAMILIES, + vector (nLocal_, 0.0)); + for (int i = 0; i < N_INTERACTION_FAMILIES; i++) { + AtomCommRealRow->scatter(pot_row[i], pot_temp[i]); + for (int ii = 0; ii < pot_temp[i].size(); ii++ ) { + pot_local[i] += pot_temp[i][ii]; + } + } +#endif + } + int ForceMatrixDecomposition::getNAtomsInRow() { +#ifdef IS_MPI + return nAtomsInRow_; +#else + return nLocal_; +#endif + } + /** + * returns the list of atoms belonging to this group. + */ + vector ForceMatrixDecomposition::getAtomsInGroupRow(int cg1){ +#ifdef IS_MPI + return groupListRow_[cg1]; +#else + return groupList_[cg1]; #endif } + + vector ForceMatrixDecomposition::getAtomsInGroupColumn(int cg2){ +#ifdef IS_MPI + return groupListCol_[cg2]; +#else + return groupList_[cg2]; +#endif + } + 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); + return d; + } + + + Vector3d ForceMatrixDecomposition::getAtomToGroupVectorRow(int atom1, int cg1){ + + Vector3d d; + +#ifdef IS_MPI + d = cgRowData.position[cg1] - atomRowData.position[atom1]; +#else + d = snap_->cgData.position[cg1] - snap_->atomData.position[atom1]; +#endif + + snap_->wrapVector(d); + return d; + } + + Vector3d ForceMatrixDecomposition::getAtomToGroupVectorColumn(int atom2, int cg2){ + Vector3d d; + +#ifdef IS_MPI + d = cgColData.position[cg2] - atomColData.position[atom2]; +#else + d = snap_->cgData.position[cg2] - snap_->atomData.position[atom2]; +#endif + + snap_->wrapVector(d); + return d; + } + + RealType ForceMatrixDecomposition::getMassFactorRow(int atom1) { +#ifdef IS_MPI + return massFactorsRow[atom1]; +#else + return massFactorsLocal[atom1]; +#endif + } + + RealType ForceMatrixDecomposition::getMassFactorColumn(int atom2) { +#ifdef IS_MPI + return massFactorsCol[atom2]; +#else + return massFactorsLocal[atom2]; +#endif + + } + + Vector3d ForceMatrixDecomposition::getInteratomicVector(int atom1, int atom2){ + Vector3d d; + +#ifdef IS_MPI + d = atomColData.position[atom2] - atomRowData.position[atom1]; +#else + d = snap_->atomData.position[atom2] - snap_->atomData.position[atom1]; +#endif + + snap_->wrapVector(d); + return d; + } + + vector ForceMatrixDecomposition::getSkipsForRowAtom(int atom1) { +#ifdef IS_MPI + return skipsForRowAtom[atom1]; +#else + return skipsForLocalAtom[atom1]; +#endif + } + + /** + * 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 the + * parallel decomposition. + */ + bool ForceMatrixDecomposition::skipAtomPair(int atom1, int atom2) { + 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]; + + // this situation should only arise in MPI simulations + if (unique_id_1 == unique_id_2) return true; + + // this prevents us from doing the pair on multiple processors + if (unique_id_1 < unique_id_2) { + if ((unique_id_1 + unique_id_2) % 2 == 0) return true; + } else { + if ((unique_id_1 + unique_id_2) % 2 == 1) return true; + } +#else + // in the normal loop, the atom numbers are unique + unique_id_1 = atom1; + unique_id_2 = atom2; +#endif + +#ifdef IS_MPI + for (vector::iterator i = skipsForRowAtom[atom1].begin(); + i != skipsForRowAtom[atom1].end(); ++i) { + if ( (*i) == unique_id_2 ) return true; + } +#else + for (vector::iterator i = skipsForLocalAtom[atom1].begin(); + i != skipsForLocalAtom[atom1].end(); ++i) { + if ( (*i) == unique_id_2 ) return true; + } +#endif + } + + int ForceMatrixDecomposition::getTopoDistance(int atom1, int atom2) { + +#ifdef IS_MPI + for (int i = 0; i < toposForRowAtom[atom1].size(); i++) { + if ( toposForRowAtom[atom1][i] == atom2 ) return topoDistRow[atom1][i]; + } +#else + for (int i = 0; i < toposForLocalAtom[atom1].size(); i++) { + if ( toposForLocalAtom[atom1][i] == atom2 ) return topoDistLocal[atom1][i]; + } +#endif + + // zero is default for unconnected (i.e. normal) pair interactions + return 0; + } + + void ForceMatrixDecomposition::addForceToAtomRow(int atom1, Vector3d fg){ +#ifdef IS_MPI + atomRowData.force[atom1] += fg; +#else + snap_->atomData.force[atom1] += fg; +#endif + } + + void ForceMatrixDecomposition::addForceToAtomColumn(int atom2, Vector3d fg){ +#ifdef IS_MPI + atomColData.force[atom2] += fg; +#else + snap_->atomData.force[atom2] += fg; +#endif + } + + // filling interaction blocks with pointers + InteractionData ForceMatrixDecomposition::fillInteractionData(int atom1, int atom2) { + InteractionData idat; + +#ifdef IS_MPI + + idat.atypes = make_pair( ff_->getAtomType(identsRow[atom1]), + ff_->getAtomType(identsCol[atom2]) ); + + 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::dslDensity) { + idat.rho1 = &(atomRowData.density[atom1]); + idat.rho2 = &(atomColData.density[atom2]); + } + + if (storageLayout_ & DataStorage::dslFunctionalDerivative) { + idat.dfrho1 = &(atomRowData.functionalDerivative[atom1]); + idat.dfrho2 = &(atomColData.functionalDerivative[atom2]); + } + +#else + + idat.atypes = make_pair( ff_->getAtomType(identsLocal[atom1]), + ff_->getAtomType(identsLocal[atom2]) ); + + 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::dslDensity) { + idat.rho1 = &(snap_->atomData.density[atom1]); + idat.rho2 = &(snap_->atomData.density[atom2]); + } + + if (storageLayout_ & DataStorage::dslFunctionalDerivative) { + idat.dfrho1 = &(snap_->atomData.functionalDerivative[atom1]); + idat.dfrho2 = &(snap_->atomData.functionalDerivative[atom2]); + } +#endif + return idat; + } + + 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]); + } + if (storageLayout_ & DataStorage::dslForce) { + idat.t1 = &(atomRowData.force[atom1]); + idat.t2 = &(atomColData.force[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]); + } + if (storageLayout_ & DataStorage::dslForce) { + idat.t1 = &(snap_->atomData.force[atom1]); + idat.t2 = &(snap_->atomData.force[atom2]); + } +#endif + } + + /* + * buildNeighborList + * + * first element of pair is row-indexed CutoffGroup + * second element of pair is column-indexed CutoffGroup + */ + vector > ForceMatrixDecomposition::buildNeighborList() { + + vector > neighborList; +#ifdef IS_MPI + cellListRow_.clear(); + cellListCol_.clear(); +#else + cellList_.clear(); +#endif + + // dangerous to not do error checking. + RealType rCut_; + + RealType rList_ = (rCut_ + skinThickness_); + RealType rl2 = rList_ * rList_; + Snapshot* snap_ = sman_->getCurrentSnapshot(); + Mat3x3d Hmat = snap_->getHmat(); + Vector3d Hx = Hmat.getColumn(0); + Vector3d Hy = Hmat.getColumn(1); + Vector3d Hz = Hmat.getColumn(2); + + nCells_.x() = (int) ( Hx.length() )/ rList_; + nCells_.y() = (int) ( Hy.length() )/ rList_; + nCells_.z() = (int) ( Hz.length() )/ rList_; + + Mat3x3d invHmat = snap_->getInvHmat(); + Vector3d rs, scaled, dr; + Vector3i whichCell; + int cellIndex; + +#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); + } +#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); + } +#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) { + + // 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); + if (dr.lengthSquare() < rl2) { + 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); + if (dr.lengthSquare() < rl2) { + 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