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