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