Skip to content

Commit

Permalink
constexpr the runtime parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
zingale committed Jan 4, 2024
1 parent f293d91 commit 1f01da8
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Exec/Make.auto_source
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ CPP_PARAMETERS := $(TOP)/Source/driver/_cpp_parameters

$(CASTRO_AUTO_SOURCE_DIR)/castro_params.H: $(CPP_PARAMETERS)
@if [ ! -d $(CASTRO_AUTO_SOURCE_DIR) ]; then mkdir -p $(CASTRO_AUTO_SOURCE_DIR); fi
PYTHONPATH=$(MICROPHYSICS_HOME)/util/build_scripts $(TOP)/Source/driver/parse_castro_params.py -o $(CASTRO_AUTO_SOURCE_DIR) $(CPP_PARAMETERS)
PYTHONPATH=$(MICROPHYSICS_HOME)/util/build_scripts $(TOP)/Source/driver/parse_castro_params.py --constexpr -o $(CASTRO_AUTO_SOURCE_DIR) $(CPP_PARAMETERS)

# for debugging
test_cxx_params: $(CASTRO_AUTO_SOURCE_DIR)/castro_params.H
Expand Down
15 changes: 8 additions & 7 deletions Source/driver/Castro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,9 @@ Castro::read_params ()
}

// Make sure not to call refluxing if we're not actually doing any hydro.
if (do_hydro == 0) {
do_reflux = 0;
}
// if (do_hydro == 0) {
// do_reflux = 0;
// }

if (max_dt < fixed_dt)
{
Expand Down Expand Up @@ -475,9 +475,9 @@ Castro::read_params ()
}
}

