Make inputrec const at simulator level (leaves some TODOs)
authorPascal Merz <pascal.merz@me.com>
Tue, 12 Jan 2021 09:36:19 +0000 (09:36 +0000)
committerPaul Bauer <paul.bauer.q@gmail.com>
Tue, 12 Jan 2021 09:36:19 +0000 (09:36 +0000)
This does not resolve the last few instances that change inputrec, but it
makes sure that the intent of inputrec being const is clear, and that future
code changes don't easily introduce additional non-const uses of inputrec.
See !46 for a similar change targetting the global topology.

Refs #3854.

src/gromacs/mdlib/update_vv.cpp
src/gromacs/mdlib/update_vv.h
src/gromacs/mdrun/isimulator.h
src/gromacs/mdrun/md.cpp
src/gromacs/mdrun/mimic.cpp
src/gromacs/mdrun/minimize.cpp
src/gromacs/mdrun/rerun.cpp
src/gromacs/mdrun/tpi.cpp

index 40b99699a8c4539b6f26ebf0141ab6a83fed822a..bd40e9b4266e8a185b3d1ad430969185046b1edf 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
  * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
- * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,2020,2021, 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.
@@ -75,7 +75,7 @@ void integrateVVFirstStep(int64_t                   step,
                           bool                      bInitStep,
                           gmx::StartingBehavior     startingBehavior,
                           int                       nstglobalcomm,
-                          t_inputrec*               ir,
+                          const t_inputrec*         ir,
                           t_forcerec*               fr,
                           t_commrec*                cr,
                           t_state*                  state,
@@ -299,7 +299,7 @@ void integrateVVFirstStep(int64_t                   step,
 }
 
 void integrateVVSecondStep(int64_t                                  step,
-                           t_inputrec*                              ir,
+                           const t_inputrec*                        ir,
                            t_forcerec*                              fr,
                            t_commrec*                               cr,
                            t_state*                                 state,
index 8699f6b9ef706c426609ea840940cf8691facfed..9c56cf4927169b7af1b80f442c4836b2fae14b69 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
  * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
- * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,2020,2021, 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.
@@ -123,7 +123,7 @@ void integrateVVFirstStep(int64_t                   step,
                           bool                      bInitStep,
                           gmx::StartingBehavior     startingBehavior,
                           int                       nstglobalcomm,
-                          t_inputrec*               ir,
+                          const t_inputrec*         ir,
                           t_forcerec*               fr,
                           t_commrec*                cr,
                           t_state*                  state,
@@ -201,7 +201,7 @@ void integrateVVFirstStep(int64_t                   step,
  * \param[in]  wcycle            Wall-clock cycle counter.
  */
 void integrateVVSecondStep(int64_t                                  step,
-                           t_inputrec*                              ir,
+                           const t_inputrec*                        ir,
                            t_forcerec*                              fr,
                            t_commrec*                               cr,
                            t_state*                                 state,
index 86ea3279a8ae7ebc7e7aed3aae894f93034e5fd4..e6957b97fbb3128958a28a3da991727e01964ff5 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2019,2020,2021, 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.
@@ -210,8 +210,8 @@ public:
     IMDOutputProvider* outputProvider;
     //! Handles notifications to MdModules for checkpoint writing
     const MdModulesNotifier& mdModulesNotifier;
-    //! Contains user input mdp options.
-    t_inputrec* inputrec;
+    //! Contains user input mdp options. Note: The const-ness is casted away in a few instances, see #3854.
+    const t_inputrec* inputrec;
     //! The Interactive Molecular Dynamics session.
     ImdSession* imdSession;
     //! The pull work object.
index 1d786bc9468c54a2702db0c68632be294dc7ac66..94568b39dd0abdbae60537b1b8ca62f97b3c9be5 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-2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2011-2019,2020,2021, 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.
@@ -162,7 +162,8 @@ void gmx::LegacySimulator::do_md()
     // alias to avoid a large ripple of nearly useless changes.
     // t_inputrec is being replaced by IMdpOptionsProvider, so this
     // will go away eventually.
-    t_inputrec*  ir = inputrec;
+    const t_inputrec* ir = inputrec;
+
     int64_t      step, step_rel;
     double       t, t0 = ir->init_t;
     gmx_bool     bGStatEveryStep, bGStat, bCalcVir, bCalcEnerStep, bCalcEner;
@@ -265,9 +266,15 @@ void gmx::LegacySimulator::do_md()
     int*                fep_state = MASTER(cr) ? &state_global->fep_state : nullptr;
     gmx::ArrayRef<real> lambda    = MASTER(cr) ? state_global->lambda : gmx::ArrayRef<real>();
     initialize_lambdas(fplog, *ir, MASTER(cr), fep_state, lambda);
-    Update     upd(*ir, deform);
-    const bool doSimulatedAnnealing = initSimulatedAnnealing(ir, &upd);
-    const bool useReplicaExchange   = (replExParams.exchangeInterval > 0);
+    Update upd(*ir, deform);
+    bool   doSimulatedAnnealing = false;
+    {
+        // TODO: Avoid changing inputrec (#3854)
+        // Simulated annealing updates the reference temperature.
+        auto* nonConstInputrec = const_cast<t_inputrec*>(inputrec);
+        doSimulatedAnnealing   = initSimulatedAnnealing(nonConstInputrec, &upd);
+    }
+    const bool useReplicaExchange = (replExParams.exchangeInterval > 0);
 
     const t_fcdata& fcdata = *fr->fcdata;
 
@@ -905,7 +912,10 @@ void gmx::LegacySimulator::do_md()
 
         if (doSimulatedAnnealing)
         {
-            update_annealing_target_temp(ir, t, &upd);
+            // TODO: Avoid changing inputrec (#3854)
+            // Simulated annealing updates the reference temperature.
+            auto* nonConstInputrec = const_cast<t_inputrec*>(inputrec);
+            update_annealing_target_temp(nonConstInputrec, t, &upd);
         }
 
         /* Stop Center of Mass motion */
@@ -1238,9 +1248,20 @@ void gmx::LegacySimulator::do_md()
                actually move to the new state before outputting
                statistics, but if performing simulated tempering, we
                do update the velocities and the tau_t. */
-
-            lamnew = ExpandedEnsembleDynamics(
-                    fplog, ir, enerd, state, &MassQ, state->fep_state, state->dfhist, step, state->v.rvec_array(), mdatoms);
+            // TODO: Avoid changing inputrec (#3854)
+            // Simulated tempering updates the reference temperature.
+            // Expanded ensemble without simulated tempering does not change the inputrec.
+            auto* nonConstInputrec = const_cast<t_inputrec*>(inputrec);
+            lamnew                 = ExpandedEnsembleDynamics(fplog,
+                                              nonConstInputrec,
+                                              enerd,
+                                              state,
+                                              &MassQ,
+                                              state->fep_state,
+                                              state->dfhist,
+                                              step,
+                                              state->v.rvec_array(),
+                                              mdatoms);
             /* history is maintained in state->dfhist, but state_global is what is sent to trajectory and log output */
             if (MASTER(cr))
             {
index adc120d1b230f79ebc9ed2d721f7288746e623c3..38d0d6ef3373c918f67f4dbc2e437645a61a0348 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,2020,2021, 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.
@@ -141,7 +141,7 @@ using gmx::SimulationSignaller;
 
 void gmx::LegacySimulator::do_mimic()
 {
-    t_inputrec*       ir = inputrec;
+    const t_inputrec* ir = inputrec;
     int64_t           step, step_rel;
     double            t;
     bool              isLastStep               = false;
@@ -205,8 +205,13 @@ void gmx::LegacySimulator::do_mimic()
     }
 
     /* Settings for rerun */
-    ir->nstlist              = 1;
-    ir->nstcalcenergy        = 1;
+    {
+        // TODO: Avoid changing inputrec (#3854)
+        auto* nonConstInputrec               = const_cast<t_inputrec*>(inputrec);
+        nonConstInputrec->nstlist            = 1;
+        nonConstInputrec->nstcalcenergy      = 1;
+        nonConstInputrec->nstxout_compressed = 0;
+    }
     int        nstglobalcomm = 1;
     const bool bNS           = true;
 
@@ -215,10 +220,17 @@ void gmx::LegacySimulator::do_mimic()
         MimicCommunicator::init();
         auto nonConstGlobalTopology = const_cast<gmx_mtop_t*>(top_global);
         MimicCommunicator::sendInitData(nonConstGlobalTopology, state_global->x);
-        ir->nsteps = MimicCommunicator::getStepNumber();
+        // TODO: Avoid changing inputrec (#3854)
+        auto* nonConstInputrec   = const_cast<t_inputrec*>(inputrec);
+        nonConstInputrec->nsteps = MimicCommunicator::getStepNumber();
+    }
+    if (DOMAINDECOMP(cr))
+    {
+        // TODO: Avoid changing inputrec (#3854)
+        auto* nonConstInputrec = const_cast<t_inputrec*>(inputrec);
+        gmx_bcast(sizeof(ir->nsteps), &nonConstInputrec->nsteps, cr->mpi_comm_mygroup);
     }
 
-    ir->nstxout_compressed         = 0;
     const SimulationGroups* groups = &top_global->groups;
     {
         auto nonConstGlobalTopology                          = const_cast<gmx_mtop_t*>(top_global);
@@ -305,7 +317,6 @@ void gmx::LegacySimulator::do_mimic()
                             nullptr,
                             FALSE);
         shouldCheckNumberOfBondedInteractions = true;
-        gmx_bcast(sizeof(ir->nsteps), &ir->nsteps, cr->mpi_comm_mygroup);
     }
     else
     {
index c71f86ba1ad0a3064e5325ca66bd731c324ca2a6..988723c0c7c53230b84f6996f71e3f9157890262 100644 (file)
@@ -2979,7 +2979,11 @@ void LegacySimulator::do_steep()
     finish_em(cr, outf, walltime_accounting, wcycle);
 
     /* To print the actual number of steps we needed somewhere */
-    inputrec->nsteps = count;
+    {
+        // TODO: Avoid changing inputrec (#3854)
+        auto* nonConstInputrec   = const_cast<t_inputrec*>(inputrec);
+        nonConstInputrec->nsteps = count;
+    }
 
     walltime_accounting_set_nsteps_done(walltime_accounting, count);
 }
@@ -3118,7 +3122,11 @@ void LegacySimulator::do_nm()
     print_em_start(fplog, cr, walltime_accounting, wcycle, NM);
 
     /* fudge nr of steps to nr of atoms */
-    inputrec->nsteps = atom_index.size() * 2;
+    {
+        // TODO: Avoid changing inputrec (#3854)
+        auto* nonConstInputrec   = const_cast<t_inputrec*>(inputrec);
+        nonConstInputrec->nsteps = atom_index.size() * 2;
+    }
 
     if (bIsMaster)
     {
index ab974dd8d708639c967b56ffe97504ced60c461b..7d97dbe99e740c0957b00c91bbba4531b20a2185 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,2020,2021, 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.
@@ -173,7 +173,7 @@ void gmx::LegacySimulator::do_rerun()
     // alias to avoid a large ripple of nearly useless changes.
     // t_inputrec is being replaced by IMdpOptionsProvider, so this
     // will go away eventually.
-    t_inputrec*       ir = inputrec;
+    const t_inputrec* ir = inputrec;
     int64_t           step, step_rel;
     double            t;
     bool              isLastStep               = false;
@@ -268,12 +268,16 @@ void gmx::LegacySimulator::do_rerun()
     }
 
     /* Settings for rerun */
-    ir->nstlist              = 1;
-    ir->nstcalcenergy        = 1;
+    {
+        // TODO: Avoid changing inputrec (#3854)
+        auto* nonConstInputrec               = const_cast<t_inputrec*>(inputrec);
+        nonConstInputrec->nstlist            = 1;
+        nonConstInputrec->nstcalcenergy      = 1;
+        nonConstInputrec->nstxout_compressed = 0;
+    }
     int        nstglobalcomm = 1;
     const bool bNS           = true;
 
-    ir->nstxout_compressed         = 0;
     const SimulationGroups* groups = &top_global->groups;
     if (ir->eI == eiMimic)
     {
index 481a36196220ff8ac67c57dc87cf15447635be8d..f468251ef9c59baf9951f492fc21fa329f11eedc 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
  * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
- * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,2020,2021, 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.
@@ -434,8 +434,12 @@ void LegacySimulator::do_tpi()
      * inserted atoms located in the center of the sphere, so we need
      * a buffer of size of the sphere and molecule radius.
      */
-    inputrec->rlist = maxCutoff + 2 * inputrec->rtpi + 2 * molRadius;
-    fr->rlist       = inputrec->rlist;
+    {
+        // TODO: Avoid changing inputrec (#3854)
+        auto* nonConstInputrec  = const_cast<t_inputrec*>(inputrec);
+        nonConstInputrec->rlist = maxCutoff + 2 * inputrec->rtpi + 2 * molRadius;
+    }
+    fr->rlist = inputrec->rlist;
     fr->nbv->changePairlistRadii(inputrec->rlist, inputrec->rlist);
 
     ngid   = groups->groups[SimulationAtomGroupType::EnergyOutput].size();