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.
44 #include "gromacs/utility/smalloc.h"
51 typedef struct gmx_shakedata
64 gmx_shakedata_t shake_init()
76 /* SOR initialization */
84 static void pv(FILE *log, char *s, rvec x)
88 fprintf(log, "%5s:", s);
89 for (m = 0; (m < DIM); m++)
91 fprintf(log, " %10.3f", x[m]);
97 void cshake(atom_id iatom[], int ncon, int *nnit, int maxnit,
98 real dist2[], real xp[], real rij[], real m2[], real omega,
99 real invmass[], real tt[], real lagr[], int *nerror)
102 * r.c. van schaik and w.f. van gunsteren
105 * Adapted for use with Gromacs by David van der Spoel november 92 and later.
107 /* default should be increased! MRS 8/4/2009 */
108 const real mytol = 1e-10;
110 int ll, i, j, i3, j3, l3;
111 int ix, iy, iz, jx, jy, jz;
112 real toler, rpij2, rrpr, tx, ty, tz, diff, acor, im, jm;
113 real xh, yh, zh, rijx, rijy, rijz;
116 int nit, error, nconv;
121 for (nit = 0; (nit < maxnit) && (nconv != 0) && (error == 0); nit++)
124 for (ll = 0; (ll < ncon) && (error == 0); ll++)
144 rpij2 = tx*tx+ty*ty+tz*tz;
148 /* iconvf is less than 1 when the error is smaller than a bound */
149 /* But if tt is too big, then it will result in looping in iconv */
151 iconvf = fabs(diff)*tt[ll];
156 rrpr = rijx*tx+rijy*ty+rijz*tz;
158 if (rrpr < toler*mytol)
164 acor = omega*diff*m2[ll]/rrpr;
185 int vec_shakef(FILE *fplog, gmx_shakedata_t shaked,
186 real invmass[], int ncon,
187 t_iparams ip[], t_iatom *iatom,
188 real tol, rvec x[], rvec prime[], real omega,
189 gmx_bool bFEP, real lambda, real lagr[],
191 gmx_bool bCalcVir, tensor vir_r_m_dr, int econq,
195 real *M2, *tt, *dist2;
197 int nit = 0, ll, i, j, type;
199 real L1, tol2, toler;
202 real g, vscale, rscale, rvscale;
204 if (ncon > shaked->nalloc)
206 shaked->nalloc = over_alloc_dd(ncon);
207 srenew(shaked->rij, shaked->nalloc);
208 srenew(shaked->M2, shaked->nalloc);
209 srenew(shaked->tt, shaked->nalloc);
210 srenew(shaked->dist2, shaked->nalloc);
215 dist2 = shaked->dist2;
220 for (ll = 0; (ll < ncon); ll++, ia += 3)
226 mm = 2*(invmass[i]+invmass[j]);
227 rij[ll][XX] = x[i][XX]-x[j][XX];
228 rij[ll][YY] = x[i][YY]-x[j][YY];
229 rij[ll][ZZ] = x[i][ZZ]-x[j][ZZ];
233 toler = sqr(L1*ip[type].constr.dA + lambda*ip[type].constr.dB);
237 toler = sqr(ip[type].constr.dA);
240 tt[ll] = 1.0/(toler*tol2);
246 cshake(iatom, ncon, &nit, maxnit, dist2, prime[0], rij[0], M2, omega, invmass, tt, lagr, &error);
249 crattle(iatom, ncon, &nit, maxnit, dist2, prime[0], rij[0], M2, omega, invmass, tt, lagr, &error, invdt, vetavar);
257 fprintf(fplog, "Shake did not converge in %d steps\n", maxnit);
259 fprintf(stderr, "Shake did not converge in %d steps\n", maxnit);
266 fprintf(fplog, "Inner product between old and new vector <= 0.0!\n"
267 "constraint #%d atoms %u and %u\n",
268 error-1, iatom[3*(error-1)+1]+1, iatom[3*(error-1)+2]+1);
270 fprintf(stderr, "Inner product between old and new vector <= 0.0!\n"
271 "constraint #%d atoms %u and %u\n",
272 error-1, iatom[3*(error-1)+1]+1, iatom[3*(error-1)+2]+1);
276 /* Constraint virial and correct the lagrange multipliers for the length */
280 for (ll = 0; (ll < ncon); ll++, ia += 3)
283 if ((econq == econqCoord) && v != NULL)
285 /* Correct the velocities */
286 mm = lagr[ll]*invmass[ia[1]]*invdt/vetavar->rscale;
287 for (i = 0; i < DIM; i++)
289 v[ia[1]][i] += mm*rij[ll][i];
291 mm = lagr[ll]*invmass[ia[2]]*invdt/vetavar->rscale;
292 for (i = 0; i < DIM; i++)
294 v[ia[2]][i] -= mm*rij[ll][i];
299 /* constraint virial */
302 if (econq == econqCoord)
304 mm = lagr[ll]/vetavar->rvscale;
306 if (econq == econqVeloc)
308 mm = lagr[ll]/(vetavar->vscale*vetavar->vscale_nhc[0]);
310 for (i = 0; i < DIM; i++)
313 for (j = 0; j < DIM; j++)
315 vir_r_m_dr[i][j] -= tmp*rij[ll][j];
321 /* Correct the lagrange multipliers for the length */
322 /* (more details would be useful here . . . )*/
327 toler = L1*ip[type].constr.dA + lambda*ip[type].constr.dB;
331 toler = ip[type].constr.dA;
339 static void check_cons(FILE *log, int nc, rvec x[], rvec prime[], rvec v[],
340 t_iparams ip[], t_iatom *iatom,
341 real invmass[], int econq)
350 " i mi j mj before after should be\n");
352 for (i = 0; (i < nc); i++, ia += 3)
356 rvec_sub(x[ai], x[aj], dx);
362 rvec_sub(prime[ai], prime[aj], dx);
364 fprintf(log, "%5d %5.2f %5d %5.2f %10.5f %10.5f %10.5f\n",
365 ai+1, 1.0/invmass[ai],
366 aj+1, 1.0/invmass[aj], d, dp, ip[ia[0]].constr.dA);
369 rvec_sub(v[ai], v[aj], dv);
371 rvec_sub(prime[ai], prime[aj], dv);
373 fprintf(log, "%5d %5.2f %5d %5.2f %10.5f %10.5f %10.5f\n",
374 ai+1, 1.0/invmass[ai],
375 aj+1, 1.0/invmass[aj], d, dp, 0.);
381 gmx_bool bshakef(FILE *log, gmx_shakedata_t shaked,
382 real invmass[], int nblocks, int sblock[],
383 t_idef *idef, t_inputrec *ir, rvec x_s[], rvec prime[],
384 t_nrnb *nrnb, real *lagr, real lambda, real *dvdlambda,
385 real invdt, rvec *v, gmx_bool bCalcVir, tensor vir_r_m_dr,
386 gmx_bool bDumpOnError, int econq, t_vetavars *vetavar)
389 real *lam, dt_2, dvdl;
390 int i, n0, ncons, blen, type;
391 int tnit = 0, trij = 0;
394 fprintf(log, "nblocks=%d, sblock[0]=%d\n", nblocks, sblock[0]);
397 ncons = idef->il[F_CONSTR].nr/3;
399 for (i = 0; i < ncons; i++)
404 iatoms = &(idef->il[F_CONSTR].iatoms[sblock[0]]);
406 for (i = 0; (i < nblocks); )
408 blen = (sblock[i+1]-sblock[i]);
410 n0 = vec_shakef(log, shaked, invmass, blen, idef->iparams,
411 iatoms, ir->shake_tol, x_s, prime, shaked->omega,
412 ir->efep != efepNO, lambda, lam, invdt, v, bCalcVir, vir_r_m_dr,
416 check_cons(log, blen, x_s, prime, v, idef->iparams, iatoms, invmass, econq);
421 if (bDumpOnError && log)
424 check_cons(log, blen, x_s, prime, v, idef->iparams, iatoms, invmass, econq);
431 iatoms += 3*blen; /* Increment pointer! */
435 /* only for position part? */
436 if (econq == econqCoord)
438 if (ir->efep != efepNO)
441 dt_2 = 1/sqr(ir->delta_t);
443 for (i = 0; i < ncons; i++)
445 type = idef->il[F_CONSTR].iatoms[3*i];
447 /* dh/dl contribution from constraint force is dh/dr (constraint force) dot dr/dl */
448 /* constraint force is -\sum_i lagr_i* d(constraint)/dr, with constrant = r^2-d^2 */
449 /* constraint force is -\sum_i lagr_i* 2 r */
450 /* so dh/dl = -\sum_i lagr_i* 2 r * dr/dl */
451 /* However, by comparison with lincs and with
452 comparison with a full thermodynamics cycle (see
453 redmine issue #1255), this is off by a factor of
454 two -- the 2r should apparently just be r. Further
455 investigation should be done at some point to
456 understand why and see if there is something deeper
459 bondA = idef->iparams[type].constr.dA;
460 bondB = idef->iparams[type].constr.dB;
461 dvdl += lagr[i] * dt_2 * ((1.0-lambda)*bondA + lambda*bondB) * (bondB-bondA);
467 fprintf(log, "tnit: %5d omega: %10.5f\n", tnit, omega);
471 if (tnit > shaked->gamma)
473 shaked->delta *= -0.5;
475 shaked->omega += shaked->delta;
476 shaked->gamma = tnit;
478 inc_nrnb(nrnb, eNR_SHAKE, tnit);
479 inc_nrnb(nrnb, eNR_SHAKE_RIJ, trij);
482 inc_nrnb(nrnb, eNR_CONSTR_V, trij*2);
486 inc_nrnb(nrnb, eNR_CONSTR_VIR, trij);
492 void crattle(atom_id iatom[], int ncon, int *nnit, int maxnit,
493 real dist2[], real vp[], real rij[], real m2[], real omega,
494 real invmass[], real tt[], real lagr[], int *nerror, real invdt, t_vetavars *vetavar)
497 * r.c. van schaik and w.f. van gunsteren
500 * Adapted for use with Gromacs by David van der Spoel november 92 and later.
501 * rattle added by M.R. Shirts, April 2004, from code written by Jay Ponder in TINKER
502 * second part of rattle algorithm
505 const real mytol = 1e-10;
507 int ll, i, j, i3, j3, l3, ii;
508 int ix, iy, iz, jx, jy, jz;
509 real toler, rijd, vpijd, vx, vy, vz, diff, acor, xdotd, fac, im, jm, imdt, jmdt;
510 real xh, yh, zh, rijx, rijy, rijz;
513 int nit, error, nconv;
514 real veta, vscale_nhc, iconvf;
516 veta = vetavar->veta;
517 vscale_nhc = vetavar->vscale_nhc[0]; /* for now, just use the first state */
521 for (nit = 0; (nit < maxnit) && (nconv != 0) && (error == 0); nit++)
524 for (ll = 0; (ll < ncon) && (error == 0); ll++)
544 vpijd = vx*rijx+vy*rijy+vz*rijz;
546 /* this is r(t+dt) \dotproduct \dot{r}(t+dt) */
547 xdotd = vpijd*vscale_nhc + veta*toler;
549 /* iconv is zero when the error is smaller than a bound */
550 iconvf = fabs(xdotd)*(tt[ll]/invdt);
555 fac = omega*2.0*m2[ll]/toler;
563 im = invmass[i]/vscale_nhc;
564 jm = invmass[j]/vscale_nhc;