Removed init_state
authorMark Abraham <mark.j.abraham@gmail.com>
Mon, 7 Nov 2016 15:59:59 +0000 (16:59 +0100)
committerMark Abraham <mark.j.abraham@gmail.com>
Sat, 21 Jan 2017 21:24:11 +0000 (22:24 +0100)
Made a simple zero-initializing constructor for t_state and the
structs of some of its members. Called them classes. Later, we might
prefer to require explicit initialization with actual values, so that
tools can detect the use of uninitialized values and find our bugs,
but for now having a constructor is a useful initial step in that
direction.

Extracted some new functions that cover some of the incidental
functionality that was also present in init_state.

Made state.lambda a std::array, thereby removing the need to consider
resizing it, and converted client code to be passed an ArrayRef rather
than hard-code the name of the specific container. This caters for
convenient future refactoring of the underlying storage, and sometimes
needing to implicitly know what the size of the container is.

Passing an ArrayRef by value is consistent with the CppCoreGuidelines,
but has potential for performance impact. Doing this means that a
caller pushes onto the stack a copy of the object (containing two
pointers), rather than previous idioms such as pointer + size, or
pointer + implicit constant size from an enum, or pointer + implicit
size in some other parameter. This could mean an extra argument is
pushed to the stack for the function call, compared with the
alternatives of pushing a pointer to data, pointer to container, or
pointer to ArrayRef. In all cases, the caller has to load the pointer
value via an offset that is known to the compiler, so that aspect is
probably irrelevant. So, we would probably prefer to avoid calling
functions that take such parameters in a tight loop, or where multiple
containers share a common size. But the uses in this patch seem to be
of sufficiently high level to be an acceptable trade of possible
performance for improved maintainability.

Change-Id: I17e7d83cfc89566f76fa9949c425b950ad6aef62

31 files changed:
src/gromacs/domdec/domdec_topology.cpp
src/gromacs/fileio/checkpoint.cpp
src/gromacs/fileio/checkpoint.h
src/gromacs/fileio/tpxio.cpp
src/gromacs/gmxpreprocess/grompp.cpp
src/gromacs/imd/imd.h
src/gromacs/listed-forces/disre.h
src/gromacs/listed-forces/listed-forces.h
src/gromacs/listed-forces/orires.h
src/gromacs/mdlib/broadcaststructs.cpp
src/gromacs/mdlib/constr.h
src/gromacs/mdlib/force.cpp
src/gromacs/mdlib/force.h
src/gromacs/mdlib/md_support.cpp
src/gromacs/mdlib/md_support.h
src/gromacs/mdlib/mdrun.h
src/gromacs/mdlib/minimize.cpp
src/gromacs/mdlib/shellfc.cpp
src/gromacs/mdlib/shellfc.h
src/gromacs/mdlib/sim_util.cpp
src/gromacs/mdlib/sim_util.h
src/gromacs/mdlib/tpi.cpp
src/gromacs/mdlib/update.h
src/gromacs/mdtypes/state.cpp
src/gromacs/mdtypes/state.h
src/gromacs/pbcutil/boxutilities.h
src/gromacs/tools/dump.cpp
src/programs/mdrun/md.cpp
src/programs/mdrun/membed.h
src/programs/mdrun/repl_ex.h
src/programs/mdrun/runner.cpp

index f72368454ac20f3a8e184276cd9e3bfcfb6c5f9e..7ee58b3e002980fd0659cfa37dc66a2c94ed2c2c 100644 (file)
@@ -2273,7 +2273,8 @@ void dd_init_local_state(gmx_domdec_t *dd,
     }
     dd_bcast(dd, NITEM_DD_INIT_LOCAL_STATE*sizeof(int), buf);
 
-    init_state(state_local, 0, buf[1], buf[2], buf[3], buf[4]);
+    init_gtc_state(state_local, buf[1], buf[2], buf[3]);
+    init_dfhist_state(state_local, buf[4]);
     state_local->flags = buf[0];
 }
 
index 23f037fcad995ad3af534818b6c38d73a959ceba..c1194266fa3c83d9fa8bb184591c72e219573429 100644 (file)
@@ -68,6 +68,7 @@
 #include "gromacs/mdtypes/md_enums.h"
 #include "gromacs/mdtypes/state.h"
 #include "gromacs/trajectory/trajectoryframe.h"
+#include "gromacs/utility/arrayref.h"
 #include "gromacs/utility/baseversion.h"
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/fatalerror.h"
@@ -549,7 +550,7 @@ static int doVectorLow(XDR *xd, StatePart part, int ecpt, int sflags,
                     xdr_datatype_names[xdrTypeInTheCode],
                     xdr_datatype_names[xdrTypeInTheFile]);
 
