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 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * 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.
52 typedef struct gmx_shakedata
65 gmx_shakedata_t shake_init()
77 /* SOR initialization */
85 static void pv(FILE *log,char *s,rvec x)
89 fprintf(log,"%5s:",s);
90 for(m=0; (m<DIM); m++)
91 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;
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;
120 for (nit=0; (nit<maxnit) && (nconv != 0) && (error == 0); nit++) {
122 for(ll=0; (ll<ncon) && (error == 0); ll++) {
141 rpij2 = tx*tx+ty*ty+tz*tz;
145 /* iconvf is less than 1 when the error is smaller than a bound */
146 /* But if tt is too big, then it will result in looping in iconv */
148 iconvf = fabs(diff)*tt[ll];
152 rrpr = rijx*tx+rijy*ty+rijz*tz;
154 if (rrpr < toler*mytol)
157 acor = omega*diff*m2[ll]/rrpr;
178 int vec_shakef(FILE *fplog,gmx_shakedata_t shaked,
179 int natoms,real invmass[],int ncon,
180 t_iparams ip[],t_iatom *iatom,
181 real tol,rvec x[],rvec prime[],real omega,
182 gmx_bool bFEP,real lambda,real lagr[],
184 gmx_bool bCalcVir,tensor vir_r_m_dr,int econq,
190 int nit=0,ll,i,j,type;
195 real g,vscale,rscale,rvscale;
197 if (ncon > shaked->nalloc)
199 shaked->nalloc = over_alloc_dd(ncon);
200 srenew(shaked->rij,shaked->nalloc);
201 srenew(shaked->M2,shaked->nalloc);
202 srenew(shaked->tt,shaked->nalloc);
203 srenew(shaked->dist2,shaked->nalloc);
208 dist2 = shaked->dist2;
213 for(ll=0; (ll<ncon); ll++,ia+=3) {
218 mm=2*(invmass[i]+invmass[j]);
219 rij[ll][XX]=x[i][XX]-x[j][XX];
220 rij[ll][YY]=x[i][YY]-x[j][YY];
221 rij[ll][ZZ]=x[i][ZZ]-x[j][ZZ];
224 toler = sqr(L1*ip[type].constr.dA + lambda*ip[type].constr.dB);
226 toler = sqr(ip[type].constr.dA);
228 tt[ll] = 1.0/(toler*tol2);
233 cshake(iatom,ncon,&nit,maxnit,dist2,prime[0],rij[0],M2,omega,invmass,tt,lagr,&error);
236 crattle(iatom,ncon,&nit,maxnit,dist2,prime[0],rij[0],M2,omega,invmass,tt,lagr,&error,invdt,vetavar);
242 fprintf(fplog,"Shake did not converge in %d steps\n",maxnit);
244 fprintf(stderr,"Shake did not converge in %d steps\n",maxnit);
250 fprintf(fplog,"Inner product between old and new vector <= 0.0!\n"
251 "constraint #%d atoms %u and %u\n",
252 error-1,iatom[3*(error-1)+1]+1,iatom[3*(error-1)+2]+1);
253 fprintf(stderr,"Inner product between old and new vector <= 0.0!\n"
254 "constraint #%d atoms %u and %u\n",
255 error-1,iatom[3*(error-1)+1]+1,iatom[3*(error-1)+2]+1);
259 /* Constraint virial and correct the lagrange multipliers for the length */
263 for(ll=0; (ll<ncon); ll++,ia+=3)
266 if ((econq == econqCoord) && v!=NULL)
268 /* Correct the velocities */
269 mm = lagr[ll]*invmass[ia[1]]*invdt/vetavar->rscale;
272 v[ia[1]][i] += mm*rij[ll][i];
274 mm = lagr[ll]*invmass[ia[2]]*invdt/vetavar->rscale;
277 v[ia[2]][i] -= mm*rij[ll][i];
282 /* constraint virial */
285 if (econq == econqCoord)
287 mm = lagr[ll]/vetavar->rvscale;
289 if (econq == econqVeloc)
291 mm = lagr[ll]/(vetavar->vscale*vetavar->vscale_nhc[0]);
298 vir_r_m_dr[i][j] -= tmp*rij[ll][j];
304 /* Correct the lagrange multipliers for the length */
305 /* (more details would be useful here . . . )*/
310 toler = L1*ip[type].constr.dA + lambda*ip[type].constr.dB;
314 toler = ip[type].constr.dA;
322 static void check_cons(FILE *log,int nc,rvec x[],rvec prime[], rvec v[],
323 t_iparams ip[],t_iatom *iatom,
324 real invmass[], int econq)
333 " i mi j mj before after should be\n");
335 for(i=0; (i<nc); i++,ia+=3) {
338 rvec_sub(x[ai],x[aj],dx);
343 rvec_sub(prime[ai],prime[aj],dx);
345 fprintf(log,"%5d %5.2f %5d %5.2f %10.5f %10.5f %10.5f\n",
346 ai+1,1.0/invmass[ai],
347 aj+1,1.0/invmass[aj],d,dp,ip[ia[0]].constr.dA);
350 rvec_sub(v[ai],v[aj],dv);
352 rvec_sub(prime[ai],prime[aj],dv);
354 fprintf(log,"%5d %5.2f %5d %5.2f %10.5f %10.5f %10.5f\n",
355 ai+1,1.0/invmass[ai],
356 aj+1,1.0/invmass[aj],d,dp,0.);
362 gmx_bool bshakef(FILE *log,gmx_shakedata_t shaked,
363 int natoms,real invmass[],int nblocks,int sblock[],
364 t_idef *idef,t_inputrec *ir,rvec x_s[],rvec prime[],
365 t_nrnb *nrnb,real *lagr,real lambda,real *dvdlambda,
366 real invdt,rvec *v,gmx_bool bCalcVir,tensor vir_r_m_dr,
367 gmx_bool bDumpOnError,int econq,t_vetavars *vetavar)
371 int i,n0,ncons,blen,type;
375 fprintf(log,"nblocks=%d, sblock[0]=%d\n",nblocks,sblock[0]);
378 ncons=idef->il[F_CONSTR].nr/3;
380 for(i=0; i<ncons; i++)
383 iatoms = &(idef->il[F_CONSTR].iatoms[sblock[0]]);
385 for(i=0; (i<nblocks); ) {
386 blen = (sblock[i+1]-sblock[i]);
388 n0 = vec_shakef(log,shaked,natoms,invmass,blen,idef->iparams,
389 iatoms,ir->shake_tol,x_s,prime,shaked->omega,
390 ir->efep!=efepNO,lambda,lam,invdt,v,bCalcVir,vir_r_m_dr,
394 check_cons(log,blen,x_s,prime,v,idef->iparams,iatoms,invmass,econq);
398 if (bDumpOnError && log)
401 check_cons(log,blen,x_s,prime,v,idef->iparams,iatoms,invmass,econq);
408 iatoms += 3*blen; /* Increment pointer! */
412 /* only for position part? */
413 if (econq == econqCoord) {
414 if (ir->efep != efepNO) {
415 dt_2 = 1/sqr(ir->delta_t);
417 for(i=0; i<ncons; i++) {
418 type = idef->il[F_CONSTR].iatoms[3*i];
419 dvdl += lagr[i]*dt_2*
420 (idef->iparams[type].constr.dB-idef->iparams[type].constr.dA);
426 fprintf(log,"tnit: %5d omega: %10.5f\n",tnit,omega);
429 if (tnit > shaked->gamma) {
430 shaked->delta *= -0.5;
432 shaked->omega += shaked->delta;
433 shaked->gamma = tnit;
435 inc_nrnb(nrnb,eNR_SHAKE,tnit);
436 inc_nrnb(nrnb,eNR_SHAKE_RIJ,trij);
438 inc_nrnb(nrnb,eNR_CONSTR_V,trij*2);
440 inc_nrnb(nrnb,eNR_CONSTR_VIR,trij);
445 void crattle(atom_id iatom[],int ncon,int *nnit,int maxnit,
446 real dist2[],real vp[],real rij[],real m2[],real omega,
447 real invmass[],real tt[],real lagr[],int *nerror,real invdt,t_vetavars *vetavar)
450 * r.c. van schaik and w.f. van gunsteren
453 * Adapted for use with Gromacs by David van der Spoel november 92 and later.
454 * rattle added by M.R. Shirts, April 2004, from code written by Jay Ponder in TINKER
455 * second part of rattle algorithm
458 const real mytol=1e-10;
460 int ll,i,j,i3,j3,l3,ii;
461 int ix,iy,iz,jx,jy,jz;
462 real toler,rijd,vpijd, vx,vy,vz,diff,acor,xdotd,fac,im,jm,imdt,jmdt;
463 real xh,yh,zh,rijx,rijy,rijz;
467 real veta,vscale_nhc,iconvf;
469 veta = vetavar->veta;
470 vscale_nhc = vetavar->vscale_nhc[0]; /* for now, just use the first state */
474 for (nit=0; (nit<maxnit) && (nconv != 0) && (error == 0); nit++) {
476 for(ll=0; (ll<ncon) && (error == 0); ll++) {
495 vpijd = vx*rijx+vy*rijy+vz*rijz;
497 /* this is r(t+dt) \dotproduct \dot{r}(t+dt) */
498 xdotd = vpijd*vscale_nhc + veta*toler;
500 /* iconv is zero when the error is smaller than a bound */
501 iconvf = fabs(xdotd)*(tt[ll]/invdt);
505 fac = omega*2.0*m2[ll]/toler;
513 im = invmass[i]/vscale_nhc;
514 jm = invmass[j]/vscale_nhc;