Merge branch release-2016
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_tune_pme.cpp
index 90dc653b1ab0a2c9083537e4cb021997e6d9aa0c..ca8eae0e00e50f2c38f769230384b36d455814a3 100644 (file)
 #include "gromacs/commandline/pargs.h"
 #include "gromacs/fft/calcgrid.h"
 #include "gromacs/fileio/checkpoint.h"
-#include "gromacs/fileio/readinp.h"
 #include "gromacs/fileio/tpxio.h"
 #include "gromacs/gmxana/gmx_ana.h"
 #include "gromacs/math/utilities.h"
 #include "gromacs/math/vec.h"
 #include "gromacs/mdlib/perf_est.h"
+#include "gromacs/mdrunutility/mdmodules.h"
 #include "gromacs/mdtypes/commrec.h"
 #include "gromacs/mdtypes/inputrec.h"
 #include "gromacs/mdtypes/md_enums.h"
@@ -168,7 +168,7 @@ static void finalize(const char *fn_out)
     fp = fopen(fn_out, "r");
     fprintf(stdout, "\n\n");
 
-    while (fgets(buf, STRLEN-1, fp) != NULL)
+    while (fgets(buf, STRLEN-1, fp) != nullptr)
     {
         fprintf(stdout, "%s", buf);
     }
@@ -218,13 +218,13 @@ static int parse_logfile(const char *logfile, const char *errfile,
         iFound = eFoundDDStr; /* Skip some case statements */
     }
 
