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.
43 #include "gromacs/legacyheaders/typedefs.h"
44 #include "gromacs/utility/smalloc.h"
45 #include "gromacs/legacyheaders/txtdump.h"
46 #include "gromacs/math/vec.h"
47 #include "gromacs/legacyheaders/nrnb.h"
48 #include "gromacs/legacyheaders/constr.h"
50 typedef struct gmx_shakedata
63 gmx_shakedata_t shake_init()
75 /* SOR initialization */
83 static void pv(FILE *log, char *s, rvec x)
87 fprintf(log, "%5s:", s);
88 for (m = 0; (m < DIM); m++)
90 fprintf(log, " %10.3f", x[m]);
96 void cshake(atom_id iatom[], int ncon, int *nnit, int maxnit,
97 real dist2[], real xp[], real rij[], real m2[], real omega,
98 real invmass[], real tt[], real lagr[], int *nerror)
101 * r.c. van schaik and w.f. van gunsteren
104 * Adapted for use with Gromacs by David van der Spoel november 92 and later.
106 /* default should be increased! MRS 8/4/2009 */
107 const real mytol = 1e-10;
109 int ll, i, j, i3, j3, l3;
110 int ix, iy, iz, jx, jy, jz;
111 real toler, rpij2, rrpr, tx, ty, tz, diff, acor, im, jm;
112 real xh, yh, zh, rijx, rijy, rijz;
115 int nit, error, nconv;
120 for (nit = 0; (nit < maxnit) && (nconv != 0) && (error == 0); nit++)
123 for (ll = 0; (ll < ncon) && (error == 0); ll++)
143 rpij2 = tx*tx+ty*ty+tz*tz;
147 /* iconvf is less than 1 when the error is smaller than a bound */
148 /* But if tt is too big, then it will result in looping in iconv */
150 iconvf = fabs(diff)*tt[ll];
155 rrpr = rijx*tx+rijy*ty+rijz*tz;
157 if (rrpr < toler*mytol)
163 acor = omega*diff*m2[ll]/rrpr;
184 int vec_shakef(FILE *fplog, gmx_shakedata_t shaked,
185 real invmass[], int ncon,
186 t_iparams ip[], t_iatom *iatom,
187 real tol, rvec x[], rvec prime[], real omega,
188 gmx_bool bFEP, real lambda, real lagr[],
190 gmx_bool bCalcVir, tensor vir_r_m_dr, int econq,
194 real *M2, *tt, *dist2;
196 int nit = 0, ll, i, j, type;
198 real L1, tol2, toler;
201 real g, vscale, rscale, rvscale;
203 if (ncon > shaked->nalloc)
205 shaked->nalloc = over_alloc_dd(ncon);
206 srenew(shaked->rij, shaked->nalloc);
207 srenew(shaked->M2, shaked->nalloc);
208 srenew(shaked->tt, shaked->nalloc);
209 srenew(shaked->dist2, shaked->nalloc);
214 dist2 = shaked->dist2;
219 for (ll = 0; (ll < ncon); ll++, ia += 3)
225 mm = 2*(invmass[i]+invmass[j]);
226 rij[ll][XX] = x[i][XX]-x[j][XX];
227 rij[ll][YY] = x[i][YY]-x[j][YY];
228 rij[ll][ZZ] = x[i][ZZ]-x[j][ZZ];
232 toler = sqr(L1*ip[type].constr.dA + lambda*ip[type].constr.dB);
236 toler = sqr(ip[type].constr.dA);
239 tt[ll] = 1.0/(toler*tol2);
245 cshake(iatom, ncon, &nit, maxnit, dist2, prime[0], rij[0], M2, omega, invmass, tt, lagr, &error);
248 crattle(iatom, ncon, &nit, maxnit, dist2, prime[0], rij[0], M2, omega, invmass, tt, lagr, &error, invdt, vetavar);
256 fprintf(fplog, "Shake did not converge in %d steps\n", maxnit);
258 fprintf(stderr, "Shake did not converge in %d steps\n", maxnit);
265 fprintf(fplog, "Inner product between old and new vector <= 0.0!\n"
266 "constraint #%d atoms %u and %u\n",
267 error-1, iatom[3*(error-1)+1]+1, iatom[3*(error-1)+2]+1);
269 fprintf(stderr, "Inner product between old and new vector <= 0.0!\n"
270 "constraint #%d atoms %u and %u\n",
271 error-1, iatom[3*(error-1)+1]+1, iatom[3*(error-1)+2]+1);
275 /* Constraint virial and correct the lagrange multipliers for the length */
279 for (ll = 0; (ll < ncon); ll++, ia += 3)
282 if ((econq == econqCoord) && v != NULL)
284 /* Correct the velocities */
285 mm = lagr[ll]*invmass[ia[1]]*invdt/vetavar->rscale;
286 for (i = 0; i < DIM; i++)
288 v[ia[1]][i] += mm*rij[ll][i];
290 mm = lagr[ll]*invmass[ia[2]]*invdt/vetavar->rscale;
291 for (i = 0; i < DIM; i++)
293 v[ia[2]][i] -= mm*rij[ll][i];
298 /* constraint virial */
301 if (econq == econqCoord)
303 mm = lagr[ll]/vetavar->rvscale;
305 if (econq == econqVeloc)
307 mm = lagr[ll]/(vetavar->vscale*vetavar->vscale_nhc[0]);
309 for (i = 0; i < DIM; i++)
312 for (j = 0; j < DIM; j++)
314 vir_r_m_dr[i][j] -= tmp*rij[ll][j];
320 /* Correct the lagrange multipliers for the length */
321 /* (more details would be useful here . . . )*/
326 toler = L1*ip[type].constr.dA + lambda*ip[type].constr.dB;
330 toler = ip[type].constr.dA;
338 static void check_cons(FILE *log, int nc, rvec x[], rvec prime[], rvec v[],
339 t_iparams ip[], t_iatom *iatom,
340 real invmass[], int econq)
349 " i mi j mj before after should be\n");
351 for (i = 0; (i < nc); i++, ia += 3)
355 rvec_sub(x[ai], x[aj], dx);
361 rvec_sub(prime[ai], prime[aj], dx);
363 fprintf(log, "%5d %5.2f %5d %5.2f %10.5f %10.5f %10.5f\n",
364 ai+1, 1.0/invmass[ai],
365 aj+1, 1.0/invmass[aj], d, dp, ip[ia[0]].constr.dA);
368 rvec_sub(v[ai], v[aj], dv);
370 rvec_sub(prime[ai], prime[aj], dv);
372 fprintf(log, "%5d %5.2f %5d %5.2f %10.5f %10.5f %10.5f\n",
373 ai+1, 1.0/invmass[ai],
374 aj+1, 1.0/invmass[aj], d, dp, 0.);
380 gmx_bool bshakef(FILE *log, gmx_shakedata_t shaked,
381 real invmass[], int nblocks, int sblock[],
382 t_idef *idef, t_inputrec *ir, rvec x_s[], rvec prime[],
383 t_nrnb *nrnb, real *lagr, real lambda, real *dvdlambda,
384 real invdt, rvec *v, gmx_bool bCalcVir, tensor vir_r_m_dr,
385 gmx_bool bDumpOnError, int econq, t_vetavars *vetavar)
388 real *lam, dt_2, dvdl;
389 int i, n0, ncons, blen, type;
390 int tnit = 0, trij = 0;
393 fprintf(log, "nblocks=%d, sblock[0]=%d\n", nblocks, sblock[0]);
396 ncons = idef->il[F_CONSTR].nr/3;
398 for (i = 0; i < ncons; i++)
403 iatoms = &(idef->il[F_CONSTR].iatoms[sblock[0]]);
405 for (i = 0; (i < nblocks); )
407 blen = (sblock[i+1]-sblock[i]);
409 n0 = vec_shakef(log, shaked, invmass, blen, idef->iparams,
410 iatoms, ir->shake_tol, x_s, prime, shaked->omega,
411 ir->efep != efepNO, lambda, lam, invdt, v, bCalcVir, vir_r_m_dr,
415 check_cons(log, blen, x_s, prime, v, idef->iparams, iatoms, invmass, econq);
420 if (bDumpOnError && log)
423 check_cons(log, blen, x_s, prime, v, idef->iparams, iatoms, invmass, econq);
430 iatoms += 3*blen; /* Increment pointer! */
434 /* only for position part? */
435 if (econq == econqCoord)
437 if (ir->efep != efepNO)
440 dt_2 = 1/sqr(ir->delta_t);
442 for (i = 0; i < ncons; i++)
444 type = idef->il[F_CONSTR].iatoms[3*i];
446 /* dh/dl contribution from constraint force is dh/dr (constraint force) dot dr/dl */
447 /* constraint force is -\sum_i lagr_i* d(constraint)/dr, with constrant = r^2-d^2 */
448 /* constraint force is -\sum_i lagr_i* 2 r */
449 /* so dh/dl = -\sum_i lagr_i* 2 r * dr/dl */
450 /* However, by comparison with lincs and with
451 comparison with a full thermodynamics cycle (see
452 redmine issue #1255), this is off by a factor of
453 two -- the 2r should apparently just be r. Further
454 investigation should be done at some point to
455 understand why and see if there is something deeper
458 bondA = idef->iparams[type].constr.dA;
459 bondB = idef->iparams[type].constr.dB;
460 dvdl += lagr[i] * dt_2 * ((1.0-lambda)*bondA + lambda*bondB) * (bondB-bondA);
466 fprintf(log, "tnit: %5d omega: %10.5f\n", tnit, omega);
470 if (tnit > shaked->gamma)
472 shaked->delta *= -0.5;
474 shaked->omega += shaked->delta;
475 shaked->gamma = tnit;
477 inc_nrnb(nrnb, eNR_SHAKE, tnit);
478 inc_nrnb(nrnb, eNR_SHAKE_RIJ, trij);
481 inc_nrnb(nrnb, eNR_CONSTR_V, trij*2);
485 inc_nrnb(nrnb, eNR_CONSTR_VIR, trij);
491 void crattle(atom_id iatom[], int ncon, int *nnit, int maxnit,
492 real dist2[], real vp[], real rij[], real m2[], real omega,
493 real invmass[], real tt[], real lagr[], int *nerror, real invdt, t_vetavars *vetavar)
496 * r.c. van schaik and w.f. van gunsteren
499 * Adapted for use with Gromacs by David van der Spoel november 92 and later.
500 * rattle added by M.R. Shirts, April 2004, from code written by Jay Ponder in TINKER
501 * second part of rattle algorithm
504 const real mytol = 1e-10;
506 int ll, i, j, i3, j3, l3, ii;
507 int ix, iy, iz, jx, jy, jz;
508 real toler, rijd, vpijd, vx, vy, vz, diff, acor, xdotd, fac, im, jm, imdt, jmdt;
509 real xh, yh, zh, rijx, rijy, rijz;
512 int nit, error, nconv;
513 real veta, vscale_nhc, iconvf;
515 veta = vetavar->veta;
516 vscale_nhc = vetavar->vscale_nhc[0]; /* for now, just use the first state */
520 for (nit = 0; (nit < maxnit) && (nconv != 0) && (error == 0); nit++)
523 for (ll = 0; (ll < ncon) && (error == 0); ll++)
543 vpijd = vx*rijx+vy*rijy+vz*rijz;
545 /* this is r(t+dt) \dotproduct \dot{r}(t+dt) */
546 xdotd = vpijd*vscale_nhc + veta*toler;
548 /* iconv is zero when the error is smaller than a bound */
549 iconvf = fabs(xdotd)*(tt[ll]/invdt);
554 fac = omega*2.0*m2[ll]/toler;
562 im = invmass[i]/vscale_nhc;
563 jm = invmass[j]/vscale_nhc;