Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add struct support for runtime_parameters.py #1422

Merged
merged 5 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 65 additions & 25 deletions util/build_scripts/runtime_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -93,6 +93,23 @@ 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 = ""

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"""
Expand All @@ -110,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"
zingale marked this conversation as resolved.
Show resolved Hide resolved
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

Expand All @@ -147,12 +188,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"):
Expand Down Expand Up @@ -180,10 +220,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
Expand Down
2 changes: 1 addition & 1 deletion util/build_scripts/write_probin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading