1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROwing Monsters And Cloning Shrimps
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
++)
89 fprintf(log
," %10.3f",x
[m
]);
94 void cshake(atom_id iatom
[],int ncon
,int *nnit
,int maxnit
,
95 real dist2
[],real xp
[],real rij
[],real m2
[],real omega
,
96 real invmass
[],real tt
[],real lagr
[],int *nerror
)
99 * r.c. van schaik and w.f. van gunsteren
102 * Adapted for use with Gromacs by David van der Spoel november 92 and later.
104 /* default should be increased! MRS 8/4/2009 */
105 const real mytol
=1e-10;
108 int ix
,iy
,iz
,jx
,jy
,jz
;
109 real toler
,rpij2
,rrpr
,tx
,ty
,tz
,diff
,acor
,im
,jm
;
110 real xh
,yh
,zh
,rijx
,rijy
,rijz
;
113 int nit
,error
,iconv
,nconv
;
117 for (nit
=0; (nit
<maxnit
) && (nconv
!= 0) && (error
== 0); nit
++) {
119 for(ll
=0; (ll
<ncon
) && (error
== 0); ll
++) {
138 rpij2
= tx
*tx
+ty
*ty
+tz
*tz
;
142 /* iconv is zero when the error is smaller than a bound */
143 iconv
= fabs(diff
)*tt
[ll
];
146 nconv
= nconv
+ iconv
;
147 rrpr
= rijx
*tx
+rijy
*ty
+rijz
*tz
;
149 if (rrpr
< toler
*mytol
)
152 acor
= omega
*diff
*m2
[ll
]/rrpr
;
173 int vec_shakef(FILE *fplog
,gmx_shakedata_t shaked
,
174 int natoms
,real invmass
[],int ncon
,
175 t_iparams ip
[],t_iatom
*iatom
,
176 real tol
,rvec x
[],rvec prime
[],real omega
,
177 bool bFEP
,real lambda
,real lagr
[],
179 bool bCalcVir
,tensor rmdr
,int econq
,
185 int nit
=0,ll
,i
,j
,type
;
190 real g
,vscale
,rscale
,rvscale
;
192 if (ncon
> shaked
->nalloc
)
194 shaked
->nalloc
= over_alloc_dd(ncon
);
195 srenew(shaked
->rij
,shaked
->nalloc
);
196 srenew(shaked
->M2
,shaked
->nalloc
);
197 srenew(shaked
->tt
,shaked
->nalloc
);
198 srenew(shaked
->dist2
,shaked
->nalloc
);
203 dist2
= shaked
->dist2
;
208 for(ll
=0; (ll
<ncon
); ll
++,ia
+=3) {
213 mm
=2*(invmass
[i
]+invmass
[j
]);
214 rij
[ll
][XX
]=x
[i
][XX
]-x
[j
][XX
];
215 rij
[ll
][YY
]=x
[i
][YY
]-x
[j
][YY
];
216 rij
[ll
][ZZ
]=x
[i
][ZZ
]-x
[j
][ZZ
];
219 toler
= sqr(L1
*ip
[type
].constr
.dA
+ lambda
*ip
[type
].constr
.dB
);
221 toler
= sqr(ip
[type
].constr
.dA
);
223 tt
[ll
] = 1.0/(toler
*tol2
);
228 cshake(iatom
,ncon
,&nit
,maxnit
,dist2
,prime
[0],rij
[0],M2
,omega
,invmass
,tt
,lagr
,&error
);
231 crattle(iatom
,ncon
,&nit
,maxnit
,dist2
,prime
[0],rij
[0],M2
,omega
,invmass
,tt
,lagr
,&error
,invdt
,vetavar
);
237 fprintf(fplog
,"Shake did not converge in %d steps\n",maxnit
);
239 fprintf(stderr
,"Shake did not converge in %d steps\n",maxnit
);
245 fprintf(fplog
,"Inner product between old and new vector <= 0.0!\n"
246 "constraint #%d atoms %u and %u\n",
247 error
-1,iatom
[3*(error
-1)+1]+1,iatom
[3*(error
-1)+2]+1);
248 fprintf(stderr
,"Inner product between old and new vector <= 0.0!\n"
249 "constraint #%d atoms %u and %u\n",
250 error
-1,iatom
[3*(error
-1)+1]+1,iatom
[3*(error
-1)+2]+1);
254 /* Constraint virial and correct the lagrange multipliers for the length */
258 for(ll
=0; (ll
<ncon
); ll
++,ia
+=3)
261 if ((econq
== econqCoord
) && v
!=NULL
)
263 /* Correct the velocities */
264 mm
= lagr
[ll
]*invmass
[ia
[1]]*invdt
/vetavar
->rscale
;
267 v
[ia
[1]][i
] += mm
*rij
[ll
][i
];
269 mm
= lagr
[ll
]*invmass
[ia
[2]]*invdt
/vetavar
->rscale
;
272 v
[ia
[2]][i
] -= mm
*rij
[ll
][i
];
277 /* constraint virial */
280 if (econq
== econqCoord
)
282 mm
= lagr
[ll
]/vetavar
->rvscale
;
284 if (econq
== econqVeloc
)
286 mm
= lagr
[ll
]/(vetavar
->vscale
*vetavar
->vscale_nhc
[0]);
293 rmdr
[i
][j
] -= tmp
*rij
[ll
][j
];
299 /* Correct the lagrange multipliers for the length */
300 /* (more details would be useful here . . . )*/
305 toler
= L1
*ip
[type
].constr
.dA
+ lambda
*ip
[type
].constr
.dB
;
309 toler
= ip
[type
].constr
.dA
;
317 static void check_cons(FILE *log
,int nc
,rvec x
[],rvec prime
[], rvec v
[],
318 t_iparams ip
[],t_iatom
*iatom
,
319 real invmass
[], int econq
)
328 " i mi j mj before after should be\n");
330 for(i
=0; (i
<nc
); i
++,ia
+=3) {
333 rvec_sub(x
[ai
],x
[aj
],dx
);
338 rvec_sub(prime
[ai
],prime
[aj
],dx
);
340 fprintf(log
,"%5d %5.2f %5d %5.2f %10.5f %10.5f %10.5f\n",
341 ai
+1,1.0/invmass
[ai
],
342 aj
+1,1.0/invmass
[aj
],d
,dp
,ip
[ia
[0]].constr
.dA
);
345 rvec_sub(v
[ai
],v
[aj
],dv
);
347 rvec_sub(prime
[ai
],prime
[aj
],dv
);
349 fprintf(log
,"%5d %5.2f %5d %5.2f %10.5f %10.5f %10.5f\n",
350 ai
+1,1.0/invmass
[ai
],
351 aj
+1,1.0/invmass
[aj
],d
,dp
,0.);
357 bool bshakef(FILE *log
,gmx_shakedata_t shaked
,
358 int natoms
,real invmass
[],int nblocks
,int sblock
[],
359 t_idef
*idef
,t_inputrec
*ir
,matrix box
,rvec x_s
[],rvec prime
[],
360 t_nrnb
*nrnb
,real
*lagr
,real lambda
,real
*dvdlambda
,
361 real invdt
,rvec
*v
,bool bCalcVir
,tensor rmdr
,bool bDumpOnError
,int econq
,t_vetavars
*vetavar
)
365 int i
,n0
,ncons
,blen
,type
;
369 fprintf(log
,"nblocks=%d, sblock[0]=%d\n",nblocks
,sblock
[0]);
372 ncons
=idef
->il
[F_CONSTR
].nr
/3;
374 for(i
=0; i
<ncons
; i
++)
377 iatoms
= &(idef
->il
[F_CONSTR
].iatoms
[sblock
[0]]);
379 for(i
=0; (i
<nblocks
); ) {
380 blen
= (sblock
[i
+1]-sblock
[i
]);
382 n0
= vec_shakef(log
,shaked
,natoms
,invmass
,blen
,idef
->iparams
,
383 iatoms
,ir
->shake_tol
,x_s
,prime
,shaked
->omega
,
384 ir
->efep
!=efepNO
,lambda
,lam
,invdt
,v
,bCalcVir
,rmdr
,econq
,vetavar
);
387 check_cons(log
,blen
,x_s
,prime
,v
,idef
->iparams
,iatoms
,invmass
,econq
);
391 if (bDumpOnError
&& log
)
394 check_cons(log
,blen
,x_s
,prime
,v
,idef
->iparams
,iatoms
,invmass
,econq
);
401 iatoms
+= 3*blen
; /* Increment pointer! */
405 /* only for position part? */
406 if (econq
== econqCoord
) {
407 if (ir
->efep
!= efepNO
) {
408 dt_2
= 1/sqr(ir
->delta_t
);
410 for(i
=0; i
<ncons
; i
++) {
411 type
= idef
->il
[F_CONSTR
].iatoms
[3*i
];
412 dvdl
+= lagr
[i
]*dt_2
*
413 (idef
->iparams
[type
].constr
.dB
-idef
->iparams
[type
].constr
.dA
);
419 fprintf(log
,"tnit: %5d omega: %10.5f\n",tnit
,omega
);
422 if (tnit
> shaked
->gamma
) {
423 shaked
->delta
*= -0.5;
425 shaked
->omega
+= shaked
->delta
;
426 shaked
->gamma
= tnit
;
428 inc_nrnb(nrnb
,eNR_SHAKE
,tnit
);
429 inc_nrnb(nrnb
,eNR_SHAKE_RIJ
,trij
);
431 inc_nrnb(nrnb
,eNR_CONSTR_V
,trij
*2);
433 inc_nrnb(nrnb
,eNR_CONSTR_VIR
,trij
);
438 void crattle(atom_id iatom
[],int ncon
,int *nnit
,int maxnit
,
439 real dist2
[],real vp
[],real rij
[],real m2
[],real omega
,
440 real invmass
[],real tt
[],real lagr
[],int *nerror
,real invdt
,t_vetavars
*vetavar
)
443 * r.c. van schaik and w.f. van gunsteren
446 * Adapted for use with Gromacs by David van der Spoel november 92 and later.
447 * rattle added by M.R. Shirts, April 2004, from code written by Jay Ponder in TINKER
448 * second part of rattle algorithm
451 const real mytol
=1e-10;
453 int ll
,i
,j
,i3
,j3
,l3
,ii
;
454 int ix
,iy
,iz
,jx
,jy
,jz
;
455 real toler
,rijd
,vpijd
, vx
,vy
,vz
,diff
,acor
,xdotd
,fac
,im
,jm
,imdt
,jmdt
;
456 real xh
,yh
,zh
,rijx
,rijy
,rijz
;
459 int nit
,error
,iconv
,nconv
;
460 real veta
,vscale_nhc
;
462 veta
= vetavar
->veta
;
463 vscale_nhc
= vetavar
->vscale_nhc
[0]; /* for now, just use the first state */
467 for (nit
=0; (nit
<maxnit
) && (nconv
!= 0) && (error
== 0); nit
++) {
469 for(ll
=0; (ll
<ncon
) && (error
== 0); ll
++) {
488 vpijd
= vx
*rijx
+vy
*rijy
+vz
*rijz
;
490 /* this is r(t+dt) \dotproduct \dot{r}(t+dt) */
491 xdotd
= vpijd
*vscale_nhc
+ veta
*toler
;
493 /* iconv is zero when the error is smaller than a bound */
494 iconv
= fabs(xdotd
)*(tt
[ll
]/invdt
);
497 nconv
= nconv
+ iconv
;
498 fac
= omega
*2.0*m2
[ll
]/toler
;
506 im
= invmass
[i
]/vscale_nhc
;
507 jm
= invmass
[j
]/vscale_nhc
;