2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
38 * \brief This file defines functions used by the domdec module
39 * while managing the construction, use and error checking for
40 * topologies local to a DD rank.
42 * \author Berk Hess <hess@kth.se>
43 * \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/force.h"
64 #include "gromacs/mdlib/forcerec.h"
65 #include "gromacs/mdlib/gmx_omp_nthreads.h"
66 #include "gromacs/mdlib/vsite.h"
67 #include "gromacs/mdtypes/commrec.h"
68 #include "gromacs/mdtypes/inputrec.h"
69 #include "gromacs/mdtypes/md_enums.h"
70 #include "gromacs/mdtypes/mdatom.h"
71 #include "gromacs/mdtypes/state.h"
72 #include "gromacs/pbcutil/mshift.h"
73 #include "gromacs/pbcutil/pbc.h"
74 #include "gromacs/topology/ifunc.h"
75 #include "gromacs/topology/mtop_util.h"
76 #include "gromacs/topology/topsort.h"
77 #include "gromacs/utility/cstringutil.h"
78 #include "gromacs/utility/exceptions.h"
79 #include "gromacs/utility/fatalerror.h"
80 #include "gromacs/utility/gmxassert.h"
81 #include "gromacs/utility/smalloc.h"
82 #include "gromacs/utility/stringutil.h"
84 #include "domdec_constraints.h"
85 #include "domdec_internal.h"
86 #include "domdec_vsite.h"
88 /*! \brief The number of integer item in the local state, used for broadcasting of the state */
89 #define NITEM_DD_INIT_LOCAL_STATE 5
92 int *index; /* Index for each atom into il */
93 int *il; /* ftype|type|a0|...|an|ftype|... */
103 /*! \brief Struct for thread local work data for local topology generation */
105 t_idef idef; /**< Partial local topology */
106 int **vsite_pbc; /**< vsite PBC structure */
107 int *vsite_pbc_nalloc; /**< Allocation sizes for vsite_pbc */
108 int nbonded; /**< The number of bondeds in this struct */
109 t_blocka excl; /**< List of exclusions */
110 int excl_count; /**< The total exclusion count for \p excl */
113 /*! \brief Struct for the reverse topology: links bonded interactions to atomsx */
114 struct gmx_reverse_top_t
116 //! @cond Doxygen_Suppress
117 gmx_bool bExclRequired; /**< Do we require all exclusions to be assigned? */
118 int n_excl_at_max; /**< The maximum number of exclusions one atom can have */
119 gmx_bool bConstr; /**< Are there constraints in this revserse top? */
120 gmx_bool bSettle; /**< Are there settles in this revserse top? */
121 gmx_bool bBCheck; /**< All bonded interactions have to be assigned? */
122 gmx_bool bInterCGInteractions; /**< Are there bondeds/exclusions between charge-groups? */
123 reverse_ilist_t *ril_mt; /**< Reverse ilist for all moltypes */
124 int ril_mt_tot_size; /**< The size of ril_mt[?].index summed over all entries */
125 int ilsort; /**< The sorting state of bondeds for free energy */
126 molblock_ind_t *mbi; /**< molblock to global atom index for quick lookup of molblocks on atom index */
127 int nmolblock; /**< The number of molblocks, size of \p mbi */
129 gmx_bool bIntermolecularInteractions; /**< Do we have intermolecular interactions? */
130 reverse_ilist_t ril_intermol; /**< Intermolecular reverse ilist */
132 /* Work data structures for multi-threading */
133 int nthread; /**< The number of threads to be used */
134 thread_work_t *th_work; /**< Thread work array for local topology generation */
138 /*! \brief Returns the number of atom entries for il in gmx_reverse_top_t */
139 static int nral_rt(int ftype)
144 if (interaction_function[ftype].flags & IF_VSITE)
146 /* With vsites the reverse topology contains
147 * two extra entries for PBC.
155 /*! \brief Return whether interactions of type \p ftype need to be assigned exactly once */
156 static gmx_bool dd_check_ftype(int ftype, gmx_bool bBCheck,
157 gmx_bool bConstr, gmx_bool bSettle)
159 return (((interaction_function[ftype].flags & IF_BOND) &&
160 !(interaction_function[ftype].flags & IF_VSITE) &&
161 (bBCheck || !(interaction_function[ftype].flags & IF_LIMZERO))) ||
162 (bConstr && (ftype == F_CONSTR || ftype == F_CONSTRNC)) ||
163 (bSettle && ftype == F_SETTLE));
166 /*! \brief Print a header on error messages */
167 static void print_error_header(FILE *fplog, const char *moltypename, int nprint)
169 fprintf(fplog, "\nMolecule type '%s'\n", moltypename);
170 fprintf(stderr, "\nMolecule type '%s'\n", moltypename);
172 "the first %d missing interactions, except for exclusions:\n",
175 "the first %d missing interactions, except for exclusions:\n",
179 /*! \brief Help print error output when interactions are missing */
180 static void print_missing_interactions_mb(FILE *fplog, t_commrec *cr,
181 const gmx_reverse_top_t *rt,
182 const char *moltypename,
183 const reverse_ilist_t *ril,
184 int a_start, int a_end,
185 int nat_mol, int nmol,
189 int nril_mol = ril->index[nat_mol];
190 snew(assigned, nmol*nril_mol);
192 int *gatindex = cr->dd->gatindex;
193 for (int ftype = 0; ftype < F_NRE; ftype++)
195 if (dd_check_ftype(ftype, rt->bBCheck, rt->bConstr, rt->bSettle))
197 int nral = NRAL(ftype);
198 const t_ilist *il = &idef->il[ftype];
199 const t_iatom *ia = il->iatoms;
200 for (int i = 0; i < il->nr; i += 1+nral)
202 int a0 = gatindex[ia[1]];
203 /* Check if this interaction is in
204 * the currently checked molblock.
206 if (a0 >= a_start && a0 < a_end)
208 int mol = (a0 - a_start)/nat_mol;
209 int a0_mol = (a0 - a_start) - mol*nat_mol;
210 int j_mol = ril->index[a0_mol];
212 while (j_mol < ril->index[a0_mol+1] && !found)
214 int j = mol*nril_mol + j_mol;
215 int ftype_j = ril->il[j_mol];
216 /* Here we need to check if this interaction has
217 * not already been assigned, since we could have
218 * multiply defined interactions.
220 if (ftype == ftype_j && ia[0] == ril->il[j_mol+1] &&
223 /* Check the atoms */
225 for (int a = 0; a < nral; a++)
227 if (gatindex[ia[1+a]] !=
228 a_start + mol*nat_mol + ril->il[j_mol+2+a])
238 j_mol += 2 + nral_rt(ftype_j);
242 gmx_incons("Some interactions seem to be assigned multiple times");
250 gmx_sumi(nmol*nril_mol, assigned, cr);
254 for (int mol = 0; mol < nmol; mol++)
257 while (j_mol < nril_mol)
259 int ftype = ril->il[j_mol];
260 int nral = NRAL(ftype);
261 int j = mol*nril_mol + j_mol;
262 if (assigned[j] == 0 &&
263 !(interaction_function[ftype].flags & IF_VSITE))
265 if (DDMASTER(cr->dd))
269 print_error_header(fplog, moltypename, nprint);
271 fprintf(fplog, "%20s atoms",
272 interaction_function[ftype].longname);
273 fprintf(stderr, "%20s atoms",
274 interaction_function[ftype].longname);
276 for (a = 0; a < nral; a++)
278 fprintf(fplog, "%5d", ril->il[j_mol+2+a]+1);
279 fprintf(stderr, "%5d", ril->il[j_mol+2+a]+1);
284 fprintf(stderr, " ");
287 fprintf(fplog, " global");
288 fprintf(stderr, " global");
289 for (a = 0; a < nral; a++)
291 fprintf(fplog, "%6d",
292 a_start+mol*nat_mol+ril->il[j_mol+2+a]+1);
293 fprintf(stderr, "%6d",
294 a_start+mol*nat_mol+ril->il[j_mol+2+a]+1);
296 fprintf(fplog, "\n");
297 fprintf(stderr, "\n");
305 j_mol += 2 + nral_rt(ftype);
312 /*! \brief Help print error output when interactions are missing */
313 static void print_missing_interactions_atoms(FILE *fplog, t_commrec *cr,
314 const gmx_mtop_t *mtop,
317 int mb, a_start, a_end;
318 const gmx_molblock_t *molb;
319 const gmx_reverse_top_t *rt;
321 rt = cr->dd->reverse_top;
323 /* Print the atoms in the missing interactions per molblock */
325 for (mb = 0; mb < mtop->nmolblock; mb++)
327 molb = &mtop->molblock[mb];
329 a_end = a_start + molb->nmol*molb->natoms_mol;
331 print_missing_interactions_mb(fplog, cr, rt,
332 *(mtop->moltype[molb->type].name),
333 &rt->ril_mt[molb->type],
334 a_start, a_end, molb->natoms_mol,
340 void dd_print_missing_interactions(FILE *fplog, t_commrec *cr,
342 const gmx_mtop_t *top_global,
343 const gmx_localtop_t *top_local,
344 t_state *state_local)
346 int ndiff_tot, cl[F_NRE], n, ndiff, rest_global, rest_local;
355 fprintf(fplog, "\nNot all bonded interactions have been properly assigned to the domain decomposition cells\n");
359 ndiff_tot = local_count - dd->nbonded_global;
361 for (ftype = 0; ftype < F_NRE; ftype++)
364 cl[ftype] = top_local->idef.il[ftype].nr/(1+nral);
367 gmx_sumi(F_NRE, cl, cr);
373 fprintf(fplog, "\nA list of missing interactions:\n");
375 fprintf(stderr, "\nA list of missing interactions:\n");
376 rest_global = dd->nbonded_global;
377 rest_local = local_count;
378 for (ftype = 0; ftype < F_NRE; ftype++)
380 /* In the reverse and local top all constraints are merged
381 * into F_CONSTR. So in the if statement we skip F_CONSTRNC
382 * and add these constraints when doing F_CONSTR.
384 if (((interaction_function[ftype].flags & IF_BOND) &&
385 (dd->reverse_top->bBCheck
386 || !(interaction_function[ftype].flags & IF_LIMZERO)))
387 || (dd->reverse_top->bConstr && ftype == F_CONSTR)
388 || (dd->reverse_top->bSettle && ftype == F_SETTLE))
390 n = gmx_mtop_ftype_count(top_global, ftype);
391 if (ftype == F_CONSTR)
393 n += gmx_mtop_ftype_count(top_global, F_CONSTRNC);
395 ndiff = cl[ftype] - n;
398 sprintf(buf, "%20s of %6d missing %6d",
399 interaction_function[ftype].longname, n, -ndiff);
402 fprintf(fplog, "%s\n", buf);
404 fprintf(stderr, "%s\n", buf);
407 rest_local -= cl[ftype];
411 ndiff = rest_local - rest_global;
414 sprintf(buf, "%20s of %6d missing %6d", "exclusions",
415 rest_global, -ndiff);
418 fprintf(fplog, "%s\n", buf);
420 fprintf(stderr, "%s\n", buf);
424 print_missing_interactions_atoms(fplog, cr, top_global, &top_local->idef);
425 write_dd_pdb("dd_dump_err", 0, "dump", top_global, cr,
426 -1, as_rvec_array(state_local->x.data()), state_local->box);
428 std::string errorMessage;
432 errorMessage = "One or more interactions were assigned to multiple domains of the domain decompostion. Please report this bug.";
436 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));
438 gmx_fatal_collective(FARGS, cr->mpi_comm_mygroup, MASTER(cr), errorMessage.c_str());
441 /*! \brief Return global topology molecule information for global atom index \p i_gl */
442 static void global_atomnr_to_moltype_ind(gmx_reverse_top_t *rt, int i_gl,
443 int *mb, int *mt, int *mol, int *i_mol)
445 molblock_ind_t *mbi = rt->mbi;
447 int end = rt->nmolblock; /* exclusive */
450 /* binary search for molblock_ind */
454 if (i_gl >= mbi[mid].a_end)
458 else if (i_gl < mbi[mid].a_start)
472 *mol = (i_gl - mbi->a_start) / mbi->natoms_mol;
473 *i_mol = (i_gl - mbi->a_start) - (*mol)*mbi->natoms_mol;
476 /*! \brief Count the exclusions for all atoms in \p cgs */
477 static void count_excls(const t_block *cgs, const t_blocka *excls,
478 int *n_excl, int *n_intercg_excl, int *n_excl_at_max)
480 int cg, at0, at1, at, excl, atj;
485 for (cg = 0; cg < cgs->nr; cg++)
487 at0 = cgs->index[cg];
488 at1 = cgs->index[cg+1];
489 for (at = at0; at < at1; at++)
491 for (excl = excls->index[at]; excl < excls->index[at+1]; excl++)
493 atj = excls->a[excl];
497 if (atj < at0 || atj >= at1)
504 *n_excl_at_max = std::max(*n_excl_at_max,
505 excls->index[at+1] - excls->index[at]);
510 /*! \brief Run the reverse ilist generation and store it in r_il when \p bAssign = TRUE */
511 static int low_make_reverse_ilist(const t_ilist *il_mt, const t_atom *atom,
512 const int * const * vsite_pbc,
514 gmx_bool bConstr, gmx_bool bSettle,
516 int *r_index, int *r_il,
517 gmx_bool bLinkToAllAtoms,
520 int ftype, nral, i, j, nlink, link;
528 for (ftype = 0; ftype < F_NRE; ftype++)
530 if ((interaction_function[ftype].flags & (IF_BOND | IF_VSITE)) ||
531 (bConstr && (ftype == F_CONSTR || ftype == F_CONSTRNC)) ||
532 (bSettle && ftype == F_SETTLE))
534 bVSite = (interaction_function[ftype].flags & IF_VSITE);
537 for (i = 0; i < il->nr; i += 1+nral)
544 /* We don't need the virtual sites for the cg-links */
554 /* Couple to the first atom in the interaction */
557 for (link = 0; link < nlink; link++)
562 assert(r_il); //with bAssign not allowed to be null
564 r_il[r_index[a]+count[a]] =
565 (ftype == F_CONSTRNC ? F_CONSTR : ftype);
566 r_il[r_index[a]+count[a]+1] = ia[0];
567 for (j = 1; j < 1+nral; j++)
569 /* Store the molecular atom number */
570 r_il[r_index[a]+count[a]+1+j] = ia[j];
573 if (interaction_function[ftype].flags & IF_VSITE)
577 /* Add an entry to iatoms for storing
578 * which of the constructing atoms are
581 r_il[r_index[a]+count[a]+2+nral] = 0;
582 for (j = 2; j < 1+nral; j++)
584 if (atom[ia[j]].ptype == eptVSite)
586 r_il[r_index[a]+count[a]+2+nral] |= (2<<j);
589 /* Store vsite pbc atom in a second extra entry */
590 r_il[r_index[a]+count[a]+2+nral+1] =
591 (vsite_pbc ? vsite_pbc[ftype-F_VSITE2][i/(1+nral)] : -2);
596 /* We do not count vsites since they are always
597 * uniquely assigned and can be assigned
598 * to multiple nodes with recursive vsites.
601 !(interaction_function[ftype].flags & IF_LIMZERO))
606 count[a] += 2 + nral_rt(ftype);
615 /*! \brief Make the reverse ilist: a list of bonded interactions linked to atoms */
616 static int make_reverse_ilist(const t_ilist *ilist,
617 const t_atoms *atoms,
618 const int * const * vsite_pbc,
619 gmx_bool bConstr, gmx_bool bSettle,
621 gmx_bool bLinkToAllAtoms,
622 reverse_ilist_t *ril_mt)
624 int nat_mt, *count, i, nint_mt;
626 /* Count the interactions */
629 low_make_reverse_ilist(ilist, atoms->atom, vsite_pbc,
631 bConstr, bSettle, bBCheck, nullptr, nullptr,
632 bLinkToAllAtoms, FALSE);
634 snew(ril_mt->index, nat_mt+1);
635 ril_mt->index[0] = 0;
636 for (i = 0; i < nat_mt; i++)
638 ril_mt->index[i+1] = ril_mt->index[i] + count[i];
641 snew(ril_mt->il, ril_mt->index[nat_mt]);
643 /* Store the interactions */
645 low_make_reverse_ilist(ilist, atoms->atom, vsite_pbc,
647 bConstr, bSettle, bBCheck,
648 ril_mt->index, ril_mt->il,
649 bLinkToAllAtoms, TRUE);
656 /*! \brief Destroys a reverse_ilist_t struct */
657 static void destroy_reverse_ilist(reverse_ilist_t *ril)
663 /*! \brief Generate the reverse topology */
664 static gmx_reverse_top_t *make_reverse_top(const gmx_mtop_t *mtop, gmx_bool bFE,
665 const int * const * const * vsite_pbc_molt,
666 gmx_bool bConstr, gmx_bool bSettle,
667 gmx_bool bBCheck, int *nint)
670 gmx_reverse_top_t *rt;
677 /* Should we include constraints (for SHAKE) in rt? */
678 rt->bConstr = bConstr;
679 rt->bSettle = bSettle;
680 rt->bBCheck = bBCheck;
682 rt->bInterCGInteractions = mtop->bIntermolecularInteractions;
683 snew(nint_mt, mtop->nmoltype);
684 snew(rt->ril_mt, mtop->nmoltype);
685 rt->ril_mt_tot_size = 0;
686 for (mt = 0; mt < mtop->nmoltype; mt++)
688 molt = &mtop->moltype[mt];
689 if (molt->cgs.nr > 1)
691 rt->bInterCGInteractions = TRUE;
694 /* Make the atom to interaction list for this molecule type */
696 make_reverse_ilist(molt->ilist, &molt->atoms,
697 vsite_pbc_molt ? vsite_pbc_molt[mt] : nullptr,
698 rt->bConstr, rt->bSettle, rt->bBCheck, FALSE,
701 rt->ril_mt_tot_size += rt->ril_mt[mt].index[molt->atoms.nr];
705 fprintf(debug, "The total size of the atom to interaction index is %d integers\n", rt->ril_mt_tot_size);
709 for (mb = 0; mb < mtop->nmolblock; mb++)
711 *nint += mtop->molblock[mb].nmol*nint_mt[mtop->molblock[mb].type];
715 /* Make an intermolecular reverse top, if necessary */
716 rt->bIntermolecularInteractions = mtop->bIntermolecularInteractions;
717 if (rt->bIntermolecularInteractions)
719 t_atoms atoms_global;
721 rt->ril_intermol.index = nullptr;
722 rt->ril_intermol.il = nullptr;
724 atoms_global.nr = mtop->natoms;
725 atoms_global.atom = nullptr; /* Only used with virtual sites */
728 make_reverse_ilist(mtop->intermolecular_ilist, &atoms_global,
730 rt->bConstr, rt->bSettle, rt->bBCheck, FALSE,
734 if (bFE && gmx_mtop_bondeds_free_energy(mtop))
736 rt->ilsort = ilsortFE_UNSORTED;
740 rt->ilsort = ilsortNO_FE;
743 /* Make a molblock index for fast searching */
744 snew(rt->mbi, mtop->nmolblock);
745 rt->nmolblock = mtop->nmolblock;
747 for (mb = 0; mb < mtop->nmolblock; mb++)
749 rt->mbi[mb].a_start = i;
750 i += mtop->molblock[mb].nmol*mtop->molblock[mb].natoms_mol;
751 rt->mbi[mb].a_end = i;
752 rt->mbi[mb].natoms_mol = mtop->molblock[mb].natoms_mol;
753 rt->mbi[mb].type = mtop->molblock[mb].type;
756 rt->nthread = gmx_omp_nthreads_get(emntDomdec);
757 snew(rt->th_work, rt->nthread);
758 if (vsite_pbc_molt != nullptr)
760 for (thread = 0; thread < rt->nthread; thread++)
762 snew(rt->th_work[thread].vsite_pbc, F_VSITEN-F_VSITE2+1);
763 snew(rt->th_work[thread].vsite_pbc_nalloc, F_VSITEN-F_VSITE2+1);
770 void dd_make_reverse_top(FILE *fplog,
771 gmx_domdec_t *dd, const gmx_mtop_t *mtop,
772 const gmx_vsite_t *vsite,
773 const t_inputrec *ir, gmx_bool bBCheck)
777 fprintf(fplog, "\nLinking all bonded interactions to atoms\n");
780 /* If normal and/or settle constraints act only within charge groups,
781 * we can store them in the reverse top and simply assign them to domains.
782 * Otherwise we need to assign them to multiple domains and set up
783 * the parallel version constraint algorithm(s).
786 dd->reverse_top = make_reverse_top(mtop, ir->efep != efepNO,
787 vsite ? vsite->vsite_pbc_molt : nullptr,
788 !dd->bInterCGcons, !dd->bInterCGsettles,
789 bBCheck, &dd->nbonded_global);
791 gmx_reverse_top_t *rt = dd->reverse_top;
793 /* With the Verlet scheme, exclusions are handled in the non-bonded
794 * kernels and only exclusions inside the cut-off lead to exclusion
795 * forces. Since each atom pair is treated at most once in the non-bonded
796 * kernels, it doesn't matter if the exclusions for the same atom pair
797 * appear multiple times in the exclusion list. In contrast, the, old,
798 * group cut-off scheme loops over a list of exclusions, so there each
799 * excluded pair should appear exactly once.
801 rt->bExclRequired = (ir->cutoff_scheme == ecutsGROUP &&
802 inputrecExclForces(ir));
807 dd->n_intercg_excl = 0;
808 rt->n_excl_at_max = 0;
809 for (mb = 0; mb < mtop->nmolblock; mb++)
811 gmx_molblock_t *molb;
813 int n_excl_mol, n_excl_icg, n_excl_at_max;
815 molb = &mtop->molblock[mb];
816 molt = &mtop->moltype[molb->type];
817 count_excls(&molt->cgs, &molt->excls,
818 &n_excl_mol, &n_excl_icg, &n_excl_at_max);
819 nexcl += molb->nmol*n_excl_mol;
820 dd->n_intercg_excl += molb->nmol*n_excl_icg;
821 rt->n_excl_at_max = std::max(rt->n_excl_at_max, n_excl_at_max);
823 if (rt->bExclRequired)
825 dd->nbonded_global += nexcl;
826 if (EEL_FULL(ir->coulombtype) && dd->n_intercg_excl > 0 && fplog)
828 fprintf(fplog, "There are %d inter charge-group exclusions,\n"
829 "will use an extra communication step for exclusion forces for %s\n",
830 dd->n_intercg_excl, eel_names[ir->coulombtype]);
834 if (vsite && vsite->n_intercg_vsite > 0)
838 fprintf(fplog, "There are %d inter charge-group virtual sites,\n"
839 "will an extra communication step for selected coordinates and forces\n",
840 vsite->n_intercg_vsite);
842 init_domdec_vsites(dd, vsite->n_intercg_vsite);
845 if (dd->bInterCGcons || dd->bInterCGsettles)
847 init_domdec_constraints(dd, mtop);
851 fprintf(fplog, "\n");
855 /*! \brief Store a vsite interaction at the end of \p il
857 * This routine is very similar to add_ifunc, but vsites interactions
858 * have more work to do than other kinds of interactions, and the
859 * complex way nral (and thus vector contents) depends on ftype
860 * confuses static analysis tools unless we fuse the vsite
861 * atom-indexing organization code with the ifunc-adding code, so that
862 * they can see that nral is the same value. */
864 add_ifunc_for_vsites(t_iatom *tiatoms, gmx_ga2la_t *ga2la,
865 int nral, gmx_bool bHomeA,
866 int a, int a_gl, int a_mol,
867 const t_iatom *iatoms,
872 if (il->nr+1+nral > il->nalloc)
874 il->nalloc = over_alloc_large(il->nr+1+nral);
875 srenew(il->iatoms, il->nalloc);
877 liatoms = il->iatoms + il->nr;
881 tiatoms[0] = iatoms[0];
885 /* We know the local index of the first atom */
890 /* Convert later in make_local_vsites */
891 tiatoms[1] = -a_gl - 1;
894 for (int k = 2; k < 1+nral; k++)
896 int ak_gl = a_gl + iatoms[k] - a_mol;
897 if (!ga2la_get_home(ga2la, ak_gl, &tiatoms[k]))
899 /* Copy the global index, convert later in make_local_vsites */
900 tiatoms[k] = -(ak_gl + 1);
902 // Note that ga2la_get_home always sets the third parameter if
905 for (int k = 0; k < 1+nral; k++)
907 liatoms[k] = tiatoms[k];
911 /*! \brief Store a bonded interaction at the end of \p il */
912 static inline void add_ifunc(int nral, t_iatom *tiatoms, t_ilist *il)
917 if (il->nr+1+nral > il->nalloc)
919 il->nalloc = over_alloc_large(il->nr+1+nral);
920 srenew(il->iatoms, il->nalloc);
922 liatoms = il->iatoms + il->nr;
923 for (k = 0; k <= nral; k++)
925 liatoms[k] = tiatoms[k];
930 /*! \brief Store a position restraint in idef and iatoms, complex because the parameters are different for each entry */
931 static void add_posres(int mol, int a_mol, const gmx_molblock_t *molb,
932 t_iatom *iatoms, const t_iparams *ip_in,
938 /* This position restraint has not been added yet,
939 * so it's index is the current number of position restraints.
941 n = idef->il[F_POSRES].nr/2;
942 if (n+1 > idef->iparams_posres_nalloc)
944 idef->iparams_posres_nalloc = over_alloc_dd(n+1);
945 srenew(idef->iparams_posres, idef->iparams_posres_nalloc);
947 ip = &idef->iparams_posres[n];
948 /* Copy the force constants */
949 *ip = ip_in[iatoms[0]];
951 /* Get the position restraint coordinates from the molblock */
952 a_molb = mol*molb->natoms_mol + a_mol;
953 if (a_molb >= molb->nposres_xA)
955 gmx_incons("Not enough position restraint coordinates");
957 ip->posres.pos0A[XX] = molb->posres_xA[a_molb][XX];
958 ip->posres.pos0A[YY] = molb->posres_xA[a_molb][YY];
959 ip->posres.pos0A[ZZ] = molb->posres_xA[a_molb][ZZ];
960 if (molb->nposres_xB > 0)
962 ip->posres.pos0B[XX] = molb->posres_xB[a_molb][XX];
963 ip->posres.pos0B[YY] = molb->posres_xB[a_molb][YY];
964 ip->posres.pos0B[ZZ] = molb->posres_xB[a_molb][ZZ];
968 ip->posres.pos0B[XX] = ip->posres.pos0A[XX];
969 ip->posres.pos0B[YY] = ip->posres.pos0A[YY];
970 ip->posres.pos0B[ZZ] = ip->posres.pos0A[ZZ];
972 /* Set the parameter index for idef->iparams_posre */
976 /*! \brief Store a flat-bottomed position restraint in idef and iatoms, complex because the parameters are different for each entry */
977 static void add_fbposres(int mol, int a_mol, const gmx_molblock_t *molb,
978 t_iatom *iatoms, const t_iparams *ip_in,
984 /* This flat-bottom position restraint has not been added yet,
985 * so it's index is the current number of position restraints.
987 n = idef->il[F_FBPOSRES].nr/2;
988 if (n+1 > idef->iparams_fbposres_nalloc)
990 idef->iparams_fbposres_nalloc = over_alloc_dd(n+1);
991 srenew(idef->iparams_fbposres, idef->iparams_fbposres_nalloc);
993 ip = &idef->iparams_fbposres[n];
994 /* Copy the force constants */
995 *ip = ip_in[iatoms[0]];
997 /* Get the position restriant coordinats from the molblock */
998 a_molb = mol*molb->natoms_mol + a_mol;
999 if (a_molb >= molb->nposres_xA)
1001 gmx_incons("Not enough position restraint coordinates");
1003 /* Take reference positions from A position of normal posres */
1004 ip->fbposres.pos0[XX] = molb->posres_xA[a_molb][XX];
1005 ip->fbposres.pos0[YY] = molb->posres_xA[a_molb][YY];
1006 ip->fbposres.pos0[ZZ] = molb->posres_xA[a_molb][ZZ];
1008 /* Note: no B-type for flat-bottom posres */
1010 /* Set the parameter index for idef->iparams_posre */
1014 /*! \brief Store a virtual site interaction, complex because of PBC and recursion */
1015 static void add_vsite(gmx_ga2la_t *ga2la, const int *index, const int *rtil,
1016 int ftype, int nral,
1017 gmx_bool bHomeA, int a, int a_gl, int a_mol,
1018 const t_iatom *iatoms,
1019 t_idef *idef, int **vsite_pbc, int *vsite_pbc_nalloc)
1021 int k, vsi, pbc_a_mol;
1022 t_iatom tiatoms[1+MAXATOMLIST];
1023 int j, ftype_r, nral_r;
1025 /* Add this interaction to the local topology */
1026 add_ifunc_for_vsites(tiatoms, ga2la, nral, bHomeA, a, a_gl, a_mol, iatoms, &idef->il[ftype]);
1030 vsi = idef->il[ftype].nr/(1+nral) - 1;
1031 if (vsi >= vsite_pbc_nalloc[ftype-F_VSITE2])
1033 vsite_pbc_nalloc[ftype-F_VSITE2] = over_alloc_large(vsi+1);
1034 srenew(vsite_pbc[ftype-F_VSITE2], vsite_pbc_nalloc[ftype-F_VSITE2]);
1038 pbc_a_mol = iatoms[1+nral+1];
1041 /* The pbc flag is one of the following two options:
1042 * -2: vsite and all constructing atoms are within the same cg, no pbc
1043 * -1: vsite and its first constructing atom are in the same cg, do pbc
1045 vsite_pbc[ftype-F_VSITE2][vsi] = pbc_a_mol;
1049 /* Set the pbc atom for this vsite so we can make its pbc
1050 * identical to the rest of the atoms in its charge group.
1051 * Since the order of the atoms does not change within a charge
1052 * group, we do not need the global to local atom index.
1054 vsite_pbc[ftype-F_VSITE2][vsi] = a + pbc_a_mol - iatoms[1];
1059 /* This vsite is non-home (required for recursion),
1060 * and therefore there is no charge group to match pbc with.
1061 * But we always turn on full_pbc to assure that higher order
1062 * recursion works correctly.
1064 vsite_pbc[ftype-F_VSITE2][vsi] = -1;
1070 /* Check for recursion */
1071 for (k = 2; k < 1+nral; k++)
1073 if ((iatoms[1+nral] & (2<<k)) && (tiatoms[k] < 0))
1075 /* This construction atoms is a vsite and not a home atom */
1078 fprintf(debug, "Constructing atom %d of vsite atom %d is a vsite and non-home\n", iatoms[k]+1, a_mol+1);
1080 /* Find the vsite construction */
1082 /* Check all interactions assigned to this atom */
1083 j = index[iatoms[k]];
1084 while (j < index[iatoms[k]+1])
1086 ftype_r = rtil[j++];
1087 nral_r = NRAL(ftype_r);
1088 if (interaction_function[ftype_r].flags & IF_VSITE)
1090 /* Add this vsite (recursion) */
1091 add_vsite(ga2la, index, rtil, ftype_r, nral_r,
1092 FALSE, -1, a_gl+iatoms[k]-iatoms[1], iatoms[k],
1093 rtil+j, idef, vsite_pbc, vsite_pbc_nalloc);
1094 j += 1 + nral_r + 2;
1106 /*! \brief Update the local atom to local charge group index */
1107 static void make_la2lc(gmx_domdec_t *dd)
1109 int *cgindex, *la2lc, cg, a;
1111 cgindex = dd->cgindex;
1113 if (dd->nat_tot > dd->la2lc_nalloc)
1115 dd->la2lc_nalloc = over_alloc_dd(dd->nat_tot);
1116 snew(dd->la2lc, dd->la2lc_nalloc);
1120 /* Make the local atom to local cg index */
1121 for (cg = 0; cg < dd->ncg_tot; cg++)
1123 for (a = cgindex[cg]; a < cgindex[cg+1]; a++)
1130 /*! \brief Returns the squared distance between charge groups \p i and \p j */
1131 static real dd_dist2(t_pbc *pbc_null, rvec *cg_cm, const int *la2lc, int i, int j)
1137 pbc_dx_aiuc(pbc_null, cg_cm[la2lc[i]], cg_cm[la2lc[j]], dx);
1141 rvec_sub(cg_cm[la2lc[i]], cg_cm[la2lc[j]], dx);
1147 /*! \brief Append t_blocka block structures 1 to nsrc in src to *dest */
1148 static void combine_blocka(t_blocka *dest, const thread_work_t *src, int nsrc)
1152 ni = src[nsrc-1].excl.nr;
1154 for (s = 0; s < nsrc; s++)
1156 na += src[s].excl.nra;
1158 if (ni + 1 > dest->nalloc_index)
1160 dest->nalloc_index = over_alloc_large(ni+1);
1161 srenew(dest->index, dest->nalloc_index);
1163 if (dest->nra + na > dest->nalloc_a)
1165 dest->nalloc_a = over_alloc_large(dest->nra+na);
1166 srenew(dest->a, dest->nalloc_a);
1168 for (s = 1; s < nsrc; s++)
1170 for (i = dest->nr+1; i < src[s].excl.nr+1; i++)
1172 dest->index[i] = dest->nra + src[s].excl.index[i];
1174 for (i = 0; i < src[s].excl.nra; i++)
1176 dest->a[dest->nra+i] = src[s].excl.a[i];
1178 dest->nr = src[s].excl.nr;
1179 dest->nra += src[s].excl.nra;
1183 /*! \brief Append t_idef structures 1 to nsrc in src to *dest,
1184 * virtual sites need special attention, as pbc info differs per vsite.
1186 static void combine_idef(t_idef *dest, const thread_work_t *src, int nsrc,
1191 for (ftype = 0; ftype < F_NRE; ftype++)
1196 for (s = 1; s < nsrc; s++)
1198 n += src[s].idef.il[ftype].nr;
1204 ild = &dest->il[ftype];
1206 if (ild->nr + n > ild->nalloc)
1208 ild->nalloc = over_alloc_large(ild->nr+n);
1209 srenew(ild->iatoms, ild->nalloc);
1213 int nral1 = 0, ftv = 0;
1215 vpbc = ((interaction_function[ftype].flags & IF_VSITE) &&
1216 vsite->vsite_pbc_loc != nullptr);
1219 nral1 = 1 + NRAL(ftype);
1220 ftv = ftype - F_VSITE2;
1221 if ((ild->nr + n)/nral1 > vsite->vsite_pbc_loc_nalloc[ftv])
1223 vsite->vsite_pbc_loc_nalloc[ftv] =
1224 over_alloc_large((ild->nr + n)/nral1);
1225 srenew(vsite->vsite_pbc_loc[ftv],
1226 vsite->vsite_pbc_loc_nalloc[ftv]);
1230 for (s = 1; s < nsrc; s++)
1235 ils = &src[s].idef.il[ftype];
1236 for (i = 0; i < ils->nr; i++)
1238 ild->iatoms[ild->nr+i] = ils->iatoms[i];
1242 for (i = 0; i < ils->nr; i += nral1)
1244 vsite->vsite_pbc_loc[ftv][(ild->nr+i)/nral1] =
1245 src[s].vsite_pbc[ftv][i/nral1];
1252 /* Position restraints need an additional treatment */
1253 if (ftype == F_POSRES || ftype == F_FBPOSRES)
1255 int nposres = dest->il[ftype].nr/2;
1256 // TODO: Simplify this code using std::vector
1257 t_iparams * &iparams_dest = (ftype == F_POSRES ? dest->iparams_posres : dest->iparams_fbposres);
1258 int &posres_nalloc = (ftype == F_POSRES ? dest->iparams_posres_nalloc : dest->iparams_fbposres_nalloc);
1259 if (nposres > posres_nalloc)
1261 posres_nalloc = over_alloc_large(nposres);
1262 srenew(iparams_dest, posres_nalloc);
1265 /* Set nposres to the number of original position restraints in dest */
1266 for (int s = 1; s < nsrc; s++)
1268 nposres -= src[s].idef.il[ftype].nr/2;
1271 for (int s = 1; s < nsrc; s++)
1273 const t_iparams *iparams_src = (ftype == F_POSRES ? src[s].idef.iparams_posres : src[s].idef.iparams_fbposres);
1275 for (int i = 0; i < src[s].idef.il[ftype].nr/2; i++)
1277 /* Correct the index into iparams_posres */
1278 dest->il[ftype].iatoms[nposres*2] = nposres;
1279 /* Copy the position restraint force parameters */
1280 iparams_dest[nposres] = iparams_src[i];
1289 /*! \brief Check and when available assign bonded interactions for local atom i
1292 check_assign_interactions_atom(int i, int i_gl,
1294 const int *index, const int *rtil,
1295 gmx_bool bInterMolInteractions,
1296 int ind_start, int ind_end,
1297 const gmx_domdec_t *dd,
1298 const gmx_domdec_zones_t *zones,
1299 const gmx_molblock_t *molb,
1300 gmx_bool bRCheckMB, ivec rcheck, gmx_bool bRCheck2B,
1305 const t_iparams *ip_in,
1307 int **vsite_pbc, int *vsite_pbc_nalloc,
1318 const t_iatom *iatoms;
1320 t_iatom tiatoms[1 + MAXATOMLIST];
1325 if (ftype == F_SETTLE)
1327 /* Settles are only in the reverse top when they
1328 * operate within a charge group. So we can assign
1329 * them without checks. We do this only for performance
1330 * reasons; it could be handled by the code below.
1334 /* Home zone: add this settle to the local topology */
1335 tiatoms[0] = iatoms[0];
1337 tiatoms[2] = i + iatoms[2] - iatoms[1];
1338 tiatoms[3] = i + iatoms[3] - iatoms[1];
1339 add_ifunc(nral, tiatoms, &idef->il[ftype]);
1344 else if (interaction_function[ftype].flags & IF_VSITE)
1346 assert(!bInterMolInteractions);
1347 /* The vsite construction goes where the vsite itself is */
1350 add_vsite(dd->ga2la, index, rtil, ftype, nral,
1351 TRUE, i, i_gl, i_mol,
1352 iatoms, idef, vsite_pbc, vsite_pbc_nalloc);
1361 tiatoms[0] = iatoms[0];
1365 assert(!bInterMolInteractions);
1366 /* Assign single-body interactions to the home zone */
1371 if (ftype == F_POSRES)
1373 add_posres(mol, i_mol, molb, tiatoms, ip_in,
1376 else if (ftype == F_FBPOSRES)
1378 add_fbposres(mol, i_mol, molb, tiatoms, ip_in,
1389 /* This is a two-body interaction, we can assign
1390 * analogous to the non-bonded assignments.
1392 int k_gl, a_loc, kz;
1394 if (!bInterMolInteractions)
1396 /* Get the global index using the offset in the molecule */
1397 k_gl = i_gl + iatoms[2] - i_mol;
1403 if (!ga2la_get(dd->ga2la, k_gl, &a_loc, &kz))
1413 /* Check zone interaction assignments */
1414 bUse = ((iz < zones->nizone &&
1416 kz >= zones->izone[iz].j0 &&
1417 kz < zones->izone[iz].j1) ||
1418 (kz < zones->nizone &&
1420 iz >= zones->izone[kz].j0 &&
1421 iz < zones->izone[kz].j1));
1426 /* If necessary check the cgcm distance */
1428 dd_dist2(pbc_null, cg_cm, la2lc,
1429 tiatoms[1], tiatoms[2]) >= rc2)
1438 /* Assign this multi-body bonded interaction to
1439 * the local node if we have all the atoms involved
1440 * (local or communicated) and the minimum zone shift
1441 * in each dimension is zero, for dimensions
1442 * with 2 DD cells an extra check may be necessary.
1444 ivec k_zero, k_plus;
1450 for (k = 1; k <= nral && bUse; k++)
1456 if (!bInterMolInteractions)
1458 /* Get the global index using the offset in the molecule */
1459 k_gl = i_gl + iatoms[k] - i_mol;
1465 bLocal = ga2la_get(dd->ga2la, k_gl, &a_loc, &kz);
1466 if (!bLocal || kz >= zones->n)
1468 /* We do not have this atom of this interaction
1469 * locally, or it comes from more than one cell
1479 for (d = 0; d < DIM; d++)
1481 if (zones->shift[kz][d] == 0)
1493 k_zero[XX] && k_zero[YY] && k_zero[ZZ]);
1498 for (d = 0; (d < DIM && bUse); d++)
1500 /* Check if the cg_cm distance falls within
1501 * the cut-off to avoid possible multiple
1502 * assignments of bonded interactions.
1506 dd_dist2(pbc_null, cg_cm, la2lc,
1507 tiatoms[k_zero[d]], tiatoms[k_plus[d]]) >= rc2)
1516 /* Add this interaction to the local topology */
1517 add_ifunc(nral, tiatoms, &idef->il[ftype]);
1518 /* Sum so we can check in global_stat
1519 * if we have everything.
1522 !(interaction_function[ftype].flags & IF_LIMZERO))
1532 /*! \brief This function looks up and assigns bonded interactions for zone iz.
1534 * With thread parallelizing each thread acts on a different atom range:
1535 * at_start to at_end.
1537 static int make_bondeds_zone(gmx_domdec_t *dd,
1538 const gmx_domdec_zones_t *zones,
1539 const gmx_molblock_t *molb,
1540 gmx_bool bRCheckMB, ivec rcheck, gmx_bool bRCheck2B,
1542 int *la2lc, t_pbc *pbc_null, rvec *cg_cm,
1543 const t_iparams *ip_in,
1546 int *vsite_pbc_nalloc,
1548 int at_start, int at_end)
1550 int i, i_gl, mb, mt, mol, i_mol;
1553 gmx_reverse_top_t *rt;
1556 rt = dd->reverse_top;
1558 bBCheck = rt->bBCheck;
1562 for (i = at_start; i < at_end; i++)
1564 /* Get the global atom number */
1565 i_gl = dd->gatindex[i];
1566 global_atomnr_to_moltype_ind(rt, i_gl, &mb, &mt, &mol, &i_mol);
1567 /* Check all intramolecular interactions assigned to this atom */
1568 index = rt->ril_mt[mt].index;
1569 rtil = rt->ril_mt[mt].il;
1571 check_assign_interactions_atom(i, i_gl, mol, i_mol,
1573 index[i_mol], index[i_mol+1],
1576 bRCheckMB, rcheck, bRCheck2B, rc2,
1581 idef, vsite_pbc, vsite_pbc_nalloc,
1587 if (rt->bIntermolecularInteractions)
1589 /* Check all intermolecular interactions assigned to this atom */
1590 index = rt->ril_intermol.index;
1591 rtil = rt->ril_intermol.il;
1593 check_assign_interactions_atom(i, i_gl, mol, i_mol,
1595 index[i_gl], index[i_gl + 1],
1598 bRCheckMB, rcheck, bRCheck2B, rc2,
1603 idef, vsite_pbc, vsite_pbc_nalloc,
1610 return nbonded_local;
1613 /*! \brief Set the exclusion data for i-zone \p iz for the case of no exclusions */
1614 static void set_no_exclusions_zone(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
1615 int iz, t_blocka *lexcls)
1619 a0 = dd->cgindex[zones->cg_range[iz]];
1620 a1 = dd->cgindex[zones->cg_range[iz+1]];
1622 for (a = a0+1; a < a1+1; a++)
1624 lexcls->index[a] = lexcls->nra;
1628 /*! \brief Set the exclusion data for i-zone \p iz
1630 * This is a legacy version for the group scheme of the same routine below.
1631 * Here charge groups and distance checks to ensure unique exclusions
1634 static int make_exclusions_zone_cg(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
1635 const gmx_moltype_t *moltype,
1636 gmx_bool bRCheck, real rc2,
1637 int *la2lc, t_pbc *pbc_null, rvec *cg_cm,
1641 int cg_start, int cg_end)
1643 int n_excl_at_max, n, count, jla0, jla1, jla;
1644 int cg, la0, la1, la, a_gl, mb, mt, mol, a_mol, j, aj_mol;
1645 const t_blocka *excls;
1651 jla0 = dd->cgindex[zones->izone[iz].jcg0];
1652 jla1 = dd->cgindex[zones->izone[iz].jcg1];
1654 n_excl_at_max = dd->reverse_top->n_excl_at_max;
1656 /* We set the end index, but note that we might not start at zero here */
1657 lexcls->nr = dd->cgindex[cg_end];
1661 for (cg = cg_start; cg < cg_end; cg++)
1663 if (n + (cg_end - cg_start)*n_excl_at_max > lexcls->nalloc_a)
1665 lexcls->nalloc_a = over_alloc_large(n + (cg_end - cg_start)*n_excl_at_max);
1666 srenew(lexcls->a, lexcls->nalloc_a);
1668 la0 = dd->cgindex[cg];
1669 la1 = dd->cgindex[cg+1];
1670 if (GET_CGINFO_EXCL_INTER(cginfo[cg]) ||
1671 !GET_CGINFO_EXCL_INTRA(cginfo[cg]))
1673 /* Copy the exclusions from the global top */
1674 for (la = la0; la < la1; la++)
1676 lexcls->index[la] = n;
1677 a_gl = dd->gatindex[la];
1678 global_atomnr_to_moltype_ind(dd->reverse_top, a_gl, &mb, &mt, &mol, &a_mol);
1679 excls = &moltype[mt].excls;
1680 for (j = excls->index[a_mol]; j < excls->index[a_mol+1]; j++)
1682 aj_mol = excls->a[j];
1683 /* This computation of jla is only correct intra-cg */
1684 jla = la + aj_mol - a_mol;
1685 if (jla >= la0 && jla < la1)
1687 /* This is an intra-cg exclusion. We can skip
1688 * the global indexing and distance checking.
1690 /* Intra-cg exclusions are only required
1691 * for the home zone.
1695 lexcls->a[n++] = jla;
1696 /* Check to avoid double counts */
1705 /* This is a inter-cg exclusion */
1706 /* Since exclusions are pair interactions,
1707 * just like non-bonded interactions,
1708 * they can be assigned properly up
1709 * to the DD cutoff (not cutoff_min as
1710 * for the other bonded interactions).
1712 if (ga2la_get(ga2la, a_gl+aj_mol-a_mol, &jla, &cell))
1714 if (iz == 0 && cell == 0)
1716 lexcls->a[n++] = jla;
1717 /* Check to avoid double counts */
1723 else if (jla >= jla0 && jla < jla1 &&
1725 dd_dist2(pbc_null, cg_cm, la2lc, la, jla) < rc2))
1727 /* jla > la, since jla0 > la */
1728 lexcls->a[n++] = jla;
1738 /* There are no inter-cg excls and this cg is self-excluded.
1739 * These exclusions are only required for zone 0,
1740 * since other zones do not see themselves.
1744 for (la = la0; la < la1; la++)
1746 lexcls->index[la] = n;
1747 for (j = la0; j < la1; j++)
1752 count += ((la1 - la0)*(la1 - la0 - 1))/2;
1756 /* We don't need exclusions for this cg */
1757 for (la = la0; la < la1; la++)
1759 lexcls->index[la] = n;
1765 lexcls->index[lexcls->nr] = n;
1771 /*! \brief Set the exclusion data for i-zone \p iz */
1772 static void make_exclusions_zone(gmx_domdec_t *dd,
1773 gmx_domdec_zones_t *zones,
1774 const gmx_moltype_t *moltype,
1778 int at_start, int at_end)
1782 int n_excl_at_max, n, at;
1786 jla0 = dd->cgindex[zones->izone[iz].jcg0];
1787 jla1 = dd->cgindex[zones->izone[iz].jcg1];
1789 n_excl_at_max = dd->reverse_top->n_excl_at_max;
1791 /* We set the end index, but note that we might not start at zero here */
1792 lexcls->nr = at_end;
1795 for (at = at_start; at < at_end; at++)
1797 if (n + 1000 > lexcls->nalloc_a)
1799 lexcls->nalloc_a = over_alloc_large(n + 1000);
1800 srenew(lexcls->a, lexcls->nalloc_a);
1802 if (GET_CGINFO_EXCL_INTER(cginfo[at]))
1804 int a_gl, mb, mt, mol, a_mol, j;
1805 const t_blocka *excls;
1807 if (n + n_excl_at_max > lexcls->nalloc_a)
1809 lexcls->nalloc_a = over_alloc_large(n + n_excl_at_max);
1810 srenew(lexcls->a, lexcls->nalloc_a);
1813 /* Copy the exclusions from the global top */
1814 lexcls->index[at] = n;
1815 a_gl = dd->gatindex[at];
1816 global_atomnr_to_moltype_ind(dd->reverse_top, a_gl,
1817 &mb, &mt, &mol, &a_mol);
1818 excls = &moltype[mt].excls;
1819 for (j = excls->index[a_mol]; j < excls->index[a_mol + 1]; j++)
1821 int aj_mol, at_j, cell;
1823 aj_mol = excls->a[j];
1825 if (ga2la_get(ga2la, a_gl + aj_mol - a_mol, &at_j, &cell))
1827 /* This check is not necessary, but it can reduce
1828 * the number of exclusions in the list, which in turn
1829 * can speed up the pair list construction a bit.
1831 if (at_j >= jla0 && at_j < jla1)
1833 lexcls->a[n++] = at_j;
1840 /* We don't need exclusions for this atom */
1841 lexcls->index[at] = n;
1845 lexcls->index[lexcls->nr] = n;
1850 /*! \brief Ensure we have enough space in \p ba for \p nindex_max indices */
1851 static void check_alloc_index(t_blocka *ba, int nindex_max)
1853 if (nindex_max+1 > ba->nalloc_index)
1855 ba->nalloc_index = over_alloc_dd(nindex_max+1);
1856 srenew(ba->index, ba->nalloc_index);
1860 /*! \brief Ensure that we have enough space for exclusion storate in \p lexcls */
1861 static void check_exclusions_alloc(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
1867 nr = dd->cgindex[zones->izone[zones->nizone-1].cg1];
1869 check_alloc_index(lexcls, nr);
1871 for (thread = 1; thread < dd->reverse_top->nthread; thread++)
1873 check_alloc_index(&dd->reverse_top->th_work[thread].excl, nr);
1877 /*! \brief Set the total count indexes for the local exclusions, needed by several functions */
1878 static void finish_local_exclusions(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
1883 lexcls->nr = dd->cgindex[zones->izone[zones->nizone-1].cg1];
1885 if (dd->n_intercg_excl == 0)
1887 /* There are no exclusions involving non-home charge groups,
1888 * but we need to set the indices for neighborsearching.
1890 la0 = dd->cgindex[zones->izone[0].cg1];
1891 for (la = la0; la < lexcls->nr; la++)
1893 lexcls->index[la] = lexcls->nra;
1896 /* nr is only used to loop over the exclusions for Ewald and RF,
1897 * so we can set it to the number of home atoms for efficiency.
1899 lexcls->nr = dd->cgindex[zones->izone[0].cg1];
1903 /*! \brief Clear a t_idef struct */
1904 static void clear_idef(t_idef *idef)
1908 /* Clear the counts */
1909 for (ftype = 0; ftype < F_NRE; ftype++)
1911 idef->il[ftype].nr = 0;
1915 /*! \brief Generate and store all required local bonded interactions in \p idef and local exclusions in \p lexcls */
1916 static int make_local_bondeds_excls(gmx_domdec_t *dd,
1917 gmx_domdec_zones_t *zones,
1918 const gmx_mtop_t *mtop,
1920 gmx_bool bRCheckMB, ivec rcheck, gmx_bool bRCheck2B,
1922 int *la2lc, t_pbc *pbc_null, rvec *cg_cm,
1923 t_idef *idef, gmx_vsite_t *vsite,
1924 t_blocka *lexcls, int *excl_count)
1926 int nzone_bondeds, nzone_excl;
1927 int izone, cg0, cg1;
1931 gmx_reverse_top_t *rt;
1933 if (dd->reverse_top->bInterCGInteractions)
1935 nzone_bondeds = zones->n;
1939 /* Only single charge group (or atom) molecules, so interactions don't
1940 * cross zone boundaries and we only need to assign in the home zone.
1945 if (dd->n_intercg_excl > 0)
1947 /* We only use exclusions from i-zones to i- and j-zones */
1948 nzone_excl = zones->nizone;
1952 /* There are no inter-cg exclusions and only zone 0 sees itself */
1956 check_exclusions_alloc(dd, zones, lexcls);
1958 rt = dd->reverse_top;
1962 /* Clear the counts */
1970 for (izone = 0; izone < nzone_bondeds; izone++)
1972 cg0 = zones->cg_range[izone];
1973 cg1 = zones->cg_range[izone + 1];
1975 #pragma omp parallel for num_threads(rt->nthread) schedule(static)
1976 for (thread = 0; thread < rt->nthread; thread++)
1983 int *vsite_pbc_nalloc;
1986 cg0t = cg0 + ((cg1 - cg0)* thread )/rt->nthread;
1987 cg1t = cg0 + ((cg1 - cg0)*(thread+1))/rt->nthread;
1995 idef_t = &rt->th_work[thread].idef;
1999 if (vsite && vsite->bHaveChargeGroups && vsite->n_intercg_vsite > 0)
2003 vsite_pbc = vsite->vsite_pbc_loc;
2004 vsite_pbc_nalloc = vsite->vsite_pbc_loc_nalloc;
2008 vsite_pbc = rt->th_work[thread].vsite_pbc;
2009 vsite_pbc_nalloc = rt->th_work[thread].vsite_pbc_nalloc;
2014 vsite_pbc = nullptr;
2015 vsite_pbc_nalloc = nullptr;
2018 rt->th_work[thread].nbonded =
2019 make_bondeds_zone(dd, zones,
2021 bRCheckMB, rcheck, bRCheck2B, rc2,
2022 la2lc, pbc_null, cg_cm, idef->iparams,
2024 vsite_pbc, vsite_pbc_nalloc,
2026 dd->cgindex[cg0t], dd->cgindex[cg1t]);
2028 if (izone < nzone_excl)
2036 excl_t = &rt->th_work[thread].excl;
2041 if (dd->cgindex[dd->ncg_tot] == dd->ncg_tot &&
2044 /* No charge groups and no distance check required */
2045 make_exclusions_zone(dd, zones,
2046 mtop->moltype, cginfo,
2053 rt->th_work[thread].excl_count =
2054 make_exclusions_zone_cg(dd, zones,
2055 mtop->moltype, bRCheck2B, rc2,
2056 la2lc, pbc_null, cg_cm, cginfo,
2063 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
2066 if (rt->nthread > 1)
2068 combine_idef(idef, rt->th_work, rt->nthread, vsite);
2071 for (thread = 0; thread < rt->nthread; thread++)
2073 nbonded_local += rt->th_work[thread].nbonded;
2076 if (izone < nzone_excl)
2078 if (rt->nthread > 1)
2080 combine_blocka(lexcls, rt->th_work, rt->nthread);
2083 for (thread = 0; thread < rt->nthread; thread++)
2085 *excl_count += rt->th_work[thread].excl_count;
2090 /* Some zones might not have exclusions, but some code still needs to
2091 * loop over the index, so we set the indices here.
2093 for (izone = nzone_excl; izone < zones->nizone; izone++)
2095 set_no_exclusions_zone(dd, zones, izone, lexcls);
2098 finish_local_exclusions(dd, zones, lexcls);
2101 fprintf(debug, "We have %d exclusions, check count %d\n",
2102 lexcls->nra, *excl_count);
2105 return nbonded_local;
2108 void dd_make_local_cgs(gmx_domdec_t *dd, t_block *lcgs)
2110 lcgs->nr = dd->ncg_tot;
2111 lcgs->index = dd->cgindex;
2114 void dd_make_local_top(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
2115 int npbcdim, matrix box,
2116 rvec cellsize_min, ivec npulse,
2120 const gmx_mtop_t *mtop, gmx_localtop_t *ltop)
2122 gmx_bool bRCheckMB, bRCheck2B;
2126 t_pbc pbc, *pbc_null = nullptr;
2130 fprintf(debug, "Making local topology\n");
2133 dd_make_local_cgs(dd, <op->cgs);
2138 if (dd->reverse_top->bInterCGInteractions)
2140 /* We need to check to which cell bondeds should be assigned */
2141 rc = dd_cutoff_twobody(dd);
2144 fprintf(debug, "Two-body bonded cut-off distance is %g\n", rc);
2147 /* Should we check cg_cm distances when assigning bonded interactions? */
2148 for (d = 0; d < DIM; d++)
2151 /* Only need to check for dimensions where the part of the box
2152 * that is not communicated is smaller than the cut-off.
2154 if (d < npbcdim && dd->nc[d] > 1 &&
2155 (dd->nc[d] - npulse[d])*cellsize_min[d] < 2*rc)
2162 /* Check for interactions between two atoms,
2163 * where we can allow interactions up to the cut-off,
2164 * instead of up to the smallest cell dimension.
2171 "dim %d cellmin %f bonded rcheck[%d] = %d, bRCheck2B = %d\n",
2172 d, cellsize_min[d], d, rcheck[d], bRCheck2B);
2175 if (bRCheckMB || bRCheck2B)
2180 pbc_null = set_pbc_dd(&pbc, fr->ePBC, dd->nc, TRUE, box);
2190 make_local_bondeds_excls(dd, zones, mtop, fr->cginfo,
2191 bRCheckMB, rcheck, bRCheck2B, rc,
2193 pbc_null, cgcm_or_x,
2195 <op->excls, &nexcl);
2197 /* The ilist is not sorted yet,
2198 * we can only do this when we have the charge arrays.
2200 ltop->idef.ilsort = ilsortUNKNOWN;
2202 if (dd->reverse_top->bExclRequired)
2204 dd->nbonded_local += nexcl;
2207 ltop->atomtypes = mtop->atomtypes;
2210 void dd_sort_local_top(gmx_domdec_t *dd, const t_mdatoms *mdatoms,
2211 gmx_localtop_t *ltop)
2213 if (dd->reverse_top->ilsort == ilsortNO_FE)
2215 ltop->idef.ilsort = ilsortNO_FE;
2219 gmx_sort_ilist_fe(<op->idef, mdatoms->chargeA, mdatoms->chargeB);
2223 gmx_localtop_t *dd_init_local_top(const gmx_mtop_t *top_global)
2225 gmx_localtop_t *top;
2230 top->idef.ntypes = top_global->ffparams.ntypes;
2231 top->idef.atnr = top_global->ffparams.atnr;
2232 top->idef.functype = top_global->ffparams.functype;
2233 top->idef.iparams = top_global->ffparams.iparams;
2234 top->idef.fudgeQQ = top_global->ffparams.fudgeQQ;
2235 top->idef.cmap_grid = top_global->ffparams.cmap_grid;
2237 for (i = 0; i < F_NRE; i++)
2239 top->idef.il[i].iatoms = nullptr;
2240 top->idef.il[i].nalloc = 0;
2242 top->idef.ilsort = ilsortUNKNOWN;
2247 void dd_init_local_state(gmx_domdec_t *dd,
2248 t_state *state_global, t_state *state_local)
2250 int buf[NITEM_DD_INIT_LOCAL_STATE];
2254 buf[0] = state_global->flags;
2255 buf[1] = state_global->ngtc;
2256 buf[2] = state_global->nnhpres;
2257 buf[3] = state_global->nhchainlength;
2258 buf[4] = state_global->dfhist ? state_global->dfhist->nlambda : 0;
2260 dd_bcast(dd, NITEM_DD_INIT_LOCAL_STATE*sizeof(int), buf);
2262 init_gtc_state(state_local, buf[1], buf[2], buf[3]);
2263 init_dfhist_state(state_local, buf[4]);
2264 state_local->flags = buf[0];
2267 /*! \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 */
2268 static void check_link(t_blocka *link, int cg_gl, int cg_gl_j)
2274 for (k = link->index[cg_gl]; k < link->index[cg_gl+1]; k++)
2276 GMX_RELEASE_ASSERT(link->a, "Inconsistent NULL pointer while making charge-group links");
2277 if (link->a[k] == cg_gl_j)
2284 GMX_RELEASE_ASSERT(link->a || link->index[cg_gl+1]+1 > link->nalloc_a,
2285 "Inconsistent allocation of link");
2286 /* Add this charge group link */
2287 if (link->index[cg_gl+1]+1 > link->nalloc_a)
2289 link->nalloc_a = over_alloc_large(link->index[cg_gl+1]+1);
2290 srenew(link->a, link->nalloc_a);
2292 link->a[link->index[cg_gl+1]] = cg_gl_j;
2293 link->index[cg_gl+1]++;
2297 /*! \brief Allocate and return an array of the charge group index for all atoms */
2298 static int *make_at2cg(t_block *cgs)
2302 snew(at2cg, cgs->index[cgs->nr]);
2303 for (cg = 0; cg < cgs->nr; cg++)
2305 for (a = cgs->index[cg]; a < cgs->index[cg+1]; a++)
2314 t_blocka *make_charge_group_links(const gmx_mtop_t *mtop, gmx_domdec_t *dd,
2315 cginfo_mb_t *cginfo_mb)
2317 gmx_bool bExclRequired;
2318 int mb, cg_offset, cg, cg_gl, a, aj, i, j, ftype, nral, nlink_mol, mol, ncgi;
2319 gmx_molblock_t *molb;
2320 gmx_moltype_t *molt;
2324 reverse_ilist_t ril, ril_intermol;
2326 cginfo_mb_t *cgi_mb;
2328 /* For each charge group make a list of other charge groups
2329 * in the system that a linked to it via bonded interactions
2330 * which are also stored in reverse_top.
2333 bExclRequired = dd->reverse_top->bExclRequired;
2335 if (mtop->bIntermolecularInteractions)
2337 if (ncg_mtop(mtop) < mtop->natoms)
2339 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.");
2344 atoms.nr = mtop->natoms;
2345 atoms.atom = nullptr;
2347 make_reverse_ilist(mtop->intermolecular_ilist, &atoms,
2348 nullptr, FALSE, FALSE, FALSE, TRUE, &ril_intermol);
2352 snew(link->index, ncg_mtop(mtop)+1);
2359 for (mb = 0; mb < mtop->nmolblock; mb++)
2361 molb = &mtop->molblock[mb];
2362 if (molb->nmol == 0)
2366 molt = &mtop->moltype[molb->type];
2368 excls = &molt->excls;
2369 a2c = make_at2cg(cgs);
2370 /* Make a reverse ilist in which the interactions are linked
2371 * to all atoms, not only the first atom as in gmx_reverse_top.
2372 * The constraints are discarded here.
2374 make_reverse_ilist(molt->ilist, &molt->atoms,
2375 nullptr, FALSE, FALSE, FALSE, TRUE, &ril);
2377 cgi_mb = &cginfo_mb[mb];
2379 for (mol = 0; mol < (mtop->bIntermolecularInteractions ? molb->nmol : 1); mol++)
2381 for (cg = 0; cg < cgs->nr; cg++)
2383 cg_gl = cg_offset + cg;
2384 link->index[cg_gl+1] = link->index[cg_gl];
2385 for (a = cgs->index[cg]; a < cgs->index[cg+1]; a++)
2388 while (i < ril.index[a+1])
2390 ftype = ril.il[i++];
2392 /* Skip the ifunc index */
2394 for (j = 0; j < nral; j++)
2399 check_link(link, cg_gl, cg_offset+a2c[aj]);
2402 i += nral_rt(ftype);
2406 /* Exclusions always go both ways */
2407 for (j = excls->index[a]; j < excls->index[a+1]; j++)
2412 check_link(link, cg_gl, cg_offset+a2c[aj]);
2417 if (mtop->bIntermolecularInteractions)
2419 i = ril_intermol.index[a];
2420 while (i < ril_intermol.index[a+1])
2422 ftype = ril_intermol.il[i++];
2424 /* Skip the ifunc index */
2426 for (j = 0; j < nral; j++)
2428 /* Here we assume we have no charge groups;
2429 * this has been checked above.
2431 aj = ril_intermol.il[i+j];
2432 check_link(link, cg_gl, aj);
2434 i += nral_rt(ftype);
2438 if (link->index[cg_gl+1] - link->index[cg_gl] > 0)
2440 SET_CGINFO_BOND_INTER(cgi_mb->cginfo[cg]);
2445 cg_offset += cgs->nr;
2447 nlink_mol = link->index[cg_offset] - link->index[cg_offset-cgs->nr];
2449 destroy_reverse_ilist(&ril);
2454 fprintf(debug, "molecule type '%s' %d cgs has %d cg links through bonded interac.\n", *molt->name, cgs->nr, nlink_mol);
2457 if (molb->nmol > mol)
2459 /* Copy the data for the rest of the molecules in this block */
2460 link->nalloc_a += (molb->nmol - mol)*nlink_mol;
2461 srenew(link->a, link->nalloc_a);
2462 for (; mol < molb->nmol; mol++)
2464 for (cg = 0; cg < cgs->nr; cg++)
2466 cg_gl = cg_offset + cg;
2467 link->index[cg_gl+1] =
2468 link->index[cg_gl+1-cgs->nr] + nlink_mol;
2469 for (j = link->index[cg_gl]; j < link->index[cg_gl+1]; j++)
2471 link->a[j] = link->a[j-nlink_mol] + cgs->nr;
2473 if (link->index[cg_gl+1] - link->index[cg_gl] > 0 &&
2474 cg_gl - cgi_mb->cg_start < cgi_mb->cg_mod)
2476 SET_CGINFO_BOND_INTER(cgi_mb->cginfo[cg_gl - cgi_mb->cg_start]);
2480 cg_offset += cgs->nr;
2485 if (mtop->bIntermolecularInteractions)
2487 destroy_reverse_ilist(&ril_intermol);
2492 fprintf(debug, "Of the %d charge groups %d are linked via bonded interactions\n", ncg_mtop(mtop), ncgi);
2503 } bonded_distance_t;
2505 /*! \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 */
2506 static void update_max_bonded_distance(real r2, int ftype, int a1, int a2,
2507 bonded_distance_t *bd)
2518 /*! \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 */
2519 static void bonded_cg_distance_mol(gmx_moltype_t *molt, int *at2cg,
2520 gmx_bool bBCheck, gmx_bool bExcl, rvec *cg_cm,
2521 bonded_distance_t *bd_2b,
2522 bonded_distance_t *bd_mb)
2524 for (int ftype = 0; ftype < F_NRE; ftype++)
2526 if (dd_check_ftype(ftype, bBCheck, FALSE, FALSE))
2528 const t_ilist *il = &molt->ilist[ftype];
2529 int nral = NRAL(ftype);
2532 for (int i = 0; i < il->nr; i += 1+nral)
2534 for (int ai = 0; ai < nral; ai++)
2536 int cgi = at2cg[il->iatoms[i+1+ai]];
2537 for (int aj = ai + 1; aj < nral; aj++)
2539 int cgj = at2cg[il->iatoms[i+1+aj]];
2542 real rij2 = distance2(cg_cm[cgi], cg_cm[cgj]);
2544 update_max_bonded_distance(rij2, ftype,
2547 (nral == 2) ? bd_2b : bd_mb);
2557 const t_blocka *excls = &molt->excls;
2558 for (int ai = 0; ai < excls->nr; ai++)
2560 int cgi = at2cg[ai];
2561 for (int j = excls->index[ai]; j < excls->index[ai+1]; j++)
2563 int cgj = at2cg[excls->a[j]];
2566 real rij2 = distance2(cg_cm[cgi], cg_cm[cgj]);
2568 /* There is no function type for exclusions, use -1 */
2569 update_max_bonded_distance(rij2, -1, ai, excls->a[j], bd_2b);
2576 /*! \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 */
2577 static void bonded_distance_intermol(const t_ilist *ilists_intermol,
2579 const rvec *x, int ePBC, const matrix box,
2580 bonded_distance_t *bd_2b,
2581 bonded_distance_t *bd_mb)
2585 set_pbc(&pbc, ePBC, box);
2587 for (int ftype = 0; ftype < F_NRE; ftype++)
2589 if (dd_check_ftype(ftype, bBCheck, FALSE, FALSE))
2591 const t_ilist *il = &ilists_intermol[ftype];
2592 int nral = NRAL(ftype);
2594 /* No nral>1 check here, since intermol interactions always
2595 * have nral>=2 (and the code is also correct for nral=1).
2597 for (int i = 0; i < il->nr; i += 1+nral)
2599 for (int ai = 0; ai < nral; ai++)
2601 int atom_i = il->iatoms[i + 1 + ai];
2603 for (int aj = ai + 1; aj < nral; aj++)
2608 int atom_j = il->iatoms[i + 1 + aj];
2610 pbc_dx(&pbc, x[atom_i], x[atom_j], dx);
2614 update_max_bonded_distance(rij2, ftype,
2616 (nral == 2) ? bd_2b : bd_mb);
2624 //! Returns whether \p molt has at least one virtual site
2625 static bool moltypeHasVsite(const gmx_moltype_t &molt)
2627 bool hasVsite = false;
2628 for (int i = 0; i < F_NRE; i++)
2630 if ((interaction_function[i].flags & IF_VSITE) &&
2631 molt.ilist[i].nr > 0)
2640 //! Compute charge group centers of mass for molecule \p molt
2641 static void get_cgcm_mol(const gmx_moltype_t *molt,
2642 const gmx_ffparams_t *ffparams,
2643 int ePBC, t_graph *graph, const matrix box,
2644 const rvec *x, rvec *xs, rvec *cg_cm)
2648 if (ePBC != epbcNONE)
2650 mk_mshift(nullptr, graph, ePBC, box, x);
2652 shift_x(graph, box, x, xs);
2653 /* By doing an extra mk_mshift the molecules that are broken
2654 * because they were e.g. imported from another software
2655 * will be made whole again. Such are the healing powers
2658 mk_mshift(nullptr, graph, ePBC, box, xs);
2662 /* We copy the coordinates so the original coordinates remain
2663 * unchanged, just to be 100% sure that we do not affect
2664 * binary reproducibility of simulations.
2666 n = molt->cgs.index[molt->cgs.nr];
2667 for (i = 0; i < n; i++)
2669 copy_rvec(x[i], xs[i]);
2673 if (moltypeHasVsite(*molt))
2675 construct_vsites(nullptr, xs, 0.0, nullptr,
2676 ffparams->iparams, molt->ilist,
2677 epbcNONE, TRUE, nullptr, nullptr);
2680 calc_cgcm(nullptr, 0, molt->cgs.nr, &molt->cgs, xs, cg_cm);
2683 void dd_bonded_cg_distance(FILE *fplog,
2684 const gmx_mtop_t *mtop,
2685 const t_inputrec *ir,
2686 const rvec *x, const matrix box,
2688 real *r_2b, real *r_mb)
2690 gmx_bool bExclRequired;
2691 int mb, at_offset, *at2cg, mol;
2693 gmx_molblock_t *molb;
2694 gmx_moltype_t *molt;
2696 bonded_distance_t bd_2b = { 0, -1, -1, -1 };
2697 bonded_distance_t bd_mb = { 0, -1, -1, -1 };
2699 bExclRequired = inputrecExclForces(ir);
2704 for (mb = 0; mb < mtop->nmolblock; mb++)
2706 molb = &mtop->molblock[mb];
2707 molt = &mtop->moltype[molb->type];
2708 if (molt->cgs.nr == 1 || molb->nmol == 0)
2710 at_offset += molb->nmol*molt->atoms.nr;
2714 if (ir->ePBC != epbcNONE)
2716 mk_graph_ilist(nullptr, molt->ilist, 0, molt->atoms.nr, FALSE, FALSE,
2720 at2cg = make_at2cg(&molt->cgs);
2721 snew(xs, molt->atoms.nr);
2722 snew(cg_cm, molt->cgs.nr);
2723 for (mol = 0; mol < molb->nmol; mol++)
2725 get_cgcm_mol(molt, &mtop->ffparams, ir->ePBC, &graph, box,
2726 x+at_offset, xs, cg_cm);
2728 bonded_distance_t bd_mol_2b = { 0, -1, -1, -1 };
2729 bonded_distance_t bd_mol_mb = { 0, -1, -1, -1 };
2731 bonded_cg_distance_mol(molt, at2cg, bBCheck, bExclRequired, cg_cm,
2732 &bd_mol_2b, &bd_mol_mb);
2734 /* Process the mol data adding the atom index offset */
2735 update_max_bonded_distance(bd_mol_2b.r2, bd_mol_2b.ftype,
2736 at_offset + bd_mol_2b.a1,
2737 at_offset + bd_mol_2b.a2,
2739 update_max_bonded_distance(bd_mol_mb.r2, bd_mol_mb.ftype,
2740 at_offset + bd_mol_mb.a1,
2741 at_offset + bd_mol_mb.a2,
2744 at_offset += molt->atoms.nr;
2749 if (ir->ePBC != epbcNONE)
2756 if (mtop->bIntermolecularInteractions)
2758 if (ncg_mtop(mtop) < mtop->natoms)
2760 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.");
2763 bonded_distance_intermol(mtop->intermolecular_ilist,
2769 *r_2b = sqrt(bd_2b.r2);
2770 *r_mb = sqrt(bd_mb.r2);
2772 if (fplog && (*r_2b > 0 || *r_mb > 0))
2775 "Initial maximum inter charge-group distances:\n");
2779 " two-body bonded interactions: %5.3f nm, %s, atoms %d %d\n",
2780 *r_2b, (bd_2b.ftype >= 0) ? interaction_function[bd_2b.ftype].longname : "Exclusion",
2781 bd_2b.a1 + 1, bd_2b.a2 + 1);
2786 " multi-body bonded interactions: %5.3f nm, %s, atoms %d %d\n",
2787 *r_mb, interaction_function[bd_mb.ftype].longname,
2788 bd_mb.a1 + 1, bd_mb.a2 + 1);