-    while (fgets(line, STRLEN, fp) != NULL)
+    while (fgets(line, STRLEN, fp) != nullptr)
     {
         /* Remove leading spaces */
         ltrim(line);
 
         /* Check for TERM and INT signals from user: */
-        if (std::strstr(line, errSIG) != NULL)
+        if (std::strstr(line, errSIG) != nullptr)
         {
             fclose(fp);
             cleandata(perfdata, test_nr);
@@ -234,7 +234,7 @@ static int parse_logfile(const char *logfile, const char *errfile,
         /* Check whether cycle resetting  worked */
         if (presteps > 0 && !bFoundResetStr)
         {
-            if (std::strstr(line, matchstrcr) != NULL)
+            if (std::strstr(line, matchstrcr) != nullptr)
             {
                 sprintf(dumstring, "step %s", "%" GMX_SCNd64);
                 sscanf(line, dumstring, &resetsteps);
@@ -356,11 +356,11 @@ static int parse_logfile(const char *logfile, const char *errfile,
     if (gmx_fexist(errfile))
     {
         fp = fopen(errfile, "r");
-        while (fgets(line, STRLEN, fp) != NULL)
+        while (fgets(line, STRLEN, fp) != nullptr)
         {
             if (str_starts(line, "Fatal error:") )
             {
-                if (fgets(line, STRLEN, fp) != NULL)
+                if (fgets(line, STRLEN, fp) != nullptr)
                 {
                     fprintf(stderr, "\nWARNING: An error occurred during this benchmark:\n"
                             "%s\n", line);
@@ -607,7 +607,7 @@ static void get_program_paths(gmx_bool bThreads, char *cmd_mpirun[], char *cmd_m
     /* Get the commands we need to set up the runs from environment variables */
     if (!bThreads)
     {
-        if ( (cp = getenv("MPIRUN")) != NULL)
+        if ( (cp = getenv("MPIRUN")) != nullptr)
         {
             *cmd_mpirun = gmx_strdup(cp);
         }
@@ -621,12 +621,12 @@ static void get_program_paths(gmx_bool bThreads, char *cmd_mpirun[], char *cmd_m
         *cmd_mpirun = gmx_strdup(empty_mpirun);
     }
 
-    if (*cmd_mdrun == NULL)
+    if (*cmd_mdrun == nullptr)
     {
         /* The use of MDRUN is deprecated, but made available in 5.1
            for backward compatibility. It may be removed in a future
            version. */
-        if ( (cp = getenv("MDRUN" )) != NULL)
+        if ( (cp = getenv("MDRUN" )) != nullptr)
         {
             *cmd_mdrun = gmx_strdup(cp);
         }
@@ -649,7 +649,7 @@ static void check_mdrun_works(gmx_bool    bThreads,
                               const char *cmd_mdrun,
                               gmx_bool    bNeedGpuSupport)
 {
-    char      *command = NULL;
+    char      *command = nullptr;
     char      *cp;
     char       line[STRLEN];
     FILE      *fp;
@@ -692,7 +692,7 @@ static void check_mdrun_works(gmx_bool    bThreads,
     while (!feof(fp) )
     {
         cp = fgets(line, STRLEN, fp);
-        if (cp != NULL)
+        if (cp != nullptr)
         {
             if (str_starts(line, match_mdrun) )
             {
@@ -869,12 +869,13 @@ static void modify_PMEsettings(
         const char     *fn_best_tpr, /* tpr file with the best performance */
         const char     *fn_sim_tpr)  /* name of tpr file to be launched */
 {
-    t_inputrec   *ir;
-    t_state       state;
-    gmx_mtop_t    mtop;
-    char          buf[200];
+    t_inputrec    *ir;
+    t_state        state;
+    gmx_mtop_t     mtop;
+    char           buf[200];
 
-    snew(ir, 1);
+    gmx::MDModules mdModules;
+    ir = mdModules.inputrec();
     read_tpx_state(fn_best_tpr, ir, &state, &mtop);
 
     /* Reset nsteps and init_step to the value of the input .tpr file */
@@ -886,8 +887,6 @@ static void modify_PMEsettings(
     fprintf(stdout, buf, ir->nsteps);
     fflush(stdout);
     write_tpx_state(fn_sim_tpr, ir, &state, &mtop);
-
-    sfree(ir);
 }
 
 static gmx_bool can_scale_rvdw(int vdwtype)
@@ -937,8 +936,8 @@ static void make_benchmark_tprs(
     }
     fprintf(stdout, ".\n");
 
-
-    snew(ir, 1);
+    gmx::MDModules mdModules;
+    ir = mdModules.inputrec();
     read_tpx_state(fn_sim_tpr, ir, &state, &mtop);
 
     /* Check if some kind of PME was chosen */
@@ -1079,7 +1078,7 @@ static void make_benchmark_tprs(
 
             /* Scale the Fourier grid spacing */
             ir->nkx = ir->nky = ir->nkz = 0;
-            calc_grid(NULL, state.box, fourierspacing*fac, &ir->nkx, &ir->nky, &ir->nkz);
+            calc_grid(nullptr, state.box, fourierspacing*fac, &ir->nkx, &ir->nky, &ir->nkz);
 
             /* Adjust other radii since various conditions need to be fulfilled */
             if (eelPME == ir->coulombtype)
@@ -1169,7 +1168,6 @@ static void make_benchmark_tprs(
     }
     fflush(stdout);
     fflush(fp);
-    sfree(ir);
 }
 
 
@@ -1180,7 +1178,7 @@ static void cleanup(const t_filenm *fnm, int nfile, int k, int nnodes,
 {
     char        numstring[STRLEN];
     char        newfilename[STRLEN];
-    const char *fn = NULL;
+    const char *fn = nullptr;
     int         i;
     const char *opt;
 
@@ -1449,8 +1447,8 @@ static void do_the_tests(
                                                    * constructing mdrun command lines */
 {
     int      i, nr, k, ret, count = 0, totaltests;
-    int     *nPMEnodes = NULL;
-    t_perf  *pd        = NULL;
+    int     *nPMEnodes = nullptr;
+    t_perf  *pd        = nullptr;
     int      cmdline_length;
     char    *command, *cmd_stub;
     char     buf[STRLEN];
@@ -1532,7 +1530,7 @@ static void do_the_tests(
         /* Loop over various numbers of PME nodes: */
         for (i = 0; i < *pmeentries; i++)
         {
-            char *cmd_gpu_ids = NULL;
+            char *cmd_gpu_ids = nullptr;
 
             pd = &perfdata[k][i];
 
@@ -2047,20 +2045,22 @@ static float inspect_tpr(int nfile, t_filenm fnm[], real *rcoulomb)
     gmx_bool     bFree;     /* Is a free energy simulation requested?         */
     gmx_bool     bNM;       /* Is a normal mode analysis requested?           */
     gmx_bool     bSwap;     /* Is water/ion position swapping requested?      */
-    t_inputrec   ir;
+    t_inputrec  *ir;
     t_state      state;
     gmx_mtop_t   mtop;
 
 
     /* Check tpr file for options that trigger extra output files */
-    read_tpx_state(opt2fn("-s", nfile, fnm), &ir, &state, &mtop);
-    bFree = (efepNO  != ir.efep );
-    bNM   = (eiNM    == ir.eI   );
-    bSwap = (eswapNO != ir.eSwapCoords);
-    bTpi  = EI_TPI(ir.eI);
+    gmx::MDModules mdModules;
+    ir = mdModules.inputrec();
+    read_tpx_state(opt2fn("-s", nfile, fnm), ir, &state, &mtop);
+    bFree = (efepNO  != ir->efep );
+    bNM   = (eiNM    == ir->eI   );
+    bSwap = (eswapNO != ir->eSwapCoords);
+    bTpi  = EI_TPI(ir->eI);
 
     /* Set these output files on the tuning command-line */
-    if (ir.bPull)
+    if (ir->bPull)
     {
         setopt("-pf", nfile, fnm);
         setopt("-px", nfile, fnm);
@@ -2083,10 +2083,11 @@ static float inspect_tpr(int nfile, t_filenm fnm[], real *rcoulomb)
         setopt("-swap", nfile, fnm);
     }
 
-    *rcoulomb = ir.rcoulomb;
+    *rcoulomb = ir->rcoulomb;
 
     /* Return the estimate for the number of PME nodes */
-    return pme_load_estimate(&mtop, &ir, state.box);
+    float npme = pme_load_estimate(&mtop, ir, state.box);
+    return npme;
 }
 
 
@@ -2206,25 +2207,25 @@ int gmx_tune_pme(int argc, char *argv[])
     int             presteps       = 1500; /* Do a full cycle reset after presteps steps */
     gmx_bool        bOverwrite     = FALSE, bKeepTPR;
     gmx_bool        bLaunch        = FALSE;
-    char           *ExtraArgs      = NULL;
-    char          **tpr_names      = NULL;
-    const char     *simulation_tpr = NULL;
-    char           *deffnm         = NULL;
+    char           *ExtraArgs      = nullptr;
+    char          **tpr_names      = nullptr;
+    const char     *simulation_tpr = nullptr;
+    char           *deffnm         = nullptr;
     int             best_npme, best_tpr;
     int             sim_part = 1; /* For benchmarks with checkpoint files */
     char            bbuf[STRLEN];
 
 
     /* Default program names if nothing else is found */
-    char               *cmd_mpirun = NULL, *cmd_mdrun = NULL;
+    char               *cmd_mpirun = nullptr, *cmd_mdrun = nullptr;
     char               *cmd_args_bench, *cmd_args_launch;
-    char               *cmd_np           = NULL;
+    char               *cmd_np           = nullptr;
 
     /* IDs of GPUs that are eligible for computation */
-    char               *eligible_gpu_ids = NULL;
-    t_eligible_gpu_ids *gpu_ids          = NULL;
+    char               *eligible_gpu_ids = nullptr;
+    t_eligible_gpu_ids *gpu_ids          = nullptr;
 
-    t_perf            **perfdata = NULL;
+    t_perf            **perfdata = nullptr;
     t_inputinfo        *info;
     int                 i;
     FILE               *fp;
@@ -2238,11 +2239,11 @@ int gmx_tune_pme(int argc, char *argv[])
         { efLOG, "-err",    "bencherr", ffWRITE },
         { efTPR, "-so",     "tuned",    ffWRITE },
         /* mdrun: */
-        { efTPR, NULL,      NULL,       ffREAD },
-        { efTRN, "-o",      NULL,       ffWRITE },
-        { efCOMPRESSED, "-x", NULL,     ffOPTWR },
-        { efCPT, "-cpi",    NULL,       ffOPTRD },
-        { efCPT, "-cpo",    NULL,       ffOPTWR },
+        { efTPR, nullptr,      nullptr,       ffREAD },
+        { efTRN, "-o",      nullptr,       ffWRITE },
+        { efCOMPRESSED, "-x", nullptr,     ffOPTWR },
+        { efCPT, "-cpi",    nullptr,       ffOPTRD },
+        { efCPT, "-cpo",    nullptr,       ffOPTWR },
         { efSTO, "-c",      "confout",  ffWRITE },
         { efEDR, "-e",      "ener",     ffWRITE },
         { efLOG, "-g",      "md",       ffWRITE },
@@ -2296,9 +2297,9 @@ int gmx_tune_pme(int argc, char *argv[])
     int             nthreads = 1;
 
     const char     *procstring[] =
-    { NULL, "np", "n", "none", NULL };
+    { nullptr, "np", "n", "none", nullptr };
     const char     *npmevalues_opt[] =
-    { NULL, "auto", "all", "subset", NULL };
+    { nullptr, "auto", "all", "subset", nullptr };
 
     gmx_bool          bAppendFiles          = TRUE;
     gmx_bool          bKeepAndNumCPT        = FALSE;
@@ -2306,7 +2307,7 @@ int gmx_tune_pme(int argc, char *argv[])
     gmx_bool          bBenchmark            = TRUE;
     gmx_bool          bCheck                = TRUE;
 
-    gmx_output_env_t *oenv = NULL;
+    gmx_output_env_t *oenv = nullptr;
 
     t_pargs           pa[] = {
         /***********************/
@@ -2375,13 +2376,13 @@ int gmx_tune_pme(int argc, char *argv[])
 
     if (!parse_common_args(&argc, argv, PCA_NOEXIT_ON_ARGS,
                            NFILE, fnm, asize(pa), pa, asize(desc), desc,
-                           0, NULL, &oenv))
+                           0, nullptr, &oenv))
     {
         return 0;
     }
 
     // procstring[0] is used inside two different conditionals further down
-    GMX_RELEASE_ASSERT(procstring[0] != NULL, "Options inconsistency; procstring[0] is NULL");
+    GMX_RELEASE_ASSERT(procstring[0] != nullptr, "Options inconsistency; procstring[0] is NULL");
 
     /* Store the remaining unparsed command line entries in a string which
      * is then attached to the mdrun command line */
@@ -2512,7 +2513,7 @@ int gmx_tune_pme(int argc, char *argv[])
     get_program_paths(bThreads, &cmd_mpirun, &cmd_mdrun);
     if (bBenchmark && repeats > 0)
     {
-        check_mdrun_works(bThreads, cmd_mpirun, cmd_np, cmd_mdrun, NULL != eligible_gpu_ids);
+        check_mdrun_works(bThreads, cmd_mpirun, cmd_np, cmd_mdrun, nullptr != eligible_gpu_ids);
     }
 
     /* Print some header info to file */
@@ -2609,7 +2610,7 @@ int gmx_tune_pme(int argc, char *argv[])
     snew(perfdata, ntprs);
     if (bBenchmark)
     {
-        GMX_RELEASE_ASSERT(npmevalues_opt[0] != NULL, "Options inconsistency; npmevalues_opt[0] is NULL");
+        GMX_RELEASE_ASSERT(npmevalues_opt[0] != nullptr, "Options inconsistency; npmevalues_opt[0] is NULL");
         do_the_tests(fp, tpr_names, maxPMEnodes, minPMEnodes, npme_fixed, npmevalues_opt[0], perfdata, &pmeentries,
                      repeats, nnodes, ntprs, bThreads, cmd_mpirun, cmd_np, cmd_mdrun,
                      cmd_args_bench, fnm, NFILE, presteps, cpt_steps, bCheck, gpu_ids);