-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsod-mpi.py
More file actions
394 lines (334 loc) · 14.3 KB
/
sod-mpi.py
File metadata and controls
394 lines (334 loc) · 14.3 KB
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
"""Demonstrate Sod's 1D shock example."""
__copyright__ = """
Copyright (C) 2020 University of Illinois Board of Trustees
"""
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import logging
import numpy as np # noqa
import pyopencl as cl
import pyopencl.tools as cl_tools
from functools import partial
from arraycontext import flatten
from meshmode.array_context import (
PyOpenCLArrayContext,
PytatoPyOpenCLArrayContext
)
from mirgecom.profiling import PyOpenCLProfilingArrayContext
from meshmode.dof_array import thaw
from meshmode.mesh import BTAG_ALL, BTAG_NONE # noqa
from grudge.eager import EagerDGDiscretization
from grudge.shortcuts import make_visualizer
from mirgecom.euler import euler_operator
from mirgecom.simutil import (
get_sim_timestep,
generate_and_distribute_mesh,
componentwise_norms
)
from mirgecom.io import make_init_message
from mirgecom.mpi import mpi_entry_point
from mirgecom.integrators import rk4_step
from mirgecom.steppers import advance_state
from mirgecom.boundary import PrescribedInviscidBoundary
from mirgecom.initializers import SodShock1D
from mirgecom.eos import IdealSingleGas
from logpyle import IntervalTimer, set_dt
from mirgecom.euler import extract_vars_for_logging, units_for_logging
from mirgecom.logging_quantities import (
initialize_logmgr,
logmgr_add_many_discretization_quantities,
logmgr_add_device_name,
logmgr_add_device_memory_usage,
set_sim_state
)
logger = logging.getLogger(__name__)
class MyRuntimeError(RuntimeError):
"""Simple exception to kill the simulation."""
pass
@mpi_entry_point
def main(ctx_factory=cl.create_some_context, use_logmgr=True,
use_leap=False, use_profiling=False, casename=None,
rst_filename=None, actx_class=PyOpenCLArrayContext):
"""Drive the example."""
cl_ctx = ctx_factory()
if casename is None:
casename = "mirgecom"
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
num_parts = comm.Get_size()
from mirgecom.simutil import global_reduce as _global_reduce
global_reduce = partial(_global_reduce, comm=comm)
logmgr = initialize_logmgr(use_logmgr,
filename=f"{casename}.sqlite", mode="wu", mpi_comm=comm)
if use_profiling:
queue = cl.CommandQueue(
cl_ctx, properties=cl.command_queue_properties.PROFILING_ENABLE)
else:
queue = cl.CommandQueue(cl_ctx)
actx = actx_class(
queue,
allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)))
# timestepping control
if use_leap:
from leap.rk import RK4MethodBuilder
timestepper = RK4MethodBuilder("state")
else:
timestepper = rk4_step
t_final = 0.01
current_cfl = 1.0
current_dt = .0001
current_t = 0
constant_cfl = False
current_step = 0
# some i/o frequencies
nstatus = 10
nrestart = 5
nviz = 10
nhealth = 10
dim = 1
rst_path = "restart_data/"
rst_pattern = (
rst_path + "{cname}-{step:04d}-{rank:04d}.pkl"
)
if rst_filename: # read the grid from restart data
rst_filename = f"{rst_filename}-{rank:04d}.pkl"
from mirgecom.restart import read_restart_data
restart_data = read_restart_data(actx, rst_filename)
local_mesh = restart_data["local_mesh"]
local_nelements = local_mesh.nelements
global_nelements = restart_data["global_nelements"]
assert restart_data["num_parts"] == num_parts
else: # generate the grid from scratch
from meshmode.mesh.generation import generate_regular_rect_mesh
nel_1d = 24
box_ll = -5.0
box_ur = 5.0
generate_mesh = partial(generate_regular_rect_mesh, a=(box_ll,)*dim,
b=(box_ur,) * dim, nelements_per_axis=(nel_1d,)*dim)
local_mesh, global_nelements = generate_and_distribute_mesh(comm,
generate_mesh)
local_nelements = local_mesh.nelements
order = 1
discr = EagerDGDiscretization(
actx, local_mesh, order=order, mpi_communicator=comm
)
nodes = thaw(actx, discr.nodes())
vis_timer = None
if logmgr:
logmgr_add_device_name(logmgr, queue)
logmgr_add_device_memory_usage(logmgr, queue)
logmgr_add_many_discretization_quantities(logmgr, discr, dim,
extract_vars_for_logging, units_for_logging)
vis_timer = IntervalTimer("t_vis", "Time spent visualizing")
logmgr.add_quantity(vis_timer)
logmgr.add_watches([
("step.max", "step = {value}, "),
("t_sim.max", "sim time: {value:1.6e} s\n"),
("min_pressure", "------- P (min, max) (Pa) = ({value:1.9e}, "),
("max_pressure", "{value:1.9e})\n"),
("t_step.max", "------- step walltime: {value:6g} s, "),
("t_log.max", "log walltime: {value:6g} s")
])
initializer = SodShock1D(dim=dim)
eos = IdealSingleGas()
boundaries = {
BTAG_ALL: PrescribedInviscidBoundary(fluid_solution_func=initializer)
}
if rst_filename:
current_t = restart_data["t"]
current_step = restart_data["step"]
current_state = restart_data["state"]
if logmgr:
from mirgecom.logging_quantities import logmgr_set_time
logmgr_set_time(logmgr, current_step, current_t)
else:
# Set the current state from time 0
current_state = initializer(nodes)
visualizer = make_visualizer(discr)
initname = initializer.__class__.__name__
eosname = eos.__class__.__name__
init_message = make_init_message(dim=dim, order=order,
nelements=local_nelements,
global_nelements=global_nelements,
dt=current_dt, t_final=t_final, nstatus=nstatus,
nviz=nviz, cfl=current_cfl,
constant_cfl=constant_cfl, initname=initname,
eosname=eosname, casename=casename)
if rank == 0:
logger.info(init_message)
def my_write_status(component_errors):
flat_component_errors = actx.to_numpy(flatten(component_errors, actx))
if rank == 0:
logger.info(
"------- errors="
+ ", ".join("%.3g" % en for en in flat_component_errors)
)
def my_write_viz(step, t, state, dv=None, exact=None, resid=None):
if dv is None:
dv = eos.dependent_vars(state)
if exact is None:
exact = initializer(x_vec=nodes, eos=eos, time=t)
if resid is None:
resid = state - exact
viz_fields = [("cv", state),
("dv", dv),
("exact", exact),
("residual", resid)]
from mirgecom.simutil import write_visfile
write_visfile(discr, viz_fields, visualizer, vizname=casename,
step=step, t=t, overwrite=True, vis_timer=vis_timer)
def my_write_restart(state, step, t):
rst_fname = rst_pattern.format(cname=casename, step=step, rank=rank)
if rst_fname != rst_filename:
rst_data = {
"local_mesh": local_mesh,
"state": state,
"t": t,
"step": step,
"order": order,
"global_nelements": global_nelements,
"num_parts": num_parts
}
from mirgecom.restart import write_restart_file
write_restart_file(actx, rst_data, rst_fname, comm)
def my_health_check(pressure, component_errors):
health_error = False
from mirgecom.simutil import check_naninf_local, check_range_local
if check_naninf_local(discr, "vol", pressure) \
or check_range_local(discr, "vol", pressure, .09, 1.1):
health_error = True
logger.info(f"{rank=}: Invalid pressure data found.")
max_error = actx.to_numpy(actx.np.max(component_errors))
exittol = .09
if max_error > exittol:
health_error = True
if rank == 0:
logger.info("Solution diverged from exact soln.")
return health_error
def my_pre_step(step, t, dt, state):
try:
dv = None
exact = None
component_errors = None
if logmgr:
logmgr.tick_before()
from mirgecom.simutil import check_step
do_viz = check_step(step=step, interval=nviz)
do_restart = check_step(step=step, interval=nrestart)
do_health = check_step(step=step, interval=nhealth)
do_status = check_step(step=step, interval=nstatus)
if do_health:
dv = eos.dependent_vars(state)
exact = initializer(x_vec=nodes, eos=eos, time=t)
component_errors = componentwise_norms(discr, state - exact)
health_errors = global_reduce(
my_health_check(dv.pressure, component_errors), op="lor")
if health_errors:
if rank == 0:
logger.info("Fluid solution failed health check.")
raise MyRuntimeError("Failed simulation health check.")
if do_restart:
my_write_restart(step=step, t=t, state=state)
if do_viz:
if dv is None:
dv = eos.dependent_vars(state)
if exact is None:
exact = initializer(x_vec=nodes, eos=eos, time=t)
resid = state - exact
my_write_viz(step=step, t=t, state=state, dv=dv, exact=exact,
resid=resid)
if do_status:
if component_errors is None:
if exact is None:
exact = initializer(x_vec=nodes, eos=eos, time=t)
component_errors = componentwise_norms(discr, state - exact)
my_write_status(component_errors)
except MyRuntimeError:
if rank == 0:
logger.info("Errors detected; attempting graceful exit.")
my_write_viz(step=step, t=t, state=state)
my_write_restart(step=step, t=t, state=state)
raise
dt = get_sim_timestep(discr, state, t, dt, current_cfl, eos, t_final,
constant_cfl)
return state, dt
def my_post_step(step, t, dt, state):
# Logmgr needs to know about EOS, dt, dim?
# imo this is a design/scope flaw
if logmgr:
set_dt(logmgr, dt)
set_sim_state(logmgr, dim, state, eos)
logmgr.tick_after()
return state, dt
def my_rhs(t, state):
return euler_operator(discr, cv=state, time=t,
boundaries=boundaries, eos=eos)
current_dt = get_sim_timestep(discr, current_state, current_t, current_dt,
current_cfl, eos, t_final, constant_cfl)
current_step, current_t, current_state = \
advance_state(rhs=my_rhs, timestepper=timestepper,
pre_step_callback=my_pre_step, dt=current_dt,
post_step_callback=my_post_step,
state=current_state, t=current_t, t_final=t_final)
# Dump the final data
if rank == 0:
logger.info("Checkpointing final state ...")
final_dv = eos.dependent_vars(current_state)
final_exact = initializer(x_vec=nodes, eos=eos, time=current_t)
final_resid = current_state - final_exact
my_write_viz(step=current_step, t=current_t, state=current_state, dv=final_dv,
exact=final_exact, resid=final_resid)
my_write_restart(step=current_step, t=current_t, state=current_state)
if logmgr:
logmgr.close()
elif use_profiling:
print(actx.tabulate_profiling_data())
finish_tol = 1e-16
assert np.abs(current_t - t_final) < finish_tol
if __name__ == "__main__":
import argparse
casename = "sod-shock"
parser = argparse.ArgumentParser(description=f"MIRGE-Com Example: {casename}")
parser.add_argument("--lazy", action="store_true",
help="switch to a lazy computation mode")
parser.add_argument("--profiling", action="store_true",
help="turn on detailed performance profiling")
parser.add_argument("--log", action="store_true", default=True,
help="turn on logging")
parser.add_argument("--leap", action="store_true",
help="use leap timestepper")
parser.add_argument("--restart_file", help="root name of restart file")
parser.add_argument("--casename", help="casename to use for i/o")
args = parser.parse_args()
if args.profiling:
if args.lazy:
raise ValueError("Can't use lazy and profiling together.")
actx_class = PyOpenCLProfilingArrayContext
else:
actx_class = PytatoPyOpenCLArrayContext if args.lazy \
else PyOpenCLArrayContext
logging.basicConfig(format="%(message)s", level=logging.INFO)
if args.casename:
casename = args.casename
rst_filename = None
if args.restart_file:
rst_filename = args.restart_file
main(use_logmgr=args.log, use_leap=args.leap, use_profiling=args.profiling,
casename=casename, rst_filename=rst_filename, actx_class=actx_class)
# vim: foldmethod=marker