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,2017,2018 by the GROMACS development team.
7 * Copyright (c) 2019,2020, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
39 * \brief Defines SHAKE code.
41 * \author David van der Spoel <david.vanderspoel@icm.uu.se>
42 * \author Berk Hess <hess@kth.se>
43 * \author Mark Abraham <mark.j.abraham@gmail.com>
44 * \ingroup module_mdlib
54 #include "gromacs/domdec/domdec_struct.h"
55 #include "gromacs/gmxlib/nrnb.h"
56 #include "gromacs/math/functions.h"
57 #include "gromacs/math/vec.h"
58 #include "gromacs/mdlib/constr.h"
59 #include "gromacs/mdlib/splitter.h"
60 #include "gromacs/mdtypes/inputrec.h"
61 #include "gromacs/mdtypes/md_enums.h"
62 #include "gromacs/mdtypes/mdatom.h"
63 #include "gromacs/topology/invblock.h"
64 #include "gromacs/utility/fatalerror.h"
65 #include "gromacs/utility/smalloc.h"
73 real* half_of_reduced_mass;
74 real* distance_squared_tolerance;
75 real* constraint_distance_squared;
81 int nblocks; /* The number of SHAKE blocks */
82 int* sblock; /* The SHAKE blocks */
83 int sblock_nalloc; /* The allocation size of sblock */
84 /*! \brief Scaled Lagrange multiplier for each constraint.
86 * Value is -2 * eta from p. 336 of the paper, divided by the
87 * constraint distance. */
88 real* scaled_lagrange_multiplier;
89 int lagr_nalloc; /* The allocation size of scaled_lagrange_multiplier */
92 shakedata* shake_init()
100 d->half_of_reduced_mass = nullptr;
101 d->distance_squared_tolerance = nullptr;
102 d->constraint_distance_squared = nullptr;
104 /* SOR initialization */
112 void done_shake(shakedata* d)
115 sfree(d->half_of_reduced_mass);
116 sfree(d->distance_squared_tolerance);
117 sfree(d->constraint_distance_squared);
119 sfree(d->scaled_lagrange_multiplier);
129 //! Compares sort blocks.
130 static int pcomp(const void* p1, const void* p2)
133 int min1, min2, max1, max2;
134 const t_sortblock* a1 = reinterpret_cast<const t_sortblock*>(p1);
135 const t_sortblock* a2 = reinterpret_cast<const t_sortblock*>(p2);
137 db = a1->blocknr - a2->blocknr;
144 min1 = std::min(a1->iatom[1], a1->iatom[2]);
145 max1 = std::max(a1->iatom[1], a1->iatom[2]);
146 min2 = std::min(a2->iatom[1], a2->iatom[2]);
147 max2 = std::max(a2->iatom[1], a2->iatom[2]);
159 //! Prints sortblocks
160 static void pr_sortblock(FILE* fp, const char* title, int nsb, t_sortblock sb[])
164 fprintf(fp, "%s\n", title);
165 for (i = 0; (i < nsb); i++)
167 fprintf(fp, "i: %5d, iatom: (%5d %5d %5d), blocknr: %5d\n", i, sb[i].iatom[0],
168 sb[i].iatom[1], sb[i].iatom[2], sb[i].blocknr);
172 //! Reallocates a vector.
173 static void resizeLagrangianData(shakedata* shaked, int ncons)
175 if (ncons > shaked->lagr_nalloc)
177 shaked->lagr_nalloc = over_alloc_dd(ncons);
178 srenew(shaked->scaled_lagrange_multiplier, shaked->lagr_nalloc);
182 void make_shake_sblock_serial(shakedata* shaked, const t_idef* idef, const t_mdatoms& md)
191 /* Since we are processing the local topology,
192 * the F_CONSTRNC ilist has been concatenated to the F_CONSTR ilist.
194 ncons = idef->il[F_CONSTR].nr / 3;
196 init_blocka(&sblocks);
197 sfree(sblocks.index); // To solve memory leak
198 gen_sblocks(nullptr, 0, md.homenr, idef, &sblocks, FALSE);
201 bstart=(idef->nodeid > 0) ? blocks->multinr[idef->nodeid-1] : 0;
202 nblocks=blocks->multinr[idef->nodeid] - bstart;
205 shaked->nblocks = sblocks.nr;
208 fprintf(debug, "ncons: %d, bstart: %d, nblocks: %d\n", ncons, bstart, shaked->nblocks);
211 /* Calculate block number for each atom */
212 inv_sblock = make_invblocka(&sblocks, md.nr);
214 done_blocka(&sblocks);
216 /* Store the block number in temp array and
217 * sort the constraints in order of the sblock number
218 * and the atom numbers, really sorting a segment of the array!
220 iatom = idef->il[F_CONSTR].iatoms;
222 for (i = 0; (i < ncons); i++, iatom += 3)
224 for (m = 0; (m < 3); m++)
226 sb[i].iatom[m] = iatom[m];
228 sb[i].blocknr = inv_sblock[iatom[1]];
231 /* Now sort the blocks */
234 pr_sortblock(debug, "Before sorting", ncons, sb);
235 fprintf(debug, "Going to sort constraints\n");
238 std::qsort(sb, ncons, sizeof(*sb), pcomp);
242 pr_sortblock(debug, "After sorting", ncons, sb);
245 iatom = idef->il[F_CONSTR].iatoms;
246 for (i = 0; (i < ncons); i++, iatom += 3)
248 for (m = 0; (m < 3); m++)
250 iatom[m] = sb[i].iatom[m];
255 snew(shaked->sblock, shaked->nblocks + 1);
257 for (i = 0; (i < ncons); i++)
259 if (sb[i].blocknr != bnr)
262 shaked->sblock[j++] = 3 * i;
266 shaked->sblock[j++] = 3 * ncons;
268 if (j != (shaked->nblocks + 1))
270 fprintf(stderr, "bstart: %d\n", bstart);
271 fprintf(stderr, "j: %d, nblocks: %d, ncons: %d\n", j, shaked->nblocks, ncons);
272 for (i = 0; (i < ncons); i++)
274 fprintf(stderr, "i: %5d sb[i].blocknr: %5d\n", i, sb[i].blocknr);
276 for (j = 0; (j <= shaked->nblocks); j++)
278 fprintf(stderr, "sblock[%3d]=%5d\n", j, shaked->sblock[j]);
282 "sblocks does not match idef->il[F_CONSTR]");
286 resizeLagrangianData(shaked, ncons);
289 // TODO: Check if this code is useful. It might never be called.
290 void make_shake_sblock_dd(shakedata* shaked, const t_ilist* ilcon, const gmx_domdec_t* dd)
295 if (dd->ncg_home + 1 > shaked->sblock_nalloc)
297 shaked->sblock_nalloc = over_alloc_dd(dd->ncg_home + 1);
298 srenew(shaked->sblock, shaked->sblock_nalloc);
301 ncons = ilcon->nr / 3;
302 iatom = ilcon->iatoms;
305 for (c = 0; c < ncons; c++)
307 if (c == 0 || iatom[1] >= cg + 1)
309 shaked->sblock[shaked->nblocks++] = 3 * c;
310 while (iatom[1] >= cg + 1)
317 shaked->sblock[shaked->nblocks] = 3 * ncons;
318 resizeLagrangianData(shaked, ncons);
321 /*! \brief Inner kernel for SHAKE constraints
323 * Original implementation from R.C. van Schaik and W.F. van Gunsteren
324 * (ETH Zuerich, June 1992), adapted for GROMACS by David van der
325 * Spoel November 1992.
327 * The algorithm here is based section five of Ryckaert, Ciccotti and
328 * Berendsen, J Comp Phys, 23, 327, 1977.
330 * \param[in] iatom Mini-topology of triples of constraint type (unused in this
331 * function) and indices of the two atoms involved
332 * \param[in] ncon Number of constraints
333 * \param[out] nnit Number of iterations performed
334 * \param[in] maxnit Maximum number of iterations permitted
335 * \param[in] constraint_distance_squared The objective value for each constraint
336 * \param[inout] positions The initial (and final) values of the positions of all atoms
337 * \param[in] initial_displacements The initial displacements of each constraint
338 * \param[in] half_of_reduced_mass Half of the reduced mass for each constraint
339 * \param[in] omega SHAKE over-relaxation factor (set non-1.0 by
340 * using shake-sor=yes in the .mdp, but there is no documentation anywhere)
341 * \param[in] invmass Inverse mass of each atom
342 * \param[in] distance_squared_tolerance Multiplicative tolerance on the difference in the
343 * square of the constrained distance (see code)
344 * \param[out] scaled_lagrange_multiplier Scaled Lagrange multiplier for each constraint (-2 * eta from p. 336
345 * of the paper, divided by the constraint distance)
346 * \param[out] nerror Zero upon success, returns one more than the index of the
347 * problematic constraint if the input was malformed
349 * \todo Make SHAKE use better data structures, in particular for iatom. */
350 void cshake(const int iatom[],
354 const real constraint_distance_squared[],
356 const real initial_displacements[],
357 const real half_of_reduced_mass[],
359 const real invmass[],
360 const real distance_squared_tolerance[],
361 real scaled_lagrange_multiplier[],
364 /* default should be increased! MRS 8/4/2009 */
365 const real mytol = 1e-10;
367 int ll, i, j, i3, j3, l3;
368 int ix, iy, iz, jx, jy, jz;
370 real constraint_distance_squared_ll;
371 real r_prime_squared;
372 real scaled_lagrange_multiplier_ll;
373 real r_prime_x, r_prime_y, r_prime_z, diff, im, jm;
374 real xh, yh, zh, rijx, rijy, rijz;
375 int nit, error, nconv;
378 // TODO nconv is used solely as a boolean, so we should write the
382 for (nit = 0; (nit < maxnit) && (nconv != 0) && (error == 0); nit++)
385 for (ll = 0; (ll < ncon) && (error == 0); ll++)
388 rijx = initial_displacements[l3 + XX];
389 rijy = initial_displacements[l3 + YY];
390 rijz = initial_displacements[l3 + ZZ];
402 /* Compute r prime between atoms i and j, which is the
403 displacement *before* this update stage */
404 r_prime_x = positions[ix] - positions[jx];
405 r_prime_y = positions[iy] - positions[jy];
406 r_prime_z = positions[iz] - positions[jz];
407 r_prime_squared = (r_prime_x * r_prime_x + r_prime_y * r_prime_y + r_prime_z * r_prime_z);
408 constraint_distance_squared_ll = constraint_distance_squared[ll];
409 diff = constraint_distance_squared_ll - r_prime_squared;
411 /* iconvf is less than 1 when the error is smaller than a bound */
412 iconvf = fabs(diff) * distance_squared_tolerance[ll];
416 nconv = static_cast<int>(iconvf);
417 r_dot_r_prime = (rijx * r_prime_x + rijy * r_prime_y + rijz * r_prime_z);
419 if (r_dot_r_prime < constraint_distance_squared_ll * mytol)
425 /* The next line solves equation 5.6 (neglecting
426 the term in g^2), for g */
427 scaled_lagrange_multiplier_ll = omega * diff * half_of_reduced_mass[ll] / r_dot_r_prime;
428 scaled_lagrange_multiplier[ll] += scaled_lagrange_multiplier_ll;
429 xh = rijx * scaled_lagrange_multiplier_ll;
430 yh = rijy * scaled_lagrange_multiplier_ll;
431 zh = rijz * scaled_lagrange_multiplier_ll;
434 positions[ix] += xh * im;
435 positions[iy] += yh * im;
436 positions[iz] += zh * im;
437 positions[jx] -= xh * jm;
438 positions[jy] -= yh * jm;
439 positions[jz] -= zh * jm;
448 //! Implements RATTLE (ie. SHAKE for velocity verlet integrators)
449 static void crattle(const int iatom[],
453 const real constraint_distance_squared[],
458 const real invmass[],
459 const real distance_squared_tolerance[],
460 real scaled_lagrange_multiplier[],
465 * r.c. van schaik and w.f. van gunsteren
468 * Adapted for use with Gromacs by David van der Spoel november 92 and later.
469 * rattle added by M.R. Shirts, April 2004, from code written by Jay Ponder in TINKER
470 * second part of rattle algorithm
473 int ll, i, j, i3, j3, l3;
474 int ix, iy, iz, jx, jy, jz;
475 real constraint_distance_squared_ll;
476 real vpijd, vx, vy, vz, acor, fac, im, jm;
477 real xh, yh, zh, rijx, rijy, rijz;
478 int nit, error, nconv;
481 // TODO nconv is used solely as a boolean, so we should write the
485 for (nit = 0; (nit < maxnit) && (nconv != 0) && (error == 0); nit++)
488 for (ll = 0; (ll < ncon) && (error == 0); ll++)
504 vx = vp[ix] - vp[jx];
505 vy = vp[iy] - vp[jy];
506 vz = vp[iz] - vp[jz];
508 vpijd = vx * rijx + vy * rijy + vz * rijz;
509 constraint_distance_squared_ll = constraint_distance_squared[ll];
511 /* iconv is zero when the error is smaller than a bound */
512 iconvf = fabs(vpijd) * (distance_squared_tolerance[ll] / invdt);
516 nconv = static_cast<int>(iconvf);
517 fac = omega * 2.0 * m2[ll] / constraint_distance_squared_ll;
519 scaled_lagrange_multiplier[ll] += acor;
541 static int vec_shakef(FILE* fplog,
543 const real invmass[],
553 real scaled_lagrange_multiplier[],
558 ConstraintVariable econq)
561 real * half_of_reduced_mass, *distance_squared_tolerance, *constraint_distance_squared;
563 int nit = 0, ll, i, j, d, d2, type;
568 real constraint_distance;
570 if (ncon > shaked->nalloc)
572 shaked->nalloc = over_alloc_dd(ncon);
573 srenew(shaked->rij, shaked->nalloc);
574 srenew(shaked->half_of_reduced_mass, shaked->nalloc);
575 srenew(shaked->distance_squared_tolerance, shaked->nalloc);
576 srenew(shaked->constraint_distance_squared, shaked->nalloc);
579 half_of_reduced_mass = shaked->half_of_reduced_mass;
580 distance_squared_tolerance = shaked->distance_squared_tolerance;
581 constraint_distance_squared = shaked->constraint_distance_squared;
585 for (ll = 0; (ll < ncon); ll++, ia += 3)
591 mm = 2.0 * (invmass[i] + invmass[j]);
592 rij[ll][XX] = x[i][XX] - x[j][XX];
593 rij[ll][YY] = x[i][YY] - x[j][YY];
594 rij[ll][ZZ] = x[i][ZZ] - x[j][ZZ];
595 half_of_reduced_mass[ll] = 1.0 / mm;
598 constraint_distance = L1 * ip[type].constr.dA + lambda * ip[type].constr.dB;
602 constraint_distance = ip[type].constr.dA;
604 constraint_distance_squared[ll] = gmx::square(constraint_distance);
605 distance_squared_tolerance[ll] = 0.5 / (constraint_distance_squared[ll] * tol);
610 case ConstraintVariable::Positions:
611 cshake(iatom, ncon, &nit, maxnit, constraint_distance_squared, prime[0], rij[0],
612 half_of_reduced_mass, omega, invmass, distance_squared_tolerance,
613 scaled_lagrange_multiplier, &error);
615 case ConstraintVariable::Velocities:
616 crattle(iatom, ncon, &nit, maxnit, constraint_distance_squared, prime[0], rij[0],
617 half_of_reduced_mass, omega, invmass, distance_squared_tolerance,
618 scaled_lagrange_multiplier, &error, invdt);
620 default: gmx_incons("Unknown constraint quantity for SHAKE");
627 fprintf(fplog, "Shake did not converge in %d steps\n", maxnit);
629 fprintf(stderr, "Shake did not converge in %d steps\n", maxnit);
637 "Inner product between old and new vector <= 0.0!\n"
638 "constraint #%d atoms %d and %d\n",
639 error - 1, iatom[3 * (error - 1) + 1] + 1, iatom[3 * (error - 1) + 2] + 1);
642 "Inner product between old and new vector <= 0.0!\n"
643 "constraint #%d atoms %d and %d\n",
644 error - 1, iatom[3 * (error - 1) + 1] + 1, iatom[3 * (error - 1) + 2] + 1);
648 /* Constraint virial and correct the Lagrange multipliers for the length */
652 for (ll = 0; (ll < ncon); ll++, ia += 3)
658 if ((econq == ConstraintVariable::Positions) && v != nullptr)
660 /* Correct the velocities */
661 mm = scaled_lagrange_multiplier[ll] * invmass[i] * invdt;
662 for (d = 0; d < DIM; d++)
664 v[ia[1]][d] += mm * rij[ll][d];
666 mm = scaled_lagrange_multiplier[ll] * invmass[j] * invdt;
667 for (d = 0; d < DIM; d++)
669 v[ia[2]][d] -= mm * rij[ll][d];
674 /* constraint virial */
677 mm = scaled_lagrange_multiplier[ll];
678 for (d = 0; d < DIM; d++)
680 tmp = mm * rij[ll][d];
681 for (d2 = 0; d2 < DIM; d2++)
683 vir_r_m_dr[d][d2] -= tmp * rij[ll][d2];
689 /* cshake and crattle produce Lagrange multipliers scaled by
690 the reciprocal of the constraint length, so fix that */
693 constraint_distance = L1 * ip[type].constr.dA + lambda * ip[type].constr.dB;
697 constraint_distance = ip[type].constr.dA;
699 scaled_lagrange_multiplier[ll] *= constraint_distance;
705 //! Check that constraints are satisfied.
706 static void check_cons(FILE* log,
713 const real invmass[],
714 ConstraintVariable econq)
722 GMX_ASSERT(v, "Input has to be non-null");
723 fprintf(log, " i mi j mj before after should be\n");
725 for (i = 0; (i < nc); i++, ia += 3)
729 rvec_sub(x[ai], x[aj], dx);
734 case ConstraintVariable::Positions:
735 rvec_sub(prime[ai], prime[aj], dx);
737 fprintf(log, "%5d %5.2f %5d %5.2f %10.5f %10.5f %10.5f\n", ai + 1,
738 1.0 / invmass[ai], aj + 1, 1.0 / invmass[aj], d, dp, ip[ia[0]].constr.dA);
740 case ConstraintVariable::Velocities:
741 rvec_sub(v[ai], v[aj], dv);
743 rvec_sub(prime[ai], prime[aj], dv);
745 fprintf(log, "%5d %5.2f %5d %5.2f %10.5f %10.5f %10.5f\n", ai + 1,
746 1.0 / invmass[ai], aj + 1, 1.0 / invmass[aj], d, dp, 0.);
748 default: gmx_incons("Unknown constraint quantity for SHAKE");
754 static bool bshakef(FILE* log,
756 const real invmass[],
758 const t_inputrec& ir,
769 ConstraintVariable econq)
772 real * lam, dt_2, dvdl;
773 int i, n0, ncon, blen, type, ll;
774 int tnit = 0, trij = 0;
776 ncon = idef.il[F_CONSTR].nr / 3;
778 for (ll = 0; ll < ncon; ll++)
780 shaked->scaled_lagrange_multiplier[ll] = 0;
783 // TODO Rewrite this block so that it is obvious that i, iatoms
784 // and lam are all iteration variables. Is this easier if the
785 // sblock data structure is organized differently?
786 iatoms = &(idef.il[F_CONSTR].iatoms[shaked->sblock[0]]);
787 lam = shaked->scaled_lagrange_multiplier;
788 for (i = 0; (i < shaked->nblocks);)
790 blen = (shaked->sblock[i + 1] - shaked->sblock[i]);
792 n0 = vec_shakef(log, shaked, invmass, blen, idef.iparams, iatoms, ir.shake_tol, x_s, prime,
793 shaked->omega, ir.efep != efepNO, lambda, lam, invdt, v, bCalcVir,
798 if (bDumpOnError && log)
801 check_cons(log, blen, x_s, prime, v, idef.iparams, iatoms, invmass, econq);
808 iatoms += 3 * blen; /* Increment pointer! */
812 /* only for position part? */
813 if (econq == ConstraintVariable::Positions)
815 if (ir.efep != efepNO)
818 /* TODO This should probably use invdt, so that sd integrator scaling works properly */
819 dt_2 = 1 / gmx::square(ir.delta_t);
821 for (ll = 0; ll < ncon; ll++)
823 type = idef.il[F_CONSTR].iatoms[3 * ll];
825 /* Per equations in the manual, dv/dl = -2 \sum_ll lagrangian_ll * r_ll * (d_B - d_A) */
826 /* The vector scaled_lagrange_multiplier[ll] contains the value -2 r_ll eta_ll
827 (eta_ll is the estimate of the Langrangian, definition on page 336 of Ryckaert et
828 al 1977), so the pre-factors are already present. */
829 bondA = idef.iparams[type].constr.dA;
830 bondB = idef.iparams[type].constr.dB;
831 dvdl += shaked->scaled_lagrange_multiplier[ll] * dt_2 * (bondB - bondA);
838 if (tnit > shaked->gamma)
840 shaked->delta *= -0.5;
842 shaked->omega += shaked->delta;
843 shaked->gamma = tnit;
845 inc_nrnb(nrnb, eNR_SHAKE, tnit);
846 inc_nrnb(nrnb, eNR_SHAKE_RIJ, trij);
849 inc_nrnb(nrnb, eNR_CONSTR_V, trij * 2);
853 inc_nrnb(nrnb, eNR_CONSTR_VIR, trij);
859 bool constrain_shake(FILE* log,
861 const real invmass[],
863 const t_inputrec& ir,
875 ConstraintVariable econq)
877 if (shaked->nblocks == 0)
884 case (ConstraintVariable::Positions):
885 bOK = bshakef(log, shaked, invmass, idef, ir, x_s, xprime, nrnb, lambda, dvdlambda,
886 invdt, v, bCalcVir, vir_r_m_dr, bDumpOnError, econq);
888 case (ConstraintVariable::Velocities):
889 bOK = bshakef(log, shaked, invmass, idef, ir, x_s, vprime, nrnb, lambda, dvdlambda,
890 invdt, nullptr, bCalcVir, vir_r_m_dr, bDumpOnError, econq);
894 "Internal error, SHAKE called for constraining something else than "