Fixing copyright issues and code contributors
[alexxy/gromacs.git] / src / mdlib / coupling.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
11  *
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.
16  *
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.
21  *
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.
26  *
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.
34  *
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.
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41 #include <assert.h>
42
43 #include "typedefs.h"
44 #include "smalloc.h"
45 #include "update.h"
46 #include "vec.h"
47 #include "macros.h"
48 #include "physics.h"
49 #include "names.h"
50 #include "gmx_fatal.h"
51 #include "txtdump.h"
52 #include "nrnb.h"
53 #include "gmx_random.h"
54 #include "update.h"
55 #include "mdrun.h"
56
57 #define NTROTTERPARTS 3
58
59 /* these integration routines are only referenced inside this file */
60 static void NHC_trotter(t_grpopts *opts,int nvar, gmx_ekindata_t *ekind,real dtfull,
61                         double xi[],double vxi[], double scalefac[], real *veta, t_extmass *MassQ, gmx_bool bEkinAveVel)
62
63 {
64     /* general routine for both barostat and thermostat nose hoover chains */
65
66     int   i,j,mi,mj,jmax;
67     double Ekin,Efac,reft,kT,nd;
68     double dt;
69     t_grp_tcstat *tcstat;
70     double *ivxi,*ixi;
71     double *iQinv;
72     double *GQ;
73     gmx_bool bBarostat;
74     int mstepsi, mstepsj;
75     int ns = SUZUKI_YOSHIDA_NUM;  /* set the degree of integration in the types/state.h file */
76     int nh = opts->nhchainlength;
77     
78     snew(GQ,nh);
79     mstepsi = mstepsj = ns;
80
81 /* if scalefac is NULL, we are doing the NHC of the barostat */
82     
83     bBarostat = FALSE;
84     if (scalefac == NULL) {
85         bBarostat = TRUE;
86     }
87
88     for (i=0; i<nvar; i++) 
89     {
90
91         /* make it easier to iterate by selecting 
92            out the sub-array that corresponds to this T group */
93         
94         ivxi = &vxi[i*nh];
95         ixi = &xi[i*nh];
96         if (bBarostat) {
97             iQinv = &(MassQ->QPinv[i*nh]); 
98             nd = 1.0; /* THIS WILL CHANGE IF NOT ISOTROPIC */
99             reft = max(0.0,opts->ref_t[0]);
100             Ekin = sqr(*veta)/MassQ->Winv;
101         } else {
102             iQinv = &(MassQ->Qinv[i*nh]);  
103             tcstat = &ekind->tcstat[i];
104             nd = opts->nrdf[i];
105             reft = max(0.0,opts->ref_t[i]);
106             if (bEkinAveVel) 
107             {
108                 Ekin = 2*trace(tcstat->ekinf)*tcstat->ekinscalef_nhc;
109             } else {
110                 Ekin = 2*trace(tcstat->ekinh)*tcstat->ekinscaleh_nhc;
111             }
112         }
113         kT = BOLTZ*reft;
114
115         for(mi=0;mi<mstepsi;mi++) 
116         {
117             for(mj=0;mj<mstepsj;mj++)
118             { 
119                 /* weighting for this step using Suzuki-Yoshida integration - fixed at 5 */
120                 dt = sy_const[ns][mj] * dtfull / mstepsi;
121                 
122                 /* compute the thermal forces */
123                 GQ[0] = iQinv[0]*(Ekin - nd*kT);
124                 
125                 for (j=0;j<nh-1;j++) 
126                 {       
127                     if (iQinv[j+1] > 0) {
128                         /* we actually don't need to update here if we save the 
129                            state of the GQ, but it's easier to just recompute*/
130                         GQ[j+1] = iQinv[j+1]*((sqr(ivxi[j])/iQinv[j])-kT);        
131                     } else {
132                         GQ[j+1] = 0;
133                     }
134                 }
135                 
136                 ivxi[nh-1] += 0.25*dt*GQ[nh-1];
137                 for (j=nh-1;j>0;j--) 
138                 { 
139                     Efac = exp(-0.125*dt*ivxi[j]);
140                     ivxi[j-1] = Efac*(ivxi[j-1]*Efac + 0.25*dt*GQ[j-1]);
141                 }
142                 
143                 Efac = exp(-0.5*dt*ivxi[0]);
144                 if (bBarostat) {
145                     *veta *= Efac;                
146                 } else {
147                     scalefac[i] *= Efac;
148                 }
149                 Ekin *= (Efac*Efac);
150                 
151                 /* Issue - if the KE is an average of the last and the current temperatures, then we might not be
152                    able to scale the kinetic energy directly with this factor.  Might take more bookkeeping -- have to
153                    think about this a bit more . . . */
154
155                 GQ[0] = iQinv[0]*(Ekin - nd*kT);
156                 
157                 /* update thermostat positions */
158                 for (j=0;j<nh;j++) 
159                 { 
160                     ixi[j] += 0.5*dt*ivxi[j];
161                 }
162                 
163                 for (j=0;j<nh-1;j++) 
164                 { 
165                     Efac = exp(-0.125*dt*ivxi[j+1]);
166                     ivxi[j] = Efac*(ivxi[j]*Efac + 0.25*dt*GQ[j]);
167                     if (iQinv[j+1] > 0) {
168                         GQ[j+1] = iQinv[j+1]*((sqr(ivxi[j])/iQinv[j])-kT);  
169                     } else {
170                         GQ[j+1] = 0;
171                     }
172                 }
173                 ivxi[nh-1] += 0.25*dt*GQ[nh-1];
174             }
175         }
176     }
177     sfree(GQ);
178 }
179
180 static void boxv_trotter(t_inputrec *ir, real *veta, real dt, tensor box, 
181                          gmx_ekindata_t *ekind, tensor vir, real pcorr, t_extmass *MassQ)
182 {
183
184     real  pscal;
185     double alpha;
186     int   i,j,d,n,nwall;
187     real  T,GW,vol;
188     tensor Winvm,ekinmod,localpres;
189     
190     /* The heat bath is coupled to a separate barostat, the last temperature group.  In the 
191        2006 Tuckerman et al paper., the order is iL_{T_baro} iL {T_part}
192     */
193     
194     if (ir->epct==epctSEMIISOTROPIC) 
195     {
196         nwall = 2;
197     } 
198     else 
199     {
200         nwall = 3;
201     }
202
203     /* eta is in pure units.  veta is in units of ps^-1. GW is in 
204        units of ps^-2.  However, eta has a reference of 1 nm^3, so care must be 
205        taken to use only RATIOS of eta in updating the volume. */
206     
207     /* we take the partial pressure tensors, modify the 
208        kinetic energy tensor, and recovert to pressure */
209     
210     if (ir->opts.nrdf[0]==0) 
211     { 
212         gmx_fatal(FARGS,"Barostat is coupled to a T-group with no degrees of freedom\n");    
213     } 
214     /* alpha factor for phase space volume, then multiply by the ekin scaling factor.  */
215     alpha = 1.0 + DIM/((double)ir->opts.nrdf[0]);
216     alpha *= ekind->tcstat[0].ekinscalef_nhc;
217     msmul(ekind->ekin,alpha,ekinmod);  
218     /* for now, we use Elr = 0, because if you want to get it right, you
219        really should be using PME. Maybe print a warning? */
220
221     pscal   = calc_pres(ir->ePBC,nwall,box,ekinmod,vir,localpres)+pcorr;
222
223     vol = det(box);
224     GW = (vol*(MassQ->Winv/PRESFAC))*(DIM*pscal - trace(ir->ref_p));   /* W is in ps^2 * bar * nm^3 */
225
226     *veta += 0.5*dt*GW;   
227 }
228
229 /* 
230  * This file implements temperature and pressure coupling algorithms:
231  * For now only the Weak coupling and the modified weak coupling.
232  *
233  * Furthermore computation of pressure and temperature is done here
234  *
235  */
236
237 real calc_pres(int ePBC,int nwall,matrix box,tensor ekin,tensor vir,
238                tensor pres)
239 {
240     int  n,m;
241     real fac;
242     
243     if (ePBC==epbcNONE || (ePBC==epbcXY && nwall!=2))
244         clear_mat(pres);
245     else {
246         /* Uitzoeken welke ekin hier van toepassing is, zie Evans & Morris - E. 
247          * Wrs. moet de druktensor gecorrigeerd worden voor de netto stroom in  
248          * het systeem...       
249          */
250         
251         fac=PRESFAC*2.0/det(box);
252         for(n=0; (n<DIM); n++)
253             for(m=0; (m<DIM); m++)
254                 pres[n][m] = (ekin[n][m] - vir[n][m])*fac;
255         
256         if (debug) {
257             pr_rvecs(debug,0,"PC: pres",pres,DIM);
258             pr_rvecs(debug,0,"PC: ekin",ekin,DIM);
259             pr_rvecs(debug,0,"PC: vir ",vir, DIM);
260             pr_rvecs(debug,0,"PC: box ",box, DIM);
261         }
262     }
263     return trace(pres)/DIM;
264 }
265
266 real calc_temp(real ekin,real nrdf)
267 {
268     if (nrdf > 0)
269         return (2.0*ekin)/(nrdf*BOLTZ);
270     else
271         return 0;
272 }
273
274 void parrinellorahman_pcoupl(FILE *fplog,gmx_large_int_t step,
275                              t_inputrec *ir,real dt,tensor pres,
276                              tensor box,tensor box_rel,tensor boxv,
277                              tensor M,matrix mu,gmx_bool bFirstStep)
278 {
279   /* This doesn't do any coordinate updating. It just
280    * integrates the box vector equations from the calculated
281    * acceleration due to pressure difference. We also compute
282    * the tensor M which is used in update to couple the particle
283    * coordinates to the box vectors.
284    *
285    * In Nose and Klein (Mol.Phys 50 (1983) no 5., p 1055) this is
286    * given as
287    *            -1    .           .     -1
288    * M_nk = (h')   * (h' * h + h' h) * h
289    *
290    * with the dots denoting time derivatives and h is the transformation from
291    * the scaled frame to the real frame, i.e. the TRANSPOSE of the box. 
292    * This also goes for the pressure and M tensors - they are transposed relative
293    * to ours. Our equation thus becomes:
294    *
295    *                  -1       .    .           -1
296    * M_gmx = M_nk' = b  * (b * b' + b * b') * b'
297    * 
298    * where b is the gromacs box matrix.                       
299    * Our box accelerations are given by
300    *   ..                                    ..
301    *   b = vol/W inv(box') * (P-ref_P)     (=h')
302    */
303   
304   int    d,n;
305   tensor winv;
306   real   vol=box[XX][XX]*box[YY][YY]*box[ZZ][ZZ];
307   real   atot,arel,change,maxchange,xy_pressure;
308   tensor invbox,pdiff,t1,t2;
309
310   real maxl;
311
312   m_inv_ur0(box,invbox);
313
314   if (!bFirstStep) {
315     /* Note that PRESFAC does not occur here.
316      * The pressure and compressibility always occur as a product,
317      * therefore the pressure unit drops out.
318      */
319     maxl=max(box[XX][XX],box[YY][YY]);
320     maxl=max(maxl,box[ZZ][ZZ]);
321     for(d=0;d<DIM;d++)
322       for(n=0;n<DIM;n++)
323         winv[d][n]=
324           (4*M_PI*M_PI*ir->compress[d][n])/(3*ir->tau_p*ir->tau_p*maxl);
325     
326     m_sub(pres,ir->ref_p,pdiff);
327     
328     if(ir->epct==epctSURFACETENSION) {
329       /* Unlike Berendsen coupling it might not be trivial to include a z
330        * pressure correction here? On the other hand we don't scale the
331        * box momentarily, but change accelerations, so it might not be crucial.
332        */
333       xy_pressure=0.5*(pres[XX][XX]+pres[YY][YY]);
334       for(d=0;d<ZZ;d++)
335         pdiff[d][d]=(xy_pressure-(pres[ZZ][ZZ]-ir->ref_p[d][d]/box[d][d]));
336     }
337     
338     tmmul(invbox,pdiff,t1);
339     /* Move the off-diagonal elements of the 'force' to one side to ensure
340      * that we obey the box constraints.
341      */
342     for(d=0;d<DIM;d++) {
343       for(n=0;n<d;n++) {
344         t1[d][n] += t1[n][d];
345         t1[n][d] = 0;
346       }
347     }
348     
349     switch (ir->epct) {
350     case epctANISOTROPIC:
351       for(d=0;d<DIM;d++) 
352         for(n=0;n<=d;n++)
353           t1[d][n] *= winv[d][n]*vol;
354       break;
355     case epctISOTROPIC:
356       /* calculate total volume acceleration */
357       atot=box[XX][XX]*box[YY][YY]*t1[ZZ][ZZ]+
358         box[XX][XX]*t1[YY][YY]*box[ZZ][ZZ]+
359         t1[XX][XX]*box[YY][YY]*box[ZZ][ZZ];
360       arel=atot/(3*vol);
361       /* set all RELATIVE box accelerations equal, and maintain total V
362        * change speed */
363       for(d=0;d<DIM;d++)
364         for(n=0;n<=d;n++)
365           t1[d][n] = winv[0][0]*vol*arel*box[d][n];    
366       break;
367     case epctSEMIISOTROPIC:
368     case epctSURFACETENSION:
369       /* Note the correction to pdiff above for surftens. coupling  */
370       
371       /* calculate total XY volume acceleration */
372       atot=box[XX][XX]*t1[YY][YY]+t1[XX][XX]*box[YY][YY];
373       arel=atot/(2*box[XX][XX]*box[YY][YY]);
374       /* set RELATIVE XY box accelerations equal, and maintain total V
375        * change speed. Dont change the third box vector accelerations */
376       for(d=0;d<ZZ;d++)
377         for(n=0;n<=d;n++)
378           t1[d][n] = winv[d][n]*vol*arel*box[d][n];
379       for(n=0;n<DIM;n++)
380         t1[ZZ][n] *= winv[d][n]*vol;
381       break;
382     default:
383       gmx_fatal(FARGS,"Parrinello-Rahman pressure coupling type %s "
384                   "not supported yet\n",EPCOUPLTYPETYPE(ir->epct));
385       break;
386     }
387     
388     maxchange=0;
389     for(d=0;d<DIM;d++)
390       for(n=0;n<=d;n++) {
391         boxv[d][n] += dt*t1[d][n];
392         
393         /* We do NOT update the box vectors themselves here, since
394          * we need them for shifting later. It is instead done last
395          * in the update() routine.
396          */
397         
398         /* Calculate the change relative to diagonal elements-
399            since it's perfectly ok for the off-diagonal ones to
400            be zero it doesn't make sense to check the change relative
401            to its current size.
402         */
403         
404         change=fabs(dt*boxv[d][n]/box[d][d]);
405         
406         if (change>maxchange)
407           maxchange=change;
408       }
409     
410     if (maxchange > 0.01 && fplog) {
411       char buf[22];
412       fprintf(fplog,
413               "\nStep %s  Warning: Pressure scaling more than 1%%. "
414               "This may mean your system\n is not yet equilibrated. "
415               "Use of Parrinello-Rahman pressure coupling during\n"
416               "equilibration can lead to simulation instability, "
417               "and is discouraged.\n",
418               gmx_step_str(step,buf));
419     }
420   }
421   
422   preserve_box_shape(ir,box_rel,boxv);
423
424   mtmul(boxv,box,t1);       /* t1=boxv * b' */
425   mmul(invbox,t1,t2);
426   mtmul(t2,invbox,M);
427
428   /* Determine the scaling matrix mu for the coordinates */
429   for(d=0;d<DIM;d++)
430     for(n=0;n<=d;n++)
431       t1[d][n] = box[d][n] + dt*boxv[d][n];
432   preserve_box_shape(ir,box_rel,t1);
433   /* t1 is the box at t+dt, determine mu as the relative change */
434   mmul_ur0(invbox,t1,mu);
435 }
436
437 void berendsen_pcoupl(FILE *fplog,gmx_large_int_t step, 
438                       t_inputrec *ir,real dt, tensor pres,matrix box,
439                       matrix mu)
440 {
441   int    d,n;
442   real   scalar_pressure, xy_pressure, p_corr_z;
443   char   *ptr,buf[STRLEN];
444
445   /*
446    *  Calculate the scaling matrix mu
447    */
448   scalar_pressure=0;
449   xy_pressure=0;
450   for(d=0; d<DIM; d++) {
451     scalar_pressure += pres[d][d]/DIM;
452     if (d != ZZ)
453       xy_pressure += pres[d][d]/(DIM-1);
454   }
455   /* Pressure is now in bar, everywhere. */
456 #define factor(d,m) (ir->compress[d][m]*dt/ir->tau_p)
457   
458   /* mu has been changed from pow(1+...,1/3) to 1+.../3, since this is
459    * necessary for triclinic scaling
460    */
461   clear_mat(mu);
462   switch (ir->epct) {
463   case epctISOTROPIC:
464     for(d=0; d<DIM; d++) 
465       {
466         mu[d][d] = 1.0 - factor(d,d)*(ir->ref_p[d][d] - scalar_pressure) /DIM;
467       }
468     break;
469   case epctSEMIISOTROPIC:
470     for(d=0; d<ZZ; d++)
471       mu[d][d] = 1.0 - factor(d,d)*(ir->ref_p[d][d]-xy_pressure)/DIM;
472     mu[ZZ][ZZ] = 
473       1.0 - factor(ZZ,ZZ)*(ir->ref_p[ZZ][ZZ] - pres[ZZ][ZZ])/DIM;
474     break;
475   case epctANISOTROPIC:
476     for(d=0; d<DIM; d++)
477       for(n=0; n<DIM; n++)
478         mu[d][n] = (d==n ? 1.0 : 0.0) 
479           -factor(d,n)*(ir->ref_p[d][n] - pres[d][n])/DIM;
480     break;
481   case epctSURFACETENSION:
482     /* ir->ref_p[0/1] is the reference surface-tension times *
483      * the number of surfaces                                */
484     if (ir->compress[ZZ][ZZ])
485       p_corr_z = dt/ir->tau_p*(ir->ref_p[ZZ][ZZ] - pres[ZZ][ZZ]);
486     else
487       /* when the compressibity is zero, set the pressure correction   *
488        * in the z-direction to zero to get the correct surface tension */
489       p_corr_z = 0;
490     mu[ZZ][ZZ] = 1.0 - ir->compress[ZZ][ZZ]*p_corr_z;
491     for(d=0; d<DIM-1; d++)
492       mu[d][d] = 1.0 + factor(d,d)*(ir->ref_p[d][d]/(mu[ZZ][ZZ]*box[ZZ][ZZ])
493                                     - (pres[ZZ][ZZ]+p_corr_z - xy_pressure))/(DIM-1);
494     break;
495   default:
496     gmx_fatal(FARGS,"Berendsen pressure coupling type %s not supported yet\n",
497                 EPCOUPLTYPETYPE(ir->epct));
498     break;
499   }
500   /* To fullfill the orientation restrictions on triclinic boxes
501    * we will set mu_yx, mu_zx and mu_zy to 0 and correct
502    * the other elements of mu to first order.
503    */
504   mu[YY][XX] += mu[XX][YY];
505   mu[ZZ][XX] += mu[XX][ZZ];
506   mu[ZZ][YY] += mu[YY][ZZ];
507   mu[XX][YY] = 0;
508   mu[XX][ZZ] = 0;
509   mu[YY][ZZ] = 0;
510
511   if (debug) {
512     pr_rvecs(debug,0,"PC: pres ",pres,3);
513     pr_rvecs(debug,0,"PC: mu   ",mu,3);
514   }
515   
516   if (mu[XX][XX]<0.99 || mu[XX][XX]>1.01 ||
517       mu[YY][YY]<0.99 || mu[YY][YY]>1.01 ||
518       mu[ZZ][ZZ]<0.99 || mu[ZZ][ZZ]>1.01) {
519     char buf2[22];
520     sprintf(buf,"\nStep %s  Warning: pressure scaling more than 1%%, "
521             "mu: %g %g %g\n",
522             gmx_step_str(step,buf2),mu[XX][XX],mu[YY][YY],mu[ZZ][ZZ]);
523     if (fplog)
524       fprintf(fplog,"%s",buf);
525     fprintf(stderr,"%s",buf);
526   }
527 }
528
529 void berendsen_pscale(t_inputrec *ir,matrix mu,
530                       matrix box,matrix box_rel,
531                       int start,int nr_atoms,
532                       rvec x[],unsigned short cFREEZE[],
533                       t_nrnb *nrnb)
534 {
535   ivec   *nFreeze=ir->opts.nFreeze;
536   int    n,d,g=0;
537       
538   /* Scale the positions */
539   for (n=start; n<start+nr_atoms; n++) {
540     if (cFREEZE)
541       g = cFREEZE[n];
542     
543     if (!nFreeze[g][XX])
544       x[n][XX] = mu[XX][XX]*x[n][XX]+mu[YY][XX]*x[n][YY]+mu[ZZ][XX]*x[n][ZZ];
545     if (!nFreeze[g][YY])
546       x[n][YY] = mu[YY][YY]*x[n][YY]+mu[ZZ][YY]*x[n][ZZ];
547     if (!nFreeze[g][ZZ])
548       x[n][ZZ] = mu[ZZ][ZZ]*x[n][ZZ];
549   }
550   /* compute final boxlengths */
551   for (d=0; d<DIM; d++) {
552     box[d][XX] = mu[XX][XX]*box[d][XX]+mu[YY][XX]*box[d][YY]+mu[ZZ][XX]*box[d][ZZ];
553     box[d][YY] = mu[YY][YY]*box[d][YY]+mu[ZZ][YY]*box[d][ZZ];
554     box[d][ZZ] = mu[ZZ][ZZ]*box[d][ZZ];
555   }      
556
557   preserve_box_shape(ir,box_rel,box);
558   
559   /* (un)shifting should NOT be done after this,
560    * since the box vectors might have changed
561    */
562   inc_nrnb(nrnb,eNR_PCOUPL,nr_atoms);
563 }
564
565 void berendsen_tcoupl(t_inputrec *ir,gmx_ekindata_t *ekind,real dt)
566 {
567     t_grpopts *opts;
568     int    i;
569     real   T,reft=0,lll;
570
571     opts = &ir->opts;
572
573     for(i=0; (i<opts->ngtc); i++)
574     {
575         if (ir->eI == eiVV)
576         {
577             T = ekind->tcstat[i].T;
578         }
579         else
580         {
581             T = ekind->tcstat[i].Th;
582         }
583
584         if ((opts->tau_t[i] > 0) && (T > 0.0)) {  
585             reft = max(0.0,opts->ref_t[i]);
586             lll  = sqrt(1.0 + (dt/opts->tau_t[i])*(reft/T-1.0));
587             ekind->tcstat[i].lambda = max(min(lll,1.25),0.8);
588         }
589         else {
590             ekind->tcstat[i].lambda = 1.0;
591         }
592
593         if (debug)
594         {
595             fprintf(debug,"TC: group %d: T: %g, Lambda: %g\n",
596                     i,T,ekind->tcstat[i].lambda);
597         }
598     }
599 }
600
601 static int poisson_variate(real lambda,gmx_rng_t rng) {
602
603     real L;
604     int k=0;
605     real p=1.0;
606
607     L = exp(-lambda);
608
609     do
610     {
611         k = k+1;
612         p *= gmx_rng_uniform_real(rng);
613     } while (p>L);
614
615     return k-1;
616 }
617
618 void andersen_tcoupl(t_inputrec *ir,t_mdatoms *md,t_state *state, gmx_rng_t rng, real rate, t_idef *idef, int nblocks, int *sblock,gmx_bool *randatom, int *randatom_list, gmx_bool *randomize, real *boltzfac)
619 {
620     t_grpopts *opts;
621     int    i,j,k,d,len,n,ngtc,gc=0;
622     int    nshake, nsettle, nrandom, nrand_group;
623     real   boltz,scal,reft,prand;
624     t_iatom *iatoms;
625
626     /* convenience variables */
627     opts = &ir->opts;
628     ngtc = opts->ngtc;
629
630     /* idef is only passed in if it's chance-per-particle andersen, so
631        it essentially serves as a boolean to determine which type of
632        andersen is being used */
633     if (idef) {
634
635         /* randomly atoms to randomize.  However, all constraint
636            groups have to have either all of the atoms or none of the
637            atoms randomize.
638
639            Algorithm:
640            1. Select whether or not to randomize each atom to get the correct probability.
641            2. Cycle through the constraint groups.
642               2a. for each constraint group, determine the fraction f of that constraint group that are
643                   chosen to be randomized.
644               2b. all atoms in the constraint group are randomized with probability f.
645         */
646
647         nrandom = 0;
648         if ((rate < 0.05) && (md->homenr > 50))
649         {
650             /* if the rate is relatively high, use a standard method, if low rate,
651              * use poisson */
652             /* poisson distributions approxmation, more efficient for
653              * low rates, fewer random numbers required */
654             nrandom = poisson_variate(md->homenr*rate,rng);  /* how many do we randomize? Use Poisson. */
655             /* now we know how many, choose them randomly. No worries about repeats, at this rate, it's negligible.
656                worst thing that happens, it lowers the true rate an negligible amount */
657             for (i=0;i<nrandom;i++)
658             {
659                 randatom[(int)(gmx_rng_uniform_real(rng)*md->homenr)] = TRUE;
660             }
661         }
662         else
663         {
664             for (i=0;i<md->homenr;i++)
665             {
666                 if (gmx_rng_uniform_real(rng)<rate)
667                 {
668                     randatom[i] = TRUE;
669                     nrandom++;
670                 }
671             }
672         }
673
674         /* instead of looping over the constraint groups, if we had a
675            list of which atoms were in which constraint groups, we
676            could then loop over only the groups that are randomized
677            now.  But that is not available now.  Create later after
678            determining whether there actually is any slowing. */
679
680         /* first, loop through the settles to make sure all groups either entirely randomized, or not randomized. */
681
682         nsettle  = idef->il[F_SETTLE].nr/2;
683         for (i=0;i<nsettle;i++)
684         {
685             iatoms = idef->il[F_SETTLE].iatoms;
686             nrand_group = 0;
687             for (k=0;k<3;k++)  /* settles are always 3 atoms, hardcoded */
688             {
689                 if (randatom[iatoms[2*i+1]+k])
690                 {
691                     nrand_group++;     /* count the number of atoms to be shaken in the settles group */
692                     randatom[iatoms[2*i+1]+k] = FALSE;
693                     nrandom--;
694                 }
695             }
696             if (nrand_group > 0)
697             {
698                 prand = (nrand_group)/3.0;  /* use this fraction to compute the probability the
699                                                whole group is randomized */
700                 if (gmx_rng_uniform_real(rng)<prand)
701                 {
702                     for (k=0;k<3;k++)
703                     {
704                         randatom[iatoms[2*i+1]+k] = TRUE;   /* mark them all to be randomized */
705                     }
706                     nrandom+=3;
707                 }
708             }
709         }
710
711         /* now loop through the shake groups */
712         nshake = nblocks;
713         for (i=0;i<nshake;i++)
714         {
715             iatoms = &(idef->il[F_CONSTR].iatoms[sblock[i]]);
716             len = sblock[i+1]-sblock[i];
717             nrand_group = 0;
718             for (k=0;k<len;k++)
719             {
720                 if (k%3 != 0)
721                 {  /* only 2/3 of the sblock items are atoms, the others are labels */
722                     if (randatom[iatoms[k]])
723                     {
724                         nrand_group++;
725                         randatom[iatoms[k]] = FALSE;  /* need to mark it false here in case the atom is in more than
726                                                          one group in the shake block */
727                         nrandom--;
728                     }
729                 }
730             }
731             if (nrand_group > 0)
732             {
733                 prand = (nrand_group)/(1.0*(2*len/3));
734                 if (gmx_rng_uniform_real(rng)<prand)
735                 {
736                     for (k=0;k<len;k++)
737                     {
738                         if (k%3 != 0)
739                         {  /* only 2/3 of the sblock items are atoms, the others are labels */
740                             randatom[iatoms[k]] = TRUE; /* randomize all of them */
741                             nrandom++;
742                         }
743                     }
744                 }
745             }
746         }
747         if (nrandom > 0)
748         {
749             n = 0;
750             for (i=0;i<md->homenr;i++)  /* now loop over the list of atoms */
751             {
752                 if (randatom[i])
753                 {
754                     randatom_list[n] = i;
755                     n++;
756                 }
757             }
758             nrandom = n;  /* there are some values of nrandom for which
759                              this algorithm won't work; for example all
760                              water molecules and nrandom =/= 3.  Better to
761                              recount and use this number (which we
762                              calculate anyway: it will not affect
763                              the average number of atoms accepted.
764                           */
765         }
766     }
767     else
768     {
769         /* if it's andersen-massive, then randomize all the atoms */
770         nrandom = md->homenr;
771         for (i=0;i<nrandom;i++)
772         {
773             randatom_list[i] = i;
774         }
775     }
776
777     /* randomize the velocities of the selected particles */
778
779     for (i=0;i<nrandom;i++)  /* now loop over the list of atoms */
780     {
781         n = randatom_list[i];
782         if (md->cTC)
783         {
784             gc   = md->cTC[n];  /* assign the atom to a temperature group if there are more than one */
785         }
786         if (randomize[gc])
787         {
788             scal = sqrt(boltzfac[gc]*md->invmass[n]);
789             for (d=0;d<DIM;d++)
790             {
791                 state->v[n][d] = scal*gmx_rng_gaussian_table(rng);
792             }
793         }
794         randatom[n] = FALSE; /* unmark this atom for randomization */
795     }
796 }
797
798
799 void nosehoover_tcoupl(t_grpopts *opts,gmx_ekindata_t *ekind,real dt,
800                        double xi[],double vxi[], t_extmass *MassQ)
801 {
802     int   i;
803     real  reft,oldvxi;
804     
805     /* note that this routine does not include Nose-hoover chains yet. Should be easy to add. */
806     
807     for(i=0; (i<opts->ngtc); i++)
808     {
809         reft = max(0.0,opts->ref_t[i]);
810         oldvxi = vxi[i];
811         vxi[i]  += dt*MassQ->Qinv[i]*(ekind->tcstat[i].Th - reft);
812         xi[i] += dt*(oldvxi + vxi[i])*0.5;
813     }
814 }
815
816 t_state *init_bufstate(const t_state *template_state)
817 {
818     t_state *state;
819     int nc = template_state->nhchainlength;
820     snew(state,1);
821     snew(state->nosehoover_xi,nc*template_state->ngtc);
822     snew(state->nosehoover_vxi,nc*template_state->ngtc);
823     snew(state->therm_integral,template_state->ngtc);
824     snew(state->nhpres_xi,nc*template_state->nnhpres);
825     snew(state->nhpres_vxi,nc*template_state->nnhpres);
826
827     return state;
828 }  
829
830 void destroy_bufstate(t_state *state) 
831 {
832     sfree(state->x);
833     sfree(state->v);
834     sfree(state->nosehoover_xi);
835     sfree(state->nosehoover_vxi);
836     sfree(state->therm_integral);
837     sfree(state->nhpres_xi);
838     sfree(state->nhpres_vxi);
839     sfree(state);
840 }  
841
842 void trotter_update(t_inputrec *ir,gmx_large_int_t step, gmx_ekindata_t *ekind, 
843                     gmx_enerdata_t *enerd, t_state *state, 
844                     tensor vir, t_mdatoms *md, 
845                     t_extmass *MassQ, int **trotter_seqlist, int trotter_seqno) 
846 {
847     
848     int n,i,j,d,ntgrp,ngtc,gc=0;
849     t_grp_tcstat *tcstat;
850     t_grpopts *opts;
851     gmx_large_int_t step_eff;
852     real ecorr,pcorr,dvdlcorr;
853     real bmass,qmass,reft,kT,dt,nd;
854     tensor dumpres,dumvir;
855     double *scalefac,dtc;
856     int *trotter_seq;
857     rvec sumv={0,0,0},consk;
858     gmx_bool bCouple;
859
860     if (trotter_seqno <= ettTSEQ2)
861     {
862         step_eff = step-1;  /* the velocity verlet calls are actually out of order -- the first half step
863                                is actually the last half step from the previous step.  Thus the first half step
864                                actually corresponds to the n-1 step*/
865                                
866     } else {
867         step_eff = step;
868     }
869
870     bCouple = (ir->nsttcouple == 1 ||
871                do_per_step(step_eff+ir->nsttcouple,ir->nsttcouple));
872
873     trotter_seq = trotter_seqlist[trotter_seqno];
874
875     if ((trotter_seq[0] == etrtSKIPALL) || (!bCouple))
876     {
877         return;
878     }
879     dtc = ir->nsttcouple*ir->delta_t;  /* This is OK for NPT, because nsttcouple == nstpcouple is enforcesd */
880     opts = &(ir->opts); /* just for ease of referencing */
881     ngtc = opts->ngtc;
882     assert(ngtc>0);
883     snew(scalefac,opts->ngtc);
884     for (i=0;i<ngtc;i++) 
885     {
886         scalefac[i] = 1;
887     }
888     /* execute the series of trotter updates specified in the trotterpart array */
889     
890     for (i=0;i<NTROTTERPARTS;i++){
891         /* allow for doubled intgrators by doubling dt instead of making 2 calls */
892         if ((trotter_seq[i] == etrtBAROV2) || (trotter_seq[i] == etrtBARONHC2) || (trotter_seq[i] == etrtNHC2))
893         {
894             dt = 2 * dtc;
895         }
896         else 
897         {
898             dt = dtc;
899         }
900
901         switch (trotter_seq[i])
902         {
903         case etrtBAROV:
904         case etrtBAROV2:
905             boxv_trotter(ir,&(state->veta),dt,state->box,ekind,vir,
906                          enerd->term[F_PDISPCORR],MassQ);
907             break;
908         case etrtBARONHC:
909         case etrtBARONHC2:
910             NHC_trotter(opts,state->nnhpres,ekind,dt,state->nhpres_xi,
911                         state->nhpres_vxi,NULL,&(state->veta),MassQ,FALSE);      
912             break;
913         case etrtNHC:
914         case etrtNHC2:
915             NHC_trotter(opts,opts->ngtc,ekind,dt,state->nosehoover_xi,
916                         state->nosehoover_vxi,scalefac,NULL,MassQ,(ir->eI==eiVV));
917             /* need to rescale the kinetic energies and velocities here.  Could 
918                scale the velocities later, but we need them scaled in order to 
919                produce the correct outputs, so we'll scale them here. */
920             
921             for (i=0; i<ngtc;i++) 
922             {
923                 tcstat = &ekind->tcstat[i];
924                 tcstat->vscale_nhc = scalefac[i]; 
925                 tcstat->ekinscaleh_nhc *= (scalefac[i]*scalefac[i]); 
926                 tcstat->ekinscalef_nhc *= (scalefac[i]*scalefac[i]); 
927             }
928             /* now that we've scaled the groupwise velocities, we can add them up to get the total */
929             /* but do we actually need the total? */
930             
931             /* modify the velocities as well */
932             for (n=md->start;n<md->start+md->homenr;n++) 
933             {
934                 if (md->cTC)   /* does this conditional need to be here? is this always true?*/
935                 { 
936                     gc = md->cTC[n];
937                 }
938                 for (d=0;d<DIM;d++) 
939                 {
940                     state->v[n][d] *= scalefac[gc];
941                 }
942                 
943                 if (debug) 
944                 {
945                     for (d=0;d<DIM;d++) 
946                     {
947                         sumv[d] += (state->v[n][d])/md->invmass[n];
948                     }
949                 }
950             }          
951             break;
952         default:
953             break;
954         }
955     }
956     /* check for conserved momentum -- worth looking at this again eventually, but not working right now.*/  
957 #if 0
958     if (debug) 
959     {
960         if (bFirstHalf) 
961         {
962             for (d=0;d<DIM;d++) 
963             {
964                 consk[d] = sumv[d]*exp((1 + 1.0/opts->nrdf[0])*((1.0/DIM)*log(det(state->box)/state->vol0)) + state->nosehoover_xi[0]); 
965             }
966             fprintf(debug,"Conserved kappa: %15.8f %15.8f %15.8f\n",consk[0],consk[1],consk[2]);    
967         }
968     }
969 #endif
970     sfree(scalefac);
971 }
972
973
974 extern void init_npt_masses(t_inputrec *ir, t_state *state, t_extmass *MassQ, gmx_bool bInit)
975 {
976     int n,i,j,d,ntgrp,ngtc,nnhpres,nh,gc=0;
977     t_grp_tcstat *tcstat;
978     t_grpopts *opts;
979     real ecorr,pcorr,dvdlcorr;
980     real bmass,qmass,reft,kT,dt,ndj,nd;
981     tensor dumpres,dumvir;
982
983     opts = &(ir->opts); /* just for ease of referencing */
984     ngtc = ir->opts.ngtc;
985     nnhpres = state->nnhpres;
986     nh = state->nhchainlength; 
987
988     if (ir->eI == eiMD) {
989         if (bInit)
990         {
991             snew(MassQ->Qinv,ngtc);
992         }
993         for(i=0; (i<ngtc); i++) 
994         { 
995             if ((opts->tau_t[i] > 0) && (opts->ref_t[i] > 0)) 
996             {
997                 MassQ->Qinv[i]=1.0/(sqr(opts->tau_t[i]/M_2PI)*opts->ref_t[i]);
998             } 
999             else 
1000             {
1001                 MassQ->Qinv[i]=0.0;     
1002             }
1003         }
1004     }
1005     else if (EI_VV(ir->eI))
1006     {
1007     /* Set pressure variables */
1008
1009         if (bInit)
1010         {
1011             if (state->vol0 == 0)
1012             {
1013                 state->vol0 = det(state->box); 
1014                 /* because we start by defining a fixed
1015                    compressibility, we need the volume at this
1016                    compressibility to solve the problem. */
1017             }
1018         }
1019
1020         /* units are nm^3 * ns^2 / (nm^3 * bar / kJ/mol) = kJ/mol  */
1021         /* Consider evaluating eventually if this the right mass to use.  All are correct, some might be more stable  */
1022         MassQ->Winv = (PRESFAC*trace(ir->compress)*BOLTZ*opts->ref_t[0])/(DIM*state->vol0*sqr(ir->tau_p/M_2PI));
1023         /* An alternate mass definition, from Tuckerman et al. */ 
1024         /* MassQ->Winv = 1.0/(sqr(ir->tau_p/M_2PI)*(opts->nrdf[0]+DIM)*BOLTZ*opts->ref_t[0]); */
1025         for (d=0;d<DIM;d++) 
1026         {
1027             for (n=0;n<DIM;n++) 
1028             {
1029                 MassQ->Winvm[d][n]= PRESFAC*ir->compress[d][n]/(state->vol0*sqr(ir->tau_p/M_2PI)); 
1030                 /* not clear this is correct yet for the anisotropic case. Will need to reevaluate
1031                  before using MTTK for anisotropic states.*/
1032             }
1033         }
1034         /* Allocate space for thermostat variables */
1035         if (bInit)
1036         {
1037             snew(MassQ->Qinv,ngtc*nh);
1038         }
1039
1040         /* now, set temperature variables */
1041         for (i=0; i<ngtc; i++)
1042         {
1043             if ((opts->tau_t[i] > 0) && (opts->ref_t[i] > 0))
1044             {
1045                 reft = max(0.0,opts->ref_t[i]);
1046                 nd = opts->nrdf[i];
1047                 kT = BOLTZ*reft;
1048                 for (j=0;j<nh;j++)
1049                 {
1050                     if (j==0)
1051                     {
1052                         ndj = nd;
1053                     }
1054                     else
1055                     {
1056                         ndj = 1;
1057                     }
1058                     MassQ->Qinv[i*nh+j]   = 1.0/(sqr(opts->tau_t[i]/M_2PI)*ndj*kT);
1059                 }
1060             }
1061             else
1062             {
1063                 reft=0.0;
1064                 for (j=0;j<nh;j++)
1065                 {
1066                     MassQ->Qinv[i*nh+j] = 0.0;
1067                 }
1068             }
1069         }
1070     }
1071 }
1072
1073 int **init_npt_vars(t_inputrec *ir, t_state *state, t_extmass *MassQ, gmx_bool bTrotter)
1074 {
1075     int n,i,j,d,ntgrp,ngtc,nnhpres,nh,gc=0;
1076     t_grp_tcstat *tcstat;
1077     t_grpopts *opts;
1078     real ecorr,pcorr,dvdlcorr;
1079     real bmass,qmass,reft,kT,dt,ndj,nd;
1080     tensor dumpres,dumvir;
1081     int **trotter_seq;
1082
1083     opts = &(ir->opts); /* just for ease of referencing */
1084     ngtc = state->ngtc;
1085     nnhpres = state->nnhpres;
1086     nh = state->nhchainlength;
1087
1088     init_npt_masses(ir, state, MassQ, TRUE);
1089     
1090     /* first, initialize clear all the trotter calls */
1091     snew(trotter_seq,ettTSEQMAX);
1092     for (i=0;i<ettTSEQMAX;i++) 
1093     {
1094         snew(trotter_seq[i],NTROTTERPARTS);
1095         for (j=0;j<NTROTTERPARTS;j++) {
1096             trotter_seq[i][j] = etrtNONE;
1097         }
1098         trotter_seq[i][0] = etrtSKIPALL;
1099     }
1100     
1101     if (!bTrotter) 
1102     {
1103         /* no trotter calls, so we never use the values in the array.
1104          * We access them (so we need to define them, but ignore
1105          * then.*/
1106
1107         return trotter_seq;
1108     }
1109
1110     /* compute the kinetic energy by using the half step velocities or
1111      * the kinetic energies, depending on the order of the trotter calls */
1112
1113     if (ir->eI==eiVV)
1114     {
1115         if (IR_NPT_TROTTER(ir)) 
1116         {
1117             /* This is the complicated version - there are 4 possible calls, depending on ordering.
1118                We start with the initial one. */
1119             /* first, a round that estimates veta. */
1120             trotter_seq[0][0] = etrtBAROV;
1121
1122             /* trotter_seq[1] is etrtNHC for 1/2 step velocities - leave zero */
1123
1124             /* The first half trotter update */
1125             trotter_seq[2][0] = etrtBAROV;
1126             trotter_seq[2][1] = etrtNHC;
1127             trotter_seq[2][2] = etrtBARONHC;
1128
1129             /* The second half trotter update */
1130             trotter_seq[3][0] = etrtBARONHC;
1131             trotter_seq[3][1] = etrtNHC;
1132             trotter_seq[3][2] = etrtBAROV;
1133
1134             /* trotter_seq[4] is etrtNHC for second 1/2 step velocities - leave zero */
1135             
1136         } 
1137         else if (IR_NVT_TROTTER(ir))
1138         {
1139             /* This is the easy version - there are only two calls, both the same.
1140                Otherwise, even easier -- no calls  */
1141             trotter_seq[2][0] = etrtNHC;
1142             trotter_seq[3][0] = etrtNHC;
1143         }
1144         else if (IR_NPH_TROTTER(ir))
1145         {
1146             /* This is the complicated version - there are 4 possible calls, depending on ordering.
1147                We start with the initial one. */
1148             /* first, a round that estimates veta. */
1149             trotter_seq[0][0] = etrtBAROV;
1150
1151             /* trotter_seq[1] is etrtNHC for 1/2 step velocities - leave zero */
1152
1153             /* The first half trotter update */
1154             trotter_seq[2][0] = etrtBAROV;
1155             trotter_seq[2][1] = etrtBARONHC;
1156
1157             /* The second half trotter update */
1158             trotter_seq[3][0] = etrtBARONHC;
1159             trotter_seq[3][1] = etrtBAROV;
1160
1161             /* trotter_seq[4] is etrtNHC for second 1/2 step velocities - leave zero */
1162         }
1163     }
1164     else if (ir->eI==eiVVAK)
1165     {
1166         if (IR_NPT_TROTTER(ir))
1167         {
1168             /* This is the complicated version - there are 4 possible calls, depending on ordering.
1169                We start with the initial one. */
1170             /* first, a round that estimates veta. */
1171             trotter_seq[0][0] = etrtBAROV;
1172
1173             /* The first half trotter update, part 1 -- double update, because it commutes */
1174             trotter_seq[1][0] = etrtNHC;
1175
1176             /* The first half trotter update, part 2 */
1177             trotter_seq[2][0] = etrtBAROV;
1178             trotter_seq[2][1] = etrtBARONHC;
1179             
1180             /* The second half trotter update, part 1 */
1181             trotter_seq[3][0] = etrtBARONHC;
1182             trotter_seq[3][1] = etrtBAROV;
1183
1184             /* The second half trotter update */
1185             trotter_seq[4][0] = etrtNHC;
1186         } 
1187         else if (IR_NVT_TROTTER(ir))
1188         {
1189             /* This is the easy version - there is only one call, both the same.
1190                Otherwise, even easier -- no calls  */
1191             trotter_seq[1][0] = etrtNHC;
1192             trotter_seq[4][0] = etrtNHC;
1193         }
1194         else if (IR_NPH_TROTTER(ir))
1195         {
1196             /* This is the complicated version - there are 4 possible calls, depending on ordering.
1197                We start with the initial one. */
1198             /* first, a round that estimates veta. */
1199             trotter_seq[0][0] = etrtBAROV; 
1200
1201             /* The first half trotter update, part 1 -- leave zero */
1202             trotter_seq[1][0] = etrtNHC;
1203
1204             /* The first half trotter update, part 2 */
1205             trotter_seq[2][0] = etrtBAROV;
1206             trotter_seq[2][1] = etrtBARONHC;
1207
1208             /* The second half trotter update, part 1 */
1209             trotter_seq[3][0] = etrtBARONHC;
1210             trotter_seq[3][1] = etrtBAROV;
1211
1212             /* The second half trotter update -- blank for now */
1213         }
1214     }
1215
1216     switch (ir->epct)
1217     {
1218     case epctISOTROPIC:  
1219     default:
1220         bmass = DIM*DIM;  /* recommended mass parameters for isotropic barostat */
1221     }    
1222
1223     snew(MassQ->QPinv,nnhpres*opts->nhchainlength);
1224
1225     /* barostat temperature */
1226     if ((ir->tau_p > 0) && (opts->ref_t[0] > 0))
1227     {
1228         reft = max(0.0,opts->ref_t[0]);
1229         kT = BOLTZ*reft;
1230         for (i=0;i<nnhpres;i++) {
1231             for (j=0;j<nh;j++) 
1232             {
1233                 if (j==0) {
1234                     qmass = bmass;
1235                 } 
1236                 else 
1237                 {
1238                     qmass = 1;
1239                 }
1240                 MassQ->QPinv[i*opts->nhchainlength+j]   = 1.0/(sqr(opts->tau_t[0]/M_2PI)*qmass*kT);
1241             }
1242         }
1243     }
1244     else 
1245     {
1246         for (i=0;i<nnhpres;i++) {
1247             for (j=0;j<nh;j++) 
1248             {
1249                 MassQ->QPinv[i*nh+j]=0.0;
1250             }
1251         }
1252     }    
1253     return trotter_seq;
1254 }
1255
1256 real NPT_energy(t_inputrec *ir, t_state *state, t_extmass *MassQ)
1257 {
1258     int  i,j,nd,ndj,bmass,qmass,ngtcall;
1259     real ener_npt,reft,eta,kT,tau;
1260     double *ivxi, *ixi;
1261     double *iQinv;
1262     real vol,dbaro,W,Q;
1263     int nh = state->nhchainlength;
1264
1265     ener_npt = 0;
1266     
1267     /* now we compute the contribution of the pressure to the conserved quantity*/
1268     
1269     if (ir->epc==epcMTTK) 
1270     {
1271         /* find the volume, and the kinetic energy of the volume */
1272         
1273         switch (ir->epct) {
1274             
1275         case epctISOTROPIC:
1276             /* contribution from the pressure momenenta */
1277             ener_npt += 0.5*sqr(state->veta)/MassQ->Winv;
1278             
1279             /* contribution from the PV term */
1280             vol = det(state->box);
1281             ener_npt += vol*trace(ir->ref_p)/(DIM*PRESFAC);
1282
1283             break;
1284         case epctANISOTROPIC:
1285             
1286             break;
1287             
1288         case epctSURFACETENSION:
1289             
1290             break;
1291         case epctSEMIISOTROPIC:
1292             
1293             break;
1294         default:
1295             break;
1296         }
1297     }
1298     
1299     if (IR_NPT_TROTTER(ir) || IR_NPH_TROTTER(ir))
1300     {
1301         /* add the energy from the barostat thermostat chain */
1302         for (i=0;i<state->nnhpres;i++) {
1303
1304             /* note -- assumes only one degree of freedom that is thermostatted in barostat */
1305             ivxi = &state->nhpres_vxi[i*nh];
1306             ixi = &state->nhpres_xi[i*nh];
1307             iQinv = &(MassQ->QPinv[i*nh]);
1308             reft = max(ir->opts.ref_t[0],0); /* using 'System' temperature */
1309             kT = BOLTZ * reft;
1310         
1311             for (j=0;j<nh;j++) 
1312             {
1313                 if (iQinv[j] > 0)
1314                 {
1315                     ener_npt += 0.5*sqr(ivxi[j])/iQinv[j];
1316                     /* contribution from the thermal variable of the NH chain */
1317                     ener_npt += ixi[j]*kT;
1318                 }
1319                 if (debug) 
1320                 {
1321                     fprintf(debug,"P-T-group: %10d Chain %4d ThermV: %15.8f ThermX: %15.8f",i,j,ivxi[j],ixi[j]);
1322                 }
1323             }
1324         }
1325     }
1326         
1327     if (ir->etc) 
1328     {
1329         for(i=0; i<ir->opts.ngtc; i++) 
1330         {
1331             ixi = &state->nosehoover_xi[i*nh];
1332             ivxi = &state->nosehoover_vxi[i*nh];
1333             iQinv = &(MassQ->Qinv[i*nh]);
1334             
1335             nd = ir->opts.nrdf[i];
1336             reft = max(ir->opts.ref_t[i],0);
1337             kT = BOLTZ * reft;
1338             
1339             if (nd > 0) 
1340             {
1341                 if (IR_NVT_TROTTER(ir))
1342                 {
1343                     /* contribution from the thermal momenta of the NH chain */
1344                     for (j=0;j<nh;j++) 
1345                     {
1346                         if (iQinv[j] > 0) {
1347                             ener_npt += 0.5*sqr(ivxi[j])/iQinv[j];
1348                             /* contribution from the thermal variable of the NH chain */
1349                             if (j==0) {
1350                                 ndj = nd;
1351                             } 
1352                             else 
1353                             {
1354                                 ndj = 1;
1355                             } 
1356                             ener_npt += ndj*ixi[j]*kT;
1357                         }
1358                     }
1359                 }
1360                 else  /* Other non Trotter temperature NH control  -- no chains yet. */
1361                 { 
1362                     ener_npt += 0.5*BOLTZ*nd*sqr(ivxi[0])/iQinv[0];
1363                     ener_npt += nd*ixi[0]*kT;
1364                 }
1365             }
1366         }
1367     }
1368     return ener_npt;
1369 }
1370
1371 static real vrescale_gamdev(int ia, gmx_rng_t rng)
1372 /* Gamma distribution, adapted from numerical recipes */
1373 {
1374     int j;
1375     real am,e,s,v1,v2,x,y;
1376
1377     if (ia < 6)
1378     {
1379         do
1380         {
1381             x = 1.0;
1382             for(j=1; j<=ia; j++)
1383             {
1384                 x *= gmx_rng_uniform_real(rng);
1385             }
1386         }
1387         while (x == 0);
1388         x = -log(x);
1389     }
1390     else
1391     {
1392         do
1393         {
1394             do
1395             {
1396                 do
1397                 {
1398                     v1 = gmx_rng_uniform_real(rng);
1399                     v2 = 2.0*gmx_rng_uniform_real(rng)-1.0;
1400                 }
1401                 while (v1*v1 + v2*v2 > 1.0 ||
1402                        v1*v1*GMX_REAL_MAX < 3.0*ia);
1403                 /* The last check above ensures that both x (3.0 > 2.0 in s)
1404                  * and the pre-factor for e do not go out of range.
1405                  */
1406                 y = v2/v1;
1407                 am = ia - 1;
1408                 s = sqrt(2.0*am + 1.0);
1409                 x = s*y + am;
1410             }
1411             while (x <= 0.0);
1412             e = (1.0 + y*y)*exp(am*log(x/am) - s*y);
1413         }
1414         while (gmx_rng_uniform_real(rng) > e);
1415     }
1416
1417     return x;
1418 }
1419
1420 static real vrescale_sumnoises(int nn,gmx_rng_t rng)
1421 {
1422 /*
1423  * Returns the sum of n independent gaussian noises squared
1424  * (i.e. equivalent to summing the square of the return values
1425  * of nn calls to gmx_rng_gaussian_real).xs
1426  */
1427   real rr;
1428
1429   if (nn == 0) {
1430     return 0.0;
1431   } else if (nn == 1) {
1432     rr = gmx_rng_gaussian_real(rng);
1433     return rr*rr;
1434   } else if (nn % 2 == 0) {
1435     return 2.0*vrescale_gamdev(nn/2,rng);
1436   } else {
1437     rr = gmx_rng_gaussian_real(rng);
1438     return 2.0*vrescale_gamdev((nn-1)/2,rng) + rr*rr;
1439   }
1440 }
1441
1442 static real vrescale_resamplekin(real kk,real sigma, int ndeg, real taut,
1443                                  gmx_rng_t rng)
1444 {
1445 /*
1446  * Generates a new value for the kinetic energy,
1447  * according to Bussi et al JCP (2007), Eq. (A7)
1448  * kk:    present value of the kinetic energy of the atoms to be thermalized (in arbitrary units)
1449  * sigma: target average value of the kinetic energy (ndeg k_b T/2)  (in the same units as kk)
1450  * ndeg:  number of degrees of freedom of the atoms to be thermalized
1451  * taut:  relaxation time of the thermostat, in units of 'how often this routine is called'
1452  */
1453   real factor,rr;
1454
1455   if (taut > 0.1) {
1456     factor = exp(-1.0/taut);
1457   } else {
1458     factor = 0.0;
1459   }
1460   rr = gmx_rng_gaussian_real(rng);
1461   return
1462     kk +
1463     (1.0 - factor)*(sigma*(vrescale_sumnoises(ndeg-1,rng) + rr*rr)/ndeg - kk) +
1464     2.0*rr*sqrt(kk*sigma/ndeg*(1.0 - factor)*factor);
1465 }
1466
1467 void vrescale_tcoupl(t_inputrec *ir,gmx_ekindata_t *ekind,real dt,
1468                      double therm_integral[],gmx_rng_t rng)
1469 {
1470     t_grpopts *opts;
1471     int    i;
1472     real   Ek,Ek_ref1,Ek_ref,Ek_new; 
1473     
1474     opts = &ir->opts;
1475
1476     for(i=0; (i<opts->ngtc); i++)
1477     {
1478         if (ir->eI == eiVV)
1479         {
1480             Ek = trace(ekind->tcstat[i].ekinf);
1481         }
1482         else
1483         {
1484             Ek = trace(ekind->tcstat[i].ekinh);
1485         }
1486         
1487         if (opts->tau_t[i] > 0 && opts->nrdf[i] > 0 && Ek > 0)
1488         {
1489             Ek_ref1 = 0.5*opts->ref_t[i]*BOLTZ;
1490             Ek_ref  = Ek_ref1*opts->nrdf[i];
1491
1492             Ek_new  = vrescale_resamplekin(Ek,Ek_ref,opts->nrdf[i],
1493                                            opts->tau_t[i]/dt,rng);
1494
1495             /* Analytically Ek_new>=0, but we check for rounding errors */
1496             if (Ek_new <= 0)
1497             {
1498                 ekind->tcstat[i].lambda = 0.0;
1499             }
1500             else
1501             {
1502                 ekind->tcstat[i].lambda = sqrt(Ek_new/Ek);
1503             }
1504
1505             therm_integral[i] -= Ek_new - Ek;
1506
1507             if (debug)
1508             {
1509                 fprintf(debug,"TC: group %d: Ekr %g, Ek %g, Ek_new %g, Lambda: %g\n",
1510                         i,Ek_ref,Ek,Ek_new,ekind->tcstat[i].lambda);
1511             }
1512         }
1513         else
1514         {
1515             ekind->tcstat[i].lambda = 1.0;
1516         }
1517     }
1518 }
1519
1520 real vrescale_energy(t_grpopts *opts,double therm_integral[])
1521 {
1522   int i;
1523   real ener;
1524
1525   ener = 0;
1526   for(i=0; i<opts->ngtc; i++) {
1527     ener += therm_integral[i];
1528   }
1529   
1530   return ener;
1531 }
1532
1533 void rescale_velocities(gmx_ekindata_t *ekind,t_mdatoms *mdatoms,
1534                         int start,int end,rvec v[])
1535 {
1536     t_grp_acc      *gstat;
1537     t_grp_tcstat   *tcstat;
1538     unsigned short *cACC,*cTC;
1539     int  ga,gt,n,d;
1540     real lg;
1541     rvec vrel;
1542
1543     tcstat = ekind->tcstat;
1544     cTC    = mdatoms->cTC;
1545
1546     if (ekind->bNEMD)
1547     {
1548         gstat  = ekind->grpstat;
1549         cACC   = mdatoms->cACC;
1550
1551         ga = 0;
1552         gt = 0;
1553         for(n=start; n<end; n++) 
1554         {
1555             if (cACC) 
1556             {
1557                 ga   = cACC[n];
1558             }
1559             if (cTC)
1560             {
1561                 gt   = cTC[n];
1562             }
1563             /* Only scale the velocity component relative to the COM velocity */
1564             rvec_sub(v[n],gstat[ga].u,vrel);
1565             lg = tcstat[gt].lambda;
1566             for(d=0; d<DIM; d++)
1567             {
1568                 v[n][d] = gstat[ga].u[d] + lg*vrel[d];
1569             }
1570         }
1571     }
1572     else
1573     {
1574         gt = 0;
1575         for(n=start; n<end; n++) 
1576         {
1577             if (cTC)
1578             {
1579                 gt   = cTC[n];
1580             }
1581             lg = tcstat[gt].lambda;
1582             for(d=0; d<DIM; d++)
1583             {
1584                 v[n][d] *= lg;
1585             }
1586         }
1587     }
1588 }
1589
1590
1591 /* set target temperatures if we are annealing */
1592 void update_annealing_target_temp(t_grpopts *opts,real t)
1593 {
1594   int i,j,n,npoints;
1595   real pert,thist=0,x;
1596
1597   for(i=0;i<opts->ngtc;i++) {
1598     npoints = opts->anneal_npoints[i];
1599     switch (opts->annealing[i]) {
1600     case eannNO:
1601       continue;
1602     case  eannPERIODIC:
1603       /* calculate time modulo the period */
1604       pert  = opts->anneal_time[i][npoints-1];
1605       n     = t / pert;
1606       thist = t - n*pert; /* modulo time */
1607       /* Make sure rounding didn't get us outside the interval */
1608       if (fabs(thist-pert) < GMX_REAL_EPS*100)
1609         thist=0;
1610       break;
1611     case eannSINGLE:
1612       thist = t;
1613       break;
1614     default:
1615       gmx_fatal(FARGS,"Death horror in update_annealing_target_temp (i=%d/%d npoints=%d)",i,opts->ngtc,npoints);
1616     }
1617     /* We are doing annealing for this group if we got here, 
1618      * and we have the (relative) time as thist.
1619      * calculate target temp */
1620     j=0;
1621     while ((j < npoints-1) && (thist>(opts->anneal_time[i][j+1])))
1622       j++;
1623     if (j < npoints-1) {
1624       /* Found our position between points j and j+1. 
1625        * Interpolate: x is the amount from j+1, (1-x) from point j 
1626        * First treat possible jumps in temperature as a special case.
1627        */
1628       if ((opts->anneal_time[i][j+1]-opts->anneal_time[i][j]) < GMX_REAL_EPS*100)
1629         opts->ref_t[i]=opts->anneal_temp[i][j+1];
1630       else {
1631         x = ((thist-opts->anneal_time[i][j])/
1632              (opts->anneal_time[i][j+1]-opts->anneal_time[i][j]));
1633         opts->ref_t[i] = x*opts->anneal_temp[i][j+1]+(1-x)*opts->anneal_temp[i][j];
1634       }
1635     }
1636     else {
1637       opts->ref_t[i] = opts->anneal_temp[i][npoints-1];
1638     }
1639   }
1640 }