WarpX
ParserUtils.H
Go to the documentation of this file.
1 /* Copyright 2022 Andrew Myers, Burlen Loring, Luca Fedeli
2  * Maxence Thevenet, Remi Lehe, Revathi Jambunathan
3  *
4  * This file is part of WarpX.
5  *
6  * License: BSD-3-Clause-LBNL
7  */
8 
9 #ifndef WARPX_UTILS_PARSER_PARSERUTILS_H_
10 #define WARPX_UTILS_PARSER_PARSERUTILS_H_
11 
12 #include <AMReX_ParmParse.H>
13 #include <AMReX_Parser.H>
14 #include <AMReX_REAL.H>
15 #include <AMReX_Vector.H>
16 
17 #include <cmath>
18 #include <string>
19 #include <type_traits>
20 
21 namespace utils::parser
22 {
31  int
32  safeCastToInt(amrex::Real x, const std::string& real_name);
33 
34 
43  long
44  safeCastToLong(amrex::Real x, const std::string& real_name);
45 
46 
54  std::string const& parse_function,
55  amrex::Vector<std::string> const& varnames);
56 
57 
66  void Store_parserString(
67  const amrex::ParmParse &pp,
68  std::string query_string,
69  std::string& stored_string);
70 
71 
80  double parseStringtoDouble(const std::string& str);
81 
82 
92  int parseStringtoInt(const std::string& str, const std::string& name);
93 
94 
95  template <int N>
97  {
98  if (parser) {
99  return parser->compile<N>();
100  } else {
101  return amrex::ParserExecutor<N>{};
102  }
103  }
104 
105 
117  template <typename T>
118  int queryWithParser (const amrex::ParmParse& a_pp, char const * const str, T& val)
119  {
120  // call amrex::ParmParse::query, check if the user specified str.
121  std::string tmp_str;
122  const int is_specified = a_pp.query(str, tmp_str);
123  if (is_specified)
124  {
125  // If so, create a parser object and apply it to the value provided by the user.
126  std::string str_val;
127  Store_parserString(a_pp, str, str_val);
128 
129  auto parser = makeParser(str_val, {});
130 
131  if constexpr (std::is_same<T, int>::value) {
132 
133  val = safeCastToInt(
134  static_cast<amrex::Real>(std::round(parser.compileHost<0>()())), str);
135  }
136  else {
137  val = static_cast<T>(parser.compileHost<0>()());
138  }
139  }
140  // return the same output as amrex::ParmParse::query
141  return is_specified;
142  }
143 
144 
145  template <typename T>
146  int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val)
147  {
148  // call amrex::ParmParse::query, check if the user specified str.
149  std::vector<std::string> tmp_str_arr;
150  const int is_specified = a_pp.queryarr(str, tmp_str_arr);
151  if (is_specified)
152  {
153  // If so, create parser objects and apply them to the values provided by the user.
154  int const n = static_cast<int>(tmp_str_arr.size());
155  val.resize(n);
156  for (int i=0 ; i < n ; i++) {
157  auto parser = makeParser(tmp_str_arr[i], {});
158  if constexpr (std::is_same<T, int>::value) {
159  val[i] = safeCastToInt(
160  static_cast<amrex::Real>(std::round(parser.compileHost<0>()())), str);
161  }
162  else {
163  val[i] = static_cast<T>(parser.compileHost<0>()());
164  }
165  }
166  }
167  // return the same output as amrex::ParmParse::query
168  return is_specified;
169  }
170 
171 
187  template <typename T>
188  int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val,
189  const int start_ix, const int num_val)
190  {
191  // call amrex::ParmParse::query, check if the user specified str.
192  std::vector<std::string> tmp_str_arr;
193  const int is_specified = a_pp.queryarr(str, tmp_str_arr, start_ix, num_val);
194  if (is_specified)
195  {
196  // If so, create parser objects and apply them to the values provided by the user.
197  int const n = static_cast<int>(tmp_str_arr.size());
198  val.resize(n);
199  for (int i=0 ; i < n ; i++) {
200  auto parser = makeParser(tmp_str_arr[i], {});
201  if constexpr (std::is_same<T, int>::value) {
202  val[i] = safeCastToInt(
203  static_cast<amrex::Real>(std::round(parser.compileHost<0>()())), str);
204  }
205  else {
206  val[i] = static_cast<T>(parser.compileHost<0>()());
207  }
208  }
209  }
210  // return the same output as amrex::ParmParse::query
211  return is_specified;
212  }
213 
214 
226  template <typename T>
227  void getWithParser (const amrex::ParmParse& a_pp, char const * const str, T& val)
228  {
229  // If so, create a parser object and apply it to the value provided by the user.
230  std::string str_val;
231  Store_parserString(a_pp, str, str_val);
232 
233  auto parser = makeParser(str_val, {});
234  if constexpr (std::is_same<T, int>::value) {
235  val = safeCastToInt(
236  static_cast<amrex::Real>(std::round(parser.compileHost<0>()())), str);
237  }
238  else {
239  val = static_cast<T>(parser.compileHost<0>()());
240  }
241  }
242 
243  template <typename T>
244  void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val)
245  {
246  // Create parser objects and apply them to the values provided by the user.
247  std::vector<std::string> tmp_str_arr;
248  a_pp.getarr(str, tmp_str_arr);
249 
250  int const n = static_cast<int>(tmp_str_arr.size());
251  val.resize(n);
252  for (int i=0 ; i < n ; i++) {
253  auto parser = makeParser(tmp_str_arr[i], {});
254  if constexpr (std::is_same<T, int>::value) {
255  val[i] = safeCastToInt(
256  static_cast<amrex::Real>(std::round(parser.compileHost<0>()())), str);
257  }
258  else {
259  val[i] = static_cast<T>(parser.compileHost<0>()());
260  }
261  }
262  }
263 
264 
280  template <typename T>
281  void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val,
282  const int start_ix, const int num_val)
283  {
284  // Create parser objects and apply them to the values provided by the user.
285  std::vector<std::string> tmp_str_arr;
286  a_pp.getarr(str, tmp_str_arr, start_ix, num_val);
287 
288  int const n = static_cast<int>(tmp_str_arr.size());
289  val.resize(n);
290  for (int i=0 ; i < n ; i++) {
291  auto parser = makeParser(tmp_str_arr[i], {});
292  if constexpr (std::is_same<T, int>::value) {
293  val[i] = safeCastToInt(
294  static_cast<amrex::Real>(std::round(parser.compileHost<0>()())), str);
295  }
296  else {
297  val[i] = static_cast<T>(parser.compileHost<0>()());
298  }
299  }
300  }
301 
302 }
303 
304 #endif // WARPX_UTILS_PARSER_PARSERUTILS_H_
int queryarr(const char *name, std::vector< int > &ref, int start_ix=FIRST, int num_val=ALL) const
void getarr(const char *name, std::vector< int > &ref, int start_ix=FIRST, int num_val=ALL) const
int query(const char *name, bool &ref, int ival=FIRST) const
i
Definition: check_interp_points_and_weights.py:174
str
Definition: run_alltests_1node.py:72
name
Definition: run_automated.py:229
int n
Definition: run_libensemble_on_warpx.py:70
parser
Definition: stencil.py:411
value
Definition: updateAMReX.py:141
Definition: IntervalsParser.H:17
int queryArrWithParser(const amrex::ParmParse &a_pp, char const *const str, std::vector< T > &val)
Definition: ParserUtils.H:146
int safeCastToInt(amrex::Real x, const std::string &real_name)
Do a safe cast of a real to an int This ensures that the float value is within the range of ints and ...
Definition: ParserUtils.cpp:69
amrex::Parser makeParser(std::string const &parse_function, amrex::Vector< std::string > const &varnames)
Initialize an amrex::Parser object from a string containing a math expression.
Definition: ParserUtils.cpp:79
long safeCastToLong(amrex::Real x, const std::string &real_name)
Do a safe cast of a real to a long This ensures that the float value is within the range of longs and...
Definition: ParserUtils.cpp:74
int parseStringtoInt(const std::string &str, const std::string &name)
Definition: ParserUtils.cpp:161
double parseStringtoDouble(const std::string &str)
Definition: ParserUtils.cpp:151
void getWithParser(const amrex::ParmParse &a_pp, char const *const str, T &val)
Definition: ParserUtils.H:227
void Store_parserString(const amrex::ParmParse &pp, std::string query_string, std::string &stored_string)
Parse a string (typically a mathematical expression) from the input file and store it into a variable...
Definition: ParserUtils.cpp:21
amrex::ParserExecutor< N > compileParser(amrex::Parser const *parser)
Definition: ParserUtils.H:96
void getArrWithParser(const amrex::ParmParse &a_pp, char const *const str, std::vector< T > &val)
Definition: ParserUtils.H:244
int queryWithParser(const amrex::ParmParse &a_pp, char const *const str, T &val)
Definition: ParserUtils.H:118