forked from Shamrock-code/Shamrock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimate_sod_csv.py
More file actions
executable file
·485 lines (404 loc) · 14.5 KB
/
Copy pathanimate_sod_csv.py
File metadata and controls
executable file
·485 lines (404 loc) · 14.5 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/usr/bin/env python3
"""
Sod Shock Tube Animation with Analytical Solution Overlay
Works with both SPH and GSPH simulation CSV outputs.
Uses shamrock.phys.SodTube for analytical solution.
Usage:
python3 animate_sod_csv.py <data_dir> [output_file] [--solver SPH|GSPH]
Examples:
python3 animate_sod_csv.py results/gsph_sod gsph_sod.gif --solver GSPH
python3 animate_sod_csv.py results/sph_sod sph_sod.gif --solver SPH
"""
import argparse
import glob
import os
import sys
import matplotlib.pyplot as plt
import numpy as np
# Use shamrock's built-in SodTube analytical solution
try:
import shamrock
HAS_SHAMROCK = True
except ImportError:
HAS_SHAMROCK = False
print("Warning: shamrock module not available, analytical solution disabled")
# Try to import animation tools
try:
from matplotlib.animation import FuncAnimation, PillowWriter
HAS_ANIMATION = True
except ImportError:
HAS_ANIMATION = False
print("Warning: Animation requires pillow. Install with: pip install pillow")
# Try to import tqdm for progress bar
try:
from tqdm import tqdm
HAS_TQDM = True
except ImportError:
HAS_TQDM = False
def parse_args():
parser = argparse.ArgumentParser(
description="Animate Sod shock tube CSV results with analytical comparison"
)
parser.add_argument("data_dir", help="Directory containing snapshot CSV files")
parser.add_argument(
"output_file",
nargs="?",
default=None,
help="Output GIF file (default: <solver>_sod_animation.gif)",
)
parser.add_argument(
"--solver",
choices=["SPH", "GSPH"],
default="GSPH",
help="Solver type for labeling (default: GSPH)",
)
return parser.parse_args()
def load_snapshot(filename):
"""Load a single snapshot CSV file."""
data = {}
metadata = {}
with open(filename, "r") as f:
# Read metadata lines (start with #)
for line in f:
if line.startswith("#"):
if ":" in line:
key, value = line[1:].strip().split(":", 1)
metadata[key.strip()] = value.strip()
else:
break
# Read header and data
f.seek(0)
lines = [l for l in f.readlines() if not l.startswith("#")]
if len(lines) < 2:
return None
header = lines[0].strip().split(",")
for col_name in header:
data[col_name] = []
for line in lines[1:]:
values = line.strip().split(",")
for i, col_name in enumerate(header):
try:
data[col_name].append(float(values[i]))
except (ValueError, IndexError):
pass
# Convert to numpy arrays
for key in data:
data[key] = np.array(data[key])
data["metadata"] = metadata
return data
def find_snapshots(data_dir):
"""Find all snapshot files in the data directory."""
files = sorted(glob.glob(f"{data_dir}/snapshot_*.csv"))
return files
def get_analytical_solution(sod, gamma, t, x_array):
"""Get analytical solution at multiple x positions."""
rho = np.zeros(len(x_array))
vel = np.zeros(len(x_array))
pres = np.zeros(len(x_array))
for i, x in enumerate(x_array):
rho[i], vel[i], pres[i] = sod.get_value(t, x)
ene = pres / ((gamma - 1) * np.maximum(rho, 1e-10))
return rho, vel, pres, ene
def main():
args = parse_args()
data_dir = args.data_dir
solver_name = args.solver
output_file = args.output_file or f"{solver_name.lower()}_sod_animation.gif"
print("=" * 70)
print(f"{solver_name} Sod Shock Tube Animation")
print("=" * 70)
print(f"Data directory: {data_dir}")
print(f"Output file: {output_file}")
print()
# Find snapshot files
print("Scanning for snapshot files...")
files = find_snapshots(data_dir)
if len(files) == 0:
print(f"ERROR: No snapshot files found in {data_dir}")
print("Looking for: snapshot_*.csv")
sys.exit(1)
print(f"Found {len(files)} snapshot files")
print()
# Load first snapshot to get parameters
first_data = load_snapshot(files[0])
if first_data is None:
print("ERROR: Could not load first snapshot")
sys.exit(1)
# Extract parameters from metadata
gamma = float(first_data["metadata"].get("gamma", "1.4"))
rho_L = float(first_data["metadata"].get("rho_L", "1.0"))
rho_R = float(first_data["metadata"].get("rho_R", "0.125"))
p_L = float(first_data["metadata"].get("p_L", "1.0"))
p_R = float(first_data["metadata"].get("p_R", "0.1"))
print("Simulation parameters:")
print(f" gamma = {gamma}")
print(f" Left: rho = {rho_L}, P = {p_L}")
print(f" Right: rho = {rho_R}, P = {p_R}")
print()
# Create analytical solution object using shamrock's built-in SodTube
sod_analytical = None
if HAS_SHAMROCK:
sod_analytical = shamrock.phys.SodTube(
gamma=gamma, rho_1=rho_L, P_1=p_L, rho_5=rho_R, P_5=p_R
)
# Determine frame skip for reasonable animation size
n_frames = len(files)
max_frames = 50
frame_skip = max(1, n_frames // max_frames)
frame_indices = list(range(0, n_frames, frame_skip))
print(f"Animation: {len(frame_indices)} frames (every {frame_skip} snapshots)")
print()
# Pre-load all frame data
print("Loading snapshot data...")
frame_data = []
pbar = tqdm(total=len(frame_indices), desc="Loading") if HAS_TQDM else None
for idx in frame_indices:
data = load_snapshot(files[idx])
if data is not None:
frame_data.append(data)
if pbar:
pbar.update(1)
if pbar:
pbar.close()
print(f"Loaded {len(frame_data)} frames")
print()
# Create animation
print("Creating animation...")
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(14, 12))
fig.suptitle(
f"{solver_name} Sod Shock Tube - Comparison with Analytical Solution",
fontsize=16,
fontweight="bold",
)
# Colors
sim_color = "#0173B2" # Blue for simulation
ana_color = "#D55E00" # Red-orange for analytical
pbar_anim = tqdm(total=len(frame_data), desc="Rendering") if HAS_TQDM else None
def update(frame_num):
"""Update function for animation."""
data = frame_data[frame_num]
# Get time from metadata
time_str = data["metadata"].get("time", "0.0")
try:
time = float(time_str.split()[0])
except:
time = 0.0
# Get simulation data
x_sim = data["pos_x"]
sort_idx = np.argsort(x_sim)
x_sim = x_sim[sort_idx]
rho_sim = data["dens"][sort_idx]
vel_sim = data["vel_x"][sort_idx]
pres_sim = data["pres"][sort_idx]
ene_sim = data["ene"][sort_idx]
# Get analytical solution
x_ana = np.linspace(x_sim.min(), x_sim.max(), 500)
if sod_analytical is not None:
rho_ana, vel_ana, pres_ana, ene_ana = get_analytical_solution(
sod_analytical, gamma, time, x_ana
)
else:
rho_ana = vel_ana = pres_ana = ene_ana = None
# Clear axes
ax1.clear()
ax2.clear()
ax3.clear()
ax4.clear()
# Density
if rho_ana is not None:
ax1.plot(
x_ana,
rho_ana,
color=ana_color,
linewidth=2.5,
label="Analytical",
zorder=1,
)
ax1.scatter(
x_sim,
rho_sim,
color=sim_color,
s=10,
alpha=0.6,
label=solver_name,
zorder=2,
)
ax1.set_ylabel("Density", fontsize=12, fontweight="bold")
ax1.set_title("Density Profile", fontsize=13, fontweight="bold")
ax1.legend(loc="upper right", fontsize=10)
ax1.grid(True, alpha=0.3)
ax1.set_xlim(x_sim.min(), x_sim.max())
# Velocity
if vel_ana is not None:
ax2.plot(
x_ana,
vel_ana,
color=ana_color,
linewidth=2.5,
label="Analytical",
zorder=1,
)
ax2.scatter(
x_sim,
vel_sim,
color=sim_color,
s=10,
alpha=0.6,
label=solver_name,
zorder=2,
)
ax2.set_ylabel("Velocity", fontsize=12, fontweight="bold")
ax2.set_title("Velocity Profile", fontsize=13, fontweight="bold")
ax2.legend(loc="upper left", fontsize=10)
ax2.grid(True, alpha=0.3)
ax2.set_xlim(x_sim.min(), x_sim.max())
# Pressure
if pres_ana is not None:
ax3.plot(
x_ana,
pres_ana,
color=ana_color,
linewidth=2.5,
label="Analytical",
zorder=1,
)
ax3.scatter(
x_sim,
pres_sim,
color=sim_color,
s=10,
alpha=0.6,
label=solver_name,
zorder=2,
)
ax3.set_ylabel("Pressure", fontsize=12, fontweight="bold")
ax3.set_xlabel("Position x", fontsize=12, fontweight="bold")
ax3.set_title("Pressure Profile", fontsize=13, fontweight="bold")
ax3.legend(loc="upper right", fontsize=10)
ax3.grid(True, alpha=0.3)
ax3.set_xlim(x_sim.min(), x_sim.max())
# Internal Energy
if ene_ana is not None:
ax4.plot(
x_ana,
ene_ana,
color=ana_color,
linewidth=2.5,
label="Analytical",
zorder=1,
)
ax4.scatter(
x_sim,
ene_sim,
color=sim_color,
s=10,
alpha=0.6,
label=solver_name,
zorder=2,
)
ax4.set_ylabel("Internal Energy", fontsize=12, fontweight="bold")
ax4.set_xlabel("Position x", fontsize=12, fontweight="bold")
ax4.set_title("Internal Energy Profile", fontsize=13, fontweight="bold")
ax4.legend(loc="upper right", fontsize=10)
ax4.grid(True, alpha=0.3)
ax4.set_xlim(x_sim.min(), x_sim.max())
# Add time label
fig.suptitle(
f"{solver_name} Sod Shock Tube - t = {time:.4f}\n"
f"Comparison with Analytical Solution",
fontsize=14,
fontweight="bold",
)
if pbar_anim:
pbar_anim.update(1)
return ax1, ax2, ax3, ax4
if HAS_ANIMATION and len(frame_data) > 0:
anim = FuncAnimation(
fig, update, frames=len(frame_data), interval=100, blit=False, repeat=True
)
# Save animation
os.makedirs(
os.path.dirname(output_file) if os.path.dirname(output_file) else ".",
exist_ok=True,
)
writer = PillowWriter(fps=10)
anim.save(output_file, writer=writer, dpi=150)
if pbar_anim:
pbar_anim.close()
plt.close()
print()
print("=" * 70)
print("Animation Complete!")
print("=" * 70)
print(f"Saved: {output_file}")
else:
print("ERROR: Cannot create animation (no data or missing dependencies)")
# Also create final state comparison plot
if len(frame_data) > 0:
print()
print("Creating final state comparison plot...")
data = frame_data[-1]
time_str = data["metadata"].get("time", "0.0")
try:
time = float(time_str.split()[0])
except:
time = 0.0
x_sim = data["pos_x"]
sort_idx = np.argsort(x_sim)
x_sim = x_sim[sort_idx]
rho_sim = data["dens"][sort_idx]
vel_sim = data["vel_x"][sort_idx]
pres_sim = data["pres"][sort_idx]
ene_sim = data["ene"][sort_idx]
x_ana = np.linspace(x_sim.min(), x_sim.max(), 500)
if sod_analytical is not None:
rho_ana, vel_ana, pres_ana, ene_ana = get_analytical_solution(
sod_analytical, gamma, time, x_ana
)
else:
rho_ana = vel_ana = pres_ana = ene_ana = None
fig2, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(14, 12))
fig2.suptitle(
f"{solver_name} Sod Shock Tube - Final State (t = {time:.4f})\n"
f"Comparison with Analytical Solution",
fontsize=14,
fontweight="bold",
)
if rho_ana is not None:
ax1.plot(x_ana, rho_ana, color=ana_color, linewidth=2.5, label="Analytical")
ax1.scatter(x_sim, rho_sim, color=sim_color, s=15, alpha=0.6, label=solver_name)
ax1.set_ylabel("Density", fontsize=12, fontweight="bold")
ax1.set_title("Density Profile", fontsize=13, fontweight="bold")
ax1.legend(fontsize=10)
ax1.grid(True, alpha=0.3)
if vel_ana is not None:
ax2.plot(x_ana, vel_ana, color=ana_color, linewidth=2.5, label="Analytical")
ax2.scatter(x_sim, vel_sim, color=sim_color, s=15, alpha=0.6, label=solver_name)
ax2.set_ylabel("Velocity", fontsize=12, fontweight="bold")
ax2.set_title("Velocity Profile", fontsize=13, fontweight="bold")
ax2.legend(fontsize=10)
ax2.grid(True, alpha=0.3)
if pres_ana is not None:
ax3.plot(x_ana, pres_ana, color=ana_color, linewidth=2.5, label="Analytical")
ax3.scatter(x_sim, pres_sim, color=sim_color, s=15, alpha=0.6, label=solver_name)
ax3.set_ylabel("Pressure", fontsize=12, fontweight="bold")
ax3.set_xlabel("Position x", fontsize=12, fontweight="bold")
ax3.set_title("Pressure Profile", fontsize=13, fontweight="bold")
ax3.legend(fontsize=10)
ax3.grid(True, alpha=0.3)
if ene_ana is not None:
ax4.plot(x_ana, ene_ana, color=ana_color, linewidth=2.5, label="Analytical")
ax4.scatter(x_sim, ene_sim, color=sim_color, s=15, alpha=0.6, label=solver_name)
ax4.set_ylabel("Internal Energy", fontsize=12, fontweight="bold")
ax4.set_xlabel("Position x", fontsize=12, fontweight="bold")
ax4.set_title("Internal Energy Profile", fontsize=13, fontweight="bold")
ax4.legend(fontsize=10)
ax4.grid(True, alpha=0.3)
plt.tight_layout()
final_plot = output_file.replace(".gif", "_final.png")
plt.savefig(final_plot, dpi=150, bbox_inches="tight")
print(f"Saved: {final_plot}")
plt.close()
print("=" * 70)
if __name__ == "__main__":
main()