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, 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
40 * while managing the construction, use and error checking for
41 * topologies local to a DD rank.
43 * \author Berk Hess <hess@kth.se>
44 * \ingroup module_domdec
57 #include "gromacs/domdec/domdec.h"
58 #include "gromacs/domdec/domdec_network.h"
59 #include "gromacs/domdec/ga2la.h"
60 #include "gromacs/gmxlib/chargegroup.h"
61 #include "gromacs/gmxlib/network.h"
62 #include "gromacs/math/vec.h"
63 #include "gromacs/mdlib/forcerec.h"
64 #include "gromacs/mdlib/gmx_omp_nthreads.h"
65 #include "gromacs/mdtypes/commrec.h"
66 #include "gromacs/mdtypes/inputrec.h"
67 #include "gromacs/mdtypes/md_enums.h"
68 #include "gromacs/mdtypes/mdatom.h"
69 #include "gromacs/mdtypes/state.h"
70 #include "gromacs/pbcutil/mshift.h"
71 #include "gromacs/pbcutil/pbc.h"
72 #include "gromacs/topology/ifunc.h"
73 #include "gromacs/topology/mtop_util.h"
74 #include "gromacs/topology/topsort.h"
75 #include "gromacs/utility/cstringutil.h"
76 #include "gromacs/utility/exceptions.h"
77 #include "gromacs/utility/fatalerror.h"
78 #include "gromacs/utility/gmxassert.h"
79 #include "gromacs/utility/logger.h"
80 #include "gromacs/utility/smalloc.h"
81 #include "gromacs/utility/strconvert.h"
82 #include "gromacs/utility/stringstream.h"
83 #include "gromacs/utility/stringutil.h"
84 #include "gromacs/utility/textwriter.h"
86 #include "domdec_constraints.h"
87 #include "domdec_internal.h"
88 #include "domdec_vsite.h"
91 /*! \brief The number of integer item in the local state, used for broadcasting of the state */
92 #define NITEM_DD_INIT_LOCAL_STATE 5
94 struct reverse_ilist_t
96 std::vector<int> index; /* Index for each atom into il */
97 std::vector<int> il; /* ftype|type|a0|...|an|ftype|... */
98 int numAtomsInMolecule; /* The number of atoms in this molecule */
101 struct MolblockIndices
109 /*! \brief Struct for thread local work data for local topology generation */
112 t_idef idef; /**< Partial local topology */
113 std::unique_ptr<VsitePbc> vsitePbc; /**< vsite PBC structure */
114 int nbonded; /**< The number of bondeds in this struct */
115 t_blocka excl; /**< List of exclusions */
116 int excl_count; /**< The total exclusion count for \p excl */
119 /*! \brief Struct for the reverse topology: links bonded interactions to atomsx */
120 struct gmx_reverse_top_t
122 //! @cond Doxygen_Suppress
123 //! \brief The maximum number of exclusions one atom can have
124 int n_excl_at_max = 0;
125 //! \brief Are there constraints in this revserse top?
126 bool bConstr = false;
127 //! \brief Are there settles in this revserse top?
128 bool bSettle = false;
129 //! \brief All bonded interactions have to be assigned?
130 bool bBCheck = false;
131 //! \brief Are there bondeds/exclusions between charge-groups?
132 bool bInterCGInteractions = false;
133 //! \brief Reverse ilist for all moltypes
134 std::vector<reverse_ilist_t> ril_mt;
135 //! \brief The size of ril_mt[?].index summed over all entries
136 int ril_mt_tot_size = 0;
137 //! \brief The sorting state of bondeds for free energy
138 int ilsort = ilsortUNKNOWN;
139 //! \brief molblock to global atom index for quick lookup of molblocks on atom index
140 std::vector<MolblockIndices> mbi;
142 //! \brief Do we have intermolecular interactions?
143 bool bIntermolecularInteractions = false;
144 //! \brief Intermolecular reverse ilist
145 reverse_ilist_t ril_intermol;
147 /* Work data structures for multi-threading */
148 //! \brief Thread work array for local topology generation
149 std::vector<thread_work_t> th_work;
153 /*! \brief Returns the number of atom entries for il in gmx_reverse_top_t */
154 static int nral_rt(int ftype)
159 if (interaction_function[ftype].flags & IF_VSITE)
161 /* With vsites the reverse topology contains an extra entry
162 * for storing if constructing atoms are vsites.
170 /*! \brief Return whether interactions of type \p ftype need to be assigned exactly once */
171 static gmx_bool dd_check_ftype(int ftype, gmx_bool bBCheck,
172 gmx_bool bConstr, gmx_bool bSettle)
174 return ((((interaction_function[ftype].flags & IF_BOND) != 0U) &&
175 ((interaction_function[ftype].flags & IF_VSITE) == 0U) &&
176 (bBCheck || ((interaction_function[ftype].flags & IF_LIMZERO) == 0U))) ||
177 (bConstr && (ftype == F_CONSTR || ftype == F_CONSTRNC)) ||
178 (bSettle && ftype == F_SETTLE));
181 /*! \brief Help print error output when interactions are missing */
183 print_missing_interactions_mb(t_commrec *cr,
184 const gmx_reverse_top_t *rt,
185 const char *moltypename,
186 const reverse_ilist_t *ril,
187 int a_start, int a_end,
188 int nat_mol, int nmol,
192 int nril_mol = ril->index[nat_mol];
193 snew(assigned, nmol*nril_mol);
194 gmx::StringOutputStream stream;
195 gmx::TextWriter log(&stream);
197 gmx::ArrayRef<const int> gatindex = cr->dd->globalAtomIndices;
198 for (int ftype = 0; ftype < F_NRE; ftype++)
200 if (dd_check_ftype(ftype, rt->bBCheck, rt->bConstr, rt->bSettle))
202 int nral = NRAL(ftype);
203 const t_ilist *il = &idef->il[ftype];
204 const t_iatom *ia = il->iatoms;
205 for (int i = 0; i < il->nr; i += 1+nral)
207 int a0 = gatindex[ia[1]];
208 /* Check if this interaction is in
209 * the currently checked molblock.
211 if (a0 >= a_start && a0 < a_end)
213 int mol = (a0 - a_start)/nat_mol;
214 int a0_mol = (a0 - a_start) - mol*nat_mol;
215 int j_mol = ril->index[a0_mol];
217 while (j_mol < ril->index[a0_mol+1] && !found)
219 int j = mol*nril_mol + j_mol;
220 int ftype_j = ril->il[j_mol];
221 /* Here we need to check if this interaction has
222 * not already been assigned, since we could have
223 * multiply defined interactions.
225 if (ftype == ftype_j && ia[0] == ril->il[j_mol+1] &&
228 /* Check the atoms */
230 for (int a = 0; a < nral; a++)
232 if (gatindex[ia[1+a]] !=
233 a_start + mol*nat_mol + ril->il[j_mol+2+a])
243 j_mol += 2 + nral_rt(ftype_j);
247 gmx_incons("Some interactions seem to be assigned multiple times");
255 gmx_sumi(nmol*nril_mol, assigned, cr);
259 for (int mol = 0; mol < nmol; mol++)
262 while (j_mol < nril_mol)
264 int ftype = ril->il[j_mol];
265 int nral = NRAL(ftype);
266 int j = mol*nril_mol + j_mol;
267 if (assigned[j] == 0 &&
268 !(interaction_function[ftype].flags & IF_VSITE))
270 if (DDMASTER(cr->dd))
274 log.writeLineFormatted("Molecule type '%s'", moltypename);
275 log.writeLineFormatted(
276 "the first %d missing interactions, except for exclusions:", nprint);
278 log.writeStringFormatted("%20s atoms",
279 interaction_function[ftype].longname);
281 for (a = 0; a < nral; a++)
283 log.writeStringFormatted("%5d", ril->il[j_mol+2+a]+1);
287 log.writeString(" ");
290 log.writeString(" global");
291 for (a = 0; a < nral; a++)
293 log.writeStringFormatted("%6d",
294 a_start+mol*nat_mol+ril->il[j_mol+2+a]+1);
296 log.ensureLineBreak();
304 j_mol += 2 + nral_rt(ftype);
309 return stream.toString();
312 /*! \brief Help print error output when interactions are missing */
313 static void print_missing_interactions_atoms(const gmx::MDLogger &mdlog,
315 const gmx_mtop_t *mtop,
318 const gmx_reverse_top_t *rt = cr->dd->reverse_top;
320 /* Print the atoms in the missing interactions per molblock */
322 for (const gmx_molblock_t &molb : mtop->molblock)
324 const gmx_moltype_t &moltype = mtop->moltype[molb.type];
326 a_end = a_start + molb.nmol*moltype.atoms.nr;
328 GMX_LOG(mdlog.warning).appendText(
329 print_missing_interactions_mb(cr, rt,
331 &rt->ril_mt[molb.type],
332 a_start, a_end, moltype.atoms.nr,
338 void dd_print_missing_interactions(const gmx::MDLogger &mdlog,
341 const gmx_mtop_t *top_global,
342 const gmx_localtop_t *top_local,
346 int ndiff_tot, cl[F_NRE], n, ndiff, rest_global, rest_local;
352 GMX_LOG(mdlog.warning).appendText(
353 "Not all bonded interactions have been properly assigned to the domain decomposition cells");
355 ndiff_tot = local_count - dd->nbonded_global;
357 for (ftype = 0; ftype < F_NRE; ftype++)
360 cl[ftype] = top_local->idef.il[ftype].nr/(1+nral);
363 gmx_sumi(F_NRE, cl, cr);
367 GMX_LOG(mdlog.warning).appendText("A list of missing interactions:");
368 rest_global = dd->nbonded_global;
369 rest_local = local_count;
370 for (ftype = 0; ftype < F_NRE; ftype++)
372 /* In the reverse and local top all constraints are merged
373 * into F_CONSTR. So in the if statement we skip F_CONSTRNC
374 * and add these constraints when doing F_CONSTR.
376 if (((interaction_function[ftype].flags & IF_BOND) &&
377 (dd->reverse_top->bBCheck
378 || !(interaction_function[ftype].flags & IF_LIMZERO)))
379 || (dd->reverse_top->bConstr && ftype == F_CONSTR)
380 || (dd->reverse_top->bSettle && ftype == F_SETTLE))
382 n = gmx_mtop_ftype_count(top_global, ftype);
383 if (ftype == F_CONSTR)
385 n += gmx_mtop_ftype_count(top_global, F_CONSTRNC);
387 ndiff = cl[ftype] - n;
390 GMX_LOG(mdlog.warning).appendTextFormatted(
391 "%20s of %6d missing %6d",
392 interaction_function[ftype].longname, n, -ndiff);
395 rest_local -= cl[ftype];
399 ndiff = rest_local - rest_global;
402 GMX_LOG(mdlog.warning).appendTextFormatted(
403 "%20s of %6d missing %6d", "exclusions",
404 rest_global, -ndiff);
408 print_missing_interactions_atoms(mdlog, cr, top_global, &top_local->idef);
409 write_dd_pdb("dd_dump_err", 0, "dump", top_global, cr,
412 std::string errorMessage;
416 errorMessage = "One or more interactions were assigned to multiple domains of the domain decompostion. Please report this bug.";
420 errorMessage = gmx::formatString("%d of the %d bonded interactions could not be calculated because some atoms involved moved further apart than the multi-body cut-off distance (%g nm) or the two-body cut-off distance (%g nm), see option -rdd, for pairs and tabulated bonds also see option -ddcheck", -ndiff_tot, cr->dd->nbonded_global, dd_cutoff_multibody(dd), dd_cutoff_twobody(dd));
422 gmx_fatal_collective(FARGS, cr->mpi_comm_mygroup, MASTER(cr), "%s", errorMessage.c_str());
425 /*! \brief Return global topology molecule information for global atom index \p i_gl */
426 static void global_atomnr_to_moltype_ind(const gmx_reverse_top_t *rt,
428 int *mb, int *mt, int *mol, int *i_mol)
430 const MolblockIndices *mbi = rt->mbi.data();
432 int end = rt->mbi.size(); /* exclusive */
435 /* binary search for molblock_ind */
439 if (i_gl >= mbi[mid].a_end)
443 else if (i_gl < mbi[mid].a_start)
457 *mol = (i_gl - mbi->a_start) / mbi->natoms_mol;
458 *i_mol = (i_gl - mbi->a_start) - (*mol)*mbi->natoms_mol;
461 /*! \brief Returns the maximum number of exclusions per atom */
462 static int getMaxNumExclusionsPerAtom(const t_blocka &excls)
465 for (int at = 0; at < excls.nr; at++)
467 const int numExcls = excls.index[at + 1] - excls.index[at];
469 GMX_RELEASE_ASSERT(numExcls != 1 || excls.a[excls.index[at]] == at,
470 "With 1 exclusion we expect a self-exclusion");
472 maxNumExcls = std::max(maxNumExcls, numExcls);
478 /*! \brief Run the reverse ilist generation and store it in r_il when \p bAssign = TRUE */
479 static int low_make_reverse_ilist(const InteractionLists &il_mt,
482 gmx_bool bConstr, gmx_bool bSettle,
484 gmx::ArrayRef<const int> r_index,
485 gmx::ArrayRef<int> r_il,
486 gmx_bool bLinkToAllAtoms,
489 int ftype, j, nlink, link;
494 for (ftype = 0; ftype < F_NRE; ftype++)
496 if ((interaction_function[ftype].flags & (IF_BOND | IF_VSITE)) ||
497 (bConstr && (ftype == F_CONSTR || ftype == F_CONSTRNC)) ||
498 (bSettle && ftype == F_SETTLE))
500 const bool bVSite = ((interaction_function[ftype].flags & IF_VSITE) != 0U);
501 const int nral = NRAL(ftype);
502 const auto &il = il_mt[ftype];
503 for (int i = 0; i < il.size(); i += 1+nral)
505 const int* ia = il.iatoms.data() + i;
510 /* We don't need the virtual sites for the cg-links */
520 /* Couple to the first atom in the interaction */
523 for (link = 0; link < nlink; link++)
528 GMX_ASSERT(!r_il.empty(), "with bAssign not allowed to be empty");
529 GMX_ASSERT(!r_index.empty(), "with bAssign not allowed to be empty");
530 r_il[r_index[a]+count[a]] =
531 (ftype == F_CONSTRNC ? F_CONSTR : ftype);
532 r_il[r_index[a]+count[a]+1] = ia[0];
533 for (j = 1; j < 1+nral; j++)
535 /* Store the molecular atom number */
536 r_il[r_index[a]+count[a]+1+j] = ia[j];
539 if (interaction_function[ftype].flags & IF_VSITE)
543 /* Add an entry to iatoms for storing
544 * which of the constructing atoms are
547 r_il[r_index[a]+count[a]+2+nral] = 0;
548 for (j = 2; j < 1+nral; j++)
550 if (atom[ia[j]].ptype == eptVSite)
552 r_il[r_index[a]+count[a]+2+nral] |= (2<<j);
559 /* We do not count vsites since they are always
560 * uniquely assigned and can be assigned
561 * to multiple nodes with recursive vsites.
564 !(interaction_function[ftype].flags & IF_LIMZERO))
569 count[a] += 2 + nral_rt(ftype);
578 /*! \brief Make the reverse ilist: a list of bonded interactions linked to atoms */
579 static int make_reverse_ilist(const InteractionLists &ilist,
580 const t_atoms *atoms,
581 gmx_bool bConstr, gmx_bool bSettle,
583 gmx_bool bLinkToAllAtoms,
584 reverse_ilist_t *ril_mt)
586 int nat_mt, *count, i, nint_mt;
588 /* Count the interactions */
591 low_make_reverse_ilist(ilist, atoms->atom,
593 bConstr, bSettle, bBCheck,
595 bLinkToAllAtoms, FALSE);
597 ril_mt->index.push_back(0);
598 for (i = 0; i < nat_mt; i++)
600 ril_mt->index.push_back(ril_mt->index[i] + count[i]);
603 ril_mt->il.resize(ril_mt->index[nat_mt]);
605 /* Store the interactions */
607 low_make_reverse_ilist(ilist, atoms->atom,
609 bConstr, bSettle, bBCheck,
610 ril_mt->index, ril_mt->il,
611 bLinkToAllAtoms, TRUE);
615 ril_mt->numAtomsInMolecule = atoms->nr;
620 /*! \brief Generate the reverse topology */
621 static gmx_reverse_top_t make_reverse_top(const gmx_mtop_t *mtop, gmx_bool bFE,
622 gmx_bool bConstr, gmx_bool bSettle,
623 gmx_bool bBCheck, int *nint)
625 gmx_reverse_top_t rt;
627 /* Should we include constraints (for SHAKE) in rt? */
628 rt.bConstr = bConstr;
629 rt.bSettle = bSettle;
630 rt.bBCheck = bBCheck;
632 rt.bInterCGInteractions = mtop->bIntermolecularInteractions;
633 rt.ril_mt.resize(mtop->moltype.size());
634 rt.ril_mt_tot_size = 0;
635 std::vector<int> nint_mt;
636 for (size_t mt = 0; mt < mtop->moltype.size(); mt++)
638 const gmx_moltype_t &molt = mtop->moltype[mt];
641 rt.bInterCGInteractions = true;
644 /* Make the atom to interaction list for this molecule type */
645 int numberOfInteractions =
646 make_reverse_ilist(molt.ilist, &molt.atoms,
647 rt.bConstr, rt.bSettle, rt.bBCheck, FALSE,
649 nint_mt.push_back(numberOfInteractions);
651 rt.ril_mt_tot_size += rt.ril_mt[mt].index[molt.atoms.nr];
655 fprintf(debug, "The total size of the atom to interaction index is %d integers\n", rt.ril_mt_tot_size);
659 for (const gmx_molblock_t &molblock : mtop->molblock)
661 *nint += molblock.nmol*nint_mt[molblock.type];
664 /* Make an intermolecular reverse top, if necessary */
665 rt.bIntermolecularInteractions = mtop->bIntermolecularInteractions;
666 if (rt.bIntermolecularInteractions)
668 t_atoms atoms_global;
670 atoms_global.nr = mtop->natoms;
671 atoms_global.atom = nullptr; /* Only used with virtual sites */
673 GMX_RELEASE_ASSERT(mtop->intermolecular_ilist, "We should have an ilist when intermolecular interactions are on");
676 make_reverse_ilist(*mtop->intermolecular_ilist,
678 rt.bConstr, rt.bSettle, rt.bBCheck, FALSE,
682 if (bFE && gmx_mtop_bondeds_free_energy(mtop))
684 rt.ilsort = ilsortFE_UNSORTED;
688 rt.ilsort = ilsortNO_FE;
691 /* Make a molblock index for fast searching */
693 for (size_t mb = 0; mb < mtop->molblock.size(); mb++)
695 const gmx_molblock_t &molb = mtop->molblock[mb];
696 const int numAtomsPerMol = mtop->moltype[molb.type].atoms.nr;
699 i += molb.nmol*numAtomsPerMol;
701 mbi.natoms_mol = numAtomsPerMol;
702 mbi.type = molb.type;
703 rt.mbi.push_back(mbi);
706 rt.th_work.resize(gmx_omp_nthreads_get(emntDomdec));
711 void dd_make_reverse_top(FILE *fplog,
712 gmx_domdec_t *dd, const gmx_mtop_t *mtop,
713 const gmx_vsite_t *vsite,
714 const t_inputrec *ir, gmx_bool bBCheck)
718 fprintf(fplog, "\nLinking all bonded interactions to atoms\n");
721 /* If normal and/or settle constraints act only within charge groups,
722 * we can store them in the reverse top and simply assign them to domains.
723 * Otherwise we need to assign them to multiple domains and set up
724 * the parallel version constraint algorithm(s).
727 dd->reverse_top = new gmx_reverse_top_t;
729 make_reverse_top(mtop, ir->efep != efepNO,
730 !dd->comm->systemInfo.haveSplitConstraints,
731 !dd->comm->systemInfo.haveSplitSettles,
732 bBCheck, &dd->nbonded_global);
734 gmx_reverse_top_t *rt = dd->reverse_top;
736 dd->haveExclusions = false;
737 rt->n_excl_at_max = 0;
738 for (const gmx_molblock_t &molb : mtop->molblock)
740 const int maxNumExclusionsPerAtom =
741 getMaxNumExclusionsPerAtom(mtop->moltype[molb.type].excls);
742 // We checked above that max 1 exclusion means only self exclusions
743 if (maxNumExclusionsPerAtom > 1)
745 dd->haveExclusions = true;
747 rt->n_excl_at_max = std::max(rt->n_excl_at_max, maxNumExclusionsPerAtom);
750 if (vsite && vsite->numInterUpdategroupVsites > 0)
754 fprintf(fplog, "There are %d inter update-group virtual sites,\n"
755 "will an extra communication step for selected coordinates and forces\n",
756 vsite->numInterUpdategroupVsites);
758 init_domdec_vsites(dd, vsite->numInterUpdategroupVsites);
761 if (dd->comm->systemInfo.haveSplitConstraints || dd->comm->systemInfo.haveSplitSettles)
763 init_domdec_constraints(dd, mtop);
767 fprintf(fplog, "\n");
771 /*! \brief Store a vsite interaction at the end of \p il
773 * This routine is very similar to add_ifunc, but vsites interactions
774 * have more work to do than other kinds of interactions, and the
775 * complex way nral (and thus vector contents) depends on ftype
776 * confuses static analysis tools unless we fuse the vsite
777 * atom-indexing organization code with the ifunc-adding code, so that
778 * they can see that nral is the same value. */
780 add_ifunc_for_vsites(t_iatom *tiatoms, const gmx_ga2la_t &ga2la,
781 int nral, gmx_bool bHomeA,
782 int a, int a_gl, int a_mol,
783 const t_iatom *iatoms,
788 if (il->nr+1+nral > il->nalloc)
790 il->nalloc = over_alloc_large(il->nr+1+nral);
791 srenew(il->iatoms, il->nalloc);
793 liatoms = il->iatoms + il->nr;
797 tiatoms[0] = iatoms[0];
801 /* We know the local index of the first atom */
806 /* Convert later in make_local_vsites */
807 tiatoms[1] = -a_gl - 1;
810 for (int k = 2; k < 1+nral; k++)
812 int ak_gl = a_gl + iatoms[k] - a_mol;
813 if (const int *homeIndex = ga2la.findHome(ak_gl))
815 tiatoms[k] = *homeIndex;
819 /* Copy the global index, convert later in make_local_vsites */
820 tiatoms[k] = -(ak_gl + 1);
822 // Note that ga2la_get_home always sets the third parameter if
825 for (int k = 0; k < 1+nral; k++)
827 liatoms[k] = tiatoms[k];
831 /*! \brief Store a bonded interaction at the end of \p il */
832 static inline void add_ifunc(int nral, const t_iatom *tiatoms, t_ilist *il)
837 if (il->nr+1+nral > il->nalloc)
839 il->nalloc = over_alloc_large(il->nr+1+nral);
840 srenew(il->iatoms, il->nalloc);
842 liatoms = il->iatoms + il->nr;
843 for (k = 0; k <= nral; k++)
845 liatoms[k] = tiatoms[k];
850 /*! \brief Store a position restraint in idef and iatoms, complex because the parameters are different for each entry */
851 static void add_posres(int mol, int a_mol, int numAtomsInMolecule,
852 const gmx_molblock_t *molb,
853 t_iatom *iatoms, const t_iparams *ip_in,
859 /* This position restraint has not been added yet,
860 * so it's index is the current number of position restraints.
862 n = idef->il[F_POSRES].nr/2;
863 if (n+1 > idef->iparams_posres_nalloc)
865 idef->iparams_posres_nalloc = over_alloc_dd(n+1);
866 srenew(idef->iparams_posres, idef->iparams_posres_nalloc);
868 ip = &idef->iparams_posres[n];
869 /* Copy the force constants */
870 *ip = ip_in[iatoms[0]];
872 /* Get the position restraint coordinates from the molblock */
873 a_molb = mol*numAtomsInMolecule + a_mol;
874 GMX_ASSERT(a_molb < ssize(molb->posres_xA), "We need a sufficient number of position restraint coordinates");
875 ip->posres.pos0A[XX] = molb->posres_xA[a_molb][XX];
876 ip->posres.pos0A[YY] = molb->posres_xA[a_molb][YY];
877 ip->posres.pos0A[ZZ] = molb->posres_xA[a_molb][ZZ];
878 if (!molb->posres_xB.empty())
880 ip->posres.pos0B[XX] = molb->posres_xB[a_molb][XX];
881 ip->posres.pos0B[YY] = molb->posres_xB[a_molb][YY];
882 ip->posres.pos0B[ZZ] = molb->posres_xB[a_molb][ZZ];
886 ip->posres.pos0B[XX] = ip->posres.pos0A[XX];
887 ip->posres.pos0B[YY] = ip->posres.pos0A[YY];
888 ip->posres.pos0B[ZZ] = ip->posres.pos0A[ZZ];
890 /* Set the parameter index for idef->iparams_posre */
894 /*! \brief Store a flat-bottomed position restraint in idef and iatoms, complex because the parameters are different for each entry */
895 static void add_fbposres(int mol, int a_mol, int numAtomsInMolecule,
896 const gmx_molblock_t *molb,
897 t_iatom *iatoms, const t_iparams *ip_in,
903 /* This flat-bottom position restraint has not been added yet,
904 * so it's index is the current number of position restraints.
906 n = idef->il[F_FBPOSRES].nr/2;
907 if (n+1 > idef->iparams_fbposres_nalloc)
909 idef->iparams_fbposres_nalloc = over_alloc_dd(n+1);
910 srenew(idef->iparams_fbposres, idef->iparams_fbposres_nalloc);
912 ip = &idef->iparams_fbposres[n];
913 /* Copy the force constants */
914 *ip = ip_in[iatoms[0]];
916 /* Get the position restraint coordinats from the molblock */
917 a_molb = mol*numAtomsInMolecule + a_mol;
918 GMX_ASSERT(a_molb < ssize(molb->posres_xA), "We need a sufficient number of position restraint coordinates");
919 /* Take reference positions from A position of normal posres */
920 ip->fbposres.pos0[XX] = molb->posres_xA[a_molb][XX];
921 ip->fbposres.pos0[YY] = molb->posres_xA[a_molb][YY];
922 ip->fbposres.pos0[ZZ] = molb->posres_xA[a_molb][ZZ];
924 /* Note: no B-type for flat-bottom posres */
926 /* Set the parameter index for idef->iparams_posre */
930 /*! \brief Store a virtual site interaction, complex because of PBC and recursion */
931 static void add_vsite(const gmx_ga2la_t &ga2la,
932 gmx::ArrayRef<const int> index,
933 gmx::ArrayRef<const int> rtil,
935 gmx_bool bHomeA, int a, int a_gl, int a_mol,
936 const t_iatom *iatoms,
940 t_iatom tiatoms[1+MAXATOMLIST];
941 int j, ftype_r, nral_r;
943 /* Add this interaction to the local topology */
944 add_ifunc_for_vsites(tiatoms, ga2la, nral, bHomeA, a, a_gl, a_mol, iatoms, &idef->il[ftype]);
948 /* Check for recursion */
949 for (k = 2; k < 1+nral; k++)
951 if ((iatoms[1+nral] & (2<<k)) && (tiatoms[k] < 0))
953 /* This construction atoms is a vsite and not a home atom */
956 fprintf(debug, "Constructing atom %d of vsite atom %d is a vsite and non-home\n", iatoms[k]+1, a_mol+1);
958 /* Find the vsite construction */
960 /* Check all interactions assigned to this atom */
961 j = index[iatoms[k]];
962 while (j < index[iatoms[k]+1])
965 nral_r = NRAL(ftype_r);
966 if (interaction_function[ftype_r].flags & IF_VSITE)
968 /* Add this vsite (recursion) */
969 add_vsite(ga2la, index, rtil, ftype_r, nral_r,
970 FALSE, -1, a_gl+iatoms[k]-iatoms[1], iatoms[k],
974 j += 1 + nral_rt(ftype_r);
981 /*! \brief Returns the squared distance between atoms \p i and \p j */
982 static real dd_dist2(t_pbc *pbc_null, const rvec *x, const int i, int j)
988 pbc_dx_aiuc(pbc_null, x[i], x[j], dx);
992 rvec_sub(x[i], x[j], dx);
998 /*! \brief Append t_blocka block structures 1 to nsrc in src to *dest */
999 static void combine_blocka(t_blocka *dest,
1000 gmx::ArrayRef<const thread_work_t> src)
1002 int ni = src.back().excl.nr;
1004 for (const thread_work_t &th_work : src)
1006 na += th_work.excl.nra;
1008 if (ni + 1 > dest->nalloc_index)
1010 dest->nalloc_index = over_alloc_large(ni+1);
1011 srenew(dest->index, dest->nalloc_index);
1013 if (dest->nra + na > dest->nalloc_a)
1015 dest->nalloc_a = over_alloc_large(dest->nra+na);
1016 srenew(dest->a, dest->nalloc_a);
1018 for (gmx::index s = 1; s < src.ssize(); s++)
1020 for (int i = dest->nr + 1; i < src[s].excl.nr + 1; i++)
1022 dest->index[i] = dest->nra + src[s].excl.index[i];
1024 for (int i = 0; i < src[s].excl.nra; i++)
1026 dest->a[dest->nra+i] = src[s].excl.a[i];
1028 dest->nr = src[s].excl.nr;
1029 dest->nra += src[s].excl.nra;
1033 /*! \brief Append t_idef structures 1 to nsrc in src to *dest */
1034 static void combine_idef(t_idef *dest,
1035 gmx::ArrayRef<const thread_work_t> src)
1039 for (ftype = 0; ftype < F_NRE; ftype++)
1042 for (gmx::index s = 1; s < src.ssize(); s++)
1044 n += src[s].idef.il[ftype].nr;
1050 ild = &dest->il[ftype];
1052 if (ild->nr + n > ild->nalloc)
1054 ild->nalloc = over_alloc_large(ild->nr+n);
1055 srenew(ild->iatoms, ild->nalloc);
1058 for (gmx::index s = 1; s < src.ssize(); s++)
1060 const t_ilist &ils = src[s].idef.il[ftype];
1062 for (int i = 0; i < ils.nr; i++)
1064 ild->iatoms[ild->nr + i] = ils.iatoms[i];
1070 /* Position restraints need an additional treatment */
1071 if (ftype == F_POSRES || ftype == F_FBPOSRES)
1073 int nposres = dest->il[ftype].nr/2;
1074 // TODO: Simplify this code using std::vector
1075 t_iparams * &iparams_dest = (ftype == F_POSRES ? dest->iparams_posres : dest->iparams_fbposres);
1076 int &posres_nalloc = (ftype == F_POSRES ? dest->iparams_posres_nalloc : dest->iparams_fbposres_nalloc);
1077 if (nposres > posres_nalloc)
1079 posres_nalloc = over_alloc_large(nposres);
1080 srenew(iparams_dest, posres_nalloc);
1083 /* Set nposres to the number of original position restraints in dest */
1084 for (gmx::index s = 1; s < src.ssize(); s++)
1086 nposres -= src[s].idef.il[ftype].nr/2;
1089 for (gmx::index s = 1; s < src.ssize(); s++)
1091 const t_iparams *iparams_src = (ftype == F_POSRES ? src[s].idef.iparams_posres : src[s].idef.iparams_fbposres);
1093 for (int i = 0; i < src[s].idef.il[ftype].nr/2; i++)
1095 /* Correct the index into iparams_posres */
1096 dest->il[ftype].iatoms[nposres*2] = nposres;
1097 /* Copy the position restraint force parameters */
1098 iparams_dest[nposres] = iparams_src[i];
1107 /*! \brief Check and when available assign bonded interactions for local atom i
1110 check_assign_interactions_atom(int i, int i_gl,
1112 int numAtomsInMolecule,
1113 gmx::ArrayRef<const int> index,
1114 gmx::ArrayRef<const int> rtil,
1115 gmx_bool bInterMolInteractions,
1116 int ind_start, int ind_end,
1117 const gmx_domdec_t *dd,
1118 const gmx_domdec_zones_t *zones,
1119 const gmx_molblock_t *molb,
1120 gmx_bool bRCheckMB, const ivec rcheck, gmx_bool bRCheck2B,
1124 const t_iparams *ip_in,
1135 t_iatom tiatoms[1 + MAXATOMLIST];
1137 const int ftype = rtil[j++];
1138 auto iatoms = gmx::constArrayRefFromArray(rtil.data() + j, rtil.size() - j);
1139 const int nral = NRAL(ftype);
1140 if (interaction_function[ftype].flags & IF_VSITE)
1142 assert(!bInterMolInteractions);
1143 /* The vsite construction goes where the vsite itself is */
1146 add_vsite(*dd->ga2la, index, rtil, ftype, nral,
1147 TRUE, i, i_gl, i_mol,
1148 iatoms.data(), idef);
1157 tiatoms[0] = iatoms[0];
1161 assert(!bInterMolInteractions);
1162 /* Assign single-body interactions to the home zone */
1167 if (ftype == F_POSRES)
1169 add_posres(mol, i_mol, numAtomsInMolecule,
1170 molb, tiatoms, ip_in, idef);
1172 else if (ftype == F_FBPOSRES)
1174 add_fbposres(mol, i_mol, numAtomsInMolecule,
1175 molb, tiatoms, ip_in, idef);
1185 /* This is a two-body interaction, we can assign
1186 * analogous to the non-bonded assignments.
1190 if (!bInterMolInteractions)
1192 /* Get the global index using the offset in the molecule */
1193 k_gl = i_gl + iatoms[2] - i_mol;
1199 if (const auto *entry = dd->ga2la->find(k_gl))
1201 int kz = entry->cell;
1206 /* Check zone interaction assignments */
1207 bUse = ((iz < zones->nizone &&
1209 kz >= zones->izone[iz].j0 &&
1210 kz < zones->izone[iz].j1) ||
1211 (kz < zones->nizone &&
1213 iz >= zones->izone[kz].j0 &&
1214 iz < zones->izone[kz].j1));
1217 GMX_ASSERT(ftype != F_CONSTR || (iz == 0 && kz == 0),
1218 "Constraint assigned here should only involve home atoms");
1221 tiatoms[2] = entry->la;
1222 /* If necessary check the cgcm distance */
1224 dd_dist2(pbc_null, cg_cm,
1225 tiatoms[1], tiatoms[2]) >= rc2)
1238 /* Assign this multi-body bonded interaction to
1239 * the local node if we have all the atoms involved
1240 * (local or communicated) and the minimum zone shift
1241 * in each dimension is zero, for dimensions
1242 * with 2 DD cells an extra check may be necessary.
1244 ivec k_zero, k_plus;
1250 for (k = 1; k <= nral && bUse; k++)
1253 if (!bInterMolInteractions)
1255 /* Get the global index using the offset in the molecule */
1256 k_gl = i_gl + iatoms[k] - i_mol;
1262 const auto *entry = dd->ga2la->find(k_gl);
1263 if (entry == nullptr || entry->cell >= zones->n)
1265 /* We do not have this atom of this interaction
1266 * locally, or it comes from more than one cell
1275 tiatoms[k] = entry->la;
1276 for (d = 0; d < DIM; d++)
1278 if (zones->shift[entry->cell][d] == 0)
1290 (k_zero[XX] != 0) && (k_zero[YY] != 0) && (k_zero[ZZ] != 0));
1295 for (d = 0; (d < DIM && bUse); d++)
1297 /* Check if the cg_cm distance falls within
1298 * the cut-off to avoid possible multiple
1299 * assignments of bonded interactions.
1303 dd_dist2(pbc_null, cg_cm,
1304 tiatoms[k_zero[d]], tiatoms[k_plus[d]]) >= rc2)
1313 /* Add this interaction to the local topology */
1314 add_ifunc(nral, tiatoms, &idef->il[ftype]);
1315 /* Sum so we can check in global_stat
1316 * if we have everything.
1319 !(interaction_function[ftype].flags & IF_LIMZERO))
1329 /*! \brief This function looks up and assigns bonded interactions for zone iz.
1331 * With thread parallelizing each thread acts on a different atom range:
1332 * at_start to at_end.
1334 static int make_bondeds_zone(gmx_domdec_t *dd,
1335 const gmx_domdec_zones_t *zones,
1336 const std::vector<gmx_molblock_t> &molb,
1337 gmx_bool bRCheckMB, ivec rcheck, gmx_bool bRCheck2B,
1339 t_pbc *pbc_null, rvec *cg_cm,
1340 const t_iparams *ip_in,
1343 const gmx::Range<int> &atomRange)
1345 int mb, mt, mol, i_mol;
1347 gmx_reverse_top_t *rt;
1350 rt = dd->reverse_top;
1352 bBCheck = rt->bBCheck;
1356 for (int i : atomRange)
1358 /* Get the global atom number */
1359 const int i_gl = dd->globalAtomIndices[i];
1360 global_atomnr_to_moltype_ind(rt, i_gl, &mb, &mt, &mol, &i_mol);
1361 /* Check all intramolecular interactions assigned to this atom */
1362 gmx::ArrayRef<const int> index = rt->ril_mt[mt].index;
1363 gmx::ArrayRef<const t_iatom> rtil = rt->ril_mt[mt].il;
1365 check_assign_interactions_atom(i, i_gl, mol, i_mol,
1366 rt->ril_mt[mt].numAtomsInMolecule,
1368 index[i_mol], index[i_mol+1],
1371 bRCheckMB, rcheck, bRCheck2B, rc2,
1381 if (rt->bIntermolecularInteractions)
1383 /* Check all intermolecular interactions assigned to this atom */
1384 index = rt->ril_intermol.index;
1385 rtil = rt->ril_intermol.il;
1387 check_assign_interactions_atom(i, i_gl, mol, i_mol,
1388 rt->ril_mt[mt].numAtomsInMolecule,
1390 index[i_gl], index[i_gl + 1],
1393 bRCheckMB, rcheck, bRCheck2B, rc2,
1404 return nbonded_local;
1407 /*! \brief Set the exclusion data for i-zone \p iz for the case of no exclusions */
1408 static void set_no_exclusions_zone(const gmx_domdec_zones_t *zones,
1412 for (int a = zones->cg_range[iz]; a < zones->cg_range[iz + 1]; a++)
1414 lexcls->index[a + 1] = lexcls->nra;
1418 /*! \brief Set the exclusion data for i-zone \p iz */
1419 static void make_exclusions_zone(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
1420 const std::vector<gmx_moltype_t> &moltype,
1421 const int *cginfo, t_blocka *lexcls, int iz,
1422 int at_start, int at_end,
1423 const gmx::ArrayRef<const int> intermolecularExclusionGroup)
1425 int n_excl_at_max, n, at;
1427 const gmx_ga2la_t &ga2la = *dd->ga2la;
1429 /* Extract the j-atom range */
1430 const gmx::Range<int> jRange(zones->izone[iz].jcg0,
1431 zones->izone[iz].jcg1);
1433 n_excl_at_max = dd->reverse_top->n_excl_at_max;
1435 /* We set the end index, but note that we might not start at zero here */
1436 lexcls->nr = at_end;
1439 for (at = at_start; at < at_end; at++)
1441 if (n + 1000 > lexcls->nalloc_a)
1443 lexcls->nalloc_a = over_alloc_large(n + 1000);
1444 srenew(lexcls->a, lexcls->nalloc_a);
1447 if (GET_CGINFO_EXCL_INTER(cginfo[at]))
1449 int a_gl, mb, mt, mol, a_mol, j;
1450 const t_blocka *excls;
1452 if (n + n_excl_at_max > lexcls->nalloc_a)
1454 lexcls->nalloc_a = over_alloc_large(n + n_excl_at_max);
1455 srenew(lexcls->a, lexcls->nalloc_a);
1458 /* Copy the exclusions from the global top */
1459 lexcls->index[at] = n;
1460 a_gl = dd->globalAtomIndices[at];
1461 global_atomnr_to_moltype_ind(dd->reverse_top, a_gl,
1462 &mb, &mt, &mol, &a_mol);
1463 excls = &moltype[mt].excls;
1464 for (j = excls->index[a_mol]; j < excls->index[a_mol + 1]; j++)
1466 const int aj_mol = excls->a[j];
1468 if (const auto *jEntry = ga2la.find(a_gl + aj_mol - a_mol))
1470 /* This check is not necessary, but it can reduce
1471 * the number of exclusions in the list, which in turn
1472 * can speed up the pair list construction a bit.
1474 if (jRange.isInRange(jEntry->la))
1476 lexcls->a[n++] = jEntry->la;
1483 /* We don't need exclusions for this atom */
1484 lexcls->index[at] = n;
1487 bool isExcludedAtom = !intermolecularExclusionGroup.empty() &&
1488 std::find(intermolecularExclusionGroup.begin(),
1489 intermolecularExclusionGroup.end(),
1490 dd->globalAtomIndices[at]) !=
1491 intermolecularExclusionGroup.end();
1495 if (n + intermolecularExclusionGroup.ssize() > lexcls->nalloc_a)
1498 over_alloc_large(n + intermolecularExclusionGroup.size());
1499 srenew(lexcls->a, lexcls->nalloc_a);
1501 for (int qmAtomGlobalIndex : intermolecularExclusionGroup)
1503 if (const auto *entry = dd->ga2la->find(qmAtomGlobalIndex))
1505 lexcls->a[n++] = entry->la;
1511 lexcls->index[lexcls->nr] = n;
1516 /*! \brief Ensure we have enough space in \p ba for \p nindex_max indices */
1517 static void check_alloc_index(t_blocka *ba, int nindex_max)
1519 if (nindex_max+1 > ba->nalloc_index)
1521 ba->nalloc_index = over_alloc_dd(nindex_max+1);
1522 srenew(ba->index, ba->nalloc_index);
1526 /*! \brief Ensure that we have enough space for exclusion storate in \p lexcls */
1527 static void check_exclusions_alloc(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
1530 const int nr = zones->izone[zones->nizone - 1].cg1;
1532 check_alloc_index(lexcls, nr);
1534 for (size_t thread = 1; thread < dd->reverse_top->th_work.size(); thread++)
1536 check_alloc_index(&dd->reverse_top->th_work[thread].excl, nr);
1540 /*! \brief Set the total count indexes for the local exclusions, needed by several functions */
1541 static void finish_local_exclusions(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
1544 const gmx::Range<int> nonhomeIzonesAtomRange(zones->izone[0].cg1,
1545 zones->izone[zones->nizone - 1].cg1);
1547 if (!dd->haveExclusions)
1549 /* There are no exclusions involving non-home charge groups,
1550 * but we need to set the indices for neighborsearching.
1552 for (int la : nonhomeIzonesAtomRange)
1554 lexcls->index[la] = lexcls->nra;
1557 /* nr is only used to loop over the exclusions for Ewald and RF,
1558 * so we can set it to the number of home atoms for efficiency.
1560 lexcls->nr = nonhomeIzonesAtomRange.begin();
1564 lexcls->nr = nonhomeIzonesAtomRange.end();
1568 /*! \brief Clear a t_idef struct */
1569 static void clear_idef(t_idef *idef)
1573 /* Clear the counts */
1574 for (ftype = 0; ftype < F_NRE; ftype++)
1576 idef->il[ftype].nr = 0;
1580 /*! \brief Generate and store all required local bonded interactions in \p idef and local exclusions in \p lexcls */
1581 static int make_local_bondeds_excls(gmx_domdec_t *dd,
1582 gmx_domdec_zones_t *zones,
1583 const gmx_mtop_t *mtop,
1585 gmx_bool bRCheckMB, ivec rcheck, gmx_bool bRCheck2B,
1587 t_pbc *pbc_null, rvec *cg_cm,
1589 t_blocka *lexcls, int *excl_count)
1591 int nzone_bondeds, nzone_excl;
1592 int izone, cg0, cg1;
1595 gmx_reverse_top_t *rt;
1597 if (dd->reverse_top->bInterCGInteractions)
1599 nzone_bondeds = zones->n;
1603 /* Only single charge group (or atom) molecules, so interactions don't
1604 * cross zone boundaries and we only need to assign in the home zone.
1609 if (dd->haveExclusions)
1611 /* We only use exclusions from i-zones to i- and j-zones */
1612 nzone_excl = zones->nizone;
1616 /* There are no exclusions and only zone 0 sees itself */
1620 check_exclusions_alloc(dd, zones, lexcls);
1622 rt = dd->reverse_top;
1626 /* Clear the counts */
1634 for (izone = 0; izone < nzone_bondeds; izone++)
1636 cg0 = zones->cg_range[izone];
1637 cg1 = zones->cg_range[izone + 1];
1639 const int numThreads = rt->th_work.size();
1640 #pragma omp parallel for num_threads(numThreads) schedule(static)
1641 for (int thread = 0; thread < numThreads; thread++)
1649 cg0t = cg0 + ((cg1 - cg0)* thread )/numThreads;
1650 cg1t = cg0 + ((cg1 - cg0)*(thread+1))/numThreads;
1658 idef_t = &rt->th_work[thread].idef;
1662 rt->th_work[thread].nbonded =
1663 make_bondeds_zone(dd, zones,
1665 bRCheckMB, rcheck, bRCheck2B, rc2,
1666 pbc_null, cg_cm, idef->iparams,
1669 gmx::Range<int>(cg0t, cg1t));
1671 if (izone < nzone_excl)
1679 excl_t = &rt->th_work[thread].excl;
1684 /* No charge groups and no distance check required */
1685 make_exclusions_zone(dd, zones, mtop->moltype, cginfo,
1686 excl_t, izone, cg0t,
1688 mtop->intermolecularExclusionGroup);
1691 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
1694 if (rt->th_work.size() > 1)
1696 combine_idef(idef, rt->th_work);
1699 for (const thread_work_t &th_work : rt->th_work)
1701 nbonded_local += th_work.nbonded;
1704 if (izone < nzone_excl)
1706 if (rt->th_work.size() > 1)
1708 combine_blocka(lexcls, rt->th_work);
1711 for (const thread_work_t &th_work : rt->th_work)
1713 *excl_count += th_work.excl_count;
1718 /* Some zones might not have exclusions, but some code still needs to
1719 * loop over the index, so we set the indices here.
1721 for (izone = nzone_excl; izone < zones->nizone; izone++)
1723 set_no_exclusions_zone(zones, izone, lexcls);
1726 finish_local_exclusions(dd, zones, lexcls);
1729 fprintf(debug, "We have %d exclusions, check count %d\n",
1730 lexcls->nra, *excl_count);
1733 return nbonded_local;
1736 void dd_make_local_top(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
1737 int npbcdim, matrix box,
1738 rvec cellsize_min, const ivec npulse,
1741 const gmx_mtop_t &mtop, gmx_localtop_t *ltop)
1743 gmx_bool bRCheckMB, bRCheck2B;
1747 t_pbc pbc, *pbc_null = nullptr;
1751 fprintf(debug, "Making local topology\n");
1757 if (dd->reverse_top->bInterCGInteractions)
1759 /* We need to check to which cell bondeds should be assigned */
1760 rc = dd_cutoff_twobody(dd);
1763 fprintf(debug, "Two-body bonded cut-off distance is %g\n", rc);
1766 /* Should we check cg_cm distances when assigning bonded interactions? */
1767 for (d = 0; d < DIM; d++)
1770 /* Only need to check for dimensions where the part of the box
1771 * that is not communicated is smaller than the cut-off.
1773 if (d < npbcdim && dd->nc[d] > 1 &&
1774 (dd->nc[d] - npulse[d])*cellsize_min[d] < 2*rc)
1781 /* Check for interactions between two atoms,
1782 * where we can allow interactions up to the cut-off,
1783 * instead of up to the smallest cell dimension.
1790 "dim %d cellmin %f bonded rcheck[%d] = %d, bRCheck2B = %s\n",
1791 d, cellsize_min[d], d, rcheck[d], gmx::boolToString(bRCheck2B));
1794 if (bRCheckMB || bRCheck2B)
1798 pbc_null = set_pbc_dd(&pbc, fr->ePBC, dd->nc, TRUE, box);
1808 make_local_bondeds_excls(dd, zones, &mtop, fr->cginfo.data(),
1809 bRCheckMB, rcheck, bRCheck2B, rc,
1810 pbc_null, cgcm_or_x,
1812 <op->excls, &nexcl);
1814 /* The ilist is not sorted yet,
1815 * we can only do this when we have the charge arrays.
1817 ltop->idef.ilsort = ilsortUNKNOWN;
1819 ltop->atomtypes = mtop.atomtypes;
1822 void dd_sort_local_top(gmx_domdec_t *dd, const t_mdatoms *mdatoms,
1823 gmx_localtop_t *ltop)
1825 if (dd->reverse_top->ilsort == ilsortNO_FE)
1827 ltop->idef.ilsort = ilsortNO_FE;
1831 gmx_sort_ilist_fe(<op->idef, mdatoms->chargeA, mdatoms->chargeB);
1835 void dd_init_local_top(const gmx_mtop_t &top_global,
1836 gmx_localtop_t *top)
1838 /* TODO: Get rid of the const casts below, e.g. by using a reference */
1839 top->idef.ntypes = top_global.ffparams.numTypes();
1840 top->idef.atnr = top_global.ffparams.atnr;
1841 top->idef.functype = const_cast<t_functype *>(top_global.ffparams.functype.data());
1842 top->idef.iparams = const_cast<t_iparams *>(top_global.ffparams.iparams.data());
1843 top->idef.fudgeQQ = top_global.ffparams.fudgeQQ;
1844 top->idef.cmap_grid = new gmx_cmap_t;
1845 *top->idef.cmap_grid = top_global.ffparams.cmap_grid;
1847 top->idef.ilsort = ilsortUNKNOWN;
1848 top->useInDomainDecomp_ = true;
1851 void dd_init_local_state(gmx_domdec_t *dd,
1852 const t_state *state_global, t_state *state_local)
1854 int buf[NITEM_DD_INIT_LOCAL_STATE];
1858 buf[0] = state_global->flags;
1859 buf[1] = state_global->ngtc;
1860 buf[2] = state_global->nnhpres;
1861 buf[3] = state_global->nhchainlength;
1862 buf[4] = state_global->dfhist ? state_global->dfhist->nlambda : 0;
1864 dd_bcast(dd, NITEM_DD_INIT_LOCAL_STATE*sizeof(int), buf);
1866 init_gtc_state(state_local, buf[1], buf[2], buf[3]);
1867 init_dfhist_state(state_local, buf[4]);
1868 state_local->flags = buf[0];
1871 /*! \brief Check if a link is stored in \p link between charge groups \p cg_gl and \p cg_gl_j and if not so, store a link */
1872 static void check_link(t_blocka *link, int cg_gl, int cg_gl_j)
1878 for (k = link->index[cg_gl]; k < link->index[cg_gl+1]; k++)
1880 GMX_RELEASE_ASSERT(link->a, "Inconsistent NULL pointer while making charge-group links");
1881 if (link->a[k] == cg_gl_j)
1888 GMX_RELEASE_ASSERT(link->a || link->index[cg_gl+1]+1 > link->nalloc_a,
1889 "Inconsistent allocation of link");
1890 /* Add this charge group link */
1891 if (link->index[cg_gl+1]+1 > link->nalloc_a)
1893 link->nalloc_a = over_alloc_large(link->index[cg_gl+1]+1);
1894 srenew(link->a, link->nalloc_a);
1896 link->a[link->index[cg_gl+1]] = cg_gl_j;
1897 link->index[cg_gl+1]++;
1901 /*! \brief Return a vector of the charge group index for all atoms */
1902 static std::vector<int> make_at2cg(const t_block &cgs)
1904 std::vector<int> at2cg(cgs.index[cgs.nr]);
1905 for (int cg = 0; cg < cgs.nr; cg++)
1907 for (int a = cgs.index[cg]; a < cgs.index[cg + 1]; a++)
1916 t_blocka *makeBondedLinks(const gmx_mtop_t *mtop,
1917 cginfo_mb_t *cginfo_mb)
1920 cginfo_mb_t *cgi_mb;
1922 /* For each charge group make a list of other charge groups
1923 * in the system that a linked to it via bonded interactions
1924 * which are also stored in reverse_top.
1927 reverse_ilist_t ril_intermol;
1928 if (mtop->bIntermolecularInteractions)
1930 if (ncg_mtop(mtop) < mtop->natoms)
1932 gmx_fatal(FARGS, "The combination of intermolecular interactions, charge groups and domain decomposition is not supported. Use cutoff-scheme=Verlet (which removes the charge groups) or run without domain decomposition.");
1937 atoms.nr = mtop->natoms;
1938 atoms.atom = nullptr;
1940 GMX_RELEASE_ASSERT(mtop->intermolecular_ilist, "We should have an ilist when intermolecular interactions are on");
1942 make_reverse_ilist(*mtop->intermolecular_ilist,
1944 FALSE, FALSE, FALSE, TRUE, &ril_intermol);
1948 snew(link->index, ncg_mtop(mtop)+1);
1955 for (size_t mb = 0; mb < mtop->molblock.size(); mb++)
1957 const gmx_molblock_t &molb = mtop->molblock[mb];
1962 const gmx_moltype_t &molt = mtop->moltype[molb.type];
1963 const t_block &cgs = molt.cgs;
1964 std::vector<int> a2c = make_at2cg(cgs);
1965 /* Make a reverse ilist in which the interactions are linked
1966 * to all atoms, not only the first atom as in gmx_reverse_top.
1967 * The constraints are discarded here.
1969 reverse_ilist_t ril;
1970 make_reverse_ilist(molt.ilist, &molt.atoms,
1971 FALSE, FALSE, FALSE, TRUE, &ril);
1973 cgi_mb = &cginfo_mb[mb];
1976 for (mol = 0; mol < (mtop->bIntermolecularInteractions ? molb.nmol : 1); mol++)
1978 for (int cg = 0; cg < cgs.nr; cg++)
1980 int cg_gl = cg_offset + cg;
1981 link->index[cg_gl+1] = link->index[cg_gl];
1982 for (int a = cgs.index[cg]; a < cgs.index[cg + 1]; a++)
1984 int i = ril.index[a];
1985 while (i < ril.index[a+1])
1987 int ftype = ril.il[i++];
1988 int nral = NRAL(ftype);
1989 /* Skip the ifunc index */
1991 for (int j = 0; j < nral; j++)
1993 int aj = ril.il[i + j];
1996 check_link(link, cg_gl, cg_offset+a2c[aj]);
1999 i += nral_rt(ftype);
2002 if (mtop->bIntermolecularInteractions)
2004 int i = ril_intermol.index[a];
2005 while (i < ril_intermol.index[a+1])
2007 int ftype = ril_intermol.il[i++];
2008 int nral = NRAL(ftype);
2009 /* Skip the ifunc index */
2011 for (int j = 0; j < nral; j++)
2013 /* Here we assume we have no charge groups;
2014 * this has been checked above.
2016 int aj = ril_intermol.il[i + j];
2017 check_link(link, cg_gl, aj);
2019 i += nral_rt(ftype);
2023 if (link->index[cg_gl+1] - link->index[cg_gl] > 0)
2025 SET_CGINFO_BOND_INTER(cgi_mb->cginfo[cg]);
2030 cg_offset += cgs.nr;
2032 int nlink_mol = link->index[cg_offset] - link->index[cg_offset - cgs.nr];
2036 fprintf(debug, "molecule type '%s' %d cgs has %d cg links through bonded interac.\n", *molt.name, cgs.nr, nlink_mol);
2039 if (molb.nmol > mol)
2041 /* Copy the data for the rest of the molecules in this block */
2042 link->nalloc_a += (molb.nmol - mol)*nlink_mol;
2043 srenew(link->a, link->nalloc_a);
2044 for (; mol < molb.nmol; mol++)
2046 for (int cg = 0; cg < cgs.nr; cg++)
2048 int cg_gl = cg_offset + cg;
2049 link->index[cg_gl + 1] =
2050 link->index[cg_gl + 1 - cgs.nr] + nlink_mol;
2051 for (int j = link->index[cg_gl]; j < link->index[cg_gl+1]; j++)
2053 link->a[j] = link->a[j - nlink_mol] + cgs.nr;
2055 if (link->index[cg_gl+1] - link->index[cg_gl] > 0 &&
2056 cg_gl - cgi_mb->cg_start < cgi_mb->cg_mod)
2058 SET_CGINFO_BOND_INTER(cgi_mb->cginfo[cg_gl - cgi_mb->cg_start]);
2062 cg_offset += cgs.nr;
2069 fprintf(debug, "Of the %d charge groups %d are linked via bonded interactions\n", ncg_mtop(mtop), ncgi);
2080 } bonded_distance_t;
2082 /*! \brief Compare distance^2 \p r2 against the distance in \p bd and if larger store it along with \p ftype and atom indices \p a1 and \p a2 */
2083 static void update_max_bonded_distance(real r2, int ftype, int a1, int a2,
2084 bonded_distance_t *bd)
2095 /*! \brief Set the distance, function type and atom indices for the longest distance between charge-groups of molecule type \p molt for two-body and multi-body bonded interactions */
2096 static void bonded_cg_distance_mol(const gmx_moltype_t *molt,
2097 const std::vector<int> &at2cg,
2098 gmx_bool bBCheck, gmx_bool bExcl, rvec *cg_cm,
2099 bonded_distance_t *bd_2b,
2100 bonded_distance_t *bd_mb)
2102 for (int ftype = 0; ftype < F_NRE; ftype++)
2104 if (dd_check_ftype(ftype, bBCheck, FALSE, FALSE))
2106 const auto &il = molt->ilist[ftype];
2107 int nral = NRAL(ftype);
2110 for (int i = 0; i < il.size(); i += 1+nral)
2112 for (int ai = 0; ai < nral; ai++)
2114 int cgi = at2cg[il.iatoms[i+1+ai]];
2115 for (int aj = ai + 1; aj < nral; aj++)
2117 int cgj = at2cg[il.iatoms[i+1+aj]];
2120 real rij2 = distance2(cg_cm[cgi], cg_cm[cgj]);
2122 update_max_bonded_distance(rij2, ftype,
2125 (nral == 2) ? bd_2b : bd_mb);
2135 const t_blocka *excls = &molt->excls;
2136 for (int ai = 0; ai < excls->nr; ai++)
2138 int cgi = at2cg[ai];
2139 for (int j = excls->index[ai]; j < excls->index[ai+1]; j++)
2141 int cgj = at2cg[excls->a[j]];
2144 real rij2 = distance2(cg_cm[cgi], cg_cm[cgj]);
2146 /* There is no function type for exclusions, use -1 */
2147 update_max_bonded_distance(rij2, -1, ai, excls->a[j], bd_2b);
2154 /*! \brief Set the distance, function type and atom indices for the longest atom distance involved in intermolecular interactions for two-body and multi-body bonded interactions */
2155 static void bonded_distance_intermol(const InteractionLists &ilists_intermol,
2157 const rvec *x, int ePBC, const matrix box,
2158 bonded_distance_t *bd_2b,
2159 bonded_distance_t *bd_mb)
2163 set_pbc(&pbc, ePBC, box);
2165 for (int ftype = 0; ftype < F_NRE; ftype++)
2167 if (dd_check_ftype(ftype, bBCheck, FALSE, FALSE))
2169 const auto &il = ilists_intermol[ftype];
2170 int nral = NRAL(ftype);
2172 /* No nral>1 check here, since intermol interactions always
2173 * have nral>=2 (and the code is also correct for nral=1).
2175 for (int i = 0; i < il.size(); i += 1+nral)
2177 for (int ai = 0; ai < nral; ai++)
2179 int atom_i = il.iatoms[i + 1 + ai];
2181 for (int aj = ai + 1; aj < nral; aj++)
2186 int atom_j = il.iatoms[i + 1 + aj];
2188 pbc_dx(&pbc, x[atom_i], x[atom_j], dx);
2192 update_max_bonded_distance(rij2, ftype,
2194 (nral == 2) ? bd_2b : bd_mb);
2202 //! Returns whether \p molt has at least one virtual site
2203 static bool moltypeHasVsite(const gmx_moltype_t &molt)
2205 bool hasVsite = false;
2206 for (int i = 0; i < F_NRE; i++)
2208 if ((interaction_function[i].flags & IF_VSITE) &&
2209 molt.ilist[i].size() > 0)
2218 //! Compute charge group centers of mass for molecule \p molt
2219 static void get_cgcm_mol(const gmx_moltype_t *molt,
2220 const gmx_ffparams_t *ffparams,
2221 int ePBC, t_graph *graph, const matrix box,
2222 const rvec *x, rvec *xs, rvec *cg_cm)
2226 if (ePBC != epbcNONE)
2228 mk_mshift(nullptr, graph, ePBC, box, x);
2230 shift_x(graph, box, x, xs);
2231 /* By doing an extra mk_mshift the molecules that are broken
2232 * because they were e.g. imported from another software
2233 * will be made whole again. Such are the healing powers
2236 mk_mshift(nullptr, graph, ePBC, box, xs);
2240 /* We copy the coordinates so the original coordinates remain
2241 * unchanged, just to be 100% sure that we do not affect
2242 * binary reproducibility of simulations.
2244 n = molt->cgs.index[molt->cgs.nr];
2245 for (i = 0; i < n; i++)
2247 copy_rvec(x[i], xs[i]);
2251 if (moltypeHasVsite(*molt))
2253 /* Convert to old, deprecated format */
2254 t_ilist ilist[F_NRE];
2255 for (int ftype = 0; ftype < F_NRE; ftype++)
2257 if (interaction_function[ftype].flags & IF_VSITE)
2259 ilist[ftype].nr = molt->ilist[ftype].size();
2260 ilist[ftype].iatoms = const_cast<int *>(molt->ilist[ftype].iatoms.data());
2264 construct_vsites(nullptr, xs, 0.0, nullptr,
2265 ffparams->iparams.data(), ilist,
2266 epbcNONE, TRUE, nullptr, nullptr);
2269 calc_cgcm(nullptr, 0, molt->cgs.nr, &molt->cgs, xs, cg_cm);
2272 void dd_bonded_cg_distance(const gmx::MDLogger &mdlog,
2273 const gmx_mtop_t *mtop,
2274 const t_inputrec *ir,
2275 const rvec *x, const matrix box,
2277 real *r_2b, real *r_mb)
2279 gmx_bool bExclRequired;
2283 bonded_distance_t bd_2b = { 0, -1, -1, -1 };
2284 bonded_distance_t bd_mb = { 0, -1, -1, -1 };
2286 bExclRequired = inputrecExclForces(ir);
2291 for (const gmx_molblock_t &molb : mtop->molblock)
2293 const gmx_moltype_t &molt = mtop->moltype[molb.type];
2294 if (molt.cgs.nr == 1 || molb.nmol == 0)
2296 at_offset += molb.nmol*molt.atoms.nr;
2300 if (ir->ePBC != epbcNONE)
2302 mk_graph_moltype(molt, &graph);
2305 std::vector<int> at2cg = make_at2cg(molt.cgs);
2306 snew(xs, molt.atoms.nr);
2307 snew(cg_cm, molt.cgs.nr);
2308 for (int mol = 0; mol < molb.nmol; mol++)
2310 get_cgcm_mol(&molt, &mtop->ffparams, ir->ePBC, &graph, box,
2311 x+at_offset, xs, cg_cm);
2313 bonded_distance_t bd_mol_2b = { 0, -1, -1, -1 };
2314 bonded_distance_t bd_mol_mb = { 0, -1, -1, -1 };
2316 bonded_cg_distance_mol(&molt, at2cg, bBCheck, bExclRequired, cg_cm,
2317 &bd_mol_2b, &bd_mol_mb);
2319 /* Process the mol data adding the atom index offset */
2320 update_max_bonded_distance(bd_mol_2b.r2, bd_mol_2b.ftype,
2321 at_offset + bd_mol_2b.a1,
2322 at_offset + bd_mol_2b.a2,
2324 update_max_bonded_distance(bd_mol_mb.r2, bd_mol_mb.ftype,
2325 at_offset + bd_mol_mb.a1,
2326 at_offset + bd_mol_mb.a2,
2329 at_offset += molt.atoms.nr;
2333 if (ir->ePBC != epbcNONE)
2340 if (mtop->bIntermolecularInteractions)
2342 if (ncg_mtop(mtop) < mtop->natoms)
2344 gmx_fatal(FARGS, "The combination of intermolecular interactions, charge groups and domain decomposition is not supported. Use cutoff-scheme=Verlet (which removes the charge groups) or run without domain decomposition.");
2347 GMX_RELEASE_ASSERT(mtop->intermolecular_ilist, "We should have an ilist when intermolecular interactions are on");
2349 bonded_distance_intermol(*mtop->intermolecular_ilist,
2355 *r_2b = sqrt(bd_2b.r2);
2356 *r_mb = sqrt(bd_mb.r2);
2358 if (*r_2b > 0 || *r_mb > 0)
2360 GMX_LOG(mdlog.info).appendText("Initial maximum distances in bonded interactions:");
2363 GMX_LOG(mdlog.info).appendTextFormatted(
2364 " two-body bonded interactions: %5.3f nm, %s, atoms %d %d",
2365 *r_2b, (bd_2b.ftype >= 0) ? interaction_function[bd_2b.ftype].longname : "Exclusion",
2366 bd_2b.a1 + 1, bd_2b.a2 + 1);
2370 GMX_LOG(mdlog.info).appendTextFormatted(
2371 " multi-body bonded interactions: %5.3f nm, %s, atoms %d %d",
2372 *r_mb, interaction_function[bd_mb.ftype].longname,
2373 bd_mb.a1 + 1, bd_mb.a2 + 1);