2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2006,2007,2008,2009,2010,2011,2012,2013,2014, 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.
43 #include "types/commrec.h"
45 #include "domdec_network.h"
48 #include "gromacs/math/vec.h"
50 #include "chargegroup.h"
51 #include "gromacs/gmxlib/topsort.h"
52 #include "mtop_util.h"
55 #include "gmx_ga2la.h"
57 #include "gmx_omp_nthreads.h"
59 #include "gromacs/utility/cstringutil.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/smalloc.h"
63 /* for dd_init_local_state */
64 #define NITEM_DD_INIT_LOCAL_STATE 5
67 int *index; /* Index for each atom into il */
68 int *il; /* ftype|type|a0|...|an|ftype|... */
69 } gmx_reverse_ilist_t;
78 typedef struct gmx_reverse_top {
79 gmx_bool bExclRequired; /* Do we require all exclusions to be assigned? */
80 gmx_bool bConstr; /* Are there constraints in this revserse top? */
81 gmx_bool bSettle; /* Are there settles in this revserse top? */
82 gmx_bool bBCheck; /* All bonded interactions have to be assigned? */
83 gmx_bool bMultiCGmols; /* Are the multi charge-group molecules? */
84 gmx_reverse_ilist_t *ril_mt; /* Reverse ilist for all moltypes */
86 int ilsort; /* The sorting state of bondeds for free energy */
87 gmx_molblock_ind_t *mbi;
90 /* Work data structures for multi-threading */
94 int **vsite_pbc_nalloc;
96 t_blocka *excl_thread;
97 int *excl_count_thread;
99 /* Pointers only used for an error message */
100 gmx_mtop_t *err_top_global;
101 gmx_localtop_t *err_top_local;
104 static int nral_rt(int ftype)
106 /* Returns the number of atom entries for il in gmx_reverse_top_t */
110 if (interaction_function[ftype].flags & IF_VSITE)
112 /* With vsites the reverse topology contains
113 * two extra entries for PBC.
121 /* This function tells which interactions need to be assigned exactly once */
122 static gmx_bool dd_check_ftype(int ftype, gmx_bool bBCheck,
123 gmx_bool bConstr, gmx_bool bSettle)
125 return (((interaction_function[ftype].flags & IF_BOND) &&
126 !(interaction_function[ftype].flags & IF_VSITE) &&
127 (bBCheck || !(interaction_function[ftype].flags & IF_LIMZERO))) ||
128 (bConstr && (ftype == F_CONSTR || ftype == F_CONSTRNC)) ||
129 (bSettle && ftype == F_SETTLE));
132 static void print_error_header(FILE *fplog, char *moltypename, int nprint)
134 fprintf(fplog, "\nMolecule type '%s'\n", moltypename);
135 fprintf(stderr, "\nMolecule type '%s'\n", moltypename);
137 "the first %d missing interactions, except for exclusions:\n",
140 "the first %d missing interactions, except for exclusions:\n",
144 static void print_missing_interactions_mb(FILE *fplog, t_commrec *cr,
145 gmx_reverse_top_t *rt,
147 gmx_reverse_ilist_t *ril,
148 int a_start, int a_end,
149 int nat_mol, int nmol,
152 int nril_mol, *assigned, *gatindex;
153 int ftype, ftype_j, nral, i, j_mol, j, k, a0, a0_mol, mol, a, a_gl;
159 nril_mol = ril->index[nat_mol];
160 snew(assigned, nmol*nril_mol);
162 gatindex = cr->dd->gatindex;
163 for (ftype = 0; ftype < F_NRE; ftype++)
165 if (dd_check_ftype(ftype, rt->bBCheck, rt->bConstr, rt->bSettle))
168 il = &idef->il[ftype];
170 for (i = 0; i < il->nr; i += 1+nral)
172 a0 = gatindex[ia[1]];
173 /* Check if this interaction is in
174 * the currently checked molblock.
176 if (a0 >= a_start && a0 < a_end)
178 mol = (a0 - a_start)/nat_mol;
179 a0_mol = (a0 - a_start) - mol*nat_mol;
180 j_mol = ril->index[a0_mol];
182 while (j_mol < ril->index[a0_mol+1] && !bFound)
184 j = mol*nril_mol + j_mol;
185 ftype_j = ril->il[j_mol];
186 /* Here we need to check if this interaction has
187 * not already been assigned, since we could have
188 * multiply defined interactions.
190 if (ftype == ftype_j && ia[0] == ril->il[j_mol+1] &&
193 /* Check the atoms */
195 for (a = 0; a < nral; a++)
197 if (gatindex[ia[1+a]] !=
198 a_start + mol*nat_mol + ril->il[j_mol+2+a])
208 j_mol += 2 + nral_rt(ftype_j);
212 gmx_incons("Some interactions seem to be assigned multiple times");
220 gmx_sumi(nmol*nril_mol, assigned, cr);
224 for (mol = 0; mol < nmol; mol++)
227 while (j_mol < nril_mol)
229 ftype = ril->il[j_mol];
231 j = mol*nril_mol + j_mol;
232 if (assigned[j] == 0 &&
233 !(interaction_function[ftype].flags & IF_VSITE))
235 if (DDMASTER(cr->dd))
239 print_error_header(fplog, moltypename, nprint);
241 fprintf(fplog, "%20s atoms",
242 interaction_function[ftype].longname);
243 fprintf(stderr, "%20s atoms",
244 interaction_function[ftype].longname);
245 for (a = 0; a < nral; a++)
247 fprintf(fplog, "%5d", ril->il[j_mol+2+a]+1);
248 fprintf(stderr, "%5d", ril->il[j_mol+2+a]+1);
253 fprintf(stderr, " ");
256 fprintf(fplog, " global");
257 fprintf(stderr, " global");
258 for (a = 0; a < nral; a++)
260 fprintf(fplog, "%6d",
261 a_start+mol*nat_mol+ril->il[j_mol+2+a]+1);
262 fprintf(stderr, "%6d",
263 a_start+mol*nat_mol+ril->il[j_mol+2+a]+1);
265 fprintf(fplog, "\n");
266 fprintf(stderr, "\n");
274 j_mol += 2 + nral_rt(ftype);
281 static void print_missing_interactions_atoms(FILE *fplog, t_commrec *cr,
282 gmx_mtop_t *mtop, t_idef *idef)
284 int mb, a_start, a_end;
285 gmx_molblock_t *molb;
286 gmx_reverse_top_t *rt;
288 rt = cr->dd->reverse_top;
290 /* Print the atoms in the missing interactions per molblock */
292 for (mb = 0; mb < mtop->nmolblock; mb++)
294 molb = &mtop->molblock[mb];
296 a_end = a_start + molb->nmol*molb->natoms_mol;
298 print_missing_interactions_mb(fplog, cr, rt,
299 *(mtop->moltype[molb->type].name),
300 &rt->ril_mt[molb->type],
301 a_start, a_end, molb->natoms_mol,
307 void dd_print_missing_interactions(FILE *fplog, t_commrec *cr, int local_count, gmx_mtop_t *top_global, t_state *state_local)
309 int ndiff_tot, cl[F_NRE], n, ndiff, rest_global, rest_local;
313 gmx_mtop_t *err_top_global;
314 gmx_localtop_t *err_top_local;
318 err_top_global = dd->reverse_top->err_top_global;
319 err_top_local = dd->reverse_top->err_top_local;
323 fprintf(fplog, "\nNot all bonded interactions have been properly assigned to the domain decomposition cells\n");
327 ndiff_tot = local_count - dd->nbonded_global;
329 for (ftype = 0; ftype < F_NRE; ftype++)
332 cl[ftype] = err_top_local->idef.il[ftype].nr/(1+nral);
335 gmx_sumi(F_NRE, cl, cr);
339 fprintf(fplog, "\nA list of missing interactions:\n");
340 fprintf(stderr, "\nA list of missing interactions:\n");
341 rest_global = dd->nbonded_global;
342 rest_local = local_count;
343 for (ftype = 0; ftype < F_NRE; ftype++)
345 /* In the reverse and local top all constraints are merged
346 * into F_CONSTR. So in the if statement we skip F_CONSTRNC
347 * and add these constraints when doing F_CONSTR.
349 if (((interaction_function[ftype].flags & IF_BOND) &&
350 (dd->reverse_top->bBCheck
351 || !(interaction_function[ftype].flags & IF_LIMZERO)))
352 || (dd->reverse_top->bConstr && ftype == F_CONSTR)
353 || (dd->reverse_top->bSettle && ftype == F_SETTLE))
356 n = gmx_mtop_ftype_count(err_top_global, ftype);
357 if (ftype == F_CONSTR)
359 n += gmx_mtop_ftype_count(err_top_global, F_CONSTRNC);
361 ndiff = cl[ftype] - n;
364 sprintf(buf, "%20s of %6d missing %6d",
365 interaction_function[ftype].longname, n, -ndiff);
366 fprintf(fplog, "%s\n", buf);
367 fprintf(stderr, "%s\n", buf);
370 rest_local -= cl[ftype];
374 ndiff = rest_local - rest_global;
377 sprintf(buf, "%20s of %6d missing %6d", "exclusions",
378 rest_global, -ndiff);
379 fprintf(fplog, "%s\n", buf);
380 fprintf(stderr, "%s\n", buf);
384 print_missing_interactions_atoms(fplog, cr, err_top_global,
385 &err_top_local->idef);
386 write_dd_pdb("dd_dump_err", 0, "dump", top_global, cr,
387 -1, state_local->x, state_local->box);
392 gmx_incons("One or more interactions were multiple assigned in the domain decompostion");
396 gmx_fatal(FARGS, "%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_mbody(cr->dd), dd_cutoff_twobody(cr->dd));
401 static void global_atomnr_to_moltype_ind(gmx_reverse_top_t *rt, int i_gl,
402 int *mb, int *mt, int *mol, int *i_mol)
407 gmx_molblock_ind_t *mbi = rt->mbi;
409 int end = rt->nmolblock; /* exclusive */
412 /* binary search for molblock_ind */
416 if (i_gl >= mbi[mid].a_end)
420 else if (i_gl < mbi[mid].a_start)
434 *mol = (i_gl - mbi->a_start) / mbi->natoms_mol;
435 *i_mol = (i_gl - mbi->a_start) - (*mol)*mbi->natoms_mol;
438 static int count_excls(t_block *cgs, t_blocka *excls, int *n_intercg_excl)
440 int n, n_inter, cg, at0, at1, at, excl, atj;
444 for (cg = 0; cg < cgs->nr; cg++)
446 at0 = cgs->index[cg];
447 at1 = cgs->index[cg+1];
448 for (at = at0; at < at1; at++)
450 for (excl = excls->index[at]; excl < excls->index[at+1]; excl++)
452 atj = excls->a[excl];
456 if (atj < at0 || atj >= at1)
468 static int low_make_reverse_ilist(t_ilist *il_mt, t_atom *atom,
471 gmx_bool bConstr, gmx_bool bSettle,
473 int *r_index, int *r_il,
474 gmx_bool bLinkToAllAtoms,
477 int ftype, nral, i, j, nlink, link;
485 for (ftype = 0; ftype < F_NRE; ftype++)
487 if ((interaction_function[ftype].flags & (IF_BOND | IF_VSITE)) ||
488 (bConstr && (ftype == F_CONSTR || ftype == F_CONSTRNC)) ||
489 (bSettle && ftype == F_SETTLE))
491 bVSite = (interaction_function[ftype].flags & IF_VSITE);
495 for (i = 0; i < il->nr; i += 1+nral)
502 /* We don't need the virtual sites for the cg-links */
512 /* Couple to the first atom in the interaction */
515 for (link = 0; link < nlink; link++)
520 r_il[r_index[a]+count[a]] =
521 (ftype == F_CONSTRNC ? F_CONSTR : ftype);
522 r_il[r_index[a]+count[a]+1] = ia[0];
523 for (j = 1; j < 1+nral; j++)
525 /* Store the molecular atom number */
526 r_il[r_index[a]+count[a]+1+j] = ia[j];
529 if (interaction_function[ftype].flags & IF_VSITE)
533 /* Add an entry to iatoms for storing
534 * which of the constructing atoms are
537 r_il[r_index[a]+count[a]+2+nral] = 0;
538 for (j = 2; j < 1+nral; j++)
540 if (atom[ia[j]].ptype == eptVSite)
542 r_il[r_index[a]+count[a]+2+nral] |= (2<<j);
545 /* Store vsite pbc atom in a second extra entry */
546 r_il[r_index[a]+count[a]+2+nral+1] =
547 (vsite_pbc ? vsite_pbc[ftype-F_VSITE2][i/(1+nral)] : -2);
552 /* We do not count vsites since they are always
553 * uniquely assigned and can be assigned
554 * to multiple nodes with recursive vsites.
557 !(interaction_function[ftype].flags & IF_LIMZERO))
562 count[a] += 2 + nral_rt(ftype);
571 static int make_reverse_ilist(gmx_moltype_t *molt,
573 gmx_bool bConstr, gmx_bool bSettle,
575 gmx_bool bLinkToAllAtoms,
576 gmx_reverse_ilist_t *ril_mt)
578 int nat_mt, *count, i, nint_mt;
580 /* Count the interactions */
581 nat_mt = molt->atoms.nr;
583 low_make_reverse_ilist(molt->ilist, molt->atoms.atom, vsite_pbc,
585 bConstr, bSettle, bBCheck, NULL, NULL,
586 bLinkToAllAtoms, FALSE);
588 snew(ril_mt->index, nat_mt+1);
589 ril_mt->index[0] = 0;
590 for (i = 0; i < nat_mt; i++)
592 ril_mt->index[i+1] = ril_mt->index[i] + count[i];
595 snew(ril_mt->il, ril_mt->index[nat_mt]);
597 /* Store the interactions */
599 low_make_reverse_ilist(molt->ilist, molt->atoms.atom, vsite_pbc,
601 bConstr, bSettle, bBCheck,
602 ril_mt->index, ril_mt->il,
603 bLinkToAllAtoms, TRUE);
610 static void destroy_reverse_ilist(gmx_reverse_ilist_t *ril)
616 static gmx_reverse_top_t *make_reverse_top(gmx_mtop_t *mtop, gmx_bool bFE,
617 int ***vsite_pbc_molt,
618 gmx_bool bConstr, gmx_bool bSettle,
619 gmx_bool bBCheck, int *nint)
622 gmx_reverse_top_t *rt;
629 /* Should we include constraints (for SHAKE) in rt? */
630 rt->bConstr = bConstr;
631 rt->bSettle = bSettle;
632 rt->bBCheck = bBCheck;
634 rt->bMultiCGmols = FALSE;
635 snew(nint_mt, mtop->nmoltype);
636 snew(rt->ril_mt, mtop->nmoltype);
637 rt->ril_mt_tot_size = 0;
638 for (mt = 0; mt < mtop->nmoltype; mt++)
640 molt = &mtop->moltype[mt];
641 if (molt->cgs.nr > 1)
643 rt->bMultiCGmols = TRUE;
646 /* Make the atom to interaction list for this molecule type */
648 make_reverse_ilist(molt, vsite_pbc_molt ? vsite_pbc_molt[mt] : NULL,
649 rt->bConstr, rt->bSettle, rt->bBCheck, FALSE,
652 rt->ril_mt_tot_size += rt->ril_mt[mt].index[molt->atoms.nr];
656 fprintf(debug, "The total size of the atom to interaction index is %d integers\n", rt->ril_mt_tot_size);
660 for (mb = 0; mb < mtop->nmolblock; mb++)
662 *nint += mtop->molblock[mb].nmol*nint_mt[mtop->molblock[mb].type];
666 if (bFE && gmx_mtop_bondeds_free_energy(mtop))
668 rt->ilsort = ilsortFE_UNSORTED;
672 rt->ilsort = ilsortNO_FE;
675 /* Make a molblock index for fast searching */
676 snew(rt->mbi, mtop->nmolblock);
677 rt->nmolblock = mtop->nmolblock;
679 for (mb = 0; mb < mtop->nmolblock; mb++)
681 rt->mbi[mb].a_start = i;
682 i += mtop->molblock[mb].nmol*mtop->molblock[mb].natoms_mol;
683 rt->mbi[mb].a_end = i;
684 rt->mbi[mb].natoms_mol = mtop->molblock[mb].natoms_mol;
685 rt->mbi[mb].type = mtop->molblock[mb].type;
688 rt->nthread = gmx_omp_nthreads_get(emntDomdec);
689 snew(rt->idef_thread, rt->nthread);
690 if (vsite_pbc_molt != NULL)
692 snew(rt->vsite_pbc, rt->nthread);
693 snew(rt->vsite_pbc_nalloc, rt->nthread);
694 for (thread = 0; thread < rt->nthread; thread++)
696 snew(rt->vsite_pbc[thread], F_VSITEN-F_VSITE2+1);
697 snew(rt->vsite_pbc_nalloc[thread], F_VSITEN-F_VSITE2+1);
700 snew(rt->nbonded_thread, rt->nthread);
701 snew(rt->excl_thread, rt->nthread);
702 snew(rt->excl_count_thread, rt->nthread);
707 void dd_make_reverse_top(FILE *fplog,
708 gmx_domdec_t *dd, gmx_mtop_t *mtop,
710 t_inputrec *ir, gmx_bool bBCheck)
712 int mb, n_recursive_vsite, nexcl, nexcl_icg, a;
713 gmx_molblock_t *molb;
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 algoirthm(s).
727 dd->reverse_top = make_reverse_top(mtop, ir->efep != efepNO,
728 vsite ? vsite->vsite_pbc_molt : NULL,
729 !dd->bInterCGcons, !dd->bInterCGsettles,
730 bBCheck, &dd->nbonded_global);
732 if (dd->reverse_top->ril_mt_tot_size >= 200000 &&
734 mtop->nmolblock == 1 && mtop->molblock[0].nmol == 1)
736 /* mtop comes from a pre Gromacs 4 tpr file */
737 const char *note = "NOTE: The tpr file used for this simulation is in an old format, for less memory usage and possibly more performance create a new tpr file with an up to date version of grompp";
740 fprintf(fplog, "\n%s\n\n", note);
744 fprintf(stderr, "\n%s\n\n", note);
748 dd->reverse_top->bExclRequired = IR_EXCL_FORCES(*ir);
751 dd->n_intercg_excl = 0;
752 for (mb = 0; mb < mtop->nmolblock; mb++)
754 molb = &mtop->molblock[mb];
755 molt = &mtop->moltype[molb->type];
756 nexcl += molb->nmol*count_excls(&molt->cgs, &molt->excls, &nexcl_icg);
757 dd->n_intercg_excl += molb->nmol*nexcl_icg;
759 if (dd->reverse_top->bExclRequired)
761 dd->nbonded_global += nexcl;
762 if (EEL_FULL(ir->coulombtype) && dd->n_intercg_excl > 0 && fplog)
764 fprintf(fplog, "There are %d inter charge-group exclusions,\n"
765 "will use an extra communication step for exclusion forces for %s\n",
766 dd->n_intercg_excl, eel_names[ir->coulombtype]);
770 if (vsite && vsite->n_intercg_vsite > 0)
774 fprintf(fplog, "There are %d inter charge-group virtual sites,\n"
775 "will an extra communication step for selected coordinates and forces\n",
776 vsite->n_intercg_vsite);
778 init_domdec_vsites(dd, vsite->n_intercg_vsite);
781 if (dd->bInterCGcons || dd->bInterCGsettles)
783 init_domdec_constraints(dd, mtop);
787 fprintf(fplog, "\n");
791 static gmx_inline void add_ifunc(int nral, t_iatom *tiatoms, t_ilist *il)
796 if (il->nr+1+nral > il->nalloc)
798 il->nalloc = over_alloc_large(il->nr+1+nral);
799 srenew(il->iatoms, il->nalloc);
801 liatoms = il->iatoms + il->nr;
802 for (k = 0; k <= nral; k++)
804 liatoms[k] = tiatoms[k];
809 static void add_posres(int mol, int a_mol, const gmx_molblock_t *molb,
810 t_iatom *iatoms, const t_iparams *ip_in,
816 /* This position restraint has not been added yet,
817 * so it's index is the current number of position restraints.
819 n = idef->il[F_POSRES].nr/2;
820 if (n+1 > idef->iparams_posres_nalloc)
822 idef->iparams_posres_nalloc = over_alloc_dd(n+1);
823 srenew(idef->iparams_posres, idef->iparams_posres_nalloc);
825 ip = &idef->iparams_posres[n];
826 /* Copy the force constants */
827 *ip = ip_in[iatoms[0]];
829 /* Get the position restraint coordinates from the molblock */
830 a_molb = mol*molb->natoms_mol + a_mol;
831 if (a_molb >= molb->nposres_xA)
833 gmx_incons("Not enough position restraint coordinates");
835 ip->posres.pos0A[XX] = molb->posres_xA[a_molb][XX];
836 ip->posres.pos0A[YY] = molb->posres_xA[a_molb][YY];
837 ip->posres.pos0A[ZZ] = molb->posres_xA[a_molb][ZZ];
838 if (molb->nposres_xB > 0)
840 ip->posres.pos0B[XX] = molb->posres_xB[a_molb][XX];
841 ip->posres.pos0B[YY] = molb->posres_xB[a_molb][YY];
842 ip->posres.pos0B[ZZ] = molb->posres_xB[a_molb][ZZ];
846 ip->posres.pos0B[XX] = ip->posres.pos0A[XX];
847 ip->posres.pos0B[YY] = ip->posres.pos0A[YY];
848 ip->posres.pos0B[ZZ] = ip->posres.pos0A[ZZ];
850 /* Set the parameter index for idef->iparams_posre */
854 static void add_fbposres(int mol, int a_mol, const gmx_molblock_t *molb,
855 t_iatom *iatoms, const t_iparams *ip_in,
861 /* This flat-bottom position restraint has not been added yet,
862 * so it's index is the current number of position restraints.
864 n = idef->il[F_FBPOSRES].nr/2;
865 if (n+1 > idef->iparams_fbposres_nalloc)
867 idef->iparams_fbposres_nalloc = over_alloc_dd(n+1);
868 srenew(idef->iparams_fbposres, idef->iparams_fbposres_nalloc);
870 ip = &idef->iparams_fbposres[n];
871 /* Copy the force constants */
872 *ip = ip_in[iatoms[0]];
874 /* Get the position restriant coordinats from the molblock */
875 a_molb = mol*molb->natoms_mol + a_mol;
876 if (a_molb >= molb->nposres_xA)
878 gmx_incons("Not enough position restraint coordinates");
880 /* Take reference positions from A position of normal posres */
881 ip->fbposres.pos0[XX] = molb->posres_xA[a_molb][XX];
882 ip->fbposres.pos0[YY] = molb->posres_xA[a_molb][YY];
883 ip->fbposres.pos0[ZZ] = molb->posres_xA[a_molb][ZZ];
885 /* Note: no B-type for flat-bottom posres */
887 /* Set the parameter index for idef->iparams_posre */
891 static void add_vsite(gmx_ga2la_t ga2la, int *index, int *rtil,
893 gmx_bool bHomeA, int a, int a_gl, int a_mol,
895 t_idef *idef, int **vsite_pbc, int *vsite_pbc_nalloc)
897 int k, ak_gl, vsi, pbc_a_mol;
898 t_iatom tiatoms[1+MAXATOMLIST], *iatoms_r;
899 int j, ftype_r, nral_r;
902 tiatoms[0] = iatoms[0];
906 /* We know the local index of the first atom */
911 /* Convert later in make_local_vsites */
912 tiatoms[1] = -a_gl - 1;
915 for (k = 2; k < 1+nral; k++)
917 ak_gl = a_gl + iatoms[k] - a_mol;
918 if (!ga2la_get_home(ga2la, ak_gl, &tiatoms[k]))
920 /* Copy the global index, convert later in make_local_vsites */
921 tiatoms[k] = -(ak_gl + 1);
925 /* Add this interaction to the local topology */
926 add_ifunc(nral, tiatoms, &idef->il[ftype]);
929 vsi = idef->il[ftype].nr/(1+nral) - 1;
930 if (vsi >= vsite_pbc_nalloc[ftype-F_VSITE2])
932 vsite_pbc_nalloc[ftype-F_VSITE2] = over_alloc_large(vsi+1);
933 srenew(vsite_pbc[ftype-F_VSITE2], vsite_pbc_nalloc[ftype-F_VSITE2]);
937 pbc_a_mol = iatoms[1+nral+1];
940 /* The pbc flag is one of the following two options:
941 * -2: vsite and all constructing atoms are within the same cg, no pbc
942 * -1: vsite and its first constructing atom are in the same cg, do pbc
944 vsite_pbc[ftype-F_VSITE2][vsi] = pbc_a_mol;
948 /* Set the pbc atom for this vsite so we can make its pbc
949 * identical to the rest of the atoms in its charge group.
950 * Since the order of the atoms does not change within a charge
951 * group, we do not need the global to local atom index.
953 vsite_pbc[ftype-F_VSITE2][vsi] = a + pbc_a_mol - iatoms[1];
958 /* This vsite is non-home (required for recursion),
959 * and therefore there is no charge group to match pbc with.
960 * But we always turn on full_pbc to assure that higher order
961 * recursion works correctly.
963 vsite_pbc[ftype-F_VSITE2][vsi] = -1;
969 /* Check for recursion */
970 for (k = 2; k < 1+nral; k++)
972 if ((iatoms[1+nral] & (2<<k)) && (tiatoms[k] < 0))
974 /* This construction atoms is a vsite and not a home atom */
977 fprintf(debug, "Constructing atom %d of vsite atom %d is a vsite and non-home\n", iatoms[k]+1, a_mol+1);
979 /* Find the vsite construction */
981 /* Check all interactions assigned to this atom */
982 j = index[iatoms[k]];
983 while (j < index[iatoms[k]+1])
986 nral_r = NRAL(ftype_r);
987 if (interaction_function[ftype_r].flags & IF_VSITE)
989 /* Add this vsite (recursion) */
990 add_vsite(ga2la, index, rtil, ftype_r, nral_r,
991 FALSE, -1, a_gl+iatoms[k]-iatoms[1], iatoms[k],
992 rtil+j, idef, vsite_pbc, vsite_pbc_nalloc);
1005 static void make_la2lc(gmx_domdec_t *dd)
1007 int *cgindex, *la2lc, cg, a;
1009 cgindex = dd->cgindex;
1011 if (dd->nat_tot > dd->la2lc_nalloc)
1013 dd->la2lc_nalloc = over_alloc_dd(dd->nat_tot);
1014 snew(dd->la2lc, dd->la2lc_nalloc);
1018 /* Make the local atom to local cg index */
1019 for (cg = 0; cg < dd->ncg_tot; cg++)
1021 for (a = cgindex[cg]; a < cgindex[cg+1]; a++)
1028 static real dd_dist2(t_pbc *pbc_null, rvec *cg_cm, const int *la2lc, int i, int j)
1034 pbc_dx_aiuc(pbc_null, cg_cm[la2lc[i]], cg_cm[la2lc[j]], dx);
1038 rvec_sub(cg_cm[la2lc[i]], cg_cm[la2lc[j]], dx);
1044 /* Append the nsrc t_blocka block structures in src to *dest */
1045 static void combine_blocka(t_blocka *dest, const t_blocka *src, int nsrc)
1049 ni = src[nsrc-1].nr;
1051 for (s = 0; s < nsrc; s++)
1055 if (ni + 1 > dest->nalloc_index)
1057 dest->nalloc_index = over_alloc_large(ni+1);
1058 srenew(dest->index, dest->nalloc_index);
1060 if (dest->nra + na > dest->nalloc_a)
1062 dest->nalloc_a = over_alloc_large(dest->nra+na);
1063 srenew(dest->a, dest->nalloc_a);
1065 for (s = 0; s < nsrc; s++)
1067 for (i = dest->nr+1; i < src[s].nr+1; i++)
1069 dest->index[i] = dest->nra + src[s].index[i];
1071 for (i = 0; i < src[s].nra; i++)
1073 dest->a[dest->nra+i] = src[s].a[i];
1075 dest->nr = src[s].nr;
1076 dest->nra += src[s].nra;
1080 /* Append the nsrc t_idef structures in src to *dest,
1081 * virtual sites need special attention, as pbc info differs per vsite.
1083 static void combine_idef(t_idef *dest, const t_idef *src, int nsrc,
1084 gmx_vsite_t *vsite, int ***vsite_pbc_t)
1090 int nral1 = 0, ftv = 0;
1092 for (ftype = 0; ftype < F_NRE; ftype++)
1095 for (s = 0; s < nsrc; s++)
1097 n += src[s].il[ftype].nr;
1101 ild = &dest->il[ftype];
1103 if (ild->nr + n > ild->nalloc)
1105 ild->nalloc = over_alloc_large(ild->nr+n);
1106 srenew(ild->iatoms, ild->nalloc);
1109 vpbc = ((interaction_function[ftype].flags & IF_VSITE) &&
1110 vsite->vsite_pbc_loc != NULL);
1113 nral1 = 1 + NRAL(ftype);
1114 ftv = ftype - F_VSITE2;
1115 if ((ild->nr + n)/nral1 > vsite->vsite_pbc_loc_nalloc[ftv])
1117 vsite->vsite_pbc_loc_nalloc[ftv] =
1118 over_alloc_large((ild->nr + n)/nral1);
1119 srenew(vsite->vsite_pbc_loc[ftv],
1120 vsite->vsite_pbc_loc_nalloc[ftv]);
1124 for (s = 0; s < nsrc; s++)
1126 ils = &src[s].il[ftype];
1127 for (i = 0; i < ils->nr; i++)
1129 ild->iatoms[ild->nr+i] = ils->iatoms[i];
1133 for (i = 0; i < ils->nr; i += nral1)
1135 vsite->vsite_pbc_loc[ftv][(ild->nr+i)/nral1] =
1136 vsite_pbc_t[s][ftv][i/nral1];
1145 /* Position restraints need an additional treatment */
1146 if (dest->il[F_POSRES].nr > 0)
1148 n = dest->il[F_POSRES].nr/2;
1149 if (n > dest->iparams_posres_nalloc)
1151 dest->iparams_posres_nalloc = over_alloc_large(n);
1152 srenew(dest->iparams_posres, dest->iparams_posres_nalloc);
1154 /* Set n to the number of original position restraints in dest */
1155 for (s = 0; s < nsrc; s++)
1157 n -= src[s].il[F_POSRES].nr/2;
1159 for (s = 0; s < nsrc; s++)
1161 for (i = 0; i < src[s].il[F_POSRES].nr/2; i++)
1163 /* Correct the index into iparams_posres */
1164 dest->il[F_POSRES].iatoms[n*2] = n;
1165 /* Copy the position restraint force parameters */
1166 dest->iparams_posres[n] = src[s].iparams_posres[i];
1173 /* This function looks up and assigns bonded interactions for zone iz.
1174 * With thread parallelizing each thread acts on a different atom range:
1175 * at_start to at_end.
1177 static int make_bondeds_zone(gmx_domdec_t *dd,
1178 const gmx_domdec_zones_t *zones,
1179 const gmx_molblock_t *molb,
1180 gmx_bool bRCheckMB, ivec rcheck, gmx_bool bRCheck2B,
1182 int *la2lc, t_pbc *pbc_null, rvec *cg_cm,
1183 const t_iparams *ip_in,
1186 int *vsite_pbc_nalloc,
1188 int at_start, int at_end)
1190 int i, i_gl, mb, mt, mol, i_mol, j, ftype, nral, d, k;
1192 t_iatom *iatoms, tiatoms[1+MAXATOMLIST];
1193 gmx_bool bBCheck, bUse, bLocal;
1194 ivec k_zero, k_plus;
1199 const gmx_domdec_ns_ranges_t *izone;
1200 gmx_reverse_top_t *rt;
1203 nizone = zones->nizone;
1204 izone = zones->izone;
1206 rt = dd->reverse_top;
1208 bBCheck = rt->bBCheck;
1214 for (i = at_start; i < at_end; i++)
1216 /* Get the global atom number */
1217 i_gl = dd->gatindex[i];
1218 global_atomnr_to_moltype_ind(rt, i_gl, &mb, &mt, &mol, &i_mol);
1219 /* Check all interactions assigned to this atom */
1220 index = rt->ril_mt[mt].index;
1221 rtil = rt->ril_mt[mt].il;
1223 while (j < index[i_mol+1])
1228 if (ftype == F_SETTLE)
1230 /* Settles are only in the reverse top when they
1231 * operate within a charge group. So we can assign
1232 * them without checks. We do this only for performance
1233 * reasons; it could be handled by the code below.
1237 /* Home zone: add this settle to the local topology */
1238 tiatoms[0] = iatoms[0];
1240 tiatoms[2] = i + iatoms[2] - iatoms[1];
1241 tiatoms[3] = i + iatoms[3] - iatoms[1];
1242 add_ifunc(nral, tiatoms, &idef->il[ftype]);
1247 else if (interaction_function[ftype].flags & IF_VSITE)
1249 /* The vsite construction goes where the vsite itself is */
1252 add_vsite(dd->ga2la, index, rtil, ftype, nral,
1253 TRUE, i, i_gl, i_mol,
1254 iatoms, idef, vsite_pbc, vsite_pbc_nalloc);
1261 tiatoms[0] = iatoms[0];
1265 /* Assign single-body interactions to the home zone */
1270 if (ftype == F_POSRES)
1272 add_posres(mol, i_mol, &molb[mb], tiatoms, ip_in,
1275 else if (ftype == F_FBPOSRES)
1277 add_fbposres(mol, i_mol, &molb[mb], tiatoms, ip_in,
1288 /* This is a two-body interaction, we can assign
1289 * analogous to the non-bonded assignments.
1291 if (!ga2la_get(ga2la, i_gl+iatoms[2]-i_mol, &a_loc, &kz))
1301 /* Check zone interaction assignments */
1302 bUse = ((iz < nizone && iz <= kz &&
1303 izone[iz].j0 <= kz && kz < izone[iz].j1) ||
1304 (kz < nizone &&iz > kz &&
1305 izone[kz].j0 <= iz && iz < izone[kz].j1));
1310 /* If necessary check the cgcm distance */
1312 dd_dist2(pbc_null, cg_cm, la2lc,
1313 tiatoms[1], tiatoms[2]) >= rc2)
1322 /* Assign this multi-body bonded interaction to
1323 * the local node if we have all the atoms involved
1324 * (local or communicated) and the minimum zone shift
1325 * in each dimension is zero, for dimensions
1326 * with 2 DD cells an extra check may be necessary.
1331 for (k = 1; k <= nral && bUse; k++)
1333 bLocal = ga2la_get(ga2la, i_gl+iatoms[k]-i_mol,
1335 if (!bLocal || kz >= zones->n)
1337 /* We do not have this atom of this interaction
1338 * locally, or it comes from more than one cell
1346 for (d = 0; d < DIM; d++)
1348 if (zones->shift[kz][d] == 0)
1360 k_zero[XX] && k_zero[YY] && k_zero[ZZ]);
1363 for (d = 0; (d < DIM && bUse); d++)
1365 /* Check if the cg_cm distance falls within
1366 * the cut-off to avoid possible multiple
1367 * assignments of bonded interactions.
1371 dd_dist2(pbc_null, cg_cm, la2lc,
1372 tiatoms[k_zero[d]], tiatoms[k_plus[d]]) >= rc2)
1381 /* Add this interaction to the local topology */
1382 add_ifunc(nral, tiatoms, &idef->il[ftype]);
1383 /* Sum so we can check in global_stat
1384 * if we have everything.
1387 !(interaction_function[ftype].flags & IF_LIMZERO))
1397 return nbonded_local;
1400 static void set_no_exclusions_zone(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
1401 int iz, t_blocka *lexcls)
1405 a0 = dd->cgindex[zones->cg_range[iz]];
1406 a1 = dd->cgindex[zones->cg_range[iz+1]];
1408 for (a = a0+1; a < a1+1; a++)
1410 lexcls->index[a] = lexcls->nra;
1414 static int make_exclusions_zone(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
1415 const gmx_moltype_t *moltype,
1416 gmx_bool bRCheck, real rc2,
1417 int *la2lc, t_pbc *pbc_null, rvec *cg_cm,
1421 int cg_start, int cg_end)
1423 int nizone, n, count, jla0, jla1, jla;
1424 int cg, la0, la1, la, a_gl, mb, mt, mol, a_mol, j, aj_mol;
1425 const t_blocka *excls;
1432 jla0 = dd->cgindex[zones->izone[iz].jcg0];
1433 jla1 = dd->cgindex[zones->izone[iz].jcg1];
1435 /* We set the end index, but note that we might not start at zero here */
1436 lexcls->nr = dd->cgindex[cg_end];
1440 for (cg = cg_start; cg < cg_end; cg++)
1442 /* Here we assume the number of exclusions in one charge group
1443 * is never larger than 1000.
1445 if (n+1000 > lexcls->nalloc_a)
1447 lexcls->nalloc_a = over_alloc_large(n+1000);
1448 srenew(lexcls->a, lexcls->nalloc_a);
1450 la0 = dd->cgindex[cg];
1451 la1 = dd->cgindex[cg+1];
1452 if (GET_CGINFO_EXCL_INTER(cginfo[cg]) ||
1453 !GET_CGINFO_EXCL_INTRA(cginfo[cg]))
1455 /* Copy the exclusions from the global top */
1456 for (la = la0; la < la1; la++)
1458 lexcls->index[la] = n;
1459 a_gl = dd->gatindex[la];
1460 global_atomnr_to_moltype_ind(dd->reverse_top, a_gl, &mb, &mt, &mol, &a_mol);
1461 excls = &moltype[mt].excls;
1462 for (j = excls->index[a_mol]; j < excls->index[a_mol+1]; j++)
1464 aj_mol = excls->a[j];
1465 /* This computation of jla is only correct intra-cg */
1466 jla = la + aj_mol - a_mol;
1467 if (jla >= la0 && jla < la1)
1469 /* This is an intra-cg exclusion. We can skip
1470 * the global indexing and distance checking.
1472 /* Intra-cg exclusions are only required
1473 * for the home zone.
1477 lexcls->a[n++] = jla;
1478 /* Check to avoid double counts */
1487 /* This is a inter-cg exclusion */
1488 /* Since exclusions are pair interactions,
1489 * just like non-bonded interactions,
1490 * they can be assigned properly up
1491 * to the DD cutoff (not cutoff_min as
1492 * for the other bonded interactions).
1494 if (ga2la_get(ga2la, a_gl+aj_mol-a_mol, &jla, &cell))
1496 if (iz == 0 && cell == 0)
1498 lexcls->a[n++] = jla;
1499 /* Check to avoid double counts */
1505 else if (jla >= jla0 && jla < jla1 &&
1507 dd_dist2(pbc_null, cg_cm, la2lc, la, jla) < rc2))
1509 /* jla > la, since jla0 > la */
1510 lexcls->a[n++] = jla;
1520 /* There are no inter-cg excls and this cg is self-excluded.
1521 * These exclusions are only required for zone 0,
1522 * since other zones do not see themselves.
1526 for (la = la0; la < la1; la++)
1528 lexcls->index[la] = n;
1529 for (j = la0; j < la1; j++)
1534 count += ((la1 - la0)*(la1 - la0 - 1))/2;
1538 /* We don't need exclusions for this cg */
1539 for (la = la0; la < la1; la++)
1541 lexcls->index[la] = n;
1547 lexcls->index[lexcls->nr] = n;
1553 static void check_alloc_index(t_blocka *ba, int nindex_max)
1555 if (nindex_max+1 > ba->nalloc_index)
1557 ba->nalloc_index = over_alloc_dd(nindex_max+1);
1558 srenew(ba->index, ba->nalloc_index);
1562 static void check_exclusions_alloc(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
1568 nr = dd->cgindex[zones->izone[zones->nizone-1].cg1];
1570 check_alloc_index(lexcls, nr);
1572 for (thread = 1; thread < dd->reverse_top->nthread; thread++)
1574 check_alloc_index(&dd->reverse_top->excl_thread[thread], nr);
1578 static void finish_local_exclusions(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
1583 lexcls->nr = dd->cgindex[zones->izone[zones->nizone-1].cg1];
1585 if (dd->n_intercg_excl == 0)
1587 /* There are no exclusions involving non-home charge groups,
1588 * but we need to set the indices for neighborsearching.
1590 la0 = dd->cgindex[zones->izone[0].cg1];
1591 for (la = la0; la < lexcls->nr; la++)
1593 lexcls->index[la] = lexcls->nra;
1596 /* nr is only used to loop over the exclusions for Ewald and RF,
1597 * so we can set it to the number of home atoms for efficiency.
1599 lexcls->nr = dd->cgindex[zones->izone[0].cg1];
1603 static void clear_idef(t_idef *idef)
1607 /* Clear the counts */
1608 for (ftype = 0; ftype < F_NRE; ftype++)
1610 idef->il[ftype].nr = 0;
1614 static int make_local_bondeds_excls(gmx_domdec_t *dd,
1615 gmx_domdec_zones_t *zones,
1616 const gmx_mtop_t *mtop,
1618 gmx_bool bRCheckMB, ivec rcheck, gmx_bool bRCheck2B,
1620 int *la2lc, t_pbc *pbc_null, rvec *cg_cm,
1621 t_idef *idef, gmx_vsite_t *vsite,
1622 t_blocka *lexcls, int *excl_count)
1624 int nzone_bondeds, nzone_excl;
1629 gmx_reverse_top_t *rt;
1631 if (dd->reverse_top->bMultiCGmols)
1633 nzone_bondeds = zones->n;
1637 /* Only single charge group molecules, so interactions don't
1638 * cross zone boundaries and we only need to assign in the home zone.
1643 if (dd->n_intercg_excl > 0)
1645 /* We only use exclusions from i-zones to i- and j-zones */
1646 nzone_excl = zones->nizone;
1650 /* There are no inter-cg exclusions and only zone 0 sees itself */
1654 check_exclusions_alloc(dd, zones, lexcls);
1656 rt = dd->reverse_top;
1660 /* Clear the counts */
1668 for (iz = 0; iz < nzone_bondeds; iz++)
1670 cg0 = zones->cg_range[iz];
1671 cg1 = zones->cg_range[iz+1];
1673 #pragma omp parallel for num_threads(rt->nthread) schedule(static)
1674 for (thread = 0; thread < rt->nthread; thread++)
1680 int *vsite_pbc_nalloc;
1683 cg0t = cg0 + ((cg1 - cg0)* thread )/rt->nthread;
1684 cg1t = cg0 + ((cg1 - cg0)*(thread+1))/rt->nthread;
1692 idef_t = &rt->idef_thread[thread];
1696 if (vsite && vsite->bHaveChargeGroups && vsite->n_intercg_vsite > 0)
1700 vsite_pbc = vsite->vsite_pbc_loc;
1701 vsite_pbc_nalloc = vsite->vsite_pbc_loc_nalloc;
1705 vsite_pbc = rt->vsite_pbc[thread];
1706 vsite_pbc_nalloc = rt->vsite_pbc_nalloc[thread];
1712 vsite_pbc_nalloc = NULL;
1715 rt->nbonded_thread[thread] =
1716 make_bondeds_zone(dd, zones,
1718 bRCheckMB, rcheck, bRCheck2B, rc2,
1719 la2lc, pbc_null, cg_cm, idef->iparams,
1721 vsite_pbc, vsite_pbc_nalloc,
1723 dd->cgindex[cg0t], dd->cgindex[cg1t]);
1725 if (iz < nzone_excl)
1733 excl_t = &rt->excl_thread[thread];
1738 rt->excl_count_thread[thread] =
1739 make_exclusions_zone(dd, zones,
1740 mtop->moltype, bRCheck2B, rc2,
1741 la2lc, pbc_null, cg_cm, cginfo,
1748 if (rt->nthread > 1)
1750 combine_idef(idef, rt->idef_thread+1, rt->nthread-1,
1751 vsite, rt->vsite_pbc+1);
1754 for (thread = 0; thread < rt->nthread; thread++)
1756 nbonded_local += rt->nbonded_thread[thread];
1759 if (iz < nzone_excl)
1761 if (rt->nthread > 1)
1763 combine_blocka(lexcls, rt->excl_thread+1, rt->nthread-1);
1766 for (thread = 0; thread < rt->nthread; thread++)
1768 *excl_count += rt->excl_count_thread[thread];
1773 /* Some zones might not have exclusions, but some code still needs to
1774 * loop over the index, so we set the indices here.
1776 for (iz = nzone_excl; iz < zones->nizone; iz++)
1778 set_no_exclusions_zone(dd, zones, iz, lexcls);
1781 finish_local_exclusions(dd, zones, lexcls);
1784 fprintf(debug, "We have %d exclusions, check count %d\n",
1785 lexcls->nra, *excl_count);
1788 return nbonded_local;
1791 void dd_make_local_cgs(gmx_domdec_t *dd, t_block *lcgs)
1793 lcgs->nr = dd->ncg_tot;
1794 lcgs->index = dd->cgindex;
1797 void dd_make_local_top(gmx_domdec_t *dd, gmx_domdec_zones_t *zones,
1798 int npbcdim, matrix box,
1799 rvec cellsize_min, ivec npulse,
1803 gmx_mtop_t *mtop, gmx_localtop_t *ltop)
1805 gmx_bool bUniqueExcl, bRCheckMB, bRCheck2B, bRCheckExcl;
1809 t_pbc pbc, *pbc_null = NULL;
1813 fprintf(debug, "Making local topology\n");
1816 dd_make_local_cgs(dd, <op->cgs);
1820 bRCheckExcl = FALSE;
1822 if (dd->reverse_top->bMultiCGmols)
1824 /* We need to check to which cell bondeds should be assigned */
1825 rc = dd_cutoff_twobody(dd);
1828 fprintf(debug, "Two-body bonded cut-off distance is %g\n", rc);
1831 /* Should we check cg_cm distances when assigning bonded interactions? */
1832 for (d = 0; d < DIM; d++)
1835 /* Only need to check for dimensions where the part of the box
1836 * that is not communicated is smaller than the cut-off.
1838 if (d < npbcdim && dd->nc[d] > 1 &&
1839 (dd->nc[d] - npulse[d])*cellsize_min[d] < 2*rc)
1846 /* Check for interactions between two atoms,
1847 * where we can allow interactions up to the cut-off,
1848 * instead of up to the smallest cell dimension.
1855 "dim %d cellmin %f bonded rcheck[%d] = %d, bRCheck2B = %d\n",
1856 d, cellsize_min[d], d, rcheck[d], bRCheck2B);
1859 if (dd->reverse_top->bExclRequired)
1861 bRCheckExcl = bRCheck2B;
1865 /* If we don't have forces on exclusions,
1866 * we don't care about exclusions being assigned mulitple times.
1868 bRCheckExcl = FALSE;
1870 if (bRCheckMB || bRCheck2B)
1875 set_pbc_dd(&pbc, fr->ePBC, dd, TRUE, box);
1886 make_local_bondeds_excls(dd, zones, mtop, fr->cginfo,
1887 bRCheckMB, rcheck, bRCheck2B, rc,
1889 pbc_null, cgcm_or_x,
1891 <op->excls, &nexcl);
1893 /* The ilist is not sorted yet,
1894 * we can only do this when we have the charge arrays.
1896 ltop->idef.ilsort = ilsortUNKNOWN;
1898 if (dd->reverse_top->bExclRequired)
1900 dd->nbonded_local += nexcl;
1902 forcerec_set_excl_load(fr, ltop);
1905 ltop->atomtypes = mtop->atomtypes;
1907 /* For an error message only */
1908 dd->reverse_top->err_top_global = mtop;
1909 dd->reverse_top->err_top_local = ltop;
1912 void dd_sort_local_top(gmx_domdec_t *dd, t_mdatoms *mdatoms,
1913 gmx_localtop_t *ltop)
1915 if (dd->reverse_top->ilsort == ilsortNO_FE)
1917 ltop->idef.ilsort = ilsortNO_FE;
1921 gmx_sort_ilist_fe(<op->idef, mdatoms->chargeA, mdatoms->chargeB);
1925 gmx_localtop_t *dd_init_local_top(gmx_mtop_t *top_global)
1927 gmx_localtop_t *top;
1932 top->idef.ntypes = top_global->ffparams.ntypes;
1933 top->idef.atnr = top_global->ffparams.atnr;
1934 top->idef.functype = top_global->ffparams.functype;
1935 top->idef.iparams = top_global->ffparams.iparams;
1936 top->idef.fudgeQQ = top_global->ffparams.fudgeQQ;
1937 top->idef.cmap_grid = top_global->ffparams.cmap_grid;
1939 for (i = 0; i < F_NRE; i++)
1941 top->idef.il[i].iatoms = NULL;
1942 top->idef.il[i].nalloc = 0;
1944 top->idef.ilsort = ilsortUNKNOWN;
1949 void dd_init_local_state(gmx_domdec_t *dd,
1950 t_state *state_global, t_state *state_local)
1952 int buf[NITEM_DD_INIT_LOCAL_STATE];
1956 buf[0] = state_global->flags;
1957 buf[1] = state_global->ngtc;
1958 buf[2] = state_global->nnhpres;
1959 buf[3] = state_global->nhchainlength;
1960 buf[4] = state_global->dfhist.nlambda;
1962 dd_bcast(dd, NITEM_DD_INIT_LOCAL_STATE*sizeof(int), buf);
1964 init_state(state_local, 0, buf[1], buf[2], buf[3], buf[4]);
1965 state_local->flags = buf[0];
1968 static void check_link(t_blocka *link, int cg_gl, int cg_gl_j)
1974 for (k = link->index[cg_gl]; k < link->index[cg_gl+1]; k++)
1976 if (link->a[k] == cg_gl_j)
1983 /* Add this charge group link */
1984 if (link->index[cg_gl+1]+1 > link->nalloc_a)
1986 link->nalloc_a = over_alloc_large(link->index[cg_gl+1]+1);
1987 srenew(link->a, link->nalloc_a);
1989 link->a[link->index[cg_gl+1]] = cg_gl_j;
1990 link->index[cg_gl+1]++;
1994 static int *make_at2cg(t_block *cgs)
1998 snew(at2cg, cgs->index[cgs->nr]);
1999 for (cg = 0; cg < cgs->nr; cg++)
2001 for (a = cgs->index[cg]; a < cgs->index[cg+1]; a++)
2010 t_blocka *make_charge_group_links(gmx_mtop_t *mtop, gmx_domdec_t *dd,
2011 cginfo_mb_t *cginfo_mb)
2013 gmx_reverse_top_t *rt;
2014 int mb, cg_offset, cg, cg_gl, a, aj, i, j, ftype, nral, nlink_mol, mol, ncgi;
2015 gmx_molblock_t *molb;
2016 gmx_moltype_t *molt;
2020 gmx_reverse_ilist_t ril;
2022 cginfo_mb_t *cgi_mb;
2024 /* For each charge group make a list of other charge groups
2025 * in the system that a linked to it via bonded interactions
2026 * which are also stored in reverse_top.
2029 rt = dd->reverse_top;
2032 snew(link->index, ncg_mtop(mtop)+1);
2039 for (mb = 0; mb < mtop->nmolblock; mb++)
2041 molb = &mtop->molblock[mb];
2042 if (molb->nmol == 0)
2046 molt = &mtop->moltype[molb->type];
2048 excls = &molt->excls;
2049 a2c = make_at2cg(cgs);
2050 /* Make a reverse ilist in which the interactions are linked
2051 * to all atoms, not only the first atom as in gmx_reverse_top.
2052 * The constraints are discarded here.
2054 make_reverse_ilist(molt, NULL, FALSE, FALSE, FALSE, TRUE, &ril);
2056 cgi_mb = &cginfo_mb[mb];
2058 for (cg = 0; cg < cgs->nr; cg++)
2060 cg_gl = cg_offset + cg;
2061 link->index[cg_gl+1] = link->index[cg_gl];
2062 for (a = cgs->index[cg]; a < cgs->index[cg+1]; a++)
2065 while (i < ril.index[a+1])
2067 ftype = ril.il[i++];
2069 /* Skip the ifunc index */
2071 for (j = 0; j < nral; j++)
2076 check_link(link, cg_gl, cg_offset+a2c[aj]);
2079 i += nral_rt(ftype);
2081 if (rt->bExclRequired)
2083 /* Exclusions always go both ways */
2084 for (j = excls->index[a]; j < excls->index[a+1]; j++)
2089 check_link(link, cg_gl, cg_offset+a2c[aj]);
2094 if (link->index[cg_gl+1] - link->index[cg_gl] > 0)
2096 SET_CGINFO_BOND_INTER(cgi_mb->cginfo[cg]);
2100 nlink_mol = link->index[cg_offset+cgs->nr] - link->index[cg_offset];
2102 cg_offset += cgs->nr;
2104 destroy_reverse_ilist(&ril);
2109 fprintf(debug, "molecule type '%s' %d cgs has %d cg links through bonded interac.\n", *molt->name, cgs->nr, nlink_mol);
2114 /* Copy the data for the rest of the molecules in this block */
2115 link->nalloc_a += (molb->nmol - 1)*nlink_mol;
2116 srenew(link->a, link->nalloc_a);
2117 for (mol = 1; mol < molb->nmol; mol++)
2119 for (cg = 0; cg < cgs->nr; cg++)
2121 cg_gl = cg_offset + cg;
2122 link->index[cg_gl+1] =
2123 link->index[cg_gl+1-cgs->nr] + nlink_mol;
2124 for (j = link->index[cg_gl]; j < link->index[cg_gl+1]; j++)
2126 link->a[j] = link->a[j-nlink_mol] + cgs->nr;
2128 if (link->index[cg_gl+1] - link->index[cg_gl] > 0 &&
2129 cg_gl - cgi_mb->cg_start < cgi_mb->cg_mod)
2131 SET_CGINFO_BOND_INTER(cgi_mb->cginfo[cg_gl - cgi_mb->cg_start]);
2135 cg_offset += cgs->nr;
2142 fprintf(debug, "Of the %d charge groups %d are linked via bonded interactions\n", ncg_mtop(mtop), ncgi);
2148 static void bonded_cg_distance_mol(gmx_moltype_t *molt, int *at2cg,
2149 gmx_bool bBCheck, gmx_bool bExcl, rvec *cg_cm,
2150 real *r_2b, int *ft2b, int *a2_1, int *a2_2,
2151 real *r_mb, int *ftmb, int *am_1, int *am_2)
2153 int ftype, nral, i, j, ai, aj, cgi, cgj;
2156 real r2_2b, r2_mb, rij2;
2160 for (ftype = 0; ftype < F_NRE; ftype++)
2162 if (dd_check_ftype(ftype, bBCheck, FALSE, FALSE))
2164 il = &molt->ilist[ftype];
2168 for (i = 0; i < il->nr; i += 1+nral)
2170 for (ai = 0; ai < nral; ai++)
2172 cgi = at2cg[il->iatoms[i+1+ai]];
2173 for (aj = 0; aj < nral; aj++)
2175 cgj = at2cg[il->iatoms[i+1+aj]];
2178 rij2 = distance2(cg_cm[cgi], cg_cm[cgj]);
2179 if (nral == 2 && rij2 > r2_2b)
2183 *a2_1 = il->iatoms[i+1+ai];
2184 *a2_2 = il->iatoms[i+1+aj];
2186 if (nral > 2 && rij2 > r2_mb)
2190 *am_1 = il->iatoms[i+1+ai];
2191 *am_2 = il->iatoms[i+1+aj];
2202 excls = &molt->excls;
2203 for (ai = 0; ai < excls->nr; ai++)
2206 for (j = excls->index[ai]; j < excls->index[ai+1]; j++)
2208 cgj = at2cg[excls->a[j]];
2211 rij2 = distance2(cg_cm[cgi], cg_cm[cgj]);
2221 *r_2b = sqrt(r2_2b);
2222 *r_mb = sqrt(r2_mb);
2225 static void get_cgcm_mol(gmx_moltype_t *molt, gmx_ffparams_t *ffparams,
2226 int ePBC, t_graph *graph, matrix box,
2228 rvec *x, rvec *xs, rvec *cg_cm)
2232 if (ePBC != epbcNONE)
2234 mk_mshift(NULL, graph, ePBC, box, x);
2236 shift_x(graph, box, x, xs);
2237 /* By doing an extra mk_mshift the molecules that are broken
2238 * because they were e.g. imported from another software
2239 * will be made whole again. Such are the healing powers
2242 mk_mshift(NULL, graph, ePBC, box, xs);
2246 /* We copy the coordinates so the original coordinates remain
2247 * unchanged, just to be 100% sure that we do not affect
2248 * binary reproducibility of simulations.
2250 n = molt->cgs.index[molt->cgs.nr];
2251 for (i = 0; i < n; i++)
2253 copy_rvec(x[i], xs[i]);
2259 construct_vsites(vsite, xs, 0.0, NULL,
2260 ffparams->iparams, molt->ilist,
2261 epbcNONE, TRUE, NULL, NULL);
2264 calc_cgcm(NULL, 0, molt->cgs.nr, &molt->cgs, xs, cg_cm);
2267 static int have_vsite_molt(gmx_moltype_t *molt)
2273 for (i = 0; i < F_NRE; i++)
2275 if ((interaction_function[i].flags & IF_VSITE) &&
2276 molt->ilist[i].nr > 0)
2285 void dd_bonded_cg_distance(FILE *fplog,
2287 t_inputrec *ir, rvec *x, matrix box,
2289 real *r_2b, real *r_mb)
2291 gmx_bool bExclRequired;
2292 int mb, cg_offset, at_offset, *at2cg, mol;
2295 gmx_molblock_t *molb;
2296 gmx_moltype_t *molt;
2298 real rmol_2b, rmol_mb;
2299 int ft2b = -1, a_2b_1 = -1, a_2b_2 = -1, ftmb = -1, a_mb_1 = -1, a_mb_2 = -1;
2300 int ftm2b = -1, amol_2b_1 = -1, amol_2b_2 = -1, ftmmb = -1, amol_mb_1 = -1, amol_mb_2 = -1;
2302 bExclRequired = IR_EXCL_FORCES(*ir);
2304 vsite = init_vsite(mtop, NULL, TRUE);
2310 for (mb = 0; mb < mtop->nmolblock; mb++)
2312 molb = &mtop->molblock[mb];
2313 molt = &mtop->moltype[molb->type];
2314 if (molt->cgs.nr == 1 || molb->nmol == 0)
2316 cg_offset += molb->nmol*molt->cgs.nr;
2317 at_offset += molb->nmol*molt->atoms.nr;
2321 if (ir->ePBC != epbcNONE)
2323 mk_graph_ilist(NULL, molt->ilist, 0, molt->atoms.nr, FALSE, FALSE,
2327 at2cg = make_at2cg(&molt->cgs);
2328 snew(xs, molt->atoms.nr);
2329 snew(cg_cm, molt->cgs.nr);
2330 for (mol = 0; mol < molb->nmol; mol++)
2332 get_cgcm_mol(molt, &mtop->ffparams, ir->ePBC, &graph, box,
2333 have_vsite_molt(molt) ? vsite : NULL,
2334 x+at_offset, xs, cg_cm);
2336 bonded_cg_distance_mol(molt, at2cg, bBCheck, bExclRequired, cg_cm,
2337 &rmol_2b, &ftm2b, &amol_2b_1, &amol_2b_2,
2338 &rmol_mb, &ftmmb, &amol_mb_1, &amol_mb_2);
2339 if (rmol_2b > *r_2b)
2343 a_2b_1 = at_offset + amol_2b_1;
2344 a_2b_2 = at_offset + amol_2b_2;
2346 if (rmol_mb > *r_mb)
2350 a_mb_1 = at_offset + amol_mb_1;
2351 a_mb_2 = at_offset + amol_mb_2;
2354 cg_offset += molt->cgs.nr;
2355 at_offset += molt->atoms.nr;
2360 if (ir->ePBC != epbcNONE)
2367 /* We should have a vsite free routine, but here we can simply free */
2370 if (fplog && (ft2b >= 0 || ftmb >= 0))
2373 "Initial maximum inter charge-group distances:\n");
2377 " two-body bonded interactions: %5.3f nm, %s, atoms %d %d\n",
2378 *r_2b, interaction_function[ft2b].longname,
2379 a_2b_1+1, a_2b_2+1);
2384 " multi-body bonded interactions: %5.3f nm, %s, atoms %d %d\n",
2385 *r_mb, interaction_function[ftmb].longname,
2386 a_mb_1+1, a_mb_2+1);