Make gmx_multisim_t a C++ object
[gromacs.git] / src / programs / mdrun / mdrun.cpp
blob73e324a80719b9c6526f04437a1afaf0e1665ea0
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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,2017,2018,2019, 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.
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.
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.
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.
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.
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.
37 /*! \defgroup module_mdrun Implementation of mdrun
38 * \ingroup group_mdrun
40 * \brief This module contains code that implements mdrun.
42 /*! \internal \file
44 * \brief This file implements mdrun
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>
51 * \ingroup module_mdrun
53 #include "gmxpre.h"
55 #include <memory>
57 #include "gromacs/commandline/pargs.h"
58 #include "gromacs/domdec/options.h"
59 #include "gromacs/fileio/gmxfio.h"
60 #include "gromacs/gmxlib/network.h"
61 #include "gromacs/mdrun/legacymdrunoptions.h"
62 #include "gromacs/mdrun/runner.h"
63 #include "gromacs/mdrun/simulationcontext.h"
64 #include "gromacs/mdrunutility/handlerestart.h"
65 #include "gromacs/mdrunutility/logging.h"
66 #include "gromacs/mdtypes/commrec.h"
67 #include "gromacs/utility/arrayref.h"
68 #include "gromacs/utility/smalloc.h"
70 #include "mdrun_main.h"
72 namespace gmx
75 //! Implements C-style main function for mdrun
76 int gmx_mdrun(int argc, char *argv[])
78 auto mdModules = std::make_unique<MDModules>();
80 std::vector<const char *>desc = {
81 "[THISMODULE] is the main computational chemistry engine",
82 "within GROMACS. Obviously, it performs Molecular Dynamics simulations,",
83 "but it can also perform Stochastic Dynamics, Energy Minimization,",
84 "test particle insertion or (re)calculation of energies.",
85 "Normal mode analysis is another option. In this case [TT]mdrun[tt]",
86 "builds a Hessian matrix from single conformation.",
87 "For usual Normal Modes-like calculations, make sure that",
88 "the structure provided is properly energy-minimized.",
89 "The generated matrix can be diagonalized by [gmx-nmeig].[PAR]",
90 "The [TT]mdrun[tt] program reads the run input file ([TT]-s[tt])",
91 "and distributes the topology over ranks if needed.",
92 "[TT]mdrun[tt] produces at least four output files.",
93 "A single log file ([TT]-g[tt]) is written.",
94 "The trajectory file ([TT]-o[tt]), contains coordinates, velocities and",
95 "optionally forces.",
96 "The structure file ([TT]-c[tt]) contains the coordinates and",
97 "velocities of the last step.",
98 "The energy file ([TT]-e[tt]) contains energies, the temperature,",
99 "pressure, etc, a lot of these things are also printed in the log file.",
100 "Optionally coordinates can be written to a compressed trajectory file",
101 "([TT]-x[tt]).[PAR]",
102 "The option [TT]-dhdl[tt] is only used when free energy calculation is",
103 "turned on.[PAR]",
104 "Running mdrun efficiently in parallel is a complex topic,",
105 "many aspects of which are covered in the online User Guide. You",
106 "should look there for practical advice on using many of the options",
107 "available in mdrun.[PAR]",
108 "ED (essential dynamics) sampling and/or additional flooding potentials",
109 "are switched on by using the [TT]-ei[tt] flag followed by an [REF].edi[ref]",
110 "file. The [REF].edi[ref] file can be produced with the [TT]make_edi[tt] tool",
111 "or by using options in the essdyn menu of the WHAT IF program.",
112 "[TT]mdrun[tt] produces a [REF].xvg[ref] output file that",
113 "contains projections of positions, velocities and forces onto selected",
114 "eigenvectors.[PAR]",
115 "When user-defined potential functions have been selected in the",
116 "[REF].mdp[ref] file the [TT]-table[tt] option is used to pass [TT]mdrun[tt]",
117 "a formatted table with potential functions. The file is read from",
118 "either the current directory or from the [TT]GMXLIB[tt] directory.",
119 "A number of pre-formatted tables are presented in the [TT]GMXLIB[tt] dir,",
120 "for 6-8, 6-9, 6-10, 6-11, 6-12 Lennard-Jones potentials with",
121 "normal Coulomb.",
122 "When pair interactions are present, a separate table for pair interaction",
123 "functions is read using the [TT]-tablep[tt] option.[PAR]",
124 "When tabulated bonded functions are present in the topology,",
125 "interaction functions are read using the [TT]-tableb[tt] option.",
126 "For each different tabulated interaction type used, a table file name must",
127 "be given. For the topology to work, a file name given here must match a",
128 "character sequence before the file extension. That sequence is: an underscore,",
129 "then a 'b' for bonds, an 'a' for angles or a 'd' for dihedrals,",
130 "and finally the matching table number index used in the topology. Note that,",
131 "these options are deprecated, and in future will be available via grompp.[PAR]",
132 "The options [TT]-px[tt] and [TT]-pf[tt] are used for writing pull COM",
133 "coordinates and forces when pulling is selected",
134 "in the [REF].mdp[ref] file.",
135 "[PAR]",
136 "The option [TT]-membed[tt] does what used to be g_membed, i.e. embed",
137 "a protein into a membrane. This module requires a number of settings",
138 "that are provided in a data file that is the argument of this option.",
139 "For more details in membrane embedding, see the documentation in the",
140 "user guide. The options [TT]-mn[tt] and [TT]-mp[tt] are used to provide",
141 "the index and topology files used for the embedding.",
142 "[PAR]",
143 "The option [TT]-pforce[tt] is useful when you suspect a simulation",
144 "crashes due to too large forces. With this option coordinates and",
145 "forces of atoms with a force larger than a certain value will",
146 "be printed to stderr. It will also terminate the run when non-finite",
147 "forces are present.",
148 "[PAR]",
149 "Checkpoints containing the complete state of the system are written",
150 "at regular intervals (option [TT]-cpt[tt]) to the file [TT]-cpo[tt],",
151 "unless option [TT]-cpt[tt] is set to -1.",
152 "The previous checkpoint is backed up to [TT]state_prev.cpt[tt] to",
153 "make sure that a recent state of the system is always available,",
154 "even when the simulation is terminated while writing a checkpoint.",
155 "With [TT]-cpnum[tt] all checkpoint files are kept and appended",
156 "with the step number.",
157 "A simulation can be continued by reading the full state from file",
158 "with option [TT]-cpi[tt]. This option is intelligent in the way that",
159 "if no checkpoint file is found, GROMACS just assumes a normal run and",
160 "starts from the first step of the [REF].tpr[ref] file. By default the output",
161 "will be appending to the existing output files. The checkpoint file",
162 "contains checksums of all output files, such that you will never",
163 "loose data when some output files are modified, corrupt or removed.",
164 "There are three scenarios with [TT]-cpi[tt]:[PAR]",
165 "[TT]*[tt] no files with matching names are present: new output files are written[PAR]",
166 "[TT]*[tt] all files are present with names and checksums matching those stored",
167 "in the checkpoint file: files are appended[PAR]",
168 "[TT]*[tt] otherwise no files are modified and a fatal error is generated[PAR]",
169 "With [TT]-noappend[tt] new output files are opened and the simulation",
170 "part number is added to all output file names.",
171 "Note that in all cases the checkpoint file itself is not renamed",
172 "and will be overwritten, unless its name does not match",
173 "the [TT]-cpo[tt] option.",
174 "[PAR]",
175 "With checkpointing the output is appended to previously written",
176 "output files, unless [TT]-noappend[tt] is used or none of the previous",
177 "output files are present (except for the checkpoint file).",
178 "The integrity of the files to be appended is verified using checksums",
179 "which are stored in the checkpoint file. This ensures that output can",
180 "not be mixed up or corrupted due to file appending. When only some",
181 "of the previous output files are present, a fatal error is generated",
182 "and no old output files are modified and no new output files are opened.",
183 "The result with appending will be the same as from a single run.",
184 "The contents will be binary identical, unless you use a different number",
185 "of ranks or dynamic load balancing or the FFT library uses optimizations",
186 "through timing.",
187 "[PAR]",
188 "With option [TT]-maxh[tt] a simulation is terminated and a checkpoint",
189 "file is written at the first neighbor search step where the run time",
190 "exceeds [TT]-maxh[tt]\\*0.99 hours. This option is particularly useful in",
191 "combination with setting [TT]nsteps[tt] to -1 either in the mdp or using the",
192 "similarly named command line option (although the latter is deprecated).",
193 "This results in an infinite run,",
194 "terminated only when the time limit set by [TT]-maxh[tt] is reached (if any)",
195 "or upon receiving a signal.",
196 "[PAR]",
197 "Interactive molecular dynamics (IMD) can be activated by using at least one",
198 "of the three IMD switches: The [TT]-imdterm[tt] switch allows one to terminate",
199 "the simulation from the molecular viewer (e.g. VMD). With [TT]-imdwait[tt],",
200 "[TT]mdrun[tt] pauses whenever no IMD client is connected. Pulling from the",
201 "IMD remote can be turned on by [TT]-imdpull[tt].",
202 "The port [TT]mdrun[tt] listens to can be altered by [TT]-imdport[tt].The",
203 "file pointed to by [TT]-if[tt] contains atom indices and forces if IMD",
204 "pulling is used."
207 LegacyMdrunOptions options;
209 if (options.updateFromCommandLine(argc, argv, desc) == 0)
211 return 0;
214 StartingBehavior startingBehavior = StartingBehavior::NewSimulation;
215 LogFilePtr logFileGuard = nullptr;
216 std::tie(startingBehavior,
217 logFileGuard) = handleRestart(options.cr.get(),
218 options.ms.get(),
219 options.mdrunOptions.appendingBehavior,
220 ssize(options.filenames),
221 options.filenames.data());
223 /* The SimulationContext is a resource owned by the client code.
224 * A more complete design should address handles to resources with appropriate
225 * lifetimes and invariants for the resources allocated to the client,
226 * to the current simulation and to scheduled tasks within the simulation.
228 * \todo Clarify Context lifetime-management requirements and reconcile with scoped ownership.
230 auto simulationContext = createSimulationContext(std::move(options.cr));
232 /* The named components for the builder exposed here are descriptive of the
233 * state of mdrun at implementation and are not intended to be prescriptive
234 * of future design. (Note the ICommandLineOptions... framework used elsewhere.)
235 * The modules should ultimately take part in composing the Director code
236 * for an extensible Builder.
238 * In the near term, we assume that resources like domain decomposition and
239 * neighbor lists must be reinitialized between simulation segments.
240 * We would prefer to rebuild resources only as necessary, but we defer such
241 * details to future optimizations.
243 auto builder = MdrunnerBuilder(std::move(mdModules));
244 builder.addSimulationMethod(options.mdrunOptions, options.pforce, startingBehavior);
245 builder.addDomainDecomposition(options.domdecOptions);
246 // \todo pass by value
247 builder.addNonBonded(options.nbpu_opt_choices[0]);
248 // \todo pass by value
249 builder.addElectrostatics(options.pme_opt_choices[0], options.pme_fft_opt_choices[0]);
250 builder.addBondedTaskAssignment(options.bonded_opt_choices[0]);
251 builder.addNeighborList(options.nstlist_cmdline);
252 builder.addReplicaExchange(options.replExParams);
253 // \todo take ownership of multisim resources (ms)
254 builder.addMultiSim(options.ms.get());
255 builder.addCommunicationRecord(simulationContext.communicationRecord_.get());
256 // \todo Provide parallelism resources through SimulationContext.
257 // Need to establish run-time values from various inputs to provide a resource handle to Mdrunner
258 builder.addHardwareOptions(options.hw_opt);
259 // \todo File names are parameters that should be managed modularly through further factoring.
260 builder.addFilenames(options.filenames);
261 // Note: The gmx_output_env_t life time is not managed after the call to parse_common_args.
262 // \todo Implement lifetime management for gmx_output_env_t.
263 // \todo Output environment should be configured outside of Mdrunner and provided as a resource.
264 builder.addOutputEnvironment(options.oenv);
265 builder.addLogFile(logFileGuard.get());
267 auto runner = builder.build();
269 return runner.mdrunner();