Remove use of where(), if DEBUG etc.
[gromacs.git] / src / gromacs / mdlib / constr.cpp
blob819310c7c195320ffd7583d2f3754558c4b98e02
1 /*
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.
37 #include "gmxpre.h"
39 #include "constr.h"
41 #include <assert.h>
42 #include <stdlib.h>
44 #include <cmath>
46 #include <algorithm>
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/lincs.h"
59 #include "gromacs/mdlib/mdrun.h"
60 #include "gromacs/mdlib/settle.h"
61 #include "gromacs/mdlib/shake.h"
62 #include "gromacs/mdtypes/commrec.h"
63 #include "gromacs/mdtypes/inputrec.h"
64 #include "gromacs/mdtypes/md_enums.h"
65 #include "gromacs/mdtypes/mdatom.h"
66 #include "gromacs/mdtypes/state.h"
67 #include "gromacs/pbcutil/pbc.h"
68 #include "gromacs/pulling/pull.h"
69 #include "gromacs/topology/block.h"
70 #include "gromacs/topology/ifunc.h"
71 #include "gromacs/topology/mtop_lookup.h"
72 #include "gromacs/topology/mtop_util.h"
73 #include "gromacs/utility/exceptions.h"
74 #include "gromacs/utility/fatalerror.h"
75 #include "gromacs/utility/pleasecite.h"
76 #include "gromacs/utility/smalloc.h"
77 #include "gromacs/utility/txtdump.h"
79 typedef struct gmx_constr {
80 int ncon_tot; /* The total number of constraints */
81 int nflexcon; /* The number of flexible constraints */
82 int n_at2con_mt; /* The size of at2con = #moltypes */
83 t_blocka *at2con_mt; /* A list of atoms to constraints */
84 int n_at2settle_mt; /* The size of at2settle = #moltypes */
85 int **at2settle_mt; /* A list of atoms to settles */
86 gmx_bool bInterCGsettles;
87 gmx_lincsdata_t lincsd; /* LINCS data */
88 gmx_shakedata_t shaked; /* SHAKE data */
89 gmx_settledata_t settled; /* SETTLE data */
90 int maxwarn; /* The maximum number of warnings */
91 int warncount_lincs;
92 int warncount_settle;
93 gmx_edsam_t ed; /* The essential dynamics data */
95 /* Thread local working data */
96 tensor *vir_r_m_dr_th; /* Thread virial contribution */
97 bool *bSettleErrorHasOccurred; /* Did a settle error occur? */
99 /* Only used for printing warnings */
100 const gmx_mtop_t *warn_mtop; /* Pointer to the global topology */
101 } t_gmx_constr;
103 int n_flexible_constraints(struct gmx_constr *constr)
105 int nflexcon;
107 if (constr)
109 nflexcon = constr->nflexcon;
111 else
113 nflexcon = 0;
116 return nflexcon;
119 static void clear_constraint_quantity_nonlocal(gmx_domdec_t *dd, rvec *q)
121 int nonlocal_at_start, nonlocal_at_end, at;
123 dd_get_constraint_range(dd, &nonlocal_at_start, &nonlocal_at_end);
125 for (at = nonlocal_at_start; at < nonlocal_at_end; at++)
127 clear_rvec(q[at]);
131 void too_many_constraint_warnings(int eConstrAlg, int warncount)
133 gmx_fatal(FARGS,
134 "Too many %s warnings (%d)\n"
135 "If you know what you are doing you can %s"
136 "set the environment variable GMX_MAXCONSTRWARN to -1,\n"
137 "but normally it is better to fix the problem",
138 (eConstrAlg == econtLINCS) ? "LINCS" : "SETTLE", warncount,
139 (eConstrAlg == econtLINCS) ?
140 "adjust the lincs warning threshold in your mdp file\nor " : "\n");
143 static void write_constr_pdb(const char *fn, const char *title,
144 const gmx_mtop_t *mtop,
145 int start, int homenr, const t_commrec *cr,
146 rvec x[], matrix box)
148 char fname[STRLEN];
149 FILE *out;
150 int dd_ac0 = 0, dd_ac1 = 0, i, ii, resnr;
151 gmx_domdec_t *dd;
152 const char *anm, *resnm;
154 dd = nullptr;
155 if (DOMAINDECOMP(cr))
157 dd = cr->dd;
158 dd_get_constraint_range(dd, &dd_ac0, &dd_ac1);
159 start = 0;
160 homenr = dd_ac1;
163 if (PAR(cr))
165 sprintf(fname, "%s_n%d.pdb", fn, cr->sim_nodeid);
167 else
169 sprintf(fname, "%s.pdb", fn);
172 out = gmx_fio_fopen(fname, "w");
174 fprintf(out, "TITLE %s\n", title);
175 gmx_write_pdb_box(out, -1, box);
176 int molb = 0;
177 for (i = start; i < start+homenr; i++)
179 if (dd != nullptr)
181 if (i >= dd->nat_home && i < dd_ac0)
183 continue;
185 ii = dd->gatindex[i];
187 else
189 ii = i;
191 mtopGetAtomAndResidueName(mtop, ii, &molb, &anm, &resnr, &resnm, nullptr);
192 gmx_fprintf_pdb_atomline(out, epdbATOM, ii+1, anm, ' ', resnm, ' ', resnr, ' ',
193 10*x[i][XX], 10*x[i][YY], 10*x[i][ZZ], 1.0, 0.0, "");
195 fprintf(out, "TER\n");
197 gmx_fio_fclose(out);
200 static void dump_confs(FILE *fplog, gmx_int64_t step, const gmx_mtop_t *mtop,
201 int start, int homenr, const t_commrec *cr,
202 rvec x[], rvec xprime[], matrix box)
204 char buf[STRLEN], buf2[22];
206 char *env = getenv("GMX_SUPPRESS_DUMP");
207 if (env)
209 return;
212 sprintf(buf, "step%sb", gmx_step_str(step, buf2));
213 write_constr_pdb(buf, "initial coordinates",
214 mtop, start, homenr, cr, x, box);
215 sprintf(buf, "step%sc", gmx_step_str(step, buf2));
216 write_constr_pdb(buf, "coordinates after constraining",
217 mtop, start, homenr, cr, xprime, box);
218 if (fplog)
220 fprintf(fplog, "Wrote pdb files with previous and current coordinates\n");
222 fprintf(stderr, "Wrote pdb files with previous and current coordinates\n");
225 gmx_bool constrain(FILE *fplog, gmx_bool bLog, gmx_bool bEner,
226 struct gmx_constr *constr,
227 t_idef *idef, const t_inputrec *ir,
228 const t_commrec *cr,
229 const gmx_multisim_t *ms,
230 gmx_int64_t step, int delta_step,
231 real step_scaling,
232 t_mdatoms *md,
233 rvec *x, rvec *xprime, rvec *min_proj,
234 gmx_bool bMolPBC, matrix box,
235 real lambda, real *dvdlambda,
236 rvec *v, tensor *vir,
237 t_nrnb *nrnb, int econq)
239 gmx_bool bOK, bDump;
240 int start, homenr;
241 tensor vir_r_m_dr;
242 real scaled_delta_t;
243 real invdt, vir_fac = 0, t;
244 t_ilist *settle;
245 int nsettle;
246 t_pbc pbc, *pbc_null;
247 char buf[22];
248 int nth, th;
250 if (econq == econqForceDispl && !EI_ENERGY_MINIMIZATION(ir->eI))
252 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");
255 bOK = TRUE;
256 bDump = FALSE;
258 start = 0;
259 homenr = md->homenr;
261 scaled_delta_t = step_scaling * ir->delta_t;
263 /* Prepare time step for use in constraint implementations, and
264 avoid generating inf when ir->delta_t = 0. */
265 if (ir->delta_t == 0)
267 invdt = 0.0;
269 else
271 invdt = 1.0/scaled_delta_t;
274 if (ir->efep != efepNO && EI_DYNAMICS(ir->eI))
276 /* Set the constraint lengths for the step at which this configuration
277 * is meant to be. The invmasses should not be changed.
279 lambda += delta_step*ir->fepvals->delta_lambda;
282 if (vir != nullptr)
284 clear_mat(vir_r_m_dr);
287 settle = &idef->il[F_SETTLE];
288 nsettle = settle->nr/(1+NRAL(F_SETTLE));
290 if (nsettle > 0)
292 nth = gmx_omp_nthreads_get(emntSETTLE);
294 else
296 nth = 1;
299 /* We do not need full pbc when constraints do not cross charge groups,
300 * i.e. when dd->constraint_comm==NULL.
301 * Note that PBC for constraints is different from PBC for bondeds.
302 * For constraints there is both forward and backward communication.
304 if (ir->ePBC != epbcNONE &&
305 (cr->dd || bMolPBC) && !(cr->dd && cr->dd->constraint_comm == nullptr))
307 /* With pbc=screw the screw has been changed to a shift
308 * by the constraint coordinate communication routine,
309 * so that here we can use normal pbc.
311 pbc_null = set_pbc_dd(&pbc, ir->ePBC,
312 DOMAINDECOMP(cr) ? cr->dd->nc : nullptr,
313 FALSE, box);
315 else
317 pbc_null = nullptr;
320 /* Communicate the coordinates required for the non-local constraints
321 * for LINCS and/or SETTLE.
323 if (cr->dd)
325 dd_move_x_constraints(cr->dd, box, x, xprime, econq == econqCoord);
327 if (v != nullptr)
329 /* We need to initialize the non-local components of v.
330 * We never actually use these values, but we do increment them,
331 * so we should avoid uninitialized variables and overflows.
333 clear_constraint_quantity_nonlocal(cr->dd, v);
337 if (constr->lincsd != nullptr)
339 bOK = constrain_lincs(fplog, bLog, bEner, ir, step, constr->lincsd, md, cr, ms,
340 x, xprime, min_proj,
341 box, pbc_null, lambda, dvdlambda,
342 invdt, v, vir != nullptr, vir_r_m_dr,
343 econq, nrnb,
344 constr->maxwarn, &constr->warncount_lincs);
345 if (!bOK && constr->maxwarn < INT_MAX)
347 if (fplog != nullptr)
349 fprintf(fplog, "Constraint error in algorithm %s at step %s\n",
350 econstr_names[econtLINCS], gmx_step_str(step, buf));
352 bDump = TRUE;
356 if (constr->shaked != nullptr)
358 bOK = constrain_shake(fplog, constr->shaked,
359 md->invmass,
360 idef, ir, x, xprime, min_proj, nrnb,
361 lambda, dvdlambda,
362 invdt, v, vir != nullptr, vir_r_m_dr,
363 constr->maxwarn < INT_MAX, econq);
365 if (!bOK && constr->maxwarn < INT_MAX)
367 if (fplog != nullptr)
369 fprintf(fplog, "Constraint error in algorithm %s at step %s\n",
370 econstr_names[econtSHAKE], gmx_step_str(step, buf));
372 bDump = TRUE;
376 if (nsettle > 0)
378 bool bSettleErrorHasOccurred = false;
380 switch (econq)
382 case econqCoord:
383 #pragma omp parallel for num_threads(nth) schedule(static)
384 for (th = 0; th < nth; th++)
388 if (th > 0)
390 clear_mat(constr->vir_r_m_dr_th[th]);
393 csettle(constr->settled,
394 nth, th,
395 pbc_null,
396 x[0], xprime[0],
397 invdt, v ? v[0] : nullptr,
398 vir != nullptr,
399 th == 0 ? vir_r_m_dr : constr->vir_r_m_dr_th[th],
400 th == 0 ? &bSettleErrorHasOccurred : &constr->bSettleErrorHasOccurred[th]);
402 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
404 inc_nrnb(nrnb, eNR_SETTLE, nsettle);
405 if (v != nullptr)
407 inc_nrnb(nrnb, eNR_CONSTR_V, nsettle*3);
409 if (vir != nullptr)
411 inc_nrnb(nrnb, eNR_CONSTR_VIR, nsettle*3);
413 break;
414 case econqVeloc:
415 case econqDeriv:
416 case econqForce:
417 case econqForceDispl:
418 #pragma omp parallel for num_threads(nth) schedule(static)
419 for (th = 0; th < nth; th++)
423 int calcvir_atom_end;
425 if (vir == nullptr)
427 calcvir_atom_end = 0;
429 else
431 calcvir_atom_end = md->homenr;
434 if (th > 0)
436 clear_mat(constr->vir_r_m_dr_th[th]);
439 int start_th = (nsettle* th )/nth;
440 int end_th = (nsettle*(th+1))/nth;
442 if (start_th >= 0 && end_th - start_th > 0)
444 settle_proj(constr->settled, econq,
445 end_th-start_th,
446 settle->iatoms+start_th*(1+NRAL(F_SETTLE)),
447 pbc_null,
449 xprime, min_proj, calcvir_atom_end,
450 th == 0 ? vir_r_m_dr : constr->vir_r_m_dr_th[th]);
453 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
455 /* This is an overestimate */
456 inc_nrnb(nrnb, eNR_SETTLE, nsettle);
457 break;
458 case econqDeriv_FlexCon:
459 /* Nothing to do, since the are no flexible constraints in settles */
460 break;
461 default:
462 gmx_incons("Unknown constraint quantity for settle");
465 if (vir != nullptr)
467 /* Reduce the virial contributions over the threads */
468 for (int th = 1; th < nth; th++)
470 m_add(vir_r_m_dr, constr->vir_r_m_dr_th[th], vir_r_m_dr);
474 if (econq == econqCoord)
476 for (int th = 1; th < nth; th++)
478 bSettleErrorHasOccurred = bSettleErrorHasOccurred || constr->bSettleErrorHasOccurred[th];
481 if (bSettleErrorHasOccurred)
483 char buf[STRLEN];
484 sprintf(buf,
485 "\nstep " "%" GMX_PRId64 ": One or more water molecules can not be settled.\n"
486 "Check for bad contacts and/or reduce the timestep if appropriate.\n",
487 step);
488 if (fplog)
490 fprintf(fplog, "%s", buf);
492 fprintf(stderr, "%s", buf);
493 constr->warncount_settle++;
494 if (constr->warncount_settle > constr->maxwarn)
496 too_many_constraint_warnings(-1, constr->warncount_settle);
498 bDump = TRUE;
500 bOK = FALSE;
505 if (vir != nullptr)
507 /* The normal uses of constrain() pass step_scaling = 1.0.
508 * The call to constrain() for SD1 that passes step_scaling =
509 * 0.5 also passes vir = NULL, so cannot reach this
510 * assertion. This assertion should remain until someone knows
511 * that this path works for their intended purpose, and then
512 * they can use scaled_delta_t instead of ir->delta_t
513 * below. */
514 assert(gmx_within_tol(step_scaling, 1.0, GMX_REAL_EPS));
515 switch (econq)
517 case econqCoord:
518 vir_fac = 0.5/(ir->delta_t*ir->delta_t);
519 break;
520 case econqVeloc:
521 vir_fac = 0.5/ir->delta_t;
522 break;
523 case econqForce:
524 case econqForceDispl:
525 vir_fac = 0.5;
526 break;
527 default:
528 gmx_incons("Unsupported constraint quantity for virial");
531 if (EI_VV(ir->eI))
533 vir_fac *= 2; /* only constraining over half the distance here */
535 for (int i = 0; i < DIM; i++)
537 for (int j = 0; j < DIM; j++)
539 (*vir)[i][j] = vir_fac*vir_r_m_dr[i][j];
544 if (bDump)
546 dump_confs(fplog, step, constr->warn_mtop, start, homenr, cr, x, xprime, box);
549 if (econq == econqCoord)
551 if (ir->bPull && pull_have_constraint(ir->pull_work))
553 if (EI_DYNAMICS(ir->eI))
555 t = ir->init_t + (step + delta_step)*ir->delta_t;
557 else
559 t = ir->init_t;
561 set_pbc(&pbc, ir->ePBC, box);
562 pull_constraint(ir->pull_work, md, &pbc, cr, ir->delta_t, t, x, xprime, v, *vir);
564 if (constr->ed && delta_step > 0)
566 /* apply the essential dynamics constraints here */
567 do_edsam(ir, step, cr, xprime, v, box, constr->ed);
571 return bOK;
574 real *constr_rmsd_data(struct gmx_constr *constr)
576 if (constr->lincsd)
578 return lincs_rmsd_data(constr->lincsd);
580 else
582 return nullptr;
586 real constr_rmsd(struct gmx_constr *constr)
588 if (constr->lincsd)
590 return lincs_rmsd(constr->lincsd);
592 else
594 return 0;
598 t_blocka make_at2con(int start, int natoms,
599 const t_ilist *ilist, const t_iparams *iparams,
600 gmx_bool bDynamics, int *nflexiblecons)
602 int *count, ncon, con, con_tot, nflexcon, ftype, i, a;
603 t_iatom *ia;
604 t_blocka at2con;
605 gmx_bool bFlexCon;
607 snew(count, natoms);
608 nflexcon = 0;
609 for (ftype = F_CONSTR; ftype <= F_CONSTRNC; ftype++)
611 ncon = ilist[ftype].nr/3;
612 ia = ilist[ftype].iatoms;
613 for (con = 0; con < ncon; con++)
615 bFlexCon = (iparams[ia[0]].constr.dA == 0 &&
616 iparams[ia[0]].constr.dB == 0);
617 if (bFlexCon)
619 nflexcon++;
621 if (bDynamics || !bFlexCon)
623 for (i = 1; i < 3; i++)
625 a = ia[i] - start;
626 count[a]++;
629 ia += 3;
632 *nflexiblecons = nflexcon;
634 at2con.nr = natoms;
635 at2con.nalloc_index = at2con.nr+1;
636 snew(at2con.index, at2con.nalloc_index);
637 at2con.index[0] = 0;
638 for (a = 0; a < natoms; a++)
640 at2con.index[a+1] = at2con.index[a] + count[a];
641 count[a] = 0;
643 at2con.nra = at2con.index[natoms];
644 at2con.nalloc_a = at2con.nra;
645 snew(at2con.a, at2con.nalloc_a);
647 /* The F_CONSTRNC constraints have constraint numbers
648 * that continue after the last F_CONSTR constraint.
650 con_tot = 0;
651 for (ftype = F_CONSTR; ftype <= F_CONSTRNC; ftype++)
653 ncon = ilist[ftype].nr/3;
654 ia = ilist[ftype].iatoms;
655 for (con = 0; con < ncon; con++)
657 bFlexCon = (iparams[ia[0]].constr.dA == 0 &&
658 iparams[ia[0]].constr.dB == 0);
659 if (bDynamics || !bFlexCon)
661 for (i = 1; i < 3; i++)
663 a = ia[i] - start;
664 at2con.a[at2con.index[a]+count[a]++] = con_tot;
667 con_tot++;
668 ia += 3;
672 sfree(count);
674 return at2con;
677 static int *make_at2settle(int natoms, const t_ilist *ilist)
679 int *at2s;
680 int a, stride, s;
682 snew(at2s, natoms);
683 /* Set all to no settle */
684 for (a = 0; a < natoms; a++)
686 at2s[a] = -1;
689 stride = 1 + NRAL(F_SETTLE);
691 for (s = 0; s < ilist->nr; s += stride)
693 at2s[ilist->iatoms[s+1]] = s/stride;
694 at2s[ilist->iatoms[s+2]] = s/stride;
695 at2s[ilist->iatoms[s+3]] = s/stride;
698 return at2s;
701 void set_constraints(struct gmx_constr *constr,
702 gmx_localtop_t *top, const t_inputrec *ir,
703 const t_mdatoms *md, const t_commrec *cr)
705 t_idef *idef = &top->idef;
707 if (constr->ncon_tot > 0)
709 /* With DD we might also need to call LINCS on a domain no constraints for
710 * communicating coordinates to other nodes that do have constraints.
712 if (ir->eConstrAlg == econtLINCS)
714 set_lincs(idef, md, EI_DYNAMICS(ir->eI), cr, constr->lincsd);
716 if (ir->eConstrAlg == econtSHAKE)
718 if (cr->dd)
720 // We are using the local topology, so there are only
721 // F_CONSTR constraints.
722 make_shake_sblock_dd(constr->shaked, &idef->il[F_CONSTR], &top->cgs, cr->dd);
724 else
726 make_shake_sblock_serial(constr->shaked, idef, md);
731 if (constr->settled)
733 settle_set_constraints(constr->settled,
734 &idef->il[F_SETTLE], md);
737 /* Make a selection of the local atoms for essential dynamics */
738 if (constr->ed && cr->dd)
740 dd_make_local_ed_indices(cr->dd, constr->ed);
744 gmx_constr_t init_constraints(FILE *fplog,
745 const gmx_mtop_t *mtop, const t_inputrec *ir,
746 bool doEssentialDynamics,
747 const t_commrec *cr)
749 int nconstraints =
750 gmx_mtop_ftype_count(mtop, F_CONSTR) +
751 gmx_mtop_ftype_count(mtop, F_CONSTRNC);
752 int nsettles =
753 gmx_mtop_ftype_count(mtop, F_SETTLE);
755 GMX_RELEASE_ASSERT(!ir->bPull || ir->pull_work != nullptr, "init_constraints called with COM pulling before/without initializing the pull code");
757 if (nconstraints + nsettles == 0 &&
758 !(ir->bPull && pull_have_constraint(ir->pull_work)) &&
759 !doEssentialDynamics)
761 return nullptr;
764 struct gmx_constr *constr;
766 snew(constr, 1);
768 constr->ncon_tot = nconstraints;
769 constr->nflexcon = 0;
770 if (nconstraints > 0)
772 constr->n_at2con_mt = mtop->moltype.size();
773 snew(constr->at2con_mt, constr->n_at2con_mt);
774 for (int mt = 0; mt < static_cast<int>(mtop->moltype.size()); mt++)
776 int nflexcon;
777 constr->at2con_mt[mt] = make_at2con(0, mtop->moltype[mt].atoms.nr,
778 mtop->moltype[mt].ilist,
779 mtop->ffparams.iparams,
780 EI_DYNAMICS(ir->eI), &nflexcon);
781 for (const gmx_molblock_t &molblock : mtop->molblock)
783 if (molblock.type == mt)
785 constr->nflexcon += molblock.nmol*nflexcon;
790 if (constr->nflexcon > 0)
792 if (fplog)
794 fprintf(fplog, "There are %d flexible constraints\n",
795 constr->nflexcon);
796 if (ir->fc_stepsize == 0)
798 fprintf(fplog, "\n"
799 "WARNING: step size for flexible constraining = 0\n"
800 " All flexible constraints will be rigid.\n"
801 " Will try to keep all flexible constraints at their original length,\n"
802 " but the lengths may exhibit some drift.\n\n");
803 constr->nflexcon = 0;
806 if (constr->nflexcon > 0)
808 please_cite(fplog, "Hess2002");
812 if (ir->eConstrAlg == econtLINCS)
814 constr->lincsd = init_lincs(fplog, mtop,
815 constr->nflexcon, constr->at2con_mt,
816 DOMAINDECOMP(cr) && cr->dd->bInterCGcons,
817 ir->nLincsIter, ir->nProjOrder);
820 if (ir->eConstrAlg == econtSHAKE)
822 if (DOMAINDECOMP(cr) && cr->dd->bInterCGcons)
824 gmx_fatal(FARGS, "SHAKE is not supported with domain decomposition and constraint that cross charge group boundaries, use LINCS");
826 if (constr->nflexcon)
828 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");
830 please_cite(fplog, "Ryckaert77a");
831 if (ir->bShakeSOR)
833 please_cite(fplog, "Barth95a");
836 constr->shaked = shake_init();
840 if (nsettles > 0)
842 please_cite(fplog, "Miyamoto92a");
844 constr->bInterCGsettles = inter_charge_group_settles(mtop);
846 constr->settled = settle_init(mtop);
848 /* Make an atom to settle index for use in domain decomposition */
849 constr->n_at2settle_mt = mtop->moltype.size();
850 snew(constr->at2settle_mt, constr->n_at2settle_mt);
851 for (size_t mt = 0; mt < mtop->moltype.size(); mt++)
853 constr->at2settle_mt[mt] =
854 make_at2settle(mtop->moltype[mt].atoms.nr,
855 &mtop->moltype[mt].ilist[F_SETTLE]);
858 /* Allocate thread-local work arrays */
859 int nthreads = gmx_omp_nthreads_get(emntSETTLE);
860 if (nthreads > 1 && constr->vir_r_m_dr_th == nullptr)
862 snew(constr->vir_r_m_dr_th, nthreads);
863 snew(constr->bSettleErrorHasOccurred, nthreads);
867 if (nconstraints + nsettles > 0 && ir->epc == epcMTTK)
869 gmx_fatal(FARGS, "Constraints are not implemented with MTTK pressure control.");
872 constr->maxwarn = 999;
873 char *env = getenv("GMX_MAXCONSTRWARN");
874 if (env)
876 constr->maxwarn = 0;
877 sscanf(env, "%8d", &constr->maxwarn);
878 if (constr->maxwarn < 0)
880 constr->maxwarn = INT_MAX;
882 if (fplog)
884 fprintf(fplog,
885 "Setting the maximum number of constraint warnings to %d\n",
886 constr->maxwarn);
888 if (MASTER(cr))
890 fprintf(stderr,
891 "Setting the maximum number of constraint warnings to %d\n",
892 constr->maxwarn);
895 constr->warncount_lincs = 0;
896 constr->warncount_settle = 0;
898 constr->warn_mtop = mtop;
900 return constr;
903 /* Put a pointer to the essential dynamics constraints into the constr struct */
904 void saveEdsamPointer(gmx_constr_t constr, gmx_edsam_t ed)
906 constr->ed = ed;
909 const t_blocka *atom2constraints_moltype(gmx_constr_t constr)
911 return constr->at2con_mt;
914 const int **atom2settle_moltype(gmx_constr_t constr)
916 return (const int **)constr->at2settle_mt;
920 gmx_bool inter_charge_group_constraints(const gmx_mtop_t *mtop)
922 const gmx_moltype_t *molt;
923 const t_block *cgs;
924 const t_ilist *il;
925 int *at2cg, cg, a, ftype, i;
926 gmx_bool bInterCG;
928 bInterCG = FALSE;
929 for (size_t mb = 0; mb < mtop->molblock.size() && !bInterCG; mb++)
931 molt = &mtop->moltype[mtop->molblock[mb].type];
933 if (molt->ilist[F_CONSTR].nr > 0 ||
934 molt->ilist[F_CONSTRNC].nr > 0 ||
935 molt->ilist[F_SETTLE].nr > 0)
937 cgs = &molt->cgs;
938 snew(at2cg, molt->atoms.nr);
939 for (cg = 0; cg < cgs->nr; cg++)
941 for (a = cgs->index[cg]; a < cgs->index[cg+1]; a++)
943 at2cg[a] = cg;
947 for (ftype = F_CONSTR; ftype <= F_CONSTRNC; ftype++)
949 il = &molt->ilist[ftype];
950 for (i = 0; i < il->nr && !bInterCG; i += 1+NRAL(ftype))
952 if (at2cg[il->iatoms[i+1]] != at2cg[il->iatoms[i+2]])
954 bInterCG = TRUE;
959 sfree(at2cg);
963 return bInterCG;
966 gmx_bool inter_charge_group_settles(const gmx_mtop_t *mtop)
968 const gmx_moltype_t *molt;
969 const t_block *cgs;
970 const t_ilist *il;
971 int *at2cg, cg, a, ftype, i;
972 gmx_bool bInterCG;
974 bInterCG = FALSE;
975 for (size_t mb = 0; mb < mtop->molblock.size() && !bInterCG; mb++)
977 molt = &mtop->moltype[mtop->molblock[mb].type];
979 if (molt->ilist[F_SETTLE].nr > 0)
981 cgs = &molt->cgs;
982 snew(at2cg, molt->atoms.nr);
983 for (cg = 0; cg < cgs->nr; cg++)
985 for (a = cgs->index[cg]; a < cgs->index[cg+1]; a++)
987 at2cg[a] = cg;
991 for (ftype = F_SETTLE; ftype <= F_SETTLE; ftype++)
993 il = &molt->ilist[ftype];
994 for (i = 0; i < il->nr && !bInterCG; i += 1+NRAL(F_SETTLE))
996 if (at2cg[il->iatoms[i+1]] != at2cg[il->iatoms[i+2]] ||
997 at2cg[il->iatoms[i+1]] != at2cg[il->iatoms[i+3]])
999 bInterCG = TRUE;
1004 sfree(at2cg);
1008 return bInterCG;