forked from python-hydro/pyro2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyro.py
executable file
·287 lines (200 loc) · 7.31 KB
/
pyro.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
#!/usr/bin/env python
from __future__ import print_function
import getopt
import os
import sys
import numpy
import pylab
import compare
import mesh.patch as patch
from util import msg, profile, runparams
usage = """
usage:
./pyro [options] <solver> <problem> <input file> [runtime parameters]
<solver> is one of:
advection
compressible
diffusion
incompressible
<problem> is one of the problems defined in the solver's problems/
sub-directory
<input file> is the inputs file to use for that problem. You can
refer to a file in the solver's problem directory directly, without
giving the path
[options] include:
--make_benchmark : store the output of this run as the new
reference solution in the solver's tests/
sub-directory
--compare_benchmark : compare the final result of this run
to the stored benchmark for this problem
(looking in the solver's tests/ sub-
directory).
[runtime parameters] override any of the runtime defaults of
parameters specified in the inputs file. For instance, to turn
off runtime visualization, add:
vis.dovis=0
to the end of the commandline.
"""
msg.bold('pyro ...')
tc = profile.TimerCollection()
tm_main = tc.timer("main")
tm_main.begin()
#-----------------------------------------------------------------------------
# command line arguments / solver setup
#-----------------------------------------------------------------------------
# parse the runtime arguments. We specify a solver (which we import
# locally under the namespace 'solver', the problem name, and the
# input file name
if len(sys.argv) == 1:
print(usage)
sys.exit(2)
# commandline argument defaults
make_bench = 0
comp_bench = 0
try: opts, next = getopt.getopt(sys.argv[1:], "",
["make_benchmark", "compare_benchmark"])
except getopt.GetoptError:
msg.fail("invalid calling sequence")
sys.exit(2)
for o, a in opts:
if o == "--make_benchmark":
make_bench = 1
if o == "--compare_benchmark":
comp_bench = 1
if make_bench and comp_bench:
msg.fail("ERROR: cannot have both --make_benchmark and --compare_benchmark")
try: solver_name = next[0]
except IndexError:
print(usage)
msg.fail("ERROR: solver name not specified on command line")
try: problem_name = next[1]
except IndexError:
print(usage)
msg.fail("ERROR: problem name not specified on command line")
try: param_file = next[2]
except IndexError:
print(usage)
msg.fail("ERROR: parameter file not specified on command line")
other_commands = []
if len(next) > 3:
other_commands = next[3:]
# actually import the solver-specific stuff under the 'solver' namespace
exec('import ' + solver_name + ' as solver')
#-----------------------------------------------------------------------------
# runtime parameters
#-----------------------------------------------------------------------------
# parameter defaults
rp = runparams.RuntimeParameters()
rp.load_params("_defaults")
rp.load_params(solver_name + "/_defaults")
# problem-specific runtime parameters
rp.load_params(solver_name + "/problems/_" + problem_name + ".defaults")
# now read in the inputs file
if not os.path.isfile(param_file):
# check if the param file lives in the solver's problems directory
param_file = solver_name + "/problems/" + param_file
if not os.path.isfile(param_file):
msg.fail("ERROR: inputs file does not exist")
rp.load_params(param_file, no_new=1)
# and any commandline overrides
rp.command_line_params(other_commands)
# write out the inputs.auto
rp.print_paramfile()
#-----------------------------------------------------------------------------
# initialization
#-----------------------------------------------------------------------------
# initialize the Simulation object -- this will hold the grid and data and
# know about the runtime parameters and which problem we are running
sim = solver.Simulation(problem_name, rp, timers=tc)
sim.initialize()
sim.preevolve()
#-----------------------------------------------------------------------------
# evolve
#-----------------------------------------------------------------------------
tmax = rp.get_param("driver.tmax")
max_steps = rp.get_param("driver.max_steps")
init_tstep_factor = rp.get_param("driver.init_tstep_factor")
max_dt_change = rp.get_param("driver.max_dt_change")
fix_dt = rp.get_param("driver.fix_dt")
pylab.ion()
n = 0
sim.cc_data.t = 0.0
# output the 0th data
basename = rp.get_param("io.basename")
sim.cc_data.write(basename + "%4.4d" % (n))
dovis = rp.get_param("vis.dovis")
if dovis:
pylab.figure(num=1, figsize=(8,6), dpi=100, facecolor='w')
sim.dovis()
nout = 0
while sim.cc_data.t < tmax and n < max_steps:
# fill boundary conditions
tm_bc = tc.timer("fill_bc")
tm_bc.begin()
sim.cc_data.fill_BC_all()
tm_bc.end()
# get the timestep
dt = sim.timestep()
if fix_dt > 0.0:
dt = fix_dt
else:
if n == 0:
dt = init_tstep_factor*dt
dt_old = dt
else:
dt = min(max_dt_change*dt_old, dt)
dt_old = dt
if sim.cc_data.t + dt > tmax:
dt = tmax - sim.cc_data.t
# evolve for a single timestep
sim.evolve(dt)
# increment the time
sim.cc_data.t += dt
n += 1
print("%5d %10.5f %10.5f" % (n, sim.cc_data.t, dt))
# output
dt_out = rp.get_param("io.dt_out")
n_out = rp.get_param("io.n_out")
if sim.cc_data.t >= (nout + 1)*dt_out or n%n_out == 0:
tm_io = tc.timer("output")
tm_io.begin()
msg.warning("outputting...")
basename = rp.get_param("io.basename")
sim.cc_data.write(basename + "%4.4d" % (n))
nout += 1
tm_io.end()
# visualization
if dovis:
tm_vis = tc.timer("vis")
tm_vis.begin()
sim.dovis()
store = rp.get_param("vis.store_images")
if store == 1:
basename = rp.get_param("io.basename")
pylab.savefig(basename + "%4.4d" % (n) + ".png")
tm_vis.end()
tm_main.end()
#-----------------------------------------------------------------------------
# benchmarks (for regression testing)
#-----------------------------------------------------------------------------
# are we comparing to a benchmark?
if comp_bench:
compare_file = solver_name + "/tests/" + basename + "%4.4d" % (n)
msg.warning("comparing to: %s " % (compare_file) )
bench_grid, bench_data = patch.read(compare_file)
result = compare.compare(sim.cc_data.grid, sim.cc_data, bench_grid, bench_data)
if result == 0:
msg.success("results match benchmark\n")
else:
msg.fail("ERROR: " + compare.errors[result] + "\n")
# are we storing a benchmark?
if make_bench:
bench_file = solver_name + "/tests/" + basename + "%4.4d" % (n)
msg.warning("storing new benchmark: %s\n " % (bench_file) )
sim.cc_data.write(bench_file)
#-----------------------------------------------------------------------------
# final reports
#-----------------------------------------------------------------------------
rp.print_unused_params()
tc.report()
sim.finalize()