diff options
author | 2022-10-02 23:25:44 +0200 | |
---|---|---|
committer | 2022-10-02 14:25:44 -0700 | |
commit | a1ade2b37db409b3c71147bf5bd27a40ded7ff11 (patch) | |
tree | 4ac9f127a5cc297f6d5659be52c05cf7f92604de /Source/Utils/WarpXUtil.cpp | |
parent | 45ec9e3550755f375a6564ae03b90bc323e15abc (diff) | |
download | WarpX-a1ade2b37db409b3c71147bf5bd27a40ded7ff11.tar.gz WarpX-a1ade2b37db409b3c71147bf5bd27a40ded7ff11.tar.zst WarpX-a1ade2b37db409b3c71147bf5bd27a40ded7ff11.zip |
Use parser for input parameters of type long (#2506)
* Use parser for input parameters of type long
* Revert "Use parser for input parameters of type long"
This reverts commit 9573bb3f693f1247e77faa433fd96dc294e68361.
* Use parser for inputs of type long
* add safeCasttoLong function
* Fix typo in comment
Diffstat (limited to 'Source/Utils/WarpXUtil.cpp')
-rw-r--r-- | Source/Utils/WarpXUtil.cpp | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/Source/Utils/WarpXUtil.cpp b/Source/Utils/WarpXUtil.cpp index afe8b7daa..eecc04b78 100644 --- a/Source/Utils/WarpXUtil.cpp +++ b/Source/Utils/WarpXUtil.cpp @@ -276,30 +276,43 @@ void Store_parserString(const amrex::ParmParse& pp, std::string query_string, f.clear(); } -int safeCastToInt(const amrex::Real x, const std::string& real_name) { - int result = 0; - bool error_detected = false; - std::string assert_msg; - // (2.0*(numeric_limits<int>::max()/2+1)) converts numeric_limits<int>::max()+1 to a real ensuring accuracy to all digits - // This accepts x = 2**31-1 but rejects 2**31. - using namespace amrex::literals; - constexpr amrex::Real max_range = (2.0_rt*static_cast<amrex::Real>(std::numeric_limits<int>::max()/2+1)); - if (x < max_range) { - if (std::ceil(x) >= std::numeric_limits<int>::min()) { - result = static_cast<int>(x); +namespace WarpXUtilSafeCast { + template< typename int_type > + AMREX_FORCE_INLINE + int_type safeCastTo(const amrex::Real x, const std::string& real_name) { + int_type result = int_type(0); + bool error_detected = false; + std::string assert_msg; + // (2.0*(numeric_limits<int>::max()/2+1)) converts numeric_limits<int>::max()+1 to a real ensuring accuracy to all digits + // This accepts x = 2**31-1 but rejects 2**31. + using namespace amrex::literals; + constexpr amrex::Real max_range = (2.0_rt*static_cast<amrex::Real>(std::numeric_limits<int_type>::max()/2+1)); + if (x < max_range) { + if (std::ceil(x) >= std::numeric_limits<int_type>::min()) { + result = static_cast<int_type>(x); + } else { + error_detected = true; + assert_msg = "Negative overflow detected when casting " + real_name + " = " + + std::to_string(x) + " to integer type"; + } + } else if (x > 0) { + error_detected = true; + assert_msg = "Overflow detected when casting " + real_name + " = " + std::to_string(x) + " to integer type"; } else { error_detected = true; - assert_msg = "Negative overflow detected when casting " + real_name + " = " + std::to_string(x) + " to int"; + assert_msg = "NaN detected when casting " + real_name + " to integer type"; } - } else if (x > 0) { - error_detected = true; - assert_msg = "Overflow detected when casting " + real_name + " = " + std::to_string(x) + " to int"; - } else { - error_detected = true; - assert_msg = "NaN detected when casting " + real_name + " to int"; - } - WARPX_ALWAYS_ASSERT_WITH_MESSAGE(!error_detected, assert_msg); - return result; + WARPX_ALWAYS_ASSERT_WITH_MESSAGE(!error_detected, assert_msg); + return result; + } +} + +int safeCastToInt(const amrex::Real x, const std::string& real_name) { + return WarpXUtilSafeCast::safeCastTo<int> (x, real_name); +} + +long safeCastToLong(const amrex::Real x, const std::string& real_name) { + return WarpXUtilSafeCast::safeCastTo<long> (x, real_name); } Parser makeParser (std::string const& parse_function, amrex::Vector<std::string> const& varnames) |