-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_constraint_slice.py
More file actions
executable file
·72 lines (61 loc) · 1.9 KB
/
Copy pathplot_constraint_slice.py
File metadata and controls
executable file
·72 lines (61 loc) · 1.9 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
#!/usr/bin/env python3
import csv
import sys
import matplotlib.pyplot as plt
import numpy as np
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} solution.csv [field]")
sys.exit(1)
csv_path = sys.argv[1]
field = sys.argv[2] if len(sys.argv) > 2 else "conformal_factor"
with open(csv_path, newline="") as stream:
rows = list(csv.DictReader(stream))
if (
rows
and field == "conformal_factor"
and field not in rows[0]
and "psi" in rows[0]
):
field = "psi"
if not rows or field not in rows[0]:
print(f"Unknown field: {field}")
sys.exit(1)
if {"i", "j", "x", "y"}.issubset(rows[0]):
nx = max(int(row["i"]) for row in rows) + 1
ny = max(int(row["j"]) for row in rows) + 1
x = np.full((nx, ny), np.nan)
y = np.full((nx, ny), np.nan)
z = np.full((nx, ny), np.nan)
for row in rows:
i = int(row["i"])
j = int(row["j"])
x[i, j] = float(row["x"])
y[i, j] = float(row["y"])
z[i, j] = float(row[field])
title = f"{field} - Cartesian slice"
else:
# Plot the first spectral domain so compactified infinity does not flatten
# the interesting part of the solution.
domain = rows[0]["domain"]
samples = [
(float(row["r"]), float(row[field]))
for row in rows
if row["domain"] == domain and np.isfinite(float(row["r"]))
]
samples.sort()
radii = np.array([sample[0] for sample in samples])
values = np.array([sample[1] for sample in samples])
axis = np.linspace(-radii[-1], radii[-1], 250)
x, y = np.meshgrid(axis, axis)
r = np.sqrt(x**2 + y**2)
z = np.interp(r, radii, values)
z[(r < radii[0]) | (r > radii[-1])] = np.nan
title = f"{field} - {domain}"
figure = plt.figure()
plot = figure.add_subplot(111, projection="3d")
plot.plot_surface(x, y, z, cmap="viridis")
plot.set_xlabel("x")
plot.set_ylabel("y")
plot.set_zlabel(field)
plot.set_title(title)
plt.show()