Separate job script files for gmxapi package versions.
[gromacs.git] / src / gromacs / fileio / readinp.h
blob37034e7c7dc234a5afe3739cf2091c0ec165a8b7
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_READINP_H
39 #define GMX_FILEIO_READINP_H
41 #include <cstring>
43 #include <string>
44 #include <utility>
45 #include <vector>
47 #include "gromacs/utility/basedefinitions.h"
49 struct warninp;
50 typedef warninp* warninp_t;
52 namespace gmx
54 template<typename>
55 class ArrayRef;
56 class KeyValueTreeObject;
57 class TextInputStream;
58 class TextOutputStream;
59 } // namespace gmx
61 /* !\brief Input file structure that is populated with entries read from a file.
63 * This structure contains the information read from the mdp file that is later used
64 * to build the ir from it. It is first constructed with a set of names and values,
65 * and later populated when checking against the available options in readir.
66 * Uses the functions below to both check entries and set the information.
68 struct t_inpfile
70 /*!\brief Minimum allowed constructor sets all elements */
71 t_inpfile(int count,
72 int inp_count,
73 bool bObsolete,
74 bool bSet,
75 bool bHandledAsKeyValueTree,
76 std::string name,
77 std::string value) :
78 count_(count),
79 bObsolete_(bObsolete),
80 bSet_(bSet),
81 bHandledAsKeyValueTree_(bHandledAsKeyValueTree),
82 name_(std::move(name)),
83 value_(std::move(value)),
84 inp_count_(inp_count)
87 int count_; /* sort order for output */
88 bool bObsolete_; /* whether it is an obsolete param value */
89 bool bSet_; /* whether it it has been read out */
90 bool bHandledAsKeyValueTree_; /* whether it it has been handled with key-value machinery */
91 std::string name_; /* name of the parameter */
92 std::string value_; /* parameter value string */
93 int inp_count_; /* number of einps read. Only valid for the first item
94 in the inpfile list. */
97 /*! \brief Create and return a vector of t_inpfile structs
98 * from "key = value" lines in \c stream corresponding to file \c fn.
100 * \param[in] stream Text stream to read.
101 * \param[in] fn Filename corresponding to \c reader.
102 * \param[out] wi Handler for context-sensitive warnings.
103 * \throws std::bad_alloc If out of memory.
104 * \throws Anything the stream underlying \c reader can throw. */
105 std::vector<t_inpfile> read_inpfile(gmx::TextInputStream* stream, const char* fn, warninp_t wi);
107 gmx::KeyValueTreeObject flatKeyValueTreeFromInpFile(gmx::ArrayRef<const t_inpfile> inp);
109 enum class WriteMdpHeader
115 /*! \brief Write "key = value" lines from \c inp to \c stream.
117 * \param[in] stream Text stream to write.
118 * \param[in] fn Filename corresponding to \c stream.
119 * \param[in] inp vector of key-value pairs.
120 * \param[in] bHaltOnUnknown Whether to issue a fatal error if an unknown key is found.
121 * \param[in] writeHeader Whether to write a header recording some context a user might like.
122 * \param[out] wi Handler for context-sensitive warnings.
123 * \throws std::bad_alloc If out of memory.
124 * \throws Anything the stream underlying \c writer can throw. */
125 void write_inpfile(gmx::TextOutputStream* stream,
126 const char* fn,
127 std::vector<t_inpfile>* inp,
128 gmx_bool bHaltOnUnknown,
129 WriteMdpHeader writeHeader,
130 warninp_t wi);
131 /* Write inp to fn, warning (and perhaps halting) if any fields are
132 * unknown. The helpful header contains irreproducible content, so
133 * its writing can be suppressed to make testing more useful. */
135 void replace_inp_entry(gmx::ArrayRef<t_inpfile> inp, const char* old_entry, const char* new_entry);
137 int search_einp(gmx::ArrayRef<const t_inpfile> inp, const char* name);
138 /* Return the index of an .mdp field with the given name within the
139 * inp vector, if it exists. Return -1 if it does not exist. */
141 void mark_einp_set(gmx::ArrayRef<t_inpfile> inp, const char* name);
143 int get_eint(std::vector<t_inpfile>* inp, const char* name, int def, warninp_t wi);
144 int get_eint(std::vector<t_inpfile>* inp, const std::string& name, int def, warninp_t wi);
146 int64_t get_eint64(std::vector<t_inpfile>* inp, const char* name, int64_t def, warninp_t wi);
147 int64_t get_eint64(std::vector<t_inpfile>* inp, const std::string& name, int64_t def, warninp_t wi);
149 double get_ereal(std::vector<t_inpfile>* inp, const char* name, double def, warninp_t wi);
150 double get_ereal(std::vector<t_inpfile>* inp, const std::string& name, double def, warninp_t wi);
152 const char* get_estr(std::vector<t_inpfile>* inp, const char* name, const char* def);
153 const char* get_estr(std::vector<t_inpfile>* inp, const std::string& name, const char* def);
155 int get_eeenum(std::vector<t_inpfile>* inp, const char* name, const char** defs, warninp_t wi);
156 /* defs must be NULL terminated */
157 int get_eeenum(std::vector<t_inpfile>* inp, const std::string& name, const char** defs, warninp_t wi);
158 /* defs must be NULL terminated */
160 int get_eenum(std::vector<t_inpfile>* inp, const char* name, const char** defs);
161 /* defs must be NULL terminated */
163 //! Replace for macro CCTYPE, prints comment string after newline
164 void printStringNewline(std::vector<t_inpfile>* inp, const char* line);
165 //! Replace for macro CTYPE, prints comment string
166 void printStringNoNewline(std::vector<t_inpfile>* inp, const char* line);
167 //! Replace for macro STYPE, checks for existing string entry and if possible replaces it
168 void setStringEntry(std::vector<t_inpfile>* inp, const char* name, char* newName, const char* def);
170 /*! \brief
171 * Returns a string value and sets the value in \p inp
173 * The value is either from \p inp when \p name is found or \p def otherwise.
175 * \note this is a wrapper function for g_estr()
177 std::string setStringEntry(std::vector<t_inpfile>* inp, const std::string& name, const std::string& def);
179 #endif