if (dgeom.IsRZ() == 1) {
rot_axis = 2;
}
// if (dgeom.IsRZ() == 1) {
// rot_axis = 2;
// }
#if (AMREX_SPACEDIM == 1)
if (do_rotation == 1) {
std::cerr << "ERROR:Castro::Rotation not implemented in 1d\n";
Expand Down Expand Up @@ -2070,7 +2070,8 @@ Castro::post_timestep (int iteration_local)
Real max_field_val = std::numeric_limits<Real>::min();

for (int lev = 0; lev <= parent->finestLevel(); ++lev) {
auto mf = getLevel(lev).derive(castro::stopping_criterion_field, state[State_Type].curTime(), 0);
std::string scf{castro::stopping_criterion_field};
auto mf = getLevel(lev).derive(scf, state[State_Type].curTime(), 0);
max_field_val = std::max(max_field_val, mf->max(0));
}

Expand Down
4 changes: 2 additions & 2 deletions Source/driver/Castro_advance_ctu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ Castro::subcycle_advance_ctu(const Real time, const Real dt, int amr_iteration,
// approximately compensate for that.

if (in_retry) {
sdc_iters += 1;
//sdc_iters += 1;
amrex::Print() << "Adding an SDC iteration due to the retry." << std::endl << std::endl;
}

Expand Down Expand Up @@ -412,7 +412,7 @@ Castro::subcycle_advance_ctu(const Real time, const Real dt, int amr_iteration,

// Set sdc_iters to its original value, in case we modified it above.

sdc_iters = sdc_iters_old;
//sdc_iters = sdc_iters_old;

// If we're allowing for retries, check for that here.

Expand Down
6 changes: 4 additions & 2 deletions Source/driver/Castro_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ Castro::restart (Amr& papa,
PMFile.open(FullPathPMFile.c_str(), std::ios::in);

if (PMFile.good()) {
PMFile >> point_mass;
Real pm;
PMFile >> pm; // point_mass;
PMFile.close();
}

Expand All @@ -198,7 +199,8 @@ Castro::restart (Amr& papa,
RotationFile.open(FullPathRotationFile.c_str(), std::ios::in);

if (RotationFile.is_open()) {
RotationFile >> castro::rotational_period;
Real rp;
RotationFile >> rp; //castro::rotational_period;
amrex::Print() << " Based on the checkpoint, setting the rotational period to "
<< std::setprecision(7) << std::fixed << castro::rotational_period << " s.\n";
RotationFile.close();
Expand Down
14 changes: 9 additions & 5 deletions Source/driver/Castro_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ Castro::variableSetUp ()

// set small positive values of the "small" quantities if they are
// negative
#if 0
if (small_dens < 0.0_rt) {
small_dens = 1.e-100_rt;
}
Expand All @@ -224,13 +225,16 @@ Castro::variableSetUp ()
if (small_ener < 0.0_rt) {
small_ener = 1.e-100_rt;
}
#endif

// now initialize the C++ Microphysics
#ifdef REACTIONS
network_init();
#endif

eos_init(castro::small_temp, castro::small_dens);
Real st{castro::small_temp};
Real sd{castro::small_dens};
eos_init(st, sd);

#ifdef RADIATION
opacity_init();
Expand All @@ -240,11 +244,11 @@ Castro::variableSetUp ()
// with the minimum permitted by the EOS, and vice versa.

Real new_min_T = std::max(small_temp, EOSData::mintemp);
small_temp = new_min_T;
//small_temp = new_min_T;
EOSData::mintemp = new_min_T;

Real new_min_rho = std::max(small_dens, EOSData::mindens);
small_dens = new_min_rho;
//small_dens = new_min_rho;
EOSData::mindens = new_min_rho;

// Given small_temp and small_dens, compute small_pres
Expand All @@ -267,8 +271,8 @@ Castro::variableSetUp ()

eos(eos_input_rt, eos_state);

castro::small_pres = amrex::max(castro::small_pres, eos_state.p);
castro::small_ener = amrex::max(castro::small_ener, eos_state.e);
//castro::small_pres = amrex::max(castro::small_pres, eos_state.p);
//castro::small_ener = amrex::max(castro::small_ener, eos_state.e);

// some consistency checks on the parameters
#ifdef REACTIONS
Expand Down
99 changes: 58 additions & 41 deletions Source/driver/parse_castro_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def read_param_file(infile):

return params

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

# output

Expand All @@ -152,19 +152,23 @@ def write_headers(params, out_directory, struct_name):
cd.write(f"#ifndef {nm.upper()}_DECLARES_H\n")
cd.write(f"#define {nm.upper()}_DECLARES_H\n")

cd.write("\n")
cd.write(f"namespace {nm} {{\n")

for ifdef in ifdefs:
if ifdef is None:
for p in [q for q in params_nm if q.ifdef is None]:
cd.write(p.get_declare_string())
else:
cd.write(f"#ifdef {ifdef}\n")
for p in [q for q in params_nm if q.ifdef == ifdef]:
cd.write(p.get_declare_string())
cd.write("#endif\n")
cd.write("}\n\n")
if constexpr:
pass
else:
cd.write("\n")
cd.write(f"namespace {nm} {{\n")

for ifdef in ifdefs:
if ifdef is None:
for p in [q for q in params_nm if q.ifdef is None]:
cd.write(p.get_declare_string())
else:
cd.write(f"#ifdef {ifdef}\n")
for p in [q for q in params_nm if q.ifdef == ifdef]:
cd.write(p.get_declare_string())
cd.write("#endif\n")

cd.write("}\n\n")
cd.write("#endif\n")
cd.close()

Expand All @@ -177,19 +181,27 @@ def write_headers(params, out_directory, struct_name):
cp.write(CWARNING)
cp.write(f"#ifndef {nm.upper()}_PARAMS_H\n")
cp.write(f"#define {nm.upper()}_PARAMS_H\n")

cp.write("#include <castro_limits.H>\n")
cp.write(f"using namespace amrex::literals;\n")
cp.write("\n")
cp.write(f"namespace {nm} {{\n")

for ifdef in ifdefs:
if ifdef is None:
for p in [q for q in params_nm if q.ifdef is None]:
cp.write(p.get_declare_string(with_extern=True))
else:
cp.write(f"#ifdef {ifdef}\n")
for p in [q for q in params_nm if q.ifdef == ifdef]:
cp.write(p.get_declare_string(with_extern=True))
cp.write("#endif\n")
if constexpr:
for p in params_nm:
type = p.get_cxx_decl()
if type == "std::string":
type = "std::string_view"
cp.write(f"constexpr {type} {p.cpp_var_name}{{{p.default_format()}}};\n")
else:
for ifdef in ifdefs:
if ifdef is None:
for p in [q for q in params_nm if q.ifdef is None]:
cp.write(p.get_declare_string(with_extern=True))
else:
cp.write(f"#ifdef {ifdef}\n")
for p in [q for q in params_nm if q.ifdef == ifdef]:
cp.write(p.get_declare_string(with_extern=True))
cp.write("#endif\n")
cp.write("}\n\n")
cp.write("#endif\n")
cp.close()
Expand All @@ -202,22 +214,25 @@ def write_headers(params, out_directory, struct_name):

cq.write(CWARNING)

for ifdef in ifdefs:
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())
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())
cq.write(p.get_query_struct_string(struct_name=struct_name, class_name="Castro"))
cq.write("\n")
cq.write("#endif\n")
cq.write("\n")
if constexpr:
pass
else:
for ifdef in ifdefs:
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())
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())
cq.write(p.get_query_struct_string(struct_name=struct_name, class_name="Castro"))
cq.write("\n")
cq.write("#endif\n")
cq.write("\n")
cq.close()

# write the job info tests
Expand Down Expand Up @@ -289,13 +304,15 @@ def main():
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("--constexpr", action="store_true",
help="force the parameters to be constexpr without being able to query")
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, args.s)
write_headers(p, args.o, args.s, args.constexpr)

if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion Source/gravity/Castro_pointmass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Castro::pointmass_update(Real time, Real dt)
<< ", to " << point_mass + mass_change_at_center << std::endl << std::endl;
}

point_mass += mass_change_at_center;
//point_mass += mass_change_at_center;

#ifdef _OPENMP
#pragma omp parallel
Expand Down
2 changes: 1 addition & 1 deletion Source/gravity/Gravity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ Gravity::install_level (int level,

std::string Gravity::get_gravity_type()
{
return gravity::gravity_type;
return std::string{gravity::gravity_type};
}

int Gravity::get_max_solve_level()
Expand Down
8 changes: 4 additions & 4 deletions Source/scf/scf_relax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ Castro::do_hscf_solve()

if (std::abs(scf_polar_radius - scf_equatorial_radius) / std::abs(scf_equatorial_radius) < 1.e-6) {

rotational_period = 0.0;
//rotational_period = 0.0;

} else {

Expand All @@ -344,9 +344,9 @@ Castro::do_hscf_solve()
// Let's also be sure not to let the period
// change by too much in a single iteration.

rotational_period = amrex::min(1.1_rt * rotational_period,
amrex::max(0.9_rt * rotational_period,
2.0_rt * M_PI / omega));
//rotational_period = amrex::min(1.1_rt * rotational_period,
// amrex::max(0.9_rt * rotational_period,
// 2.0_rt * M_PI / omega));

}

Expand Down

0 comments on commit 1f01da8

Please sign in to comment.