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("utils::parser::getArrWithParser failed");
177 }
178 }
179
180
196 template <typename T>
197 void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val,
198 const int start_ix, const int num_val)
199 {
200 int const is_specified = queryArrWithParser(a_pp, str, val, start_ix, num_val);
201 if (is_specified == 0) {
202 throw std::runtime_error("utils::parser::getArrWithParser failed");
203 }
204 }
205
206
223 template <typename T>
224 int queryWithParser (const amrex::ParmParse& a_pp, std::string const& group, char const * const str, T& val)
225 {
226 const bool is_specified_without_group = a_pp.contains(str);
227 const std::string grp_str = group + "." + std::string(str);
228 const bool is_specified_with_group = (group.empty() ? false : a_pp.contains(grp_str.c_str()));
229
230 if (is_specified_without_group && !is_specified_with_group) {
231 // If found without the group but not with the group, then use the one without the group.
232 return queryWithParser(a_pp, str, val);
233 } else {
234 // Otherwise, use the one with the group even if not found, in which case an exception may be raised.
235 return queryWithParser(a_pp, grp_str.c_str(), val);
236 }
237 }
238
239
240 template <typename T>
241 int queryArrWithParser (const amrex::ParmParse& a_pp, std::string const& group, char const * const str, std::vector<T>& val)
242 {
243 const bool is_specified_without_group = a_pp.contains(str);
244 const std::string grp_str = group + "." + std::string(str);
245 const bool is_specified_with_group = (group.empty() ? false : a_pp.contains(grp_str.c_str()));
246
247 if (is_specified_without_group && !is_specified_with_group) {
248 // If found without the group but not with the group, then use the one without the group.
249 return queryArrWithParser(a_pp, str, val);
250 } else {
251 // Otherwise, use the one with the group even if not found, in which case an exception may be raised.
252 return queryArrWithParser(a_pp, grp_str.c_str(), val);
253 }
254 }
255
256
277 template <typename T>
278 int queryArrWithParser (const amrex::ParmParse& a_pp, std::string const& group, char const * const str, std::vector<T>& val,
279 const int start_ix, const int num_val)
280 {
281 const bool is_specified_without_group = a_pp.contains(str);
282 const std::string grp_str = group + "." + std::string(str);
283 const bool is_specified_with_group = (group.empty() ? false : a_pp.contains(grp_str.c_str()));
284
285 if (is_specified_without_group && !is_specified_with_group) {
286 // If found without the group but not with the group, then use the one without the group.
287 return queryArrWithParser(a_pp, str, val, start_ix, num_val);
288 } else {
289 // Otherwise, use the one with the group even if not found, in which case an exception may be raised.
290 return queryArrWithParser(a_pp, grp_str.c_str(), val, start_ix, num_val);
291 }
292 }
293
306 int query (const amrex::ParmParse& a_pp, std::string const& group, char const * str, std::string& val);
307
324 template <typename T>
325 void getWithParser (const amrex::ParmParse& a_pp, std::string const& group, char const * const str, T& val)
326 {
327 const bool is_specified_without_group = a_pp.contains(str);
328 const std::string grp_str = group + "." + std::string(str);
329 const bool is_specified_with_group = (group.empty() ? false : a_pp.contains(grp_str.c_str()));
330
331 if (is_specified_without_group && !is_specified_with_group) {
332 // If found without the group but not with the group, then use the one without the group.
333 getWithParser(a_pp, str, val);
334 } else {
335 // Otherwise, use the one with the group even if not found, in which case an exception may be raised.
336 getWithParser(a_pp, grp_str.c_str(), val);
337 }
338 }
339
340 template <typename T>
341 void getArrWithParser (const amrex::ParmParse& a_pp, std::string const& group, char const * const str, std::vector<T>& val)
342 {
343 const bool is_specified_without_group = a_pp.contains(str);
344 const std::string grp_str = group + "." + std::string(str);
345 const bool is_specified_with_group = (group.empty() ? false : a_pp.contains(grp_str.c_str()));
346
347 if (is_specified_without_group && !is_specified_with_group) {
348 // If found without the group but not with the group, then use the one without the group.
349 getArrWithParser(a_pp, str, val);
350 } else {
351 // Otherwise, use the one with the group even if not found, in which case an exception may be raised.
352 getArrWithParser(a_pp, grp_str.c_str(), val);
353 }
354 }
355
356
377 template <typename T>
378 void getArrWithParser (const amrex::ParmParse& a_pp, std::string const& group, char const * const str, std::vector<T>& val,
379 const int start_ix, const int num_val)
380 {
381 const bool is_specified_without_group = a_pp.contains(str);
382 const std::string grp_str = group + "." + std::string(str);
383 const bool is_specified_with_group = (group.empty() ? false : a_pp.contains(grp_str.c_str()));
384
385 if (is_specified_without_group && !is_specified_with_group) {
386 // If found without the group but not with the group, then use the one without the group.
387 getArrWithParser(a_pp, str, val, start_ix, num_val);
388 } else {
389 // Otherwise, use the one with the group even if not found, in which case an exception may be raised.
390 getArrWithParser(a_pp, grp_str.c_str(), val, start_ix, num_val);
391 }
392 }
393
406 void get (amrex::ParmParse const& a_pp, std::string const& group, char const * str, std::string& val);
407
408}
409
410#endif // WARPX_UTILS_PARSER_PARSERUTILS_H_
int queryAsDouble(std::string_view name, T &ref) const
void getAsDouble(std::string_view name, T &ref) const
bool contains(std::string_view name) const
int queryarrAsDouble(std::string_view name, int nvals, T *ptr) const
int countval(std::string_view name, int n=LAST) const
Definition IntervalsParser.H:17
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