Replace all mdrun rngs with cycle based rng
[alexxy/gromacs.git] / src / gromacs / mdlib / update.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  * 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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41
42 #include <stdio.h>
43 #include <math.h>
44
45 #include "types/commrec.h"
46 #include "sysstuff.h"
47 #include "smalloc.h"
48 #include "typedefs.h"
49 #include "nrnb.h"
50 #include "physics.h"
51 #include "macros.h"
52 #include "vec.h"
53 #include "main.h"
54 #include "update.h"
55 #include "gromacs/random/random.h"
56 #include "mshift.h"
57 #include "tgroup.h"
58 #include "force.h"
59 #include "names.h"
60 #include "txtdump.h"
61 #include "mdrun.h"
62 #include "constr.h"
63 #include "disre.h"
64 #include "orires.h"
65 #include "gmx_omp_nthreads.h"
66
67 #include "gromacs/fileio/confio.h"
68 #include "gromacs/fileio/futil.h"
69 #include "gromacs/timing/wallcycle.h"
70 #include "gromacs/utility/gmxomp.h"
71 #include "gromacs/pulling/pull.h"
72
73 /*For debugging, start at v(-dt/2) for velolcity verlet -- uncomment next line */
74 /*#define STARTFROMDT2*/
75
76 typedef struct {
77     double gdt;
78     double eph;
79     double emh;
80     double em;
81     double b;
82     double c;
83     double d;
84 } gmx_sd_const_t;
85
86 typedef struct {
87     real V;
88     real X;
89     real Yv;
90     real Yx;
91 } gmx_sd_sigma_t;
92
93 typedef struct {
94     /* BD stuff */
95     real           *bd_rf;
96     /* SD stuff */
97     gmx_sd_const_t *sdc;
98     gmx_sd_sigma_t *sdsig;
99     rvec           *sd_V;
100     int             sd_V_nalloc;
101     /* andersen temperature control stuff */
102     gmx_bool       *randomize_group;
103     real           *boltzfac;
104 } gmx_stochd_t;
105
106 typedef struct gmx_update
107 {
108     gmx_stochd_t *sd;
109     /* xprime for constraint algorithms */
110     rvec         *xp;
111     int           xp_nalloc;
112
113     /* Variables for the deform algorithm */
114     gmx_int64_t     deformref_step;
115     matrix          deformref_box;
116 } t_gmx_update;
117
118
119 static void do_update_md(int start, int nrend, double dt,
120                          t_grp_tcstat *tcstat,
121                          double nh_vxi[],
122                          gmx_bool bNEMD, t_grp_acc *gstat, rvec accel[],
123                          ivec nFreeze[],
124                          real invmass[],
125                          unsigned short ptype[], unsigned short cFREEZE[],
126                          unsigned short cACC[], unsigned short cTC[],
127                          rvec x[], rvec xprime[], rvec v[],
128                          rvec f[], matrix M,
129                          gmx_bool bNH, gmx_bool bPR)
130 {
131     double imass, w_dt;
132     int    gf = 0, ga = 0, gt = 0;
133     rvec   vrel;
134     real   vn, vv, va, vb, vnrel;
135     real   lg, vxi = 0, u;
136     int    n, d;
137
138     if (bNH || bPR)
139     {
140         /* Update with coupling to extended ensembles, used for
141          * Nose-Hoover and Parrinello-Rahman coupling
142          * Nose-Hoover uses the reversible leap-frog integrator from
143          * Holian et al. Phys Rev E 52(3) : 2338, 1995
144          */
145         for (n = start; n < nrend; n++)
146         {
147             imass = invmass[n];
148             if (cFREEZE)
149             {
150                 gf   = cFREEZE[n];
151             }
152             if (cACC)
153             {
154                 ga   = cACC[n];
155             }
156             if (cTC)
157             {
158                 gt   = cTC[n];
159             }
160             lg   = tcstat[gt].lambda;
161             if (bNH)
162             {
163                 vxi   = nh_vxi[gt];
164             }
165             rvec_sub(v[n], gstat[ga].u, vrel);
166
167             for (d = 0; d < DIM; d++)
168             {
169                 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
170                 {
171                     vnrel = (lg*vrel[d] + dt*(imass*f[n][d] - 0.5*vxi*vrel[d]
172                                               - iprod(M[d], vrel)))/(1 + 0.5*vxi*dt);
173                     /* do not scale the mean velocities u */
174                     vn             = gstat[ga].u[d] + accel[ga][d]*dt + vnrel;
175                     v[n][d]        = vn;
176                     xprime[n][d]   = x[n][d]+vn*dt;
177                 }
178                 else
179                 {
180                     v[n][d]        = 0.0;
181                     xprime[n][d]   = x[n][d];
182                 }
183             }
184         }
185     }
186     else if (cFREEZE != NULL ||
187              nFreeze[0][XX] || nFreeze[0][YY] || nFreeze[0][ZZ] ||
188              bNEMD)
189     {
190         /* Update with Berendsen/v-rescale coupling and freeze or NEMD */
191         for (n = start; n < nrend; n++)
192         {
193             w_dt = invmass[n]*dt;
194             if (cFREEZE)
195             {
196                 gf   = cFREEZE[n];
197             }
198             if (cACC)
199             {
200                 ga   = cACC[n];
201             }
202             if (cTC)
203             {
204                 gt   = cTC[n];
205             }
206             lg   = tcstat[gt].lambda;
207
208             for (d = 0; d < DIM; d++)
209             {
210                 vn             = v[n][d];
211                 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
212                 {
213                     vv             = lg*vn + f[n][d]*w_dt;
214
215                     /* do not scale the mean velocities u */
216                     u              = gstat[ga].u[d];
217                     va             = vv + accel[ga][d]*dt;
218                     vb             = va + (1.0-lg)*u;
219                     v[n][d]        = vb;
220                     xprime[n][d]   = x[n][d]+vb*dt;
221                 }
222                 else
223                 {
224                     v[n][d]        = 0.0;
225                     xprime[n][d]   = x[n][d];
226                 }
227             }
228         }
229     }
230     else
231     {
232         /* Plain update with Berendsen/v-rescale coupling */
233         for (n = start; n < nrend; n++)
234         {
235             if ((ptype[n] != eptVSite) && (ptype[n] != eptShell))
236             {
237                 w_dt = invmass[n]*dt;
238                 if (cTC)
239                 {
240                     gt = cTC[n];
241                 }
242                 lg = tcstat[gt].lambda;
243
244                 for (d = 0; d < DIM; d++)
245                 {
246                     vn           = lg*v[n][d] + f[n][d]*w_dt;
247                     v[n][d]      = vn;
248                     xprime[n][d] = x[n][d] + vn*dt;
249                 }
250             }
251             else
252             {
253                 for (d = 0; d < DIM; d++)
254                 {
255                     v[n][d]        = 0.0;
256                     xprime[n][d]   = x[n][d];
257                 }
258             }
259         }
260     }
261 }
262
263 static void do_update_vv_vel(int start, int nrend, double dt,
264                              rvec accel[], ivec nFreeze[], real invmass[],
265                              unsigned short ptype[], unsigned short cFREEZE[],
266                              unsigned short cACC[], rvec v[], rvec f[],
267                              gmx_bool bExtended, real veta, real alpha)
268 {
269     double imass, w_dt;
270     int    gf = 0, ga = 0;
271     rvec   vrel;
272     real   u, vn, vv, va, vb, vnrel;
273     int    n, d;
274     double g, mv1, mv2;
275
276     if (bExtended)
277     {
278         g        = 0.25*dt*veta*alpha;
279         mv1      = exp(-g);
280         mv2      = series_sinhx(g);
281     }
282     else
283     {
284         mv1      = 1.0;
285         mv2      = 1.0;
286     }
287     for (n = start; n < nrend; n++)
288     {
289         w_dt = invmass[n]*dt;
290         if (cFREEZE)
291         {
292             gf   = cFREEZE[n];
293         }
294         if (cACC)
295         {
296             ga   = cACC[n];
297         }
298
299         for (d = 0; d < DIM; d++)
300         {
301             if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
302             {
303                 v[n][d]             = mv1*(mv1*v[n][d] + 0.5*(w_dt*mv2*f[n][d]))+0.5*accel[ga][d]*dt;
304             }
305             else
306             {
307                 v[n][d]        = 0.0;
308             }
309         }
310     }
311 } /* do_update_vv_vel */
312
313 static void do_update_vv_pos(int start, int nrend, double dt,
314                              ivec nFreeze[],
315                              unsigned short ptype[], unsigned short cFREEZE[],
316                              rvec x[], rvec xprime[], rvec v[],
317                              gmx_bool bExtended, real veta)
318 {
319     double imass, w_dt;
320     int    gf = 0;
321     int    n, d;
322     double g, mr1, mr2;
323
324     /* Would it make more sense if Parrinello-Rahman was put here? */
325     if (bExtended)
326     {
327         g        = 0.5*dt*veta;
328         mr1      = exp(g);
329         mr2      = series_sinhx(g);
330     }
331     else
332     {
333         mr1      = 1.0;
334         mr2      = 1.0;
335     }
336
337     for (n = start; n < nrend; n++)
338     {
339
340         if (cFREEZE)
341         {
342             gf   = cFREEZE[n];
343         }
344
345         for (d = 0; d < DIM; d++)
346         {
347             if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
348             {
349                 xprime[n][d]   = mr1*(mr1*x[n][d]+mr2*dt*v[n][d]);
350             }
351             else
352             {
353                 xprime[n][d]   = x[n][d];
354             }
355         }
356     }
357 } /* do_update_vv_pos */
358
359 static void do_update_visc(int start, int nrend, double dt,
360                            t_grp_tcstat *tcstat,
361                            double nh_vxi[],
362                            real invmass[],
363                            unsigned short ptype[], unsigned short cTC[],
364                            rvec x[], rvec xprime[], rvec v[],
365                            rvec f[], matrix M, matrix box, real
366                            cos_accel, real vcos,
367                            gmx_bool bNH, gmx_bool bPR)
368 {
369     double imass, w_dt;
370     int    gt = 0;
371     real   vn, vc;
372     real   lg, vxi = 0, vv;
373     real   fac, cosz;
374     rvec   vrel;
375     int    n, d;
376
377     fac = 2*M_PI/(box[ZZ][ZZ]);
378
379     if (bNH || bPR)
380     {
381         /* Update with coupling to extended ensembles, used for
382          * Nose-Hoover and Parrinello-Rahman coupling
383          */
384         for (n = start; n < nrend; n++)
385         {
386             imass = invmass[n];
387             if (cTC)
388             {
389                 gt   = cTC[n];
390             }
391             lg   = tcstat[gt].lambda;
392             cosz = cos(fac*x[n][ZZ]);
393
394             copy_rvec(v[n], vrel);
395
396             vc            = cosz*vcos;
397             vrel[XX]     -= vc;
398             if (bNH)
399             {
400                 vxi        = nh_vxi[gt];
401             }
402             for (d = 0; d < DIM; d++)
403             {
404                 vn             = v[n][d];
405
406                 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell))
407                 {
408                     vn  = (lg*vrel[d] + dt*(imass*f[n][d] - 0.5*vxi*vrel[d]
409                                             - iprod(M[d], vrel)))/(1 + 0.5*vxi*dt);
410                     if (d == XX)
411                     {
412                         vn += vc + dt*cosz*cos_accel;
413                     }
414                     v[n][d]        = vn;
415                     xprime[n][d]   = x[n][d]+vn*dt;
416                 }
417                 else
418                 {
419                     xprime[n][d]   = x[n][d];
420                 }
421             }
422         }
423     }
424     else
425     {
426         /* Classic version of update, used with berendsen coupling */
427         for (n = start; n < nrend; n++)
428         {
429             w_dt = invmass[n]*dt;
430             if (cTC)
431             {
432                 gt   = cTC[n];
433             }
434             lg   = tcstat[gt].lambda;
435             cosz = cos(fac*x[n][ZZ]);
436
437             for (d = 0; d < DIM; d++)
438             {
439                 vn             = v[n][d];
440
441                 if ((ptype[n] != eptVSite) && (ptype[n] != eptShell))
442                 {
443                     if (d == XX)
444                     {
445                         vc           = cosz*vcos;
446                         /* Do not scale the cosine velocity profile */
447                         vv           = vc + lg*(vn - vc + f[n][d]*w_dt);
448                         /* Add the cosine accelaration profile */
449                         vv          += dt*cosz*cos_accel;
450                     }
451                     else
452                     {
453                         vv           = lg*(vn + f[n][d]*w_dt);
454                     }
455                     v[n][d]        = vv;
456                     xprime[n][d]   = x[n][d]+vv*dt;
457                 }
458                 else
459                 {
460                     v[n][d]        = 0.0;
461                     xprime[n][d]   = x[n][d];
462                 }
463             }
464         }
465     }
466 }
467
468 static gmx_stochd_t *init_stochd(t_inputrec *ir)
469 {
470     gmx_stochd_t   *sd;
471     gmx_sd_const_t *sdc;
472     int             ngtc, n, th;
473     real            y;
474
475     snew(sd, 1);
476
477     ngtc = ir->opts.ngtc;
478
479     if (ir->eI == eiBD)
480     {
481         snew(sd->bd_rf, ngtc);
482     }
483     else if (EI_SD(ir->eI))
484     {
485         snew(sd->sdc, ngtc);
486         snew(sd->sdsig, ngtc);
487
488         sdc = sd->sdc;
489         for (n = 0; n < ngtc; n++)
490         {
491             if (ir->opts.tau_t[n] > 0)
492             {
493                 sdc[n].gdt = ir->delta_t/ir->opts.tau_t[n];
494                 sdc[n].eph = exp(sdc[n].gdt/2);
495                 sdc[n].emh = exp(-sdc[n].gdt/2);
496                 sdc[n].em  = exp(-sdc[n].gdt);
497             }
498             else
499             {
500                 /* No friction and noise on this group */
501                 sdc[n].gdt = 0;
502                 sdc[n].eph = 1;
503                 sdc[n].emh = 1;
504                 sdc[n].em  = 1;
505             }
506             if (sdc[n].gdt >= 0.05)
507             {
508                 sdc[n].b = sdc[n].gdt*(sdc[n].eph*sdc[n].eph - 1)
509                     - 4*(sdc[n].eph - 1)*(sdc[n].eph - 1);
510                 sdc[n].c = sdc[n].gdt - 3 + 4*sdc[n].emh - sdc[n].em;
511                 sdc[n].d = 2 - sdc[n].eph - sdc[n].emh;
512             }
513             else
514             {
515                 y = sdc[n].gdt/2;
516                 /* Seventh order expansions for small y */
517                 sdc[n].b = y*y*y*y*(1/3.0+y*(1/3.0+y*(17/90.0+y*7/9.0)));
518                 sdc[n].c = y*y*y*(2/3.0+y*(-1/2.0+y*(7/30.0+y*(-1/12.0+y*31/1260.0))));
519                 sdc[n].d = y*y*(-1+y*y*(-1/12.0-y*y/360.0));
520             }
521             if (debug)
522             {
523                 fprintf(debug, "SD const tc-grp %d: b %g  c %g  d %g\n",
524                         n, sdc[n].b, sdc[n].c, sdc[n].d);
525             }
526         }
527     }
528     else if (ETC_ANDERSEN(ir->etc))
529     {
530         int        ngtc;
531         t_grpopts *opts;
532         real       reft;
533
534         opts = &ir->opts;
535         ngtc = opts->ngtc;
536
537         snew(sd->randomize_group, ngtc);
538         snew(sd->boltzfac, ngtc);
539
540         /* for now, assume that all groups, if randomized, are randomized at the same rate, i.e. tau_t is the same. */
541         /* since constraint groups don't necessarily match up with temperature groups! This is checked in readir.c */
542
543         for (n = 0; n < ngtc; n++)
544         {
545             reft = max(0.0, opts->ref_t[n]);
546             if ((opts->tau_t[n] > 0) && (reft > 0))  /* tau_t or ref_t = 0 means that no randomization is done */
547             {
548                 sd->randomize_group[n] = TRUE;
549                 sd->boltzfac[n]        = BOLTZ*opts->ref_t[n];
550             }
551             else
552             {
553                 sd->randomize_group[n] = FALSE;
554             }
555         }
556     }
557     return sd;
558 }
559
560 gmx_update_t init_update(t_inputrec *ir)
561 {
562     t_gmx_update *upd;
563
564     snew(upd, 1);
565
566     if (ir->eI == eiBD || EI_SD(ir->eI) || ir->etc == etcVRESCALE || ETC_ANDERSEN(ir->etc))
567     {
568         upd->sd    = init_stochd(ir);
569     }
570
571     upd->xp        = NULL;
572     upd->xp_nalloc = 0;
573
574     return upd;
575 }
576
577 static void do_update_sd1(gmx_stochd_t *sd,
578                           int start, int nrend, double dt,
579                           rvec accel[], ivec nFreeze[],
580                           real invmass[], unsigned short ptype[],
581                           unsigned short cFREEZE[], unsigned short cACC[],
582                           unsigned short cTC[],
583                           rvec x[], rvec xprime[], rvec v[], rvec f[],
584                           int ngtc, real tau_t[], real ref_t[],
585                           gmx_int64_t step, int seed, int* gatindex)
586 {
587     gmx_sd_const_t *sdc;
588     gmx_sd_sigma_t *sig;
589     real            kT;
590     int             gf = 0, ga = 0, gt = 0;
591     real            ism, sd_V;
592     int             n, d;
593
594     sdc = sd->sdc;
595     sig = sd->sdsig;
596
597     for (n = 0; n < ngtc; n++)
598     {
599         kT = BOLTZ*ref_t[n];
600         /* The mass is encounted for later, since this differs per atom */
601         sig[n].V  = sqrt(kT*(1 - sdc[n].em*sdc[n].em));
602     }
603
604     for (n = start; n < nrend; n++)
605     {
606         real rnd[3];
607         int  ng  = gatindex ? gatindex[n] : n;
608         ism = sqrt(invmass[n]);
609         if (cFREEZE)
610         {
611             gf  = cFREEZE[n];
612         }
613         if (cACC)
614         {
615             ga  = cACC[n];
616         }
617         if (cTC)
618         {
619             gt  = cTC[n];
620         }
621
622         gmx_rng_cycle_3gaussian_table(step, ng, seed, RND_SEED_UPDATE, rnd);
623         for (d = 0; d < DIM; d++)
624         {
625             if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
626             {
627                 sd_V = ism*sig[gt].V*rnd[d];
628
629                 v[n][d] = v[n][d]*sdc[gt].em
630                     + (invmass[n]*f[n][d] + accel[ga][d])*tau_t[gt]*(1 - sdc[gt].em)
631                     + sd_V;
632
633                 xprime[n][d] = x[n][d] + v[n][d]*dt;
634             }
635             else
636             {
637                 v[n][d]      = 0.0;
638                 xprime[n][d] = x[n][d];
639             }
640         }
641     }
642 }
643
644 static void check_sd2_work_data_allocation(gmx_stochd_t *sd, int nrend)
645 {
646     if (nrend > sd->sd_V_nalloc)
647     {
648         sd->sd_V_nalloc = over_alloc_dd(nrend);
649         srenew(sd->sd_V, sd->sd_V_nalloc);
650     }
651 }
652
653 static void do_update_sd2_Tconsts(gmx_stochd_t *sd,
654                                   int           ngtc,
655                                   const real    tau_t[],
656                                   const real    ref_t[])
657 {
658     /* This is separated from the update below, because it is single threaded */
659     gmx_sd_const_t *sdc;
660     gmx_sd_sigma_t *sig;
661     int             gt;
662     real            kT;
663
664     sdc = sd->sdc;
665     sig = sd->sdsig;
666
667     for (gt = 0; gt < ngtc; gt++)
668     {
669         kT = BOLTZ*ref_t[gt];
670         /* The mass is encounted for later, since this differs per atom */
671         sig[gt].V  = sqrt(kT*(1-sdc[gt].em));
672         sig[gt].X  = sqrt(kT*sqr(tau_t[gt])*sdc[gt].c);
673         sig[gt].Yv = sqrt(kT*sdc[gt].b/sdc[gt].c);
674         sig[gt].Yx = sqrt(kT*sqr(tau_t[gt])*sdc[gt].b/(1-sdc[gt].em));
675     }
676 }
677
678 static void do_update_sd2(gmx_stochd_t *sd,
679                           gmx_bool bInitStep,
680                           int start, int nrend,
681                           rvec accel[], ivec nFreeze[],
682                           real invmass[], unsigned short ptype[],
683                           unsigned short cFREEZE[], unsigned short cACC[],
684                           unsigned short cTC[],
685                           rvec x[], rvec xprime[], rvec v[], rvec f[],
686                           rvec sd_X[],
687                           const real tau_t[],
688                           gmx_bool bFirstHalf, gmx_int64_t step, int seed,
689                           int* gatindex)
690 {
691     gmx_sd_const_t *sdc;
692     gmx_sd_sigma_t *sig;
693     /* The random part of the velocity update, generated in the first
694      * half of the update, needs to be remembered for the second half.
695      */
696     rvec  *sd_V;
697     real   kT;
698     int    gf = 0, ga = 0, gt = 0;
699     real   vn = 0, Vmh, Xmh;
700     real   ism;
701     int    n, d, ng;
702
703     sdc  = sd->sdc;
704     sig  = sd->sdsig;
705     sd_V = sd->sd_V;
706
707     for (n = start; n < nrend; n++)
708     {
709         real rnd[6], rndi[3];
710         ng  = gatindex ? gatindex[n] : n;
711         ism = sqrt(invmass[n]);
712         if (cFREEZE)
713         {
714             gf  = cFREEZE[n];
715         }
716         if (cACC)
717         {
718             ga  = cACC[n];
719         }
720         if (cTC)
721         {
722             gt  = cTC[n];
723         }
724
725         gmx_rng_cycle_6gaussian_table(step*2+(bFirstHalf ? 1 : 2), ng, seed, RND_SEED_UPDATE, rnd);
726         if (bInitStep)
727         {
728             gmx_rng_cycle_3gaussian_table(step*2, ng, seed, RND_SEED_UPDATE, rndi);
729         }
730         for (d = 0; d < DIM; d++)
731         {
732             if (bFirstHalf)
733             {
734                 vn             = v[n][d];
735             }
736             if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
737             {
738                 if (bFirstHalf)
739                 {
740                     if (bInitStep)
741                     {
742                         sd_X[n][d] = ism*sig[gt].X*rndi[d];
743                     }
744                     Vmh = sd_X[n][d]*sdc[gt].d/(tau_t[gt]*sdc[gt].c)
745                         + ism*sig[gt].Yv*rnd[d*2];
746                     sd_V[n][d] = ism*sig[gt].V*rnd[d*2+1];
747
748                     v[n][d] = vn*sdc[gt].em
749                         + (invmass[n]*f[n][d] + accel[ga][d])*tau_t[gt]*(1 - sdc[gt].em)
750                         + sd_V[n][d] - sdc[gt].em*Vmh;
751
752                     xprime[n][d] = x[n][d] + v[n][d]*tau_t[gt]*(sdc[gt].eph - sdc[gt].emh);
753                 }
754                 else
755                 {
756                     /* Correct the velocities for the constraints.
757                      * This operation introduces some inaccuracy,
758                      * since the velocity is determined from differences in coordinates.
759                      */
760                     v[n][d] =
761                         (xprime[n][d] - x[n][d])/(tau_t[gt]*(sdc[gt].eph - sdc[gt].emh));
762
763                     Xmh = sd_V[n][d]*tau_t[gt]*sdc[gt].d/(sdc[gt].em-1)
764                         + ism*sig[gt].Yx*rnd[d*2];
765                     sd_X[n][d] = ism*sig[gt].X*rnd[d*2+1];
766
767                     xprime[n][d] += sd_X[n][d] - Xmh;
768
769                 }
770             }
771             else
772             {
773                 if (bFirstHalf)
774                 {
775                     v[n][d]        = 0.0;
776                     xprime[n][d]   = x[n][d];
777                 }
778             }
779         }
780     }
781 }
782
783 static void do_update_bd_Tconsts(double dt, real friction_coefficient,
784                                  int ngtc, const real ref_t[],
785                                  real *rf)
786 {
787     /* This is separated from the update below, because it is single threaded */
788     int gt;
789
790     if (friction_coefficient != 0)
791     {
792         for (gt = 0; gt < ngtc; gt++)
793         {
794             rf[gt] = sqrt(2.0*BOLTZ*ref_t[gt]/(friction_coefficient*dt));
795         }
796     }
797     else
798     {
799         for (gt = 0; gt < ngtc; gt++)
800         {
801             rf[gt] = sqrt(2.0*BOLTZ*ref_t[gt]);
802         }
803     }
804 }
805
806 static void do_update_bd(int start, int nrend, double dt,
807                          ivec nFreeze[],
808                          real invmass[], unsigned short ptype[],
809                          unsigned short cFREEZE[], unsigned short cTC[],
810                          rvec x[], rvec xprime[], rvec v[],
811                          rvec f[], real friction_coefficient,
812                          real *rf, gmx_int64_t step, int seed,
813                          int* gatindex)
814 {
815     /* note -- these appear to be full step velocities . . .  */
816     int    gf = 0, gt = 0;
817     real   vn;
818     real   invfr = 0;
819     int    n, d;
820
821     if (friction_coefficient != 0)
822     {
823         invfr = 1.0/friction_coefficient;
824     }
825
826     for (n = start; (n < nrend); n++)
827     {
828         real rnd[3];
829         int  ng  = gatindex ? gatindex[n] : n;
830
831         if (cFREEZE)
832         {
833             gf = cFREEZE[n];
834         }
835         if (cTC)
836         {
837             gt = cTC[n];
838         }
839         gmx_rng_cycle_3gaussian_table(step, ng, seed, RND_SEED_UPDATE, rnd);
840         for (d = 0; (d < DIM); d++)
841         {
842             if ((ptype[n] != eptVSite) && (ptype[n] != eptShell) && !nFreeze[gf][d])
843             {
844                 if (friction_coefficient != 0)
845                 {
846                     vn = invfr*f[n][d] + rf[gt]*rnd[d];
847                 }
848                 else
849                 {
850                     /* NOTE: invmass = 2/(mass*friction_constant*dt) */
851                     vn = 0.5*invmass[n]*f[n][d]*dt
852                         + sqrt(0.5*invmass[n])*rf[gt]*rnd[d];
853                 }
854
855                 v[n][d]      = vn;
856                 xprime[n][d] = x[n][d]+vn*dt;
857             }
858             else
859             {
860                 v[n][d]      = 0.0;
861                 xprime[n][d] = x[n][d];
862             }
863         }
864     }
865 }
866
867 static void dump_it_all(FILE gmx_unused *fp, const char gmx_unused *title,
868                         int gmx_unused natoms, rvec gmx_unused x[], rvec gmx_unused xp[],
869                         rvec gmx_unused v[], rvec gmx_unused f[])
870 {
871 #ifdef DEBUG
872     if (fp)
873     {
874         fprintf(fp, "%s\n", title);
875         pr_rvecs(fp, 0, "x", x, natoms);
876         pr_rvecs(fp, 0, "xp", xp, natoms);
877         pr_rvecs(fp, 0, "v", v, natoms);
878         pr_rvecs(fp, 0, "f", f, natoms);
879     }
880 #endif
881 }
882
883 static void calc_ke_part_normal(rvec v[], t_grpopts *opts, t_mdatoms *md,
884                                 gmx_ekindata_t *ekind, t_nrnb *nrnb, gmx_bool bEkinAveVel,
885                                 gmx_bool bSaveEkinOld)
886 {
887     int           g;
888     t_grp_tcstat *tcstat  = ekind->tcstat;
889     t_grp_acc    *grpstat = ekind->grpstat;
890     int           nthread, thread;
891
892     /* three main: VV with AveVel, vv with AveEkin, leap with AveEkin.  Leap with AveVel is also
893        an option, but not supported now.  Additionally, if we are doing iterations.
894        bEkinAveVel: If TRUE, we sum into ekin, if FALSE, into ekinh.
895        bSavEkinOld: If TRUE (in the case of iteration = bIterate is TRUE), we don't copy over the ekinh_old.
896        If FALSE, we overrwrite it.
897      */
898
899     /* group velocities are calculated in update_ekindata and
900      * accumulated in acumulate_groups.
901      * Now the partial global and groups ekin.
902      */
903     for (g = 0; (g < opts->ngtc); g++)
904     {
905
906         if (!bSaveEkinOld)
907         {
908             copy_mat(tcstat[g].ekinh, tcstat[g].ekinh_old);
909         }
910         if (bEkinAveVel)
911         {
912             clear_mat(tcstat[g].ekinf);
913         }
914         else
915         {
916             clear_mat(tcstat[g].ekinh);
917         }
918         if (bEkinAveVel)
919         {
920             tcstat[g].ekinscalef_nhc = 1.0; /* need to clear this -- logic is complicated! */
921         }
922     }
923     ekind->dekindl_old = ekind->dekindl;
924
925     nthread = gmx_omp_nthreads_get(emntUpdate);
926
927 #pragma omp parallel for num_threads(nthread) schedule(static)
928     for (thread = 0; thread < nthread; thread++)
929     {
930         int     start_t, end_t, n;
931         int     ga, gt;
932         rvec    v_corrt;
933         real    hm;
934         int     d, m;
935         matrix *ekin_sum;
936         real   *dekindl_sum;
937
938         start_t = ((thread+0)*md->homenr)/nthread;
939         end_t   = ((thread+1)*md->homenr)/nthread;
940
941         ekin_sum    = ekind->ekin_work[thread];
942         dekindl_sum = ekind->dekindl_work[thread];
943
944         for (gt = 0; gt < opts->ngtc; gt++)
945         {
946             clear_mat(ekin_sum[gt]);
947         }
948         *dekindl_sum = 0.0;
949
950         ga = 0;
951         gt = 0;
952         for (n = start_t; n < end_t; n++)
953         {
954             if (md->cACC)
955             {
956                 ga = md->cACC[n];
957             }
958             if (md->cTC)
959             {
960                 gt = md->cTC[n];
961             }
962             hm   = 0.5*md->massT[n];
963
964             for (d = 0; (d < DIM); d++)
965             {
966                 v_corrt[d]  = v[n][d]  - grpstat[ga].u[d];
967             }
968             for (d = 0; (d < DIM); d++)
969             {
970                 for (m = 0; (m < DIM); m++)
971                 {
972                     /* if we're computing a full step velocity, v_corrt[d] has v(t).  Otherwise, v(t+dt/2) */
973                     ekin_sum[gt][m][d] += hm*v_corrt[m]*v_corrt[d];
974                 }
975             }
976             if (md->nMassPerturbed && md->bPerturbed[n])
977             {
978                 *dekindl_sum +=
979                     0.5*(md->massB[n] - md->massA[n])*iprod(v_corrt, v_corrt);
980             }
981         }
982     }
983
984     ekind->dekindl = 0;
985     for (thread = 0; thread < nthread; thread++)
986     {
987         for (g = 0; g < opts->ngtc; g++)
988         {
989             if (bEkinAveVel)
990             {
991                 m_add(tcstat[g].ekinf, ekind->ekin_work[thread][g],
992                       tcstat[g].ekinf);
993             }
994             else
995             {
996                 m_add(tcstat[g].ekinh, ekind->ekin_work[thread][g],
997                       tcstat[g].ekinh);
998             }
999         }
1000
1001         ekind->dekindl += *ekind->dekindl_work[thread];
1002     }
1003
1004     inc_nrnb(nrnb, eNR_EKIN, md->homenr);
1005 }
1006
1007 static void calc_ke_part_visc(matrix box, rvec x[], rvec v[],
1008                               t_grpopts *opts, t_mdatoms *md,
1009                               gmx_ekindata_t *ekind,
1010                               t_nrnb *nrnb, gmx_bool bEkinAveVel)
1011 {
1012     int           start = 0, homenr = md->homenr;
1013     int           g, d, n, m, gt = 0;
1014     rvec          v_corrt;
1015     real          hm;
1016     t_grp_tcstat *tcstat = ekind->tcstat;
1017     t_cos_acc    *cosacc = &(ekind->cosacc);
1018     real          dekindl;
1019     real          fac, cosz;
1020     double        mvcos;
1021
1022     for (g = 0; g < opts->ngtc; g++)
1023     {
1024         copy_mat(ekind->tcstat[g].ekinh, ekind->tcstat[g].ekinh_old);
1025         clear_mat(ekind->tcstat[g].ekinh);
1026     }
1027     ekind->dekindl_old = ekind->dekindl;
1028
1029     fac     = 2*M_PI/box[ZZ][ZZ];
1030     mvcos   = 0;
1031     dekindl = 0;
1032     for (n = start; n < start+homenr; n++)
1033     {
1034         if (md->cTC)
1035         {
1036             gt = md->cTC[n];
1037         }
1038         hm   = 0.5*md->massT[n];
1039
1040         /* Note that the times of x and v differ by half a step */
1041         /* MRS -- would have to be changed for VV */
1042         cosz         = cos(fac*x[n][ZZ]);
1043         /* Calculate the amplitude of the new velocity profile */
1044         mvcos       += 2*cosz*md->massT[n]*v[n][XX];
1045
1046         copy_rvec(v[n], v_corrt);
1047         /* Subtract the profile for the kinetic energy */
1048         v_corrt[XX] -= cosz*cosacc->vcos;
1049         for (d = 0; (d < DIM); d++)
1050         {
1051             for (m = 0; (m < DIM); m++)
1052             {
1053                 /* if we're computing a full step velocity, v_corrt[d] has v(t).  Otherwise, v(t+dt/2) */
1054                 if (bEkinAveVel)
1055                 {
1056                     tcstat[gt].ekinf[m][d] += hm*v_corrt[m]*v_corrt[d];
1057                 }
1058                 else
1059                 {
1060                     tcstat[gt].ekinh[m][d] += hm*v_corrt[m]*v_corrt[d];
1061                 }
1062             }
1063         }
1064         if (md->nPerturbed && md->bPerturbed[n])
1065         {
1066             dekindl += 0.5*(md->massB[n] - md->massA[n])*iprod(v_corrt, v_corrt);
1067         }
1068     }
1069     ekind->dekindl = dekindl;
1070     cosacc->mvcos  = mvcos;
1071
1072     inc_nrnb(nrnb, eNR_EKIN, homenr);
1073 }
1074
1075 void calc_ke_part(t_state *state, t_grpopts *opts, t_mdatoms *md,
1076                   gmx_ekindata_t *ekind, t_nrnb *nrnb, gmx_bool bEkinAveVel, gmx_bool bSaveEkinOld)
1077 {
1078     if (ekind->cosacc.cos_accel == 0)
1079     {
1080         calc_ke_part_normal(state->v, opts, md, ekind, nrnb, bEkinAveVel, bSaveEkinOld);
1081     }
1082     else
1083     {
1084         calc_ke_part_visc(state->box, state->x, state->v, opts, md, ekind, nrnb, bEkinAveVel);
1085     }
1086 }
1087
1088 extern void init_ekinstate(ekinstate_t *ekinstate, const t_inputrec *ir)
1089 {
1090     ekinstate->ekin_n = ir->opts.ngtc;
1091     snew(ekinstate->ekinh, ekinstate->ekin_n);
1092     snew(ekinstate->ekinf, ekinstate->ekin_n);
1093     snew(ekinstate->ekinh_old, ekinstate->ekin_n);
1094     snew(ekinstate->ekinscalef_nhc, ekinstate->ekin_n);
1095     snew(ekinstate->ekinscaleh_nhc, ekinstate->ekin_n);
1096     snew(ekinstate->vscale_nhc, ekinstate->ekin_n);
1097     ekinstate->dekindl = 0;
1098     ekinstate->mvcos   = 0;
1099 }
1100
1101 void update_ekinstate(ekinstate_t *ekinstate, gmx_ekindata_t *ekind)
1102 {
1103     int i;
1104
1105     for (i = 0; i < ekinstate->ekin_n; i++)
1106     {
1107         copy_mat(ekind->tcstat[i].ekinh, ekinstate->ekinh[i]);
1108         copy_mat(ekind->tcstat[i].ekinf, ekinstate->ekinf[i]);
1109         copy_mat(ekind->tcstat[i].ekinh_old, ekinstate->ekinh_old[i]);
1110         ekinstate->ekinscalef_nhc[i] = ekind->tcstat[i].ekinscalef_nhc;
1111         ekinstate->ekinscaleh_nhc[i] = ekind->tcstat[i].ekinscaleh_nhc;
1112         ekinstate->vscale_nhc[i]     = ekind->tcstat[i].vscale_nhc;
1113     }
1114
1115     copy_mat(ekind->ekin, ekinstate->ekin_total);
1116     ekinstate->dekindl = ekind->dekindl;
1117     ekinstate->mvcos   = ekind->cosacc.mvcos;
1118
1119 }
1120
1121 void restore_ekinstate_from_state(t_commrec *cr,
1122                                   gmx_ekindata_t *ekind, ekinstate_t *ekinstate)
1123 {
1124     int i, n;
1125
1126     if (MASTER(cr))
1127     {
1128         for (i = 0; i < ekinstate->ekin_n; i++)
1129         {
1130             copy_mat(ekinstate->ekinh[i], ekind->tcstat[i].ekinh);
1131             copy_mat(ekinstate->ekinf[i], ekind->tcstat[i].ekinf);
1132             copy_mat(ekinstate->ekinh_old[i], ekind->tcstat[i].ekinh_old);
1133             ekind->tcstat[i].ekinscalef_nhc = ekinstate->ekinscalef_nhc[i];
1134             ekind->tcstat[i].ekinscaleh_nhc = ekinstate->ekinscaleh_nhc[i];
1135             ekind->tcstat[i].vscale_nhc     = ekinstate->vscale_nhc[i];
1136         }
1137
1138         copy_mat(ekinstate->ekin_total, ekind->ekin);
1139
1140         ekind->dekindl      = ekinstate->dekindl;
1141         ekind->cosacc.mvcos = ekinstate->mvcos;
1142         n                   = ekinstate->ekin_n;
1143     }
1144
1145     if (PAR(cr))
1146     {
1147         gmx_bcast(sizeof(n), &n, cr);
1148         for (i = 0; i < n; i++)
1149         {
1150             gmx_bcast(DIM*DIM*sizeof(ekind->tcstat[i].ekinh[0][0]),
1151                       ekind->tcstat[i].ekinh[0], cr);
1152             gmx_bcast(DIM*DIM*sizeof(ekind->tcstat[i].ekinf[0][0]),
1153                       ekind->tcstat[i].ekinf[0], cr);
1154             gmx_bcast(DIM*DIM*sizeof(ekind->tcstat[i].ekinh_old[0][0]),
1155                       ekind->tcstat[i].ekinh_old[0], cr);
1156
1157             gmx_bcast(sizeof(ekind->tcstat[i].ekinscalef_nhc),
1158                       &(ekind->tcstat[i].ekinscalef_nhc), cr);
1159             gmx_bcast(sizeof(ekind->tcstat[i].ekinscaleh_nhc),
1160                       &(ekind->tcstat[i].ekinscaleh_nhc), cr);
1161             gmx_bcast(sizeof(ekind->tcstat[i].vscale_nhc),
1162                       &(ekind->tcstat[i].vscale_nhc), cr);
1163         }
1164         gmx_bcast(DIM*DIM*sizeof(ekind->ekin[0][0]),
1165                   ekind->ekin[0], cr);
1166
1167         gmx_bcast(sizeof(ekind->dekindl), &ekind->dekindl, cr);
1168         gmx_bcast(sizeof(ekind->cosacc.mvcos), &ekind->cosacc.mvcos, cr);
1169     }
1170 }
1171
1172 void set_deform_reference_box(gmx_update_t upd, gmx_int64_t step, matrix box)
1173 {
1174     upd->deformref_step = step;
1175     copy_mat(box, upd->deformref_box);
1176 }
1177
1178 static void deform(gmx_update_t upd,
1179                    int start, int homenr, rvec x[], matrix box, matrix *scale_tot,
1180                    const t_inputrec *ir, gmx_int64_t step)
1181 {
1182     matrix bnew, invbox, mu;
1183     real   elapsed_time;
1184     int    i, j;
1185
1186     elapsed_time = (step + 1 - upd->deformref_step)*ir->delta_t;
1187     copy_mat(box, bnew);
1188     for (i = 0; i < DIM; i++)
1189     {
1190         for (j = 0; j < DIM; j++)
1191         {
1192             if (ir->deform[i][j] != 0)
1193             {
1194                 bnew[i][j] =
1195                     upd->deformref_box[i][j] + elapsed_time*ir->deform[i][j];
1196             }
1197         }
1198     }
1199     /* We correct the off-diagonal elements,
1200      * which can grow indefinitely during shearing,
1201      * so the shifts do not get messed up.
1202      */
1203     for (i = 1; i < DIM; i++)
1204     {
1205         for (j = i-1; j >= 0; j--)
1206         {
1207             while (bnew[i][j] - box[i][j] > 0.5*bnew[j][j])
1208             {
1209                 rvec_dec(bnew[i], bnew[j]);
1210             }
1211             while (bnew[i][j] - box[i][j] < -0.5*bnew[j][j])
1212             {
1213                 rvec_inc(bnew[i], bnew[j]);
1214             }
1215         }
1216     }
1217     m_inv_ur0(box, invbox);
1218     copy_mat(bnew, box);
1219     mmul_ur0(box, invbox, mu);
1220
1221     for (i = start; i < start+homenr; i++)
1222     {
1223         x[i][XX] = mu[XX][XX]*x[i][XX]+mu[YY][XX]*x[i][YY]+mu[ZZ][XX]*x[i][ZZ];
1224         x[i][YY] = mu[YY][YY]*x[i][YY]+mu[ZZ][YY]*x[i][ZZ];
1225         x[i][ZZ] = mu[ZZ][ZZ]*x[i][ZZ];
1226     }
1227     if (*scale_tot)
1228     {
1229         /* The transposes of the scaling matrices are stored,
1230          * so we need to do matrix multiplication in the inverse order.
1231          */
1232         mmul_ur0(*scale_tot, mu, *scale_tot);
1233     }
1234 }
1235
1236 static void combine_forces(int nstcalclr,
1237                            gmx_constr_t constr,
1238                            t_inputrec *ir, t_mdatoms *md, t_idef *idef,
1239                            t_commrec *cr,
1240                            gmx_int64_t step,
1241                            t_state *state, gmx_bool bMolPBC,
1242                            int start, int nrend,
1243                            rvec f[], rvec f_lr[],
1244                            t_nrnb *nrnb)
1245 {
1246     int  i, d, nm1;
1247
1248     /* f contains the short-range forces + the long range forces
1249      * which are stored separately in f_lr.
1250      */
1251
1252     if (constr != NULL && !(ir->eConstrAlg == econtSHAKE && ir->epc == epcNO))
1253     {
1254         /* We need to constrain the LR forces separately,
1255          * because due to the different pre-factor for the SR and LR
1256          * forces in the update algorithm, we can not determine
1257          * the constraint force for the coordinate constraining.
1258          * Constrain only the additional LR part of the force.
1259          */
1260         /* MRS -- need to make sure this works with trotter integration -- the constraint calls may not be right.*/
1261         constrain(NULL, FALSE, FALSE, constr, idef, ir, NULL, cr, step, 0, md,
1262                   state->x, f_lr, f_lr, bMolPBC, state->box, state->lambda[efptBONDED], NULL,
1263                   NULL, NULL, nrnb, econqForce, ir->epc == epcMTTK, state->veta, state->veta);
1264     }
1265
1266     /* Add nstcalclr-1 times the LR force to the sum of both forces
1267      * and store the result in forces_lr.
1268      */
1269     nm1 = nstcalclr - 1;
1270     for (i = start; i < nrend; i++)
1271     {
1272         for (d = 0; d < DIM; d++)
1273         {
1274             f_lr[i][d] = f[i][d] + nm1*f_lr[i][d];
1275         }
1276     }
1277 }
1278
1279 void update_tcouple(gmx_int64_t       step,
1280                     t_inputrec       *inputrec,
1281                     t_state          *state,
1282                     gmx_ekindata_t   *ekind,
1283                     t_extmass        *MassQ,
1284                     t_mdatoms        *md)
1285
1286 {
1287     gmx_bool   bTCouple = FALSE;
1288     real       dttc;
1289     int        i, start, end, homenr, offset;
1290
1291     /* if using vv with trotter decomposition methods, we do this elsewhere in the code */
1292     if (inputrec->etc != etcNO &&
1293         !(IR_NVT_TROTTER(inputrec) || IR_NPT_TROTTER(inputrec) || IR_NPH_TROTTER(inputrec)))
1294     {
1295         /* We should only couple after a step where energies were determined (for leapfrog versions)
1296            or the step energies are determined, for velocity verlet versions */
1297
1298         if (EI_VV(inputrec->eI))
1299         {
1300             offset = 0;
1301         }
1302         else
1303         {
1304             offset = 1;
1305         }
1306         bTCouple = (inputrec->nsttcouple == 1 ||
1307                     do_per_step(step+inputrec->nsttcouple-offset,
1308                                 inputrec->nsttcouple));
1309     }
1310
1311     if (bTCouple)
1312     {
1313         dttc = inputrec->nsttcouple*inputrec->delta_t;
1314
1315         switch (inputrec->etc)
1316         {
1317             case etcNO:
1318                 break;
1319             case etcBERENDSEN:
1320                 berendsen_tcoupl(inputrec, ekind, dttc);
1321                 break;
1322             case etcNOSEHOOVER:
1323                 nosehoover_tcoupl(&(inputrec->opts), ekind, dttc,
1324                                   state->nosehoover_xi, state->nosehoover_vxi, MassQ);
1325                 break;
1326             case etcVRESCALE:
1327                 vrescale_tcoupl(inputrec, step, ekind, dttc,
1328                                 state->therm_integral);
1329                 break;
1330         }
1331         /* rescale in place here */
1332         if (EI_VV(inputrec->eI))
1333         {
1334             rescale_velocities(ekind, md, 0, md->homenr, state->v);
1335         }
1336     }
1337     else
1338     {
1339         /* Set the T scaling lambda to 1 to have no scaling */
1340         for (i = 0; (i < inputrec->opts.ngtc); i++)
1341         {
1342             ekind->tcstat[i].lambda = 1.0;
1343         }
1344     }
1345 }
1346
1347 void update_pcouple(FILE             *fplog,
1348                     gmx_int64_t       step,
1349                     t_inputrec       *inputrec,
1350                     t_state          *state,
1351                     matrix            pcoupl_mu,
1352                     matrix            M,
1353                     gmx_bool          bInitStep)
1354 {
1355     gmx_bool   bPCouple = FALSE;
1356     real       dtpc     = 0;
1357     int        i;
1358
1359     /* if using Trotter pressure, we do this in coupling.c, so we leave it false. */
1360     if (inputrec->epc != epcNO && (!(IR_NPT_TROTTER(inputrec) || IR_NPH_TROTTER(inputrec))))
1361     {
1362         /* We should only couple after a step where energies were determined */
1363         bPCouple = (inputrec->nstpcouple == 1 ||
1364                     do_per_step(step+inputrec->nstpcouple-1,
1365                                 inputrec->nstpcouple));
1366     }
1367
1368     clear_mat(pcoupl_mu);
1369     for (i = 0; i < DIM; i++)
1370     {
1371         pcoupl_mu[i][i] = 1.0;
1372     }
1373
1374     clear_mat(M);
1375
1376     if (bPCouple)
1377     {
1378         dtpc = inputrec->nstpcouple*inputrec->delta_t;
1379
1380         switch (inputrec->epc)
1381         {
1382             /* We can always pcoupl, even if we did not sum the energies
1383              * the previous step, since state->pres_prev is only updated
1384              * when the energies have been summed.
1385              */
1386             case (epcNO):
1387                 break;
1388             case (epcBERENDSEN):
1389                 if (!bInitStep)
1390                 {
1391                     berendsen_pcoupl(fplog, step, inputrec, dtpc, state->pres_prev, state->box,
1392                                      pcoupl_mu);
1393                 }
1394                 break;
1395             case (epcPARRINELLORAHMAN):
1396                 parrinellorahman_pcoupl(fplog, step, inputrec, dtpc, state->pres_prev,
1397                                         state->box, state->box_rel, state->boxv,
1398                                         M, pcoupl_mu, bInitStep);
1399                 break;
1400             default:
1401                 break;
1402         }
1403     }
1404 }
1405
1406 static rvec *get_xprime(const t_state *state, gmx_update_t upd)
1407 {
1408     if (state->nalloc > upd->xp_nalloc)
1409     {
1410         upd->xp_nalloc = state->nalloc;
1411         srenew(upd->xp, upd->xp_nalloc);
1412     }
1413
1414     return upd->xp;
1415 }
1416
1417 void update_constraints(FILE             *fplog,
1418                         gmx_int64_t       step,
1419                         real             *dvdlambda, /* the contribution to be added to the bonded interactions */
1420                         t_inputrec       *inputrec,  /* input record and box stuff      */
1421                         gmx_ekindata_t   *ekind,
1422                         t_mdatoms        *md,
1423                         t_state          *state,
1424                         gmx_bool          bMolPBC,
1425                         t_graph          *graph,
1426                         rvec              force[],   /* forces on home particles */
1427                         t_idef           *idef,
1428                         tensor            vir_part,
1429                         t_commrec        *cr,
1430                         t_nrnb           *nrnb,
1431                         gmx_wallcycle_t   wcycle,
1432                         gmx_update_t      upd,
1433                         gmx_constr_t      constr,
1434                         gmx_bool          bFirstHalf,
1435                         gmx_bool          bCalcVir,
1436                         real              vetanew)
1437 {
1438     gmx_bool             bExtended, bLastStep, bLog = FALSE, bEner = FALSE, bDoConstr = FALSE;
1439     double               dt;
1440     real                 dt_1;
1441     int                  start, homenr, nrend, i, n, m, g, d;
1442     tensor               vir_con;
1443     rvec                *vbuf, *xprime = NULL;
1444     int                  nth, th;
1445
1446     if (constr)
1447     {
1448         bDoConstr = TRUE;
1449     }
1450     if (bFirstHalf && !EI_VV(inputrec->eI))
1451     {
1452         bDoConstr = FALSE;
1453     }
1454
1455     /* for now, SD update is here -- though it really seems like it
1456        should be reformulated as a velocity verlet method, since it has two parts */
1457
1458     start  = 0;
1459     homenr = md->homenr;
1460     nrend  = start+homenr;
1461
1462     dt   = inputrec->delta_t;
1463     dt_1 = 1.0/dt;
1464
1465     /*
1466      *  Steps (7C, 8C)
1467      *  APPLY CONSTRAINTS:
1468      *  BLOCK SHAKE
1469
1470      * When doing PR pressure coupling we have to constrain the
1471      * bonds in each iteration. If we are only using Nose-Hoover tcoupling
1472      * it is enough to do this once though, since the relative velocities
1473      * after this will be normal to the bond vector
1474      */
1475
1476     if (bDoConstr)
1477     {
1478         /* clear out constraints before applying */
1479         clear_mat(vir_part);
1480
1481         xprime = get_xprime(state, upd);
1482
1483         bLastStep = (step == inputrec->init_step+inputrec->nsteps);
1484         bLog      = (do_per_step(step, inputrec->nstlog) || bLastStep || (step < 0));
1485         bEner     = (do_per_step(step, inputrec->nstenergy) || bLastStep);
1486         /* Constrain the coordinates xprime */
1487         wallcycle_start(wcycle, ewcCONSTR);
1488         if (EI_VV(inputrec->eI) && bFirstHalf)
1489         {
1490             constrain(NULL, bLog, bEner, constr, idef,
1491                       inputrec, ekind, cr, step, 1, md,
1492                       state->x, state->v, state->v,
1493                       bMolPBC, state->box,
1494                       state->lambda[efptBONDED], dvdlambda,
1495                       NULL, bCalcVir ? &vir_con : NULL, nrnb, econqVeloc,
1496                       inputrec->epc == epcMTTK, state->veta, vetanew);
1497         }
1498         else
1499         {
1500             constrain(NULL, bLog, bEner, constr, idef,
1501                       inputrec, ekind, cr, step, 1, md,
1502                       state->x, xprime, NULL,
1503                       bMolPBC, state->box,
1504                       state->lambda[efptBONDED], dvdlambda,
1505                       state->v, bCalcVir ? &vir_con : NULL, nrnb, econqCoord,
1506                       inputrec->epc == epcMTTK, state->veta, state->veta);
1507         }
1508         wallcycle_stop(wcycle, ewcCONSTR);
1509
1510         where();
1511
1512         dump_it_all(fplog, "After Shake",
1513                     state->natoms, state->x, xprime, state->v, force);
1514
1515         if (bCalcVir)
1516         {
1517             if (inputrec->eI == eiSD2)
1518             {
1519                 /* A correction factor eph is needed for the SD constraint force */
1520                 /* Here we can, unfortunately, not have proper corrections
1521                  * for different friction constants, so we use the first one.
1522                  */
1523                 for (i = 0; i < DIM; i++)
1524                 {
1525                     for (m = 0; m < DIM; m++)
1526                     {
1527                         vir_part[i][m] += upd->sd->sdc[0].eph*vir_con[i][m];
1528                     }
1529                 }
1530             }
1531             else
1532             {
1533                 m_add(vir_part, vir_con, vir_part);
1534             }
1535             if (debug)
1536             {
1537                 pr_rvecs(debug, 0, "constraint virial", vir_part, DIM);
1538             }
1539         }
1540     }
1541
1542     where();
1543     if ((inputrec->eI == eiSD2) && !(bFirstHalf))
1544     {
1545         xprime = get_xprime(state, upd);
1546
1547         nth = gmx_omp_nthreads_get(emntUpdate);
1548
1549 #pragma omp parallel for num_threads(nth) schedule(static)
1550         for (th = 0; th < nth; th++)
1551         {
1552             int start_th, end_th;
1553
1554             start_th = start + ((nrend-start)* th   )/nth;
1555             end_th   = start + ((nrend-start)*(th+1))/nth;
1556
1557             /* The second part of the SD integration */
1558             do_update_sd2(upd->sd,
1559                           FALSE, start_th, end_th,
1560                           inputrec->opts.acc, inputrec->opts.nFreeze,
1561                           md->invmass, md->ptype,
1562                           md->cFREEZE, md->cACC, md->cTC,
1563                           state->x, xprime, state->v, force, state->sd_X,
1564                           inputrec->opts.tau_t,
1565                           FALSE, step, inputrec->ld_seed,
1566                           DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
1567         }
1568         inc_nrnb(nrnb, eNR_UPDATE, homenr);
1569
1570         if (bDoConstr)
1571         {
1572             /* Constrain the coordinates xprime */
1573             wallcycle_start(wcycle, ewcCONSTR);
1574             constrain(NULL, bLog, bEner, constr, idef,
1575                       inputrec, NULL, cr, step, 1, md,
1576                       state->x, xprime, NULL,
1577                       bMolPBC, state->box,
1578                       state->lambda[efptBONDED], dvdlambda,
1579                       NULL, NULL, nrnb, econqCoord, FALSE, 0, 0);
1580             wallcycle_stop(wcycle, ewcCONSTR);
1581         }
1582     }
1583
1584     /* We must always unshift after updating coordinates; if we did not shake
1585        x was shifted in do_force */
1586
1587     if (!(bFirstHalf)) /* in the first half of vv, no shift. */
1588     {
1589         if (graph && (graph->nnodes > 0))
1590         {
1591             unshift_x(graph, state->box, state->x, upd->xp);
1592             if (TRICLINIC(state->box))
1593             {
1594                 inc_nrnb(nrnb, eNR_SHIFTX, 2*graph->nnodes);
1595             }
1596             else
1597             {
1598                 inc_nrnb(nrnb, eNR_SHIFTX, graph->nnodes);
1599             }
1600         }
1601         else
1602         {
1603 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntUpdate)) schedule(static)
1604             for (i = start; i < nrend; i++)
1605             {
1606                 copy_rvec(upd->xp[i], state->x[i]);
1607             }
1608         }
1609
1610         dump_it_all(fplog, "After unshift",
1611                     state->natoms, state->x, upd->xp, state->v, force);
1612     }
1613 /* ############# END the update of velocities and positions ######### */
1614 }
1615
1616 void update_box(FILE             *fplog,
1617                 gmx_int64_t       step,
1618                 t_inputrec       *inputrec,  /* input record and box stuff      */
1619                 t_mdatoms        *md,
1620                 t_state          *state,
1621                 rvec              force[],   /* forces on home particles */
1622                 matrix           *scale_tot,
1623                 matrix            pcoupl_mu,
1624                 t_nrnb           *nrnb,
1625                 gmx_update_t      upd)
1626 {
1627     gmx_bool             bExtended, bLastStep, bLog = FALSE, bEner = FALSE;
1628     double               dt;
1629     real                 dt_1;
1630     int                  start, homenr, nrend, i, n, m, g;
1631     tensor               vir_con;
1632
1633     start  = 0;
1634     homenr = md->homenr;
1635     nrend  = start+homenr;
1636
1637     bExtended =
1638         (inputrec->etc == etcNOSEHOOVER) ||
1639         (inputrec->epc == epcPARRINELLORAHMAN) ||
1640         (inputrec->epc == epcMTTK);
1641
1642     dt = inputrec->delta_t;
1643
1644     where();
1645
1646     /* now update boxes */
1647     switch (inputrec->epc)
1648     {
1649         case (epcNO):
1650             break;
1651         case (epcBERENDSEN):
1652             berendsen_pscale(inputrec, pcoupl_mu, state->box, state->box_rel,
1653                              start, homenr, state->x, md->cFREEZE, nrnb);
1654             break;
1655         case (epcPARRINELLORAHMAN):
1656             /* The box velocities were updated in do_pr_pcoupl in the update
1657              * iteration, but we dont change the box vectors until we get here
1658              * since we need to be able to shift/unshift above.
1659              */
1660             for (i = 0; i < DIM; i++)
1661             {
1662                 for (m = 0; m <= i; m++)
1663                 {
1664                     state->box[i][m] += dt*state->boxv[i][m];
1665                 }
1666             }
1667             preserve_box_shape(inputrec, state->box_rel, state->box);
1668
1669             /* Scale the coordinates */
1670             for (n = start; (n < start+homenr); n++)
1671             {
1672                 tmvmul_ur0(pcoupl_mu, state->x[n], state->x[n]);
1673             }
1674             break;
1675         case (epcMTTK):
1676             switch (inputrec->epct)
1677             {
1678                 case (epctISOTROPIC):
1679                     /* DIM * eta = ln V.  so DIM*eta_new = DIM*eta_old + DIM*dt*veta =>
1680                        ln V_new = ln V_old + 3*dt*veta => V_new = V_old*exp(3*dt*veta) =>
1681                        Side length scales as exp(veta*dt) */
1682
1683                     msmul(state->box, exp(state->veta*dt), state->box);
1684
1685                     /* Relate veta to boxv.  veta = d(eta)/dT = (1/DIM)*1/V dV/dT.
1686                        o               If we assume isotropic scaling, and box length scaling
1687                        factor L, then V = L^DIM (det(M)).  So dV/dt = DIM
1688                        L^(DIM-1) dL/dt det(M), and veta = (1/L) dL/dt.  The
1689                        determinant of B is L^DIM det(M), and the determinant
1690                        of dB/dt is (dL/dT)^DIM det (M).  veta will be
1691                        (det(dB/dT)/det(B))^(1/3).  Then since M =
1692                        B_new*(vol_new)^(1/3), dB/dT_new = (veta_new)*B(new). */
1693
1694                     msmul(state->box, state->veta, state->boxv);
1695                     break;
1696                 default:
1697                     break;
1698             }
1699             break;
1700         default:
1701             break;
1702     }
1703
1704     if ((!(IR_NPT_TROTTER(inputrec) || IR_NPH_TROTTER(inputrec))) && scale_tot)
1705     {
1706         /* The transposes of the scaling matrices are stored,
1707          * therefore we need to reverse the order in the multiplication.
1708          */
1709         mmul_ur0(*scale_tot, pcoupl_mu, *scale_tot);
1710     }
1711
1712     if (DEFORM(*inputrec))
1713     {
1714         deform(upd, start, homenr, state->x, state->box, scale_tot, inputrec, step);
1715     }
1716     where();
1717     dump_it_all(fplog, "After update",
1718                 state->natoms, state->x, upd->xp, state->v, force);
1719 }
1720
1721 void update_coords(FILE             *fplog,
1722                    gmx_int64_t       step,
1723                    t_inputrec       *inputrec,  /* input record and box stuff   */
1724                    t_mdatoms        *md,
1725                    t_state          *state,
1726                    gmx_bool          bMolPBC,
1727                    rvec             *f,    /* forces on home particles */
1728                    gmx_bool          bDoLR,
1729                    rvec             *f_lr,
1730                    t_fcdata         *fcd,
1731                    gmx_ekindata_t   *ekind,
1732                    matrix            M,
1733                    gmx_update_t      upd,
1734                    gmx_bool          bInitStep,
1735                    int               UpdatePart,
1736                    t_commrec        *cr, /* these shouldn't be here -- need to think about it */
1737                    t_nrnb           *nrnb,
1738                    gmx_constr_t      constr,
1739                    t_idef           *idef)
1740 {
1741     gmx_bool          bNH, bPR, bLastStep, bLog = FALSE, bEner = FALSE;
1742     double            dt, alpha;
1743     real             *imass, *imassin;
1744     rvec             *force;
1745     real              dt_1;
1746     int               start, homenr, nrend, i, j, d, n, m, g;
1747     int               blen0, blen1, iatom, jatom, nshake, nsettle, nconstr, nexpand;
1748     int              *icom = NULL;
1749     tensor            vir_con;
1750     rvec             *vcom, *xcom, *vall, *xall, *xin, *vin, *forcein, *fall, *xpall, *xprimein, *xprime;
1751     int               nth, th;
1752
1753     /* Running the velocity half does nothing except for velocity verlet */
1754     if ((UpdatePart == etrtVELOCITY1 || UpdatePart == etrtVELOCITY2) &&
1755         !EI_VV(inputrec->eI))
1756     {
1757         gmx_incons("update_coords called for velocity without VV integrator");
1758     }
1759
1760     start  = 0;
1761     homenr = md->homenr;
1762     nrend  = start+homenr;
1763
1764     xprime = get_xprime(state, upd);
1765
1766     dt   = inputrec->delta_t;
1767     dt_1 = 1.0/dt;
1768
1769     /* We need to update the NMR restraint history when time averaging is used */
1770     if (state->flags & (1<<estDISRE_RM3TAV))
1771     {
1772         update_disres_history(fcd, &state->hist);
1773     }
1774     if (state->flags & (1<<estORIRE_DTAV))
1775     {
1776         update_orires_history(fcd, &state->hist);
1777     }
1778
1779
1780     bNH = inputrec->etc == etcNOSEHOOVER;
1781     bPR = ((inputrec->epc == epcPARRINELLORAHMAN) || (inputrec->epc == epcMTTK));
1782
1783     if (bDoLR && inputrec->nstcalclr > 1 && !EI_VV(inputrec->eI))  /* get this working with VV? */
1784     {
1785         /* Store the total force + nstcalclr-1 times the LR force
1786          * in forces_lr, so it can be used in a normal update algorithm
1787          * to produce twin time stepping.
1788          */
1789         /* is this correct in the new construction? MRS */
1790         combine_forces(inputrec->nstcalclr, constr, inputrec, md, idef, cr,
1791                        step, state, bMolPBC,
1792                        start, nrend, f, f_lr, nrnb);
1793         force = f_lr;
1794     }
1795     else
1796     {
1797         force = f;
1798     }
1799
1800     /* ############# START The update of velocities and positions ######### */
1801     where();
1802     dump_it_all(fplog, "Before update",
1803                 state->natoms, state->x, xprime, state->v, force);
1804
1805     if (inputrec->eI == eiSD2)
1806     {
1807         check_sd2_work_data_allocation(upd->sd, nrend);
1808
1809         do_update_sd2_Tconsts(upd->sd,
1810                               inputrec->opts.ngtc,
1811                               inputrec->opts.tau_t,
1812                               inputrec->opts.ref_t);
1813     }
1814     if (inputrec->eI == eiBD)
1815     {
1816         do_update_bd_Tconsts(dt, inputrec->bd_fric,
1817                              inputrec->opts.ngtc, inputrec->opts.ref_t,
1818                              upd->sd->bd_rf);
1819     }
1820
1821     nth = gmx_omp_nthreads_get(emntUpdate);
1822
1823 #pragma omp parallel for num_threads(nth) schedule(static) private(alpha)
1824     for (th = 0; th < nth; th++)
1825     {
1826         int start_th, end_th;
1827
1828         start_th = start + ((nrend-start)* th   )/nth;
1829         end_th   = start + ((nrend-start)*(th+1))/nth;
1830
1831         switch (inputrec->eI)
1832         {
1833             case (eiMD):
1834                 if (ekind->cosacc.cos_accel == 0)
1835                 {
1836                     do_update_md(start_th, end_th, dt,
1837                                  ekind->tcstat, state->nosehoover_vxi,
1838                                  ekind->bNEMD, ekind->grpstat, inputrec->opts.acc,
1839                                  inputrec->opts.nFreeze,
1840                                  md->invmass, md->ptype,
1841                                  md->cFREEZE, md->cACC, md->cTC,
1842                                  state->x, xprime, state->v, force, M,
1843                                  bNH, bPR);
1844                 }
1845                 else
1846                 {
1847                     do_update_visc(start_th, end_th, dt,
1848                                    ekind->tcstat, state->nosehoover_vxi,
1849                                    md->invmass, md->ptype,
1850                                    md->cTC, state->x, xprime, state->v, force, M,
1851                                    state->box,
1852                                    ekind->cosacc.cos_accel,
1853                                    ekind->cosacc.vcos,
1854                                    bNH, bPR);
1855                 }
1856                 break;
1857             case (eiSD1):
1858                 do_update_sd1(upd->sd,
1859                               start_th, end_th, dt,
1860                               inputrec->opts.acc, inputrec->opts.nFreeze,
1861                               md->invmass, md->ptype,
1862                               md->cFREEZE, md->cACC, md->cTC,
1863                               state->x, xprime, state->v, force,
1864                               inputrec->opts.ngtc, inputrec->opts.tau_t, inputrec->opts.ref_t,
1865                               step, inputrec->ld_seed, DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
1866                 break;
1867             case (eiSD2):
1868                 /* The SD update is done in 2 parts, because an extra constraint step
1869                  * is needed
1870                  */
1871                 do_update_sd2(upd->sd,
1872                               bInitStep, start_th, end_th,
1873                               inputrec->opts.acc, inputrec->opts.nFreeze,
1874                               md->invmass, md->ptype,
1875                               md->cFREEZE, md->cACC, md->cTC,
1876                               state->x, xprime, state->v, force, state->sd_X,
1877                               inputrec->opts.tau_t,
1878                               TRUE, step, inputrec->ld_seed,
1879                               DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
1880                 break;
1881             case (eiBD):
1882                 do_update_bd(start_th, end_th, dt,
1883                              inputrec->opts.nFreeze, md->invmass, md->ptype,
1884                              md->cFREEZE, md->cTC,
1885                              state->x, xprime, state->v, force,
1886                              inputrec->bd_fric,
1887                              upd->sd->bd_rf,
1888                              step, inputrec->ld_seed, DOMAINDECOMP(cr) ? cr->dd->gatindex : NULL);
1889                 break;
1890             case (eiVV):
1891             case (eiVVAK):
1892                 alpha = 1.0 + DIM/((double)inputrec->opts.nrdf[0]); /* assuming barostat coupled to group 0. */
1893                 switch (UpdatePart)
1894                 {
1895                     case etrtVELOCITY1:
1896                     case etrtVELOCITY2:
1897                         do_update_vv_vel(start_th, end_th, dt,
1898                                          inputrec->opts.acc, inputrec->opts.nFreeze,
1899                                          md->invmass, md->ptype,
1900                                          md->cFREEZE, md->cACC,
1901                                          state->v, force,
1902                                          (bNH || bPR), state->veta, alpha);
1903                         break;
1904                     case etrtPOSITION:
1905                         do_update_vv_pos(start_th, end_th, dt,
1906                                          inputrec->opts.nFreeze,
1907                                          md->ptype, md->cFREEZE,
1908                                          state->x, xprime, state->v,
1909                                          (bNH || bPR), state->veta);
1910                         break;
1911                 }
1912                 break;
1913             default:
1914                 gmx_fatal(FARGS, "Don't know how to update coordinates");
1915                 break;
1916         }
1917     }
1918
1919 }
1920
1921
1922 void correct_ekin(FILE *log, int start, int end, rvec v[], rvec vcm, real mass[],
1923                   real tmass, tensor ekin)
1924 {
1925     /*
1926      * This is a debugging routine. It should not be called for production code
1927      *
1928      * The kinetic energy should calculated according to:
1929      *   Ekin = 1/2 m (v-vcm)^2
1930      * However the correction is not always applied, since vcm may not be
1931      * known in time and we compute
1932      *   Ekin' = 1/2 m v^2 instead
1933      * This can be corrected afterwards by computing
1934      *   Ekin = Ekin' + 1/2 m ( -2 v vcm + vcm^2)
1935      * or in hsorthand:
1936      *   Ekin = Ekin' - m v vcm + 1/2 m vcm^2
1937      */
1938     int    i, j, k;
1939     real   m, tm;
1940     rvec   hvcm, mv;
1941     tensor dekin;
1942
1943     /* Local particles */
1944     clear_rvec(mv);
1945
1946     /* Processor dependent part. */
1947     tm = 0;
1948     for (i = start; (i < end); i++)
1949     {
1950         m      = mass[i];
1951         tm    += m;
1952         for (j = 0; (j < DIM); j++)
1953         {
1954             mv[j] += m*v[i][j];
1955         }
1956     }
1957     /* Shortcut */
1958     svmul(1/tmass, vcm, vcm);
1959     svmul(0.5, vcm, hvcm);
1960     clear_mat(dekin);
1961     for (j = 0; (j < DIM); j++)
1962     {
1963         for (k = 0; (k < DIM); k++)
1964         {
1965             dekin[j][k] += vcm[k]*(tm*hvcm[j]-mv[j]);
1966         }
1967     }
1968     pr_rvecs(log, 0, "dekin", dekin, DIM);
1969     pr_rvecs(log, 0, " ekin", ekin, DIM);
1970     fprintf(log, "dekin = %g, ekin = %g  vcm = (%8.4f %8.4f %8.4f)\n",
1971             trace(dekin), trace(ekin), vcm[XX], vcm[YY], vcm[ZZ]);
1972     fprintf(log, "mv = (%8.4f %8.4f %8.4f)\n",
1973             mv[XX], mv[YY], mv[ZZ]);
1974 }
1975
1976 extern gmx_bool update_randomize_velocities(t_inputrec *ir, gmx_int64_t step, const t_commrec *cr,
1977                                             t_mdatoms *md, t_state *state, gmx_update_t upd, gmx_constr_t constr)
1978 {
1979
1980     int  i;
1981     real rate = (ir->delta_t)/ir->opts.tau_t[0];
1982
1983     if (ir->etc == etcANDERSEN && constr != NULL)
1984     {
1985         gmx_fatal(FARGS, "Normal Andersen is currently not supported with constraints, use massive Andersen instead");
1986     }
1987
1988     /* proceed with andersen if 1) it's fixed probability per
1989        particle andersen or 2) it's massive andersen and it's tau_t/dt */
1990     if ((ir->etc == etcANDERSEN) || do_per_step(step, (int)(1.0/rate)))
1991     {
1992         andersen_tcoupl(ir, step, cr, md, state, rate,
1993                         upd->sd->randomize_group, upd->sd->boltzfac);
1994         return TRUE;
1995     }
1996     return FALSE;
1997 }