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, 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/pbcutil/pbc.h"
64 #include "gromacs/pulling/pull.h"
65 #include "gromacs/topology/block.h"
66 #include "gromacs/topology/invblock.h"
67 #include "gromacs/topology/mtop_lookup.h"
68 #include "gromacs/topology/mtop_util.h"
69 #include "gromacs/utility/exceptions.h"
70 #include "gromacs/utility/fatalerror.h"
71 #include "gromacs/utility/pleasecite.h"
72 #include "gromacs/utility/smalloc.h"
73 #include "gromacs/utility/txtdump.h"
75 typedef struct gmx_constr {
76 int ncon_tot; /* The total number of constraints */
77 int nflexcon; /* The number of flexible constraints */
78 int n_at2con_mt; /* The size of at2con = #moltypes */
79 t_blocka *at2con_mt; /* A list of atoms to constraints */
80 int n_at2settle_mt; /* The size of at2settle = #moltypes */
81 int **at2settle_mt; /* A list of atoms to settles */
82 gmx_bool bInterCGsettles;
83 gmx_lincsdata_t lincsd; /* LINCS data */
84 gmx_shakedata_t shaked; /* SHAKE data */
85 gmx_settledata_t settled; /* SETTLE data */
86 int nblocks; /* The number of SHAKE blocks */
87 int *sblock; /* The SHAKE blocks */
88 int sblock_nalloc; /* The allocation size of sblock */
89 real *lagr; /* -2 times the Lagrange multipliers for SHAKE */
90 int lagr_nalloc; /* The allocation size of lagr */
91 int maxwarn; /* The maximum number of warnings */
94 gmx_edsam_t ed; /* The essential dynamics data */
96 /* Thread local working data */
97 tensor *vir_r_m_dr_th; /* Thread virial contribution */
98 bool *bSettleErrorHasOccurred; /* Did a settle error occur? */
100 /* Only used for printing warnings */
101 const gmx_mtop_t *warn_mtop; /* Pointer to the global topology */
109 static int pcomp(const void *p1, const void *p2)
112 int min1, min2, max1, max2;
113 t_sortblock *a1 = (t_sortblock *)p1;
114 t_sortblock *a2 = (t_sortblock *)p2;
116 db = a1->blocknr-a2->blocknr;
123 min1 = std::min(a1->iatom[1], a1->iatom[2]);
124 max1 = std::max(a1->iatom[1], a1->iatom[2]);
125 min2 = std::min(a2->iatom[1], a2->iatom[2]);
126 max2 = std::max(a2->iatom[1], a2->iatom[2]);
138 int n_flexible_constraints(struct gmx_constr *constr)
144 nflexcon = constr->nflexcon;
154 static void clear_constraint_quantity_nonlocal(gmx_domdec_t *dd, rvec *q)
156 int nonlocal_at_start, nonlocal_at_end, at;
158 dd_get_constraint_range(dd, &nonlocal_at_start, &nonlocal_at_end);
160 for (at = nonlocal_at_start; at < nonlocal_at_end; at++)
166 void too_many_constraint_warnings(int eConstrAlg, int warncount)
169 "Too many %s warnings (%d)\n"
170 "If you know what you are doing you can %s"
171 "set the environment variable GMX_MAXCONSTRWARN to -1,\n"
172 "but normally it is better to fix the problem",
173 (eConstrAlg == econtLINCS) ? "LINCS" : "SETTLE", warncount,
174 (eConstrAlg == econtLINCS) ?
175 "adjust the lincs warning threshold in your mdp file\nor " : "\n");
178 static void write_constr_pdb(const char *fn, const char *title,
179 const gmx_mtop_t *mtop,
180 int start, int homenr, t_commrec *cr,
181 rvec x[], matrix box)
185 int dd_ac0 = 0, dd_ac1 = 0, i, ii, resnr;
187 const char *anm, *resnm;
190 if (DOMAINDECOMP(cr))
193 dd_get_constraint_range(dd, &dd_ac0, &dd_ac1);
200 sprintf(fname, "%s_n%d.pdb", fn, cr->sim_nodeid);
204 sprintf(fname, "%s.pdb", fn);
207 out = gmx_fio_fopen(fname, "w");
209 fprintf(out, "TITLE %s\n", title);
210 gmx_write_pdb_box(out, -1, box);
212 for (i = start; i < start+homenr; i++)
216 if (i >= dd->nat_home && i < dd_ac0)
220 ii = dd->gatindex[i];
226 mtopGetAtomAndResidueName(mtop, ii, &molb, &anm, &resnr, &resnm, nullptr);
227 gmx_fprintf_pdb_atomline(out, epdbATOM, ii+1, anm, ' ', resnm, ' ', resnr, ' ',
228 10*x[i][XX], 10*x[i][YY], 10*x[i][ZZ], 1.0, 0.0, "");
230 fprintf(out, "TER\n");
235 static void dump_confs(FILE *fplog, gmx_int64_t step, const gmx_mtop_t *mtop,
236 int start, int homenr, t_commrec *cr,
237 rvec x[], rvec xprime[], matrix box)
239 char buf[STRLEN], buf2[22];
241 char *env = getenv("GMX_SUPPRESS_DUMP");
247 sprintf(buf, "step%sb", gmx_step_str(step, buf2));
248 write_constr_pdb(buf, "initial coordinates",
249 mtop, start, homenr, cr, x, box);
250 sprintf(buf, "step%sc", gmx_step_str(step, buf2));
251 write_constr_pdb(buf, "coordinates after constraining",
252 mtop, start, homenr, cr, xprime, box);
255 fprintf(fplog, "Wrote pdb files with previous and current coordinates\n");
257 fprintf(stderr, "Wrote pdb files with previous and current coordinates\n");
260 static void pr_sortblock(FILE *fp, const char *title, int nsb, t_sortblock sb[])
264 fprintf(fp, "%s\n", title);
265 for (i = 0; (i < nsb); i++)
267 fprintf(fp, "i: %5d, iatom: (%5d %5d %5d), blocknr: %5d\n",
268 i, sb[i].iatom[0], sb[i].iatom[1], sb[i].iatom[2],
273 gmx_bool constrain(FILE *fplog, gmx_bool bLog, gmx_bool bEner,
274 struct gmx_constr *constr,
275 t_idef *idef, t_inputrec *ir,
277 gmx_int64_t step, int delta_step,
280 rvec *x, rvec *xprime, rvec *min_proj,
281 gmx_bool bMolPBC, matrix box,
282 real lambda, real *dvdlambda,
283 rvec *v, tensor *vir,
284 t_nrnb *nrnb, int econq)
290 real invdt, vir_fac = 0, t;
293 t_pbc pbc, *pbc_null;
297 if (econq == econqForceDispl && !EI_ENERGY_MINIMIZATION(ir->eI))
299 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");
308 scaled_delta_t = step_scaling * ir->delta_t;
310 /* Prepare time step for use in constraint implementations, and
311 avoid generating inf when ir->delta_t = 0. */
312 if (ir->delta_t == 0)
318 invdt = 1.0/scaled_delta_t;
321 if (ir->efep != efepNO && EI_DYNAMICS(ir->eI))
323 /* Set the constraint lengths for the step at which this configuration
324 * is meant to be. The invmasses should not be changed.
326 lambda += delta_step*ir->fepvals->delta_lambda;
331 clear_mat(vir_r_m_dr);
336 settle = &idef->il[F_SETTLE];
337 nsettle = settle->nr/(1+NRAL(F_SETTLE));
341 nth = gmx_omp_nthreads_get(emntSETTLE);
348 /* We do not need full pbc when constraints do not cross charge groups,
349 * i.e. when dd->constraint_comm==NULL.
350 * Note that PBC for constraints is different from PBC for bondeds.
351 * For constraints there is both forward and backward communication.
353 if (ir->ePBC != epbcNONE &&
354 (cr->dd || bMolPBC) && !(cr->dd && cr->dd->constraint_comm == NULL))
356 /* With pbc=screw the screw has been changed to a shift
357 * by the constraint coordinate communication routine,
358 * so that here we can use normal pbc.
360 pbc_null = set_pbc_dd(&pbc, ir->ePBC,
361 DOMAINDECOMP(cr) ? cr->dd->nc : nullptr,
369 /* Communicate the coordinates required for the non-local constraints
370 * for LINCS and/or SETTLE.
374 dd_move_x_constraints(cr->dd, box, x, xprime, econq == econqCoord);
378 /* We need to initialize the non-local components of v.
379 * We never actually use these values, but we do increment them,
380 * so we should avoid uninitialized variables and overflows.
382 clear_constraint_quantity_nonlocal(cr->dd, v);
386 if (constr->lincsd != NULL)
388 bOK = constrain_lincs(fplog, bLog, bEner, ir, step, constr->lincsd, md, cr,
390 box, pbc_null, lambda, dvdlambda,
391 invdt, v, vir != NULL, vir_r_m_dr,
393 constr->maxwarn, &constr->warncount_lincs);
394 if (!bOK && constr->maxwarn < INT_MAX)
398 fprintf(fplog, "Constraint error in algorithm %s at step %s\n",
399 econstr_names[econtLINCS], gmx_step_str(step, buf));
405 if (constr->nblocks > 0)
410 bOK = bshakef(fplog, constr->shaked,
411 md->invmass, constr->nblocks, constr->sblock,
412 idef, ir, x, xprime, nrnb,
413 constr->lagr, lambda, dvdlambda,
414 invdt, v, vir != NULL, vir_r_m_dr,
415 constr->maxwarn < INT_MAX, econq);
418 bOK = bshakef(fplog, constr->shaked,
419 md->invmass, constr->nblocks, constr->sblock,
420 idef, ir, x, min_proj, nrnb,
421 constr->lagr, lambda, dvdlambda,
422 invdt, NULL, vir != NULL, vir_r_m_dr,
423 constr->maxwarn < INT_MAX, econq);
426 gmx_fatal(FARGS, "Internal error, SHAKE called for constraining something else than coordinates");
430 if (!bOK && constr->maxwarn < INT_MAX)
434 fprintf(fplog, "Constraint error in algorithm %s at step %s\n",
435 econstr_names[econtSHAKE], gmx_step_str(step, buf));
443 bool bSettleErrorHasOccurred = false;
448 #pragma omp parallel for num_threads(nth) schedule(static)
449 for (th = 0; th < nth; th++)
455 clear_mat(constr->vir_r_m_dr_th[th]);
458 csettle(constr->settled,
462 invdt, v ? v[0] : NULL,
464 th == 0 ? vir_r_m_dr : constr->vir_r_m_dr_th[th],
465 th == 0 ? &bSettleErrorHasOccurred : &constr->bSettleErrorHasOccurred[th]);
467 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
469 inc_nrnb(nrnb, eNR_SETTLE, nsettle);
472 inc_nrnb(nrnb, eNR_CONSTR_V, nsettle*3);
476 inc_nrnb(nrnb, eNR_CONSTR_VIR, nsettle*3);
482 case econqForceDispl:
483 #pragma omp parallel for num_threads(nth) schedule(static)
484 for (th = 0; th < nth; th++)
488 int calcvir_atom_end;
492 calcvir_atom_end = 0;
496 calcvir_atom_end = md->homenr;
501 clear_mat(constr->vir_r_m_dr_th[th]);
504 int start_th = (nsettle* th )/nth;
505 int end_th = (nsettle*(th+1))/nth;
507 if (start_th >= 0 && end_th - start_th > 0)
509 settle_proj(constr->settled, econq,
511 settle->iatoms+start_th*(1+NRAL(F_SETTLE)),
514 xprime, min_proj, calcvir_atom_end,
515 th == 0 ? vir_r_m_dr : constr->vir_r_m_dr_th[th]);
518 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
520 /* This is an overestimate */
521 inc_nrnb(nrnb, eNR_SETTLE, nsettle);
523 case econqDeriv_FlexCon:
524 /* Nothing to do, since the are no flexible constraints in settles */
527 gmx_incons("Unknown constraint quantity for settle");
532 /* Reduce the virial contributions over the threads */
533 for (int th = 1; th < nth; th++)
535 m_add(vir_r_m_dr, constr->vir_r_m_dr_th[th], vir_r_m_dr);
539 if (econq == econqCoord)
541 for (int th = 1; th < nth; th++)
543 bSettleErrorHasOccurred = bSettleErrorHasOccurred || constr->bSettleErrorHasOccurred[th];
546 if (bSettleErrorHasOccurred)
550 "\nstep " "%" GMX_PRId64 ": One or more water molecules can not be settled.\n"
551 "Check for bad contacts and/or reduce the timestep if appropriate.\n",
555 fprintf(fplog, "%s", buf);
557 fprintf(stderr, "%s", buf);
558 constr->warncount_settle++;
559 if (constr->warncount_settle > constr->maxwarn)
561 too_many_constraint_warnings(-1, constr->warncount_settle);
572 /* The normal uses of constrain() pass step_scaling = 1.0.
573 * The call to constrain() for SD1 that passes step_scaling =
574 * 0.5 also passes vir = NULL, so cannot reach this
575 * assertion. This assertion should remain until someone knows
576 * that this path works for their intended purpose, and then
577 * they can use scaled_delta_t instead of ir->delta_t
579 assert(gmx_within_tol(step_scaling, 1.0, GMX_REAL_EPS));
583 vir_fac = 0.5/(ir->delta_t*ir->delta_t);
586 vir_fac = 0.5/ir->delta_t;
589 case econqForceDispl:
593 gmx_incons("Unsupported constraint quantity for virial");
598 vir_fac *= 2; /* only constraining over half the distance here */
600 for (int i = 0; i < DIM; i++)
602 for (int j = 0; j < DIM; j++)
604 (*vir)[i][j] = vir_fac*vir_r_m_dr[i][j];
611 dump_confs(fplog, step, constr->warn_mtop, start, homenr, cr, x, xprime, box);
614 if (econq == econqCoord)
616 if (ir->bPull && pull_have_constraint(ir->pull_work))
618 if (EI_DYNAMICS(ir->eI))
620 t = ir->init_t + (step + delta_step)*ir->delta_t;
626 set_pbc(&pbc, ir->ePBC, box);
627 pull_constraint(ir->pull_work, md, &pbc, cr, ir->delta_t, t, x, xprime, v, *vir);
629 if (constr->ed && delta_step > 0)
631 /* apply the essential dynamcs constraints here */
632 do_edsam(ir, step, cr, xprime, v, box, constr->ed);
639 real *constr_rmsd_data(struct gmx_constr *constr)
643 return lincs_rmsd_data(constr->lincsd);
651 real constr_rmsd(struct gmx_constr *constr)
655 return lincs_rmsd(constr->lincsd);
663 static void make_shake_sblock_serial(struct gmx_constr *constr,
664 t_idef *idef, const t_mdatoms *md)
673 /* Since we are processing the local topology,
674 * the F_CONSTRNC ilist has been concatenated to the F_CONSTR ilist.
676 ncons = idef->il[F_CONSTR].nr/3;
678 init_blocka(&sblocks);
679 gen_sblocks(NULL, 0, md->homenr, idef, &sblocks, FALSE);
682 bstart=(idef->nodeid > 0) ? blocks->multinr[idef->nodeid-1] : 0;
683 nblocks=blocks->multinr[idef->nodeid] - bstart;
686 constr->nblocks = sblocks.nr;
689 fprintf(debug, "ncons: %d, bstart: %d, nblocks: %d\n",
690 ncons, bstart, constr->nblocks);
693 /* Calculate block number for each atom */
694 inv_sblock = make_invblocka(&sblocks, md->nr);
696 done_blocka(&sblocks);
698 /* Store the block number in temp array and
699 * sort the constraints in order of the sblock number
700 * and the atom numbers, really sorting a segment of the array!
703 pr_idef(fplog, 0, "Before Sort", idef);
705 iatom = idef->il[F_CONSTR].iatoms;
707 for (i = 0; (i < ncons); i++, iatom += 3)
709 for (m = 0; (m < 3); m++)
711 sb[i].iatom[m] = iatom[m];
713 sb[i].blocknr = inv_sblock[iatom[1]];
716 /* Now sort the blocks */
719 pr_sortblock(debug, "Before sorting", ncons, sb);
720 fprintf(debug, "Going to sort constraints\n");
723 qsort(sb, ncons, (size_t)sizeof(*sb), pcomp);
727 pr_sortblock(debug, "After sorting", ncons, sb);
730 iatom = idef->il[F_CONSTR].iatoms;
731 for (i = 0; (i < ncons); i++, iatom += 3)
733 for (m = 0; (m < 3); m++)
735 iatom[m] = sb[i].iatom[m];
739 pr_idef(fplog, 0, "After Sort", idef);
743 snew(constr->sblock, constr->nblocks+1);
745 for (i = 0; (i < ncons); i++)
747 if (sb[i].blocknr != bnr)
750 constr->sblock[j++] = 3*i;
754 constr->sblock[j++] = 3*ncons;
756 if (j != (constr->nblocks+1))
758 fprintf(stderr, "bstart: %d\n", bstart);
759 fprintf(stderr, "j: %d, nblocks: %d, ncons: %d\n",
760 j, constr->nblocks, ncons);
761 for (i = 0; (i < ncons); i++)
763 fprintf(stderr, "i: %5d sb[i].blocknr: %5d\n", i, sb[i].blocknr);
765 for (j = 0; (j <= constr->nblocks); j++)
767 fprintf(stderr, "sblock[%3d]=%5d\n", j, (int)constr->sblock[j]);
769 gmx_fatal(FARGS, "DEATH HORROR: "
770 "sblocks does not match idef->il[F_CONSTR]");
776 static void make_shake_sblock_dd(struct gmx_constr *constr,
777 const t_ilist *ilcon, const t_block *cgs,
778 const gmx_domdec_t *dd)
783 if (dd->ncg_home+1 > constr->sblock_nalloc)
785 constr->sblock_nalloc = over_alloc_dd(dd->ncg_home+1);
786 srenew(constr->sblock, constr->sblock_nalloc);
790 iatom = ilcon->iatoms;
793 for (c = 0; c < ncons; c++)
795 if (c == 0 || iatom[1] >= cgs->index[cg+1])
797 constr->sblock[constr->nblocks++] = 3*c;
798 while (iatom[1] >= cgs->index[cg+1])
805 constr->sblock[constr->nblocks] = 3*ncons;
808 t_blocka make_at2con(int start, int natoms,
809 const t_ilist *ilist, const t_iparams *iparams,
810 gmx_bool bDynamics, int *nflexiblecons)
812 int *count, ncon, con, con_tot, nflexcon, ftype, i, a;
819 for (ftype = F_CONSTR; ftype <= F_CONSTRNC; ftype++)
821 ncon = ilist[ftype].nr/3;
822 ia = ilist[ftype].iatoms;
823 for (con = 0; con < ncon; con++)
825 bFlexCon = (iparams[ia[0]].constr.dA == 0 &&
826 iparams[ia[0]].constr.dB == 0);
831 if (bDynamics || !bFlexCon)
833 for (i = 1; i < 3; i++)
842 *nflexiblecons = nflexcon;
845 at2con.nalloc_index = at2con.nr+1;
846 snew(at2con.index, at2con.nalloc_index);
848 for (a = 0; a < natoms; a++)
850 at2con.index[a+1] = at2con.index[a] + count[a];
853 at2con.nra = at2con.index[natoms];
854 at2con.nalloc_a = at2con.nra;
855 snew(at2con.a, at2con.nalloc_a);
857 /* The F_CONSTRNC constraints have constraint numbers
858 * that continue after the last F_CONSTR constraint.
861 for (ftype = F_CONSTR; ftype <= F_CONSTRNC; ftype++)
863 ncon = ilist[ftype].nr/3;
864 ia = ilist[ftype].iatoms;
865 for (con = 0; con < ncon; con++)
867 bFlexCon = (iparams[ia[0]].constr.dA == 0 &&
868 iparams[ia[0]].constr.dB == 0);
869 if (bDynamics || !bFlexCon)
871 for (i = 1; i < 3; i++)
874 at2con.a[at2con.index[a]+count[a]++] = con_tot;
887 static int *make_at2settle(int natoms, const t_ilist *ilist)
893 /* Set all to no settle */
894 for (a = 0; a < natoms; a++)
899 stride = 1 + NRAL(F_SETTLE);
901 for (s = 0; s < ilist->nr; s += stride)
903 at2s[ilist->iatoms[s+1]] = s/stride;
904 at2s[ilist->iatoms[s+2]] = s/stride;
905 at2s[ilist->iatoms[s+3]] = s/stride;
911 void set_constraints(struct gmx_constr *constr,
912 gmx_localtop_t *top, const t_inputrec *ir,
913 const t_mdatoms *md, t_commrec *cr)
915 t_idef *idef = &top->idef;
917 if (constr->ncon_tot > 0)
919 /* We are using the local topology,
920 * so there are only F_CONSTR constraints.
922 int ncons = idef->il[F_CONSTR].nr/3;
924 /* With DD we might also need to call LINCS with ncons=0 for
925 * communicating coordinates to other nodes that do have constraints.
927 if (ir->eConstrAlg == econtLINCS)
929 set_lincs(idef, md, EI_DYNAMICS(ir->eI), cr, constr->lincsd);
931 if (ir->eConstrAlg == econtSHAKE)
935 make_shake_sblock_dd(constr, &idef->il[F_CONSTR], &top->cgs, cr->dd);
939 make_shake_sblock_serial(constr, idef, md);
941 if (ncons > constr->lagr_nalloc)
943 constr->lagr_nalloc = over_alloc_dd(ncons);
944 srenew(constr->lagr, constr->lagr_nalloc);
951 settle_set_constraints(constr->settled,
952 &idef->il[F_SETTLE], md);
955 /* Make a selection of the local atoms for essential dynamics */
956 if (constr->ed && cr->dd)
958 dd_make_local_ed_indices(cr->dd, constr->ed);
962 static void constr_recur(const t_blocka *at2con,
963 const t_ilist *ilist, const t_iparams *iparams,
965 int at, int depth, int nc, int *path,
966 real r0, real r1, real *r2max,
978 ncon1 = ilist[F_CONSTR].nr/3;
979 ia1 = ilist[F_CONSTR].iatoms;
980 ia2 = ilist[F_CONSTRNC].iatoms;
982 /* Loop over all constraints connected to this atom */
983 for (c = at2con->index[at]; c < at2con->index[at+1]; c++)
986 /* Do not walk over already used constraints */
988 for (a1 = 0; a1 < depth; a1++)
997 ia = constr_iatomptr(ncon1, ia1, ia2, con);
998 /* Flexible constraints currently have length 0, which is incorrect */
1001 len = iparams[ia[0]].constr.dA;
1005 len = iparams[ia[0]].constr.dB;
1007 /* In the worst case the bond directions alternate */
1018 /* Assume angles of 120 degrees between all bonds */
1019 if (rn0*rn0 + rn1*rn1 + rn0*rn1 > *r2max)
1021 *r2max = rn0*rn0 + rn1*rn1 + r0*rn1;
1024 fprintf(debug, "Found longer constraint distance: r0 %5.3f r1 %5.3f rmax %5.3f\n", rn0, rn1, sqrt(*r2max));
1025 for (a1 = 0; a1 < depth; a1++)
1027 fprintf(debug, " %d %5.3f",
1029 iparams[constr_iatomptr(ncon1, ia1, ia2, con)[0]].constr.dA);
1031 fprintf(debug, " %d %5.3f\n", con, len);
1034 /* Limit the number of recursions to 1000*nc,
1035 * so a call does not take more than a second,
1036 * even for highly connected systems.
1038 if (depth + 1 < nc && *count < 1000*nc)
1050 constr_recur(at2con, ilist, iparams,
1051 bTopB, a1, depth+1, nc, path, rn0, rn1, r2max, count);
1058 static real constr_r_max_moltype(const gmx_moltype_t *molt,
1059 const t_iparams *iparams,
1060 const t_inputrec *ir)
1062 int natoms, nflexcon, *path, at, count;
1065 real r0, r1, r2maxA, r2maxB, rmax, lam0, lam1;
1067 if (molt->ilist[F_CONSTR].nr == 0 &&
1068 molt->ilist[F_CONSTRNC].nr == 0)
1073 natoms = molt->atoms.nr;
1075 at2con = make_at2con(0, natoms, molt->ilist, iparams,
1076 EI_DYNAMICS(ir->eI), &nflexcon);
1077 snew(path, 1+ir->nProjOrder);
1078 for (at = 0; at < 1+ir->nProjOrder; at++)
1084 for (at = 0; at < natoms; at++)
1090 constr_recur(&at2con, molt->ilist, iparams,
1091 FALSE, at, 0, 1+ir->nProjOrder, path, r0, r1, &r2maxA, &count);
1093 if (ir->efep == efepNO)
1095 rmax = sqrt(r2maxA);
1100 for (at = 0; at < natoms; at++)
1105 constr_recur(&at2con, molt->ilist, iparams,
1106 TRUE, at, 0, 1+ir->nProjOrder, path, r0, r1, &r2maxB, &count);
1108 lam0 = ir->fepvals->init_lambda;
1109 if (EI_DYNAMICS(ir->eI))
1111 lam0 += ir->init_step*ir->fepvals->delta_lambda;
1113 rmax = (1 - lam0)*sqrt(r2maxA) + lam0*sqrt(r2maxB);
1114 if (EI_DYNAMICS(ir->eI))
1116 lam1 = ir->fepvals->init_lambda + (ir->init_step + ir->nsteps)*ir->fepvals->delta_lambda;
1117 rmax = std::max(rmax, (1 - lam1)*std::sqrt(r2maxA) + lam1*std::sqrt(r2maxB));
1121 done_blocka(&at2con);
1127 real constr_r_max(FILE *fplog, const gmx_mtop_t *mtop, const t_inputrec *ir)
1133 for (mt = 0; mt < mtop->nmoltype; mt++)
1135 rmax = std::max(rmax,
1136 constr_r_max_moltype(&mtop->moltype[mt],
1137 mtop->ffparams.iparams, ir));
1142 fprintf(fplog, "Maximum distance for %d constraints, at 120 deg. angles, all-trans: %.3f nm\n", 1+ir->nProjOrder, rmax);
1148 gmx_constr_t init_constraints(FILE *fplog,
1149 const gmx_mtop_t *mtop, const t_inputrec *ir,
1150 gmx_edsam_t ed, t_state *state,
1154 gmx_mtop_ftype_count(mtop, F_CONSTR) +
1155 gmx_mtop_ftype_count(mtop, F_CONSTRNC);
1157 gmx_mtop_ftype_count(mtop, F_SETTLE);
1159 GMX_RELEASE_ASSERT(!ir->bPull || ir->pull_work != NULL, "init_constraints called with COM pulling before/without initializing the pull code");
1161 if (nconstraints + nsettles == 0 &&
1162 !(ir->bPull && pull_have_constraint(ir->pull_work)) &&
1168 struct gmx_constr *constr;
1172 constr->ncon_tot = nconstraints;
1173 constr->nflexcon = 0;
1174 if (nconstraints > 0)
1176 constr->n_at2con_mt = mtop->nmoltype;
1177 snew(constr->at2con_mt, constr->n_at2con_mt);
1178 for (int mt = 0; mt < mtop->nmoltype; mt++)
1181 constr->at2con_mt[mt] = make_at2con(0, mtop->moltype[mt].atoms.nr,
1182 mtop->moltype[mt].ilist,
1183 mtop->ffparams.iparams,
1184 EI_DYNAMICS(ir->eI), &nflexcon);
1185 for (int i = 0; i < mtop->nmolblock; i++)
1187 if (mtop->molblock[i].type == mt)
1189 constr->nflexcon += mtop->molblock[i].nmol*nflexcon;
1194 if (constr->nflexcon > 0)
1198 fprintf(fplog, "There are %d flexible constraints\n",
1200 if (ir->fc_stepsize == 0)
1203 "WARNING: step size for flexible constraining = 0\n"
1204 " All flexible constraints will be rigid.\n"
1205 " Will try to keep all flexible constraints at their original length,\n"
1206 " but the lengths may exhibit some drift.\n\n");
1207 constr->nflexcon = 0;
1210 if (constr->nflexcon > 0)
1212 please_cite(fplog, "Hess2002");
1216 if (ir->eConstrAlg == econtLINCS)
1218 constr->lincsd = init_lincs(fplog, mtop,
1219 constr->nflexcon, constr->at2con_mt,
1220 DOMAINDECOMP(cr) && cr->dd->bInterCGcons,
1221 ir->nLincsIter, ir->nProjOrder);
1224 if (ir->eConstrAlg == econtSHAKE)
1226 if (DOMAINDECOMP(cr) && cr->dd->bInterCGcons)
1228 gmx_fatal(FARGS, "SHAKE is not supported with domain decomposition and constraint that cross charge group boundaries, use LINCS");
1230 if (constr->nflexcon)
1232 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");
1234 please_cite(fplog, "Ryckaert77a");
1237 please_cite(fplog, "Barth95a");
1240 constr->shaked = shake_init();
1246 please_cite(fplog, "Miyamoto92a");
1248 constr->bInterCGsettles = inter_charge_group_settles(mtop);
1250 constr->settled = settle_init(mtop);
1252 /* Make an atom to settle index for use in domain decomposition */
1253 constr->n_at2settle_mt = mtop->nmoltype;
1254 snew(constr->at2settle_mt, constr->n_at2settle_mt);
1255 for (int mt = 0; mt < mtop->nmoltype; mt++)
1257 constr->at2settle_mt[mt] =
1258 make_at2settle(mtop->moltype[mt].atoms.nr,
1259 &mtop->moltype[mt].ilist[F_SETTLE]);
1262 /* Allocate thread-local work arrays */
1263 int nthreads = gmx_omp_nthreads_get(emntSETTLE);
1264 if (nthreads > 1 && constr->vir_r_m_dr_th == NULL)
1266 snew(constr->vir_r_m_dr_th, nthreads);
1267 snew(constr->bSettleErrorHasOccurred, nthreads);
1271 if (nconstraints + nsettles > 0 && ir->epc == epcMTTK)
1273 gmx_fatal(FARGS, "Constraints are not implemented with MTTK pressure control.");
1276 constr->maxwarn = 999;
1277 char *env = getenv("GMX_MAXCONSTRWARN");
1280 constr->maxwarn = 0;
1281 sscanf(env, "%8d", &constr->maxwarn);
1282 if (constr->maxwarn < 0)
1284 constr->maxwarn = INT_MAX;
1289 "Setting the maximum number of constraint warnings to %d\n",
1295 "Setting the maximum number of constraint warnings to %d\n",
1299 constr->warncount_lincs = 0;
1300 constr->warncount_settle = 0;
1302 /* Initialize the essential dynamics sampling.
1303 * Put the pointer to the ED struct in constr */
1305 if (ed != NULL || state->edsamstate != NULL)
1307 init_edsam(mtop, ir, cr, ed, as_rvec_array(state->x.data()), state->box, state->edsamstate);
1310 constr->warn_mtop = mtop;
1315 const t_blocka *atom2constraints_moltype(gmx_constr_t constr)
1317 return constr->at2con_mt;
1320 const int **atom2settle_moltype(gmx_constr_t constr)
1322 return (const int **)constr->at2settle_mt;
1326 gmx_bool inter_charge_group_constraints(const gmx_mtop_t *mtop)
1328 const gmx_moltype_t *molt;
1332 int *at2cg, cg, a, ftype, i;
1336 for (mb = 0; mb < mtop->nmolblock && !bInterCG; mb++)
1338 molt = &mtop->moltype[mtop->molblock[mb].type];
1340 if (molt->ilist[F_CONSTR].nr > 0 ||
1341 molt->ilist[F_CONSTRNC].nr > 0 ||
1342 molt->ilist[F_SETTLE].nr > 0)
1345 snew(at2cg, molt->atoms.nr);
1346 for (cg = 0; cg < cgs->nr; cg++)
1348 for (a = cgs->index[cg]; a < cgs->index[cg+1]; a++)
1354 for (ftype = F_CONSTR; ftype <= F_CONSTRNC; ftype++)
1356 il = &molt->ilist[ftype];
1357 for (i = 0; i < il->nr && !bInterCG; i += 1+NRAL(ftype))
1359 if (at2cg[il->iatoms[i+1]] != at2cg[il->iatoms[i+2]])
1373 gmx_bool inter_charge_group_settles(const gmx_mtop_t *mtop)
1375 const gmx_moltype_t *molt;
1379 int *at2cg, cg, a, ftype, i;
1383 for (mb = 0; mb < mtop->nmolblock && !bInterCG; mb++)
1385 molt = &mtop->moltype[mtop->molblock[mb].type];
1387 if (molt->ilist[F_SETTLE].nr > 0)
1390 snew(at2cg, molt->atoms.nr);
1391 for (cg = 0; cg < cgs->nr; cg++)
1393 for (a = cgs->index[cg]; a < cgs->index[cg+1]; a++)
1399 for (ftype = F_SETTLE; ftype <= F_SETTLE; ftype++)
1401 il = &molt->ilist[ftype];
1402 for (i = 0; i < il->nr && !bInterCG; i += 1+NRAL(F_SETTLE))
1404 if (at2cg[il->iatoms[i+1]] != at2cg[il->iatoms[i+2]] ||
1405 at2cg[il->iatoms[i+1]] != at2cg[il->iatoms[i+3]])
1419 /* helper functions for andersen temperature control, because the
1420 * gmx_constr construct is only defined in constr.c. Return the list
1421 * of blocks (get_sblock) and the number of blocks (get_nblocks). */
1423 extern int *get_sblock(struct gmx_constr *constr)
1425 return constr->sblock;
1428 extern int get_nblocks(struct gmx_constr *constr)
1430 return constr->nblocks;