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

start of moving runtime parameters to structs #2688

Merged
merged 8 commits into from
Jan 2, 2024
Merged
9 changes: 8 additions & 1 deletion Source/driver/Castro.H
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ constexpr int PSTAR_BISECT_FACTOR = 5;
#include <memory>
#include <iostream>

#include <params_type.H>

using std::istream;
using std::ostream;

Expand Down Expand Up @@ -535,7 +537,6 @@ public:
#include <Castro_mhd.H>
#endif


///
/// Estimate time step.
///
Expand Down Expand Up @@ -1198,6 +1199,12 @@ public:
static Vector<std::unique_ptr<std::fstream> > data_logs;
static Vector<std::unique_ptr<std::fstream> > problem_data_logs;

///
/// runtime parameters
//
static params_t params;


protected:


Expand Down
2 changes: 2 additions & 0 deletions Source/driver/Castro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ int Castro::NUM_GROW_SRC = -1;
int Castro::lastDtPlotLimited = 0;
Real Castro::lastDtBeforePlotLimiting = 0.0;

params_t Castro::params;

Real Castro::num_zones_advanced = 0.0;

Vector<std::string> Castro::source_names;
Expand Down
61 changes: 53 additions & 8 deletions Source/driver/parse_castro_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

parameters have the format:

name type default need-in-fortran? ifdef
name type default ifdef

the first three (name, type, default) are mandatory:

Expand All @@ -22,8 +22,6 @@

the next are optional:

need-in-fortran: no longer used

ifdef: only define this parameter if the name provided is #ifdef-ed

Any line beginning with a "#" is ignored
Expand Down Expand Up @@ -131,7 +129,7 @@ def read_param_file(infile):

return params

def write_headers(params, out_directory):
def write_headers(params, out_directory, struct_name):

# output

Expand Down Expand Up @@ -196,7 +194,7 @@ def write_headers(params, out_directory):
cp.write("#endif\n")
cp.close()

# write castro_queries.H
# write name_queries.H
try:
cq = open(f"{out_directory}/{nm}_queries.H", "w", encoding="UTF-8")
except OSError:
Expand All @@ -208,13 +206,15 @@ def write_headers(params, out_directory):
if ifdef is None:
for p in [q for q in params_nm if q.ifdef is None]:
cq.write(p.get_default_string())
cq.write(p.get_query_string("C++"))
cq.write(p.get_query_string())
cq.write(p.get_query_struct_string(struct_name=struct_name, class_name="Castro"))
cq.write("\n")
else:
cq.write(f"#ifdef {ifdef}\n")
for p in [q for q in params_nm if q.ifdef == ifdef]:
cq.write(p.get_default_string())
cq.write(p.get_query_string("C++"))
cq.write(p.get_query_string())
cq.write(p.get_query_struct_string(struct_name=struct_name, class_name="Castro"))
cq.write("\n")
cq.write("#endif\n")
cq.write("\n")
Expand All @@ -238,19 +238,64 @@ def write_headers(params, out_directory):

jo.close()

# now write a single file that contains all of the parameter structs
try:
sf = open(f"{out_directory}/{struct_name}_type.H", "w", encoding="UTF-8")
except OSError:
sys.exit(f"unable to open {struct_name}_type.H for writing")

sf.write(CWARNING)
sf.write(f"#ifndef {struct_name.upper()}_TYPE_H\n")
sf.write(f"#define {struct_name.upper()}_TYPE_H\n\n")

sf.write("#include <castro_limits.H>\n\n")

for nm in namespaces:

params_nm = [q for q in params if q.namespace == nm]
# sort by repr since None may be present
ifdefs = sorted({q.ifdef for q in params_nm}, key=repr)

sf.write(f"struct {nm}_t {{\n")
print("namespace = ", nm)
for ifdef in ifdefs:
if ifdef is None:
for p in [q for q in params_nm if q.ifdef is None]:
sf.write(p.get_struct_entry())
else:
sf.write(f"#ifdef {ifdef}\n")
for p in [q for q in params_nm if q.ifdef == ifdef]:
sf.write(p.get_struct_entry())
sf.write("#endif\n")


sf.write("};\n\n")

# now the parent struct

sf.write(f"struct {struct_name}_t {{\n")
for nm in namespaces:
sf.write(f" {nm}_t {nm};\n")
sf.write("};\n\n")

sf.write("#endif\n")
sf.close()


def main():
"""the main driver"""
parser = argparse.ArgumentParser()
parser.add_argument("-o", type=str, default=None,
help="output directory for the generated files")
parser.add_argument("-s", type=str, default="params",
help="name for the name struct that will hold the parameters")
parser.add_argument("input_file", type=str, nargs=1,
help="input file containing the list of parameters we will define")

args = parser.parse_args()

p = read_param_file(args.input_file[0])
write_headers(p, args.o)
write_headers(p, args.o, args.s)

if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion Util/scripts/write_probdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def write_probin(prob_param_files, cxx_prefix):
fout.write(f" {p.get_default_string()}")

if p.in_namelist:
fout.write(f" {p.get_query_string('C++')}")
fout.write(f" {p.get_query_string()}")
fout.write("\n")
fout.write(" }\n")

Expand Down