|
| 1 | +#include <AMReX.H> |
| 2 | +#include <AMReX_Box.H> |
| 3 | +#include <AMReX_Utility.H> |
| 4 | +#include <AMReX_Print.H> |
| 5 | +#include <AMReX_ParmParse.H> |
| 6 | + |
| 7 | +using namespace amrex; |
| 8 | + |
| 9 | +int main(int argc, char* argv[]) |
| 10 | +{ |
| 11 | + amrex::Initialize(argc,argv); |
| 12 | + { |
| 13 | + ParmParse::SetParserPrefix("physical_constants"); |
| 14 | + ParmParse pp("physical_constants"); |
| 15 | + pp.add("c", 299792458.); |
| 16 | + pp.add("pi", 3.14159265358979323846); |
| 17 | + } |
| 18 | + { |
| 19 | + ParmParse pp; |
| 20 | + |
| 21 | + std::string name; |
| 22 | + pp.query("name", name); |
| 23 | + AMREX_ALWAYS_ASSERT(name == "I am w"); |
| 24 | + pp.query("name", name, 1); |
| 25 | + AMREX_ALWAYS_ASSERT(name == "line 2"); |
| 26 | + |
| 27 | + Box box; |
| 28 | + pp.query("b", box); |
| 29 | + AMREX_ALWAYS_ASSERT(box == Box(IntVect(AMREX_D_DECL(1,2,3)), |
| 30 | + IntVect(AMREX_D_DECL(7,8,9)), |
| 31 | + IntVect(AMREX_D_DECL(1,0,1)))); |
| 32 | + |
| 33 | + double f0 = -1; |
| 34 | + pp.query("f", f0); |
| 35 | + AMREX_ALWAYS_ASSERT(f0 == 7); |
| 36 | + |
| 37 | + std::vector<int> f; |
| 38 | + pp.queryarr("f", f); |
| 39 | + AMREX_ALWAYS_ASSERT(f[0] == 7 && f[1] == 99 && f[2] == 11); |
| 40 | + |
| 41 | + std::vector<double> g; |
| 42 | + pp.queryarr("g", g); |
| 43 | + AMREX_ALWAYS_ASSERT(amrex::almostEqual(g[0], 7.2) && |
| 44 | + amrex::almostEqual(g[1], 11.6)); |
| 45 | + |
| 46 | + double w; |
| 47 | + pp.query("w", w); |
| 48 | + AMREX_ALWAYS_ASSERT(w == 1); |
| 49 | + pp.queryWithParser("w", w); |
| 50 | + AMREX_ALWAYS_ASSERT(w == -1); |
| 51 | + } |
| 52 | + { |
| 53 | + ParmParse pp("amrex", "my_constants"); |
| 54 | + double foo = -1, bar = -2, bar2 = -3; |
| 55 | + pp.getWithParser("foo", foo); |
| 56 | + AMREX_ALWAYS_ASSERT(amrex::almostEqual(foo, 6.0-std::sqrt(299792458.))); |
| 57 | + pp.get("bar", bar); |
| 58 | + AMREX_ALWAYS_ASSERT(foo == bar); |
| 59 | + pp.get("bar2", bar2); |
| 60 | + AMREX_ALWAYS_ASSERT(bar == bar2); |
| 61 | + } |
| 62 | + { |
| 63 | + ParmParse pp; |
| 64 | + std::array<double,3> prob_lo, prob_hi; |
| 65 | + pp.get("geom.prob_lo", prob_lo); |
| 66 | + pp.get("geom.prob_hi", prob_hi); |
| 67 | + AMREX_ALWAYS_ASSERT(amrex::almostEqual(prob_lo[0], -1.0) && |
| 68 | + amrex::almostEqual(prob_lo[1], -1.0) && |
| 69 | + amrex::almostEqual(prob_lo[2], -1.0) && |
| 70 | + amrex::almostEqual(prob_hi[0], 1.0) && |
| 71 | + amrex::almostEqual(prob_hi[1], 1.0) && |
| 72 | + amrex::almostEqual(prob_hi[2], 1.0)); |
| 73 | + } |
| 74 | + { |
| 75 | + ParmParse pp; |
| 76 | + auto parser = pp.makeParser("pi*x+c*y", {"x","y"}); |
| 77 | + auto exe = parser.compile<2>(); |
| 78 | + AMREX_ALWAYS_ASSERT(amrex::almostEqual(3.14159265358979323846+299792458., |
| 79 | + exe(1.0,1.0)) && |
| 80 | + amrex::almostEqual(3.14159265358979323846, exe(1.0,0.0)) && |
| 81 | + amrex::almostEqual(299792458., exe(0.0, 1.0))); |
| 82 | + } |
| 83 | + { |
| 84 | + ParmParse pp; |
| 85 | + long long int i = 123456789012345; |
| 86 | + long long int j = 0; |
| 87 | + pp.get("long_int_1", j); |
| 88 | + AMREX_ALWAYS_ASSERT(i==j); |
| 89 | + pp.get("long_int_2", j); |
| 90 | + AMREX_ALWAYS_ASSERT(i==j); |
| 91 | + pp.get("long_int_3", j); |
| 92 | + AMREX_ALWAYS_ASSERT(i==j); |
| 93 | + } |
| 94 | + try |
| 95 | + { |
| 96 | + ParmParse pp("code"); |
| 97 | + int a = 0; |
| 98 | + pp.query("a",a); |
| 99 | + amrex::Abort("Should not get here, because query should raise an exception"); |
| 100 | + } catch (std::runtime_error const&) { |
| 101 | + // Runtime error as expected |
| 102 | + } |
| 103 | + { |
| 104 | + int max_steps = -1; |
| 105 | + ParmParse pp("", "my_constants"); |
| 106 | + pp.query("max_steps", max_steps); |
| 107 | + AMREX_ALWAYS_ASSERT(max_steps == 40); |
| 108 | + int warpx_max_steps = -1; |
| 109 | + pp.query("warpx.max_steps", warpx_max_steps); |
| 110 | + AMREX_ALWAYS_ASSERT(max_steps == 40); |
| 111 | + } |
| 112 | + { |
| 113 | + ParmParse::SetParserPrefix("my_constants"); |
| 114 | + ParmParse pp; |
| 115 | + |
| 116 | + int ny = 0; |
| 117 | + pp.queryAsDouble("ny", ny); |
| 118 | + AMREX_ALWAYS_ASSERT(ny == 64); |
| 119 | + |
| 120 | + Array<int,3> n_cell{0,0,0}; |
| 121 | + pp.queryarrAsDouble("n_cell", 3, n_cell.data()); |
| 122 | + AMREX_ALWAYS_ASSERT(n_cell[0] == 64 && n_cell[1] == 64 && n_cell[2] == 64); |
| 123 | + } |
| 124 | + { |
| 125 | + amrex::Print() << "SUCCESS\n"; |
| 126 | + } |
| 127 | + amrex::Finalize(); |
| 128 | +} |
0 commit comments