WarpX
Loading...
Searching...
No Matches
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
21namespace utils::parser
22{
29 amrex::Parser makeParser (
30 std::string const& parse_function,
31 amrex::Vector<std::string> const& varnames);
32
33
43 amrex::ParmParse const& pp,
44 std::string const& query_string,
45 std::string& stored_string);
46
61 const amrex::ParmParse &pp,
62 std::string const& group,
63 std::string const& query_string,
64 std::string& stored_string);
65
75 amrex::ParmParse const& pp,
76 std::string const& query_string,
77 std::string& stored_string);
78
79 template <int N>
81 {
82 if (parser) {
83 return parser->compile<N>();
84 } else {
86 }
87 }
88
89
101 template <typename T>
102 int queryWithParser (const amrex::ParmParse& a_pp, char const * const str, T& val)
103 {
104 return a_pp.queryAsDouble(str, val);
105 }
106
107
108 template <typename T>
109 int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val)
110 {
111 auto nvals = a_pp.countval(str);
112 if (nvals > 0) {
113 val.resize(nvals);
114 return a_pp.queryarrAsDouble(str, nvals, val.data());
115 } else {
116 return 0;
117 }
118 }
119
120
136 template <typename T>
137 int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val,
138 const int start_ix, const int num_val)
139 {
140 const int is_specified = queryArrWithParser(a_pp, str, val);
141 if (is_specified)
142 {
143 val.erase(val.begin(), val.begin()+start_ix);
144 if (num_val != amrex::ParmParse::LAST) {
145 if (num_val > int(val.size())) { return 0; }
146 val.resize(num_val);
147 }
148 }
149 // return the same output as amrex::ParmParse::query
150 return is_specified;
151 }
152
153
165 template <typename T>
166 void getWithParser (const amrex::ParmParse& a_pp, char const * const str, T& val)
167 {
168 a_pp.getAsDouble(str, val);
169 }
170
171 template <typename T>
172 void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val)
173 {
174 int const is_specified = queryArrWithParser(a_pp, str, val);
175 if (is_specified == 0) {
176 throw std::runtime_error(
177 "utils::parser::getArrWithParser: must specify the parameter '" +
178 a_pp.prefixedName(str) + "' in the inputs"
179 );
180 }
181 }
182
183
199 template <typename T>
200 void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val,
201 const int start_ix, const int num_val)
202 {
203 int const is_specified = queryArrWithParser(a_pp, str, val, start_ix, num_val);
204 if (is_specified == 0) {
205 throw std::runtime_error(
206 "utils::parser::getArrWithParser: must specify the parameter '" +
207 a_pp.prefixedName(str) + "' in the inputs"
208 );
209 }
210 }
211
212
229 template <typename T>
230 int queryWithParser (const amrex::ParmParse& a_pp, std::string const& group, char const * const str, T& val)
231 {
232 const bool is_specified_without_group = a_pp.contains(str);
233 const std::string grp_str = group + "." + std::string(str);
234 const bool is_specified_with_group = (group.empty() ? false : a_pp.contains(grp_str));
235
236 if (is_specified_without_group && !is_specified_with_group) {
237 // If found without the group but not with the group, then use the one without the group.
238 return queryWithParser(a_pp, str, val);
239 } else {
240 // Otherwise, use the one with the group even if not found, in which case an exception may be raised.
241 return queryWithParser(a_pp, grp_str.c_str(), val);
242 }
243 }
244
245
246 template <typename T>
247 int queryArrWithParser (const amrex::ParmParse& a_pp, std::string const& group, char const * const str, std::vector<T>& val)
248 {
249 const bool is_specified_without_group = a_pp.contains(str);
250 const std::string grp_str = group + "." + std::string(str);
251 const bool is_specified_with_group = (group.empty() ? false : a_pp.contains(grp_str));
252
253 if (is_specified_without_group && !is_specified_with_group) {
254 // If found without the group but not with the group, then use the one without the group.
255 return queryArrWithParser(a_pp, str, val);
256 } else {
257 // Otherwise, use the one with the group even if not found, in which case an exception may be raised.
258 return queryArrWithParser(a_pp, grp_str.c_str(), val);
259 }
260 }
261
262
283 template <typename T>
284 int queryArrWithParser (const amrex::ParmParse& a_pp, std::string const& group, char const * const str, std::vector<T>& val,
285 const int start_ix, const int num_val)
286 {
287 const bool is_specified_without_group = a_pp.contains(str);
288 const std::string grp_str = group + "." + std::string(str);
289 const bool is_specified_with_group = (group.empty() ? false : a_pp.contains(grp_str));
290
291 if (is_specified_without_group && !is_specified_with_group) {
292 // If found without the group but not with the group, then use the one without the group.
293 return queryArrWithParser(a_pp, str, val, start_ix, num_val);
294 } else {
295 // Otherwise, use the one with the group even if not found, in which case an exception may be raised.
296 return queryArrWithParser(a_pp, grp_str.c_str(), val, start_ix, num_val);
297 }
298 }
299
312 int query (const amrex::ParmParse& a_pp, std::string const& group, char const * str, std::string& val);
313
330 template <typename T>
331 void getWithParser (const amrex::ParmParse& a_pp, std::string const& group, char const * const str, T& val)
332 {
333 const bool is_specified_without_group = a_pp.contains(str);
334 const std::string grp_str = group + "." + std::string(str);
335 const bool is_specified_with_group = (group.empty() ? false : a_pp.contains(grp_str));
336
337 if (is_specified_without_group && !is_specified_with_group) {
338 // If found without the group but not with the group, then use the one without the group.
339 getWithParser(a_pp, str, val);
340 } else {
341 // Otherwise, use the one with the group even if not found, in which case an exception may be raised.
342 getWithParser(a_pp, grp_str.c_str(), val);
343 }
344 }
345
346 template <typename T>
347 void getArrWithParser (const amrex::ParmParse& a_pp, std::string const& group, char const * const str, std::vector<T>& val)
348 {
349 const bool is_specified_without_group = a_pp.contains(str);
350 const std::string grp_str = group + "." + std::string(str);
351 const bool is_specified_with_group = (group.empty() ? false : a_pp.contains(grp_str));
352
353 if (is_specified_without_group && !is_specified_with_group) {
354 // If found without the group but not with the group, then use the one without the group.
355 getArrWithParser(a_pp, str, val);
356 } else {
357 // Otherwise, use the one with the group even if not found, in which case an exception may be raised.
358 getArrWithParser(a_pp, grp_str.c_str(), val);
359 }
360 }
361
362
383 template <typename T>
384 void getArrWithParser (const amrex::ParmParse& a_pp, std::string const& group, char const * const str, std::vector<T>& val,
385 const int start_ix, const int num_val)
386 {
387 const bool is_specified_without_group = a_pp.contains(str);
388 const std::string grp_str = group + "." + std::string(str);
389 const bool is_specified_with_group = (group.empty() ? false : a_pp.contains(grp_str));
390
391 if (is_specified_without_group && !is_specified_with_group) {
392 // If found without the group but not with the group, then use the one without the group.
393 getArrWithParser(a_pp, str, val, start_ix, num_val);
394 } else {
395 // Otherwise, use the one with the group even if not found, in which case an exception may be raised.
396 getArrWithParser(a_pp, grp_str.c_str(), val, start_ix, num_val);
397 }
398 }
399
412 void get (amrex::ParmParse const& a_pp, std::string const& group, char const * str, std::string& val);
413
414}
415
416#endif // WARPX_UTILS_PARSER_PARSERUTILS_H_
int queryarrAsDouble(std::string_view name, int nvals, T *ptr) const
bool contains(std::string_view name) const
int queryAsDouble(std::string_view name, T &ref) const
std::string prefixedName(std::string_view str) const
void getAsDouble(std::string_view name, T &ref) const
int countval(std::string_view name, int n=LAST) const
Definition BTDIntervalsParser.H:18
int queryArrWithParser(const amrex::ParmParse &a_pp, char const *const str, std::vector< T > &val)
Definition ParserUtils.H:109
amrex::ParserExecutor< N > compileParser(amrex::Parser const *parser)
Definition ParserUtils.H:80
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:97
void getWithParser(const amrex::ParmParse &a_pp, char const *const str, T &val)
Definition ParserUtils.H:166
int query(const amrex::ParmParse &a_pp, std::string const &group, char const *str, std::string &val)
Definition ParserUtils.cpp:67
void Store_parserString(amrex::ParmParse const &pp, std::string const &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
void getArrWithParser(const amrex::ParmParse &a_pp, char const *const str, std::vector< T > &val)
Definition ParserUtils.H:172
void get(amrex::ParmParse const &a_pp, std::string const &group, char const *str, std::string &val)
Definition ParserUtils.cpp:82
bool Query_parserString(amrex::ParmParse const &pp, std::string const &query_string, std::string &stored_string)
If the input is provided, parse the string (typically a mathematical expression) from the input file ...
Definition ParserUtils.cpp:54
int queryWithParser(const amrex::ParmParse &a_pp, char const *const str, T &val)
Definition ParserUtils.H:102