From 494ad404f5d3896f3617f8d3d9207f3e5ecf4bae Mon Sep 17 00:00:00 2001 From: Revathi Jambunathan <41089244+RevathiJambunathan@users.noreply.github.com> Date: Sat, 22 Jul 2023 14:36:56 -0700 Subject: [PATCH] Parm parse getter query for selected types (int, bool, Real) (#158) * adding wrappers for parmparse get and query for selected types with tests Co-authored-by: Edoardo Zoni Co-authored-by: Axel Huebl * initialize-finalize called once for all tests Co-authored-by: Axel Huebl * add dir for input file * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Cleaning * Cleaning * Fix typo --------- Co-authored-by: Edoardo Zoni Co-authored-by: Axel Huebl Co-authored-by: Axel Huebl Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- src/Base/ParmParse.cpp | 37 ++++++++++++++++++++++++++++++++++++- tests/parmparse_inputs | 3 +++ tests/test_parmparse.py | 18 ++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/parmparse_inputs create mode 100644 tests/test_parmparse.py diff --git a/src/Base/ParmParse.cpp b/src/Base/ParmParse.cpp index c906d83e..3ca9f9f5 100644 --- a/src/Base/ParmParse.cpp +++ b/src/Base/ParmParse.cpp @@ -30,7 +30,7 @@ void init_ParmParse(py::module &m) .def("remove", &ParmParse::remove) - .def("addfile", &ParmParse::addfile) + .def_static("addfile", &ParmParse::addfile) .def("add", py::overload_cast(&ParmParse::add)) .def("add", py::overload_cast(&ParmParse::add)) @@ -52,6 +52,41 @@ void init_ParmParse(py::module &m) .def("addarr", py::overload_cast const &>(&ParmParse::addarr)) // TODO: getters and queries + .def("get_bool", + [](ParmParse &pp, std::string name, int ival) { + bool ref; + pp.get(name.c_str(), ref, ival); + return ref; + }, + "parses input values", py::arg("name"), py::arg("ival")=0 + ) + + .def("get_int", + [](ParmParse &pp, std::string name, int ival) { + int ref; + pp.get(name.c_str(), ref, ival); + return ref; + }, + "parses input values", py::arg("name"), py::arg("ival")=0 + ) + + .def("get_real", + [](ParmParse &pp, std::string name, int ival) { + amrex::Real ref; + pp.get(name.c_str(), ref, ival); + return ref; + }, + "parses input values", py::arg("name"), py::arg("ival")=0 + ) + + .def("query_int", + [](ParmParse &pp, std::string name, int ival) { + int ref; + bool exist = pp.query(name.c_str(), ref, ival); + return std::make_tuple(exist,ref); + }, + "queries input values", py::arg("name"), py::arg("ival")=0 + ) // TODO: dumpTable, hasUnusedInputs, getUnusedInputs, getEntries ; diff --git a/tests/parmparse_inputs b/tests/parmparse_inputs new file mode 100644 index 00000000..b6a22a87 --- /dev/null +++ b/tests/parmparse_inputs @@ -0,0 +1,3 @@ +param.dt = 1.e-5 +param.ncell = 100 +param.do_pml = 1 diff --git a/tests/test_parmparse.py b/tests/test_parmparse.py new file mode 100644 index 00000000..2871d2fd --- /dev/null +++ b/tests/test_parmparse.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +import os + +import amrex.space3d as amr + + +def test_parmparse(): + pp = amr.ParmParse("") + dir_name = os.path.dirname(__file__) + pp.addfile(os.path.join(dir_name, "parmparse_inputs")) + pp_param = amr.ParmParse("param") + _, ncell = pp_param.query_int("ncell") + dt = pp_param.get_real("dt") + dopml = pp_param.get_bool("do_pml") + + assert dopml == True + assert dt == 1.0e-5 + assert ncell == 100