From 9ea3f652091032ade506dd0000df1c4e15e9a25c Mon Sep 17 00:00:00 2001 From: Berk Hess Date: Thu, 2 Sep 2021 16:09:11 +0000 Subject: [PATCH] Minor clean-up and optimization in DD topology --- src/gromacs/domdec/localtopology.cpp | 65 ++++++++++++-------------- src/gromacs/domdec/reversetopology.cpp | 37 +++++++-------- src/gromacs/domdec/reversetopology.h | 23 ++++++--- 3 files changed, 63 insertions(+), 62 deletions(-) diff --git a/src/gromacs/domdec/localtopology.cpp b/src/gromacs/domdec/localtopology.cpp index 0b8c76f659..d026983b0e 100644 --- a/src/gromacs/domdec/localtopology.cpp +++ b/src/gromacs/domdec/localtopology.cpp @@ -614,11 +614,6 @@ static int make_bondeds_zone(gmx_reverse_top_t* rt, int izone, const gmx::Range& atomRange) { - int mb = 0; - int mt = 0; - int mol = 0; - int atomIndexInMolecule = 0; - const auto ddBondedChecking = rt->options().ddBondedChecking; int numBondedInteractions = 0; @@ -626,21 +621,22 @@ static int make_bondeds_zone(gmx_reverse_top_t* rt, for (int i : atomRange) { /* Get the global atom number */ - const int globalAtomIndex = globalAtomIndices[i]; - global_atomnr_to_moltype_ind( - rt->molblockIndices(), globalAtomIndex, &mb, &mt, &mol, &atomIndexInMolecule); + const int globalAtomIndex = globalAtomIndices[i]; + const MolecularTopologyAtomIndices mtai = + globalAtomIndexToMoltypeIndices(rt->molblockIndices(), globalAtomIndex); /* Check all intramolecular interactions assigned to this atom */ - gmx::ArrayRef index = rt->interactionListForMoleculeType(mt).index; - gmx::ArrayRef rtil = rt->interactionListForMoleculeType(mt).il; + const auto& ilistMol = rt->interactionListForMoleculeType(mtai.moleculeType); + gmx::ArrayRef index = ilistMol.index; + gmx::ArrayRef rtil = ilistMol.il; numBondedInteractions += assignInteractionsForAtom( i, globalAtomIndex, - atomIndexInMolecule, + mtai.atomIndex, index, rtil, - index[atomIndexInMolecule], - index[atomIndexInMolecule + 1], + index[mtai.atomIndex], + index[mtai.atomIndex + 1], ga2la, zones, checkDistanceMultiBody, @@ -656,24 +652,24 @@ static int make_bondeds_zone(gmx_reverse_top_t* rt, // Assign position restraints, when present, for the home zone if (izone == 0 && rt->hasPositionRestraints()) { - numBondedInteractions += assignPositionRestraintsForAtom( - i, - mol, - atomIndexInMolecule, - rt->interactionListForMoleculeType(mt).numAtomsInMolecule, - rtil, - index[atomIndexInMolecule], - index[atomIndexInMolecule + 1], - molb[mb], - ip_in, - idef); + numBondedInteractions += assignPositionRestraintsForAtom(i, + mtai.moleculeIndex, + mtai.atomIndex, + ilistMol.numAtomsInMolecule, + rtil, + index[mtai.atomIndex], + index[mtai.atomIndex + 1], + molb[mtai.blockIndex], + ip_in, + idef); } if (rt->hasIntermolecularInteractions()) { /* Check all intermolecular interactions assigned to this atom */ - index = rt->interactionListForIntermolecularInteractions().index; - rtil = rt->interactionListForIntermolecularInteractions().il; + const auto& ilistIntermol = rt->interactionListForIntermolecularInteractions(); + index = ilistIntermol.index; + rtil = ilistIntermol.il; numBondedInteractions += assignInteractionsForAtom( i, @@ -724,18 +720,15 @@ static void make_exclusions_zone(ArrayRef globalAtomInd if (atomInfo[at] & gmx::sc_atomInfo_Exclusion) { - int mb = 0; - int mt = 0; - int mol = 0; - int a_mol = 0; - /* Copy the exclusions from the global top */ - int a_gl = globalAtomIndices[at]; - global_atomnr_to_moltype_ind(molblockIndices, a_gl, &mb, &mt, &mol, &a_mol); - const auto excls = moltype[mt].excls[a_mol]; - for (const int aj_mol : excls) + const int globalAtomIndex = globalAtomIndices[at]; + const MolecularTopologyAtomIndices mtai = + globalAtomIndexToMoltypeIndices(molblockIndices, globalAtomIndex); + const auto& excls = moltype[mtai.moleculeType].excls[mtai.atomIndex]; + for (const int excludedAtomIndexInMolecule : excls) { - if (const auto* jEntry = ga2la.find(a_gl + aj_mol - a_mol)) + if (const auto* jEntry = + ga2la.find(globalAtomIndex + excludedAtomIndexInMolecule - mtai.atomIndex)) { /* This check is not necessary, but it can reduce * the number of exclusions in the list, which in turn diff --git a/src/gromacs/domdec/reversetopology.cpp b/src/gromacs/domdec/reversetopology.cpp index 30ca66aa2d..85125791e1 100644 --- a/src/gromacs/domdec/reversetopology.cpp +++ b/src/gromacs/domdec/reversetopology.cpp @@ -128,27 +128,22 @@ bool dd_check_ftype(const int ftype, const ReverseTopOptions& rtOptions) || (rtOptions.includeSettles && ftype == F_SETTLE)); } -void global_atomnr_to_moltype_ind(ArrayRef molblockIndices, - int i_gl, - int* mb, - int* mt, - int* mol, - int* i_mol) +MolecularTopologyAtomIndices globalAtomIndexToMoltypeIndices(const gmx::ArrayRef molblockIndices, + const int globalAtomIndex) { - const MolblockIndices* mbi = molblockIndices.data(); - int start = 0; - int end = molblockIndices.size(); /* exclusive */ - int mid = 0; + // Find the molblock the atom belongs to using bisection + int start = 0; + int end = molblockIndices.size(); /* exclusive */ + int mid = 0; - /* binary search for molblock_ind */ - while (TRUE) + while (true) { mid = (start + end) / 2; - if (i_gl >= mbi[mid].a_end) + if (globalAtomIndex >= molblockIndices[mid].a_end) { start = mid + 1; } - else if (i_gl < mbi[mid].a_start) + else if (globalAtomIndex < molblockIndices[mid].a_start) { end = mid; } @@ -158,12 +153,16 @@ void global_atomnr_to_moltype_ind(ArrayRef molblockIndice } } - *mb = mid; - mbi += mid; + const MolblockIndices& mbi = molblockIndices[mid]; - *mt = mbi->type; - *mol = (i_gl - mbi->a_start) / mbi->natoms_mol; - *i_mol = (i_gl - mbi->a_start) - (*mol) * mbi->natoms_mol; + MolecularTopologyAtomIndices mtai; + + mtai.blockIndex = mid; + mtai.moleculeType = mbi.type; + mtai.moleculeIndex = (globalAtomIndex - mbi.a_start) / mbi.natoms_mol; + mtai.atomIndex = (globalAtomIndex - mbi.a_start) - mtai.moleculeIndex * mbi.natoms_mol; + + return mtai; } /*! \brief Returns the maximum number of exclusions per atom */ diff --git a/src/gromacs/domdec/reversetopology.h b/src/gromacs/domdec/reversetopology.h index 9cb998b259..b8cbe284bc 100644 --- a/src/gromacs/domdec/reversetopology.h +++ b/src/gromacs/domdec/reversetopology.h @@ -167,13 +167,22 @@ int nral_rt(int ftype); /*! \brief Return whether interactions of type \p ftype need to be assigned exactly once */ bool dd_check_ftype(int ftype, const ReverseTopOptions& rtOptions); -/*! \brief Return global topology molecule information for global atom index \p i_gl */ -void global_atomnr_to_moltype_ind(gmx::ArrayRef molblockIndices, - int i_gl, - int* mb, - int* mt, - int* mol, - int* i_mol); +//! Molecular topology indices of a global molecule a global atom belongs to +struct MolecularTopologyAtomIndices +{ + //! The index of the molecule block + int blockIndex; + //! The molecule type + int moleculeType; + //! The index of the molecule in the block + int moleculeIndex; + //! The index of the atom in the molecule + int atomIndex; +}; + +//! Return global topology molecule information for global atom index \p globalAtomIndex +MolecularTopologyAtomIndices globalAtomIndexToMoltypeIndices(gmx::ArrayRef molblockIndices, + int globalAtomIndex); /*! \brief Make the reverse ilist: a list of bonded interactions linked to atoms */ void make_reverse_ilist(const InteractionLists& ilist, -- 2.22.0