Minor tweaks to sum_dhdl and setCurrentLambdasLocal
authorPascal Merz <pascal.merz@me.com>
Tue, 17 Sep 2019 02:32:57 +0000 (20:32 -0600)
committerPascal Merz <pascal.merz@me.com>
Tue, 17 Sep 2019 21:44:04 +0000 (15:44 -0600)
setCurrentLambdasLocal() is taking the entire t_state as input,
but only uses the lambda vector and the free energy state. The
function arguments were changed to reflect that. This facilitates
rewriting the state data structure.

This change also adds a trivial const qualifier to sum_dhdl.

Change-Id: I50cb2b77711c13c1d77f6d55cfd32a324f94f76e

src/gromacs/mdlib/enerdata_utils.cpp
src/gromacs/mdlib/enerdata_utils.h
src/gromacs/mdlib/md_support.cpp
src/gromacs/mdlib/md_support.h
src/gromacs/mdrun/md.cpp
src/gromacs/mdrun/mimic.cpp
src/gromacs/mdrun/minimize.cpp
src/gromacs/mdrun/rerun.cpp

index 9baf464dd07bbcdc18cf86cbe668550ace172656..b5dd905e6f5184602b577bfa8c63e881ea9c1686 100644 (file)
@@ -90,7 +90,7 @@ void sum_epot(gmx_grppairener_t *grpp, real *epot)
     }
 }
 
