Merge branch release-5-0
[alexxy/gromacs.git] / src / gromacs / mdlib / coupling.cpp
similarity index 95%
rename from src/gromacs/mdlib/coupling.c
rename to src/gromacs/mdlib/coupling.cpp
index 2025ec9193c33f88830be94125ec749c27689957..d0169dcdc28d4af97d65db9e4a2e5d454520ac33 100644 (file)
  * To help us fund GROMACS development, we humbly ask that you cite
  * the research papers on the package. Check out http://www.gromacs.org.
  */
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "gmxpre.h"
+
 #include <assert.h>
 
-#include "typedefs.h"
-#include "types/commrec.h"
-#include "gromacs/utility/smalloc.h"
-#include "update.h"
-#include "vec.h"
-#include "macros.h"
-#include "physics.h"
-#include "names.h"
-#include "gmx_fatal.h"
-#include "txtdump.h"
-#include "nrnb.h"
+#include <algorithm>
+
+#include "gromacs/legacyheaders/gmx_omp_nthreads.h"
+#include "gromacs/legacyheaders/macros.h"
+#include "gromacs/legacyheaders/mdrun.h"
+#include "gromacs/legacyheaders/names.h"
+#include "gromacs/legacyheaders/nrnb.h"
+#include "gromacs/legacyheaders/txtdump.h"
+#include "gromacs/legacyheaders/typedefs.h"
+#include "gromacs/legacyheaders/update.h"
+#include "gromacs/legacyheaders/types/commrec.h"
+#include "gromacs/math/units.h"
+#include "gromacs/math/vec.h"
 #include "gromacs/random/random.h"
-#include "update.h"
-#include "mdrun.h"
+#include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/smalloc.h"
 
 #define NTROTTERPARTS 3
 
