ARTEMIS
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  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 
132 
133  val = safeCastToInt(std::round(parser.compileHost<0>()()), str);
134  }
135  else {
136  val = static_cast<T>(parser.compileHost<0>()());
137  }
138  }
139  // return the same output as amrex::ParmParse::query
140  return is_specified;
141  }
142 
143 
144  template <typename T>
145  int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val)
146  {
147  // call amrex::ParmParse::query, check if the user specified str.
148  std::vector<std::string> tmp_str_arr;
149  int is_specified = a_pp.queryarr(str, tmp_str_arr);
150  if (is_specified)
151  {
152  // If so, create parser objects and apply them to the values provided by the user.
153  int const n = static_cast<int>(tmp_str_arr.size());
154  val.resize(n);
155  for (int i=0 ; i < n ; i++) {
156  auto parser = makeParser(tmp_str_arr[i], {});
158  val[i] = safeCastToInt(std::round(parser.compileHost<0>()()), str);
159  }
160  else {
161  val[i] = static_cast<T>(parser.compileHost<0>()());
162  }
163  }
164  }
165  // return the same output as amrex::ParmParse::query
166  return is_specified;
167  }
168 
169 
185  template <typename T>
186  int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val,
187  const int start_ix, const int num_val)
188  {
189  // call amrex::ParmParse::query, check if the user specified str.
190  std::vector<std::string> tmp_str_arr;
191  int is_specified = a_pp.queryarr(str, tmp_str_arr, start_ix, num_val);
192  if (is_specified)
193  {
194  // If so, create parser objects and apply them to the values provided by the user.
195  int const n = static_cast<int>(tmp_str_arr.size());
196  val.resize(n);
197  for (int i=0 ; i < n ; i++) {
198  auto parser = makeParser(tmp_str_arr[i], {});
200  val[i] = safeCastToInt(std::round(parser.compileHost<0>()()), str);
201  }
202  else {
203  val[i] = static_cast<T>(parser.compileHost<0>()());
204  }
205  }
206  }
207  // return the same output as amrex::ParmParse::query
208  return is_specified;
209  }
210 
211 
223  template <typename T>
224  void getWithParser (const amrex::ParmParse& a_pp, char const * const str, T& val)
225  {
226  // If so, create a parser object and apply it to the value provided by the user.
227  std::string str_val;
228  Store_parserString(a_pp, str, str_val);
229 
230  auto parser = makeParser(str_val, {});
232  val = safeCastToInt(std::round(parser.compileHost<0>()()), str);
233  }
234  else {
235  val = static_cast<T>(parser.compileHost<0>()());
236  }
237  }
238 
239  template <typename T>
240  void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val)
241  {
242  // Create parser objects and apply them to the values provided by the user.
243  std::vector<std::string> tmp_str_arr;
244  a_pp.getarr(str, tmp_str_arr);
245 
246  int const n = static_cast<int>(tmp_str_arr.size());
247  val.resize(n);
248  for (int i=0 ; i < n ; i++) {
249  auto parser = makeParser(tmp_str_arr[i], {});
251  val[i] = safeCastToInt(std::round(parser.compileHost<0>()()), str);
252  }
253  else {
254  val[i] = static_cast<T>(parser.compileHost<0>()());
255  }
256  }
257  }
258 
259 
275  template <typename T>
276  void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val,
277  const int start_ix, const int num_val)
278  {
279  // Create parser objects and apply them to the values provided by the user.
280  std::vector<std::string> tmp_str_arr;
281  a_pp.getarr(str, tmp_str_arr, start_ix, num_val);
282 
283  int const n = static_cast<int>(tmp_str_arr.size());
284  val.resize(n);
285  for (int i=0 ; i < n ; i++) {
286  auto parser = makeParser(tmp_str_arr[i], {});
288  val[i] = safeCastToInt(std::round(parser.compileHost<0>()()), str);
289  }
290  else {
291  val[i] = static_cast<T>(parser.compileHost<0>()());
292  }
293  }
294  }
295 
296 }
297 
298 #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
parser
Definition: run_alltests.py:111
int n
Definition: run_libensemble_on_warpx.py:67
string name
Definition: stencil.py:452
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:145
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:68
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:78
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:73
int parseStringtoInt(const std::string &str, const std::string &name)
Definition: ParserUtils.cpp:160
double parseStringtoDouble(const std::string &str)
Definition: ParserUtils.cpp:150
void getWithParser(const amrex::ParmParse &a_pp, char const *const str, T &val)
Definition: ParserUtils.H:224
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:240
int queryWithParser(const amrex::ParmParse &a_pp, char const *const str, T &val)
Definition: ParserUtils.H:118