Repository navigation
Expand file tree
/
Copy pathplot_cfd.py
More file actions
154 lines (120 loc) · 4.87 KB
/
Copy pathplot_cfd.py
File metadata and controls
154 lines (120 loc) · 4.87 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
import os
import pickle
import argparse
from pathlib import Path
import numpy as np
from matplotlib import animation
import matplotlib.pyplot as plt
from matplotlib import tri as mtri
def plot_cfd_color(data, filename):
figure =plt.figure(figsize = (12,7))
axis = figure.add_subplot(211)
axis2 = figure.add_subplot(212)
skip = 1
num_steps = data[0]['true_velocity'].shape[0]
num_frames = len(data) * num_steps // skip
# compute bounds
bounds = []
for trajectory in data:
bb_min = trajectory['true_velocity'].min(axis=(0, 1))
bb_max = trajectory['true_velocity'].max(axis=(0, 1))
bounds.append((bb_min, bb_max))
def animate(frame):
step = (frame*skip) % num_steps
traj = (frame*skip) // num_steps
axis.cla()
axis.set_aspect('equal')
axis.set_axis_off()
vmin, vmax = bounds[traj]
pos = data[traj]['mesh_pos'][step]
faces = data[traj]['cells'][step]
velocity = data[traj]['pred_velocity'][step]
triang = mtri.Triangulation(pos[:, 0], pos[:, 1], faces)
axis.tripcolor(triang, velocity[:, 0], vmin=vmin[0], vmax=vmax[0])
axis.triplot(triang, 'ko-', ms=0.5, lw=0.3)
axis.set_title("Predicted")
step = (frame*skip) % num_steps
traj = (frame*skip) // num_steps
axis2.cla()
axis2.set_aspect('equal')
axis2.set_axis_off()
vmin, vmax = bounds[traj]
pos = data[traj]['mesh_pos'][step]
faces = data[traj]['cells'][step]
velocity = data[traj]['true_velocity'][step]
triang = mtri.Triangulation(pos[:, 0], pos[:, 1], faces)
axis2.tripcolor(triang, velocity[:, 0], vmin=vmin[0], vmax=vmax[0])
axis2.triplot(triang, 'ko-', ms=0.5, lw=0.3)
axis2.set_title("Ground Truth")
figure.suptitle(f"Time step: {frame}")
return figure,
_ = animation.FuncAnimation(figure, animate, frames=num_frames, interval=30)
plt.show(block=True)
# ani.save(filename)
def plot_cfd_quiver(data, filename):
figure =plt.figure(figsize = (12,7))
axis = figure.add_subplot(211)
axis2 = figure.add_subplot(212)
skip = 1
num_steps = data[0]['true_velocity'].shape[0]
num_frames = len(data) * num_steps // skip
# compute bounds
bounds = []
for trajectory in data:
bb_min = trajectory['true_velocity'].min(axis=(0, 1))
bb_max = trajectory['true_velocity'].max(axis=(0, 1))
bounds.append((bb_min, bb_max))
def animate(frame):
step = (frame*skip) % num_steps
traj = (frame*skip) // num_steps
axis.cla()
axis.set_aspect('equal')
axis.set_axis_off()
vmin, vmax = bounds[traj]
pos = data[traj]['mesh_pos'][step]
faces = data[traj]['cells'][step]
velocity = data[traj]['pred_velocity'][step]
triang = mtri.Triangulation(pos[:, 0], pos[:, 1], faces)
axis.quiver(pos[:, 0], pos[:, 1], velocity[:, 0], velocity[:, 1])
axis.triplot(triang, 'ko-', ms=0.5, lw=0.3)
axis.set_title("Predicted")
step = (frame*skip) % num_steps
traj = (frame*skip) // num_steps
axis2.cla()
axis2.set_aspect('equal')
axis2.set_axis_off()
vmin, vmax = bounds[traj]
pos = data[traj]['mesh_pos'][step]
faces = data[traj]['cells'][step]
velocity = data[traj]['true_velocity'][step]
triang = mtri.Triangulation(pos[:, 0], pos[:, 1], faces)
axis2.quiver(pos[:, 0], pos[:, 1], velocity[:, 0], velocity[:, 1])
axis2.triplot(triang, 'ko-', ms=0.5, lw=0.3)
axis2.set_title("Ground Truth")
figure.suptitle(f"Time step: {frame}")
return figure,
ani = animation.FuncAnimation(figure, animate, frames=num_frames, interval=30)
plt.show()
# ani.save(filename)
def generate_all():
results_path = os.path.join(os.path.dirname(__file__), '..', 'models', 'results', 'cfd', 'og_long-step4200000-loss0.01691.hdf5')
output_path = os.path.join(os.path.dirname(__file__), 'animations', 'cfd', 'og_long-step4200000-loss0.01691.hdf5')
Path(output_path).mkdir(parents=True, exist_ok=True)
for i in range(10, 100):
with open(os.path.join(results_path, f'{i:03d}.eval'), 'rb') as f:
data = pickle.load(f)
plot_cfd_color([data], os.path.join(output_path, f'c{i:03d}.mp4'))
plot_cfd_quiver([data], os.path.join(output_path, f'q{i:03d}.mp4'))
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--datafile")
parser.add_argument("-o", "--output", help="output file")
args = parser.parse_args()
if (args.output == None):
args.output = "out.mp4"
with open(args.datafile, "rb") as f:
data = pickle.load(f)
plot_cfd_color([data], args.output)
if __name__ == "__main__":
# generate_all()
main()