-void sum_dhdl(gmx_enerdata_t *enerd, gmx::ArrayRef<const real> lambda, t_lambda *fepvals)
+void sum_dhdl(gmx_enerdata_t *enerd, gmx::ArrayRef<const real> lambda, const t_lambda &fepvals)
 {
     int    index;
 
@@ -98,7 +98,7 @@ void sum_dhdl(gmx_enerdata_t *enerd, gmx::ArrayRef<const real> lambda, t_lambda
     enerd->term[F_DVDL]       = 0.0;
     for (int i = 0; i < efptNR; i++)
     {
-        if (fepvals->separate_dvdl[i])
+        if (fepvals.separate_dvdl[i])
         {
             /* could this be done more readably/compactly? */
             switch (i)
@@ -140,7 +140,7 @@ void sum_dhdl(gmx_enerdata_t *enerd, gmx::ArrayRef<const real> lambda, t_lambda
         }
     }
 
-    if (fepvals->separate_dvdl[efptBONDED])
+    if (fepvals.separate_dvdl[efptBONDED])
     {
         enerd->term[F_DVDL_BONDED] += enerd->term[F_DVDL_CONSTR];
     }
@@ -149,7 +149,7 @@ void sum_dhdl(gmx_enerdata_t *enerd, gmx::ArrayRef<const real> lambda, t_lambda
         enerd->term[F_DVDL] += enerd->term[F_DVDL_CONSTR];
     }
 
-    for (int 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,
@@ -165,7 +165,7 @@ void sum_dhdl(gmx_enerdata_t *enerd, gmx::ArrayRef<const real> lambda, t_lambda
         for (gmx::index j = 0; j < lambda.ssize(); j++)
         {
             /* Note that this loop is over all dhdl components, not just the separated ones */
-            const double dlam  = fepvals->all_lambda[j][i] - lambda[j];
+            const double dlam  = fepvals.all_lambda[j][i] - lambda[j];
 
             enerpart_lambda   += dlam*enerd->dvdl_lin[j];
 
@@ -173,13 +173,13 @@ void sum_dhdl(gmx_enerdata_t *enerd, gmx::ArrayRef<const real> lambda, t_lambda
              * a linear extrapolation. This is an approximation, but usually
              * quite accurate since constraints change little between lambdas.
              */
-            if ((j == efptBONDED && fepvals->separate_dvdl[efptBONDED]) ||
-                (j == efptFEP && !fepvals->separate_dvdl[efptBONDED]))
+            if ((j == efptBONDED && fepvals.separate_dvdl[efptBONDED]) ||
+                (j == efptFEP && !fepvals.separate_dvdl[efptBONDED]))
             {
                 enerpart_lambda += dlam*enerd->term[F_DVDL_CONSTR];
             }
 
-            if (j == efptMASS && !fepvals->separate_dvdl[j])
+            if (j == efptMASS && !fepvals.separate_dvdl[j])
             {
                 enerpart_lambda += dlam*enerd->term[F_DKDL];
             }
@@ -187,7 +187,7 @@ void sum_dhdl(gmx_enerdata_t *enerd, gmx::ArrayRef<const real> lambda, t_lambda
             if (debug)
             {
                 fprintf(debug, "enerdiff lam %g: (%15s), non-linear %f linear %f*%f\n",
-                        fepvals->all_lambda[j][i], efpt_names[j],
+                        fepvals.all_lambda[j][i], efpt_names[j],
                         enerpart_lambda - enerd->enerpart_lambda[0],
                         dlam, enerd->dvdl_lin[j]);
             }
index 0f8f1ae0395d68c635827e16164dc15f09e49b7f..bfecf22d5d5f7bab4ccc1b85d931d8f37713a499 100644 (file)
@@ -55,7 +55,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, gmx::ArrayRef<const real> lambda, t_lambda *fepvals);
+void sum_dhdl(gmx_enerdata_t *enerd, gmx::ArrayRef<const real> lambda, const t_lambda &fepvals);
 /* Sum the free energy contributions */
 
 #endif
index a6cfbaff7338233210f471f965ed8b8e6f1d4eab..b43e108126a925eb8300e861bf93c873cf39d44d 100644 (file)
@@ -342,8 +342,9 @@ void setCurrentLambdasRerun(int64_t step, const t_lambda *fepvals,
     }
 }
 
-void setCurrentLambdasLocal(int64_t step, const t_lambda *fepvals,
-                            const double *lam0, t_state *state)
+void setCurrentLambdasLocal(const int64_t step, const t_lambda *fepvals,
+                            const double *lam0, gmx::ArrayRef<real> lambda,
+                            const int currentFEPState)
 /* find the current lambdas.  If rerunning, we either read in a state, or a lambda value,
    requiring different logic. */
 {
@@ -359,7 +360,7 @@ void setCurrentLambdasLocal(int64_t step, const t_lambda *fepvals,
             frac          = frac*fepvals->n_lambda - fep_state;
             for (int i = 0; i < efptNR; i++)
             {
-                state->lambda[i] = lam0[i] + (fepvals->all_lambda[i][fep_state]) +
+                lambda[i] = lam0[i] + (fepvals->all_lambda[i][fep_state]) +
                     frac*(fepvals->all_lambda[i][fep_state + 1] - fepvals->all_lambda[i][fep_state]);
             }
         }
@@ -367,18 +368,18 @@ void setCurrentLambdasLocal(int64_t step, const t_lambda *fepvals,
         {
             for (int i = 0; i < efptNR; i++)
             {
-                state->lambda[i] = lam0[i] + frac;
+                lambda[i] = lam0[i] + frac;
             }
         }
     }
     else
     {
         /* if < 0, fep_state was never defined, and we should not set lambda from the state */
-        if (state->fep_state > -1)
+        if (currentFEPState > -1)
         {
             for (int i = 0; i < efptNR; i++)
             {
-                state->lambda[i] = fepvals->all_lambda[i][state->fep_state];
+                lambda[i] = fepvals->all_lambda[i][currentFEPState];
             }
         }
     }
index 19acc1afd21a83e651991a9b405cfd079b2fcc5f..5a92104105ce52a8c6f65d29bb23a276c50d3a67 100644 (file)
@@ -56,6 +56,7 @@ struct t_trxframe;
 
 namespace gmx
 {
+template <typename T> class ArrayRef;
 class Constraints;
 class MDLogger;
 class SimulationSignaller;
@@ -115,7 +116,8 @@ void setCurrentLambdasRerun(int64_t step, const t_lambda *fepvals,
 
 /* Set the lambda values at each step of mdrun when they change */
 void setCurrentLambdasLocal(int64_t step, const t_lambda *fepvals,
-                            const double *lam0, t_state *state);
+                            const double *lam0, gmx::ArrayRef<real> lambda,
+                            int currentFEPState);
 
 int multisim_min(const gmx_multisim_t *ms, int nmin, int n);
 /* Set an appropriate value for n across the whole multi-simulation */
index 279c766398a33e9b48f491e439186b5c263975c2..f0e36b335009032ab86a829526f5c6b6dd1b5c50 100644 (file)
@@ -721,7 +721,7 @@ void gmx::LegacySimulator::do_md()
         if (ir->efep != efepNO || ir->bSimTemp)
         {
             /* find and set the current lambdas */
-            setCurrentLambdasLocal(step, ir->fepvals, lam0, state);
+            setCurrentLambdasLocal(step, ir->fepvals, lam0, state->lambda, state->fep_state);
 
             bDoDHDL      = do_per_step(step, ir->fepvals->nstdhdl);
             bDoFEP       = ((ir->efep != efepNO) && do_per_step(step, nstfep));
@@ -1064,7 +1064,7 @@ void gmx::LegacySimulator::do_md()
             /* 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)
             {
-                sum_dhdl(enerd, state->lambda, ir->fepvals);
+                sum_dhdl(enerd, state->lambda, *ir->fepvals);
             }
         }
 
@@ -1364,7 +1364,7 @@ void gmx::LegacySimulator::do_md()
         {
             /* 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 f33e97fc54d39e8ccddc0a7ecf37da50834280de..af7a542a80aad4192372302f12c85b32c19efa95 100644 (file)
@@ -376,7 +376,7 @@ void gmx::LegacySimulator::do_mimic()
 
         if (ir->efep != efepNO)
         {
-            setCurrentLambdasLocal(step, ir->fepvals, lam0, state);
+            setCurrentLambdasLocal(step, ir->fepvals, lam0, state->lambda, state->fep_state);
         }
 
         if (MASTER(cr))
@@ -556,7 +556,7 @@ void gmx::LegacySimulator::do_mimic()
         {
             /* 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);
         }
 
         /* Output stuff */
index 87a825cff7d86773e39f4959a186638ff7e04072..263410c6cc15cf3de045d6e4a9f1396dca442d79 100644 (file)
@@ -915,7 +915,7 @@ EnergyEvaluator::run(em_state_t *ems, rvec mu_tot,
     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 b554582cb0f40f2216e40f33fa624c0d6419994a..2cc56466142f269a3d95f74aced5990a9e37dbf0 100644 (file)
@@ -655,7 +655,7 @@ void gmx::LegacySimulator::do_rerun()
         {
             /* 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);
         }
 
         /* Output stuff */