-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrunfast2d.py
executable file
·83 lines (65 loc) · 2 KB
/
runfast2d.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
#!/usr/bin/env python3
"""
Demonstration of NRL LCPFCT code in Python
"""
from matplotlib.pyplot import draw, pause, figure
import matplotlib.animation as anim
import fast2d
import xarray
from pathlib import Path
import numpy as np
import argparse
maxtstep = 801
WRITER = 'ffmpeg'
FPS = 10
CODEC = 'ffv1'
DPI = 72
def runfast2d():
rho, vr, vz, erg = fast2d.fast2d() # fortran to c order axes
i = range(
rho.shape[2]
) # time steps are dynamic, need to pass them out from Forran in the future
ds = xarray.Dataset(
{
"density": (('time', 'r', 'z'), rho.transpose(2, 1, 0)),
'velocity_r': (('time', 'r', 'z'), vr.transpose(2, 1, 0)),
'velocity_z': (('time', 'r', 'z'), vz.transpose(2, 1, 0)),
'pressure': (('time', 'r', 'z'), erg.transpose(2, 1, 0)),
},
coords={
'time_index': i,
'r': np.arange(0, 64 * 0.1, 0.1),
'z': np.arange(0, 64 * 0.1, 0.1),
},
)
return ds
def plotfast2d(ds: xarray.Dataset, outfile: Path):
fg = figure(figsize=(8, 8))
ax = fg.subplots(2, 2)
ax = ax.ravel()
ht = fg.suptitle('ti=0')
cmap = (None, None, 'bwr', 'bwr')
outfile = Path(outfile).expanduser()
print('writing', outfile)
Writer = anim.writers[WRITER]
w = Writer(fps=FPS, codec=CODEC)
with w.saving(fg, str(outfile), DPI):
for i in ds.time_index:
for j, k in enumerate(('density', 'pressure', 'velocity_r', 'velocity_z')):
ht.set_text(f'ti={i.item()}')
ax[j].cla()
ax[j].imshow(ds[k][i, ...], origin='lower', cmap=cmap[j])
ax[j].set_title(k)
draw()
pause(0.01)
w.grab_frame()
def main():
p = argparse.ArgumentParser()
p.add_argument(
'outfn', help='output movie file to write', nargs='?', default='fast2d.avi'
)
p = p.parse_args()
ds = runfast2d()
plotfast2d(ds, p.outfn)
if __name__ == '__main__':
main()