2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
48 #include "gromacs/domdec/domdec.h"
49 #include "gromacs/domdec/domdec_struct.h"
50 #include "gromacs/essentialdynamics/edsam.h"
51 #include "gromacs/fileio/confio.h"
52 #include "gromacs/fileio/gmxfio.h"
53 #include "gromacs/fileio/pdbio.h"
54 #include "gromacs/gmxlib/nrnb.h"
55 #include "gromacs/math/utilities.h"
56 #include "gromacs/math/vec.h"
57 #include "gromacs/mdlib/gmx_omp_nthreads.h"
58 #include "gromacs/mdlib/mdrun.h"
59 #include "gromacs/mdlib/splitter.h"
60 #include "gromacs/mdtypes/commrec.h"
61 #include "gromacs/mdtypes/inputrec.h"
62 #include "gromacs/mdtypes/md_enums.h"
63 #include "gromacs/mdtypes/state.h"
64 #include "gromacs/pbcutil/pbc.h"
65 #include "gromacs/pulling/pull.h"
66 #include "gromacs/topology/block.h"
67 #include "gromacs/topology/invblock.h"
68 #include "gromacs/topology/mtop_lookup.h"
69 #include "gromacs/topology/mtop_util.h"
70 #include "gromacs/utility/exceptions.h"
71 #include "gromacs/utility/fatalerror.h"
72 #include "gromacs/utility/pleasecite.h"
73 #include "gromacs/utility/smalloc.h"
74 #include "gromacs/utility/txtdump.h"
76 typedef struct gmx_constr {
77 int ncon_tot; /* The total number of constraints */
78 int nflexcon; /* The number of flexible constraints */
79 int n_at2con_mt; /* The size of at2con = #moltypes */
80 t_blocka *at2con_mt; /* A list of atoms to constraints */
81 int n_at2settle_mt; /* The size of at2settle = #moltypes */
82 int **at2settle_mt; /* A list of atoms to settles */
83 gmx_bool bInterCGsettles;
84 gmx_lincsdata_t lincsd; /* LINCS data */
85 gmx_shakedata_t shaked; /* SHAKE data */
86 gmx_settledata_t settled; /* SETTLE data */
87 int nblocks; /* The number of SHAKE blocks */
88 int *sblock; /* The SHAKE blocks */
89 int sblock_nalloc; /* The allocation size of sblock */
90 real *lagr; /* -2 times the Lagrange multipliers for SHAKE */
91 int lagr_nalloc; /* The allocation size of lagr */
92 int maxwarn; /* The maximum number of warnings */
95 gmx_edsam_t ed; /* The essential dynamics data */
97 /* Thread local working data */
98 tensor *vir_r_m_dr_th; /* Thread virial contribution */
99 bool *bSettleErrorHasOccurred; /* Did a settle error occur? */
101 /* Only used for printing warnings */
102 const gmx_mtop_t *warn_mtop; /* Pointer to the global topology */
110 static int pcomp(const void *p1, const void *p2)
113 int min1, min2, max1, max2;
114 t_sortblock *a1 = (t_sortblock *)p1;
115 t_sortblock *a2 = (t_sortblock *)p2;
117 db = a1->blocknr-a2->blocknr;
124 min1 = std::min(a1->iatom[1], a1->iatom[2]);
125 max1 = std::max(a1->iatom[1], a1->iatom[2]);
126 min2 = std::min(a2->iatom[1], a2->iatom[2]);
127 max2 = std::max(a2->iatom[1], a2->iatom[2]);
139 int n_flexible_constraints(struct gmx_constr *constr)
145 nflexcon = constr->nflexcon;
155 static void clear_constraint_quantity_nonlocal(gmx_domdec_t *dd, rvec *q)
157 int nonlocal_at_start, nonlocal_at_end, at;
159 dd_get_constraint_range(dd, &nonlocal_at_start, &nonlocal_at_end);
161 for (at = nonlocal_at_start; at < nonlocal_at_end; at++)
167 void too_many_constraint_warnings(int eConstrAlg, int warncount)
170 "Too many %s warnings (%d)\n"
171 "If you know what you are doing you can %s"
172 "set the environment variable GMX_MAXCONSTRWARN to -1,\n"
173 "but normally it is better to fix the problem",
174 (eConstrAlg == econtLINCS) ? "LINCS" : "SETTLE", warncount,
175 (eConstrAlg == econtLINCS) ?
176 "adjust the lincs warning threshold in your mdp file\nor " : "\n");
179 static void write_constr_pdb(const char *fn, const char *title,
180 const gmx_mtop_t *mtop,
181 int start, int homenr, t_commrec *cr,
182 rvec x[], matrix box)
186 int dd_ac0 = 0, dd_ac1 = 0, i, ii, resnr;
188 const char *anm, *resnm;
191 if (DOMAINDECOMP(cr))
194 dd_get_constraint_range(dd, &dd_ac0, &dd_ac1);
201 sprintf(fname, "%s_n%d.pdb", fn, cr->sim_nodeid);
205 sprintf(fname, "%s.pdb", fn);
208 out = gmx_fio_fopen(fname, "w");
210 fprintf(out, "TITLE %s\n", title);
211 gmx_write_pdb_box(out, -1, box);
213 for (i = start; i < start+homenr; i++)
217 if (i >= dd->nat_home && i < dd_ac0)
221 ii = dd->gatindex[i];
227 mtopGetAtomAndResidueName(mtop, ii, &molb, &anm, &resnr, &resnm, nullptr);
228 gmx_fprintf_pdb_atomline(out, epdbATOM, ii+1, anm, ' ', resnm, ' ', resnr, ' ',
229 10*x[i][XX], 10*x[i][YY], 10*x[i][ZZ], 1.0, 0.0, "");
231 fprintf(out, "TER\n");
236 static void dump_confs(FILE *fplog, gmx_int64_t step, const gmx_mtop_t *mtop,
237 int start, int homenr, t_commrec *cr,
238 rvec x[], rvec xprime[], matrix box)
240 char buf[STRLEN], buf2[22];
242 char *env = getenv("GMX_SUPPRESS_DUMP");
248 sprintf(buf, "step%sb", gmx_step_str(step, buf2));
249 write_constr_pdb(buf, "initial coordinates",
250 mtop, start, homenr, cr, x, box);
251 sprintf(buf, "step%sc", gmx_step_str(step, buf2));
252 write_constr_pdb(buf, "coordinates after constraining",
253 mtop, start, homenr, cr, xprime, box);
256 fprintf(fplog, "Wrote pdb files with previous and current coordinates\n");
258 fprintf(stderr, "Wrote pdb files with previous and current coordinates\n");
261 static void pr_sortblock(FILE *fp, const char *title, int nsb, t_sortblock sb[])
265 fprintf(fp, "%s\n", title);
266 for (i = 0; (i < nsb); i++)
268 fprintf(fp, "i: %5d, iatom: (%5d %5d %5d), blocknr: %5d\n",
269 i, sb[i].iatom[0], sb[i].iatom[1], sb[i].iatom[2],
274 gmx_bool constrain(FILE *fplog, gmx_bool bLog, gmx_bool bEner,
275 struct gmx_constr *constr,
276 t_idef *idef, t_inputrec *ir,
278 gmx_int64_t step, int delta_step,
281 rvec *x, rvec *xprime, rvec *min_proj,
282 gmx_bool bMolPBC, matrix box,
283 real lambda, real *dvdlambda,
284 rvec *v, tensor *vir,
285 t_nrnb *nrnb, int econq)
291 real invdt, vir_fac = 0, t;
294 t_pbc pbc, *pbc_null;
298 if (econq == econqForceDispl && !EI_ENERGY_MINIMIZATION(ir->eI))
300 gmx_incons("constrain called for forces displacements while not doing energy minimization, can not do this while the LINCS and SETTLE constraint connection matrices are mass weighted");
309 scaled_delta_t = step_scaling * ir->delta_t;
311 /* Prepare time step for use in constraint implementations, and
312 avoid generating inf when ir->delta_t = 0. */
313 if (ir->delta_t == 0)
319 invdt = 1.0/scaled_delta_t;
322 if (ir->efep != efepNO && EI_DYNAMICS(ir->eI))
324 /* Set the constraint lengths for the step at which this configuration
325 * is meant to be. The invmasses should not be changed.
327 lambda += delta_step*ir->fepvals->delta_lambda;
332 clear_mat(vir_r_m_dr);
337 settle = &idef->il[F_SETTLE];
338 nsettle = settle->nr/(1+NRAL(F_SETTLE));
342 nth = gmx_omp_nthreads_get(emntSETTLE);
349 /* We do not need full pbc when constraints do not cross charge groups,
350 * i.e. when dd->constraint_comm==NULL.
351 * Note that PBC for constraints is different from PBC for bondeds.
352 * For constraints there is both forward and backward communication.
354 if (ir->ePBC != epbcNONE &&
355 (cr->dd || bMolPBC) && !(cr->dd && cr->dd->constraint_comm == nullptr))
357 /* With pbc=screw the screw has been changed to a shift
358 * by the constraint coordinate communication routine,
359 * so that here we can use normal pbc.
361 pbc_null = set_pbc_dd(&pbc, ir->ePBC,
362 DOMAINDECOMP(cr) ? cr->dd->nc : nullptr,
370 /* Communicate the coordinates required for the non-local constraints
371 * for LINCS and/or SETTLE.
375 dd_move_x_constraints(cr->dd, box, x, xprime, econq == econqCoord);
379 /* We need to initialize the non-local components of v.
380 * We never actually use these values, but we do increment them,
381 * so we should avoid uninitialized variables and overflows.
383 clear_constraint_quantity_nonlocal(cr->dd, v);
387 if (constr->lincsd != nullptr)
389 bOK = constrain_lincs(fplog, bLog, bEner, ir, step, constr->lincsd, md, cr,
391 box, pbc_null, lambda, dvdlambda,
392 invdt, v, vir != nullptr, vir_r_m_dr,
394 constr->maxwarn, &constr->warncount_lincs);
395 if (!bOK && constr->maxwarn < INT_MAX)
397 if (fplog != nullptr)
399 fprintf(fplog, "Constraint error in algorithm %s at step %s\n",
400 econstr_names[econtLINCS], gmx_step_str(step, buf));
406 if (constr->nblocks > 0)
411 bOK = bshakef(fplog, constr->shaked,
412 md->invmass, constr->nblocks, constr->sblock,
413 idef, ir, x, xprime, nrnb,
414 constr->lagr, lambda, dvdlambda,
415 invdt, v, vir != nullptr, vir_r_m_dr,
416 constr->maxwarn < INT_MAX, econq);
419 bOK = bshakef(fplog, constr->shaked,
420 md->invmass, constr->nblocks, constr->sblock,
421 idef, ir, x, min_proj, nrnb,
422 constr->lagr, lambda, dvdlambda,
423 invdt, nullptr, vir != nullptr, vir_r_m_dr,
424 constr->maxwarn < INT_MAX, econq);
427 gmx_fatal(FARGS, "Internal error, SHAKE called for constraining something else than coordinates");
431 if (!bOK && constr->maxwarn < INT_MAX)
433 if (fplog != nullptr)
435 fprintf(fplog, "Constraint error in algorithm %s at step %s\n",
436 econstr_names[econtSHAKE], gmx_step_str(step, buf));
444 bool bSettleErrorHasOccurred = false;
449 #pragma omp parallel for num_threads(nth) schedule(static)
450 for (th = 0; th < nth; th++)
456 clear_mat(constr->vir_r_m_dr_th[th]);
459 csettle(constr->settled,
463 invdt, v ? v[0] : nullptr,
465 th == 0 ? vir_r_m_dr : constr->vir_r_m_dr_th[th],
466 th == 0 ? &bSettleErrorHasOccurred : &constr->bSettleErrorHasOccurred[th]);
468 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
470 inc_nrnb(nrnb, eNR_SETTLE, nsettle);
473 inc_nrnb(nrnb, eNR_CONSTR_V, nsettle*3);
477 inc_nrnb(nrnb, eNR_CONSTR_VIR, nsettle*3);
483 case econqForceDispl:
484 #pragma omp parallel for num_threads(nth) schedule(static)
485 for (th = 0; th < nth; th++)
489 int calcvir_atom_end;
493 calcvir_atom_end = 0;
497 calcvir_atom_end = md->homenr;
502 clear_mat(constr->vir_r_m_dr_th[th]);
505 int start_th = (nsettle* th )/nth;
506 int end_th = (nsettle*(th+1))/nth;
508 if (start_th >= 0 && end_th - start_th > 0)
510 settle_proj(constr->settled, econq,
512 settle->iatoms+start_th*(1+NRAL(F_SETTLE)),
515 xprime, min_proj, calcvir_atom_end,
516 th == 0 ? vir_r_m_dr : constr->vir_r_m_dr_th[th]);
519 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
521 /* This is an overestimate */
522 inc_nrnb(nrnb, eNR_SETTLE, nsettle);
524 case econqDeriv_FlexCon:
525 /* Nothing to do, since the are no flexible constraints in settles */
528 gmx_incons("Unknown constraint quantity for settle");
533 /* Reduce the virial contributions over the threads */
534 for (int th = 1; th < nth; th++)
536 m_add(vir_r_m_dr, constr->vir_r_m_dr_th[th], vir_r_m_dr);
540 if (econq == econqCoord)
542 for (int th = 1; th < nth; th++)
544 bSettleErrorHasOccurred = bSettleErrorHasOccurred || constr->bSettleErrorHasOccurred[th];
547 if (bSettleErrorHasOccurred)
551 "\nstep " "%" GMX_PRId64 ": One or more water molecules can not be settled.\n"
552 "Check for bad contacts and/or reduce the timestep if appropriate.\n",
556 fprintf(fplog, "%s", buf);
558 fprintf(stderr, "%s", buf);
559 constr->warncount_settle++;
560 if (constr->warncount_settle > constr->maxwarn)
562 too_many_constraint_warnings(-1, constr->warncount_settle);
573 /* The normal uses of constrain() pass step_scaling = 1.0.
574 * The call to constrain() for SD1 that passes step_scaling =
575 * 0.5 also passes vir = NULL, so cannot reach this
576 * assertion. This assertion should remain until someone knows
577 * that this path works for their intended purpose, and then
578 * they can use scaled_delta_t instead of ir->delta_t
580 assert(gmx_within_tol(step_scaling, 1.0, GMX_REAL_EPS));
584 vir_fac = 0.5/(ir->delta_t*ir->delta_t);
587 vir_fac = 0.5/ir->delta_t;
590 case econqForceDispl:
594 gmx_incons("Unsupported constraint quantity for virial");
599 vir_fac *= 2; /* only constraining over half the distance here */
601 for (int i = 0; i < DIM; i++)
603 for (int j = 0; j < DIM; j++)
605 (*vir)[i][j] = vir_fac*vir_r_m_dr[i][j];
612 dump_confs(fplog, step, constr->warn_mtop, start, homenr, cr, x, xprime, box);
615 if (econq == econqCoord)
617 if (ir->bPull && pull_have_constraint(ir->pull_work))
619 if (EI_DYNAMICS(ir->eI))
621 t = ir->init_t + (step + delta_step)*ir->delta_t;
627 set_pbc(&pbc, ir->ePBC, box);
628 pull_constraint(ir->pull_work, md, &pbc, cr, ir->delta_t, t, x, xprime, v, *vir);
630 if (constr->ed && delta_step > 0)
632 /* apply the essential dynamics constraints here */
633 do_edsam(ir, step, cr, xprime, v, box, constr->ed);
637 if (v != nullptr && md->cFREEZE)
639 /* Set the velocities of frozen dimensions to zero */
641 // cppcheck-suppress unreadVariable
642 int gmx_unused numThreads = gmx_omp_nthreads_get(emntUpdate);
644 #pragma omp parallel for num_threads(numThreads) schedule(static)
645 for (int i = 0; i < md->homenr; i++)
647 int freezeGroup = md->cFREEZE[i];
649 for (int d = 0; d < DIM; d++)
651 if (ir->opts.nFreeze[freezeGroup][d])
662 real *constr_rmsd_data(struct gmx_constr *constr)
666 return lincs_rmsd_data(constr->lincsd);
674 real constr_rmsd(struct gmx_constr *constr)
678 return lincs_rmsd(constr->lincsd);
686 static void make_shake_sblock_serial(struct gmx_constr *constr,
687 t_idef *idef, const t_mdatoms *md)
696 /* Since we are processing the local topology,
697 * the F_CONSTRNC ilist has been concatenated to the F_CONSTR ilist.
699 ncons = idef->il[F_CONSTR].nr/3;
701 init_blocka(&sblocks);
702 gen_sblocks(nullptr, 0, md->homenr, idef, &sblocks, FALSE);
705 bstart=(idef->nodeid > 0) ? blocks->multinr[idef->nodeid-1] : 0;
706 nblocks=blocks->multinr[idef->nodeid] - bstart;
709 constr->nblocks = sblocks.nr;
712 fprintf(debug, "ncons: %d, bstart: %d, nblocks: %d\n",
713 ncons, bstart, constr->nblocks);
716 /* Calculate block number for each atom */
717 inv_sblock = make_invblocka(&sblocks, md->nr);
719 done_blocka(&sblocks);
721 /* Store the block number in temp array and
722 * sort the constraints in order of the sblock number
723 * and the atom numbers, really sorting a segment of the array!
726 pr_idef(fplog, 0, "Before Sort", idef);
728 iatom = idef->il[F_CONSTR].iatoms;
730 for (i = 0; (i < ncons); i++, iatom += 3)
732 for (m = 0; (m < 3); m++)
734 sb[i].iatom[m] = iatom[m];
736 sb[i].blocknr = inv_sblock[iatom[1]];
739 /* Now sort the blocks */
742 pr_sortblock(debug, "Before sorting", ncons, sb);
743 fprintf(debug, "Going to sort constraints\n");
746 qsort(sb, ncons, (size_t)sizeof(*sb), pcomp);
750 pr_sortblock(debug, "After sorting", ncons, sb);
753 iatom = idef->il[F_CONSTR].iatoms;
754 for (i = 0; (i < ncons); i++, iatom += 3)
756 for (m = 0; (m < 3); m++)
758 iatom[m] = sb[i].iatom[m];
762 pr_idef(fplog, 0, "After Sort", idef);
766 snew(constr->sblock, constr->nblocks+1);
768 for (i = 0; (i < ncons); i++)
770 if (sb[i].blocknr != bnr)
773 constr->sblock[j++] = 3*i;
777 constr->sblock[j++] = 3*ncons;
779 if (j != (constr->nblocks+1))
781 fprintf(stderr, "bstart: %d\n", bstart);
782 fprintf(stderr, "j: %d, nblocks: %d, ncons: %d\n",
783 j, constr->nblocks, ncons);
784 for (i = 0; (i < ncons); i++)
786 fprintf(stderr, "i: %5d sb[i].blocknr: %5d\n", i, sb[i].blocknr);
788 for (j = 0; (j <= constr->nblocks); j++)
790 fprintf(stderr, "sblock[%3d]=%5d\n", j, (int)constr->sblock[j]);
792 gmx_fatal(FARGS, "DEATH HORROR: "
793 "sblocks does not match idef->il[F_CONSTR]");
799 static void make_shake_sblock_dd(struct gmx_constr *constr,
800 const t_ilist *ilcon, const t_block *cgs,
801 const gmx_domdec_t *dd)
806 if (dd->ncg_home+1 > constr->sblock_nalloc)
808 constr->sblock_nalloc = over_alloc_dd(dd->ncg_home+1);
809 srenew(constr->sblock, constr->sblock_nalloc);
813 iatom = ilcon->iatoms;
816 for (c = 0; c < ncons; c++)
818 if (c == 0 || iatom[1] >= cgs->index[cg+1])
820 constr->sblock[constr->nblocks++] = 3*c;
821 while (iatom[1] >= cgs->index[cg+1])
828 constr->sblock[constr->nblocks] = 3*ncons;
831 t_blocka make_at2con(int start, int natoms,
832 const t_ilist *ilist, const t_iparams *iparams,
833 gmx_bool bDynamics, int *nflexiblecons)
835 int *count, ncon, con, con_tot, nflexcon, ftype, i, a;
842 for (ftype = F_CONSTR; ftype <= F_CONSTRNC; ftype++)
844 ncon = ilist[ftype].nr/3;
845 ia = ilist[ftype].iatoms;
846 for (con = 0; con < ncon; con++)
848 bFlexCon = (iparams[ia[0]].constr.dA == 0 &&
849 iparams[ia[0]].constr.dB == 0);
854 if (bDynamics || !bFlexCon)
856 for (i = 1; i < 3; i++)
865 *nflexiblecons = nflexcon;
868 at2con.nalloc_index = at2con.nr+1;
869 snew(at2con.index, at2con.nalloc_index);
871 for (a = 0; a < natoms; a++)
873 at2con.index[a+1] = at2con.index[a] + count[a];
876 at2con.nra = at2con.index[natoms];
877 at2con.nalloc_a = at2con.nra;
878 snew(at2con.a, at2con.nalloc_a);
880 /* The F_CONSTRNC constraints have constraint numbers
881 * that continue after the last F_CONSTR constraint.
884 for (ftype = F_CONSTR; ftype <= F_CONSTRNC; ftype++)
886 ncon = ilist[ftype].nr/3;
887 ia = ilist[ftype].iatoms;
888 for (con = 0; con < ncon; con++)
890 bFlexCon = (iparams[ia[0]].constr.dA == 0 &&
891 iparams[ia[0]].constr.dB == 0);
892 if (bDynamics || !bFlexCon)
894 for (i = 1; i < 3; i++)
897 at2con.a[at2con.index[a]+count[a]++] = con_tot;
910 static int *make_at2settle(int natoms, const t_ilist *ilist)
916 /* Set all to no settle */
917 for (a = 0; a < natoms; a++)
922 stride = 1 + NRAL(F_SETTLE);
924 for (s = 0; s < ilist->nr; s += stride)
926 at2s[ilist->iatoms[s+1]] = s/stride;
927 at2s[ilist->iatoms[s+2]] = s/stride;
928 at2s[ilist->iatoms[s+3]] = s/stride;
934 void set_constraints(struct gmx_constr *constr,
935 gmx_localtop_t *top, const t_inputrec *ir,
936 const t_mdatoms *md, t_commrec *cr)
938 t_idef *idef = &top->idef;
940 if (constr->ncon_tot > 0)
942 /* We are using the local topology,
943 * so there are only F_CONSTR constraints.
945 int ncons = idef->il[F_CONSTR].nr/3;
947 /* With DD we might also need to call LINCS with ncons=0 for
948 * communicating coordinates to other nodes that do have constraints.
950 if (ir->eConstrAlg == econtLINCS)
952 set_lincs(idef, md, EI_DYNAMICS(ir->eI), cr, constr->lincsd);
954 if (ir->eConstrAlg == econtSHAKE)
958 make_shake_sblock_dd(constr, &idef->il[F_CONSTR], &top->cgs, cr->dd);
962 make_shake_sblock_serial(constr, idef, md);
964 if (ncons > constr->lagr_nalloc)
966 constr->lagr_nalloc = over_alloc_dd(ncons);
967 srenew(constr->lagr, constr->lagr_nalloc);
974 settle_set_constraints(constr->settled,
975 &idef->il[F_SETTLE], md);
978 /* Make a selection of the local atoms for essential dynamics */
979 if (constr->ed && cr->dd)
981 dd_make_local_ed_indices(cr->dd, constr->ed);
985 static void constr_recur(const t_blocka *at2con,
986 const t_ilist *ilist, const t_iparams *iparams,
988 int at, int depth, int nc, int *path,
989 real r0, real r1, real *r2max,
1001 ncon1 = ilist[F_CONSTR].nr/3;
1002 ia1 = ilist[F_CONSTR].iatoms;
1003 ia2 = ilist[F_CONSTRNC].iatoms;
1005 /* Loop over all constraints connected to this atom */
1006 for (c = at2con->index[at]; c < at2con->index[at+1]; c++)
1009 /* Do not walk over already used constraints */
1011 for (a1 = 0; a1 < depth; a1++)
1013 if (con == path[a1])
1020 ia = constr_iatomptr(ncon1, ia1, ia2, con);
1021 /* Flexible constraints currently have length 0, which is incorrect */
1024 len = iparams[ia[0]].constr.dA;
1028 len = iparams[ia[0]].constr.dB;
1030 /* In the worst case the bond directions alternate */
1041 /* Assume angles of 120 degrees between all bonds */
1042 if (rn0*rn0 + rn1*rn1 + rn0*rn1 > *r2max)
1044 *r2max = rn0*rn0 + rn1*rn1 + r0*rn1;
1047 fprintf(debug, "Found longer constraint distance: r0 %5.3f r1 %5.3f rmax %5.3f\n", rn0, rn1, sqrt(*r2max));
1048 for (a1 = 0; a1 < depth; a1++)
1050 fprintf(debug, " %d %5.3f",
1052 iparams[constr_iatomptr(ncon1, ia1, ia2, con)[0]].constr.dA);
1054 fprintf(debug, " %d %5.3f\n", con, len);
1057 /* Limit the number of recursions to 1000*nc,
1058 * so a call does not take more than a second,
1059 * even for highly connected systems.
1061 if (depth + 1 < nc && *count < 1000*nc)
1073 constr_recur(at2con, ilist, iparams,
1074 bTopB, a1, depth+1, nc, path, rn0, rn1, r2max, count);
1081 static real constr_r_max_moltype(const gmx_moltype_t *molt,
1082 const t_iparams *iparams,
1083 const t_inputrec *ir)
1085 int natoms, nflexcon, *path, at, count;
1088 real r0, r1, r2maxA, r2maxB, rmax, lam0, lam1;
1090 if (molt->ilist[F_CONSTR].nr == 0 &&
1091 molt->ilist[F_CONSTRNC].nr == 0)
1096 natoms = molt->atoms.nr;
1098 at2con = make_at2con(0, natoms, molt->ilist, iparams,
1099 EI_DYNAMICS(ir->eI), &nflexcon);
1100 snew(path, 1+ir->nProjOrder);
1101 for (at = 0; at < 1+ir->nProjOrder; at++)
1107 for (at = 0; at < natoms; at++)
1113 constr_recur(&at2con, molt->ilist, iparams,
1114 FALSE, at, 0, 1+ir->nProjOrder, path, r0, r1, &r2maxA, &count);
1116 if (ir->efep == efepNO)
1118 rmax = sqrt(r2maxA);
1123 for (at = 0; at < natoms; at++)
1128 constr_recur(&at2con, molt->ilist, iparams,
1129 TRUE, at, 0, 1+ir->nProjOrder, path, r0, r1, &r2maxB, &count);
1131 lam0 = ir->fepvals->init_lambda;
1132 if (EI_DYNAMICS(ir->eI))
1134 lam0 += ir->init_step*ir->fepvals->delta_lambda;
1136 rmax = (1 - lam0)*sqrt(r2maxA) + lam0*sqrt(r2maxB);
1137 if (EI_DYNAMICS(ir->eI))
1139 lam1 = ir->fepvals->init_lambda + (ir->init_step + ir->nsteps)*ir->fepvals->delta_lambda;
1140 rmax = std::max(rmax, (1 - lam1)*std::sqrt(r2maxA) + lam1*std::sqrt(r2maxB));
1144 done_blocka(&at2con);
1150 real constr_r_max(FILE *fplog, const gmx_mtop_t *mtop, const t_inputrec *ir)
1156 for (mt = 0; mt < mtop->nmoltype; mt++)
1158 rmax = std::max(rmax,
1159 constr_r_max_moltype(&mtop->moltype[mt],
1160 mtop->ffparams.iparams, ir));
1165 fprintf(fplog, "Maximum distance for %d constraints, at 120 deg. angles, all-trans: %.3f nm\n", 1+ir->nProjOrder, rmax);
1171 gmx_constr_t init_constraints(FILE *fplog,
1172 const gmx_mtop_t *mtop, const t_inputrec *ir,
1173 bool doEssentialDynamics,
1177 gmx_mtop_ftype_count(mtop, F_CONSTR) +
1178 gmx_mtop_ftype_count(mtop, F_CONSTRNC);
1180 gmx_mtop_ftype_count(mtop, F_SETTLE);
1182 GMX_RELEASE_ASSERT(!ir->bPull || ir->pull_work != nullptr, "init_constraints called with COM pulling before/without initializing the pull code");
1184 if (nconstraints + nsettles == 0 &&
1185 !(ir->bPull && pull_have_constraint(ir->pull_work)) &&
1186 !doEssentialDynamics)
1191 struct gmx_constr *constr;
1195 constr->ncon_tot = nconstraints;
1196 constr->nflexcon = 0;
1197 if (nconstraints > 0)
1199 constr->n_at2con_mt = mtop->nmoltype;
1200 snew(constr->at2con_mt, constr->n_at2con_mt);
1201 for (int mt = 0; mt < mtop->nmoltype; mt++)
1204 constr->at2con_mt[mt] = make_at2con(0, mtop->moltype[mt].atoms.nr,
1205 mtop->moltype[mt].ilist,
1206 mtop->ffparams.iparams,
1207 EI_DYNAMICS(ir->eI), &nflexcon);
1208 for (int i = 0; i < mtop->nmolblock; i++)
1210 if (mtop->molblock[i].type == mt)
1212 constr->nflexcon += mtop->molblock[i].nmol*nflexcon;
1217 if (constr->nflexcon > 0)
1221 fprintf(fplog, "There are %d flexible constraints\n",
1223 if (ir->fc_stepsize == 0)
1226 "WARNING: step size for flexible constraining = 0\n"
1227 " All flexible constraints will be rigid.\n"
1228 " Will try to keep all flexible constraints at their original length,\n"
1229 " but the lengths may exhibit some drift.\n\n");
1230 constr->nflexcon = 0;
1233 if (constr->nflexcon > 0)
1235 please_cite(fplog, "Hess2002");
1239 if (ir->eConstrAlg == econtLINCS)
1241 constr->lincsd = init_lincs(fplog, mtop,
1242 constr->nflexcon, constr->at2con_mt,
1243 DOMAINDECOMP(cr) && cr->dd->bInterCGcons,
1244 ir->nLincsIter, ir->nProjOrder);
1247 if (ir->eConstrAlg == econtSHAKE)
1249 if (DOMAINDECOMP(cr) && cr->dd->bInterCGcons)
1251 gmx_fatal(FARGS, "SHAKE is not supported with domain decomposition and constraint that cross charge group boundaries, use LINCS");
1253 if (constr->nflexcon)
1255 gmx_fatal(FARGS, "For this system also velocities and/or forces need to be constrained, this can not be done with SHAKE, you should select LINCS");
1257 please_cite(fplog, "Ryckaert77a");
1260 please_cite(fplog, "Barth95a");
1263 constr->shaked = shake_init();
1269 please_cite(fplog, "Miyamoto92a");
1271 constr->bInterCGsettles = inter_charge_group_settles(mtop);
1273 constr->settled = settle_init(mtop);
1275 /* Make an atom to settle index for use in domain decomposition */
1276 constr->n_at2settle_mt = mtop->nmoltype;
1277 snew(constr->at2settle_mt, constr->n_at2settle_mt);
1278 for (int mt = 0; mt < mtop->nmoltype; mt++)
1280 constr->at2settle_mt[mt] =
1281 make_at2settle(mtop->moltype[mt].atoms.nr,
1282 &mtop->moltype[mt].ilist[F_SETTLE]);
1285 /* Allocate thread-local work arrays */
1286 int nthreads = gmx_omp_nthreads_get(emntSETTLE);
1287 if (nthreads > 1 && constr->vir_r_m_dr_th == nullptr)
1289 snew(constr->vir_r_m_dr_th, nthreads);
1290 snew(constr->bSettleErrorHasOccurred, nthreads);
1294 if (nconstraints + nsettles > 0 && ir->epc == epcMTTK)
1296 gmx_fatal(FARGS, "Constraints are not implemented with MTTK pressure control.");
1299 constr->maxwarn = 999;
1300 char *env = getenv("GMX_MAXCONSTRWARN");
1303 constr->maxwarn = 0;
1304 sscanf(env, "%8d", &constr->maxwarn);
1305 if (constr->maxwarn < 0)
1307 constr->maxwarn = INT_MAX;
1312 "Setting the maximum number of constraint warnings to %d\n",
1318 "Setting the maximum number of constraint warnings to %d\n",
1322 constr->warncount_lincs = 0;
1323 constr->warncount_settle = 0;
1325 constr->warn_mtop = mtop;
1330 /* Put a pointer to the essential dynamics constraints into the constr struct */
1331 void saveEdsamPointer(gmx_constr_t constr, gmx_edsam_t ed)
1336 const t_blocka *atom2constraints_moltype(gmx_constr_t constr)
1338 return constr->at2con_mt;
1341 const int **atom2settle_moltype(gmx_constr_t constr)
1343 return (const int **)constr->at2settle_mt;
1347 gmx_bool inter_charge_group_constraints(const gmx_mtop_t *mtop)
1349 const gmx_moltype_t *molt;
1353 int *at2cg, cg, a, ftype, i;
1357 for (mb = 0; mb < mtop->nmolblock && !bInterCG; mb++)
1359 molt = &mtop->moltype[mtop->molblock[mb].type];
1361 if (molt->ilist[F_CONSTR].nr > 0 ||
1362 molt->ilist[F_CONSTRNC].nr > 0 ||
1363 molt->ilist[F_SETTLE].nr > 0)
1366 snew(at2cg, molt->atoms.nr);
1367 for (cg = 0; cg < cgs->nr; cg++)
1369 for (a = cgs->index[cg]; a < cgs->index[cg+1]; a++)
1375 for (ftype = F_CONSTR; ftype <= F_CONSTRNC; ftype++)
1377 il = &molt->ilist[ftype];
1378 for (i = 0; i < il->nr && !bInterCG; i += 1+NRAL(ftype))
1380 if (at2cg[il->iatoms[i+1]] != at2cg[il->iatoms[i+2]])
1394 gmx_bool inter_charge_group_settles(const gmx_mtop_t *mtop)
1396 const gmx_moltype_t *molt;
1400 int *at2cg, cg, a, ftype, i;
1404 for (mb = 0; mb < mtop->nmolblock && !bInterCG; mb++)
1406 molt = &mtop->moltype[mtop->molblock[mb].type];
1408 if (molt->ilist[F_SETTLE].nr > 0)
1411 snew(at2cg, molt->atoms.nr);
1412 for (cg = 0; cg < cgs->nr; cg++)
1414 for (a = cgs->index[cg]; a < cgs->index[cg+1]; a++)
1420 for (ftype = F_SETTLE; ftype <= F_SETTLE; ftype++)
1422 il = &molt->ilist[ftype];
1423 for (i = 0; i < il->nr && !bInterCG; i += 1+NRAL(F_SETTLE))
1425 if (at2cg[il->iatoms[i+1]] != at2cg[il->iatoms[i+2]] ||
1426 at2cg[il->iatoms[i+1]] != at2cg[il->iatoms[i+3]])
1440 /* helper functions for andersen temperature control, because the
1441 * gmx_constr construct is only defined in constr.c. Return the list
1442 * of blocks (get_sblock) and the number of blocks (get_nblocks). */
1444 extern int *get_sblock(struct gmx_constr *constr)
1446 return constr->sblock;
1449 extern int get_nblocks(struct gmx_constr *constr)
1451 return constr->nblocks;