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,2019, 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.
38 * \brief Defines the high-level constraint code.
40 * \author Berk Hess <hess@kth.se>
41 * \author Mark Abraham <mark.j.abraham@gmail.com>
42 * \ingroup module_mdlib
54 #include "gromacs/domdec/domdec.h"
55 #include "gromacs/domdec/domdec_struct.h"
56 #include "gromacs/essentialdynamics/edsam.h"
57 #include "gromacs/fileio/confio.h"
58 #include "gromacs/fileio/gmxfio.h"
59 #include "gromacs/fileio/pdbio.h"
60 #include "gromacs/gmxlib/nrnb.h"
61 #include "gromacs/math/utilities.h"
62 #include "gromacs/math/vec.h"
63 #include "gromacs/mdlib/gmx_omp_nthreads.h"
64 #include "gromacs/mdlib/lincs.h"
65 #include "gromacs/mdlib/settle.h"
66 #include "gromacs/mdlib/shake.h"
67 #include "gromacs/mdtypes/commrec.h"
68 #include "gromacs/mdtypes/inputrec.h"
69 #include "gromacs/mdtypes/md_enums.h"
70 #include "gromacs/mdtypes/mdatom.h"
71 #include "gromacs/mdtypes/state.h"
72 #include "gromacs/pbcutil/pbc.h"
73 #include "gromacs/pulling/pull.h"
74 #include "gromacs/timing/wallcycle.h"
75 #include "gromacs/topology/block.h"
76 #include "gromacs/topology/ifunc.h"
77 #include "gromacs/topology/mtop_lookup.h"
78 #include "gromacs/topology/mtop_util.h"
79 #include "gromacs/utility/arrayref.h"
80 #include "gromacs/utility/exceptions.h"
81 #include "gromacs/utility/fatalerror.h"
82 #include "gromacs/utility/gmxassert.h"
83 #include "gromacs/utility/pleasecite.h"
84 #include "gromacs/utility/smalloc.h"
85 #include "gromacs/utility/txtdump.h"
90 /* \brief Impl class for Constraints
92 * \todo Members like md, idef are valid only for the lifetime of a
93 * domain, which would be good to make clearer in the structure of the
94 * code. It should not be possible to call apply() if setConstraints()
95 * has not been called. For example, this could be achieved if
96 * setConstraints returned a valid object with such a method. That
97 * still requires that we manage the lifetime of that object
98 * correctly, however. */
99 class Constraints::Impl
102 Impl(const gmx_mtop_t &mtop_p,
103 const t_inputrec &ir_p,
106 const t_mdatoms &md_p,
107 const t_commrec *cr_p,
108 const gmx_multisim_t *ms,
110 gmx_wallcycle *wcycle_p,
111 bool pbcHandlingRequired,
115 void setConstraints(const gmx_localtop_t &top,
116 const t_mdatoms &md);
117 bool apply(bool bLog,
130 ConstraintVariable econq);
131 //! The total number of constraints.
133 //! The number of flexible constraints.
135 //! A list of atoms to constraints for each moleculetype.
136 std::vector<t_blocka> at2con_mt;
137 //! A list of atoms to settles for each moleculetype
138 std::vector < std::vector < int>> at2settle_mt;
140 Lincs *lincsd = nullptr; // TODO this should become a unique_ptr
142 shakedata *shaked = nullptr;
144 settledata *settled = nullptr;
145 //! The maximum number of warnings.
147 //! The number of warnings for LINCS.
148 int warncount_lincs = 0;
149 //! The number of warnings for SETTLE.
150 int warncount_settle = 0;
151 //! The essential dynamics data.
152 gmx_edsam * ed = nullptr;
154 //! Thread-local virial contribution.
155 tensor *vir_r_m_dr_th = {nullptr};
156 //! Did a settle error occur?
157 bool *bSettleErrorHasOccurred = nullptr;
159 //! Pointer to the global topology - only used for printing warnings.
160 const gmx_mtop_t &mtop;
161 //! Parameters for the interactions in this domain.
162 const t_idef *idef = nullptr;
163 //! Data about atoms in this domain.
165 //! Whether we need to do pbc for handling bonds.
166 bool pbcHandlingRequired_ = false;
170 //! Communication support.
171 const t_commrec *cr = nullptr;
172 //! Multi-sim support.
173 const gmx_multisim_t *ms = nullptr;
174 //! Pulling code object, if any.
175 pull_t *pull_work = nullptr;
176 /*!\brief Input options.
178 * \todo Replace with IMdpOptions */
179 const t_inputrec &ir;
180 //! Flop counting support.
181 t_nrnb *nrnb = nullptr;
182 //! Tracks wallcycle usage.
183 gmx_wallcycle *wcycle;
186 Constraints::~Constraints() = default;
188 int Constraints::numFlexibleConstraints() const
190 return impl_->nflexcon;
193 bool Constraints::havePerturbedConstraints() const
195 const gmx_ffparams_t &ffparams = impl_->mtop.ffparams;
197 for (size_t i = 0; i < ffparams.functype.size(); i++)
199 if ((ffparams.functype[i] == F_CONSTR ||
200 ffparams.functype[i] == F_CONSTRNC) &&
201 ffparams.iparams[i].constr.dA != ffparams.iparams[i].constr.dB)
210 //! Clears constraint quantities for atoms in nonlocal region.
211 static void clear_constraint_quantity_nonlocal(gmx_domdec_t *dd, rvec *q)
213 int nonlocal_at_start, nonlocal_at_end, at;
215 dd_get_constraint_range(dd, &nonlocal_at_start, &nonlocal_at_end);
217 for (at = nonlocal_at_start; at < nonlocal_at_end; at++)
223 void too_many_constraint_warnings(int eConstrAlg, int warncount)
226 "Too many %s warnings (%d)\n"
227 "If you know what you are doing you can %s"
228 "set the environment variable GMX_MAXCONSTRWARN to -1,\n"
229 "but normally it is better to fix the problem",
230 (eConstrAlg == econtLINCS) ? "LINCS" : "SETTLE", warncount,
231 (eConstrAlg == econtLINCS) ?
232 "adjust the lincs warning threshold in your mdp file\nor " : "\n");
235 //! Writes out coordinates.
236 static void write_constr_pdb(const char *fn, const char *title,
237 const gmx_mtop_t &mtop,
238 int start, int homenr, const t_commrec *cr,
239 const rvec x[], const matrix box)
243 int dd_ac0 = 0, dd_ac1 = 0, i, ii, resnr;
245 const char *anm, *resnm;
248 if (DOMAINDECOMP(cr))
251 dd_get_constraint_range(dd, &dd_ac0, &dd_ac1);
258 sprintf(fname, "%s_n%d.pdb", fn, cr->sim_nodeid);
262 sprintf(fname, "%s.pdb", fn);
265 out = gmx_fio_fopen(fname, "w");
267 fprintf(out, "TITLE %s\n", title);
268 gmx_write_pdb_box(out, -1, box);
270 for (i = start; i < start+homenr; i++)
274 if (i >= dd_numHomeAtoms(*dd) && i < dd_ac0)
278 ii = dd->globalAtomIndices[i];
284 mtopGetAtomAndResidueName(mtop, ii, &molb, &anm, &resnr, &resnm, nullptr);
285 gmx_fprintf_pdb_atomline(out, epdbATOM, ii+1, anm, ' ', resnm, ' ', resnr, ' ',
286 10*x[i][XX], 10*x[i][YY], 10*x[i][ZZ], 1.0, 0.0, "");
288 fprintf(out, "TER\n");
293 //! Writes out domain contents to help diagnose crashes.
294 static void dump_confs(FILE *log, int64_t step, const gmx_mtop_t &mtop,
295 int start, int homenr, const t_commrec *cr,
296 const rvec x[], rvec xprime[], const matrix box)
298 char buf[STRLEN], buf2[22];
300 char *env = getenv("GMX_SUPPRESS_DUMP");
306 sprintf(buf, "step%sb", gmx_step_str(step, buf2));
307 write_constr_pdb(buf, "initial coordinates",
308 mtop, start, homenr, cr, x, box);
309 sprintf(buf, "step%sc", gmx_step_str(step, buf2));
310 write_constr_pdb(buf, "coordinates after constraining",
311 mtop, start, homenr, cr, xprime, box);
314 fprintf(log, "Wrote pdb files with previous and current coordinates\n");
316 fprintf(stderr, "Wrote pdb files with previous and current coordinates\n");
320 Constraints::apply(bool bLog,
333 ConstraintVariable econq)
335 return impl_->apply(bLog,
352 Constraints::Impl::apply(bool bLog,
365 ConstraintVariable econq)
371 real invdt, vir_fac = 0, t;
373 t_pbc pbc, *pbc_null;
377 wallcycle_start(wcycle, ewcCONSTR);
379 if (econq == ConstraintVariable::ForceDispl && !EI_ENERGY_MINIMIZATION(ir.eI))
381 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");
390 scaled_delta_t = step_scaling * ir.delta_t;
392 /* Prepare time step for use in constraint implementations, and
393 avoid generating inf when ir.delta_t = 0. */
400 invdt = 1.0/scaled_delta_t;
403 if (ir.efep != efepNO && EI_DYNAMICS(ir.eI))
405 /* Set the constraint lengths for the step at which this configuration
406 * is meant to be. The invmasses should not be changed.
408 lambda += delta_step*ir.fepvals->delta_lambda;
413 clear_mat(vir_r_m_dr);
415 const t_ilist *settle = &idef->il[F_SETTLE];
416 nsettle = settle->nr/(1+NRAL(F_SETTLE));
420 nth = gmx_omp_nthreads_get(emntSETTLE);
427 /* We do not need full pbc when constraints do not cross update groups
428 * i.e. when dd->constraint_comm==NULL.
429 * Note that PBC for constraints is different from PBC for bondeds.
430 * For constraints there is both forward and backward communication.
432 if (ir.ePBC != epbcNONE &&
433 (cr->dd || pbcHandlingRequired_) && !(cr->dd && cr->dd->constraint_comm == nullptr))
435 /* With pbc=screw the screw has been changed to a shift
436 * by the constraint coordinate communication routine,
437 * so that here we can use normal pbc.
439 pbc_null = set_pbc_dd(&pbc, ir.ePBC,
440 DOMAINDECOMP(cr) ? cr->dd->nc : nullptr,
448 /* Communicate the coordinates required for the non-local constraints
449 * for LINCS and/or SETTLE.
453 dd_move_x_constraints(cr->dd, box, x, xprime, econq == ConstraintVariable::Positions);
457 /* We need to initialize the non-local components of v.
458 * We never actually use these values, but we do increment them,
459 * so we should avoid uninitialized variables and overflows.
461 clear_constraint_quantity_nonlocal(cr->dd, v);
465 if (lincsd != nullptr)
467 bOK = constrain_lincs(bLog || bEner, ir, step, lincsd, md, cr, ms,
469 box, pbc_null, lambda, dvdlambda,
470 invdt, v, vir != nullptr, vir_r_m_dr,
472 maxwarn, &warncount_lincs);
473 if (!bOK && maxwarn < INT_MAX)
477 fprintf(log, "Constraint error in algorithm %s at step %s\n",
478 econstr_names[econtLINCS], gmx_step_str(step, buf));
484 if (shaked != nullptr)
486 bOK = constrain_shake(log, shaked,
488 *idef, ir, x, xprime, min_proj, nrnb,
490 invdt, v, vir != nullptr, vir_r_m_dr,
491 maxwarn < INT_MAX, econq);
493 if (!bOK && maxwarn < INT_MAX)
497 fprintf(log, "Constraint error in algorithm %s at step %s\n",
498 econstr_names[econtSHAKE], gmx_step_str(step, buf));
506 bool bSettleErrorHasOccurred0 = false;
510 case ConstraintVariable::Positions:
511 #pragma omp parallel for num_threads(nth) schedule(static)
512 for (int th = 0; th < nth; th++)
518 clear_mat(vir_r_m_dr_th[th]);
525 invdt, v ? v[0] : nullptr,
527 th == 0 ? vir_r_m_dr : vir_r_m_dr_th[th],
528 th == 0 ? &bSettleErrorHasOccurred0 : &bSettleErrorHasOccurred[th]);
530 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
532 inc_nrnb(nrnb, eNR_SETTLE, nsettle);
535 inc_nrnb(nrnb, eNR_CONSTR_V, nsettle*3);
539 inc_nrnb(nrnb, eNR_CONSTR_VIR, nsettle*3);
542 case ConstraintVariable::Velocities:
543 case ConstraintVariable::Derivative:
544 case ConstraintVariable::Force:
545 case ConstraintVariable::ForceDispl:
546 #pragma omp parallel for num_threads(nth) schedule(static)
547 for (int th = 0; th < nth; th++)
551 int calcvir_atom_end;
555 calcvir_atom_end = 0;
559 calcvir_atom_end = md.homenr;
564 clear_mat(vir_r_m_dr_th[th]);
567 int start_th = (nsettle* th )/nth;
568 int end_th = (nsettle*(th+1))/nth;
570 if (start_th >= 0 && end_th - start_th > 0)
572 settle_proj(settled, econq,
574 settle->iatoms+start_th*(1+NRAL(F_SETTLE)),
577 xprime, min_proj, calcvir_atom_end,
578 th == 0 ? vir_r_m_dr : vir_r_m_dr_th[th]);
581 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
583 /* This is an overestimate */
584 inc_nrnb(nrnb, eNR_SETTLE, nsettle);
586 case ConstraintVariable::Deriv_FlexCon:
587 /* Nothing to do, since the are no flexible constraints in settles */
590 gmx_incons("Unknown constraint quantity for settle");
595 /* Reduce the virial contributions over the threads */
596 for (int th = 1; th < nth; th++)
598 m_add(vir_r_m_dr, vir_r_m_dr_th[th], vir_r_m_dr);
602 if (econq == ConstraintVariable::Positions)
604 for (int th = 1; th < nth; th++)
606 bSettleErrorHasOccurred0 = bSettleErrorHasOccurred0 || bSettleErrorHasOccurred[th];
609 if (bSettleErrorHasOccurred0)
613 "\nstep " "%" PRId64 ": One or more water molecules can not be settled.\n"
614 "Check for bad contacts and/or reduce the timestep if appropriate.\n",
618 fprintf(log, "%s", buf);
620 fprintf(stderr, "%s", buf);
622 if (warncount_settle > maxwarn)
624 too_many_constraint_warnings(-1, warncount_settle);
635 /* The normal uses of constrain() pass step_scaling = 1.0.
636 * The call to constrain() for SD1 that passes step_scaling =
637 * 0.5 also passes vir = NULL, so cannot reach this
638 * assertion. This assertion should remain until someone knows
639 * that this path works for their intended purpose, and then
640 * they can use scaled_delta_t instead of ir.delta_t
642 assert(gmx_within_tol(step_scaling, 1.0, GMX_REAL_EPS));
645 case ConstraintVariable::Positions:
646 vir_fac = 0.5/(ir.delta_t*ir.delta_t);
648 case ConstraintVariable::Velocities:
649 vir_fac = 0.5/ir.delta_t;
651 case ConstraintVariable::Force:
652 case ConstraintVariable::ForceDispl:
656 gmx_incons("Unsupported constraint quantity for virial");
661 vir_fac *= 2; /* only constraining over half the distance here */
663 for (int i = 0; i < DIM; i++)
665 for (int j = 0; j < DIM; j++)
667 (*vir)[i][j] = vir_fac*vir_r_m_dr[i][j];
674 dump_confs(log, step, mtop, start, homenr, cr, x, xprime, box);
677 if (econq == ConstraintVariable::Positions)
679 if (ir.bPull && pull_have_constraint(pull_work))
681 if (EI_DYNAMICS(ir.eI))
683 t = ir.init_t + (step + delta_step)*ir.delta_t;
689 set_pbc(&pbc, ir.ePBC, box);
690 pull_constraint(pull_work, &md, &pbc, cr, ir.delta_t, t, x, xprime, v, *vir);
692 if (ed && delta_step > 0)
694 /* apply the essential dynamics constraints here */
695 do_edsam(&ir, step, cr, xprime, v, box, ed);
698 wallcycle_stop(wcycle, ewcCONSTR);
700 if (v != nullptr && md.cFREEZE)
702 /* Set the velocities of frozen dimensions to zero */
704 int gmx_unused numThreads = gmx_omp_nthreads_get(emntUpdate);
706 #pragma omp parallel for num_threads(numThreads) schedule(static)
707 for (int i = 0; i < md.homenr; i++)
709 int freezeGroup = md.cFREEZE[i];
711 for (int d = 0; d < DIM; d++)
713 if (ir.opts.nFreeze[freezeGroup][d])
724 ArrayRef<real> Constraints::rmsdData() const
728 return lincs_rmsdData(impl_->lincsd);
736 real Constraints::rmsd() const
740 return lincs_rmsd(impl_->lincsd);
748 int Constraints::numConstraintsTotal()
750 return impl_->ncon_tot;
753 FlexibleConstraintTreatment flexibleConstraintTreatment(bool haveDynamicsIntegrator)
755 if (haveDynamicsIntegrator)
757 return FlexibleConstraintTreatment::Include;
761 return FlexibleConstraintTreatment::Exclude;
765 /*! \brief Returns a block struct to go from atoms to constraints
767 * The block struct will contain constraint indices with lower indices
768 * directly matching the order in F_CONSTR and higher indices matching
769 * the order in F_CONSTRNC offset by the number of constraints in F_CONSTR.
771 * \param[in] numAtoms The number of atoms to construct the list for
772 * \param[in] ilists The interaction lists, size F_NRE
773 * \param[in] iparams Interaction parameters, can be null when flexibleConstraintTreatment=Include
774 * \param[in] flexibleConstraintTreatment The flexible constraint treatment, see enum above
775 * \returns a block struct with all constraints for each atom
777 template <typename T>
779 makeAtomsToConstraintsList(int numAtoms,
781 const t_iparams *iparams,
782 FlexibleConstraintTreatment flexibleConstraintTreatment)
784 GMX_ASSERT(flexibleConstraintTreatment == FlexibleConstraintTreatment::Include || iparams != nullptr, "With flexible constraint detection we need valid iparams");
786 std::vector<int> count(numAtoms);
788 for (int ftype = F_CONSTR; ftype <= F_CONSTRNC; ftype++)
790 const T &ilist = ilists[ftype];
791 const int stride = 1 + NRAL(ftype);
792 for (int i = 0; i < ilist.size(); i += stride)
794 if (flexibleConstraintTreatment == FlexibleConstraintTreatment::Include ||
795 !isConstraintFlexible(iparams, ilist.iatoms[i]))
797 for (int j = 1; j < 3; j++)
799 int a = ilist.iatoms[i + j];
807 at2con.nr = numAtoms;
808 at2con.nalloc_index = at2con.nr + 1;
809 snew(at2con.index, at2con.nalloc_index);
811 for (int a = 0; a < numAtoms; a++)
813 at2con.index[a + 1] = at2con.index[a] + count[a];
816 at2con.nra = at2con.index[at2con.nr];
817 at2con.nalloc_a = at2con.nra;
818 snew(at2con.a, at2con.nalloc_a);
820 /* The F_CONSTRNC constraints have constraint numbers
821 * that continue after the last F_CONSTR constraint.
823 int numConstraints = 0;
824 for (int ftype = F_CONSTR; ftype <= F_CONSTRNC; ftype++)
826 const T &ilist = ilists[ftype];
827 const int stride = 1 + NRAL(ftype);
828 for (int i = 0; i < ilist.size(); i += stride)
830 if (flexibleConstraintTreatment == FlexibleConstraintTreatment::Include ||
831 !isConstraintFlexible(iparams, ilist.iatoms[i]))
833 for (int j = 1; j < 3; j++)
835 int a = ilist.iatoms[i + j];
836 at2con.a[at2con.index[a] + count[a]++] = numConstraints;
846 t_blocka make_at2con(int numAtoms,
847 const t_ilist *ilist,
848 const t_iparams *iparams,
849 FlexibleConstraintTreatment flexibleConstraintTreatment)
851 return makeAtomsToConstraintsList(numAtoms, ilist, iparams, flexibleConstraintTreatment);
854 t_blocka make_at2con(const gmx_moltype_t &moltype,
855 gmx::ArrayRef<const t_iparams> iparams,
856 FlexibleConstraintTreatment flexibleConstraintTreatment)
858 return makeAtomsToConstraintsList(moltype.atoms.nr, moltype.ilist.data(), iparams.data(), flexibleConstraintTreatment);
861 //! Return the number of flexible constraints in the \c ilist and \c iparams.
862 template <typename T>
864 countFlexibleConstraintsTemplate(const T *ilist,
865 const t_iparams *iparams)
868 for (int ftype = F_CONSTR; ftype <= F_CONSTRNC; ftype++)
870 const int numIatomsPerConstraint = 3;
871 for (int i = 0; i < ilist[ftype].size(); i += numIatomsPerConstraint)
873 const int type = ilist[ftype].iatoms[i];
874 if (iparams[type].constr.dA == 0 &&
875 iparams[type].constr.dB == 0)
885 int countFlexibleConstraints(const t_ilist *ilist,
886 const t_iparams *iparams)
888 return countFlexibleConstraintsTemplate(ilist, iparams);
891 //! Returns the index of the settle to which each atom belongs.
892 static std::vector<int> make_at2settle(int natoms,
893 const InteractionList &ilist)
895 /* Set all to no settle */
896 std::vector<int> at2s(natoms, -1);
898 const int stride = 1 + NRAL(F_SETTLE);
900 for (int s = 0; s < ilist.size(); s += stride)
902 at2s[ilist.iatoms[s + 1]] = s/stride;
903 at2s[ilist.iatoms[s + 2]] = s/stride;
904 at2s[ilist.iatoms[s + 3]] = s/stride;
911 Constraints::Impl::setConstraints(const gmx_localtop_t &top,
918 /* With DD we might also need to call LINCS on a domain no constraints for
919 * communicating coordinates to other nodes that do have constraints.
921 if (ir.eConstrAlg == econtLINCS)
923 set_lincs(top.idef, md, EI_DYNAMICS(ir.eI), cr, lincsd);
925 if (ir.eConstrAlg == econtSHAKE)
929 // We are using the local topology, so there are only
930 // F_CONSTR constraints.
931 make_shake_sblock_dd(shaked, &idef->il[F_CONSTR], cr->dd);
935 make_shake_sblock_serial(shaked, idef, md);
942 settle_set_constraints(settled,
943 &idef->il[F_SETTLE], md);
946 /* Make a selection of the local atoms for essential dynamics */
949 dd_make_local_ed_indices(cr->dd, ed);
954 Constraints::setConstraints(const gmx_localtop_t &top,
957 impl_->setConstraints(top, md);
960 /*! \brief Makes a per-moleculetype container of mappings from atom
961 * indices to constraint indices.
963 * Note that flexible constraints are only enabled with a dynamical integrator. */
964 static std::vector<t_blocka>
965 makeAtomToConstraintMappings(const gmx_mtop_t &mtop,
966 FlexibleConstraintTreatment flexibleConstraintTreatment)
968 std::vector<t_blocka> mapping;
969 mapping.reserve(mtop.moltype.size());
970 for (const gmx_moltype_t &moltype : mtop.moltype)
972 mapping.push_back(make_at2con(moltype,
973 mtop.ffparams.iparams,
974 flexibleConstraintTreatment));
979 Constraints::Constraints(const gmx_mtop_t &mtop,
980 const t_inputrec &ir,
985 const gmx_multisim_t *ms,
987 gmx_wallcycle *wcycle,
988 bool pbcHandlingRequired,
991 : impl_(new Impl(mtop,
1000 pbcHandlingRequired,
1006 Constraints::Impl::Impl(const gmx_mtop_t &mtop_p,
1007 const t_inputrec &ir_p,
1010 const t_mdatoms &md_p,
1011 const t_commrec *cr_p,
1012 const gmx_multisim_t *ms_p,
1014 gmx_wallcycle *wcycle_p,
1015 bool pbcHandlingRequired,
1018 : ncon_tot(numConstraints),
1021 pbcHandlingRequired_(pbcHandlingRequired),
1025 pull_work(pull_work),
1030 if (numConstraints + numSettles > 0 && ir.epc == epcMTTK)
1032 gmx_fatal(FARGS, "Constraints are not implemented with MTTK pressure control.");
1036 if (numConstraints > 0)
1038 at2con_mt = makeAtomToConstraintMappings(mtop,
1039 flexibleConstraintTreatment(EI_DYNAMICS(ir.eI)));
1041 for (const gmx_molblock_t &molblock : mtop.molblock)
1044 countFlexibleConstraintsTemplate(mtop.moltype[molblock.type].ilist.data(),
1045 mtop.ffparams.iparams.data());
1046 nflexcon += molblock.nmol*count;
1053 fprintf(log, "There are %d flexible constraints\n",
1055 if (ir.fc_stepsize == 0)
1058 "WARNING: step size for flexible constraining = 0\n"
1059 " All flexible constraints will be rigid.\n"
1060 " Will try to keep all flexible constraints at their original length,\n"
1061 " but the lengths may exhibit some drift.\n\n");
1067 please_cite(log, "Hess2002");
1071 if (ir.eConstrAlg == econtLINCS)
1073 lincsd = init_lincs(log, mtop,
1074 nflexcon, at2con_mt,
1075 DOMAINDECOMP(cr) && ddHaveSplitConstraints(*cr->dd),
1076 ir.nLincsIter, ir.nProjOrder);
1079 if (ir.eConstrAlg == econtSHAKE)
1081 if (DOMAINDECOMP(cr) && ddHaveSplitConstraints(*cr->dd))
1083 gmx_fatal(FARGS, "SHAKE is not supported with domain decomposition and constraint that cross domain boundaries, use LINCS");
1087 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");
1089 please_cite(log, "Ryckaert77a");
1092 please_cite(log, "Barth95a");
1095 shaked = shake_init();
1101 please_cite(log, "Miyamoto92a");
1103 settled = settle_init(mtop);
1105 /* Make an atom to settle index for use in domain decomposition */
1106 for (size_t mt = 0; mt < mtop.moltype.size(); mt++)
1108 at2settle_mt.emplace_back(make_at2settle(mtop.moltype[mt].atoms.nr,
1109 mtop.moltype[mt].ilist[F_SETTLE]));
1112 /* Allocate thread-local work arrays */
1113 int nthreads = gmx_omp_nthreads_get(emntSETTLE);
1114 if (nthreads > 1 && vir_r_m_dr_th == nullptr)
1116 snew(vir_r_m_dr_th, nthreads);
1117 snew(bSettleErrorHasOccurred, nthreads);
1122 char *env = getenv("GMX_MAXCONSTRWARN");
1126 sscanf(env, "%8d", &maxwarn);
1134 "Setting the maximum number of constraint warnings to %d\n",
1140 "Setting the maximum number of constraint warnings to %d\n",
1144 warncount_lincs = 0;
1145 warncount_settle = 0;
1148 Constraints::Impl::~Impl()
1150 for (auto blocka : at2con_mt)
1152 done_blocka(&blocka);
1154 if (bSettleErrorHasOccurred != nullptr)
1156 sfree(bSettleErrorHasOccurred);
1158 if (vir_r_m_dr_th != nullptr)
1160 sfree(vir_r_m_dr_th);
1162 if (settled != nullptr)
1164 settle_free(settled);
1169 void Constraints::saveEdsamPointer(gmx_edsam * ed)
1174 ArrayRef<const t_blocka>
1175 Constraints::atom2constraints_moltype() const
1177 return impl_->at2con_mt;
1180 ArrayRef < const std::vector < int>> Constraints::atom2settle_moltype() const
1182 return impl_->at2settle_mt;
1185 void do_constrain_first(FILE *fplog, gmx::Constraints *constr,
1186 const t_inputrec *ir, const t_mdatoms *md,
1188 ArrayRefWithPadding<RVec> x,
1189 ArrayRefWithPadding<RVec> v,
1193 int i, m, start, end;
1195 real dt = ir->delta_t;
1199 auto xRvec = as_rvec_array(x.paddedArrayRef().data());
1200 auto vRvec = as_rvec_array(v.paddedArrayRef().data());
1202 /* We need to allocate one element extra, since we might use
1203 * (unaligned) 4-wide SIMD loads to access rvec entries.
1205 snew(savex, natoms + 1);
1212 fprintf(debug, "vcm: start=%d, homenr=%d, end=%d\n",
1213 start, md->homenr, end);
1215 /* Do a first constrain to reset particles... */
1216 step = ir->init_step;
1219 char buf[STEPSTRSIZE];
1220 fprintf(fplog, "\nConstraining the starting coordinates (step %s)\n",
1221 gmx_step_str(step, buf));
1225 /* constrain the current position */
1226 constr->apply(TRUE, FALSE,
1228 xRvec, xRvec, nullptr,
1231 nullptr, nullptr, gmx::ConstraintVariable::Positions);
1234 /* constrain the inital velocity, and save it */
1235 /* also may be useful if we need the ekin from the halfstep for velocity verlet */
1236 constr->apply(TRUE, FALSE,
1238 xRvec, vRvec, vRvec,
1241 nullptr, nullptr, gmx::ConstraintVariable::Velocities);
1243 /* constrain the inital velocities at t-dt/2 */
1244 if (EI_STATE_VELOCITY(ir->eI) && ir->eI != eiVV)
1246 auto subX = x.paddedArrayRef().subArray(start, end);
1247 auto subV = v.paddedArrayRef().subArray(start, end);
1248 for (i = start; (i < end); i++)
1250 for (m = 0; (m < DIM); m++)
1252 /* Reverse the velocity */
1253 subV[i][m] = -subV[i][m];
1254 /* Store the position at t-dt in buf */
1255 savex[i][m] = subX[i][m] + dt*subV[i][m];
1258 /* Shake the positions at t=-dt with the positions at t=0
1259 * as reference coordinates.
1263 char buf[STEPSTRSIZE];
1264 fprintf(fplog, "\nConstraining the coordinates at t0-dt (step %s)\n",
1265 gmx_step_str(step, buf));
1268 constr->apply(TRUE, FALSE,
1270 xRvec, savex, nullptr,
1273 vRvec, nullptr, gmx::ConstraintVariable::Positions);
1275 for (i = start; i < end; i++)
1277 for (m = 0; m < DIM; m++)
1279 /* Re-reverse the velocities */
1280 subV[i][m] = -subV[i][m];