From f1d63ad36e03dc74a4e7c0ecb9191b59080c212a Mon Sep 17 00:00:00 2001
From: Carlo Camilloni <carlo.camilloni@gmail.com>
Date: Wed, 22 Jun 2016 09:46:29 +0200
Subject: [PATCH] GMX2016 patch updated

---
 ...beta1.config => gromacs-2016-beta2.config} |  0
 .../src/gromacs/CMakeLists.txt                | 32 ++---------
 .../src/gromacs/CMakeLists.txt.preplumed      | 32 ++---------
 .../src/gromacs/mdlib/force.cpp               |  0
 .../src/gromacs/mdlib/force.cpp.preplumed     |  0
 .../src/gromacs/mdlib/minimize.cpp            | 54 +++++++++++++------
 .../src/gromacs/mdlib/minimize.cpp.preplumed  | 54 +++++++++++++------
 .../src/programs/mdrun/md.cpp                 |  4 ++
 .../src/programs/mdrun/md.cpp.preplumed       |  0
 .../src/programs/mdrun/mdrun.cpp              | 11 ++--
 .../src/programs/mdrun/mdrun.cpp.preplumed    | 11 ++--
 .../src/programs/mdrun/repl_ex.cpp            | 17 ++++--
 .../src/programs/mdrun/repl_ex.cpp.preplumed  | 17 ++++--
 .../src/programs/mdrun/runner.cpp             |  5 +-
 .../src/programs/mdrun/runner.cpp.preplumed   |  5 +-
 15 files changed, 134 insertions(+), 108 deletions(-)
 rename patches/{gromacs-2016-beta1.config => gromacs-2016-beta2.config} (100%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/gromacs/CMakeLists.txt (88%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/gromacs/CMakeLists.txt.preplumed (88%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/gromacs/mdlib/force.cpp (100%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/gromacs/mdlib/force.cpp.preplumed (100%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/gromacs/mdlib/minimize.cpp (98%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/gromacs/mdlib/minimize.cpp.preplumed (98%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/programs/mdrun/md.cpp (99%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/programs/mdrun/md.cpp.preplumed (100%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/programs/mdrun/mdrun.cpp (98%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/programs/mdrun/mdrun.cpp.preplumed (98%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/programs/mdrun/repl_ex.cpp (98%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/programs/mdrun/repl_ex.cpp.preplumed (98%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/programs/mdrun/runner.cpp (99%)
 rename patches/{gromacs-2016-beta1.diff => gromacs-2016-beta2.diff}/src/programs/mdrun/runner.cpp.preplumed (99%)

diff --git a/patches/gromacs-2016-beta1.config b/patches/gromacs-2016-beta2.config
similarity index 100%
rename from patches/gromacs-2016-beta1.config
rename to patches/gromacs-2016-beta2.config
diff --git a/patches/gromacs-2016-beta1.diff/src/gromacs/CMakeLists.txt b/patches/gromacs-2016-beta2.diff/src/gromacs/CMakeLists.txt
similarity index 88%
rename from patches/gromacs-2016-beta1.diff/src/gromacs/CMakeLists.txt
rename to patches/gromacs-2016-beta2.diff/src/gromacs/CMakeLists.txt
index 2f8a59296..a9f3b4fd3 100644
--- a/patches/gromacs-2016-beta1.diff/src/gromacs/CMakeLists.txt
+++ b/patches/gromacs-2016-beta2.diff/src/gromacs/CMakeLists.txt
@@ -71,35 +71,6 @@ function (gmx_write_installed_header_list)
     file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/installed-headers.txt "${_list}")
 endfunction()
 
-if(GMX_USE_TNG)
-    option(GMX_EXTERNAL_TNG "Use external TNG instead of compiling the version shipped with GROMACS."
-           OFF)
-    # Detect TNG if GMX_EXTERNAL_TNG is explicitly ON
-    if(GMX_EXTERNAL_TNG)
-        find_package(TNG_IO 1.6.0)
-        if(NOT TNG_IO_FOUND)
-            message(FATAL_ERROR
-                "TNG >= 1.6.0 not found. "
-                "You can set GMX_EXTERNAL_TNG=OFF to compile TNG.")
-        endif()
-        include_directories(SYSTEM ${TNG_IO_INCLUDE_DIRS})
-    endif()
-    if(NOT GMX_EXTERNAL_TNG)
-        include(${CMAKE_SOURCE_DIR}/src/external/tng_io/BuildTNG.cmake)
-        tng_get_source_list(TNG_SOURCES TNG_IO_DEFINITIONS)
-        list(APPEND LIBGROMACS_SOURCES ${TNG_SOURCES})
-        tng_set_source_properties(WITH_ZLIB ${HAVE_ZLIB})
-
-        if (HAVE_ZLIB)
-            list(APPEND GMX_EXTRA_LIBRARIES ${ZLIB_LIBRARIES})
-            include_directories(SYSTEM ${ZLIB_INCLUDE_DIRS})
-        endif()
-    endif()
-else()
-    # We still need to get tng/tng_io_fwd.h from somewhere!
-    include_directories(BEFORE ${CMAKE_SOURCE_DIR}/src/external/tng_io/include)
-endif()
-
 add_subdirectory(gmxlib)
 add_subdirectory(mdlib)
 add_subdirectory(listed-forces)
@@ -152,6 +123,9 @@ list(APPEND LIBGROMACS_SOURCES ${GMXLIB_SOURCES} ${MDLIB_SOURCES} ${PROPERTY_SOU
 tmpi_get_source_list(THREAD_MPI_SOURCES ${CMAKE_SOURCE_DIR}/src/external/thread_mpi/src)
 list(APPEND LIBGROMACS_SOURCES ${THREAD_MPI_SOURCES})
 
+list(APPEND LIBGROMACS_SOURCES ${TNG_SOURCES})
+tng_set_source_properties(WITH_ZLIB ${HAVE_ZLIB})
+
 configure_file(version.h.cmakein version.h)
 gmx_install_headers(
     analysisdata.h
diff --git a/patches/gromacs-2016-beta1.diff/src/gromacs/CMakeLists.txt.preplumed b/patches/gromacs-2016-beta2.diff/src/gromacs/CMakeLists.txt.preplumed
similarity index 88%
rename from patches/gromacs-2016-beta1.diff/src/gromacs/CMakeLists.txt.preplumed
rename to patches/gromacs-2016-beta2.diff/src/gromacs/CMakeLists.txt.preplumed
index edc051fe8..ab9d64df4 100644
--- a/patches/gromacs-2016-beta1.diff/src/gromacs/CMakeLists.txt.preplumed
+++ b/patches/gromacs-2016-beta2.diff/src/gromacs/CMakeLists.txt.preplumed
@@ -69,35 +69,6 @@ function (gmx_write_installed_header_list)
     file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/installed-headers.txt "${_list}")
 endfunction()
 
-if(GMX_USE_TNG)
-    option(GMX_EXTERNAL_TNG "Use external TNG instead of compiling the version shipped with GROMACS."
-           OFF)
-    # Detect TNG if GMX_EXTERNAL_TNG is explicitly ON
-    if(GMX_EXTERNAL_TNG)
-        find_package(TNG_IO 1.6.0)
-        if(NOT TNG_IO_FOUND)
-            message(FATAL_ERROR
-                "TNG >= 1.6.0 not found. "
-                "You can set GMX_EXTERNAL_TNG=OFF to compile TNG.")
-        endif()
-        include_directories(SYSTEM ${TNG_IO_INCLUDE_DIRS})
-    endif()
-    if(NOT GMX_EXTERNAL_TNG)
-        include(${CMAKE_SOURCE_DIR}/src/external/tng_io/BuildTNG.cmake)
-        tng_get_source_list(TNG_SOURCES TNG_IO_DEFINITIONS)
-        list(APPEND LIBGROMACS_SOURCES ${TNG_SOURCES})
-        tng_set_source_properties(WITH_ZLIB ${HAVE_ZLIB})
-
-        if (HAVE_ZLIB)
-            list(APPEND GMX_EXTRA_LIBRARIES ${ZLIB_LIBRARIES})
-            include_directories(SYSTEM ${ZLIB_INCLUDE_DIRS})
-        endif()
-    endif()
-else()
-    # We still need to get tng/tng_io_fwd.h from somewhere!
-    include_directories(BEFORE ${CMAKE_SOURCE_DIR}/src/external/tng_io/include)
-endif()
-
 add_subdirectory(gmxlib)
 add_subdirectory(mdlib)
 add_subdirectory(listed-forces)
@@ -150,6 +121,9 @@ list(APPEND LIBGROMACS_SOURCES ${GMXLIB_SOURCES} ${MDLIB_SOURCES} ${PROPERTY_SOU
 tmpi_get_source_list(THREAD_MPI_SOURCES ${CMAKE_SOURCE_DIR}/src/external/thread_mpi/src)
 list(APPEND LIBGROMACS_SOURCES ${THREAD_MPI_SOURCES})
 
+list(APPEND LIBGROMACS_SOURCES ${TNG_SOURCES})
+tng_set_source_properties(WITH_ZLIB ${HAVE_ZLIB})
+
 configure_file(version.h.cmakein version.h)
 gmx_install_headers(
     analysisdata.h
diff --git a/patches/gromacs-2016-beta1.diff/src/gromacs/mdlib/force.cpp b/patches/gromacs-2016-beta2.diff/src/gromacs/mdlib/force.cpp
similarity index 100%
rename from patches/gromacs-2016-beta1.diff/src/gromacs/mdlib/force.cpp
rename to patches/gromacs-2016-beta2.diff/src/gromacs/mdlib/force.cpp
diff --git a/patches/gromacs-2016-beta1.diff/src/gromacs/mdlib/force.cpp.preplumed b/patches/gromacs-2016-beta2.diff/src/gromacs/mdlib/force.cpp.preplumed
similarity index 100%
rename from patches/gromacs-2016-beta1.diff/src/gromacs/mdlib/force.cpp.preplumed
rename to patches/gromacs-2016-beta2.diff/src/gromacs/mdlib/force.cpp.preplumed
diff --git a/patches/gromacs-2016-beta1.diff/src/gromacs/mdlib/minimize.cpp b/patches/gromacs-2016-beta2.diff/src/gromacs/mdlib/minimize.cpp
similarity index 98%
rename from patches/gromacs-2016-beta1.diff/src/gromacs/mdlib/minimize.cpp
rename to patches/gromacs-2016-beta2.diff/src/gromacs/mdlib/minimize.cpp
index 3ce783281..b60b80991 100644
--- a/patches/gromacs-2016-beta1.diff/src/gromacs/mdlib/minimize.cpp
+++ b/patches/gromacs-2016-beta2.diff/src/gromacs/mdlib/minimize.cpp
@@ -614,8 +614,10 @@ static void write_em_traj(FILE *fplog, t_commrec *cr,
     }
 }
 
-//! Do one minimization step
-static void do_em_step(t_commrec *cr, t_inputrec *ir, t_mdatoms *md,
+//! \brief Do one minimization step
+//
+// \returns true when the step succeeded, false when a constraint error occurred
+static bool do_em_step(t_commrec *cr, t_inputrec *ir, t_mdatoms *md,
                        gmx_bool bMolPBC,
                        em_state_t *ems1, real a, rvec *f, em_state_t *ems2,
                        gmx_constr_t constr, gmx_localtop_t *top,
@@ -630,6 +632,8 @@ static void do_em_step(t_commrec *cr, t_inputrec *ir, t_mdatoms *md,
     real     dvdl_constr;
     int      nthreads gmx_unused;
 
+    bool     validStep = true;
+
     s1 = &ems1->s;
     s2 = &ems2->s;
 
@@ -744,13 +748,23 @@ static void do_em_step(t_commrec *cr, t_inputrec *ir, t_mdatoms *md,
     {
         wallcycle_start(wcycle, ewcCONSTR);
         dvdl_constr = 0;
-        constrain(NULL, TRUE, TRUE, constr, &top->idef,
-                  ir, cr, count, 0, 1.0, md,
-                  s1->x, s2->x, NULL, bMolPBC, s2->box,
-                  s2->lambda[efptBONDED], &dvdl_constr,
-                  NULL, NULL, nrnb, econqCoord);
+        validStep   =
+            constrain(NULL, TRUE, TRUE, constr, &top->idef,
+                      ir, cr, count, 0, 1.0, md,
+                      s1->x, s2->x, NULL, bMolPBC, s2->box,
+                      s2->lambda[efptBONDED], &dvdl_constr,
+                      NULL, NULL, nrnb, econqCoord);
         wallcycle_stop(wcycle, ewcCONSTR);
+
+        // We should move this check to the different minimizers
+        if (!validStep && ir->eI != eiSteep)
+        {
+            gmx_fatal(FARGS, "The coordinates could not be constrained. Minimizer '%s' can not handle constraint failures, use minimizer '%s' before using '%s'.",
+                      EI(ir->eI), EI(eiSteep), EI(ir->eI));
+        }
     }
+
+    return validStep;
 }
 
 //! Prepare EM for using domain decomposition parallellization
@@ -2635,18 +2649,28 @@ double do_steep(FILE *fplog, t_commrec *cr,
         bAbort = (nsteps >= 0) && (count == nsteps);
 
         /* set new coordinates, except for first step */
+        bool validStep = true;
         if (count > 0)
         {
-            do_em_step(cr, inputrec, mdatoms, fr->bMolPBC,
-                       s_min, stepsize, s_min->f, s_try,
-                       constr, top, nrnb, wcycle, count);
+            validStep =
+                do_em_step(cr, inputrec, mdatoms, fr->bMolPBC,
+                           s_min, stepsize, s_min->f, s_try,
+                           constr, top, nrnb, wcycle, count);
         }
 
-        evaluate_energy(fplog, cr,
-                        top_global, s_try, top,
-                        inputrec, nrnb, wcycle, gstat,
-                        vsite, constr, fcd, graph, mdatoms, fr,
-                        mu_tot, enerd, vir, pres, count, count == 0);
+        if (validStep)
+        {
+            evaluate_energy(fplog, cr,
+                            top_global, s_try, top,
+                            inputrec, nrnb, wcycle, gstat,
+                            vsite, constr, fcd, graph, mdatoms, fr,
+                            mu_tot, enerd, vir, pres, count, count == 0);
+        }
+        else
+        {
+            // Signal constraint error during stepping with energy=inf
+            s_try->epot = std::numeric_limits<real>::infinity();
+        }
 
         if (MASTER(cr))
         {
diff --git a/patches/gromacs-2016-beta1.diff/src/gromacs/mdlib/minimize.cpp.preplumed b/patches/gromacs-2016-beta2.diff/src/gromacs/mdlib/minimize.cpp.preplumed
similarity index 98%
rename from patches/gromacs-2016-beta1.diff/src/gromacs/mdlib/minimize.cpp.preplumed
rename to patches/gromacs-2016-beta2.diff/src/gromacs/mdlib/minimize.cpp.preplumed
index 5a2056940..55200ce0a 100644
--- a/patches/gromacs-2016-beta1.diff/src/gromacs/mdlib/minimize.cpp.preplumed
+++ b/patches/gromacs-2016-beta2.diff/src/gromacs/mdlib/minimize.cpp.preplumed
@@ -570,8 +570,10 @@ static void write_em_traj(FILE *fplog, t_commrec *cr,
     }
 }
 
-//! Do one minimization step
-static void do_em_step(t_commrec *cr, t_inputrec *ir, t_mdatoms *md,
+//! \brief Do one minimization step
+//
+// \returns true when the step succeeded, false when a constraint error occurred
+static bool do_em_step(t_commrec *cr, t_inputrec *ir, t_mdatoms *md,
                        gmx_bool bMolPBC,
                        em_state_t *ems1, real a, rvec *f, em_state_t *ems2,
                        gmx_constr_t constr, gmx_localtop_t *top,
@@ -586,6 +588,8 @@ static void do_em_step(t_commrec *cr, t_inputrec *ir, t_mdatoms *md,
     real     dvdl_constr;
     int      nthreads gmx_unused;
 
+    bool     validStep = true;
+
     s1 = &ems1->s;
     s2 = &ems2->s;
 
@@ -700,13 +704,23 @@ static void do_em_step(t_commrec *cr, t_inputrec *ir, t_mdatoms *md,
     {
         wallcycle_start(wcycle, ewcCONSTR);
         dvdl_constr = 0;
-        constrain(NULL, TRUE, TRUE, constr, &top->idef,
-                  ir, cr, count, 0, 1.0, md,
-                  s1->x, s2->x, NULL, bMolPBC, s2->box,
-                  s2->lambda[efptBONDED], &dvdl_constr,
-                  NULL, NULL, nrnb, econqCoord);
+        validStep   =
+            constrain(NULL, TRUE, TRUE, constr, &top->idef,
+                      ir, cr, count, 0, 1.0, md,
+                      s1->x, s2->x, NULL, bMolPBC, s2->box,
+                      s2->lambda[efptBONDED], &dvdl_constr,
+                      NULL, NULL, nrnb, econqCoord);
         wallcycle_stop(wcycle, ewcCONSTR);
+
+        // We should move this check to the different minimizers
+        if (!validStep && ir->eI != eiSteep)
+        {
+            gmx_fatal(FARGS, "The coordinates could not be constrained. Minimizer '%s' can not handle constraint failures, use minimizer '%s' before using '%s'.",
+                      EI(ir->eI), EI(eiSteep), EI(ir->eI));
+        }
     }
+
+    return validStep;
 }
 
 //! Prepare EM for using domain decomposition parallellization
@@ -2561,18 +2575,28 @@ double do_steep(FILE *fplog, t_commrec *cr,
         bAbort = (nsteps >= 0) && (count == nsteps);
 
         /* set new coordinates, except for first step */
+        bool validStep = true;
         if (count > 0)
         {
-            do_em_step(cr, inputrec, mdatoms, fr->bMolPBC,
-                       s_min, stepsize, s_min->f, s_try,
-                       constr, top, nrnb, wcycle, count);
+            validStep =
+                do_em_step(cr, inputrec, mdatoms, fr->bMolPBC,
+                           s_min, stepsize, s_min->f, s_try,
+                           constr, top, nrnb, wcycle, count);
         }
 
-        evaluate_energy(fplog, cr,
-                        top_global, s_try, top,
-                        inputrec, nrnb, wcycle, gstat,
-                        vsite, constr, fcd, graph, mdatoms, fr,
-                        mu_tot, enerd, vir, pres, count, count == 0);
+        if (validStep)
+        {
+            evaluate_energy(fplog, cr,
+                            top_global, s_try, top,
+                            inputrec, nrnb, wcycle, gstat,
+                            vsite, constr, fcd, graph, mdatoms, fr,
+                            mu_tot, enerd, vir, pres, count, count == 0);
+        }
+        else
+        {
+            // Signal constraint error during stepping with energy=inf
+            s_try->epot = std::numeric_limits<real>::infinity();
+        }
 
         if (MASTER(cr))
         {
diff --git a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/md.cpp b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/md.cpp
similarity index 99%
rename from patches/gromacs-2016-beta1.diff/src/programs/mdrun/md.cpp
rename to patches/gromacs-2016-beta2.diff/src/programs/mdrun/md.cpp
index 2bd872565..19cea9f69 100644
--- a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/md.cpp
+++ b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/md.cpp
@@ -1165,6 +1165,8 @@ double gmx::do_md(FILE *fplog, t_commrec *cr, int nfile, const t_filenm fnm[],
             /* PLUMED */
             plumedNeedsEnergy=0;
             if(plumedswitch){
+              int pversion=0;
+              plumed_cmd(plumedmain,"getApiVersion",&pversion);
               long int lstep=step; plumed_cmd(plumedmain,"setStepLong",&lstep);
               plumed_cmd(plumedmain,"setPositions",&state->x[0][0]);
               plumed_cmd(plumedmain,"setMasses",&mdatoms->massT[0]);
@@ -1172,6 +1174,8 @@ double gmx::do_md(FILE *fplog, t_commrec *cr, int nfile, const t_filenm fnm[],
               plumed_cmd(plumedmain,"setBox",&state->box[0][0]);
               plumed_cmd(plumedmain,"prepareCalc",NULL);
               plumed_cmd(plumedmain,"setStopFlag",&plumedWantsToStop);
+              int checkp=0; if(bCPT) checkp=1;
+              if(pversion>3) plumed_cmd(plumedmain,"doCheckPoint",&checkp);
               plumed_cmd(plumedmain,"setForces",&f[0][0]);
               plumed_cmd(plumedmain,"isEnergyNeeded",&plumedNeedsEnergy);
               clear_mat(plumed_vir);
diff --git a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/md.cpp.preplumed b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/md.cpp.preplumed
similarity index 100%
rename from patches/gromacs-2016-beta1.diff/src/programs/mdrun/md.cpp.preplumed
rename to patches/gromacs-2016-beta2.diff/src/programs/mdrun/md.cpp.preplumed
diff --git a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/mdrun.cpp b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/mdrun.cpp
similarity index 98%
rename from patches/gromacs-2016-beta1.diff/src/programs/mdrun/mdrun.cpp
rename to patches/gromacs-2016-beta2.diff/src/programs/mdrun/mdrun.cpp
index 55f3529fa..3efc27c46 100644
--- a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/mdrun.cpp
+++ b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/mdrun.cpp
@@ -141,10 +141,11 @@ int gmx_mdrun(int argc, char *argv[])
         "functions is read using the [TT]-tablep[tt] option.[PAR]",
         "When tabulated bonded functions are present in the topology,",
         "interaction functions are read using the [TT]-tableb[tt] option.",
-        "For each different tabulated interaction type the table file name is",
-        "modified in a different way: before the file extension an underscore is",
-        "appended, then a 'b' for bonds, an 'a' for angles or a 'd' for dihedrals",
-        "and finally the table number of the interaction type.[PAR]",
+        "For each different tabulated interaction type used, a table file name must",
+        "be given. For the topology to work, a file name given here must match a",
+        "character sequence before the file extension. That sequence is: an underscore,",
+        "then a 'b' for bonds, an 'a' for angles or a 'd' for dihedrals,",
+        "and finally the matching table number index used in the topology.[PAR]",
         "The options [TT]-px[tt] and [TT]-pf[tt] are used for writing pull COM",
         "coordinates and forces when pulling is selected",
         "in the [REF].mdp[ref] file.[PAR]",
@@ -246,7 +247,7 @@ int gmx_mdrun(int argc, char *argv[])
         { efXVG, "-field",  "field",    ffOPTWR },
         { efXVG, "-table",  "table",    ffOPTRD },
         { efXVG, "-tablep", "tablep",   ffOPTRD },
-        { efXVG, "-tableb", "table",    ffOPTRD },
+        { efXVG, "-tableb", "table",    ffOPTRDMULT },
         { efTRX, "-rerun",  "rerun",    ffOPTRD },
         { efXVG, "-tpi",    "tpi",      ffOPTWR },
         { efXVG, "-tpid",   "tpidist",  ffOPTWR },
diff --git a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/mdrun.cpp.preplumed b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/mdrun.cpp.preplumed
similarity index 98%
rename from patches/gromacs-2016-beta1.diff/src/programs/mdrun/mdrun.cpp.preplumed
rename to patches/gromacs-2016-beta2.diff/src/programs/mdrun/mdrun.cpp.preplumed
index 9458dfbf9..e56c4a145 100644
--- a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/mdrun.cpp.preplumed
+++ b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/mdrun.cpp.preplumed
@@ -134,10 +134,11 @@ int gmx_mdrun(int argc, char *argv[])
         "functions is read using the [TT]-tablep[tt] option.[PAR]",
         "When tabulated bonded functions are present in the topology,",
         "interaction functions are read using the [TT]-tableb[tt] option.",
-        "For each different tabulated interaction type the table file name is",
-        "modified in a different way: before the file extension an underscore is",
-        "appended, then a 'b' for bonds, an 'a' for angles or a 'd' for dihedrals",
-        "and finally the table number of the interaction type.[PAR]",
+        "For each different tabulated interaction type used, a table file name must",
+        "be given. For the topology to work, a file name given here must match a",
+        "character sequence before the file extension. That sequence is: an underscore,",
+        "then a 'b' for bonds, an 'a' for angles or a 'd' for dihedrals,",
+        "and finally the matching table number index used in the topology.[PAR]",
         "The options [TT]-px[tt] and [TT]-pf[tt] are used for writing pull COM",
         "coordinates and forces when pulling is selected",
         "in the [REF].mdp[ref] file.[PAR]",
@@ -239,7 +240,7 @@ int gmx_mdrun(int argc, char *argv[])
         { efXVG, "-field",  "field",    ffOPTWR },
         { efXVG, "-table",  "table",    ffOPTRD },
         { efXVG, "-tablep", "tablep",   ffOPTRD },
-        { efXVG, "-tableb", "table",    ffOPTRD },
+        { efXVG, "-tableb", "table",    ffOPTRDMULT },
         { efTRX, "-rerun",  "rerun",    ffOPTRD },
         { efXVG, "-tpi",    "tpi",      ffOPTWR },
         { efXVG, "-tpid",   "tpidist",  ffOPTWR },
diff --git a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/repl_ex.cpp b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/repl_ex.cpp
similarity index 98%
rename from patches/gromacs-2016-beta1.diff/src/programs/mdrun/repl_ex.cpp
rename to patches/gromacs-2016-beta2.diff/src/programs/mdrun/repl_ex.cpp
index 2b870b96d..70d9f30b8 100644
--- a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/repl_ex.cpp
+++ b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/repl_ex.cpp
@@ -976,6 +976,8 @@ test_for_replica_exchange(FILE                 *fplog,
         pind[i] = re->ind[i];
     }
 
+    rng.restart( step, 0 );
+
     /* PLUMED */
     int plumed_test_exchange_pattern=0;
     /* END PLUMED */
@@ -985,10 +987,13 @@ test_for_replica_exchange(FILE                 *fplog,
         /* multiple random switch exchange */
         int nself = 0;
 
-        rng.restart( step, 0 );
 
         for (i = 0; i < re->nex + nself; i++)
         {
+            // For now this is superfluous, but just in case we ever add more
+            // calls in different branches it is safer to always reset the distribution.
+            uniformNreplDist.reset();
+
             /* randomly select a pair  */
             /* in theory, could reduce this by identifying only which switches had a nonneglibible
                probability of occurring (log p > -100) and only operate on those switches */
@@ -1033,7 +1038,10 @@ test_for_replica_exchange(FILE                 *fplog,
                 {
                     prob[0] = exp(-delta);
                 }
-                /* roll a number to determine if accepted */
+                // roll a number to determine if accepted. For now it is superfluous to
+                // reset, but just in case we ever add more calls in different branches
+                // it is safer to always reset the distribution.
+                uniformRealDist.reset();
                 bEx[0] = uniformRealDist(rng) < prob[0];
             }
             re->prob_sum[0] += prob[0];
@@ -1116,7 +1124,10 @@ test_for_replica_exchange(FILE                 *fplog,
                     {
                         prob[i] = exp(-delta);
                     }
-                    /* roll a number to determine if accepted */
+                    // roll a number to determine if accepted. For now it is superfluous to
+                    // reset, but just in case we ever add more calls in different branches
+                    // it is safer to always reset the distribution.
+                    uniformRealDist.reset();
                     bEx[i] = uniformRealDist(rng) < prob[i];
                 }
                 re->prob_sum[i] += prob[i];
diff --git a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/repl_ex.cpp.preplumed b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/repl_ex.cpp.preplumed
similarity index 98%
rename from patches/gromacs-2016-beta1.diff/src/programs/mdrun/repl_ex.cpp.preplumed
rename to patches/gromacs-2016-beta2.diff/src/programs/mdrun/repl_ex.cpp.preplumed
index ab12fc4f1..a3d89573f 100644
--- a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/repl_ex.cpp.preplumed
+++ b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/repl_ex.cpp.preplumed
@@ -961,15 +961,20 @@ test_for_replica_exchange(FILE                 *fplog,
         pind[i] = re->ind[i];
     }
 
+    rng.restart( step, 0 );
+
     if (bMultiEx)
     {
         /* multiple random switch exchange */
         int nself = 0;
 
-        rng.restart( step, 0 );
 
         for (i = 0; i < re->nex + nself; i++)
         {
+            // For now this is superfluous, but just in case we ever add more
+            // calls in different branches it is safer to always reset the distribution.
+            uniformNreplDist.reset();
+
             /* randomly select a pair  */
             /* in theory, could reduce this by identifying only which switches had a nonneglibible
                probability of occurring (log p > -100) and only operate on those switches */
@@ -1014,7 +1019,10 @@ test_for_replica_exchange(FILE                 *fplog,
                 {
                     prob[0] = exp(-delta);
                 }
-                /* roll a number to determine if accepted */
+                // roll a number to determine if accepted. For now it is superfluous to
+                // reset, but just in case we ever add more calls in different branches
+                // it is safer to always reset the distribution.
+                uniformRealDist.reset();
                 bEx[0] = uniformRealDist(rng) < prob[0];
             }
             re->prob_sum[0] += prob[0];
@@ -1060,7 +1068,10 @@ test_for_replica_exchange(FILE                 *fplog,
                     {
                         prob[i] = exp(-delta);
                     }
-                    /* roll a number to determine if accepted */
+                    // roll a number to determine if accepted. For now it is superfluous to
+                    // reset, but just in case we ever add more calls in different branches
+                    // it is safer to always reset the distribution.
+                    uniformRealDist.reset();
                     bEx[i] = uniformRealDist(rng) < prob[i];
                 }
                 re->prob_sum[i] += prob[i];
diff --git a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/runner.cpp b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/runner.cpp
similarity index 99%
rename from patches/gromacs-2016-beta1.diff/src/programs/mdrun/runner.cpp
rename to patches/gromacs-2016-beta2.diff/src/programs/mdrun/runner.cpp
index d642a42e2..b8550c39e 100644
--- a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/runner.cpp
+++ b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/runner.cpp
@@ -989,7 +989,8 @@ int mdrunner(gmx_hw_opt_t *hw_opt,
                         cr, ddxyz, &npme,
                         inputrec, state, &bReadEkin,
                         (Flags & MD_APPENDFILES),
-                        (Flags & MD_APPENDFILESSET));
+                        (Flags & MD_APPENDFILESSET),
+                        (Flags & MD_REPRODUCIBLE));
 
         if (bReadEkin)
         {
@@ -1159,7 +1160,7 @@ int mdrunner(gmx_hw_opt_t *hw_opt,
         init_forcerec(fplog, fr, fcd, inputrec, mtop, cr, box,
                       opt2fn("-table", nfile, fnm),
                       opt2fn("-tablep", nfile, fnm),
-                      opt2fn("-tableb", nfile, fnm),
+                      getFilenm("-tableb", nfile, fnm),
                       nbpu_opt,
                       FALSE,
                       pforce);
diff --git a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/runner.cpp.preplumed b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/runner.cpp.preplumed
similarity index 99%
rename from patches/gromacs-2016-beta1.diff/src/programs/mdrun/runner.cpp.preplumed
rename to patches/gromacs-2016-beta2.diff/src/programs/mdrun/runner.cpp.preplumed
index 3294776ff..ddc5895c9 100644
--- a/patches/gromacs-2016-beta1.diff/src/programs/mdrun/runner.cpp.preplumed
+++ b/patches/gromacs-2016-beta2.diff/src/programs/mdrun/runner.cpp.preplumed
@@ -983,7 +983,8 @@ int mdrunner(gmx_hw_opt_t *hw_opt,
                         cr, ddxyz, &npme,
                         inputrec, state, &bReadEkin,
                         (Flags & MD_APPENDFILES),
-                        (Flags & MD_APPENDFILESSET));
+                        (Flags & MD_APPENDFILESSET),
+                        (Flags & MD_REPRODUCIBLE));
 
         if (bReadEkin)
         {
@@ -1153,7 +1154,7 @@ int mdrunner(gmx_hw_opt_t *hw_opt,
         init_forcerec(fplog, fr, fcd, inputrec, mtop, cr, box,
                       opt2fn("-table", nfile, fnm),
                       opt2fn("-tablep", nfile, fnm),
-                      opt2fn("-tableb", nfile, fnm),
+                      getFilenm("-tableb", nfile, fnm),
                       nbpu_opt,
                       FALSE,
                       pforce);
-- 
GitLab