From 1ba010f23e3deaea02c6c5e64f077c04607dceea Mon Sep 17 00:00:00 2001 From: Michael Zingale Date: Tue, 19 Dec 2023 14:10:14 -0500 Subject: [PATCH 1/2] add struct support for runtime_parameters.py this will be used to make runtime parameters no longer managed --- util/build_scripts/runtime_parameters.py | 37 +++++++++++++++++------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/util/build_scripts/runtime_parameters.py b/util/build_scripts/runtime_parameters.py index 010b347d08..d44d18bed0 100755 --- a/util/build_scripts/runtime_parameters.py +++ b/util/build_scripts/runtime_parameters.py @@ -69,9 +69,9 @@ def get_cxx_decl(self): """ get the C++ declaration """ if self.dtype == "real": return "amrex::Real" - elif self.dtype == "string": + if self.dtype == "string": return "std::string" - elif self.dtype == "bool": + if self.dtype == "bool": return "bool" return "int" @@ -93,6 +93,24 @@ def get_declare_string(self, with_extern=False): return f"{tstr};\n" + def get_struct_entry(self, indent=4): + """this is the line that goes into a struct that defines the + runtime parameters""" + + ostr = "" + + print("here: ", self.cpp_var_name) + if not self.debug_default is None: + ostr += "#ifdef AMREX_DEBUG\n" + ostr += f"{' '*indent}{self.get_cxx_decl()} {self.cpp_var_name}{{{self.default_format(lang='C++', debug=True)}}};\n" + ostr += "#else\n" + ostr += f"{' '*indent}{self.get_cxx_decl()} {self.cpp_var_name}{{{self.default_format(lang='C++')}}};\n" + ostr += "#endif\n" + else: + ostr += f"{' '*indent}{self.get_cxx_decl()} {self.cpp_var_name}{{{self.default_format(lang='C++')}}};\n" + + return ostr + def get_default_string(self): """this is the line that goes into, e.g., castro_declares.H included into Castro.cpp""" @@ -147,12 +165,11 @@ def default_format(self, lang="C++", debug=False): if self.dtype == "string": return f'{val}' - elif self.dtype in ["bool", "logical"] and lang == "C++": + if self.dtype in ["bool", "logical"] and lang == "C++": if val.lower() in [".true.", "true"]: return 1 - else: - return 0 - elif self.dtype == "real" and lang == "C++": + return 0 + if self.dtype == "real" and lang == "C++": if "d" in val: val = val.replace("d", "e") if not val.endswith("_rt"): @@ -180,10 +197,10 @@ def is_array(self): isize = int(self.size) except ValueError: return True - else: - if isize == 1: - return False - return True + + if isize == 1: + return False + return True def __lt__(self, other): return self.priority < other.priority From e0771520cbc74b397c541beac2af4fb4ece0bf2b Mon Sep 17 00:00:00 2001 From: Michael Zingale Date: Tue, 19 Dec 2023 17:10:54 -0500 Subject: [PATCH 2/2] more struct support --- util/build_scripts/runtime_parameters.py | 55 +++++++++++++++++------- util/build_scripts/write_probin.py | 2 +- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/util/build_scripts/runtime_parameters.py b/util/build_scripts/runtime_parameters.py index d44d18bed0..6480376765 100755 --- a/util/build_scripts/runtime_parameters.py +++ b/util/build_scripts/runtime_parameters.py @@ -99,7 +99,6 @@ def get_struct_entry(self, indent=4): ostr = "" - print("here: ", self.cpp_var_name) if not self.debug_default is None: ostr += "#ifdef AMREX_DEBUG\n" ostr += f"{' '*indent}{self.get_cxx_decl()} {self.cpp_var_name}{{{self.default_format(lang='C++', debug=True)}}};\n" @@ -128,27 +127,51 @@ def get_default_string(self): return ostr - def get_query_string(self, language): + def get_query_string(self): """this is the line that queries the ParmParse object to get the value of the runtime parameter from the inputs file. This goes into, e.g., castro_queries.H included into Castro.cpp""" ostr = "" - if language == "C++": - if self.is_array(): - # we need to create an amrex::Vector to read and then - # copy into our managed array - ostr += "\n" - ostr += f" amrex::Vector<{self.get_cxx_decl()}> {self.name}_tmp({self.size}, {self.default_format(lang='C++')});\n" - ostr += f" if (pp.queryarr(\"{self.name}\", {self.name}_tmp, 0, {self.size})) {{\n" - ostr += f" for (int n = 0; n < {self.size}; n++) {{\n" - ostr += f" {self.nm_pre}{self.cpp_var_name}[n] = {self.name}_tmp[n];\n" - ostr += " }\n\n" - ostr += " }\n\n" - else: - ostr += f"pp.query(\"{self.name}\", {self.nm_pre}{self.cpp_var_name});\n" + if self.is_array(): + # we need to create an amrex::Vector to read and then + # copy into our managed array + ostr += "\n" + ostr += f" amrex::Vector<{self.get_cxx_decl()}> {self.name}_tmp({self.size}, {self.default_format(lang='C++')});\n" + ostr += f" if (pp.queryarr(\"{self.name}\", {self.name}_tmp, 0, {self.size})) {{\n" + ostr += f" for (int n = 0; n < {self.size}; n++) {{\n" + ostr += f" {self.nm_pre}{self.cpp_var_name}[n] = {self.name}_tmp[n];\n" + ostr += " }\n\n" + ostr += " }\n\n" else: - sys.exit("invalid language choice in get_query_string") + ostr += f"pp.query(\"{self.name}\", {self.nm_pre}{self.cpp_var_name});\n" + + return ostr + + def get_query_struct_string(self, struct_name="params", class_name=None): + """this is the line that queries the ParmParse object to get + the value of the runtime parameter from the inputs file. + This is intended to use when we have a struct holding the runtime parameters, + and will have the form class_name::struct_name.namespace.param""" + + if class_name is None: + cname = "" + else: + cname = f"{class_name}::" + + ostr = "" + if self.is_array(): + # we need to create an amrex::Vector to read and then + # copy into our managed array + ostr += "\n" + ostr += f" amrex::Vector<{self.get_cxx_decl()}> {self.name}_tmp({self.size}, {self.default_format(lang='C++')});\n" + ostr += f" if (pp.queryarr(\"{self.name}\", {self.name}_tmp, 0, {self.size})) {{\n" + ostr += f" for (int n = 0; n < {self.size}; n++) {{\n" + ostr += f" {cname}{struct_name}.{self.namespace}{self.namespace_suffix}.{self.cpp_var_name}[n] = {self.name}_tmp[n];\n" + ostr += " }\n\n" + ostr += " }\n\n" + else: + ostr += f"pp.query(\"{self.name}\", {cname}{struct_name}.{self.namespace}{self.namespace_suffix}.{self.cpp_var_name});\n" return ostr diff --git a/util/build_scripts/write_probin.py b/util/build_scripts/write_probin.py index 1852b97c54..1b2371cc3d 100755 --- a/util/build_scripts/write_probin.py +++ b/util/build_scripts/write_probin.py @@ -255,7 +255,7 @@ def write_probin(param_files, fout.write(f" amrex::ParmParse pp(\"{nm}\");\n") for p in params_nm: fout.write(f" {p.get_default_string()}") - fout.write(f" {p.get_query_string('C++')}\n") + fout.write(f" {p.get_query_string()}\n") fout.write(" }\n") fout.write(" }\n")