@@ -94,7 +95,7 @@ static void NHC_trotter(t_grpopts *opts, int nvar, gmx_ekindata_t *ekind, real d
 {
     /* general routine for both barostat and thermostat nose hoover chains */
 
-    int           i, j, mi, mj, jmax;
+    int           i, j, mi, mj;
     double        Ekin, Efac, reft, kT, nd;
     double        dt;
     t_grp_tcstat *tcstat;
@@ -129,7 +130,7 @@ static void NHC_trotter(t_grpopts *opts, int nvar, gmx_ekindata_t *ekind, real d
         {
             iQinv = &(MassQ->QPinv[i*nh]);
             nd    = 1.0; /* THIS WILL CHANGE IF NOT ISOTROPIC */
-            reft  = max(0.0, opts->ref_t[0]);
+            reft  = std::max<real>(0, opts->ref_t[0]);
             Ekin  = sqr(*veta)/MassQ->Winv;
         }
         else
@@ -137,7 +138,7 @@ static void NHC_trotter(t_grpopts *opts, int nvar, gmx_ekindata_t *ekind, real d
             iQinv  = &(MassQ->Qinv[i*nh]);
             tcstat = &ekind->tcstat[i];
             nd     = opts->nrdf[i];
-            reft   = max(0.0, opts->ref_t[i]);
+            reft   = std::max<real>(0, opts->ref_t[i]);
             if (bEkinAveVel)
             {
                 Ekin = 2*trace(tcstat->ekinf)*tcstat->ekinscalef_nhc;
@@ -229,9 +230,9 @@ static void boxv_trotter(t_inputrec *ir, real *veta, real dt, tensor box,
 
     real   pscal;
     double alpha;
-    int    i, j, d, n, nwall;
-    real   T, GW, vol;
-    tensor Winvm, ekinmod, localpres;
+    int    nwall;
+    real   GW, vol;
+    tensor ekinmod, localpres;
 
     /* The heat bath is coupled to a separate barostat, the last temperature group.  In the
        2006 Tuckerman et al paper., the order is iL_{T_baro} iL {T_part}
@@ -375,8 +376,8 @@ void parrinellorahman_pcoupl(FILE *fplog, gmx_int64_t step,
          * The pressure and compressibility always occur as a product,
          * therefore the pressure unit drops out.
          */
-        maxl = max(box[XX][XX], box[YY][YY]);
-        maxl = max(maxl, box[ZZ][ZZ]);
+        maxl = std::max(box[XX][XX], box[YY][YY]);
+        maxl = std::max(maxl, box[ZZ][ZZ]);
         for (d = 0; d < DIM; d++)
         {
             for (n = 0; n < DIM; n++)
@@ -533,7 +534,7 @@ void berendsen_pcoupl(FILE *fplog, gmx_int64_t step,
 {
     int     d, n;
     real    scalar_pressure, xy_pressure, p_corr_z;
-    char   *ptr, buf[STRLEN];
+    char    buf[STRLEN];
 
     /*
      *  Calculate the scaling matrix mu
@@ -646,12 +647,25 @@ void berendsen_pscale(t_inputrec *ir, matrix mu,
                       t_nrnb *nrnb)
 {
     ivec   *nFreeze = ir->opts.nFreeze;
-    int     n, d, g = 0;
+    int     n, d;
+    int     nthreads gmx_unused;
+
+#ifndef __clang_analyzer__
+    // cppcheck-suppress unreadVariable
+    nthreads = gmx_omp_nthreads_get(emntUpdate);
+#endif
 
     /* Scale the positions */
+#pragma omp parallel for num_threads(nthreads) schedule(static)
     for (n = start; n < start+nr_atoms; n++)
     {
-        if (cFREEZE)
+        int g;
+
+        if (cFREEZE == NULL)
+        {
+            g = 0;
+        }
+        else
         {
             g = cFREEZE[n];
         }
@@ -706,9 +720,9 @@ void berendsen_tcoupl(t_inputrec *ir, gmx_ekindata_t *ekind, real dt)
 
         if ((opts->tau_t[i] > 0) && (T > 0.0))
         {
-            reft                    = max(0.0, opts->ref_t[i]);
+            reft                    = std::max<real>(0, opts->ref_t[i]);
             lll                     = sqrt(1.0 + (dt/opts->tau_t[i])*(reft/T-1.0));
-            ekind->tcstat[i].lambda = max(min(lll, 1.25), 0.8);
+            ekind->tcstat[i].lambda = std::max<real>(std::min<real>(lll, 1.25), 0.8);
         }
         else
         {
@@ -783,7 +797,7 @@ void nosehoover_tcoupl(t_grpopts *opts, gmx_ekindata_t *ekind, real dt,
 
     for (i = 0; (i < opts->ngtc); i++)
     {
-        reft     = max(0.0, opts->ref_t[i]);
+        reft     = std::max<real>(0, opts->ref_t[i]);
         oldvxi   = vxi[i];
         vxi[i]  += dt*MassQ->Qinv[i]*(ekind->tcstat[i].Th - reft);
         xi[i]   += dt*(oldvxi + vxi[i])*0.5;
@@ -822,16 +836,14 @@ void trotter_update(t_inputrec *ir, gmx_int64_t step, gmx_ekindata_t *ekind,
                     t_extmass *MassQ, int **trotter_seqlist, int trotter_seqno)
 {
 
-    int             n, i, j, d, ntgrp, ngtc, gc = 0;
+    int             n, i, d, ngtc, gc = 0;
     t_grp_tcstat   *tcstat;
     t_grpopts      *opts;
     gmx_int64_t     step_eff;
-    real            ecorr, pcorr, dvdlcorr;
-    real            bmass, qmass, reft, kT, dt, nd;
-    tensor          dumpres, dumvir;
+    real            dt;
     double         *scalefac, dtc;
     int            *trotter_seq;
-    rvec            sumv = {0, 0, 0}, consk;
+    rvec            sumv = {0, 0, 0};
     gmx_bool        bCouple;
 
     if (trotter_seqno <= ettTSEQ2)
@@ -953,16 +965,12 @@ void trotter_update(t_inputrec *ir, gmx_int64_t step, gmx_ekindata_t *ekind,
 
 extern void init_npt_masses(t_inputrec *ir, t_state *state, t_extmass *MassQ, gmx_bool bInit)
 {
-    int           n, i, j, d, ntgrp, ngtc, nnhpres, nh, gc = 0;
-    t_grp_tcstat *tcstat;
+    int           n, i, j, d, ngtc, nh;
     t_grpopts    *opts;
-    real          ecorr, pcorr, dvdlcorr;
-    real          bmass, qmass, reft, kT, dt, ndj, nd;
-    tensor        dumpres, dumvir;
+    real          reft, kT, ndj, nd;
 
     opts    = &(ir->opts); /* just for ease of referencing */
     ngtc    = ir->opts.ngtc;
-    nnhpres = state->nnhpres;
     nh      = state->nhchainlength;
 
     if (ir->eI == eiMD)
@@ -1023,7 +1031,7 @@ extern void init_npt_masses(t_inputrec *ir, t_state *state, t_extmass *MassQ, gm
         {
             if ((opts->tau_t[i] > 0) && (opts->ref_t[i] > 0))
             {
-                reft = max(0.0, opts->ref_t[i]);
+                reft = std::max<real>(0, opts->ref_t[i]);
                 nd   = opts->nrdf[i];
                 kT   = BOLTZ*reft;
                 for (j = 0; j < nh; j++)
@@ -1041,7 +1049,6 @@ extern void init_npt_masses(t_inputrec *ir, t_state *state, t_extmass *MassQ, gm
             }
             else
             {
-                reft = 0.0;
                 for (j = 0; j < nh; j++)
                 {
                     MassQ->Qinv[i*nh+j] = 0.0;
@@ -1053,16 +1060,12 @@ extern void init_npt_masses(t_inputrec *ir, t_state *state, t_extmass *MassQ, gm
 
 int **init_npt_vars(t_inputrec *ir, t_state *state, t_extmass *MassQ, gmx_bool bTrotter)
 {
-    int           n, i, j, d, ntgrp, ngtc, nnhpres, nh, gc = 0;
-    t_grp_tcstat *tcstat;
+    int           i, j, nnhpres, nh;
     t_grpopts    *opts;
-    real          ecorr, pcorr, dvdlcorr;
-    real          bmass, qmass, reft, kT, dt, ndj, nd;
-    tensor        dumpres, dumvir;
+    real          bmass, qmass, reft, kT;
     int         **trotter_seq;
 
     opts    = &(ir->opts); /* just for ease of referencing */
-    ngtc    = state->ngtc;
     nnhpres = state->nnhpres;
     nh      = state->nhchainlength;
 
@@ -1207,7 +1210,7 @@ int **init_npt_vars(t_inputrec *ir, t_state *state, t_extmass *MassQ, gmx_bool b
     /* barostat temperature */
     if ((ir->tau_p > 0) && (opts->ref_t[0] > 0))
     {
-        reft = max(0.0, opts->ref_t[0]);
+        reft = std::max<real>(0, opts->ref_t[0]);
         kT   = BOLTZ*reft;
         for (i = 0; i < nnhpres; i++)
         {
@@ -1240,11 +1243,12 @@ int **init_npt_vars(t_inputrec *ir, t_state *state, t_extmass *MassQ, gmx_bool b
 
 real NPT_energy(t_inputrec *ir, t_state *state, t_extmass *MassQ)
 {
-    int     i, j, nd, ndj, bmass, qmass, ngtcall;
-    real    ener_npt, reft, eta, kT, tau;
+    int     i, j;
+    real    nd, ndj;
+    real    ener_npt, reft, kT;
     double *ivxi, *ixi;
     double *iQinv;
-    real    vol, dbaro, W, Q;
+    real    vol;
     int     nh = state->nhchainlength;
 
     ener_npt = 0;
@@ -1292,7 +1296,7 @@ real NPT_energy(t_inputrec *ir, t_state *state, t_extmass *MassQ)
             ivxi  = &state->nhpres_vxi[i*nh];
             ixi   = &state->nhpres_xi[i*nh];
             iQinv = &(MassQ->QPinv[i*nh]);
-            reft  = max(ir->opts.ref_t[0], 0); /* using 'System' temperature */
+            reft  = std::max<real>(ir->opts.ref_t[0], 0.0); /* using 'System' temperature */
             kT    = BOLTZ * reft;
 
             for (j = 0; j < nh; j++)
@@ -1320,10 +1324,10 @@ real NPT_energy(t_inputrec *ir, t_state *state, t_extmass *MassQ)
             iQinv = &(MassQ->Qinv[i*nh]);
 
             nd   = ir->opts.nrdf[i];
-            reft = max(ir->opts.ref_t[i], 0);
+            reft = std::max<real>(ir->opts.ref_t[i], 0);
             kT   = BOLTZ * reft;
 
-            if (nd > 0)
+            if (nd > 0.0)
             {
                 if (IR_NVT_TROTTER(ir))
                 {
@@ -1340,7 +1344,7 @@ real NPT_energy(t_inputrec *ir, t_state *state, t_extmass *MassQ)
                             }
                             else
                             {
-                                ndj = 1;
+                                ndj = 1.0;
                             }
                             ener_npt += ndj*ixi[j]*kT;
                         }
@@ -1637,7 +1641,7 @@ void update_annealing_target_temp(t_grpopts *opts, real t)
             case  eannPERIODIC:
                 /* calculate time modulo the period */
                 pert  = opts->anneal_time[i][npoints-1];
-                n     = t / pert;
+                n     = static_cast<int>(t / pert);
                 thist = t - n*pert; /* modulo time */
                 /* Make sure rounding didn't get us outside the interval */
                 if (fabs(thist-pert) < GMX_REAL_EPS*100)