1804ab4b3663d8865a739d2142702c938418cc54
[alexxy/gromacs.git] / src / programs / mdrun / mdrun.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2011,2012,2013,2014,2015,2016, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 /*! \defgroup module_mdrun Implementation of mdrun
38  * \ingroup group_mdrun
39  *
40  * \brief This module contains code that implements mdrun.
41  */
42 /*! \internal \file
43  *
44  * \brief This file implements mdrun
45  *
46  * \author Berk Hess <hess@kth.se>
47  * \author David van der Spoel <david.vanderspoel@icm.uu.se>
48  * \author Erik Lindahl <erik@kth.se>
49  * \author Mark Abraham <mark.j.abraham@gmail.com>
50  *
51  * \ingroup module_mdrun
52  */
53 #include "gmxpre.h"
54
55 #include "config.h"
56
57 #include <stdio.h>
58 #include <string.h>
59
60 #include "gromacs/commandline/filenm.h"
61 #include "gromacs/commandline/pargs.h"
62 #include "gromacs/gmxlib/network.h"
63 #include "gromacs/mdlib/main.h"
64 #include "gromacs/mdlib/mdrun.h"
65 #include "gromacs/mdrunutility/handlerestart.h"
66 #include "gromacs/mdtypes/commrec.h"
67 #include "gromacs/utility/arraysize.h"
68 #include "gromacs/utility/fatalerror.h"
69 #include "gromacs/utility/smalloc.h"
70
71 #include "mdrun_main.h"
72 #include "runner.h"
73
74 /*! \brief Return whether either of the command-line parameters that
75  *  will trigger a multi-simulation is set */
76 static bool is_multisim_option_set(int argc, const char *const argv[])
77 {
78     for (int i = 0; i < argc; ++i)
79     {
80         if (strcmp(argv[i], "-multi") == 0 || strcmp(argv[i], "-multidir") == 0)
81         {
82             return true;
83         }
84     }
85     return false;
86 }
87
88 //! Implements C-style main function for mdrun
89 int gmx_mdrun(int argc, char *argv[])
90 {
91     const char   *desc[] = {
92         "[THISMODULE] is the main computational chemistry engine",
93         "within GROMACS. Obviously, it performs Molecular Dynamics simulations,",
94         "but it can also perform Stochastic Dynamics, Energy Minimization,",
95         "test particle insertion or (re)calculation of energies.",
96         "Normal mode analysis is another option. In this case [TT]mdrun[tt]",
97         "builds a Hessian matrix from single conformation.",
98         "For usual Normal Modes-like calculations, make sure that",
99         "the structure provided is properly energy-minimized.",
100         "The generated matrix can be diagonalized by [gmx-nmeig].[PAR]",
101         "The [TT]mdrun[tt] program reads the run input file ([TT]-s[tt])",
102         "and distributes the topology over ranks if needed.",
103         "[TT]mdrun[tt] produces at least four output files.",
104         "A single log file ([TT]-g[tt]) is written.",
105         "The trajectory file ([TT]-o[tt]), contains coordinates, velocities and",
106         "optionally forces.",
107         "The structure file ([TT]-c[tt]) contains the coordinates and",
108         "velocities of the last step.",
109         "The energy file ([TT]-e[tt]) contains energies, the temperature,",
110         "pressure, etc, a lot of these things are also printed in the log file.",
111         "Optionally coordinates can be written to a compressed trajectory file",
112         "([TT]-x[tt]).[PAR]",
113         "The option [TT]-dhdl[tt] is only used when free energy calculation is",
114         "turned on.[PAR]",
115         "Running mdrun efficiently in parallel is a complex topic topic,",
116         "many aspects of which are covered in the online User Guide. You",
117         "should look there for practical advice on using many of the options",
118         "available in mdrun.[PAR]",
119         "ED (essential dynamics) sampling and/or additional flooding potentials",
120         "are switched on by using the [TT]-ei[tt] flag followed by an [REF].edi[ref]",
121         "file. The [REF].edi[ref] file can be produced with the [TT]make_edi[tt] tool",
122         "or by using options in the essdyn menu of the WHAT IF program.",
123         "[TT]mdrun[tt] produces a [REF].xvg[ref] output file that",
124         "contains projections of positions, velocities and forces onto selected",
125         "eigenvectors.[PAR]",
126         "When user-defined potential functions have been selected in the",
127         "[REF].mdp[ref] file the [TT]-table[tt] option is used to pass [TT]mdrun[tt]",
128         "a formatted table with potential functions. The file is read from",
129         "either the current directory or from the [TT]GMXLIB[tt] directory.",
130         "A number of pre-formatted tables are presented in the [TT]GMXLIB[tt] dir,",
131         "for 6-8, 6-9, 6-10, 6-11, 6-12 Lennard-Jones potentials with",
132         "normal Coulomb.",
133         "When pair interactions are present, a separate table for pair interaction",
134         "functions is read using the [TT]-tablep[tt] option.[PAR]",
135         "When tabulated bonded functions are present in the topology,",
136         "interaction functions are read using the [TT]-tableb[tt] option.",
137         "For each different tabulated interaction type used, a table file name must",
138         "be given. For the topology to work, a file name given here must match a",
139         "character sequence before the file extension. That sequence is: an underscore,",
140         "then a 'b' for bonds, an 'a' for angles or a 'd' for dihedrals,",
141         "and finally the matching table number index used in the topology.[PAR]",
142         "The options [TT]-px[tt] and [TT]-pf[tt] are used for writing pull COM",
143         "coordinates and forces when pulling is selected",
144         "in the [REF].mdp[ref] file.[PAR]",
145         "Finally some experimental algorithms can be tested when the",
146         "appropriate options have been given. Currently under",
147         "investigation are: polarizability.",
148         "[PAR]",
149         "The option [TT]-membed[tt] does what used to be g_membed, i.e. embed",
150         "a protein into a membrane. This module requires a number of settings",
151         "that are provided in a data file that is the argument of this option.",
152         "For more details in membrane embedding, see the documentation in the",
153         "user guide. The options [TT]-mn[tt] and [TT]-mp[tt] are used to provide",
154         "the index and topology files used for the embedding.",
155         "[PAR]",
156         "The option [TT]-pforce[tt] is useful when you suspect a simulation",
157         "crashes due to too large forces. With this option coordinates and",
158         "forces of atoms with a force larger than a certain value will",
159         "be printed to stderr. It will also terminate the run when non-finite",
160         "forces are present.",
161         "[PAR]",
162         "Checkpoints containing the complete state of the system are written",
163         "at regular intervals (option [TT]-cpt[tt]) to the file [TT]-cpo[tt],",
164         "unless option [TT]-cpt[tt] is set to -1.",
165         "The previous checkpoint is backed up to [TT]state_prev.cpt[tt] to",
166         "make sure that a recent state of the system is always available,",
167         "even when the simulation is terminated while writing a checkpoint.",
168         "With [TT]-cpnum[tt] all checkpoint files are kept and appended",
169         "with the step number.",
170         "A simulation can be continued by reading the full state from file",
171         "with option [TT]-cpi[tt]. This option is intelligent in the way that",
172         "if no checkpoint file is found, GROMACS just assumes a normal run and",
173         "starts from the first step of the [REF].tpr[ref] file. By default the output",
174         "will be appending to the existing output files. The checkpoint file",
175         "contains checksums of all output files, such that you will never",
176         "loose data when some output files are modified, corrupt or removed.",
177         "There are three scenarios with [TT]-cpi[tt]:[PAR]",
178         "[TT]*[tt] no files with matching names are present: new output files are written[PAR]",
179         "[TT]*[tt] all files are present with names and checksums matching those stored",
180         "in the checkpoint file: files are appended[PAR]",
181         "[TT]*[tt] otherwise no files are modified and a fatal error is generated[PAR]",
182         "With [TT]-noappend[tt] new output files are opened and the simulation",
183         "part number is added to all output file names.",
184         "Note that in all cases the checkpoint file itself is not renamed",
185         "and will be overwritten, unless its name does not match",
186         "the [TT]-cpo[tt] option.",
187         "[PAR]",
188         "With checkpointing the output is appended to previously written",
189         "output files, unless [TT]-noappend[tt] is used or none of the previous",
190         "output files are present (except for the checkpoint file).",
191         "The integrity of the files to be appended is verified using checksums",
192         "which are stored in the checkpoint file. This ensures that output can",
193         "not be mixed up or corrupted due to file appending. When only some",
194         "of the previous output files are present, a fatal error is generated",
195         "and no old output files are modified and no new output files are opened.",
196         "The result with appending will be the same as from a single run.",
197         "The contents will be binary identical, unless you use a different number",
198         "of ranks or dynamic load balancing or the FFT library uses optimizations",
199         "through timing.",
200         "[PAR]",
201         "With option [TT]-maxh[tt] a simulation is terminated and a checkpoint",
202         "file is written at the first neighbor search step where the run time",
203         "exceeds [TT]-maxh[tt]\\*0.99 hours. This option is particularly useful in",
204         "combination with setting [TT]nsteps[tt] to -1 either in the mdp or using the",
205         "similarly named command line option. This results in an infinite run,",
206         "terminated only when the time limit set by [TT]-maxh[tt] is reached (if any)"
207         "or upon receiving a signal."
208         "[PAR]",
209         "When [TT]mdrun[tt] receives a TERM signal, it will stop as soon as",
210         "checkpoint file can be written, i.e. after the next global communication step.",
211         "When [TT]mdrun[tt] receives an INT signal (e.g. when ctrl+C is",
212         "pressed), it will stop at the next neighbor search step or at the",
213         "second global communication step, whichever happens later.",
214         "In both cases all the usual output will be written to file.",
215         "When running with MPI, a signal to one of the [TT]mdrun[tt] ranks",
216         "is sufficient, this signal should not be sent to mpirun or",
217         "the [TT]mdrun[tt] process that is the parent of the others.",
218         "[PAR]",
219         "Interactive molecular dynamics (IMD) can be activated by using at least one",
220         "of the three IMD switches: The [TT]-imdterm[tt] switch allows one to terminate",
221         "the simulation from the molecular viewer (e.g. VMD). With [TT]-imdwait[tt],",
222         "[TT]mdrun[tt] pauses whenever no IMD client is connected. Pulling from the",
223         "IMD remote can be turned on by [TT]-imdpull[tt].",
224         "The port [TT]mdrun[tt] listens to can be altered by [TT]-imdport[tt].The",
225         "file pointed to by [TT]-if[tt] contains atom indices and forces if IMD",
226         "pulling is used."
227         "[PAR]",
228         "When [TT]mdrun[tt] is started with MPI, it does not run niced by default."
229     };
230     t_commrec    *cr;
231     t_filenm      fnm[] = {
232         { efTPR, NULL,      NULL,       ffREAD },
233         { efTRN, "-o",      NULL,       ffWRITE },
234         { efCOMPRESSED, "-x", NULL,     ffOPTWR },
235         { efCPT, "-cpi",    NULL,       ffOPTRD | ffALLOW_MISSING },
236         { efCPT, "-cpo",    NULL,       ffOPTWR },
237         { efSTO, "-c",      "confout",  ffWRITE },
238         { efEDR, "-e",      "ener",     ffWRITE },
239         { efLOG, "-g",      "md",       ffWRITE },
240         { efXVG, "-dhdl",   "dhdl",     ffOPTWR },
241         { efXVG, "-field",  "field",    ffOPTWR },
242         { efXVG, "-table",  "table",    ffOPTRD },
243         { efXVG, "-tablep", "tablep",   ffOPTRD },
244         { efXVG, "-tableb", "table",    ffOPTRDMULT },
245         { efTRX, "-rerun",  "rerun",    ffOPTRD },
246         { efXVG, "-tpi",    "tpi",      ffOPTWR },
247         { efXVG, "-tpid",   "tpidist",  ffOPTWR },
248         { efEDI, "-ei",     "sam",      ffOPTRD },
249         { efXVG, "-eo",     "edsam",    ffOPTWR },
250         { efXVG, "-devout", "deviatie", ffOPTWR },
251         { efXVG, "-runav",  "runaver",  ffOPTWR },
252         { efXVG, "-px",     "pullx",    ffOPTWR },
253         { efXVG, "-pf",     "pullf",    ffOPTWR },
254         { efXVG, "-ro",     "rotation", ffOPTWR },
255         { efLOG, "-ra",     "rotangles", ffOPTWR },
256         { efLOG, "-rs",     "rotslabs", ffOPTWR },
257         { efLOG, "-rt",     "rottorque", ffOPTWR },
258         { efMTX, "-mtx",    "nm",       ffOPTWR },
259         { efRND, "-multidir", NULL,      ffOPTRDMULT},
260         { efDAT, "-membed", "membed",   ffOPTRD },
261         { efTOP, "-mp",     "membed",   ffOPTRD },
262         { efNDX, "-mn",     "membed",   ffOPTRD },
263         { efXVG, "-if",     "imdforces", ffOPTWR },
264         { efXVG, "-swap",   "swapions", ffOPTWR }
265     };
266     const int     NFILE = asize(fnm);
267
268     /* Command line options ! */
269     gmx_bool          bDDBondCheck  = TRUE;
270     gmx_bool          bDDBondComm   = TRUE;
271     gmx_bool          bTunePME      = TRUE;
272     gmx_bool          bVerbose      = FALSE;
273     gmx_bool          bRerunVSite   = FALSE;
274     gmx_bool          bConfout      = TRUE;
275     gmx_bool          bReproducible = FALSE;
276     gmx_bool          bIMDwait      = FALSE;
277     gmx_bool          bIMDterm      = FALSE;
278     gmx_bool          bIMDpull      = FALSE;
279
280     int               npme          = -1;
281     int               nstlist       = 0;
282     int               nmultisim     = 0;
283     int               nstglobalcomm = -1;
284     int               repl_ex_nst   = 0;
285     int               repl_ex_seed  = -1;
286     int               repl_ex_nex   = 0;
287     int               nstepout      = 100;
288     int               resetstep     = -1;
289     gmx_int64_t       nsteps        = -2;   /* the value -2 means that the mdp option will be used */
290     int               imdport       = 8888; /* can be almost anything, 8888 is easy to remember */
291
292     rvec              realddxyz                   = {0, 0, 0};
293     const char       *ddrank_opt[ddrankorderNR+1] =
294     { NULL, "interleave", "pp_pme", "cartesian", NULL };
295     const char       *dddlb_opt[] =
296     { NULL, "auto", "no", "yes", NULL };
297     const char       *thread_aff_opt[threadaffNR+1] =
298     { NULL, "auto", "on", "off", NULL };
299     const char       *nbpu_opt[] =
300     { NULL, "auto", "cpu", "gpu", "gpu_cpu", NULL };
301     real              rdd                   = 0.0, rconstr = 0.0, dlb_scale = 0.8, pforce = -1;
302     char             *ddcsx                 = NULL, *ddcsy = NULL, *ddcsz = NULL;
303     real              cpt_period            = 15.0, max_hours = -1;
304     gmx_bool          bTryToAppendFiles     = TRUE;
305     gmx_bool          bKeepAndNumCPT        = FALSE;
306     gmx_bool          bResetCountersHalfWay = FALSE;
307     gmx_output_env_t *oenv                  = NULL;
308
309     /* Non transparent initialization of a complex gmx_hw_opt_t struct.
310      * But unfortunately we are not allowed to call a function here,
311      * since declarations follow below.
312      */
313     gmx_hw_opt_t    hw_opt = {
314         0, 0, 0, 0, threadaffSEL, 0, 0,
315         { NULL, FALSE, 0, NULL }
316     };
317
318     t_pargs         pa[] = {
319
320         { "-dd",      FALSE, etRVEC, {&realddxyz},
321           "Domain decomposition grid, 0 is optimize" },
322         { "-ddorder", FALSE, etENUM, {ddrank_opt},
323           "DD rank order" },
324         { "-npme",    FALSE, etINT, {&npme},
325           "Number of separate ranks to be used for PME, -1 is guess" },
326         { "-nt",      FALSE, etINT, {&hw_opt.nthreads_tot},
327           "Total number of threads to start (0 is guess)" },
328         { "-ntmpi",   FALSE, etINT, {&hw_opt.nthreads_tmpi},
329           "Number of thread-MPI threads to start (0 is guess)" },
330         { "-ntomp",   FALSE, etINT, {&hw_opt.nthreads_omp},
331           "Number of OpenMP threads per MPI rank to start (0 is guess)" },
332         { "-ntomp_pme", FALSE, etINT, {&hw_opt.nthreads_omp_pme},
333           "Number of OpenMP threads per MPI rank to start (0 is -ntomp)" },
334         { "-pin",     FALSE, etENUM, {thread_aff_opt},
335           "Whether mdrun should try to set thread affinities" },
336         { "-pinoffset", FALSE, etINT, {&hw_opt.core_pinning_offset},
337           "The lowest logical core number to which mdrun should pin the first thread" },
338         { "-pinstride", FALSE, etINT, {&hw_opt.core_pinning_stride},
339           "Pinning distance in logical cores for threads, use 0 to minimize the number of threads per physical core" },
340         { "-gpu_id",  FALSE, etSTR, {&hw_opt.gpu_opt.gpu_id},
341           "List of GPU device id-s to use, specifies the per-node PP rank to GPU mapping" },
342         { "-ddcheck", FALSE, etBOOL, {&bDDBondCheck},
343           "Check for all bonded interactions with DD" },
344         { "-ddbondcomm", FALSE, etBOOL, {&bDDBondComm},
345           "HIDDENUse special bonded atom communication when [TT]-rdd[tt] > cut-off" },
346         { "-rdd",     FALSE, etREAL, {&rdd},
347           "The maximum distance for bonded interactions with DD (nm), 0 is determine from initial coordinates" },
348         { "-rcon",    FALSE, etREAL, {&rconstr},
349           "Maximum distance for P-LINCS (nm), 0 is estimate" },
350         { "-dlb",     FALSE, etENUM, {dddlb_opt},
351           "Dynamic load balancing (with DD)" },
352         { "-dds",     FALSE, etREAL, {&dlb_scale},
353           "Fraction in (0,1) by whose reciprocal the initial DD cell size will be increased in order to "
354           "provide a margin in which dynamic load balancing can act while preserving the minimum cell size." },
355         { "-ddcsx",   FALSE, etSTR, {&ddcsx},
356           "HIDDENA string containing a vector of the relative sizes in the x "
357           "direction of the corresponding DD cells. Only effective with static "
358           "load balancing." },
359         { "-ddcsy",   FALSE, etSTR, {&ddcsy},
360           "HIDDENA string containing a vector of the relative sizes in the y "
361           "direction of the corresponding DD cells. Only effective with static "
362           "load balancing." },
363         { "-ddcsz",   FALSE, etSTR, {&ddcsz},
364           "HIDDENA string containing a vector of the relative sizes in the z "
365           "direction of the corresponding DD cells. Only effective with static "
366           "load balancing." },
367         { "-gcom",    FALSE, etINT, {&nstglobalcomm},
368           "Global communication frequency" },
369         { "-nb",      FALSE, etENUM, {&nbpu_opt},
370           "Calculate non-bonded interactions on" },
371         { "-nstlist", FALSE, etINT, {&nstlist},
372           "Set nstlist when using a Verlet buffer tolerance (0 is guess)" },
373         { "-tunepme", FALSE, etBOOL, {&bTunePME},
374           "Optimize PME load between PP/PME ranks or GPU/CPU" },
375         { "-v",       FALSE, etBOOL, {&bVerbose},
376           "Be loud and noisy" },
377         { "-pforce",  FALSE, etREAL, {&pforce},
378           "Print all forces larger than this (kJ/mol nm)" },
379         { "-reprod",  FALSE, etBOOL, {&bReproducible},
380           "Try to avoid optimizations that affect binary reproducibility" },
381         { "-cpt",     FALSE, etREAL, {&cpt_period},
382           "Checkpoint interval (minutes)" },
383         { "-cpnum",   FALSE, etBOOL, {&bKeepAndNumCPT},
384           "Keep and number checkpoint files" },
385         { "-append",  FALSE, etBOOL, {&bTryToAppendFiles},
386           "Append to previous output files when continuing from checkpoint instead of adding the simulation part number to all file names" },
387         { "-nsteps",  FALSE, etINT64, {&nsteps},
388           "Run this number of steps, overrides .mdp file option (-1 means infinite, -2 means use mdp option, smaller is invalid)" },
389         { "-maxh",   FALSE, etREAL, {&max_hours},
390           "Terminate after 0.99 times this time (hours)" },
391         { "-multi",   FALSE, etINT, {&nmultisim},
392           "Do multiple simulations in parallel" },
393         { "-replex",  FALSE, etINT, {&repl_ex_nst},
394           "Attempt replica exchange periodically with this period (steps)" },
395         { "-nex",  FALSE, etINT, {&repl_ex_nex},
396           "Number of random exchanges to carry out each exchange interval (N^3 is one suggestion).  -nex zero or not specified gives neighbor replica exchange." },
397         { "-reseed",  FALSE, etINT, {&repl_ex_seed},
398           "Seed for replica exchange, -1 is generate a seed" },
399         { "-imdport",    FALSE, etINT, {&imdport},
400           "HIDDENIMD listening port" },
401         { "-imdwait",  FALSE, etBOOL, {&bIMDwait},
402           "HIDDENPause the simulation while no IMD client is connected" },
403         { "-imdterm",  FALSE, etBOOL, {&bIMDterm},
404           "HIDDENAllow termination of the simulation from IMD client" },
405         { "-imdpull",  FALSE, etBOOL, {&bIMDpull},
406           "HIDDENAllow pulling in the simulation from IMD client" },
407         { "-rerunvsite", FALSE, etBOOL, {&bRerunVSite},
408           "HIDDENRecalculate virtual site coordinates with [TT]-rerun[tt]" },
409         { "-confout", FALSE, etBOOL, {&bConfout},
410           "HIDDENWrite the last configuration with [TT]-c[tt] and force checkpointing at the last step" },
411         { "-stepout", FALSE, etINT, {&nstepout},
412           "HIDDENFrequency of writing the remaining wall clock time for the run" },
413         { "-resetstep", FALSE, etINT, {&resetstep},
414           "HIDDENReset cycle counters after these many time steps" },
415         { "-resethway", FALSE, etBOOL, {&bResetCountersHalfWay},
416           "HIDDENReset the cycle counters after half the number of steps or halfway [TT]-maxh[tt]" }
417     };
418     unsigned long   Flags;
419     ivec            ddxyz;
420     int             dd_rank_order;
421     gmx_bool        bDoAppendFiles, bStartFromCpt;
422     FILE           *fplog;
423     int             rc;
424     char          **multidir = NULL;
425
426     cr = init_commrec();
427
428     unsigned long PCA_Flags = PCA_CAN_SET_DEFFNM;
429     // With -multi or -multidir, the file names are going to get processed
430     // further (or the working directory changed), so we can't check for their
431     // existence during parsing.  It isn't useful to do any completion based on
432     // file system contents, either.
433     if (is_multisim_option_set(argc, argv))
434     {
435         PCA_Flags |= PCA_DISABLE_INPUT_FILE_CHECKING;
436     }
437
438     /* Comment this in to do fexist calls only on master
439      * works not with rerun or tables at the moment
440      * also comment out the version of init_forcerec in md.c
441      * with NULL instead of opt2fn
442      */
443     /*
444        if (!MASTER(cr))
445        {
446        PCA_Flags |= PCA_NOT_READ_NODE;
447        }
448      */
449
450     if (!parse_common_args(&argc, argv, PCA_Flags, NFILE, fnm, asize(pa), pa,
451                            asize(desc), desc, 0, NULL, &oenv))
452     {
453         sfree(cr);
454         return 0;
455     }
456
457
458     dd_rank_order = nenum(ddrank_opt);
459
460     hw_opt.thread_affinity = nenum(thread_aff_opt);
461
462     /* now check the -multi and -multidir option */
463     if (opt2bSet("-multidir", NFILE, fnm))
464     {
465         if (nmultisim > 0)
466         {
467             gmx_fatal(FARGS, "mdrun -multi and -multidir options are mutually exclusive.");
468         }
469         nmultisim = opt2fns(&multidir, "-multidir", NFILE, fnm);
470     }
471
472
473     if (repl_ex_nst != 0 && nmultisim < 2)
474     {
475         gmx_fatal(FARGS, "Need at least two replicas for replica exchange (option -multi)");
476     }
477
478     if (repl_ex_nex < 0)
479     {
480         gmx_fatal(FARGS, "Replica exchange number of exchanges needs to be positive");
481     }
482
483     if (nmultisim >= 1)
484     {
485 #if !GMX_THREAD_MPI
486         init_multisystem(cr, nmultisim, multidir, NFILE, fnm);
487 #else
488         gmx_fatal(FARGS, "mdrun -multi or -multidir are not supported with the thread-MPI library. "
489                   "Please compile GROMACS with a proper external MPI library.");
490 #endif
491     }
492
493     if (!opt2bSet("-cpi", NFILE, fnm))
494     {
495         // If we are not starting from a checkpoint we never allow files to be appended
496         // to, since that has caused a ton of strange behaviour and bugs in the past.
497         if (opt2parg_bSet("-append", asize(pa), pa))
498         {
499             // If the user explicitly used the -append option, explain that it is not possible.
500             gmx_fatal(FARGS, "GROMACS can only append to files when restarting from a checkpoint.");
501         }
502         else
503         {
504             // If the user did not say anything explicit, just disable appending.
505             bTryToAppendFiles = FALSE;
506         }
507     }
508
509     handleRestart(cr, bTryToAppendFiles, NFILE, fnm, &bDoAppendFiles, &bStartFromCpt);
510
511     Flags = opt2bSet("-rerun", NFILE, fnm) ? MD_RERUN : 0;
512     Flags = Flags | (bDDBondCheck  ? MD_DDBONDCHECK  : 0);
513     Flags = Flags | (bDDBondComm   ? MD_DDBONDCOMM   : 0);
514     Flags = Flags | (bTunePME      ? MD_TUNEPME      : 0);
515     Flags = Flags | (bConfout      ? MD_CONFOUT      : 0);
516     Flags = Flags | (bRerunVSite   ? MD_RERUN_VSITE  : 0);
517     Flags = Flags | (bReproducible ? MD_REPRODUCIBLE : 0);
518     Flags = Flags | (bDoAppendFiles  ? MD_APPENDFILES  : 0);
519     Flags = Flags | (opt2parg_bSet("-append", asize(pa), pa) ? MD_APPENDFILESSET : 0);
520     Flags = Flags | (bKeepAndNumCPT ? MD_KEEPANDNUMCPT : 0);
521     Flags = Flags | (bStartFromCpt ? MD_STARTFROMCPT : 0);
522     Flags = Flags | (bResetCountersHalfWay ? MD_RESETCOUNTERSHALFWAY : 0);
523     Flags = Flags | (opt2parg_bSet("-ntomp", asize(pa), pa) ? MD_NTOMPSET : 0);
524     Flags = Flags | (bIMDwait      ? MD_IMDWAIT      : 0);
525     Flags = Flags | (bIMDterm      ? MD_IMDTERM      : 0);
526     Flags = Flags | (bIMDpull      ? MD_IMDPULL      : 0);
527
528     /* We postpone opening the log file if we are appending, so we can
529        first truncate the old log file and append to the correct position
530        there instead.  */
531     if (MASTER(cr) && !bDoAppendFiles)
532     {
533         gmx_log_open(ftp2fn(efLOG, NFILE, fnm), cr,
534                      Flags & MD_APPENDFILES, &fplog);
535     }
536     else
537     {
538         fplog = NULL;
539     }
540
541     ddxyz[XX] = (int)(realddxyz[XX] + 0.5);
542     ddxyz[YY] = (int)(realddxyz[YY] + 0.5);
543     ddxyz[ZZ] = (int)(realddxyz[ZZ] + 0.5);
544
545     rc = gmx::mdrunner(&hw_opt, fplog, cr, NFILE, fnm, oenv, bVerbose,
546                        nstglobalcomm, ddxyz, dd_rank_order, npme, rdd, rconstr,
547                        dddlb_opt[0], dlb_scale, ddcsx, ddcsy, ddcsz,
548                        nbpu_opt[0], nstlist,
549                        nsteps, nstepout, resetstep,
550                        nmultisim, repl_ex_nst, repl_ex_nex, repl_ex_seed,
551                        pforce, cpt_period, max_hours, imdport, Flags);
552
553     /* Log file has to be closed in mdrunner if we are appending to it
554        (fplog not set here) */
555     if (MASTER(cr) && !bDoAppendFiles)
556     {
557         gmx_log_close(fplog);
558     }
559
560     return rc;
561 }