-            /* Matchting int and real should never occur, but check anyhow */
+            /* Matching int and real should never occur, but check anyhow */
             if (xdrTypeInTheFile == xdr_datatype_int ||
                 xdrTypeInTheCode == xdr_datatype_int)
             {
@@ -572,7 +573,7 @@ static int doVectorLow(XDR *xd, StatePart part, int ecpt, int sflags,
             /* This conditional ensures that we don't resize on write.
              * In particular in the state where this code was written
              * PaddedRVecVector has a size of numElemInThefile and we
-             * don't want to loose that padding here.
+             * don't want to lose that padding here.
              */
             if (vector->size() < static_cast<unsigned int>(numElemInTheFile))
             {
@@ -618,7 +619,7 @@ static int doVectorLow(XDR *xd, StatePart part, int ecpt, int sflags,
     return 0;
 }
 
-//! \brief Read/Write an std::vector
+//! \brief Read/Write a std::vector.
 template <typename T>
 static int doVector(XDR *xd, StatePart part, int ecpt, int sflags,
                     std::vector<T> *vector, FILE *list)
@@ -626,36 +627,36 @@ static int doVector(XDR *xd, StatePart part, int ecpt, int sflags,
     return doVectorLow<T>(xd, part, ecpt, sflags, -1, nullptr, nullptr, vector, list, CptElementType::real);
 }
 
-//! \brief Read/Write an std::vector, on read checks the number of elements matches \p numElements
-template <typename T>
-static int doVector(XDR *xd, StatePart part, int ecpt, int sflags,
-                    int numElements, std::vector<T> *vector, FILE *list)
+//! \brief Read/Write an ArrayRef<real>.
+static int doRealArrayRef(XDR *xd, StatePart part, int ecpt, int sflags,
+                          gmx::ArrayRef<real> vector, FILE *list)
 {
-    return doVectorLow<T>(xd, part, ecpt, sflags, numElements, nullptr, nullptr, vector, list, CptElementType::real);
+    real *v_real = vector.data();
+    return doVectorLow<real>(xd, part, ecpt, sflags, vector.size(), nullptr, &v_real, nullptr, list, CptElementType::real);
 }
 
-//! \brief Read/Write a PaddedRVecVector, on read checks the number of elements matches \p numElements
-static int doPaddedVector(XDR *xd, StatePart part, int ecpt, int sflags,
-                          int numElements, PaddedRVecVector *v, FILE *list)
+//! \brief Read/Write a PaddedRVecVector.
+static int doPaddedRvecVector(XDR *xd, StatePart part, int ecpt, int sflags,
+                              gmx::PaddedRVecVector *vector, FILE *list)
 {
-    rvec *v_rvec;
+    real *v_real;
 
     if (list == nullptr && (sflags & (1 << ecpt)))
     {
-        /* We resize the vector here to avoid pointer reallocation in
-         * do_cpte_reals_low. Note the we allocate 1 element extra for SIMD.
-         */
-        v->resize(numElements + 1);
-        v_rvec = as_rvec_array(v->data());
+        v_real = vector->data()->as_vec();
     }
     else
     {
-        v_rvec = nullptr;
+        v_real = nullptr;
     }
+    // The current invariant of a PaddedRVecVector is that its size is
+    // one larger than necessary to store the data. Make sure that we
+    // read/write only the valid data, and don't leak to the outside
+    // world that currently we find it convenient internally to
+    // allocate one extra element.
+    gmx::ArrayRef<real> ref(v_real, v_real + (vector->size()-1) * DIM);
 
-    return doVectorLow<real>(xd, part, ecpt, sflags,
-                             numElements*DIM, nullptr, (real **)(&v_rvec), nullptr,
-                             list, CptElementType::real3);
+    return doRealArrayRef(xd, part, ecpt, sflags, ref, list);
 }
 
 /* This function stores n along with the reals for reading,
@@ -1017,19 +1018,19 @@ static int do_cpt_state(XDR *xd,
                         FILE *list)
 {
     int             ret    = 0;
-
-    const int       nnht   = state->nhchainlength*state->ngtc;
-    const int       nnhtp  = state->nhchainlength*state->nnhpres;
-
     const StatePart part   = StatePart::microState;
     const int       sflags = state->flags;
+    // If reading, state->natoms was probably just read, so
+    // allocations need to be managed. If writing, this won't change
+    // anything that matters.
+    state_change_natoms(state, state->natoms);
     for (int i = 0; (i < estNR && ret == 0); i++)
     {
         if (fflags & (1<<i))
         {
             switch (i)
             {
-                case estLAMBDA:  ret      = doVector<real>(xd, part, i, sflags, static_cast<int>(efptNR), &state->lambda, list); break;
+                case estLAMBDA:  ret      = doRealArrayRef(xd, part, i, sflags, gmx::arrayRefFromArray<real>(state->lambda.data(), state->lambda.size()), list); break;
                 case estFEPSTATE: ret     = do_cpte_int (xd, part, i, sflags, &state->fep_state, list); break;
                 case estBOX:     ret      = do_cpte_matrix(xd, part, i, sflags, state->box, list); break;
                 case estBOX_REL: ret      = do_cpte_matrix(xd, part, i, sflags, state->box_rel, list); break;
@@ -1037,15 +1038,15 @@ static int do_cpt_state(XDR *xd,
                 case estPRES_PREV: ret    = do_cpte_matrix(xd, part, i, sflags, state->pres_prev, list); break;
                 case estSVIR_PREV:  ret   = do_cpte_matrix(xd, part, i, sflags, state->svir_prev, list); break;
                 case estFVIR_PREV:  ret   = do_cpte_matrix(xd, part, i, sflags, state->fvir_prev, list); break;
-                case estNH_XI:   ret      = doVector<double>(xd, part, i, sflags, nnht, &state->nosehoover_xi, list); break;
-                case estNH_VXI:  ret      = doVector<double>(xd, part, i, sflags, nnht, &state->nosehoover_vxi, list); break;
-                case estNHPRES_XI:   ret  = doVector<double>(xd, part, i, sflags, nnhtp, &state->nhpres_xi, list); break;
-                case estNHPRES_VXI:  ret  = doVector<double>(xd, part, i, sflags, nnhtp, &state->nhpres_vxi, list); break;
-                case estTC_INT:  ret      = doVector<double>(xd, part, i, sflags, state->ngtc, &state->therm_integral, list); break;
+                case estNH_XI:   ret      = doVector<double>(xd, part, i, sflags, &state->nosehoover_xi, list); break;
+                case estNH_VXI:  ret      = doVector<double>(xd, part, i, sflags, &state->nosehoover_vxi, list); break;
+                case estNHPRES_XI:   ret  = doVector<double>(xd, part, i, sflags, &state->nhpres_xi, list); break;
+                case estNHPRES_VXI:  ret  = doVector<double>(xd, part, i, sflags, &state->nhpres_vxi, list); break;
+                case estTC_INT:  ret      = doVector<double>(xd, part, i, sflags, &state->therm_integral, list); break;
                 case estVETA:    ret      = do_cpte_real(xd, part, i, sflags, &state->veta, list); break;
                 case estVOL0:    ret      = do_cpte_real(xd, part, i, sflags, &state->vol0, list); break;
-                case estX:       ret      = doPaddedVector(xd, part, i, sflags, state->natoms, &state->x, list); break;
-                case estV:       ret      = doPaddedVector(xd, part, i, sflags, state->natoms, &state->v, list); break;
+                case estX:       ret      = doPaddedRvecVector(xd, part, i, sflags, &state->x, list); break;
+                case estV:       ret      = doPaddedRvecVector(xd, part, i, sflags, &state->v, list); break;
                 /* The RNG entries are no longer written,
                  * the next 4 lines are only for reading old files.
                  */
@@ -1085,9 +1086,9 @@ static int do_cpt_ekinstate(XDR *xd, int fflags, ekinstate_t *ekins,
                 case eeksEKINF:      ret = do_cpte_matrices(xd, part, i, fflags, ekins->ekin_n, &ekins->ekinf, list); break;
                 case eeksEKINO:      ret = do_cpte_matrices(xd, part, i, fflags, ekins->ekin_n, &ekins->ekinh_old, list); break;
                 case eeksEKINTOTAL:  ret = do_cpte_matrix(xd, part, i, fflags, ekins->ekin_total, list); break;
-                case eeksEKINSCALEF: ret = doVector<double>(xd, part, i, fflags, ekins->ekin_n, &ekins->ekinscalef_nhc, list); break;
-                case eeksVSCALE:     ret = doVector<double>(xd, part, i, fflags, ekins->ekin_n, &ekins->vscale_nhc, list); break;
-                case eeksEKINSCALEH: ret = doVector<double>(xd, part, i, fflags, ekins->ekin_n, &ekins->ekinscaleh_nhc, list); break;
+                case eeksEKINSCALEF: ret = doVector<double>(xd, part, i, fflags, &ekins->ekinscalef_nhc, list); break;
+                case eeksVSCALE:     ret = doVector<double>(xd, part, i, fflags, &ekins->vscale_nhc, list); break;
+                case eeksEKINSCALEH: ret = doVector<double>(xd, part, i, fflags, &ekins->ekinscaleh_nhc, list); break;
                 case eeksDEKINDL:   ret  = do_cpte_real(xd, part, i, fflags, &ekins->dekindl, list); break;
                 case eeksMVCOS:      ret = do_cpte_real(xd, part, i, fflags, &ekins->mvcos, list); break;
                 default:
@@ -1274,10 +1275,10 @@ static int do_cpt_enerhist(XDR *xd, gmx_bool bRead,
             switch (i)
             {
                 case eenhENERGY_N:     ret = do_cpte_int(xd, part, i, fflags, &energyHistoryNumEnergies, list); break;
-                case eenhENERGY_AVER:  ret = doVector<double>(xd, part, i, fflags, energyHistoryNumEnergies, &enerhist->ener_ave, list); break;
-                case eenhENERGY_SUM:   ret = doVector<double>(xd, part, i, fflags, energyHistoryNumEnergies, &enerhist->ener_sum, list); break;
+                case eenhENERGY_AVER:  ret = doVector<double>(xd, part, i, fflags, &enerhist->ener_ave, list); break;
+                case eenhENERGY_SUM:   ret = doVector<double>(xd, part, i, fflags, &enerhist->ener_sum, list); break;
                 case eenhENERGY_NSUM:  do_cpt_step_err(xd, eenh_names[i], &enerhist->nsum, list); break;
-                case eenhENERGY_SUM_SIM: ret = doVector<double>(xd, part, i, fflags, energyHistoryNumEnergies, &enerhist->ener_sum_sim, list); break;
+                case eenhENERGY_SUM_SIM: ret = doVector<double>(xd, part, i, fflags, &enerhist->ener_sum_sim, list); break;
                 case eenhENERGY_NSUM_SIM:   do_cpt_step_err(xd, eenh_names[i], &enerhist->nsum_sim, list); break;
                 case eenhENERGY_NSTEPS:     do_cpt_step_err(xd, eenh_names[i], &enerhist->nsteps, list); break;
                 case eenhENERGY_NSTEPS_SIM: do_cpt_step_err(xd, eenh_names[i], &enerhist->nsteps_sim, list); break;
@@ -2535,17 +2536,11 @@ read_checkpoint_state(const char *fn, int *simulation_part,
 
 void read_checkpoint_trxframe(t_fileio *fp, t_trxframe *fr)
 {
-    /* This next line is nasty because the sub-structures of t_state
-     * cannot be assumed to be zeroed (or even initialized in ways the
-     * rest of the code might assume). Using snew would be better, but
-     * this will all go away for 5.0. */
     t_state         state;
     int             simulation_part;
     gmx_int64_t     step;
     double          t;
 
-    init_state(&state, 0, 0, 0, 0, 0);
-
     read_checkpoint_data(fp, &simulation_part, &step, &t, &state, nullptr, nullptr);
 
     fr->natoms  = state.natoms;
@@ -2594,8 +2589,7 @@ void list_checkpoint(const char *fn, FILE *out)
     gmx_file_position_t *outputfiles;
     int                  nfiles;
 
-    t_state              state = {};
-    init_state(&state, 0, 0, 0, 0, 0);
+    t_state              state;
 
     fp = gmx_fio_open(fn, "r");
     do_cpt_header(gmx_fio_getxdr(fp), TRUE, &file_version,
@@ -2667,8 +2661,6 @@ read_checkpoint_simulation_part_and_filenames(t_fileio             *fp,
     double      t;
     t_state     state;
 
-    init_state(&state, 0, 0, 0, 0, 0);
-
     read_checkpoint_data(fp, simulation_part, &step, &t, &state,
                          nfiles, outputfiles);
     if (gmx_fio_close(fp) != 0)
index 1da65e2a13c3c49d13dfb559fa22eaef3882e2ea..ca9ca68678403aeff948048d2b1ecb046dd2607f 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -52,7 +52,7 @@ struct gmx_file_position_t;
 struct t_commrec;
 struct t_fileio;
 struct t_inputrec;
-struct t_state;
+class t_state;
 struct t_trxframe;
 
 /* the name of the environment variable to disable fsync failure checks with */
index 24d71f8e6d68af744d1dfc0cbd075d3aff4bf86a..338866545c85047223090f59ae9597eaed7aa411 100644 (file)
@@ -3080,6 +3080,7 @@ static int do_tpx(t_fileio *fio, gmx_bool bRead,
 
     if (!bRead)
     {
+        GMX_RELEASE_ASSERT(state != nullptr, "Cannot write a null state");
         GMX_RELEASE_ASSERT(x == nullptr && v == nullptr, "Passing separate x and v pointers to do_tpx() is not supported when writing");
 
         tpx.natoms    = state->natoms;
@@ -3106,14 +3107,11 @@ static int do_tpx(t_fileio *fio, gmx_bool bRead,
 
     if (bRead)
     {
-        state->flags  = 0;
-        if (x != nullptr)
+        state->flags = 0;
+        init_gtc_state(state, tpx.ngtc, 0, 0);
+        if (x == nullptr)
         {
-            init_state(state, 0, tpx.ngtc, 0, 0, 0);
-        }
-        else
-        {
-            init_state(state, tpx.natoms, tpx.ngtc, 0, 0, 0);
+            state_change_natoms(state, tpx.natoms);
         }
     }
 
@@ -3334,7 +3332,7 @@ int read_tpx(const char *fn,
              rvec *x, rvec *v, gmx_mtop_t *mtop)
 {
     t_fileio *fio;
-    t_state   state {};
+    t_state   state;
     int       ePBC;
 
     fio     = open_tpx(fn, "r");
index 023e3808ea2693a6164737ae82c49398f87f2619..26db602a6603d8c0785ab4608c56f793083cc52b 100644 (file)
@@ -607,7 +607,6 @@ new_status(const char *topfile, const char *topppfile, const char *confin,
     rvec       *x = nullptr;
     rvec       *v = nullptr;
     snew(conftop, 1);
-    init_state(state, 0, 0, 0, 0, 0);
     read_tps_conf(confin, conftop, nullptr, &x, &v, state->box, FALSE);
     state->natoms = conftop->atoms.nr;
     if (state->natoms != sys->natoms)
@@ -1715,7 +1714,7 @@ int gmx_grompp(int argc, char *argv[])
         gmx_fatal(FARGS, "%s does not exist", fn);
     }
 
-    t_state state {};
+    t_state state;
     new_status(fn, opt2fn_null("-pp", NFILE, fnm), opt2fn("-c", NFILE, fnm),
                opts, ir, bZero, bGenVel, bVerbose, &state,
                atype, sys, &nmi, &mi, &intermolecular_interactions,
index cd27b816a9370cc5d12eaf97157036e21860dfeb..12a26aa3ad962c67c1ecdff5e50c44e55f2b7462 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2014,2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2014,2015,2016,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -78,7 +78,7 @@ struct t_filenm;
 struct t_gmx_IMD;
 struct t_IMD;
 struct t_inputrec;
-struct t_state;
+class t_state;
 
 static const char IMDstr[] = "IMD:";  /**< Tag output from the IMD module with this string. */
 
index 183847d3709d1df0fc41a12f4b49d13f8bfbfa79..7c1bb1062d9e661f701ffdfd94fe0f671c516516 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
 #include "gromacs/utility/basedefinitions.h"
 
 struct gmx_mtop_t;
-struct history_t;
+class history_t;
 struct t_commrec;
 struct t_inputrec;
 struct t_pbc;
-struct t_state;
+class t_state;
 
 /*! \brief
  * Initiates *fcd data.
index acb95107dce6431d07efab3dd9862ec1fd015d2d..793ff361e603167766286c3b9412e6175bfb0bd6 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2014,2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2014,2015,2016,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -71,7 +71,7 @@
 
 struct gmx_enerdata_t;
 struct gmx_grppairener_t;
-struct history_t;
+class history_t;
 struct t_commrec;
 struct t_fcdata;
 struct t_forcerec;
@@ -80,7 +80,7 @@ struct t_inputrec;
 struct t_lambda;
 struct t_mdatoms;
 struct t_nrnb;
-struct t_state;
+class t_state;
 
 #ifdef __cplusplus
 extern "C" {
index c245898f054880e17e5e3231cf9a8bea64d6bd29..eac50d6e619e1dd29a373cf5d791fe78e6e8a5e7 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2010,2014,2015, by the GROMACS development team, led by
+ * Copyright (c) 2010,2014,2015,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
 
 struct gmx_mtop_t;
 struct gmx_multisim_t;
-struct history_t;
+class history_t;
 struct t_inputrec;
 struct t_pbc;
 struct t_commrec;
 struct t_fcdata;
 struct t_oriresdata;
-struct t_state;
+class t_state;
 
 /*! \brief
  * Decides whether orientation restraints can work, and initializes
index 2e12e605b5fb50a418efcf019f31d4dc3b416ec7..fcebb03e97f1ebe171ff73ac87d756149d66e8db 100644 (file)
@@ -277,7 +277,6 @@ void bcast_state(const t_commrec *cr, t_state *state)
     block_bc(cr, state->nnhpres);
     block_bc(cr, state->nhchainlength);
     block_bc(cr, state->flags);
-    state->lambda.resize(efptNR);
 
     if (cr->dd)
     {
index b3f8657c08ce08fb6e7e89c9b8a3ec4f9d075369..4a5c1a8dd34fafded509492e970a492269752331 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -64,7 +64,7 @@ extern "C" {
 #endif
 
 struct t_pbc;
-struct t_state;
+class t_state;
 
 enum
 {
index c8361b951e4ccbb5551606097d67653fe687f69c..14f23b8fd0bdb6b47454e033460fc9d39f2da737 100644 (file)
@@ -734,14 +734,14 @@ void sum_epot(gmx_grppairener_t *grpp, real *epot)
     }
 }
 
-void sum_dhdl(gmx_enerdata_t *enerd, const std::vector<real> *lambda, t_lambda *fepvals)
+void sum_dhdl(gmx_enerdata_t *enerd, gmx::ConstArrayRef<real> lambda, t_lambda *fepvals)
 {
-    int    i, j, index;
+    int    index;
     double dlam;
 
     enerd->dvdl_lin[efptVDW] += enerd->term[F_DVDL_VDW];  /* include dispersion correction */
     enerd->term[F_DVDL]       = 0.0;
-    for (i = 0; i < efptNR; i++)
+    for (int i = 0; i < efptNR; i++)
     {
         if (fepvals->separate_dvdl[i])
         {
@@ -802,7 +802,7 @@ void sum_dhdl(gmx_enerdata_t *enerd, const std::vector<real> *lambda, t_lambda *
     }
     enerd->term[F_DVDL_CONSTR] = 0;
 
-    for (i = 0; i < fepvals->n_lambda; i++)
+    for (int i = 0; i < fepvals->n_lambda; i++)
     {
         /* note we are iterating over fepvals here!
            For the current lam, dlam = 0 automatically,
@@ -813,10 +813,10 @@ void sum_dhdl(gmx_enerdata_t *enerd, const std::vector<real> *lambda, t_lambda *
            current lambda, because the contributions to the current
            lambda are automatically zeroed */
 
-        for (j = 0; j < efptNR; j++)
+        for (size_t j = 0; j < lambda.size(); j++)
         {
             /* Note that this loop is over all dhdl components, not just the separated ones */
-            dlam = (fepvals->all_lambda[j][i] - (*lambda)[j]);
+            dlam = (fepvals->all_lambda[j][i] - lambda[j]);
             enerd->enerpart_lambda[i+1] += dlam*enerd->dvdl_lin[j];
             if (debug)
             {
index 704561c4642f05875e7afe15dfc4c78a4b0035bc..903cc5cde26980e79ccad49231f5264147fc200b 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
 #include "gromacs/mdtypes/forcerec.h"
 #include "gromacs/mdtypes/state.h"
 #include "gromacs/timing/wallcycle.h"
+#include "gromacs/utility/arrayref.h"
 
 struct gmx_edsam;
 struct gmx_gpu_info_t;
 struct gmx_groups_t;
 struct gmx_vsite_t;
-struct history_t;
+class history_t;
 struct nonbonded_verlet_t;
 struct t_blocka;
 struct t_commrec;
@@ -150,7 +151,7 @@ void reset_enerdata(gmx_enerdata_t *enerd);
 void sum_epot(gmx_grppairener_t *grpp, real *epot);
 /* Locally sum the non-bonded potential energy terms */
 
-void sum_dhdl(gmx_enerdata_t *enerd, const std::vector<real> *lambda, t_lambda *fepvals);
+void sum_dhdl(gmx_enerdata_t *enerd, gmx::ConstArrayRef<real> lambda, t_lambda *fepvals);
 /* Sum the free energy contributions */
 
 /* Compute the average C6 and C12 params for LJ corrections */
@@ -167,7 +168,7 @@ void do_force(FILE *log, t_commrec *cr,
               tensor vir_force,
               t_mdatoms *mdatoms,
               gmx_enerdata_t *enerd, t_fcdata *fcd,
-              std::vector<real> *lambda, struct t_graph *graph,
+              gmx::ArrayRef<real> lambda, t_graph *graph,
               t_forcerec *fr,
               gmx_vsite_t *vsite, rvec mu_tot,
               double t, struct gmx_edsam *ed,
index af7db38b2d73ea02be4a8405c8a744d834452594..8b74b39a030535b259a5d9a3739aaa6a204193b4 100644 (file)
@@ -571,7 +571,6 @@ void set_state_entries(t_state *state, const t_inputrec *ir)
         state->flags |= (1<<estFEPSTATE);
     }
     state->flags |= (1<<estX);
-    state->lambda.resize(efptNR);
     GMX_RELEASE_ASSERT(state->x.size() >= static_cast<unsigned int>(state->natoms), "We should start a run with an initialized state->x");
     if (EI_DYNAMICS(ir->eI))
     {
index a28460b7e75f461860990af2044374a2d08690f6..f2f904b2bd5af1f87279a29f7a40d2336ccbc334 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -51,7 +51,7 @@ struct t_forcerec;
 struct t_grpopts;
 struct t_lambda;
 struct t_nrnb;
-struct t_state;
+class t_state;
 struct t_trxframe;
 
 namespace gmx
index ec62537731eef23503061a662807d4490cea2597..c04475e0a1859a0d4695f4268ba2cfaab8bea602 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -54,7 +54,7 @@ struct t_inputrec;
 struct t_lambda;
 struct t_mdatoms;
 struct t_simtemp;
-struct t_state;
+class t_state;
 
 #define MD_POLARISE       (1<<2)
 #define MD_RERUN          (1<<4)
index d959351aef40b5ae2fcc2d14a6409efc95147dcf..1eba0227b4d3fc07476bf2166bf58a752d59cf6c 100644 (file)
@@ -343,7 +343,7 @@ void init_em(FILE *fplog, const char *title,
     state_global->ngtc = 0;
 
     /* Initialize lambda variables */
-    initialize_lambdas(fplog, ir, &(state_global->fep_state), &state_global->lambda, nullptr);
+    initialize_lambdas(fplog, ir, &(state_global->fep_state), state_global->lambda, nullptr);
 
     init_nrnb(nrnb);
 
@@ -758,7 +758,7 @@ static void evaluate_energy(FILE *fplog, t_commrec *cr,
              count, nrnb, wcycle, top, &top_global->groups,
              ems->s.box, &ems->s.x, &ems->s.hist,
              &ems->f, force_vir, mdatoms, enerd, fcd,
-             &ems->s.lambda, graph, fr, vsite, mu_tot, t, nullptr, TRUE,
+             ems->s.lambda, graph, fr, vsite, mu_tot, t, nullptr, TRUE,
              GMX_FORCE_STATECHANGED | GMX_FORCE_ALLFORCES |
              GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY |
              (bNS ? GMX_FORCE_NS : 0));
@@ -817,7 +817,7 @@ static void evaluate_energy(FILE *fplog, t_commrec *cr,
     enerd->term[F_PRES] =
         calc_pres(fr->ePBC, inputrec->nwall, ems->s.box, ekin, vir, pres);
 
-    sum_dhdl(enerd, &ems->s.lambda, inputrec->fepvals);
+    sum_dhdl(enerd, ems->s.lambda, inputrec->fepvals);
 
     if (EI_ENERGY_MINIMIZATION(inputrec->eI))
     {
index ebcf6feb6dee7d6f49eb11b1b4bafce092ca0659..e92597b46032cbcd3558f72911daf71c0531b289 100644 (file)
@@ -894,7 +894,7 @@ static void init_adir(FILE *log, gmx_shellfc_t *shfc,
                       rvec *x_old, rvec *x_init, rvec *x,
                       rvec *f, rvec *acc_dir,
                       gmx_bool bMolPBC, matrix box,
-                      const std::vector<real> *lambda, real *dvdlambda,
+                      gmx::ConstArrayRef<real> lambda, real *dvdlambda,
                       t_nrnb *nrnb)
 {
     rvec           *xnold, *xnew;
@@ -944,11 +944,11 @@ static void init_adir(FILE *log, gmx_shellfc_t *shfc,
     }
     constrain(log, FALSE, FALSE, constr, idef, ir, cr, step, 0, 1.0, md,
               x, xnold, nullptr, bMolPBC, box,
-              (*lambda)[efptBONDED], &(dvdlambda[efptBONDED]),
+              lambda[efptBONDED], &(dvdlambda[efptBONDED]),
               nullptr, nullptr, nrnb, econqCoord);
     constrain(log, FALSE, FALSE, constr, idef, ir, cr, step, 0, 1.0, md,
               x, xnew, nullptr, bMolPBC, box,
-              (*lambda)[efptBONDED], &(dvdlambda[efptBONDED]),
+              lambda[efptBONDED], &(dvdlambda[efptBONDED]),
               nullptr, nullptr, nrnb, econqCoord);
 
     for (n = 0; n < end; n++)
@@ -965,7 +965,7 @@ static void init_adir(FILE *log, gmx_shellfc_t *shfc,
     /* Project the acceleration on the old bond directions */
     constrain(log, FALSE, FALSE, constr, idef, ir, cr, step, 0, 1.0, md,
               x_old, xnew, acc_dir, bMolPBC, box,
-              (*lambda)[efptBONDED], &(dvdlambda[efptBONDED]),
+              lambda[efptBONDED], &(dvdlambda[efptBONDED]),
               nullptr, nullptr, nrnb, econqDeriv_FlexCon);
 }
 
@@ -1113,7 +1113,7 @@ void relax_shell_flexcon(FILE *fplog, t_commrec *cr, gmx_bool bVerbose,
     do_force(fplog, cr, inputrec, mdstep, nrnb, wcycle, top, groups,
              state->box, &state->x, &state->hist,
              force[Min], force_vir, md, enerd, fcd,
-             &state->lambda, graph,
+             state->lambda, graph,
              fr, vsite, mu_tot, t, nullptr, bBornRadii,
              (bDoNS ? GMX_FORCE_NS : 0) | force_flags);
 
@@ -1124,7 +1124,7 @@ void relax_shell_flexcon(FILE *fplog, t_commrec *cr, gmx_bool bVerbose,
                   constr, idef, inputrec, cr, dd_ac1, mdstep, md, end,
                   shfc->x_old, as_rvec_array(state->x.data()), as_rvec_array(state->x.data()), as_rvec_array(force[Min]->data()),
                   shfc->acc_dir,
-                  fr->bMolPBC, state->box, &state->lambda, &dum, nrnb);
+                  fr->bMolPBC, state->box, state->lambda, &dum, nrnb);
 
         for (i = 0; i < end; i++)
         {
@@ -1192,7 +1192,7 @@ void relax_shell_flexcon(FILE *fplog, t_commrec *cr, gmx_bool bVerbose,
             init_adir(fplog, shfc,
                       constr, idef, inputrec, cr, dd_ac1, mdstep, md, end,
                       x_old, as_rvec_array(state->x.data()), as_rvec_array(pos[Min]->data()), as_rvec_array(force[Min]->data()), acc_dir,
-                      fr->bMolPBC, state->box, &state->lambda, &dum, nrnb);
+                      fr->bMolPBC, state->box, state->lambda, &dum, nrnb);
 
             directional_sd(pos[Min], pos[Try], acc_dir, end, fr->fc_stepsize);
         }
@@ -1215,7 +1215,7 @@ void relax_shell_flexcon(FILE *fplog, t_commrec *cr, gmx_bool bVerbose,
         do_force(fplog, cr, inputrec, 1, nrnb, wcycle,
                  top, groups, state->box, pos[Try], &state->hist,
                  force[Try], force_vir,
-                 md, enerd, fcd, &state->lambda, graph,
+                 md, enerd, fcd, state->lambda, graph,
                  fr, vsite, mu_tot, t, nullptr, bBornRadii,
                  force_flags);
 
@@ -1230,7 +1230,7 @@ void relax_shell_flexcon(FILE *fplog, t_commrec *cr, gmx_bool bVerbose,
             init_adir(fplog, shfc,
                       constr, idef, inputrec, cr, dd_ac1, mdstep, md, end,
                       x_old, as_rvec_array(state->x.data()), as_rvec_array(pos[Try]->data()), as_rvec_array(force[Try]->data()), acc_dir,
-                      fr->bMolPBC, state->box, &state->lambda, &dum, nrnb);
+                      fr->bMolPBC, state->box, state->lambda, &dum, nrnb);
 
             for (i = 0; i < end; i++)
             {
index f02f5127df8df44fb2c622bfdd10a44046d038f7..58cb2056b46305c70ee346e2eef17c56fd94dd89 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2008, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -52,7 +52,7 @@ struct t_forcerec;
 struct t_fcdata;
 struct t_graph;
 struct t_inputrec;
-struct t_state;
+class t_state;
 
 /* Initialization function, also predicts the initial shell postions.
  */
index e026136e56d26fac0d5911e9d5f50a4d3ce8451c..9cc24b911fe3f11f832356f74d3fc1aabc51bca3 100644 (file)
@@ -98,6 +98,7 @@
 #include "gromacs/timing/wallcycle.h"
 #include "gromacs/timing/wallcyclereporting.h"
 #include "gromacs/timing/walltime_accounting.h"
+#include "gromacs/utility/arrayref.h"
 #include "gromacs/utility/basedefinitions.h"
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/exceptions.h"
@@ -1842,7 +1843,7 @@ void do_force(FILE *fplog, t_commrec *cr,
               tensor vir_force,
               t_mdatoms *mdatoms,
               gmx_enerdata_t *enerd, t_fcdata *fcd,
-              std::vector<real> *lambda, t_graph *graph,
+              gmx::ArrayRef<real> lambda, t_graph *graph,
               t_forcerec *fr,
               gmx_vsite_t *vsite, rvec mu_tot,
               double t, gmx_edsam_t ed,
@@ -1871,7 +1872,7 @@ void do_force(FILE *fplog, t_commrec *cr,
                                 force, vir_force,
                                 mdatoms,
                                 enerd, fcd,
-                                lambda->data(), graph,
+                                lambda.data(), graph,
                                 fr, fr->ic,
                                 vsite, mu_tot,
                                 t, ed,
@@ -1887,7 +1888,7 @@ void do_force(FILE *fplog, t_commrec *cr,
                                force, vir_force,
                                mdatoms,
                                enerd, fcd,
-                               lambda->data(), graph,
+                               lambda.data(), graph,
                                fr, vsite, mu_tot,
                                t, ed,
                                bBornRadii,
@@ -2567,7 +2568,7 @@ void finish_run(FILE *fplog, const gmx::MDLogger &mdlog, t_commrec *cr,
     }
 }
 
-extern void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, std::vector<real> *lambda, double *lam0)
+extern void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, gmx::ArrayRef<real> lambda, double *lam0)
 {
     /* this function works, but could probably use a logic rewrite to keep all the different
        types of efep straight. */
@@ -2582,25 +2583,23 @@ extern void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, std:
                                             if checkpoint is set -- a kludge is in for now
                                             to prevent this.*/
 
-    lambda->resize(efptNR);
-
     for (int i = 0; i < efptNR; i++)
     {
         /* overwrite lambda state with init_lambda for now for backwards compatibility */
         if (fep->init_lambda >= 0) /* if it's -1, it was never initializd */
         {
-            (*lambda)[i] = fep->init_lambda;
+            lambda[i] = fep->init_lambda;
             if (lam0)
             {
-                lam0[i] = (*lambda)[i];
+                lam0[i] = lambda[i];
             }
         }
         else
         {
-            (*lambda)[i] = fep->all_lambda[i][*fep_state];
+            lambda[i] = fep->all_lambda[i][*fep_state];
             if (lam0)
             {
-                lam0[i] = (*lambda)[i];
+                lam0[i] = lambda[i];
             }
         }
     }
@@ -2620,9 +2619,9 @@ extern void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, std:
     if (fplog != nullptr)
     {
         fprintf(fplog, "Initial vector of lambda components:[ ");
-        for (int i = 0; i < efptNR; i++)
+        for (const auto &l : lambda)
         {
-            fprintf(fplog, "%10.4f ", (*lambda)[i]);
+            fprintf(fplog, "%10.4f ", l);
         }
         fprintf(fplog, "]\n");
     }
@@ -2633,7 +2632,7 @@ extern void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, std:
 void init_md(FILE *fplog,
              t_commrec *cr, t_inputrec *ir, const gmx_output_env_t *oenv,
              double *t, double *t0,
-             std::vector<real> *lambda, int *fep_state, double *lam0,
+             gmx::ArrayRef<real> lambda, int *fep_state, double *lam0,
              t_nrnb *nrnb, gmx_mtop_t *mtop,
              gmx_update_t **upd,
              int nfile, const t_filenm fnm[],
index 0b969c514e1bf98a1eb75434be1af6b741899d4f..95e9d9a77e66ee8ed998666cc89a99628a169f10 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -43,6 +43,7 @@
 #include "gromacs/mdlib/vcm.h"
 #include "gromacs/timing/wallcycle.h"
 #include "gromacs/timing/walltime_accounting.h"
+#include "gromacs/utility/arrayref.h"
 
 struct gmx_constr;
 struct gmx_localtop_t;
@@ -135,7 +136,7 @@ void calc_dispcorr(t_inputrec *ir, t_forcerec *fr,
                    matrix box, real lambda, tensor pres, tensor virial,
                    real *prescorr, real *enercorr, real *dvdlcorr);
 
-void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, std::vector<real> *lambda, double *lam0);
+void initialize_lambdas(FILE *fplog, t_inputrec *ir, int *fep_state, gmx::ArrayRef<real> lambda, double *lam0);
 
 void do_constrain_first(FILE *log, gmx_constr *constr,
                         t_inputrec *inputrec, t_mdatoms *md,
@@ -145,7 +146,7 @@ void do_constrain_first(FILE *log, gmx_constr *constr,
 void init_md(FILE *fplog,
              t_commrec *cr, t_inputrec *ir, const gmx_output_env_t *oenv,
              double *t, double *t0,
-             std::vector<real> *lambda, int *fep_state, double *lam0,
+             gmx::ArrayRef<real> lambda, int *fep_state, double *lam0,
              t_nrnb *nrnb, gmx_mtop_t *mtop,
              gmx_update_t **upd,
              int nfile, const t_filenm fnm[],
index bced9d71f7913e04a7d63ac21653cd4559aa192c..db0d76f71483047eb948f82a678b27db05c129c1 100644 (file)
@@ -662,7 +662,7 @@ double do_tpi(FILE *fplog, t_commrec *cr, const gmx::MDLogger gmx_unused &mdlog,
                      step, nrnb, wcycle, top, &top_global->groups,
                      state_global->box, &state_global->x, &state_global->hist,
                      &f, force_vir, mdatoms, enerd, fcd,
-                     &state_global->lambda,
+                     state_global->lambda,
                      nullptr, fr, nullptr, mu_tot, t, nullptr, FALSE,
                      GMX_FORCE_NONBONDED | GMX_FORCE_ENERGY |
                      (bNS ? GMX_FORCE_DYNAMICBOX | GMX_FORCE_NS : 0) |
index 0cd7b74d1819bec05d1684cc20b0d3cde37f5396..efb558efe827485bcd5796e4586681aaf889e261 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -43,7 +43,7 @@
 #include "gromacs/utility/basedefinitions.h"
 #include "gromacs/utility/real.h"
 
-struct ekinstate_t;
+class ekinstate_t;
 struct gmx_constr;
 struct gmx_ekindata_t;
 struct gmx_enerdata_t;
@@ -55,7 +55,7 @@ struct t_idef;
 struct t_inputrec;
 struct t_mdatoms;
 struct t_nrnb;
-struct t_state;
+class t_state;
 
 /* Abstract type for update */
 struct gmx_update_t;
index c6a5242954de202ee645696764c4be132dc9106c..2c44d7c6ea5b86e4e2e3d83782d07c7d442450ae 100644 (file)
 /* The source code in this file should be thread-safe.
       Please keep it that way. */
 
-static void zero_history(history_t *hist)
+history_t::history_t() : disre_initf(0),
+                         ndisrepairs(0),
+                         disre_rm3tav(nullptr),
+                         orire_initf(0),
+                         norire_Dtav(0),
+                         orire_Dtav(nullptr)
 {
-    hist->disre_initf  = 0;
-    hist->ndisrepairs  = 0;
-    hist->disre_rm3tav = nullptr;
-    hist->orire_initf  = 0;
-    hist->norire_Dtav  = 0;
-    hist->orire_Dtav   = nullptr;
-}
+};
 
-static void zero_ekinstate(ekinstate_t *eks)
+ekinstate_t::ekinstate_t() : bUpToDate(FALSE),
+                             ekin_n(0),
+                             ekinh(nullptr),
+                             ekinf(nullptr),
+                             ekinh_old(nullptr),
+                             ekin_total(),
+                             ekinscalef_nhc(),
+                             ekinscaleh_nhc(),
+                             vscale_nhc(),
+                             dekindl(0),
+                             mvcos(0)
 {
-    eks->ekin_n         = 0;
-    eks->ekinh          = nullptr;
-    eks->ekinf          = nullptr;
-    eks->ekinh_old      = nullptr;
-    eks->ekinscalef_nhc.resize(0);
-    eks->ekinscaleh_nhc.resize(0);
-    eks->vscale_nhc.resize(0);
-    eks->dekindl        = 0;
-    eks->mvcos          = 0;
-}
+    clear_mat(ekin_total);
+};
 
 static void init_swapstate(swapstate_t *swapstate)
 {
@@ -109,20 +110,11 @@ void init_gtc_state(t_state *state, int ngtc, int nnhpres, int nhchainlength)
 }
 
 
-void init_state(t_state *state, int natoms, int ngtc, int nnhpres, int nhchainlength, int dfhistNumLambda)
+/* Checkpoint code relies on this function having no effect if
+   state->natoms is > 0 and passed as natoms. */
+void state_change_natoms(t_state *state, int natoms)
 {
-    state->natoms    = natoms;
-    state->flags     = 0;
-    state->fep_state = 0;
-    state->lambda.resize(efptNR, 0);
-    state->veta   = 0;
-    clear_mat(state->box);
-    clear_mat(state->box_rel);
-    clear_mat(state->boxv);
-    clear_mat(state->pres_prev);
-    clear_mat(state->svir_prev);
-    clear_mat(state->fvir_prev);
-    init_gtc_state(state, ngtc, nnhpres, nhchainlength);
+    state->natoms = natoms;
     if (state->natoms > 0)
     {
         /* We need to allocate one element extra, since we might use
@@ -137,8 +129,10 @@ void init_state(t_state *state, int natoms, int ngtc, int nnhpres, int nhchainle
         state->v.resize(0);
     }
     state->cg_p.resize(0);
-    zero_history(&state->hist);
-    zero_ekinstate(&state->ekinstate);
+}
+
+void init_dfhist_state(t_state *state, int dfhistNumLambda)
+{
     if (dfhistNumLambda > 0)
     {
         snew(state->dfhist, 1);
@@ -148,11 +142,6 @@ void init_state(t_state *state, int natoms, int ngtc, int nnhpres, int nhchainle
     {
         state->dfhist = nullptr;
     }
-    state->swapstate       = nullptr;
-    state->edsamstate      = nullptr;
-    state->ddp_count       = 0;
-    state->ddp_count_cg_gl = 0;
-    state->cg_gl.resize(0);
 }
 
 void comp_state(const t_state *st1, const t_state *st2,
@@ -244,3 +233,43 @@ rvec *getRvecArrayFromPaddedRVecVector(const PaddedRVecVector *v,
 
     return dest;
 }
+
+t_state::t_state() : natoms(0),
+                     ngtc(0),
+                     nnhpres(0),
+                     nhchainlength(0),
+                     flags(0),
+                     fep_state(0),
+                     lambda(),
+                     nosehoover_xi(),
+                     nosehoover_vxi(),
+                     nhpres_xi(),
+                     nhpres_vxi(),
+                     therm_integral(),
+                     veta(0),
+                     vol0(0),
+                     x(),
+                     v(),
+                     cg_p(),
+                     ekinstate(),
+                     hist(),
+                     swapstate(nullptr),
+                     dfhist(nullptr),
+                     edsamstate(nullptr),
+                     ddp_count(0),
+                     ddp_count_cg_gl(0),
+                     cg_gl()
+{
+    // It would be nicer to initialize these with {} or {{0}} in the
+    // above initialization list, but uncrustify doesn't understand
+    // that.
+    // TODO Fix this if we switch to clang-format some time.
+    // cppcheck-suppress useInitializationList
+    lambda = {{ 0 }};
+    clear_mat(box);
+    clear_mat(box_rel);
+    clear_mat(boxv);
+    clear_mat(pres_prev);
+    clear_mat(svir_prev);
+    clear_mat(fvir_prev);
+}
index 5b1ed2462798384cb49666e137c23fa1fdedd282..6a5896842ffa99f99071a4b0358d9069a55aa4da 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -37,6 +37,7 @@
 #ifndef GMX_MDTYPES_STATE_H
 #define GMX_MDTYPES_STATE_H
 
+#include <array>
 #include <vector>
 
 #include "gromacs/math/paddedvector.h"
@@ -72,15 +73,18 @@ enum {
 /* The names of the state entries, defined in src/gmxlib/checkpoint.c */
 extern const char *est_names[estNR];
 
-typedef struct history_t
+class history_t
 {
-    real  disre_initf;  /* The scaling factor for initializing the time av. */
-    int   ndisrepairs;  /* The number of distance restraints                */
-    real *disre_rm3tav; /* The r^-3 time averaged pair distances            */
-    real  orire_initf;  /* The scaling factor for initializing the time av. */
-    int   norire_Dtav;  /* The number of matrix element in dtav (npair*5)   */
-    real *orire_Dtav;   /* The time averaged orientation tensors            */
-} history_t;
+    public:
+        history_t();
+
+        real  disre_initf;  /* The scaling factor for initializing the time av. */
+        int   ndisrepairs;  /* The number of distance restraints                */
+        real *disre_rm3tav; /* The r^-3 time averaged pair distances            */
+        real  orire_initf;  /* The scaling factor for initializing the time av. */
+        int   norire_Dtav;  /* The number of matrix element in dtav (npair*5)   */
+        real *orire_Dtav;   /* The time averaged orientation tensors            */
+};
 
 /* Struct used for checkpointing only.
  * This struct would not be required with unlimited precision.
@@ -88,20 +92,23 @@ typedef struct history_t
  * can cause the kinetic energy in the MD loop to differ by a few bits from
  * the kinetic energy one would determine from state.v.
  */
-typedef struct ekinstate_t
+class ekinstate_t
 {
-    gmx_bool             bUpToDate;
-    int                  ekin_n;
-    tensor              *ekinh;
-    tensor              *ekinf;
-    tensor              *ekinh_old;
-    tensor               ekin_total;
-    std::vector<double>  ekinscalef_nhc;
-    std::vector<double>  ekinscaleh_nhc;
-    std::vector<double>  vscale_nhc;
-    real                 dekindl;
-    real                 mvcos;
-} ekinstate_t;
+    public:
+        ekinstate_t();
+
+        gmx_bool             bUpToDate;
+        int                  ekin_n;
+        tensor              *ekinh;
+        tensor              *ekinf;
+        tensor              *ekinh_old;
+        tensor               ekin_total;
+        std::vector<double>  ekinscalef_nhc;
+        std::vector<double>  ekinscaleh_nhc;
+        std::vector<double>  vscale_nhc;
+        real                 dekindl;
+        real                 mvcos;
+};
 
 typedef struct df_history_t
 {
@@ -192,44 +199,49 @@ typedef struct swapstate_t
 swapstate_t;
 
 
-typedef struct t_state
+class t_state
 {
-    int                     natoms;
-    int                     ngtc;
-    int                     nnhpres;
-    int                     nhchainlength;   /* number of nose-hoover chains               */
-    int                     flags;           /* Flags telling which entries are present      */
-    int                     fep_state;       /* indicates which of the alchemical states we are in                 */
-    std::vector<real>       lambda;          /* lambda vector                               */
-    matrix                  box;             /* box vector coordinates                         */
-    matrix                  box_rel;         /* Relitaive box vectors to preserve shape        */
-    matrix                  boxv;            /* box velocitites for Parrinello-Rahman pcoupl */
-    matrix                  pres_prev;       /* Pressure of the previous step for pcoupl  */
-    matrix                  svir_prev;       /* Shake virial for previous step for pcoupl */
-    matrix                  fvir_prev;       /* Force virial of the previous step for pcoupl  */
-    std::vector<double>     nosehoover_xi;   /* for Nose-Hoover tcoupl (ngtc)       */
-    std::vector<double>     nosehoover_vxi;  /* for N-H tcoupl (ngtc)               */
-    std::vector<double>     nhpres_xi;       /* for Nose-Hoover pcoupl for barostat     */
-    std::vector<double>     nhpres_vxi;      /* for Nose-Hoover pcoupl for barostat     */
-    std::vector<double>     therm_integral;  /* for N-H/V-rescale tcoupl (ngtc)     */
-    real                    veta;            /* trotter based isotropic P-coupling             */
-    real                    vol0;            /* initial volume,required for computing NPT conserverd quantity */
-    PaddedRVecVector        x;               /* the coordinates (natoms)                     */
-    PaddedRVecVector        v;               /* the velocities (natoms)                      */
-    PaddedRVecVector        cg_p;            /* p vector for conjugate gradient minimization */
-
-    ekinstate_t             ekinstate;       /* The state of the kinetic energy data      */
-
-    /* History for special algorithms, should be moved to a history struct */
-    history_t               hist;            /* Time history for restraints                  */
-    swapstate_t            *swapstate;       /* Position swapping                       */
-    df_history_t           *dfhist;          /*Free energy history for free energy analysis  */
-    edsamstate_t           *edsamstate;      /* Essential dynamics / flooding history */
-
-    int                     ddp_count;       /* The DD partitioning count for this state  */
-    int                     ddp_count_cg_gl; /* The DD part. count for index_gl     */
-    std::vector<int>        cg_gl;           /* The global cg number of the local cgs        */
-} t_state;
+    public:
+        // Constructor
+        t_state();
+
+        // All things public
+        int                      natoms;
+        int                      ngtc;
+        int                      nnhpres;
+        int                      nhchainlength;  /* number of nose-hoover chains               */
+        int                      flags;          /* Flags telling which entries are present      */
+        int                      fep_state;      /* indicates which of the alchemical states we are in                 */
+        std::array<real, efptNR> lambda;         /* lambda vector                               */
+        matrix                   box;            /* box vector coordinates                         */
+        matrix                   box_rel;        /* Relitaive box vectors to preserve shape        */
+        matrix                   boxv;           /* box velocitites for Parrinello-Rahman pcoupl */
+        matrix                   pres_prev;      /* Pressure of the previous step for pcoupl  */
+        matrix                   svir_prev;      /* Shake virial for previous step for pcoupl */
+        matrix                   fvir_prev;      /* Force virial of the previous step for pcoupl  */
+        std::vector<double>      nosehoover_xi;  /* for Nose-Hoover tcoupl (ngtc)       */
+        std::vector<double>      nosehoover_vxi; /* for N-H tcoupl (ngtc)               */
+        std::vector<double>      nhpres_xi;      /* for Nose-Hoover pcoupl for barostat     */
+        std::vector<double>      nhpres_vxi;     /* for Nose-Hoover pcoupl for barostat     */
+        std::vector<double>      therm_integral; /* for N-H/V-rescale tcoupl (ngtc)     */
+        real                     veta;           /* trotter based isotropic P-coupling             */
+        real                     vol0;           /* initial volume,required for computing NPT conserverd quantity */
+        PaddedRVecVector         x;              /* the coordinates (natoms)                     */
+        PaddedRVecVector         v;              /* the velocities (natoms)                      */
+        PaddedRVecVector         cg_p;           /* p vector for conjugate gradient minimization */
+
+        ekinstate_t              ekinstate;      /* The state of the kinetic energy data      */
+
+        /* History for special algorithms, should be moved to a history struct */
+        history_t               hist;            /* Time history for restraints                  */
+        swapstate_t            *swapstate;       /* Position swapping                       */
+        df_history_t           *dfhist;          /*Free energy history for free energy analysis  */
+        edsamstate_t           *edsamstate;      /* Essential dynamics / flooding history */
+
+        int                     ddp_count;       /* The DD partitioning count for this state  */
+        int                     ddp_count_cg_gl; /* The DD part. count for index_gl     */
+        std::vector<int>        cg_gl;           /* The global cg number of the local cgs        */
+};
 
 typedef struct t_extmass
 {
@@ -250,9 +262,14 @@ typedef struct
     double *vscale_nhc;
 } t_vetavars;
 
+//! Allocates memory for temperature coupling
 void init_gtc_state(t_state *state, int ngtc, int nnhpres, int nhchainlength);
 
-void init_state(t_state *state, int natoms, int ngtc, int nnhpres, int nhchainlength, int dfhistNumLambda);
+//! Change the number of atoms represented by this state, allocating memory as needed.
+void state_change_natoms(t_state *state, int natoms);
+
+//! Allocates memory for free-energy history
+void init_dfhist_state(t_state *state, int dfhistNumLambda);
 
 void comp_state(const t_state *st1, const t_state *st2, gmx_bool bRMSD, real ftol, real abstol);
 
index 2f8ca1de112f51ad834fe1a51bb90fb69ced85f3..824cd62ad1721eff664909d42ac33407e4ca18e4 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2015,2016,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -42,7 +42,7 @@
 #include "gromacs/utility/real.h"
 
 struct t_inputrec;
-struct t_state;
+class t_state;
 
 /*! \brief Make sure the relative box shape remains the same
  *
index 0979763b0ac0ab483638cafa102476bd10afc2ca..6b9e31c30d4e32470b278d3c5ccaa21236f651a6 100644 (file)
@@ -78,7 +78,7 @@ static void list_tpx(const char *fn,
 {
     FILE         *gp;
     int           indent, i, j, **gcount, atot;
-    t_state       state {};
+    t_state       state;
     t_inputrec   *ir = nullptr;
     t_tpxheader   tpx;
     gmx_mtop_t    mtop;
index 63583b9055b0aef4351e2a0230e40ff8777eb5f3..3dccf983aa2d4eedc3d697ef737442e90204a679 100644 (file)
@@ -352,7 +352,7 @@ double gmx::do_md(FILE *fplog, t_commrec *cr, const gmx::MDLogger &mdlog,
     }
 
     /* Initial values */
-    init_md(fplog, cr, ir, oenv, &t, &t0, &state_global->lambda,
+    init_md(fplog, cr, ir, oenv, &t, &t0, state_global->lambda,
             &(state_global->fep_state), lam0,
             nrnb, top_global, &upd,
             nfile, fnm, &outf, &mdebin,
@@ -417,7 +417,7 @@ double gmx::do_md(FILE *fplog, t_commrec *cr, const gmx::MDLogger &mdlog,
     {
         top = dd_init_local_top(top_global);
 
-        stateInstance = std::unique_ptr<t_state>(new t_state {});
+        stateInstance = std::unique_ptr<t_state>(new t_state);
         state         = stateInstance.get();
         dd_init_local_state(cr->dd, state_global, state);
     }
@@ -1087,7 +1087,7 @@ double gmx::do_md(FILE *fplog, t_commrec *cr, const gmx::MDLogger &mdlog,
             do_force(fplog, cr, ir, step, nrnb, wcycle, top, groups,
                      state->box, &state->x, &state->hist,
                      &f, force_vir, mdatoms, enerd, fcd,
-                     &state->lambda, graph,
+                     state->lambda, graph,
                      fr, vsite, mu_tot, t, ed, bBornRadii,
                      (bNS ? GMX_FORCE_NS : 0) | force_flags);
         }
@@ -1235,7 +1235,7 @@ double gmx::do_md(FILE *fplog, t_commrec *cr, const gmx::MDLogger &mdlog,
             /* sum up the foreign energy and dhdl terms for vv.  currently done every step so that dhdl is correct in the .edr */
             if (ir->efep != efepNO && !bRerunMD)
             {
-                sum_dhdl(enerd, &state->lambda, ir->fepvals);
+                sum_dhdl(enerd, state->lambda, ir->fepvals);
             }
         }
 
@@ -1556,7 +1556,7 @@ double gmx::do_md(FILE *fplog, t_commrec *cr, const gmx::MDLogger &mdlog,
         {
             /* Sum up the foreign energy and dhdl terms for md and sd.
                Currently done every step so that dhdl is correct in the .edr */
-            sum_dhdl(enerd, &state->lambda, ir->fepvals);
+            sum_dhdl(enerd, state->lambda, ir->fepvals);
         }
 
         update_pcouple_after_coordinates(fplog, step, ir, mdatoms,
index d4da9ee8198c8e8919602cdc7567b4dd7a16e39c..9f754fa9ae5d94afaefdc2ba4647258ea8bf2b39 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2012,2014,2015, by the GROMACS development team, led by
+ * Copyright (c) 2012,2014,2015,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -46,7 +46,7 @@ struct gmx_mtop_t;
 struct t_commrec;
 struct t_filenm;
 struct t_inputrec;
-struct t_state;
+class t_state;
 
 /* initialisation of membed code */
 gmx_membed_t *init_membed(FILE *fplog, int nfile, const t_filenm fnm[], gmx_mtop_t *mtop,
index dcb47d1fdd3a425bdb4f1414c259bddff07ac653..2a0a517eb7af2b906beaa6db7b78d85078695635 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2011,2012,2013,2014,2015, by the GROMACS development team, led by
+ * Copyright (c) 2011,2012,2013,2014,2015,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -47,7 +47,7 @@ struct gmx_enerdata_t;
 struct gmx_multisim_t;
 struct t_commrec;
 struct t_inputrec;
-struct t_state;
+class t_state;
 
 /* Abstract type for replica exchange */
 typedef struct gmx_repl_ex *gmx_repl_ex_t;
index dc1b6cb85fc0b62ed12c845d3ff873a627b149fb..63327cbd434942f18959a08936335142daa4740f 100644 (file)
@@ -484,7 +484,7 @@ static void increase_nstlist(FILE *fp, t_commrec *cr,
             {
                 gmx_incons("Changing nstlist with domain decomposition and unbounded dimensions is not implemented yet");
             }
-            t_state state_tmp {};
+            t_state state_tmp;
             copy_mat(box, state_tmp.box);
             bDD = change_dd_cutoff(cr, &state_tmp, ir, rlist_new);
         }
@@ -772,7 +772,7 @@ int mdrunner(gmx_hw_opt_t *hw_opt,
         please_cite(fplog, "Berendsen95a");
     }
 
-    std::unique_ptr<t_state> stateInstance = std::unique_ptr<t_state>(new t_state {});
+    std::unique_ptr<t_state> stateInstance = std::unique_ptr<t_state>(new t_state);
     t_state *                state         = stateInstance.get();
 
     if (SIMMASTER(cr))