2346f0e00c3e6796055025983345caea3a402074
[alexxy/gromacs.git] / src / gromacs / fileio / tpxio.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 #ifndef GMX_FILEIO_TPXIO_H
38 #define GMX_FILEIO_TPXIO_H
39
40 #include <cstdio>
41
42 #include <vector>
43
44 #include "gromacs/math/vectypes.h"
45 #include "gromacs/utility/arrayref.h"
46 #include "gromacs/utility/basedefinitions.h"
47 #include "gromacs/utility/real.h"
48
49 struct gmx_mtop_t;
50 struct t_atoms;
51 struct t_block;
52 struct t_inputrec;
53 class t_state;
54 struct t_topology;
55
56 /*! \libinternal
57  * \brief
58  * First part of the TPR file structure containing information about
59  * the general aspect of the system.
60  */
61 struct TpxFileHeader
62 {
63     //! Non zero if input_rec is present.
64     bool bIr = false;
65     //! Non zero if a box is present.
66     bool bBox = false;
67     //! Non zero if a topology is present.
68     bool bTop = false;
69     //! Non zero if coordinates are present.
70     bool bX = false;
71     //! Non zero if velocities are present.
72     bool bV = false;
73     //! Non zero if forces are present (no longer supported, but retained so old .tpr can be read)
74     bool bF = false;
75     //! The total number of atoms.
76     int natoms = 0;
77     //! The number of temperature coupling groups.
78     int ngtc = 0;
79     //! Current value of lambda.
80     real lambda = 0;
81     //! Current value of the alchemical state - not yet printed out.
82     int fep_state = 0;
83     /*a better decision will eventually (5.0 or later) need to be made
84        on how to treat the alchemical state of the system, which can now
85        vary through a simulation, and cannot be completely described
86        though a single lambda variable, or even a single state
87        index. Eventually, should probably be a vector. MRS*/
88     //! Size of the TPR body in chars (equal to number of bytes) during I/O.
89     int64_t sizeOfTprBody = 0;
90     //! File version.
91     int fileVersion = 0;
92     //! File generation.
93     int fileGeneration = 0;
94     //! If the tpr file was written in double precision.
95     bool isDouble = false;
96 };
97
98 /*! \brief
99  * Contains the partly deserialized contents of a TPR file.
100  *
101  * Convenience struct that holds a fully deserialized TPR file header,
102  * and the body of the TPR file as char buffer that can be deserialized
103  * independently from the header.
104  */
105 struct PartialDeserializedTprFile
106 {
107     //! The file header.
108     TpxFileHeader header;
109     //! The file body.
110     std::vector<char> body;
111     //! Flag for PBC needed by legacy implementation.
112     int ePBC = -1;
113 };
114
115 /*
116  * These routines handle reading and writing of preprocessed
117  * topology files in any of the following formats:
118  * TPR : topology in XDR format, portable accross platforms
119  *
120  * Files are written in the precision with which the source are compiled,
121  * but double and single precision can be read by either.
122  */
123
124 /*! \brief
125  * Read the header from a tpx file and then close it again.
126  *
127  * By setting \p canReadTopologyOnly to true, it is possible to read future
128  * versions too (we skip the changed inputrec), provided we havent
129  * changed the topology description. If it is possible to read
130  * the inputrec it will still be done even if canReadTopologyOnly is true.
131  *
132  * \param[in] fileName The name of the input file.
133  * \param[in] canReadTopologyOnly If reading the inputrec can be skipped or not.
134  * \returns An initialized and populated TPX File header object.
135  */
136 TpxFileHeader readTpxHeader(const char* fileName, bool canReadTopologyOnly);
137
138 void write_tpx_state(const char* fn, const t_inputrec* ir, const t_state* state, const gmx_mtop_t* mtop);
139 /* Write a file, and close it again.
140  */
141
142 /*! \brief
143  * Complete deserialization of TPR file into the individual data structures.
144  *
145  * If \p state is nullptr, only populates ir and mtop.
146  *
147  * \param[in] partialDeserializedTpr Struct with header and char buffer needed to populate system.
148  * \param[out] ir Input rec to populate.
149  * \param[out] state System state variables to populate.
150  * \param[out] x Separate vector for coordinates, deprecated.
151  * \param[out] v Separate vector for velocities, deprecated.
152  * \param[out] mtop Global topology to populate.
153  *
154  * \returns PBC flag.
155  */
156 int completeTprDeserialization(PartialDeserializedTprFile* partialDeserializedTpr,
157                                t_inputrec*                 ir,
158                                t_state*                    state,
159                                rvec*                       x,
160                                rvec*                       v,
161                                gmx_mtop_t*                 mtop);
162
163 //! Overload for final TPR deserialization when not using state vectors.
164 int completeTprDeserialization(PartialDeserializedTprFile* partialDeserializedTpr,
165                                t_inputrec*                 ir,
166                                gmx_mtop_t*                 mtop);
167
168 /*! \brief
169  * Read a file to set up a simulation and close it after reading.
170  *
171  * Main function used to initialize simulations. Reads the input \p fn
172  * to populate the \p state, \p ir and \p mtop needed to run a simulations.
173  *
174  * This function returns the partial deserialized TPR file
175  * that can then be communicated to set up non-master nodes to run simulations.
176  *
177  * \param[in] fn Input file name.
178  * \param[out] ir Input parameters to be set, or nullptr.
179  * \param[out] state State variables for the simulation.
180  * \param[out] mtop Global simulation topolgy.
181  * \returns Struct with header and body in char vector.
182  */
183 PartialDeserializedTprFile read_tpx_state(const char* fn, t_inputrec* ir, t_state* state, gmx_mtop_t* mtop);
184
185 /*! \brief
186  * Read a file and close it again.
187  *
188  * Reads a topology input file and populates the fields if the passed
189  * variables are valid. It is possible to pass \p ir, \p natoms,
190  * \p x, \p v or \p mtop as nullptr to the function. In those cases,
191  * the variables will not be populated from the input file. Passing both
192  * \p x and \p v as nullptr is not supported. If both \p natoms and
193  * \p mtop are passed as valid objects to the function, the total atom
194  * number from \p mtop will be set in \p natoms. Otherwise \p natoms
195  * will not be changed. If \p box is valid, the box will be set from
196  * the information read in from the file.
197  *
198  * \param[in] fn Input file name.
199  * \param[out] ir Input parameters to be set, or nullptr.
200  * \param[out] box Box matrix.
201  * \param[out] natoms Total atom numbers to be set, or nullptr.
202  * \param[out] x Positions to be filled from file, or nullptr.
203  * \param[out] v Velocities to be filled from file, or nullptr.
204  * \param[out] mtop Topology to be populated, or nullptr.
205  * \returns ir->ePBC if it was read from the file.
206  */
207 int read_tpx(const char* fn, t_inputrec* ir, matrix box, int* natoms, rvec* x, rvec* v, gmx_mtop_t* mtop);
208
209 int read_tpx_top(const char* fn, t_inputrec* ir, matrix box, int* natoms, rvec* x, rvec* v, t_topology* top);
210 /* As read_tpx, but for the old t_topology struct */
211
212 gmx_bool fn2bTPX(const char* file);
213 /* return if *file is one of the TPX file types */
214
215 void pr_tpxheader(FILE* fp, int indent, const char* title, const TpxFileHeader* sh);
216
217 #endif