Move code for managing mdrun -cpi and -append
[alexxy/gromacs.git] / src / programs / mdrun / mdrun.cpp
index c584010d3d8f99f71cb6ab540391b0b22fa172ea..f924821d93c1c7cc20bb9ccacce4ec9c8641de03 100644 (file)
@@ -59,7 +59,6 @@
 
 #include "gromacs/commandline/pargs.h"
 #include "gromacs/fileio/filenm.h"
-#include "gromacs/legacyheaders/checkpoint.h"
 #include "gromacs/legacyheaders/macros.h"
 #include "gromacs/legacyheaders/main.h"
 #include "gromacs/legacyheaders/mdrun.h"
@@ -67,6 +66,7 @@
 #include "gromacs/legacyheaders/readinp.h"
 #include "gromacs/legacyheaders/typedefs.h"
 #include "gromacs/legacyheaders/types/commrec.h"
+#include "gromacs/mdrunutility/handlerestart.h"
 #include "gromacs/utility/fatalerror.h"
 
 #include "mdrun_main.h"
@@ -299,7 +299,7 @@ int gmx_mdrun(int argc, char *argv[])
     real            rdd                   = 0.0, rconstr = 0.0, dlb_scale = 0.8, pforce = -1;
     char           *ddcsx                 = NULL, *ddcsy = NULL, *ddcsz = NULL;
     real            cpt_period            = 15.0, max_hours = -1;
-    gmx_bool        bAppendFiles          = TRUE;
+    gmx_bool        bTryToAppendFiles     = TRUE;
     gmx_bool        bKeepAndNumCPT        = FALSE;
     gmx_bool        bResetCountersHalfWay = FALSE;
     output_env_t    oenv                  = NULL;
@@ -382,7 +382,7 @@ int gmx_mdrun(int argc, char *argv[])
           "Checkpoint interval (minutes)" },
         { "-cpnum",   FALSE, etBOOL, {&bKeepAndNumCPT},
           "Keep and number checkpoint files" },
-        { "-append",  FALSE, etBOOL, {&bAppendFiles},
+        { "-append",  FALSE, etBOOL, {&bTryToAppendFiles},
           "Append to previous output files when continuing from checkpoint instead of adding the simulation part number to all file names" },
         { "-nsteps",  FALSE, etINT64, {&nsteps},
           "Run this number of steps, overrides .mdp file option (-1 means infinite, -2 means use mdp option, smaller is invalid)" },
@@ -418,11 +418,8 @@ int gmx_mdrun(int argc, char *argv[])
     unsigned long   Flags;
     ivec            ddxyz;
     int             dd_node_order;
-    gmx_bool        bAddPart;
-    FILE           *fplog, *fpmulti;
-    int             sim_part, sim_part_fn;
-    const char     *part_suffix = ".part";
-    char            suffix[STRLEN];
+    gmx_bool        bDoAppendFiles, bStartFromCpt;
+    FILE           *fplog;
     int             rc;
     char          **multidir = NULL;
 
@@ -498,65 +495,8 @@ int gmx_mdrun(int argc, char *argv[])
 #endif
     }
 
-    bAddPart = !bAppendFiles;
-
-    /* Check if there is ANY checkpoint file available */
-    sim_part    = 1;
-    sim_part_fn = sim_part;
-    if (opt2bSet("-cpi", NFILE, fnm))
-    {
-        bAppendFiles =
-            read_checkpoint_simulation_part(opt2fn_master("-cpi", NFILE,
-                                                          fnm, cr),
-                                            &sim_part_fn, cr,
-                                            bAppendFiles, NFILE, fnm,
-                                            part_suffix, &bAddPart);
-        if (sim_part_fn == 0 && MULTIMASTER(cr))
-        {
-            fprintf(stdout, "No previous checkpoint file present, assuming this is a new run.\n");
-        }
-        else
-        {
-            sim_part = sim_part_fn + 1;
-        }
-
-        if (MULTISIM(cr) && MASTER(cr))
-        {
-            if (MULTIMASTER(cr))
-            {
-                /* Log file is not yet available, so if there's a
-                 * problem we can only write to stderr. */
-                fpmulti = stderr;
-            }
-            else
-            {
-                fpmulti = NULL;
-            }
-            check_multi_int(fpmulti, cr->ms, sim_part, "simulation part", TRUE);
-        }
-    }
-    else
-    {
-        bAppendFiles = FALSE;
-    }
-
-    if (!bAppendFiles)
-    {
-        sim_part_fn = sim_part;
-    }
-
-    if (bAddPart)
-    {
-        /* Rename all output files (except checkpoint files) */
-        /* create new part name first (zero-filled) */
-        sprintf(suffix, "%s%04d", part_suffix, sim_part_fn);
-
-        add_suffix_to_output_names(fnm, NFILE, suffix);
-        if (MULTIMASTER(cr))
-        {
-            fprintf(stdout, "Checkpoint file is from part %d, new output files will be suffixed '%s'.\n", sim_part-1, suffix);
-        }
-    }
+    handleRestart(cr, bTryToAppendFiles, NFILE, fnm,
+                  &bDoAppendFiles, &bStartFromCpt);
 
     Flags = opt2bSet("-rerun", NFILE, fnm) ? MD_RERUN : 0;
     Flags = Flags | (bDDBondCheck  ? MD_DDBONDCHECK  : 0);
@@ -565,10 +505,10 @@ int gmx_mdrun(int argc, char *argv[])
     Flags = Flags | (bConfout      ? MD_CONFOUT      : 0);
     Flags = Flags | (bRerunVSite   ? MD_RERUN_VSITE  : 0);
     Flags = Flags | (bReproducible ? MD_REPRODUCIBLE : 0);
-    Flags = Flags | (bAppendFiles  ? MD_APPENDFILES  : 0);
+    Flags = Flags | (bDoAppendFiles ? MD_APPENDFILES  : 0);
     Flags = Flags | (opt2parg_bSet("-append", asize(pa), pa) ? MD_APPENDFILESSET : 0);
     Flags = Flags | (bKeepAndNumCPT ? MD_KEEPANDNUMCPT : 0);
-    Flags = Flags | (sim_part > 1    ? MD_STARTFROMCPT : 0);
+    Flags = Flags | (bStartFromCpt ? MD_STARTFROMCPT : 0);
     Flags = Flags | (bResetCountersHalfWay ? MD_RESETCOUNTERSHALFWAY : 0);
     Flags = Flags | (bIMDwait      ? MD_IMDWAIT      : 0);
     Flags = Flags | (bIMDterm      ? MD_IMDTERM      : 0);
@@ -577,7 +517,7 @@ int gmx_mdrun(int argc, char *argv[])
     /* We postpone opening the log file if we are appending, so we can
        first truncate the old log file and append to the correct position
        there instead.  */
-    if (MASTER(cr) && !bAppendFiles)
+    if (MASTER(cr) && !bDoAppendFiles)
     {
         gmx_log_open(ftp2fn(efLOG, NFILE, fnm), cr,
                      Flags & MD_APPENDFILES, &fplog);
@@ -601,7 +541,7 @@ int gmx_mdrun(int argc, char *argv[])
 
     /* Log file has to be closed in mdrunner if we are appending to it
        (fplog not set here) */
-    if (MASTER(cr) && !bAppendFiles)
+    if (MASTER(cr) && !bDoAppendFiles)
     {
         gmx_log_close(fplog);
     }