Skip to content

Commit 0cc5a37

Browse files
authored
create a struct to hold the Microphysics runtime parameters (#1433)
This now creates a header extern_type.H with a struct extern_t that holds all of the runtime parameters for Microphysics. The struct is filled on initialization and returned by init_extern_parameters(). This can replace the global version of the runtime parameters. This also revives the test_parameters unit test to show how to access things.
1 parent 41df7b4 commit 0cc5a37

File tree

10 files changed

+168
-35
lines changed

10 files changed

+168
-35
lines changed

Make.Microphysics

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ $(MICROPHYSICS_AUTO_SOURCE_DIR)/extern_parameters.H: $(EXTERN_PARAMETERS) $(EXTE
159159
@if [ ! -d $(MICROPHYSICS_AUTO_SOURCE_DIR) ]; then mkdir -p $(MICROPHYSICS_AUTO_SOURCE_DIR); fi
160160
$(MICROPHYSICS_HOME)/util/build_scripts/write_probin.py \
161161
--cxx_prefix $(MICROPHYSICS_AUTO_SOURCE_DIR)/extern \
162-
--pa "$(EXTERN_PARAMETERS)" --use_namespace
162+
--pa "$(EXTERN_PARAMETERS)"
163163

164164
# for debugging
165165
test_extern_params: $(MICROPHYSICS_AUTO_SOURCE_DIR)/extern_parameters.cpp

unit_test/test_parameters/GNUmakefile

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
PRECISION = DOUBLE
2+
PROFILE = FALSE
3+
4+
DEBUG = FALSE
5+
6+
DIM = 3
7+
8+
COMP = gnu
9+
10+
USE_MPI = FALSE
11+
USE_OMP = FALSE
12+
13+
USE_REACT = TRUE
14+
15+
EBASE = main
16+
17+
BL_NO_FORT = TRUE
18+
19+
# define the location of the Microphysics top directory
20+
MICROPHYSICS_HOME := ../..
21+
22+
# This sets the EOS directory
23+
EOS_DIR := helmholtz
24+
25+
# This sets the network directory
26+
NETWORK_DIR := aprox19
27+
28+
EXTERN_SEARCH += . ..
29+
30+
Bpack := ./Make.package
31+
Blocs := .
32+
33+
include $(MICROPHYSICS_HOME)/unit_test/Make.unit_test
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CEXE_sources += main.cpp
2+
3+
CEXE_headers += test_parameters.H

unit_test/test_parameters/_parameters

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@namespace: unit_test
2+
3+
dens_min real 1.e6
4+
dens_max real 1.e9
5+
temp_min real 1.e6
6+
temp_max real 1.e12
7+
8+
small_temp real 1.e4
9+
small_dens real 1.e-4
10+
11+
test_string string "test"

unit_test/test_parameters/inputs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
unit_test.test_string = "this is a C++ string"
2+
eos.eos_input_is_constant = 0
3+
4+
amr.probin = probin

unit_test/test_parameters/main.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <AMReX_PlotFileUtil.H>
2+
#include <AMReX_ParmParse.H>
3+
#include <AMReX_Print.H>
4+
5+
#include <AMReX_Geometry.H>
6+
#include <AMReX_MultiFab.H>
7+
#include <AMReX_BCRec.H>
8+
9+
10+
using namespace amrex;
11+
12+
#include <test_parameters.H>
13+
#include <AMReX_buildInfo.H>
14+
15+
#include <extern_parameters.H>
16+
#include <unit_test.H>
17+
18+
int main (int argc, char* argv[])
19+
{
20+
amrex::Initialize(argc, argv);
21+
22+
main_main();
23+
24+
amrex::Finalize();
25+
return 0;
26+
}
27+
28+
void main_main ()
29+
{
30+
31+
// do the runtime parameter initializations and microphysics inits
32+
if (ParallelDescriptor::IOProcessor()) {
33+
std::cout << "reading extern runtime parameters ..." << std::endl;
34+
}
35+
36+
ParmParse ppa("amr");
37+
38+
auto params = init_unit_test();
39+
40+
std::cout << "in C++" << std::endl;
41+
42+
std::cout << " eos_input_is_constant = " << eos_rp::eos_input_is_constant << " " << params.eos.eos_input_is_constant << std::endl;
43+
std::cout << " test_string = " << unit_test_rp::test_string << " " << params.unit_test.test_string << std::endl;
44+
std::cout << " dens_min = " << unit_test_rp::dens_min << " " << params.unit_test.dens_min << std::endl;
45+
std::cout << " nonaka_file = " << integrator_rp::nonaka_file << " " << params.integrator.nonaka_file << std::endl;
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef TEST_EOS_H
2+
#define TEST_EOS_H
3+
4+
void main_main();
5+
6+
#endif

unit_test/unit_test.H

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
void write_job_info(const std::string& dir);
55

66
AMREX_INLINE
7-
void
7+
extern_t
88
init_unit_test() {
99

1010
// now sync with C++ and read in the C++ parameters
11-
init_extern_parameters();
11+
auto params = init_extern_parameters();
12+
return params;
1213

1314
}
1415
#endif

util/build_scripts/runtime_parameters.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ def get_query_struct_string(self, struct_name="params", class_name=None):
173173
ostr += f" amrex::Vector<{self.get_cxx_decl()}> {self.name}_tmp({self.size}, {self.default_format()});\n"
174174
ostr += f" if (pp.queryarr(\"{self.name}\", {self.name}_tmp, 0, {self.size})) {{\n"
175175
ostr += f" for (int n = 0; n < {self.size}; n++) {{\n"
176-
ostr += f" {cname}{struct_name}.{self.namespace}{self.namespace_suffix}.{self.cpp_var_name}[n] = {self.name}_tmp[n];\n"
176+
ostr += f" {cname}{struct_name}.{self.namespace}.{self.cpp_var_name}[n] = {self.name}_tmp[n];\n"
177177
ostr += " }\n\n"
178178
ostr += " }\n\n"
179179
else:
180-
ostr += f"pp.query(\"{self.name}\", {cname}{struct_name}.{self.namespace}{self.namespace_suffix}.{self.cpp_var_name});\n"
180+
ostr += f"pp.query(\"{self.name}\", {cname}{struct_name}.{self.namespace}.{self.cpp_var_name});\n"
181181

182182
return ostr
183183

util/build_scripts/write_probin.py

+58-30
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import argparse
2525
import os
2626
import sys
27+
import warnings
2728

2829
import runtime_parameters
2930

@@ -64,19 +65,19 @@ def get_next_line(fin):
6465
return line[:pos]
6566

6667

67-
def parse_param_file(params_list, param_file, use_namespace=False):
68+
def parse_param_file(params_list, param_file):
6869
"""read all the parameters in a given parameter file and add valid
6970
parameters to the params list.
7071
"""
7172

7273
namespace = None
7374

7475
try:
75-
f = open(param_file, "r")
76-
except IOError:
76+
f = open(param_file)
77+
except OSError:
7778
sys.exit(f"write_probin.py: ERROR: file {param_file} does not exist")
78-
else:
79-
print(f"write_probin.py: working on parameter file {param_file}...")
79+
80+
print(f"write_probin.py: working on parameter file {param_file}...")
8081

8182
line = get_next_line(f)
8283

@@ -111,18 +112,13 @@ def parse_param_file(params_list, param_file, use_namespace=False):
111112
err = 1
112113
continue
113114

114-
if use_namespace:
115-
skip_namespace_in_declare = False
116-
else:
117-
skip_namespace_in_declare = True
118-
119115
current_param = runtime_parameters.Param(name, dtype, default,
120116
namespace=namespace, namespace_suffix="_rp",
121-
skip_namespace_in_declare=skip_namespace_in_declare)
117+
skip_namespace_in_declare=False)
122118

123119
try:
124120
current_param.priority = int(fields[3])
125-
except:
121+
except IndexError:
126122
pass
127123

128124
skip = 0
@@ -159,8 +155,7 @@ def abort(outfile):
159155
sys.exit(1)
160156

161157

162-
def write_probin(param_files,
163-
out_file, use_namespace, cxx_prefix):
158+
def write_probin(param_files, out_file, cxx_prefix):
164159

165160
""" write_probin will read through the list of parameter files and
166161
output the new out_file """
@@ -173,7 +168,7 @@ def write_probin(param_files,
173168
# read the parameters defined in the parameter files
174169

175170
for f in param_files:
176-
err = parse_param_file(params, f, use_namespace=use_namespace)
171+
err = parse_param_file(params, f)
177172
if err:
178173
abort(out_file)
179174

@@ -188,19 +183,18 @@ def write_probin(param_files,
188183
with open(ofile, "w") as fout:
189184
fout.write(CXX_HEADER)
190185

191-
fout.write(f" void init_{cxx_base}_parameters();\n\n")
186+
fout.write(f"#include <{cxx_base}_type.H>\n\n")
187+
fout.write(f" {cxx_base}_t init_{cxx_base}_parameters();\n\n")
192188

193189
for nm in sorted(namespaces):
194190
params_in_nm = [q for q in params if q.namespace == nm]
195191

196-
if use_namespace:
197-
fout.write(f" namespace {nm}_rp {{\n")
192+
fout.write(f" namespace {nm}_rp {{\n")
198193

199194
for p in params_in_nm:
200195
fout.write(f" {p.get_declare_string(with_extern=True)}")
201196

202-
if use_namespace:
203-
fout.write(" }\n")
197+
fout.write(" }\n")
204198

205199
fout.write(CXX_FOOTER)
206200

@@ -211,7 +205,7 @@ def write_probin(param_files,
211205
for p in params:
212206
fout.write(p.get_job_info_test())
213207

214-
# finally the C++ initialization routines
208+
# now the C++ initialization routines
215209

216210
ofile = f"{cxx_prefix}_parameters.cpp"
217211
with open(ofile, "w") as fout:
@@ -225,20 +219,22 @@ def write_probin(param_files,
225219
for nm in sorted(namespaces):
226220
params_in_nm = [q for q in params if q.namespace == nm]
227221

228-
if use_namespace:
229-
fout.write(f" namespace {nm}_rp {{\n")
222+
fout.write(f" namespace {nm}_rp {{\n")
230223

231224
for p in params_in_nm:
232225
fout.write(f" {p.get_declare_string()}")
233226

234-
if use_namespace:
235-
fout.write(" }\n")
227+
fout.write(" }\n")
236228

237229
fout.write("\n")
238-
fout.write(f" void init_{cxx_base}_parameters() {{\n")
230+
fout.write(f" {cxx_base}_t init_{cxx_base}_parameters() {{\n")
239231

240232
# we need access to _rt
241-
fout.write(" using namespace amrex;\n\n")
233+
fout.write(" using namespace amrex;\n\n")
234+
235+
# create the struct that will hold all the parameters -- this is what
236+
# we will return
237+
fout.write(f" {cxx_base}_t params;\n\n")
242238

243239
# now write the parmparse code to get the value from the C++
244240
# inputs. this will overwrite
@@ -255,26 +251,58 @@ def write_probin(param_files,
255251
fout.write(f" amrex::ParmParse pp(\"{nm}\");\n")
256252
for p in params_nm:
257253
fout.write(f" {p.get_default_string()}")
258-
fout.write(f" {p.get_query_string()}\n")
254+
fout.write(f" {p.get_query_string()}")
255+
fout.write(f" {p.get_query_struct_string(struct_name='params')}\n")
259256
fout.write(" }\n")
260257

258+
fout.write(" return params;\n\n")
259+
261260
fout.write(" }\n")
262261

262+
# finally, a single file that contains all of the parameter structs
263+
264+
ofile = f"{cxx_prefix}_type.H"
265+
with open(ofile, "w") as fout:
266+
fout.write(f"#ifndef {cxx_base.upper()}_TYPE_H\n")
267+
fout.write(f"#define {cxx_base.upper()}_TYPE_H\n\n")
268+
fout.write("using namespace amrex::literals;\n\n")
269+
270+
for nm in sorted(namespaces):
271+
params_nm = [q for q in params if q.namespace == nm]
272+
273+
fout.write(f"struct {nm}_param_t {{\n")
274+
for p in params_nm:
275+
fout.write(p.get_struct_entry())
276+
fout.write("};\n\n")
277+
278+
# now the parent struct
279+
280+
fout.write(f"struct {cxx_base}_t {{\n")
281+
for nm in namespaces:
282+
fout.write(f" {nm}_param_t {nm};\n")
283+
fout.write("};\n\n")
284+
285+
fout.write("#endif\n")
286+
287+
263288
def main():
264289

265290
parser = argparse.ArgumentParser()
266291
parser.add_argument('-o', type=str, default="", help='out_file')
267292
parser.add_argument('--pa', type=str, help='parameter files')
268293
parser.add_argument('--use_namespaces', action="store_true",
269-
help="put parameters in namespaces")
294+
help="[deprecated] put parameters in namespaces")
270295
parser.add_argument('--cxx_prefix', type=str, default="extern",
271296
help="a name to use in the C++ file names")
272297

273298
args = parser.parse_args()
274299

300+
if args.use_namespaces:
301+
warnings.warn("the --use_namespaces option will be removed in the future", DeprecationWarning)
302+
275303
param_files = args.pa.split()
276304

277-
write_probin(param_files, args.o, args.use_namespaces, args.cxx_prefix)
305+
write_probin(param_files, args.o, args.cxx_prefix)
278306

279307
if __name__ == "__main__":
280308
main()

0 commit comments

Comments
 (0)