97e1fa47157e4099eeaa3a5d8083e2785a86cef5
[alexxy/gromacs.git] / src / gromacs / fileio / checkpoint.h
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) 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.
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
38 #ifndef _checkpoint_h
39 #define _checkpoint_h
40
41 #include <cstdio>
42
43 #include <vector>
44
45 #include "gromacs/math/vectypes.h"
46 #include "gromacs/utility/basedefinitions.h"
47
48 class energyhistory_t;
49 struct gmx_file_position_t;
50 struct ObservablesHistory;
51 struct t_commrec;
52 struct t_fileio;
53 struct t_inputrec;
54 class t_state;
55 struct t_trxframe;
56
57 /* the name of the environment variable to disable fsync failure checks with */
58 #define GMX_IGNORE_FSYNC_FAILURE_ENV "GMX_IGNORE_FSYNC_FAILURE"
59
60 // TODO Replace this mechanism with std::array<char, 1024> or similar.
61 #define CPTSTRLEN 1024
62
63 // TODO Expand this into being a container of all data for
64 // serialization of a checkpoint, which can be stored by the caller
65 // (e.g. so that mdrun doesn't have to open the checkpoint twice).
66 // This will separate issues of allocation from those of
67 // serialization, help separate comparison from reading, and have
68 // better defined transformation functions to/from trajectory frame
69 // data structures.
70 //
71 // Several fields were once written to checkpoint file headers, but
72 // have been removed. So that old files can continue to be read,
73 // the names of such fields contain the string "_UNUSED" so that it
74 // is clear they should not be used.
75 struct CheckpointHeaderContents
76 {
77     int         file_version;
78     char        version[CPTSTRLEN];
79     char        btime_UNUSED[CPTSTRLEN];
80     char        buser_UNUSED[CPTSTRLEN];
81     char        bhost_UNUSED[CPTSTRLEN];
82     int         double_prec;
83     char        fprog[CPTSTRLEN];
84     char        ftime[CPTSTRLEN];
85     int         eIntegrator;
86     int         simulation_part;
87     int64_t     step;
88     double      t;
89     int         nnodes;
90     ivec        dd_nc;
91     int         npme;
92     int         natoms;
93     int         ngtc;
94     int         nnhpres;
95     int         nhchainlength;
96     int         nlambda;
97     int         flags_state;
98     int         flags_eks;
99     int         flags_enh;
100     int         flagsPullHistory;
101     int         flags_dfh;
102     int         flags_awhh;
103     int         nED;
104     int         eSwapCoords;
105 };
106
107 /* Write a checkpoint to <fn>.cpt
108  * Appends the _step<step>.cpt with bNumberAndKeep,
109  * otherwise moves the previous <fn>.cpt to <fn>_prev.cpt
110  */
111 void write_checkpoint(const char *fn, gmx_bool bNumberAndKeep,
112                       FILE *fplog, const t_commrec *cr,
113                       ivec domdecCells, int nppnodes,
114                       int eIntegrator, int simulation_part,
115                       gmx_bool bExpanded, int elamstats,
116                       int64_t step, double t,
117                       t_state *state, ObservablesHistory *observablesHistory);
118
119 /* Loads a checkpoint from fn for run continuation.
120  * Generates a fatal error on system size mismatch.
121  * The master node reads the file
122  * and communicates all the modified number of steps,
123  * but not the state itself.
124  * With reproducibilityRequested warns about version, build, #ranks differences.
125  */
126 void load_checkpoint(const char *fn, t_fileio *logfio,
127                      const t_commrec *cr, const ivec dd_nc,
128                      t_inputrec *ir, t_state *state,
129                      ObservablesHistory *observablesHistory,
130                      gmx_bool reproducibilityRequested);
131
132 /* Read everything that can be stored in t_trxframe from a checkpoint file */
133 void read_checkpoint_trxframe(struct t_fileio *fp, t_trxframe *fr);
134
135 /* Print the complete contents of checkpoint file fn to out */
136 void list_checkpoint(const char *fn, FILE *out);
137
138 /*!\brief Read simulation step and part from a checkpoint file
139  *
140  * Used by tune_pme to handle tuning with a checkpoint file as part of the input.
141  *
142  * \param[in]  filename         Name of checkpoint file
143  * \param[out] simulation_part  The part of the simulation that wrote the checkpoint
144  * \param[out] step             The final step number of the simulation that wrote the checkpoint
145  *
146  * The output variables will both contain 0 if filename is NULL, the file
147  * does not exist, or is not readable. */
148 void read_checkpoint_part_and_step(const char  *filename,
149                                    int         *simulation_part,
150                                    int64_t     *step);
151
152 /*!\brief Return header information from an open checkpoint file.
153  *
154  * Used by mdrun to handle restarts
155  *
156  * \param[in]  fp               Handle to open checkpoint file
157  * \param[out] outputfiles      Container of output file names from the previous run. */
158 CheckpointHeaderContents
159 read_checkpoint_simulation_part_and_filenames(t_fileio                         *fp,
160                                               std::vector<gmx_file_position_t> *outputfiles);
161
162 #endif