From b83193a7f1c1437838735e7fdd3c5044ebf97b13 Mon Sep 17 00:00:00 2001
From: Giovanni Bussi <giovanni.bussi@gmail.com>
Date: Thu, 26 Sep 2013 10:30:29 +0200
Subject: [PATCH] Improved hrex patch

This now allows to activate hrex from the command line:
mdrun -hrex
---
 patches/gromacs-4.6.3.config                    |  3 +--
 patches/gromacs-4.6.3.diff/src/kernel/md.c      |  9 +++++++--
 patches/gromacs-4.6.3.diff/src/kernel/mdrun.c   | 16 ++++++++++++++++
 patches/gromacs-4.6.3.diff/src/kernel/repl_ex.c |  8 +++++++-
 4 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/patches/gromacs-4.6.3.config b/patches/gromacs-4.6.3.config
index 6346748e7..57f60f95b 100644
--- a/patches/gromacs-4.6.3.config
+++ b/patches/gromacs-4.6.3.config
@@ -14,8 +14,7 @@ Implementation described in:
 
 To use it:
 * Prepare separate topologies (topol0.tpr, topol1.tpr, etc)
-* Set environment variable PLUMED_HREX (export PLUMED_HREX=1)
-* Run a normal replica exchange with gromacs
+* Run a normal replica exchange with gromacs adding the flag "-hrex" on the command line
 
 Suggested checks:
 * Try with several identical force fields and different seed/starting point
diff --git a/patches/gromacs-4.6.3.diff/src/kernel/md.c b/patches/gromacs-4.6.3.diff/src/kernel/md.c
index ec5ffd289..67b79a9d3 100644
--- a/patches/gromacs-4.6.3.diff/src/kernel/md.c
+++ b/patches/gromacs-4.6.3.diff/src/kernel/md.c
@@ -99,6 +99,11 @@ extern int    plumedswitch;
 extern plumed plumedmain;
 /* END PLUMED */
 
+/* PLUMED HREX */
+extern int plumed_hrex;
+/* END PLUMED HREX */
+
+
 #ifdef GMX_LIB_MPI
 #include <mpi.h>
 #endif
@@ -1139,9 +1144,9 @@ double do_md(FILE *fplog, t_commrec *cr, int nfile, const t_filenm fnm[],
 
         GMX_MPE_LOG(ev_timestep2);
 
+
         gmx_bool bHREX;
-        bHREX= repl_ex_nst > 0 && (step>0) && !bLastStep && do_per_step(step,repl_ex_nst)
-            && getenv("PLUMED_HREX");
+        bHREX= repl_ex_nst > 0 && (step>0) && !bLastStep && do_per_step(step,repl_ex_nst) && plumed_hrex;
 
 /* Hamiltonian Replica Exchange */
         if(plumedswitch) if(bHREX){
diff --git a/patches/gromacs-4.6.3.diff/src/kernel/mdrun.c b/patches/gromacs-4.6.3.diff/src/kernel/mdrun.c
index ba301007d..9edfb4861 100644
--- a/patches/gromacs-4.6.3.diff/src/kernel/mdrun.c
+++ b/patches/gromacs-4.6.3.diff/src/kernel/mdrun.c
@@ -64,6 +64,11 @@ int    plumedswitch;
 plumed plumedmain;
 /* END PLUMED */
 
+/* PLUMED HREX */
+int plumed_hrex;
+/* END PLUMED HREX */
+
+
 int cmain(int argc, char *argv[])
 {
     const char   *desc[] = {
@@ -540,6 +545,8 @@ int cmain(int argc, char *argv[])
           "Number of random exchanges to carry out each exchange interval (N^3 is one suggestion).  -nex zero or not specified gives neighbor replica exchange." },
         { "-reseed",  FALSE, etINT, {&repl_ex_seed},
           "Seed for replica exchange, -1 is generate a seed" },
+        { "-hrex",  FALSE, etBOOL, {&plumed_hrex},
+          "Enable hamiltonian replica exchange" },
         { "-rerunvsite", FALSE, etBOOL, {&bRerunVSite},
           "HIDDENRecalculate virtual site coordinates with [TT]-rerun[tt]" },
         { "-ionize",  FALSE, etBOOL, {&bIonize},
@@ -764,6 +771,15 @@ int cmain(int argc, char *argv[])
       plumed_cmd(plumedmain,"setPlumedDat",ftp2fn(efDAT,NFILE,fnm));
       plumedswitch=1;
     }
+    /* PLUMED HREX*/
+    if(getenv("PLUMED_HREX")) plumed_hrex=1;
+    if(plumed_hrex){
+      if(!plumedswitch)  gmx_fatal(FARGS,"-hrex (or PLUMED_HREX) requires -plumed");
+      if(repl_ex_nst==0) gmx_fatal(FARGS,"-hrex (or PLUMED_HREX) replica exchange");
+      if(repl_ex_nex!=0) gmx_fatal(FARGS,"-hrex (or PLUMED_HREX) not compatible with -nex");
+    }
+    /* END PLUMED HREX */
+
     /* END PLUMED */
 
     rc = mdrunner(&hw_opt, fplog, cr, NFILE, fnm, oenv, bVerbose, bCompact,
diff --git a/patches/gromacs-4.6.3.diff/src/kernel/repl_ex.c b/patches/gromacs-4.6.3.diff/src/kernel/repl_ex.c
index 2f5f99159..e3a1078b6 100644
--- a/patches/gromacs-4.6.3.diff/src/kernel/repl_ex.c
+++ b/patches/gromacs-4.6.3.diff/src/kernel/repl_ex.c
@@ -59,6 +59,10 @@ extern int    plumedswitch;
 extern plumed plumedmain;
 /* END PLUMED */
 
+/* PLUMED HREX */
+extern int plumed_hrex;
+/* END PLUMED HREX */
+
 #define PROBABILITYCUTOFF 100
 /* we don't bother evaluating if events are more rare than exp(-100) = 3.7x10^-44 */
 
@@ -889,7 +893,7 @@ static real calc_delta(FILE *fplog, gmx_bool bPrint, struct gmx_repl_ex *re, int
 /* PLUMED */
 /* this is necessary because with plumed HREX the energy contribution is
    already taken into account */
-    if(getenv("PLUMED_HREX")) delta=0.0;
+    if(plumed_hrex) delta=0.0;
 /* END PLUMED */
 
     if (re->bNPT)
@@ -1003,6 +1007,8 @@ test_for_replica_exchange(FILE                 *fplog,
     int plumed_test_exchange_pattern=0;
     /* END PLUMED */
 
+    if(plumed_test_exchange_pattern && plumed_hrex) gmx_fatal(FARGS,"hrex not compatible with ad hoc exchange patterns");
+
     if (bMultiEx)
     {
         /* multiple random switch exchange */
-- 
GitLab