-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathpyro_sim.py
executable file
·411 lines (311 loc) · 12.6 KB
/
pyro_sim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python3
import argparse
import importlib
import os
import matplotlib.pyplot as plt
import pyro.util.io_pyro as io
import pyro.util.profile_pyro as profile
from pyro.util import compare, msg, runparams
valid_solvers = ["advection",
"advection_nonuniform",
"advection_rk",
"advection_fv4",
"advection_weno",
"burgers",
"viscous_burgers",
"compressible",
"compressible_rk",
"compressible_fv4",
"compressible_sdc",
"compressible_react",
"compressible_sr",
"diffusion",
"incompressible",
"incompressible_viscous",
"lm_atm",
"swe"]
class Pyro:
"""
The main driver to run pyro.
"""
def __init__(self, solver_name, from_commandline=False):
"""
Constructor
Parameters
----------
solver_name : str
Name of solver to use
from_commandline : bool
True if we are running from the commandline -- this enables
runtime vis by default.
"""
if from_commandline:
msg.bold('pyro ...')
if solver_name not in valid_solvers:
msg.fail(f"ERROR: {solver_name} is not a valid solver")
self.from_commandline = from_commandline
self.pyro_home = os.path.dirname(os.path.realpath(__file__)) + '/'
if not solver_name.startswith("pyro."):
solver_import = "pyro." + solver_name
else:
solver_import = solver_name
# import desired solver under "solver" namespace
self.solver = importlib.import_module(solver_import)
self.solver_name = solver_name
# -------------------------------------------------------------------------
# runtime parameters
# -------------------------------------------------------------------------
# parameter defaults
self.rp = runparams.RuntimeParameters()
self.rp.load_params(self.pyro_home + "_defaults")
self.rp.load_params(self.pyro_home + self.solver_name + "/_defaults")
self.tc = profile.TimerCollection()
self.is_initialized = False
def initialize_problem(self, problem_name, inputs_file=None, inputs_dict=None,
other_commands=None):
"""
Initialize the specific problem
Parameters
----------
problem_name : str
Name of the problem
inputs_file : str
Filename containing problem's runtime parameters
inputs_dict : dict
Dictionary containing extra runtime parameters
other_commands : str
Other command line parameter options
"""
# pylint: disable=attribute-defined-outside-init
problem_defaults_file = self.pyro_home + self.solver_name + \
"/problems/_" + problem_name + ".defaults"
# problem-specific runtime parameters
if os.path.isfile(problem_defaults_file):
self.rp.load_params(problem_defaults_file)
# now read in the inputs file
if inputs_file is None:
problem = importlib.import_module("pyro.{}.problems.{}".format(self.solver_name, problem_name))
inputs_file = problem.DEFAULT_INPUTS
if inputs_file is not None:
if not os.path.isfile(inputs_file):
# check if the param file lives in the solver's problems directory
inputs_file = self.pyro_home + self.solver_name + "/problems/" + inputs_file
if not os.path.isfile(inputs_file):
msg.fail("ERROR: inputs file does not exist")
self.rp.load_params(inputs_file, no_new=1)
# manually override the dovis and verbose defaults
# for Jupyter, we want runtime vis disabled by default
if not self.from_commandline:
self.rp.set_param("vis.dovis", 0)
self.rp.set_param("driver.verbose", 0)
if inputs_dict is not None:
for k, v in inputs_dict.items():
self.rp.set_param(k, v)
# and any commandline overrides
if other_commands is not None:
self.rp.command_line_params(other_commands)
# write out the inputs.auto
self.rp.print_paramfile()
self.verbose = self.rp.get_param("driver.verbose")
self.dovis = self.rp.get_param("vis.dovis")
# -------------------------------------------------------------------------
# initialization
# -------------------------------------------------------------------------
# initialize the Simulation object -- this will hold the grid and
# data and know about the runtime parameters and which problem we
# are running
self.sim = self.solver.Simulation(
self.solver_name, problem_name, self.rp, timers=self.tc)
self.sim.initialize()
self.sim.preevolve()
plt.ion()
self.sim.cc_data.t = 0.0
self.is_initialized = True
def run_sim(self):
"""
Evolve entire simulation
"""
if not self.is_initialized:
msg.fail("ERROR: problem has not been initialized")
tm_main = self.tc.timer("main")
tm_main.begin()
# output the 0th data
basename = self.rp.get_param("io.basename")
self.sim.write(f"{basename}{self.sim.n:04d}")
if self.dovis:
plt.figure(num=1, figsize=(8, 6), dpi=100, facecolor='w')
self.sim.dovis()
while not self.sim.finished():
self.single_step()
# final output
if self.verbose > 0:
msg.warning("outputting...")
basename = self.rp.get_param("io.basename")
self.sim.write(f"{basename}{self.sim.n:04d}")
tm_main.end()
# -------------------------------------------------------------------------
# final reports
# -------------------------------------------------------------------------
if self.verbose > 0:
self.rp.print_unused_params()
self.tc.report()
self.sim.finalize()
def single_step(self):
"""
Do a single step
"""
if not self.is_initialized:
msg.fail("ERROR: problem has not been initialized")
# fill boundary conditions
self.sim.cc_data.fill_BC_all()
# get the timestep
self.sim.compute_timestep()
# evolve for a single timestep
self.sim.evolve()
if self.verbose > 0:
print("%5d %10.5f %10.5f" %
(self.sim.n, self.sim.cc_data.t, self.sim.dt))
# output
if self.sim.do_output():
if self.verbose > 0:
msg.warning("outputting...")
basename = self.rp.get_param("io.basename")
self.sim.write(f"{basename}{self.sim.n:04d}")
# visualization
if self.dovis:
tm_vis = self.tc.timer("vis")
tm_vis.begin()
self.sim.dovis()
store = self.rp.get_param("vis.store_images")
if store == 1:
basename = self.rp.get_param("io.basename")
plt.savefig(f"{basename}{self.sim.n:04d}.png")
tm_vis.end()
def __repr__(self):
s = f"Pyro('{self.solver_name}')"
return s
def __str__(self):
s = f"Solver = {self.solver_name}\n"
if self.is_initialized:
s += f"Problem = {self.sim.problem_name}\n"
s += f"Simulation time = {self.sim.cc_data.t}\n"
s += f"Simulation step number = {self.sim.n}\n"
s += "\nRuntime Parameters"
s += "\n------------------\n"
s += str(self.rp)
return s
def get_var(self, v):
"""Alias for the data's get_var routine, returns the
simulation data given the variable name v.
"""
if not self.is_initialized:
msg.fail("ERROR: problem has not been initialized")
return self.sim.cc_data.get_var(v)
def get_grid(self):
"""Return the underlying grid object for the simulation
"""
if not self.is_initialized:
msg.fail("ERROR: problem has not been initialized")
return self.sim.cc_data.grid
def get_sim(self):
"""Return the Simulation object"""
return self.sim
class PyroBenchmark(Pyro):
"""
A subclass of Pyro for benchmarking. Inherits everything from pyro, but adds benchmarking routines.
"""
def __init__(self, solver_name, comp_bench=False,
reset_bench_on_fail=False, make_bench=False):
"""
Constructor
Parameters
----------
solver_name : str
Name of solver to use
comp_bench : bool
Are we comparing to a benchmark?
reset_bench_on_fail : bool
Do we reset the benchmark on fail?
make_bench : bool
Are we storing a benchmark?
"""
super().__init__(solver_name)
self.comp_bench = comp_bench
self.reset_bench_on_fail = reset_bench_on_fail
self.make_bench = make_bench
def run_sim(self, rtol=1.e-12):
"""
Evolve entire simulation and compare to benchmark at the end.
"""
super().run_sim()
result = 0
if self.comp_bench:
result = self.compare_to_benchmark(rtol)
if self.make_bench or (result != 0 and self.reset_bench_on_fail):
self.store_as_benchmark()
if self.comp_bench:
return result
return self.sim
def compare_to_benchmark(self, rtol):
""" Are we comparing to a benchmark? """
basename = self.rp.get_param("io.basename")
compare_file = "{}{}/tests/{}{:04d}".format(
self.pyro_home, self.solver_name, basename, self.sim.n)
msg.warning(f"comparing to: {compare_file} ")
try:
sim_bench = io.read(compare_file)
except OSError:
msg.warning("ERROR opening compare file")
return "ERROR opening compare file"
result = compare.compare(self.sim.cc_data, sim_bench.cc_data, rtol)
if result == 0:
msg.success(f"results match benchmark to within relative tolerance of {rtol}\n")
else:
msg.warning("ERROR: " + compare.errors[result] + "\n")
return result
def store_as_benchmark(self):
""" Are we storing a benchmark? """
if not os.path.isdir(self.pyro_home + self.solver_name + "/tests/"):
try:
os.mkdir(self.pyro_home + self.solver_name + "/tests/")
except (FileNotFoundError, PermissionError):
msg.fail(
"ERROR: unable to create the solver's tests/ directory")
basename = self.rp.get_param("io.basename")
bench_file = self.pyro_home + self.solver_name + "/tests/" + \
basename + "%4.4d" % (self.sim.n)
msg.warning(f"storing new benchmark: {bench_file}\n")
self.sim.write(bench_file)
def parse_args():
"""Parse the runtime parameters"""
p = argparse.ArgumentParser()
p.add_argument("--make_benchmark",
help="create a new benchmark file for regression testing",
action="store_true")
p.add_argument("--compare_benchmark",
help="compare the end result to the stored benchmark",
action="store_true")
p.add_argument("solver", metavar="solver-name", type=str, nargs=1,
help="name of the solver to use", choices=valid_solvers)
p.add_argument("problem", metavar="problem-name", type=str, nargs=1,
help="name of the problem to run")
p.add_argument("param", metavar="inputs-file", type=str, nargs=1,
help="name of the inputs file")
p.add_argument("other", metavar="runtime-parameters", type=str, nargs="*",
help="additional runtime parameters that override the inputs file "
"in the format section.option=value")
return p.parse_args()
def main():
args = parse_args()
if args.compare_benchmark or args.make_benchmark:
pyro = PyroBenchmark(args.solver[0],
comp_bench=args.compare_benchmark,
make_bench=args.make_benchmark)
else:
pyro = Pyro(args.solver[0], from_commandline=True)
pyro.initialize_problem(problem_name=args.problem[0],
inputs_file=args.param[0],
other_commands=args.other)
pyro.run_sim()
if __name__ == "__main__":
main()