8 #ifndef HIPACE_Parser_H_
9 #define HIPACE_Parser_H_
94 replaceArrWithParser<int>(pp_geom,
"is_periodic");
95 replaceArrWithParser<double>(pp_geom,
"prob_lo");
96 replaceArrWithParser<double>(pp_geom,
"prob_hi");
106 bool the_arena_is_managed =
false;
107 pp_amrex.
queryAdd(
"the_arena_is_managed", the_arena_is_managed);
110 std::string omp_threads =
"nosmt";
111 pp_amrex.
queryAdd(
"omp_threads", omp_threads);
122 bool error_detected =
false;
123 std::string assert_msg;
126 if (
x < (2.0*(std::numeric_limits<int>::max()/2+1))) {
127 if (std::ceil(
x) >= std::numeric_limits<int>::min()) {
128 result =
static_cast<int>(
x);
130 error_detected =
true;
131 assert_msg =
"Error: Negative overflow detected when casting " +
132 real_name +
" = " + std::to_string(
x) +
" to int";
135 error_detected =
true;
136 assert_msg =
"Error: Overflow detected when casting " +
137 real_name +
" = " + std::to_string(
x) +
" to int";
139 error_detected =
true;
140 assert_msg =
"Error: NaN detected when casting " + real_name +
" to int";
153 amrex::Long result = 0;
154 bool error_detected =
false;
155 std::string assert_msg;
159 if (
x < (2.0*(std::numeric_limits<amrex::Long>::max()/2+1))) {
160 if (std::ceil(
x) >= std::numeric_limits<amrex::Long>::min()) {
161 result =
static_cast<amrex::Long
>(
x);
163 error_detected =
true;
164 assert_msg =
"Error: Negative overflow detected when casting " +
165 real_name +
" = " + std::to_string(
x) +
" to amrex::Long";
168 error_detected =
true;
169 assert_msg =
"Error: Overflow detected when casting " +
170 real_name +
" = " + std::to_string(
x) +
" to amrex::Long";
172 error_detected =
true;
173 assert_msg =
"Error: NaN detected when casting " + real_name +
" to amrex::Long";
188 static std::set<std::string> recursive_symbols{};
190 parser.registerVariables(varnames);
192 std::set<std::string> symbols =
parser.symbols();
193 for (
auto const& v : varnames) symbols.erase(v);
204 for (
auto const& s : symbols) {
207 "Expressions contains recursive symbol " + s);
211 parser.setConstant(s, usr_constant->second);
216 recursive_symbols.insert(s);
218 recursive_symbols.erase(s);
228 parser.setConstant(s, phy_constant->second);
246 val =
parser.compileHost<0>()();
253 val =
static_cast<float>(
parser.compileHost<0>()());
274 val =
parser.compileHost<0>()();
279 std::string loc_str =
str;
282 std::string::size_type pos = 0;
283 while ( (pos=loc_str.find(
"{{")) != std::string::npos) {
284 loc_str.replace(pos, 2, std::string{char(1)});
286 while ( (pos=loc_str.rfind(
"}}")) != std::string::npos) {
287 loc_str.replace(pos, 2, std::string{char(2)});
291 static std::set<std::string> recursive_symbols{};
292 while ( (pos=loc_str.rfind(
"{")) != std::string::npos) {
293 const auto pos_end = loc_str.find(
"}", pos);
294 if (pos_end == std::string::npos) {
297 <<
"', unclosed brace!\n";
300 auto pos_start_trim = pos + 1;
301 auto pos_count_trim = pos_end - pos - 1;
303 while (loc_str.at(pos_start_trim) ==
' ' && pos_count_trim > 0) {
307 while (loc_str.at(pos_start_trim + pos_count_trim - 1) ==
' ' && pos_count_trim > 0) {
310 const std::string parse_string = loc_str.substr(pos_start_trim, pos_count_trim);
313 if (pos_count_trim == 0) {
314 loc_str.replace(pos, pos_end-pos+1,
"");
315 }
else if (pp_my_constants.
contains(parse_string.c_str())) {
317 if (recursive_symbols.count(parse_string) != 0) {
320 <<
"' contains recursive symbol '"
325 std::string replacer;
326 pp_my_constants.
get(parse_string.c_str(), replacer);
327 std::string parse_val;
328 recursive_symbols.insert(parse_string);
331 recursive_symbols.erase(parse_string);
332 loc_str.replace(pos, pos_end-pos+1, parse_val);
335 double parse_val = 0.;
337 std::stringstream ss{};
339 loc_str.replace(pos, pos_end-pos+1, ss.str());
343 if (do_escape_backtransform) {
345 while ( (pos=loc_str.find(
char(1))) != std::string::npos) {
346 loc_str.replace(pos, 1,
"{");
348 while ( (pos=loc_str.rfind(
char(2))) != std::string::npos) {
349 loc_str.replace(pos, 1,
"}");
367 if (!str_arr.empty()) {
369 for (
auto i=1ul ;
i != str_arr.size() ; ++
i) {
370 str +=
' ' + str_arr[
i];
379 auto const n = str_arr.size();
381 for (
auto i=0ul ;
i != n ; ++
i) {
389 auto const n = str_arr.size();
391 for (
auto i=0ul ;
i != n ; ++
i) {
396 template<
class T, std::
size_t size>
399 const auto n = str_arr.size();
401 for(
auto const&
str : str_arr) {
407 for (
auto i=0ul ;
i != n ; ++
i) {
414 const auto n = str_arr.size();
415 if (n != AMREX_SPACEDIM) {
416 for(
auto const&
str : str_arr) {
420 <<
" should be " << AMREX_SPACEDIM <<
'\n';
423 for (
auto i=0ul ;
i != n ; ++
i) {
430 const auto n = str_arr.size();
431 if (n != AMREX_SPACEDIM) {
432 for(
auto const&
str : str_arr) {
436 <<
" should be " << AMREX_SPACEDIM <<
'\n';
439 for (
auto i=0ul ;
i != n ; ++
i) {
454 std::vector<std::string> f;
468 std::vector<std::string> f;
522 std::string clean_str = func_str;
523 for (
char&
c : clean_str) {
524 if (
c==
'\n' ||
c==
'\r' ||
c==
'\t')
c =
' ';
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX, MSG)
#define AMREX_ALWAYS_ASSERT(EX)
bool queryWithParserAlt(const amrex::ParmParse &pp, char const *const str, T &val, const amrex::ParmParse &pp_alt)
return if input file contains the expression, if so it is parsed into val. The input is searched in p...
Definition: Parser.H:486
auto makeFunctionWithParser(std::string const &func_str, amrex::Parser &parser, amrex::Vector< std::string > const &varnames)
return function object for Host and Device from the input file
Definition: Parser.H:518
void getWithParserAlt(const amrex::ParmParse &pp, char const *const str, T &val, const amrex::ParmParse &pp_alt)
fill val with the evaluated expression from the input file The input is searched in pp first,...
Definition: Parser.H:502
bool queryWithParser(const amrex::ParmParse &pp, char const *const str, T &val)
return if input file contains the expression, if so it is parsed into val
Definition: Parser.H:467
void getWithParser(const amrex::ParmParse &pp, char const *const str, T &val)
fill val with the evaluated expression from the input file
Definition: Parser.H:453
bool contains(const char *name) const
void addarr(const char *name, const std::vector< int > &ref)
void get(const char *name, bool &ref, int ival=FIRST) const
int queryarr(const char *name, std::vector< int > &ref, int start_ix=FIRST, int num_val=ALL) const
int queryAdd(const char *name, T &ref)
void getarr(const char *name, std::vector< int > &ref, int start_ix=FIRST, int num_val=ALL) const
int remove(const char *name)
void add(const char *name, bool val)
int i
Definition: MakeOpenBoundary.py:152
void replaceAmrexParamsWithParser()
replace AMReX input parameters with Parsed version
Definition: Parser.H:92
void fillWithParserArr(std::vector< std::string > const &str_arr, T &val)
fill second argument val of array type with a value obtained through Parsing str_arr if val is just a...
Definition: Parser.H:365
void addConstantsToParser()
add Physical constants to Parser constants
Definition: Parser.H:46
amrex::Long safeCastToLong(const double x, const std::string &real_name)
return valid Long, asserts if inf or NaN
Definition: Parser.H:152
void replaceArrWithParser(amrex::ParmParse &pp, char const *const str)
array version of replaceWithParser
Definition: Parser.H:79
void initParser(amrex::Parser &parser, amrex::Vector< std::string > const &varnames)
init Parser ready to compile
Definition: Parser.H:185
void fillWithParser(std::string const &str, double &val)
fill second argument val with a value obtained through Parsing str for std::string: val is same as st...
Definition: Parser.H:243
std::map< std::string, double > my_constants_cache
Definition: Parser.cpp:10
void setDefaultParams()
set default ParmParse parameters before AMReX is initialized
Definition: Parser.H:102
int safeCastToInt(const double x, const std::string &real_name)
return valid int, asserts if inf or NaN
Definition: Parser.H:120
void replaceWithParser(amrex::ParmParse &pp, char const *const str)
replace ParmParse input with a Parsed version
Definition: Parser.H:64
std::map< std::string, double > hipace_constants
Definition: Parser.cpp:14
static constexpr auto m_p
Definition: Constants.H:22
static constexpr auto r_e
Definition: Constants.H:24
static constexpr auto mu0
Definition: Constants.H:19
static constexpr auto q_e
Definition: Constants.H:20
static constexpr auto hbar
Definition: Constants.H:23
static constexpr auto m_e
Definition: Constants.H:21
static constexpr auto c
Definition: Constants.H:17
static constexpr auto ep0
Definition: Constants.H:18
@ N
Definition: MultiLaser.H:92
AMREX_GPU_HOST_DEVICE Long size(T const &b) noexcept
std::ostream & ErrorStream()
void Abort(const std::string &msg)
str
Definition: checksumAPI.py:112
parser
Definition: checksumAPI.py:104