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, 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.
41 #include "gromacs/legacyheaders/constr.h"
42 #include "gromacs/legacyheaders/nrnb.h"
43 #include "gromacs/legacyheaders/txtdump.h"
44 #include "gromacs/legacyheaders/typedefs.h"
45 #include "gromacs/math/vec.h"
46 #include "gromacs/utility/smalloc.h"
48 typedef struct gmx_shakedata
51 real *half_of_reduced_mass;
52 real *distance_squared_tolerance;
53 real *constraint_distance_squared;
61 gmx_shakedata_t shake_init()
69 d->half_of_reduced_mass = NULL;
70 d->distance_squared_tolerance = NULL;
71 d->constraint_distance_squared = NULL;
73 /* SOR initialization */
81 static void pv(FILE *log, char *s, rvec x)
85 fprintf(log, "%5s:", s);
86 for (m = 0; (m < DIM); m++)
88 fprintf(log, " %10.3f", x[m]);
94 /*! \brief Inner kernel for SHAKE constraints
96 * Original implementation from R.C. van Schaik and W.F. van Gunsteren
97 * (ETH Zuerich, June 1992), adapted for GROMACS by David van der
98 * Spoel November 1992.
100 * The algorithm here is based section five of Ryckaert, Ciccotti and
101 * Berendsen, J Comp Phys, 23, 327, 1977.
103 * \param[in] iatom Mini-topology of triples of constraint type (unused in this
104 * function) and indices of the two atoms involved
105 * \param[in] ncon Number of constraints
106 * \param[out] nnit Number of iterations performed
107 * \param[in] maxnit Maximum number of iterations permitted
108 * \param[in] constraint_distance_squared The objective value for each constraint
109 * \param[inout] positions The initial (and final) values of the positions of all atoms
110 * \param[in] initial_displacements The initial displacements of each constraint
111 * \param[in] half_of_reduced_mass Half of the reduced mass for each constraint
112 * \param[in] omega SHAKE over-relaxation factor (set non-1.0 by
113 * using shake-sor=yes in the .mdp, but there is no documentation anywhere)
114 * \param[in] invmass Inverse mass of each atom
115 * \param[in] distance_squared_tolerance Multiplicative tolerance on the difference in the
116 * square of the constrained distance (see code)
117 * \param[out] scaled_lagrange_multiplier Scaled Lagrange multiplier for each constraint (-2 * eta from p. 336
118 * of the paper, divided by the constraint distance)
119 * \param[out] nerror Zero upon success, returns one more than the index of the
120 * problematic constraint if the input was malformed
122 * \todo Make SHAKE use better data structures, in particular for iatom. */
123 void cshake(const atom_id iatom[], int ncon, int *nnit, int maxnit,
124 const real constraint_distance_squared[], real positions[],
125 const real initial_displacements[], const real half_of_reduced_mass[], real omega,
126 const real invmass[], const real distance_squared_tolerance[],
127 real scaled_lagrange_multiplier[], int *nerror)
129 /* default should be increased! MRS 8/4/2009 */
130 const real mytol = 1e-10;
132 int ll, i, j, i3, j3, l3;
133 int ix, iy, iz, jx, jy, jz;
135 real constraint_distance_squared_ll;
136 real r_prime_squared;
137 real scaled_lagrange_multiplier_ll;
138 real r_prime_x, r_prime_y, r_prime_z, diff, im, jm;
139 real xh, yh, zh, rijx, rijy, rijz;
142 int nit, error, nconv;
147 for (nit = 0; (nit < maxnit) && (nconv != 0) && (error == 0); nit++)
150 for (ll = 0; (ll < ncon) && (error == 0); ll++)
153 rijx = initial_displacements[l3+XX];
154 rijy = initial_displacements[l3+YY];
155 rijz = initial_displacements[l3+ZZ];
167 /* Compute r prime between atoms i and j, which is the
168 displacement *before* this update stage */
169 r_prime_x = positions[ix]-positions[jx];
170 r_prime_y = positions[iy]-positions[jy];
171 r_prime_z = positions[iz]-positions[jz];
172 r_prime_squared = (r_prime_x * r_prime_x +
173 r_prime_y * r_prime_y +
174 r_prime_z * r_prime_z);
175 constraint_distance_squared_ll = constraint_distance_squared[ll];
176 diff = constraint_distance_squared_ll - r_prime_squared;
178 /* iconvf is less than 1 when the error is smaller than a bound */
179 iconvf = fabs(diff) * distance_squared_tolerance[ll];
184 r_dot_r_prime = (rijx * r_prime_x +
188 if (r_dot_r_prime < constraint_distance_squared_ll * mytol)
194 /* The next line solves equation 5.6 (neglecting
195 the term in g^2), for g */
196 scaled_lagrange_multiplier_ll = omega*diff*half_of_reduced_mass[ll]/r_dot_r_prime;
197 scaled_lagrange_multiplier[ll] += scaled_lagrange_multiplier_ll;
198 xh = rijx * scaled_lagrange_multiplier_ll;
199 yh = rijy * scaled_lagrange_multiplier_ll;
200 zh = rijz * scaled_lagrange_multiplier_ll;
203 positions[ix] += xh*im;
204 positions[iy] += yh*im;
205 positions[iz] += zh*im;
206 positions[jx] -= xh*jm;
207 positions[jy] -= yh*jm;
208 positions[jz] -= zh*jm;
217 int vec_shakef(FILE *fplog, gmx_shakedata_t shaked,
218 real invmass[], int ncon,
219 t_iparams ip[], t_iatom *iatom,
220 real tol, rvec x[], rvec prime[], real omega,
221 gmx_bool bFEP, real lambda, real scaled_lagrange_multiplier[],
223 gmx_bool bCalcVir, tensor vir_r_m_dr, int econq,
227 real *half_of_reduced_mass, *distance_squared_tolerance, *constraint_distance_squared;
229 int nit = 0, ll, i, j, d, d2, type;
234 real vscale, rscale, rvscale;
235 real constraint_distance;
237 if (ncon > shaked->nalloc)
239 shaked->nalloc = over_alloc_dd(ncon);
240 srenew(shaked->rij, shaked->nalloc);
241 srenew(shaked->half_of_reduced_mass, shaked->nalloc);
242 srenew(shaked->distance_squared_tolerance, shaked->nalloc);
243 srenew(shaked->constraint_distance_squared, shaked->nalloc);
246 half_of_reduced_mass = shaked->half_of_reduced_mass;
247 distance_squared_tolerance = shaked->distance_squared_tolerance;
248 constraint_distance_squared = shaked->constraint_distance_squared;
252 for (ll = 0; (ll < ncon); ll++, ia += 3)
258 mm = 2.0*(invmass[i]+invmass[j]);
259 rij[ll][XX] = x[i][XX]-x[j][XX];
260 rij[ll][YY] = x[i][YY]-x[j][YY];
261 rij[ll][ZZ] = x[i][ZZ]-x[j][ZZ];
262 half_of_reduced_mass[ll] = 1.0/mm;
265 constraint_distance = L1*ip[type].constr.dA + lambda*ip[type].constr.dB;
269 constraint_distance = ip[type].constr.dA;
271 constraint_distance_squared[ll] = sqr(constraint_distance);
272 distance_squared_tolerance[ll] = 0.5/(constraint_distance_squared[ll]*tol);
278 cshake(iatom, ncon, &nit, maxnit, constraint_distance_squared, prime[0], rij[0], half_of_reduced_mass, omega, invmass, distance_squared_tolerance, scaled_lagrange_multiplier, &error);
281 crattle(iatom, ncon, &nit, maxnit, constraint_distance_squared, prime[0], rij[0], half_of_reduced_mass, omega, invmass, distance_squared_tolerance, scaled_lagrange_multiplier, &error, invdt, vetavar);
289 fprintf(fplog, "Shake did not converge in %d steps\n", maxnit);
291 fprintf(stderr, "Shake did not converge in %d steps\n", maxnit);
298 fprintf(fplog, "Inner product between old and new vector <= 0.0!\n"
299 "constraint #%d atoms %d and %d\n",
300 error-1, iatom[3*(error-1)+1]+1, iatom[3*(error-1)+2]+1);
302 fprintf(stderr, "Inner product between old and new vector <= 0.0!\n"
303 "constraint #%d atoms %d and %d\n",
304 error-1, iatom[3*(error-1)+1]+1, iatom[3*(error-1)+2]+1);
308 /* Constraint virial and correct the Lagrange multipliers for the length */
312 for (ll = 0; (ll < ncon); ll++, ia += 3)
318 if ((econq == econqCoord) && v != NULL)
320 /* Correct the velocities */
321 mm = scaled_lagrange_multiplier[ll]*invmass[i]*invdt/vetavar->rscale;
322 for (d = 0; d < DIM; d++)
324 v[ia[1]][d] += mm*rij[ll][d];
326 mm = scaled_lagrange_multiplier[ll]*invmass[j]*invdt/vetavar->rscale;
327 for (d = 0; d < DIM; d++)
329 v[ia[2]][d] -= mm*rij[ll][d];
334 /* constraint virial */
337 if (econq == econqCoord)
339 mm = scaled_lagrange_multiplier[ll]/vetavar->rvscale;
341 if (econq == econqVeloc)
343 mm = scaled_lagrange_multiplier[ll]/(vetavar->vscale*vetavar->vscale_nhc[0]);
345 for (d = 0; d < DIM; d++)
348 for (d2 = 0; d2 < DIM; d2++)
350 vir_r_m_dr[d][d2] -= tmp*rij[ll][d2];
356 /* cshake and crattle produce Lagrange multipliers scaled by
357 the reciprocal of the constraint length, so fix that */
360 constraint_distance = L1*ip[type].constr.dA + lambda*ip[type].constr.dB;
364 constraint_distance = ip[type].constr.dA;
366 scaled_lagrange_multiplier[ll] *= constraint_distance;
372 static void check_cons(FILE *log, int nc, rvec x[], rvec prime[], rvec v[],
373 t_iparams ip[], t_iatom *iatom,
374 real invmass[], int econq)
383 " i mi j mj before after should be\n");
385 for (i = 0; (i < nc); i++, ia += 3)
389 rvec_sub(x[ai], x[aj], dx);
395 rvec_sub(prime[ai], prime[aj], dx);
397 fprintf(log, "%5d %5.2f %5d %5.2f %10.5f %10.5f %10.5f\n",
398 ai+1, 1.0/invmass[ai],
399 aj+1, 1.0/invmass[aj], d, dp, ip[ia[0]].constr.dA);
402 rvec_sub(v[ai], v[aj], dv);
404 rvec_sub(prime[ai], prime[aj], dv);
406 fprintf(log, "%5d %5.2f %5d %5.2f %10.5f %10.5f %10.5f\n",
407 ai+1, 1.0/invmass[ai],
408 aj+1, 1.0/invmass[aj], d, dp, 0.);
414 gmx_bool bshakef(FILE *log, gmx_shakedata_t shaked,
415 real invmass[], int nblocks, int sblock[],
416 t_idef *idef, t_inputrec *ir, rvec x_s[], rvec prime[],
417 t_nrnb *nrnb, real *scaled_lagrange_multiplier, real lambda, real *dvdlambda,
418 real invdt, rvec *v, gmx_bool bCalcVir, tensor vir_r_m_dr,
419 gmx_bool bDumpOnError, int econq, t_vetavars *vetavar)
423 int i, n0, ncon, blen, type, ll;
424 int tnit = 0, trij = 0;
427 fprintf(log, "nblocks=%d, sblock[0]=%d\n", nblocks, sblock[0]);
430 ncon = idef->il[F_CONSTR].nr/3;
432 for (ll = 0; ll < ncon; ll++)
434 scaled_lagrange_multiplier[ll] = 0;
437 iatoms = &(idef->il[F_CONSTR].iatoms[sblock[0]]);
438 for (i = 0; (i < nblocks); )
440 blen = (sblock[i+1]-sblock[i]);
442 n0 = vec_shakef(log, shaked, invmass, blen, idef->iparams,
443 iatoms, ir->shake_tol, x_s, prime, shaked->omega,
444 ir->efep != efepNO, lambda, scaled_lagrange_multiplier, invdt, v, bCalcVir, vir_r_m_dr,
448 check_cons(log, blen, x_s, prime, v, idef->iparams, iatoms, invmass, econq);
453 if (bDumpOnError && log)
456 check_cons(log, blen, x_s, prime, v, idef->iparams, iatoms, invmass, econq);
463 iatoms += 3*blen; /* Increment pointer! */
464 scaled_lagrange_multiplier += blen;
467 /* only for position part? */
468 if (econq == econqCoord)
470 if (ir->efep != efepNO)
473 /* TODO This should probably use invdt, so that sd integrator scaling works properly */
474 dt_2 = 1/sqr(ir->delta_t);
476 for (ll = 0; ll < ncon; ll++)
478 type = idef->il[F_CONSTR].iatoms[3*ll];
480 /* Per equations in the manual, dv/dl = -2 \sum_ll lagrangian_ll * r_ll * (d_B - d_A) */
481 /* The vector scaled_lagrange_multiplier[ll] contains the value -2 r_ll eta_ll (eta_ll is the
482 estimate of the Langrangian, definition on page 336 of Ryckaert et al 1977),
483 so the pre-factors are already present. */
484 bondA = idef->iparams[type].constr.dA;
485 bondB = idef->iparams[type].constr.dB;
486 dvdl += scaled_lagrange_multiplier[ll] * dt_2 * (bondB - bondA);
492 fprintf(log, "tnit: %5d omega: %10.5f\n", tnit, omega);
496 if (tnit > shaked->gamma)
498 shaked->delta *= -0.5;
500 shaked->omega += shaked->delta;
501 shaked->gamma = tnit;
503 inc_nrnb(nrnb, eNR_SHAKE, tnit);
504 inc_nrnb(nrnb, eNR_SHAKE_RIJ, trij);
507 inc_nrnb(nrnb, eNR_CONSTR_V, trij*2);
511 inc_nrnb(nrnb, eNR_CONSTR_VIR, trij);
517 void crattle(atom_id iatom[], int ncon, int *nnit, int maxnit,
518 real constraint_distance_squared[], real vp[], real rij[], real m2[], real omega,
519 real invmass[], real distance_squared_tolerance[], real scaled_lagrange_multiplier[],
520 int *nerror, real invdt, t_vetavars *vetavar)
523 * r.c. van schaik and w.f. van gunsteren
526 * Adapted for use with Gromacs by David van der Spoel november 92 and later.
527 * rattle added by M.R. Shirts, April 2004, from code written by Jay Ponder in TINKER
528 * second part of rattle algorithm
531 const real mytol = 1e-10;
533 int ll, i, j, i3, j3, l3, ii;
534 int ix, iy, iz, jx, jy, jz;
535 real constraint_distance_squared_ll;
536 real rijd, vpijd, vx, vy, vz, diff, acor, xdotd, fac, im, jm, imdt, jmdt;
537 real xh, yh, zh, rijx, rijy, rijz;
540 int nit, error, nconv;
541 real veta, vscale_nhc, iconvf;
543 veta = vetavar->veta;
544 vscale_nhc = vetavar->vscale_nhc[0]; /* for now, just use the first state */
548 for (nit = 0; (nit < maxnit) && (nconv != 0) && (error == 0); nit++)
551 for (ll = 0; (ll < ncon) && (error == 0); ll++)
571 vpijd = vx*rijx+vy*rijy+vz*rijz;
572 constraint_distance_squared_ll = constraint_distance_squared[ll];
573 /* this is r(t+dt) \dotproduct \dot{r}(t+dt) */
574 xdotd = vpijd*vscale_nhc + veta*constraint_distance_squared_ll;
576 /* iconv is zero when the error is smaller than a bound */
577 iconvf = fabs(xdotd)*(distance_squared_tolerance[ll]/invdt);
582 fac = omega*2.0*m2[ll]/constraint_distance_squared_ll;
584 scaled_lagrange_multiplier[ll] += acor;
590 im = invmass[i]/vscale_nhc;
591 jm = invmass[j]/vscale_nhc;