SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / fileio / readinp.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 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.
11  *
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.
16  *
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.
21  *
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.
26  *
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.
34  *
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.
37  */
38 #ifndef GMX_FILEIO_READINP_H
39 #define GMX_FILEIO_READINP_H
40
41 #include <cstring>
42
43 #include <string>
44 #include <utility>
45 #include <vector>
46
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"
52
53 namespace gmx
54 {
55 template<typename>
56 class ArrayRef;
57 class KeyValueTreeObject;
58 class TextInputStream;
59 class TextOutputStream;
60 } // namespace gmx
61
62 /* !\brief Input file structure that is populated with entries read from a file.
63  *
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.
68  */
69 struct t_inpfile
70 {
71     /*!\brief Minimum allowed constructor sets all elements */
72     t_inpfile(int         count,
73               int         inp_count,
74               bool        bObsolete,
75               bool        bSet,
76               bool        bHandledAsKeyValueTree,
77               std::string name,
78               std::string value) :
79         count_(count),
80         bObsolete_(bObsolete),
81         bSet_(bSet),
82         bHandledAsKeyValueTree_(bHandledAsKeyValueTree),
83         name_(std::move(name)),
84         value_(std::move(value)),
85         inp_count_(inp_count)
86     {
87     }
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. */
96 };
97
98 /*! \brief Create and return a vector of t_inpfile structs
99  * from "key = value" lines in \c stream corresponding to file \c fn.
100  *
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);
107
108 gmx::KeyValueTreeObject flatKeyValueTreeFromInpFile(gmx::ArrayRef<const t_inpfile> inp);
109
110 enum class WriteMdpHeader
111 {
112     no,
113     yes
114 };
115
116 /*! \brief Write "key = value" lines from \c inp to \c stream.
117  *
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,
127                    const char*             fn,
128                    std::vector<t_inpfile>* inp,
129                    gmx_bool                bHaltOnUnknown,
130                    WriteMdpHeader          writeHeader,
131                    warninp_t               wi);
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. */
135
136 void replace_inp_entry(gmx::ArrayRef<t_inpfile> inp, const char* old_entry, const char* new_entry);
137
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. */
141
142 void mark_einp_set(gmx::ArrayRef<t_inpfile> inp, const char* name);
143
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);
146
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);
149
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);
152
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);
155
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 */
160
161 int get_eenum(std::vector<t_inpfile>* inp, const char* name, const char** defs);
162 /* defs must be NULL terminated */
163
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);
166
167 /*! \brief Read option from input and return corresponding enum value
168  *
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.
171  *
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
177  */
178 template<typename EnumType>
179 EnumType getEnum(std::vector<t_inpfile>* inp, const char* name, warninp* wi)
180 {
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);
188     if (ii == -1)
189     {
190         // If the option wasn't set, we return EnumType::Default
191         inp->back().value_.assign(defaultName);
192         return defaultEnumValue;
193     }
194
195     // Check if option string can be mapped to a valid enum value.
196     //
197     // Note that this cannot be replaced with
198     // StringToEnumValueConverter until all instantiations of this
199     // function have a matching enumValueToString, and all of the
200     // latter are in the same namespace. Currently some of those
201     // function declarations are in gmx namespace and some are not.
202     const auto* optionString = (*inp)[ii].value_.c_str();
203     for (auto enumValue : gmx::EnumerationWrapper<EnumType>{})
204     {
205         if (gmx_strcasecmp_min(enumValueToString(enumValue), optionString) == 0)
206         {
207             return enumValue;
208         }
209     }
210
211     // If we get here, the option set is invalid. Print error.
212     std::string errorMessage = gmx::formatString(
213             "Invalid enum '%s' for variable %s, using '%s'\n", optionString, name, defaultName);
214     errorMessage += gmx::formatString("Next time, use one of:");
215     for (auto enumValue : gmx::EnumerationWrapper<EnumType>{})
216     {
217         errorMessage += gmx::formatString(" '%s'", enumValueToString(enumValue));
218     }
219     if (wi != nullptr)
220     {
221         warning_error(wi, errorMessage);
222     }
223     else
224     {
225         fprintf(stderr, "%s\n", errorMessage.c_str());
226     }
227     (*inp)[ii].value_.assign(defaultName);
228     return defaultEnumValue;
229 }
230
231
232 //! Replace for macro CCTYPE, prints comment string after newline
233 void printStringNewline(std::vector<t_inpfile>* inp, const char* line);
234 //! Replace for macro CTYPE, prints comment string
235 void printStringNoNewline(std::vector<t_inpfile>* inp, const char* line);
236 //! Replace for macro STYPE, checks for existing string entry and if possible replaces it
237 void setStringEntry(std::vector<t_inpfile>* inp, const char* name, char* newName, const char* def);
238
239 /*! \brief
240  * Returns a string value and sets the value in \p inp
241  *
242  * The value is either from \p inp when \p name is found or \p def otherwise.
243  *
244  * \note this is a wrapper function for g_estr()
245  */
246 std::string setStringEntry(std::vector<t_inpfile>* inp, const std::string& name, const std::string& def);
247
248 #endif