24
24
import argparse
25
25
import os
26
26
import sys
27
+ import warnings
27
28
28
29
import runtime_parameters
29
30
@@ -64,19 +65,19 @@ def get_next_line(fin):
64
65
return line [:pos ]
65
66
66
67
67
- def parse_param_file (params_list , param_file , use_namespace = False ):
68
+ def parse_param_file (params_list , param_file ):
68
69
"""read all the parameters in a given parameter file and add valid
69
70
parameters to the params list.
70
71
"""
71
72
72
73
namespace = None
73
74
74
75
try :
75
- f = open (param_file , "r" )
76
- except IOError :
76
+ f = open (param_file )
77
+ except OSError :
77
78
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 } ..." )
80
81
81
82
line = get_next_line (f )
82
83
@@ -111,18 +112,13 @@ def parse_param_file(params_list, param_file, use_namespace=False):
111
112
err = 1
112
113
continue
113
114
114
- if use_namespace :
115
- skip_namespace_in_declare = False
116
- else :
117
- skip_namespace_in_declare = True
118
-
119
115
current_param = runtime_parameters .Param (name , dtype , default ,
120
116
namespace = namespace , namespace_suffix = "_rp" ,
121
- skip_namespace_in_declare = skip_namespace_in_declare )
117
+ skip_namespace_in_declare = False )
122
118
123
119
try :
124
120
current_param .priority = int (fields [3 ])
125
- except :
121
+ except IndexError :
126
122
pass
127
123
128
124
skip = 0
@@ -159,8 +155,7 @@ def abort(outfile):
159
155
sys .exit (1 )
160
156
161
157
162
- def write_probin (param_files ,
163
- out_file , use_namespace , cxx_prefix ):
158
+ def write_probin (param_files , out_file , cxx_prefix ):
164
159
165
160
""" write_probin will read through the list of parameter files and
166
161
output the new out_file """
@@ -173,7 +168,7 @@ def write_probin(param_files,
173
168
# read the parameters defined in the parameter files
174
169
175
170
for f in param_files :
176
- err = parse_param_file (params , f , use_namespace = use_namespace )
171
+ err = parse_param_file (params , f )
177
172
if err :
178
173
abort (out_file )
179
174
@@ -188,19 +183,18 @@ def write_probin(param_files,
188
183
with open (ofile , "w" ) as fout :
189
184
fout .write (CXX_HEADER )
190
185
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 " )
192
188
193
189
for nm in sorted (namespaces ):
194
190
params_in_nm = [q for q in params if q .namespace == nm ]
195
191
196
- if use_namespace :
197
- fout .write (f" namespace { nm } _rp {{\n " )
192
+ fout .write (f" namespace { nm } _rp {{\n " )
198
193
199
194
for p in params_in_nm :
200
195
fout .write (f" { p .get_declare_string (with_extern = True )} " )
201
196
202
- if use_namespace :
203
- fout .write (" }\n " )
197
+ fout .write (" }\n " )
204
198
205
199
fout .write (CXX_FOOTER )
206
200
@@ -211,7 +205,7 @@ def write_probin(param_files,
211
205
for p in params :
212
206
fout .write (p .get_job_info_test ())
213
207
214
- # finally the C++ initialization routines
208
+ # now the C++ initialization routines
215
209
216
210
ofile = f"{ cxx_prefix } _parameters.cpp"
217
211
with open (ofile , "w" ) as fout :
@@ -225,20 +219,22 @@ def write_probin(param_files,
225
219
for nm in sorted (namespaces ):
226
220
params_in_nm = [q for q in params if q .namespace == nm ]
227
221
228
- if use_namespace :
229
- fout .write (f" namespace { nm } _rp {{\n " )
222
+ fout .write (f" namespace { nm } _rp {{\n " )
230
223
231
224
for p in params_in_nm :
232
225
fout .write (f" { p .get_declare_string ()} " )
233
226
234
- if use_namespace :
235
- fout .write (" }\n " )
227
+ fout .write (" }\n " )
236
228
237
229
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 " )
239
231
240
232
# 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 " )
242
238
243
239
# now write the parmparse code to get the value from the C++
244
240
# inputs. this will overwrite
@@ -255,26 +251,58 @@ def write_probin(param_files,
255
251
fout .write (f" amrex::ParmParse pp(\" { nm } \" );\n " )
256
252
for p in params_nm :
257
253
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 " )
259
256
fout .write (" }\n " )
260
257
258
+ fout .write (" return params;\n \n " )
259
+
261
260
fout .write (" }\n " )
262
261
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
+
263
288
def main ():
264
289
265
290
parser = argparse .ArgumentParser ()
266
291
parser .add_argument ('-o' , type = str , default = "" , help = 'out_file' )
267
292
parser .add_argument ('--pa' , type = str , help = 'parameter files' )
268
293
parser .add_argument ('--use_namespaces' , action = "store_true" ,
269
- help = "put parameters in namespaces" )
294
+ help = "[deprecated] put parameters in namespaces" )
270
295
parser .add_argument ('--cxx_prefix' , type = str , default = "extern" ,
271
296
help = "a name to use in the C++ file names" )
272
297
273
298
args = parser .parse_args ()
274
299
300
+ if args .use_namespaces :
301
+ warnings .warn ("the --use_namespaces option will be removed in the future" , DeprecationWarning )
302
+
275
303
param_files = args .pa .split ()
276
304
277
- write_probin (param_files , args .o , args .use_namespaces , args . cxx_prefix )
305
+ write_probin (param_files , args .o , args .cxx_prefix )
278
306
279
307
if __name__ == "__main__" :
280
308
main ()
0 commit comments