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,2021, 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
47 #include "gromacs/fileio/warninp.h"
48 #include "gromacs/utility/basedefinitions.h"
49 #include "gromacs/utility/cstringutil.h"
50 #include "gromacs/utility/enumerationhelpers.h"
51 #include "gromacs/utility/stringutil.h"
57 class KeyValueTreeObject;
58 class TextInputStream;
59 class TextOutputStream;
62 /* !\brief Input file structure that is populated with entries read from a file.
64 * This structure contains the information read from the mdp file that is later used
65 * to build the ir from it. It is first constructed with a set of names and values,
66 * and later populated when checking against the available options in readir.
67 * Uses the functions below to both check entries and set the information.
71 /*!\brief Minimum allowed constructor sets all elements */
76 bool bHandledAsKeyValueTree,
80 bObsolete_(bObsolete),
82 bHandledAsKeyValueTree_(bHandledAsKeyValueTree),
83 name_(std::move(name)),
84 value_(std::move(value)),
88 int count_; /* sort order for output */
89 bool bObsolete_; /* whether it is an obsolete param value */
90 bool bSet_; /* whether it it has been read out */
91 bool bHandledAsKeyValueTree_; /* whether it it has been handled with key-value machinery */
92 std::string name_; /* name of the parameter */
93 std::string value_; /* parameter value string */
94 int inp_count_; /* number of einps read. Only valid for the first item
95 in the inpfile list. */
98 /*! \brief Create and return a vector of t_inpfile structs
99 * from "key = value" lines in \c stream corresponding to file \c fn.
101 * \param[in] stream Text stream to read.
102 * \param[in] fn Filename corresponding to \c reader.
103 * \param[out] wi Handler for context-sensitive warnings.
104 * \throws std::bad_alloc If out of memory.
105 * \throws Anything the stream underlying \c reader can throw. */
106 std::vector<t_inpfile> read_inpfile(gmx::TextInputStream* stream, const char* fn, warninp_t wi);
108 gmx::KeyValueTreeObject flatKeyValueTreeFromInpFile(gmx::ArrayRef<const t_inpfile> inp);
110 enum class WriteMdpHeader
116 /*! \brief Write "key = value" lines from \c inp to \c stream.
118 * \param[in] stream Text stream to write.
119 * \param[in] fn Filename corresponding to \c stream.
120 * \param[in] inp vector of key-value pairs.
121 * \param[in] bHaltOnUnknown Whether to issue a fatal error if an unknown key is found.
122 * \param[in] writeHeader Whether to write a header recording some context a user might like.
123 * \param[out] wi Handler for context-sensitive warnings.
124 * \throws std::bad_alloc If out of memory.
125 * \throws Anything the stream underlying \c writer can throw. */
126 void write_inpfile(gmx::TextOutputStream* stream,
128 std::vector<t_inpfile>* inp,
129 gmx_bool bHaltOnUnknown,
130 WriteMdpHeader writeHeader,
132 /* Write inp to fn, warning (and perhaps halting) if any fields are
133 * unknown. The helpful header contains irreproducible content, so
134 * its writing can be suppressed to make testing more useful. */
136 void replace_inp_entry(gmx::ArrayRef<t_inpfile> inp, const char* old_entry, const char* new_entry);
138 int search_einp(gmx::ArrayRef<const t_inpfile> inp, const char* name);
139 /* Return the index of an .mdp field with the given name within the
140 * inp vector, if it exists. Return -1 if it does not exist. */
142 void mark_einp_set(gmx::ArrayRef<t_inpfile> inp, const char* name);
144 int get_eint(std::vector<t_inpfile>* inp, const char* name, int def, warninp_t wi);
145 int get_eint(std::vector<t_inpfile>* inp, const std::string& name, int def, warninp_t wi);
147 int64_t get_eint64(std::vector<t_inpfile>* inp, const char* name, int64_t def, warninp_t wi);
148 int64_t get_eint64(std::vector<t_inpfile>* inp, const std::string& name, int64_t def, warninp_t wi);
150 double get_ereal(std::vector<t_inpfile>* inp, const char* name, double def, warninp_t wi);
151 double get_ereal(std::vector<t_inpfile>* inp, const std::string& name, double def, warninp_t wi);
153 const char* get_estr(std::vector<t_inpfile>* inp, const char* name, const char* def);
154 const char* get_estr(std::vector<t_inpfile>* inp, const std::string& name, const char* def);
156 int get_eeenum(std::vector<t_inpfile>* inp, const char* name, const char** defs, warninp_t wi);
157 /* defs must be NULL terminated */
158 int get_eeenum(std::vector<t_inpfile>* inp, const std::string& name, const char** defs, warninp_t wi);
159 /* defs must be NULL terminated */
161 int get_eenum(std::vector<t_inpfile>* inp, const char* name, const char** defs);
162 /* defs must be NULL terminated */
164 //! Get index of option `name`. Exposed here so that `getEnum` can access it.
165 int get_einp(std::vector<t_inpfile>* inp, const char* name);
167 /*! \brief Read option from input and return corresponding enum value
169 * If the option is not set, return the first value of the enum as default.
170 * Defined here so we don't need to instantiate the templates in the source file.
172 * \tparam EnumType The type of enum to be returned
173 * \param[in] inp The input file vector
174 * \param[in] name The name of the option to be read
175 * \param[out] wi Handler for context-sensitive warnings.
176 * \return Enum value corresponding to read input
178 template<typename EnumType>
179 EnumType getEnum(std::vector<t_inpfile>* inp, const char* name, warninp* wi)
181 // If there's no valid option, we'll use the EnumType::Default.
182 // Note, this assumes the enum is zero based, which is also assumed by
183 // EnumerationWrapper and EnumerationArray.
184 const auto defaultEnumValue = EnumType::Default;
185 const auto& defaultName = enumValueToString(defaultEnumValue);
186 // Get index of option in input
187 const auto ii = get_einp(inp, name);
190 // If the option wasn't set, we return EnumType::Default
191 inp->back().value_.assign(defaultName);
192 return defaultEnumValue;
195 // Check if option string can be mapped to a valid enum value
196 const auto* optionString = (*inp)[ii].value_.c_str();
197 for (auto enumValue : gmx::EnumerationWrapper<EnumType>{})
199 if (gmx_strcasecmp_min(enumValueToString(enumValue), optionString) == 0)
205 // If we get here, the option set is invalid. Print error.
206 std::string errorMessage = gmx::formatString(
207 "Invalid enum '%s' for variable %s, using '%s'\n", optionString, name, defaultName);
208 errorMessage += gmx::formatString("Next time, use one of:");
209 for (auto enumValue : gmx::EnumerationWrapper<EnumType>{})
211 errorMessage += gmx::formatString(" '%s'", enumValueToString(enumValue));
215 warning_error(wi, errorMessage);
219 fprintf(stderr, "%s\n", errorMessage.c_str());
221 (*inp)[ii].value_.assign(defaultName);
222 return defaultEnumValue;
226 //! Replace for macro CCTYPE, prints comment string after newline
227 void printStringNewline(std::vector<t_inpfile>* inp, const char* line);
228 //! Replace for macro CTYPE, prints comment string
229 void printStringNoNewline(std::vector<t_inpfile>* inp, const char* line);
230 //! Replace for macro STYPE, checks for existing string entry and if possible replaces it
231 void setStringEntry(std::vector<t_inpfile>* inp, const char* name, char* newName, const char* def);
234 * Returns a string value and sets the value in \p inp
236 * The value is either from \p inp when \p name is found or \p def otherwise.
238 * \note this is a wrapper function for g_estr()
240 std::string setStringEntry(std::vector<t_inpfile>* inp, const std::string& name, const std::string& def);