Simplified uniform GPU selection in CMake
[gromacs.git] / src / gromacs / fileio / tpxio.h
blob5fc0bea2bce3b675c67b49982c45d42d541fb849
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) 2013,2014,2015,2016,2017 by the GROMACS development team.
7 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
38 #ifndef GMX_FILEIO_TPXIO_H
39 #define GMX_FILEIO_TPXIO_H
41 #include <cstdio>
43 #include <vector>
45 #include "gromacs/math/vectypes.h"
46 #include "gromacs/pbcutil/pbc.h"
47 #include "gromacs/utility/basedefinitions.h"
48 #include "gromacs/utility/real.h"
50 struct gmx_mtop_t;
51 struct t_atoms;
52 struct t_block;
53 struct t_inputrec;
54 class t_state;
55 struct t_topology;
57 namespace gmx
59 template<typename>
60 class ArrayRef;
62 /*! \libinternal
63 * \brief
64 * First part of the TPR file structure containing information about
65 * the general aspect of the system.
67 struct TpxFileHeader
69 //! Non zero if input_rec is present.
70 bool bIr = false;
71 //! Non zero if a box is present.
72 bool bBox = false;
73 //! Non zero if a topology is present.
74 bool bTop = false;
75 //! Non zero if coordinates are present.
76 bool bX = false;
77 //! Non zero if velocities are present.
78 bool bV = false;
79 //! Non zero if forces are present (no longer supported, but retained so old .tpr can be read)
80 bool bF = false;
81 //! The total number of atoms.
82 int natoms = 0;
83 //! The number of temperature coupling groups.
84 int ngtc = 0;
85 //! Current value of lambda.
86 real lambda = 0;
87 //! Current value of the alchemical state - not yet printed out.
88 int fep_state = 0;
89 /*a better decision will eventually (5.0 or later) need to be made
90 on how to treat the alchemical state of the system, which can now
91 vary through a simulation, and cannot be completely described
92 though a single lambda variable, or even a single state
93 index. Eventually, should probably be a vector. MRS*/
94 //! Size of the TPR body in chars (equal to number of bytes) during I/O.
95 int64_t sizeOfTprBody = 0;
96 //! File version.
97 int fileVersion = 0;
98 //! File generation.
99 int fileGeneration = 0;
100 //! If the tpr file was written in double precision.
101 bool isDouble = false;
104 /*! \brief
105 * Contains the partly deserialized contents of a TPR file.
107 * Convenience struct that holds a fully deserialized TPR file header,
108 * and the body of the TPR file as char buffer that can be deserialized
109 * independently from the header.
111 struct PartialDeserializedTprFile
113 //! The file header.
114 TpxFileHeader header;
115 //! The file body.
116 std::vector<char> body;
117 //! Flag for PBC needed by legacy implementation.
118 PbcType pbcType = PbcType::Unset;
122 * These routines handle reading and writing of preprocessed
123 * topology files in any of the following formats:
124 * TPR : topology in XDR format, portable accross platforms
126 * Files are written in the precision with which the source are compiled,
127 * but double and single precision can be read by either.
130 /*! \brief
131 * Read the header from a tpx file and then close it again.
133 * By setting \p canReadTopologyOnly to true, it is possible to read future
134 * versions too (we skip the changed inputrec), provided we havent
135 * changed the topology description. If it is possible to read
136 * the inputrec it will still be done even if canReadTopologyOnly is true.
138 * \param[in] fileName The name of the input file.
139 * \param[in] canReadTopologyOnly If reading the inputrec can be skipped or not.
140 * \returns An initialized and populated TPX File header object.
142 TpxFileHeader readTpxHeader(const char* fileName, bool canReadTopologyOnly);
144 void write_tpx_state(const char* fn, const t_inputrec* ir, const t_state* state, const gmx_mtop_t* mtop);
145 /* Write a file, and close it again.
148 /*! \brief
149 * Complete deserialization of TPR file into the individual data structures.
151 * If \p state is nullptr, only populates ir and mtop.
153 * \param[in] partialDeserializedTpr Struct with header and char buffer needed to populate system.
154 * \param[out] ir Input rec to populate.
155 * \param[out] state System state variables to populate.
156 * \param[out] x Separate vector for coordinates, deprecated.
157 * \param[out] v Separate vector for velocities, deprecated.
158 * \param[out] mtop Global topology to populate.
160 * \returns PBC flag.
162 PbcType completeTprDeserialization(PartialDeserializedTprFile* partialDeserializedTpr,
163 t_inputrec* ir,
164 t_state* state,
165 rvec* x,
166 rvec* v,
167 gmx_mtop_t* mtop);
169 //! Overload for final TPR deserialization when not using state vectors.
170 PbcType completeTprDeserialization(PartialDeserializedTprFile* partialDeserializedTpr,
171 t_inputrec* ir,
172 gmx_mtop_t* mtop);
174 /*! \brief
175 * Read a file to set up a simulation and close it after reading.
177 * Main function used to initialize simulations. Reads the input \p fn
178 * to populate the \p state, \p ir and \p mtop needed to run a simulations.
180 * This function returns the partial deserialized TPR file
181 * that can then be communicated to set up non-master nodes to run simulations.
183 * \param[in] fn Input file name.
184 * \param[out] ir Input parameters to be set, or nullptr.
185 * \param[out] state State variables for the simulation.
186 * \param[out] mtop Global simulation topolgy.
187 * \returns Struct with header and body in char vector.
189 PartialDeserializedTprFile read_tpx_state(const char* fn, t_inputrec* ir, t_state* state, gmx_mtop_t* mtop);
191 /*! \brief
192 * Read a file and close it again.
194 * Reads a topology input file and populates the fields if the passed
195 * variables are valid. It is possible to pass \p ir, \p natoms,
196 * \p x, \p v or \p mtop as nullptr to the function. In those cases,
197 * the variables will not be populated from the input file. Passing both
198 * \p x and \p v as nullptr is not supported. If both \p natoms and
199 * \p mtop are passed as valid objects to the function, the total atom
200 * number from \p mtop will be set in \p natoms. Otherwise \p natoms
201 * will not be changed. If \p box is valid, the box will be set from
202 * the information read in from the file.
204 * \param[in] fn Input file name.
205 * \param[out] ir Input parameters to be set, or nullptr.
206 * \param[out] box Box matrix.
207 * \param[out] natoms Total atom numbers to be set, or nullptr.
208 * \param[out] x Positions to be filled from file, or nullptr.
209 * \param[out] v Velocities to be filled from file, or nullptr.
210 * \param[out] mtop Topology to be populated, or nullptr.
211 * \returns ir->pbcType if it was read from the file.
213 PbcType read_tpx(const char* fn, t_inputrec* ir, matrix box, int* natoms, rvec* x, rvec* v, gmx_mtop_t* mtop);
215 PbcType read_tpx_top(const char* fn, t_inputrec* ir, matrix box, int* natoms, rvec* x, rvec* v, t_topology* top);
216 /* As read_tpx, but for the old t_topology struct */
218 gmx_bool fn2bTPX(const char* file);
219 /* return if *file is one of the TPX file types */
221 void pr_tpxheader(FILE* fp, int indent, const char* title, const TpxFileHeader* sh);
223 #endif