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