diff options
Diffstat (limited to 'Source/Utils')
-rw-r--r-- | Source/Utils/WarpXUtil.H | 148 | ||||
-rw-r--r-- | Source/Utils/WarpXUtil.cpp | 129 |
2 files changed, 130 insertions, 147 deletions
diff --git a/Source/Utils/WarpXUtil.H b/Source/Utils/WarpXUtil.H index f5f434932..9360c14d4 100644 --- a/Source/Utils/WarpXUtil.H +++ b/Source/Utils/WarpXUtil.H @@ -247,8 +247,56 @@ amrex::ParserExecutor<N> compileParser (amrex::Parser const* parser) * \param[in] str name of the parameter to read * \param[out] val where the value queried and parsed is stored, either a scalar or vector */ -int queryWithParser (const amrex::ParmParse& a_pp, char const * const str, float& val); -int queryWithParser (const amrex::ParmParse& a_pp, char const * const str, double& val); +template <typename T> +int queryWithParser (const amrex::ParmParse& a_pp, char const * const str, T& val) +{ + // call amrex::ParmParse::query, check if the user specified str. + std::string tmp_str; + int is_specified = a_pp.query(str, tmp_str); + if (is_specified) + { + // If so, create a parser object and apply it to the value provided by the user. + std::string str_val; + Store_parserString(a_pp, str, str_val); + + auto parser = makeParser(str_val, {}); + + if (std::is_same<T, int>::value) { + + val = safeCastToInt(std::round(parser.compileHost<0>()()), str); + } + else { + val = static_cast<T>(parser.compileHost<0>()()); + } + } + // return the same output as amrex::ParmParse::query + return is_specified; +} + +template <typename T> +int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val) +{ + // call amrex::ParmParse::query, check if the user specified str. + std::vector<std::string> tmp_str_arr; + int is_specified = a_pp.queryarr(str, tmp_str_arr); + if (is_specified) + { + // If so, create parser objects and apply them to the values provided by the user. + int const n = static_cast<int>(tmp_str_arr.size()); + val.resize(n); + for (int i=0 ; i < n ; i++) { + auto parser = makeParser(tmp_str_arr[i], {}); + if (std::is_same<T, int>::value) { + val[i] = safeCastToInt(std::round(parser.compileHost<0>()()), str); + } + else { + val[i] = static_cast<T>(parser.compileHost<0>()()); + } + } + } + // return the same output as amrex::ParmParse::query + return is_specified; +} /** Similar to amrex::ParmParse::query, but also supports math expressions for the value. * @@ -265,13 +313,31 @@ int queryWithParser (const amrex::ParmParse& a_pp, char const * const str, doubl * \param[in] num_val number of input values to use (optional with arrays, default is * amrex::ParmParse::LAST for reading until the last input value) */ -int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<amrex::Real>& val, - const int start_ix = amrex::ParmParse::FIRST, - const int num_val = amrex::ParmParse::LAST); -int queryWithParser (const amrex::ParmParse& a_pp, char const * const str, int& val); -int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<int>& val, - const int start_ix = amrex::ParmParse::FIRST, - const int num_val = amrex::ParmParse::LAST); +template <typename T> +int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val, + const int start_ix, const int num_val) +{ + // call amrex::ParmParse::query, check if the user specified str. + std::vector<std::string> tmp_str_arr; + int is_specified = a_pp.queryarr(str, tmp_str_arr, start_ix, num_val); + if (is_specified) + { + // If so, create parser objects and apply them to the values provided by the user. + int const n = static_cast<int>(tmp_str_arr.size()); + val.resize(n); + for (int i=0 ; i < n ; i++) { + auto parser = makeParser(tmp_str_arr[i], {}); + if (std::is_same<T, int>::value) { + val[i] = safeCastToInt(std::round(parser.compileHost<0>()()), str); + } + else { + val[i] = static_cast<T>(parser.compileHost<0>()()); + } + } + } + // return the same output as amrex::ParmParse::query + return is_specified; +} /** Similar to amrex::ParmParse::get, but also supports math expressions for the value. * @@ -284,8 +350,41 @@ int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, st * \param[in] str name of the parameter to read * \param[out] val where the value queried and parsed is stored */ -void getWithParser (const amrex::ParmParse& a_pp, char const * const str, float& val); -void getWithParser (const amrex::ParmParse& a_pp, char const * const str, double& val); +template <typename T> +void getWithParser (const amrex::ParmParse& a_pp, char const * const str, T& val) +{ + // If so, create a parser object and apply it to the value provided by the user. + std::string str_val; + Store_parserString(a_pp, str, str_val); + + auto parser = makeParser(str_val, {}); + if (std::is_same<T, int>::value) { + val = safeCastToInt(std::round(parser.compileHost<0>()()), str); + } + else { + val = static_cast<T>(parser.compileHost<0>()()); + } +} + +template <typename T> +void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val) +{ + // Create parser objects and apply them to the values provided by the user. + std::vector<std::string> tmp_str_arr; + a_pp.getarr(str, tmp_str_arr); + + int const n = static_cast<int>(tmp_str_arr.size()); + val.resize(n); + for (int i=0 ; i < n ; i++) { + auto parser = makeParser(tmp_str_arr[i], {}); + if (std::is_same<T, int>::value) { + val[i] = safeCastToInt(std::round(parser.compileHost<0>()()), str); + } + else { + val[i] = static_cast<T>(parser.compileHost<0>()()); + } + } +} /** Similar to amrex::ParmParse::get, but also supports math expressions for the value. * @@ -302,13 +401,26 @@ void getWithParser (const amrex::ParmParse& a_pp, char const * const str, double * \param[in] num_val number of input values to use (optional with arrays, default is * amrex::ParmParse::LAST for reading until the last input value) */ -void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<amrex::Real>& val, - const int start_ix = amrex::ParmParse::FIRST, - const int num_val = amrex::ParmParse::LAST); -void getWithParser (const amrex::ParmParse& a_pp, char const * const str, int& val); -void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<int>& val, - const int start_ix = amrex::ParmParse::FIRST, - const int num_val = amrex::ParmParse::LAST); +template <typename T> +void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<T>& val, + const int start_ix, const int num_val) +{ + // Create parser objects and apply them to the values provided by the user. + std::vector<std::string> tmp_str_arr; + a_pp.getarr(str, tmp_str_arr, start_ix, num_val); + + int const n = static_cast<int>(tmp_str_arr.size()); + val.resize(n); + for (int i=0 ; i < n ; i++) { + auto parser = makeParser(tmp_str_arr[i], {}); + if (std::is_same<T, int>::value) { + val[i] = safeCastToInt(std::round(parser.compileHost<0>()()), str); + } + else { + val[i] = static_cast<T>(parser.compileHost<0>()()); + } + } +} namespace WarpXUtilStr { diff --git a/Source/Utils/WarpXUtil.cpp b/Source/Utils/WarpXUtil.cpp index 9ab96873a..afe8b7daa 100644 --- a/Source/Utils/WarpXUtil.cpp +++ b/Source/Utils/WarpXUtil.cpp @@ -388,135 +388,6 @@ parseStringtoInt(std::string str, std::string name) return ival; } -// Overloads for float/double instead of amrex::Real to allow makeParser() to query for -// my_constants as double even in single precision mode -// Always parsing in double precision avoids potential overflows that may occur when parsing user's -// expressions because of the limited range of exponentials in single precision -int -queryWithParser (const amrex::ParmParse& a_pp, char const * const str, float& val) -{ - // call amrex::ParmParse::query, check if the user specified str. - std::string tmp_str; - int is_specified = a_pp.query(str, tmp_str); - if (is_specified) - { - // If so, create a parser object and apply it to the value provided by the user. - std::string str_val; - Store_parserString(a_pp, str, str_val); - val = static_cast<float>(parseStringtoReal(str_val)); - } - // return the same output as amrex::ParmParse::query - return is_specified; -} - -void -getWithParser (const amrex::ParmParse& a_pp, char const * const str, float& val) -{ - // If so, create a parser object and apply it to the value provided by the user. - std::string str_val; - Store_parserString(a_pp, str, str_val); - val = static_cast<float>(parseStringtoReal(str_val)); -} - -int -queryWithParser (const amrex::ParmParse& a_pp, char const * const str, double& val) -{ - // call amrex::ParmParse::query, check if the user specified str. - std::string tmp_str; - int is_specified = a_pp.query(str, tmp_str); - if (is_specified) - { - // If so, create a parser object and apply it to the value provided by the user. - std::string str_val; - Store_parserString(a_pp, str, str_val); - val = parseStringtoReal(str_val); - } - // return the same output as amrex::ParmParse::query - return is_specified; -} - -void -getWithParser (const amrex::ParmParse& a_pp, char const * const str, double& val) -{ - // If so, create a parser object and apply it to the value provided by the user. - std::string str_val; - Store_parserString(a_pp, str, str_val); - val = parseStringtoReal(str_val); -} - -int -queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<amrex::Real>& val, - const int start_ix, const int num_val) -{ - // call amrex::ParmParse::query, check if the user specified str. - std::vector<std::string> tmp_str_arr; - int is_specified = a_pp.queryarr(str, tmp_str_arr, start_ix, num_val); - if (is_specified) - { - // If so, create parser objects and apply them to the values provided by the user. - int const n = static_cast<int>(tmp_str_arr.size()); - val.resize(n); - for (int i=0 ; i < n ; i++) { - val[i] = static_cast<amrex::Real>(parseStringtoReal(tmp_str_arr[i])); - } - } - // return the same output as amrex::ParmParse::query - return is_specified; -} - -void -getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<amrex::Real>& val, - const int start_ix, const int num_val) -{ - // Create parser objects and apply them to the values provided by the user. - std::vector<std::string> tmp_str_arr; - a_pp.getarr(str, tmp_str_arr, start_ix, num_val); - - int const n = static_cast<int>(tmp_str_arr.size()); - val.resize(n); - for (int i=0 ; i < n ; i++) { - val[i] = static_cast<amrex::Real>(parseStringtoReal(tmp_str_arr[i])); - } -} - -int queryWithParser (const amrex::ParmParse& a_pp, char const * const str, int& val) { - amrex::Real rval; - const int result = queryWithParser(a_pp, str, rval); - if (result) { - val = safeCastToInt(std::round(rval), str); - } - return result; -} - -void getWithParser (const amrex::ParmParse& a_pp, char const * const str, int& val) { - amrex::Real rval; - getWithParser(a_pp, str, rval); - val = safeCastToInt(std::round(rval), str); -} - -int queryArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<int>& val, - const int start_ix, const int num_val) { - std::vector<amrex::Real> rval; - const int result = queryArrWithParser(a_pp, str, rval, start_ix, num_val); - if (result) { - val.resize(rval.size()); - for (unsigned long i = 0 ; i < val.size() ; i++) { - val[i] = safeCastToInt(std::round(rval[i]), str); - } - } - return result; -} - -void getArrWithParser (const amrex::ParmParse& a_pp, char const * const str, std::vector<int>& val, - const int start_ix, const int num_val) { - std::vector<amrex::Real> rval; - getArrWithParser(a_pp, str, rval, start_ix, num_val); - val.resize(rval.size()); - for (unsigned long i = 0 ; i < val.size() ; i++) { - val[i] = safeCastToInt(std::round(rval[i]), str); - } -} - void CheckDims () { // Ensure that geometry.dims is set properly. |