2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2006 - 2014, The GROMACS development team.
5 * Copyright (c) 2015,2016,2017,2018,2019,2020,2021, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
39 * \brief This file defines functions used by the domdec module while
40 * building topologies local to a domain
42 * \author Berk Hess <hess@kth.se>
43 * \ingroup module_domdec
48 #include "gromacs/domdec/localtopology.h"
54 #include "gromacs/domdec/domdec_internal.h"
55 #include "gromacs/domdec/domdec_struct.h"
56 #include "gromacs/domdec/ga2la.h"
57 #include "gromacs/domdec/options.h"
58 #include "gromacs/domdec/reversetopology.h"
59 #include "gromacs/math/vec.h"
60 #include "gromacs/mdtypes/atominfo.h"
61 #include "gromacs/mdtypes/forcerec.h"
62 #include "gromacs/mdtypes/mdatom.h"
63 #include "gromacs/pbcutil/pbc.h"
64 #include "gromacs/topology/idef.h"
65 #include "gromacs/topology/mtop_util.h"
66 #include "gromacs/topology/topsort.h"
67 #include "gromacs/utility/arrayref.h"
68 #include "gromacs/utility/fatalerror.h"
69 #include "gromacs/utility/gmxassert.h"
70 #include "gromacs/utility/listoflists.h"
71 #include "gromacs/utility/strconvert.h"
74 using gmx::DDBondedChecking;
75 using gmx::ListOfLists;
78 //! Container for returning molecule type and index information for an atom
81 int local; //!< The local index
82 int global; //!< The global index
83 int withinMolecule; //!< The index in the molecule this atom belongs to
86 //! Container for returning molecule type and index information for an atom
89 int molblockIndex; //!< The index into the molecule block array
90 int moleculeType; //!< The molecule type
91 int moleculeIndex; //!< The index of the molecule in the molecule block
92 int atomIndexInMolecule; //!< The index of the atom in the molecule
95 /*! \brief Return global topology molecule information for global atom index \p i_gl */
96 static AtomInMolblock atomInMolblockFromGlobalAtomnr(ArrayRef<const MolblockIndices> molblockIndices,
97 const int globalAtomIndex)
99 // Find the molecule block whose range of global atom indices
100 // includes globalAtomIndex, by being the first for which
101 // globalAtomIndex is not greater than its end.
102 auto molblockIt = std::partition_point(
103 molblockIndices.begin(),
104 molblockIndices.end(),
105 [globalAtomIndex](const MolblockIndices& mbi) { return mbi.a_end <= globalAtomIndex; });
109 aim.molblockIndex = std::distance(molblockIndices.begin(), molblockIt);
110 aim.moleculeType = molblockIt->type;
111 aim.moleculeIndex = (globalAtomIndex - molblockIt->a_start) / molblockIt->natoms_mol;
112 aim.atomIndexInMolecule =
113 (globalAtomIndex - molblockIt->a_start) - (aim.moleculeIndex) * molblockIt->natoms_mol;
118 /*! \brief Store a vsite at the end of \p il, returns an array with parameter and atom indices
120 * This routine is very similar to add_ifunc, but vsites interactions
121 * have more work to do than other kinds of interactions, and the
122 * complex way nral (and thus vector contents) depends on ftype
123 * confuses static analysis tools unless we fuse the vsite
124 * atom-indexing organization code with the ifunc-adding code, so that
125 * they can see that nral is the same value.
127 * \param[in] ga2la The global to local atom index mapping
128 * \param[in] nral The number of atoms involved in this vsite
129 * \param[in] isLocalVsite Whether all atoms involved in this vsite are local
130 * \param[in] atomIndexSet The atom index set for the virtual site
131 * \param[in] iatoms Indices, 0: interaction type, 1: vsite (unused), 2 ...: constructing atoms
132 * \param[in,out] il The interaction list to add this vsite to
134 * \returns an array with the parameter index and the NRAL atom indices
136 static inline ArrayRef<const int> add_ifunc_for_vsites(const gmx_ga2la_t& ga2la,
138 const bool isLocalVsite,
139 const AtomIndexSet& atomIndexSet,
140 ArrayRef<const int> iatoms,
143 std::array<int, 1 + MAXATOMLIST> tiatoms;
146 tiatoms[0] = iatoms[0];
150 /* We know the local index of the first atom */
151 tiatoms[1] = atomIndexSet.local;
155 /* Convert later in make_local_vsites */
156 tiatoms[1] = -atomIndexSet.global - 1;
159 GMX_ASSERT(nral >= 2 && nral <= 5, "Invalid nral for vsites");
160 for (int k = 2; k < 1 + nral; k++)
162 int ak_gl = atomIndexSet.global + iatoms[k] - atomIndexSet.withinMolecule;
163 if (const int* homeIndex = ga2la.findHome(ak_gl))
165 tiatoms[k] = *homeIndex;
169 /* Copy the global index, convert later in make_local_vsites */
170 tiatoms[k] = -(ak_gl + 1);
172 // Note that ga2la_get_home always sets the third parameter if
175 il->push_back(tiatoms[0], nral, tiatoms.data() + 1);
177 // Return an array with the parameter index and the nral atom indices
178 return ArrayRef<int>(il->iatoms).subArray(il->iatoms.size() - (1 + nral), 1 + nral);
181 /*! \brief Store a position restraint in idef and iatoms, complex because the parameters are different for each entry */
182 static void add_posres(int mol,
184 int numAtomsInMolecule,
185 const gmx_molblock_t& molb,
186 gmx::ArrayRef<int> iatoms,
187 const t_iparams* ip_in,
188 InteractionDefinitions* idef)
190 /* This position restraint has not been added yet,
191 * so it's index is the current number of position restraints.
193 const int n = idef->il[F_POSRES].size() / 2;
195 /* Get the position restraint coordinates from the molblock */
196 const int a_molb = mol * numAtomsInMolecule + a_mol;
197 GMX_ASSERT(a_molb < ssize(molb.posres_xA),
198 "We need a sufficient number of position restraint coordinates");
199 /* Copy the force constants */
200 t_iparams ip = ip_in[iatoms[0]];
201 ip.posres.pos0A[XX] = molb.posres_xA[a_molb][XX];
202 ip.posres.pos0A[YY] = molb.posres_xA[a_molb][YY];
203 ip.posres.pos0A[ZZ] = molb.posres_xA[a_molb][ZZ];
204 if (!molb.posres_xB.empty())
206 ip.posres.pos0B[XX] = molb.posres_xB[a_molb][XX];
207 ip.posres.pos0B[YY] = molb.posres_xB[a_molb][YY];
208 ip.posres.pos0B[ZZ] = molb.posres_xB[a_molb][ZZ];
212 ip.posres.pos0B[XX] = ip.posres.pos0A[XX];
213 ip.posres.pos0B[YY] = ip.posres.pos0A[YY];
214 ip.posres.pos0B[ZZ] = ip.posres.pos0A[ZZ];
216 /* Set the parameter index for idef->iparams_posres */
218 idef->iparams_posres.push_back(ip);
220 GMX_ASSERT(int(idef->iparams_posres.size()) == n + 1,
221 "The index of the parameter type should match n");
224 /*! \brief Store a flat-bottomed position restraint in idef and iatoms, complex because the parameters are different for each entry */
225 static void add_fbposres(int mol,
227 int numAtomsInMolecule,
228 const gmx_molblock_t& molb,
229 gmx::ArrayRef<int> iatoms,
230 const t_iparams* ip_in,
231 InteractionDefinitions* idef)
233 /* This flat-bottom position restraint has not been added yet,
234 * so it's index is the current number of position restraints.
236 const int n = idef->il[F_FBPOSRES].size() / 2;
238 /* Get the position restraint coordinats from the molblock */
239 const int a_molb = mol * numAtomsInMolecule + a_mol;
240 GMX_ASSERT(a_molb < ssize(molb.posres_xA),
241 "We need a sufficient number of position restraint coordinates");
242 /* Copy the force constants */
243 t_iparams ip = ip_in[iatoms[0]];
244 /* Take reference positions from A position of normal posres */
245 ip.fbposres.pos0[XX] = molb.posres_xA[a_molb][XX];
246 ip.fbposres.pos0[YY] = molb.posres_xA[a_molb][YY];
247 ip.fbposres.pos0[ZZ] = molb.posres_xA[a_molb][ZZ];
249 /* Note: no B-type for flat-bottom posres */
251 /* Set the parameter index for idef->iparams_fbposres */
253 idef->iparams_fbposres.push_back(ip);
255 GMX_ASSERT(int(idef->iparams_fbposres.size()) == n + 1,
256 "The index of the parameter type should match n");
259 /*! \brief Store a virtual site interaction, complex because of PBC and recursion */
260 static void add_vsite(const gmx_ga2la_t& ga2la,
261 const reverse_ilist_t& reverseIlist,
264 const bool isLocalVsite,
265 const AtomIndexSet& atomIndexSet,
266 ArrayRef<const int> iatoms,
267 InteractionDefinitions* idef)
269 /* Add this interaction to the local topology */
270 ArrayRef<const int> tiatoms =
271 add_ifunc_for_vsites(ga2la, nral, isLocalVsite, atomIndexSet, iatoms, &idef->il[ftype]);
273 if (iatoms[1 + nral])
275 /* Check for recursion */
276 for (int k = 2; k < 1 + nral; k++)
278 if ((iatoms[1 + nral] & (2 << k)) && (tiatoms[k] < 0))
280 /* This construction atoms is a vsite and not a home atom */
284 "Constructing atom %d of vsite atom %d is a vsite and non-home\n",
286 atomIndexSet.withinMolecule + 1);
288 /* Find the vsite construction */
290 /* Check all interactions assigned to this atom */
291 int j = reverseIlist.index[iatoms[k]];
292 while (j < reverseIlist.index[iatoms[k] + 1])
294 int ftype_r = reverseIlist.il[j++];
295 int nral_r = NRAL(ftype_r);
296 if (interaction_function[ftype_r].flags & IF_VSITE)
298 /* Add this vsite (recursion) */
299 const AtomIndexSet atomIndexRecur = {
300 -1, atomIndexSet.global + iatoms[k] - iatoms[1], iatoms[k]
308 gmx::arrayRefFromArray(reverseIlist.il.data() + j,
309 reverseIlist.il.size() - j),
312 j += 1 + nral_rt(ftype_r);
319 /*! \brief Returns the squared distance between atoms \p i and \p j */
320 static real dd_dist2(const t_pbc* pbc_null, ArrayRef<const RVec> coordinates, const int i, const int j)
326 pbc_dx_aiuc(pbc_null, coordinates[i], coordinates[j], dx);
330 rvec_sub(coordinates[i], coordinates[j], dx);
336 /*! \brief Append t_idef structures 1 to nsrc in src to *dest */
337 static void combine_idef(InteractionDefinitions* dest, gmx::ArrayRef<const thread_work_t> src)
339 for (int ftype = 0; ftype < F_NRE; ftype++)
342 for (gmx::index s = 1; s < src.ssize(); s++)
344 n += src[s].idef.il[ftype].size();
348 for (gmx::index s = 1; s < src.ssize(); s++)
350 dest->il[ftype].append(src[s].idef.il[ftype]);
353 /* Position restraints need an additional treatment */
354 if (ftype == F_POSRES || ftype == F_FBPOSRES)
356 int nposres = dest->il[ftype].size() / 2;
357 std::vector<t_iparams>& iparams_dest =
358 (ftype == F_POSRES ? dest->iparams_posres : dest->iparams_fbposres);
360 /* Set nposres to the number of original position restraints in dest */
361 for (gmx::index s = 1; s < src.ssize(); s++)
363 nposres -= src[s].idef.il[ftype].size() / 2;
366 for (gmx::index s = 1; s < src.ssize(); s++)
368 const std::vector<t_iparams>& iparams_src =
369 (ftype == F_POSRES ? src[s].idef.iparams_posres : src[s].idef.iparams_fbposres);
370 iparams_dest.insert(iparams_dest.end(), iparams_src.begin(), iparams_src.end());
372 /* Correct the indices into iparams_posres */
373 for (int i = 0; i < src[s].idef.il[ftype].size() / 2; i++)
375 /* Correct the index into iparams_posres */
376 dest->il[ftype].iatoms[nposres * 2] = nposres;
381 int(iparams_dest.size()) == nposres,
382 "The number of parameters should match the number of restraints");
388 /*! \brief Determine whether the local domain has responsibility for
389 * any of the bonded interactions for local atom \p atomIndex
390 * and assign those to the local domain.
392 * \returns The total number of bonded interactions for this atom for
393 * which this domain is responsible.
395 static inline int assignInteractionsForAtom(const AtomIndexSet& atomIndexSet,
396 const reverse_ilist_t& reverseIlist,
397 const gmx_ga2la_t& ga2la,
398 const gmx_domdec_zones_t& zones,
399 const bool checkDistanceMultiBody,
401 const bool checkDistanceTwoBody,
402 const real cutoffSquared,
403 const t_pbc* pbc_null,
404 ArrayRef<const RVec> coordinates,
405 InteractionDefinitions* idef,
407 const DDBondedChecking ddBondedChecking)
409 gmx::ArrayRef<const DDPairInteractionRanges> iZones = zones.iZones;
411 ArrayRef<const int> rtil = reverseIlist.il;
413 int numBondedInteractions = 0;
415 int j = reverseIlist.index[atomIndexSet.withinMolecule];
416 const int indexEnd = reverseIlist.index[atomIndexSet.withinMolecule + 1];
419 int tiatoms[1 + MAXATOMLIST];
421 const int ftype = rtil[j++];
422 auto iatoms = gmx::constArrayRefFromArray(rtil.data() + j, rtil.size() - j);
423 const int nral = NRAL(ftype);
424 if (interaction_function[ftype].flags & IF_VSITE)
426 /* The vsite construction goes where the vsite itself is */
429 add_vsite(ga2la, reverseIlist, ftype, nral, true, atomIndexSet, iatoms, idef);
437 tiatoms[0] = iatoms[0];
441 /* Assign single-body interactions to the home zone.
442 * Position restraints are not handled here, but separately.
444 if (iz == 0 && !(ftype == F_POSRES || ftype == F_FBPOSRES))
447 tiatoms[1] = atomIndexSet.local;
452 /* This is a two-body interaction, we can assign
453 * analogous to the non-bonded assignments.
455 /* Get the global index using the offset in the molecule */
456 const int k_gl = atomIndexSet.global + iatoms[2] - atomIndexSet.withinMolecule;
457 if (const auto* entry = ga2la.find(k_gl))
459 int kz = entry->cell;
464 /* Check zone interaction assignments */
465 bUse = ((iz < iZones.ssize() && iz <= kz && iZones[iz].jZoneRange.isInRange(kz))
466 || (kz < iZones.ssize() && iz > kz && iZones[kz].jZoneRange.isInRange(iz)));
469 GMX_ASSERT(ftype != F_CONSTR || (iz == 0 && kz == 0),
470 "Constraint assigned here should only involve home atoms");
472 tiatoms[1] = atomIndexSet.local;
473 tiatoms[2] = entry->la;
474 /* If necessary check the cgcm distance */
475 if (checkDistanceTwoBody
476 && dd_dist2(pbc_null, coordinates, tiatoms[1], tiatoms[2]) >= cutoffSquared)
489 /* Assign this multi-body bonded interaction to
490 * the local domain if we have all the atoms involved
491 * (local or communicated) and the minimum zone shift
492 * in each dimension is zero, for dimensions
493 * with 2 DD cells an extra check may be necessary.
499 for (int k = 1; k <= nral && bUse; k++)
501 /* Get the global index using the offset in the molecule */
502 const int k_gl = atomIndexSet.global + iatoms[k] - atomIndexSet.withinMolecule;
503 const auto* entry = ga2la.find(k_gl);
504 if (entry == nullptr || entry->cell >= zones.n)
506 /* We do not have this atom of this interaction
507 * locally, or it comes from more than one cell
514 tiatoms[k] = entry->la;
515 for (int d = 0; d < DIM; d++)
517 if (zones.shift[entry->cell][d] == 0)
528 bUse = (bUse && (k_zero[XX] != 0) && (k_zero[YY] != 0) && (k_zero[ZZ] != 0));
529 if (checkDistanceMultiBody)
531 for (int d = 0; (d < DIM && bUse); d++)
533 /* Check if the distance falls within
534 * the cut-off to avoid possible multiple
535 * assignments of bonded interactions.
537 if (rcheck[d] && k_plus[d]
538 && dd_dist2(pbc_null, coordinates, tiatoms[k_zero[d]], tiatoms[k_plus[d]])
548 /* Add this interaction to the local topology */
549 idef->il[ftype].push_back(tiatoms[0], nral, tiatoms + 1);
550 /* Sum so we can check in global_stat
551 * if we have everything.
553 if (ddBondedChecking == DDBondedChecking::All
554 || !(interaction_function[ftype].flags & IF_LIMZERO))
556 numBondedInteractions++;
560 j += 1 + nral_rt(ftype);
563 return numBondedInteractions;
566 /*! \brief Determine whether the local domain has responsibility for
567 * any of the position restraints for local atom \p atomIndex
568 * and assign those to the local domain.
570 * \returns The total number of bonded interactions for this atom for
571 * which this domain is responsible.
573 static inline int assignPositionRestraintsForAtom(const AtomIndexSet& atomIndexSet,
574 const int moleculeIndex,
575 const int numAtomsInMolecule,
576 const reverse_ilist_t& reverseIlist,
577 const gmx_molblock_t& molb,
578 const t_iparams* ip_in,
579 InteractionDefinitions* idef)
581 constexpr int nral = 1;
583 ArrayRef<const int> rtil = reverseIlist.il;
585 int numBondedInteractions = 0;
587 int j = reverseIlist.index[atomIndexSet.withinMolecule];
588 const int indexEnd = reverseIlist.index[atomIndexSet.withinMolecule + 1];
591 const int ftype = rtil[j++];
592 auto iatoms = gmx::constArrayRefFromArray(rtil.data() + j, rtil.size() - j);
594 if (ftype == F_POSRES || ftype == F_FBPOSRES)
596 std::array<int, 1 + nral> tiatoms = { iatoms[0], atomIndexSet.local };
597 if (ftype == F_POSRES)
599 add_posres(moleculeIndex, atomIndexSet.withinMolecule, numAtomsInMolecule, molb, tiatoms, ip_in, idef);
604 moleculeIndex, atomIndexSet.withinMolecule, numAtomsInMolecule, molb, tiatoms, ip_in, idef);
606 idef->il[ftype].push_back(tiatoms[0], nral, tiatoms.data() + 1);
607 numBondedInteractions++;
609 j += 1 + nral_rt(ftype);
612 return numBondedInteractions;
615 /*! \brief This function looks up and assigns bonded interactions for zone iz.
617 * With thread parallelizing each thread acts on a different atom range:
618 * at_start to at_end.
620 static int make_bondeds_zone(const gmx_reverse_top_t& rt,
621 ArrayRef<const int> globalAtomIndices,
622 const gmx_ga2la_t& ga2la,
623 const gmx_domdec_zones_t& zones,
624 const std::vector<gmx_molblock_t>& molb,
625 const bool checkDistanceMultiBody,
627 const bool checkDistanceTwoBody,
628 const real cutoffSquared,
629 const t_pbc* pbc_null,
630 ArrayRef<const RVec> coordinates,
631 const t_iparams* ip_in,
632 InteractionDefinitions* idef,
634 const gmx::Range<int>& atomRange)
636 const auto ddBondedChecking = rt.options().ddBondedChecking;
638 int numBondedInteractions = 0;
640 for (int atomIndexLocal : atomRange)
642 /* Get the global atom number */
643 const int atomIndexGlobal = globalAtomIndices[atomIndexLocal];
644 const auto aim = atomInMolblockFromGlobalAtomnr(rt.molblockIndices(), atomIndexGlobal);
646 const AtomIndexSet atomIndexMol = { atomIndexLocal, atomIndexGlobal, aim.atomIndexInMolecule };
647 const auto& ilistMol = rt.interactionListForMoleculeType(aim.moleculeType);
648 numBondedInteractions += assignInteractionsForAtom(atomIndexMol,
652 checkDistanceMultiBody,
654 checkDistanceTwoBody,
662 // Assign position restraints, when present, for the home zone
663 if (izone == 0 && rt.hasPositionRestraints())
665 numBondedInteractions +=
666 assignPositionRestraintsForAtom(atomIndexMol,
668 ilistMol.numAtomsInMolecule,
669 rt.interactionListForMoleculeType(aim.moleculeType),
670 molb[aim.molblockIndex],
675 if (rt.hasIntermolecularInteractions())
677 /* Check all intermolecular interactions assigned to this atom.
678 * Note that we will index the intermolecular reverse ilist with atomIndexGlobal.
680 const AtomIndexSet atomIndexIntermol = { atomIndexLocal, atomIndexGlobal, atomIndexGlobal };
681 numBondedInteractions +=
682 assignInteractionsForAtom(atomIndexIntermol,
683 rt.interactionListForIntermolecularInteractions(),
686 checkDistanceMultiBody,
688 checkDistanceTwoBody,
698 return numBondedInteractions;
701 /*! \brief Set the exclusion data for i-zone \p iz */
702 static void make_exclusions_zone(ArrayRef<const int> globalAtomIndices,
703 const gmx_ga2la_t& ga2la,
704 const gmx_domdec_zones_t& zones,
705 ArrayRef<const MolblockIndices> molblockIndices,
706 const std::vector<gmx_moltype_t>& moltype,
707 gmx::ArrayRef<const int64_t> atomInfo,
708 ListOfLists<int>* lexcls,
712 const gmx::ArrayRef<const int> intermolecularExclusionGroup)
714 const auto& jAtomRange = zones.iZones[iz].jAtomRange;
716 const gmx::index oldNumLists = lexcls->ssize();
718 std::vector<int> exclusionsForAtom;
719 for (int at = at_start; at < at_end; at++)
721 exclusionsForAtom.clear();
723 if (atomInfo[at] & gmx::sc_atomInfo_Exclusion)
725 /* Copy the exclusions from the global top */
726 const int globalAtomIndex = globalAtomIndices[at];
727 const MolecularTopologyAtomIndices mtai =
728 globalAtomIndexToMoltypeIndices(molblockIndices, globalAtomIndex);
729 const auto& excls = moltype[mtai.moleculeType].excls[mtai.atomIndex];
730 for (const int excludedAtomIndexInMolecule : excls)
732 if (const auto* jEntry =
733 ga2la.find(globalAtomIndex + excludedAtomIndexInMolecule - mtai.atomIndex))
735 /* This check is not necessary, but it can reduce
736 * the number of exclusions in the list, which in turn
737 * can speed up the pair list construction a bit.
739 if (jAtomRange.isInRange(jEntry->la))
741 exclusionsForAtom.push_back(jEntry->la);
747 bool isExcludedAtom = !intermolecularExclusionGroup.empty()
748 && std::find(intermolecularExclusionGroup.begin(),
749 intermolecularExclusionGroup.end(),
750 globalAtomIndices[at])
751 != intermolecularExclusionGroup.end();
755 for (int qmAtomGlobalIndex : intermolecularExclusionGroup)
757 if (const auto* entry = ga2la.find(qmAtomGlobalIndex))
759 exclusionsForAtom.push_back(entry->la);
764 /* Append the exclusions for this atom to the topology */
765 lexcls->pushBack(exclusionsForAtom);
769 lexcls->ssize() - oldNumLists == at_end - at_start,
770 "The number of exclusion list should match the number of atoms in the range");
773 /*! \brief Generate and store all required local bonded interactions in \p idef and local exclusions in \p lexcls
775 * \returns Total count of bonded interactions in the local topology on this domain */
776 static int make_local_bondeds_excls(const gmx_domdec_t& dd,
777 const gmx_domdec_zones_t& zones,
778 const gmx_mtop_t& mtop,
779 ArrayRef<const int64_t> atomInfo,
780 const bool checkDistanceMultiBody,
782 const gmx_bool checkDistanceTwoBody,
784 const t_pbc* pbc_null,
785 ArrayRef<const RVec> coordinates,
786 InteractionDefinitions* idef,
787 ListOfLists<int>* lexcls)
789 int nzone_bondeds = 0;
791 if (dd.reverse_top->hasInterAtomicInteractions())
793 nzone_bondeds = zones.n;
797 /* Only single charge group (or atom) molecules, so interactions don't
798 * cross zone boundaries and we only need to assign in the home zone.
803 /* We only use exclusions from i-zones to i- and j-zones */
804 const int numIZonesForExclusions = (dd.haveExclusions ? zones.iZones.size() : 0);
806 const gmx_reverse_top_t& rt = *dd.reverse_top;
808 const real cutoffSquared = gmx::square(cutoff);
810 /* Clear the counts */
812 int numBondedInteractions = 0;
816 for (int izone = 0; izone < nzone_bondeds; izone++)
818 const int cg0 = zones.cg_range[izone];
819 const int cg1 = zones.cg_range[izone + 1];
821 gmx::ArrayRef<thread_work_t> threadWorkObjects = rt.threadWorkObjects();
822 const int numThreads = threadWorkObjects.size();
823 #pragma omp parallel for num_threads(numThreads) schedule(static)
824 for (int thread = 0; thread < numThreads; thread++)
828 InteractionDefinitions* idef_t = nullptr;
830 int cg0t = cg0 + ((cg1 - cg0) * thread) / numThreads;
831 int cg1t = cg0 + ((cg1 - cg0) * (thread + 1)) / numThreads;
839 idef_t = &threadWorkObjects[thread].idef;
843 threadWorkObjects[thread].numBondedInteractions =
844 make_bondeds_zone(rt,
845 dd.globalAtomIndices,
849 checkDistanceMultiBody,
851 checkDistanceTwoBody,
855 idef->iparams.data(),
858 gmx::Range<int>(cg0t, cg1t));
860 if (izone < numIZonesForExclusions)
862 ListOfLists<int>* excl_t = nullptr;
865 // Thread 0 stores exclusions directly in the final storage
870 // Threads > 0 store in temporary storage, starting at list index 0
871 excl_t = &threadWorkObjects[thread].excl;
875 /* No charge groups and no distance check required */
876 make_exclusions_zone(dd.globalAtomIndices,
879 rt.molblockIndices(),
886 mtop.intermolecularExclusionGroup);
889 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
892 if (threadWorkObjects.size() > 1)
894 combine_idef(idef, threadWorkObjects);
897 for (const thread_work_t& th_work : threadWorkObjects)
899 numBondedInteractions += th_work.numBondedInteractions;
902 if (izone < numIZonesForExclusions)
904 for (std::size_t th = 1; th < threadWorkObjects.size(); th++)
906 lexcls->appendListOfLists(threadWorkObjects[th].excl);
913 fprintf(debug, "We have %d exclusions\n", lexcls->numElements());
916 return numBondedInteractions;
919 int dd_make_local_top(const gmx_domdec_t& dd,
920 const gmx_domdec_zones_t& zones,
926 ArrayRef<const RVec> coordinates,
927 const gmx_mtop_t& mtop,
928 gmx::ArrayRef<const int64_t> atomInfo,
929 gmx_localtop_t* ltop)
933 t_pbc pbc, *pbc_null = nullptr;
937 fprintf(debug, "Making local topology\n");
940 bool checkDistanceMultiBody = false;
941 bool checkDistanceTwoBody = false;
943 if (dd.reverse_top->hasInterAtomicInteractions())
945 /* We need to check to which cell bondeds should be assigned */
946 rc = dd_cutoff_twobody(&dd);
949 fprintf(debug, "Two-body bonded cut-off distance is %g\n", rc);
952 /* Should we check distances when assigning bonded interactions? */
953 for (int d = 0; d < DIM; d++)
956 /* Only need to check for dimensions where the part of the box
957 * that is not communicated is smaller than the cut-off.
959 if (d < npbcdim && dd.numCells[d] > 1 && (dd.numCells[d] - npulse[d]) * cellsize_min[d] < 2 * rc)
961 if (dd.numCells[d] == 2)
964 checkDistanceMultiBody = TRUE;
966 /* Check for interactions between two atoms,
967 * where we can allow interactions up to the cut-off,
968 * instead of up to the smallest cell dimension.
970 checkDistanceTwoBody = TRUE;
975 "dim %d cellmin %f bonded rcheck[%d] = %d, checkDistanceTwoBody = %s\n",
980 gmx::boolToString(checkDistanceTwoBody));
983 if (checkDistanceMultiBody || checkDistanceTwoBody)
987 pbc_null = set_pbc_dd(&pbc, fr->pbcType, dd.numCells, TRUE, box);
996 int numBondedInteractionsToReduce = make_local_bondeds_excls(dd,
1000 checkDistanceMultiBody,
1002 checkDistanceTwoBody,
1009 if (dd.reverse_top->doListedForcesSorting())
1011 gmx_sort_ilist_fe(<op->idef, atomInfo);
1015 ltop->idef.ilsort = ilsortNO_FE;
1018 return numBondedInteractionsToReduce;