Peano
Loading...
Searching...
No Matches
plotSolution.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
20
21# Computes the analytical solution of the Noh problem and plots the SPH answer
22
23
24# Parameters
25gas_gamma = 5.0 / 3.0 # Polytropic index
26rho0 = 1.0 # Background density
27P0 = 1.0e-6 # Background pressure
28v0 = 1
29
30import matplotlib
31
32matplotlib.use("Agg")
33
34import matplotlib.pyplot as plt
35from scipy import stats
36import numpy as np
37import sys
38
40
41if len(sys.argv) < 2:
42 time = 0.2
43else:
44 time = float(sys.argv[1])
45
46pvdfile = "particles.pvd"
47reader = ParticleVTUReader(pvdfile=pvdfile, snapshot_time=time)
48partData = reader.load()
49time = reader.get_snapshot_time()
50
51# show me what particle fields are stored
52# partData.show_attribute_list()
53
54x = partData.x[:, 0]
55y = partData.x[:, 1]
56vx = partData.v[:, 0]
57vy = partData.v[:, 1]
58u = partData.u
59# S = partData.S # doesn't exist yet?
60P = partData.pressure
61rho = partData.density
62
63
64r = np.sqrt((x - 1.0) ** 2 + (y - 1.0) ** 2)
65v = -np.sqrt(vx**2 + vy**2)
66
67# Bin the data
68r_bin_edge = np.arange(0.0, 1.0, 0.02)
69r_bin = 0.5 * (r_bin_edge[1:] + r_bin_edge[:-1])
70rho_bin, _, _ = stats.binned_statistic(r, rho, statistic="mean", bins=r_bin_edge)
71v_bin, _, _ = stats.binned_statistic(r, v, statistic="mean", bins=r_bin_edge)
72P_bin, _, _ = stats.binned_statistic(r, P, statistic="mean", bins=r_bin_edge)
73# S_bin, _, _ = stats.binned_statistic(r, S, statistic="mean", bins=r_bin_edge)
74u_bin, _, _ = stats.binned_statistic(r, u, statistic="mean", bins=r_bin_edge)
75rho2_bin, _, _ = stats.binned_statistic(r, rho**2, statistic="mean", bins=r_bin_edge)
76v2_bin, _, _ = stats.binned_statistic(r, v**2, statistic="mean", bins=r_bin_edge)
77P2_bin, _, _ = stats.binned_statistic(r, P**2, statistic="mean", bins=r_bin_edge)
78# S2_bin, _, _ = stats.binned_statistic(r, S ** 2, statistic="mean", bins=r_bin_edge)
79u2_bin, _, _ = stats.binned_statistic(r, u**2, statistic="mean", bins=r_bin_edge)
80rho_sigma_bin = np.sqrt(rho2_bin - rho_bin**2)
81v_sigma_bin = np.sqrt(v2_bin - v_bin**2)
82P_sigma_bin = np.sqrt(P2_bin - P_bin**2)
83# S_sigma_bin = np.sqrt(S2_bin - S_bin ** 2)
84u_sigma_bin = np.sqrt(u2_bin - u_bin**2)
85
86# Analytic solution
87N = 1000 # Number of points
88
89x_s = np.arange(0, 2.0, 2.0 / N) - 1.0
90rho_s = np.ones(N) * rho0
91P_s = np.ones(N) * rho0
92v_s = np.ones(N) * v0
93
94# Shock position
95u0 = rho0 * P0 * (gas_gamma - 1)
96us = 0.5 * (gas_gamma - 1) * v0
97rs = us * time
98
99# Post-shock values
100rho_s[np.abs(x_s) < rs] = rho0 * ((gas_gamma + 1) / (gas_gamma - 1)) ** 2
101P_s[np.abs(x_s) < rs] = 0.5 * rho0 * v0**2 * (gas_gamma + 1) ** 2 / (gas_gamma - 1)
102v_s[np.abs(x_s) < rs] = 0.0
103
104# Pre-shock values
105rho_s[np.abs(x_s) >= rs] = rho0 * (1 + v0 * time / np.abs(x_s[np.abs(x_s) >= rs]))
106P_s[np.abs(x_s) >= rs] = 0
107v_s[x_s >= rs] = -v0
108v_s[x_s <= -rs] = v0
109
110# Additional arrays
111u_s = P_s / (rho_s * (gas_gamma - 1.0)) # internal energy
112s_s = P_s / rho_s**gas_gamma # entropic function
113
114# Plot the interesting quantities
115plt.figure(figsize=(7, 7 / 1.6))
116
117line_color = "C4"
118binned_color = "C2"
119binned_marker_size = 4
120
121scatter_props = dict(
122 marker=".",
123 ms=1,
124 markeredgecolor="none",
125 alpha=0.2,
126 zorder=-1,
127 rasterized=True,
128 linestyle="none",
129)
130
131errorbar_props = dict(color=binned_color, ms=binned_marker_size, fmt=".", lw=1.2)
132
133rows = 2
134cols = 2
135
136# Velocity profile --------------------------------
137plt.subplot(rows, cols, 1)
138plt.plot(r, v, **scatter_props)
139plt.plot(x_s, v_s, "--", color=line_color, alpha=0.8, lw=1.2)
140plt.errorbar(r_bin, v_bin, yerr=v_sigma_bin, **errorbar_props)
141plt.xlabel("${\\rm{Radius}}~r$", labelpad=0)
142plt.ylabel("${\\rm{Velocity}}~v_r$", labelpad=-4)
143plt.xlim(0, 0.5)
144plt.ylim(-1.2, 0.4)
145
146# Density profile --------------------------------
147plt.subplot(rows, cols, 2)
148plt.plot(r, rho, **scatter_props)
149plt.plot(x_s, rho_s, "--", color=line_color, alpha=0.8, lw=1.2)
150plt.errorbar(r_bin, rho_bin, yerr=rho_sigma_bin, **errorbar_props)
151plt.xlabel("${\\rm{Radius}}~r$", labelpad=0)
152plt.ylabel("${\\rm{Density}}~\\rho$", labelpad=0)
153plt.xlim(0, 0.5)
154plt.ylim(0.95, 19)
155
156# Pressure profile --------------------------------
157plt.subplot(rows, cols, 3)
158plt.plot(r, P, **scatter_props)
159plt.plot(x_s, P_s, "--", color=line_color, alpha=0.8, lw=1.2)
160plt.errorbar(r_bin, P_bin, yerr=P_sigma_bin, **errorbar_props)
161plt.xlabel("${\\rm{Radius}}~r$", labelpad=0)
162plt.ylabel("${\\rm{Pressure}}~P$", labelpad=0)
163plt.xlim(0, 0.5)
164plt.ylim(-0.5, 11)
165
166# Internal energy profile -------------------------
167plt.subplot(rows, cols, 4)
168plt.plot(r, u, **scatter_props)
169plt.plot(x_s, u_s, "--", color=line_color, alpha=0.8, lw=1.2)
170plt.errorbar(r_bin, u_bin, yerr=u_sigma_bin, **errorbar_props)
171plt.xlabel("${\\rm{Radius}}~r$", labelpad=0)
172plt.ylabel("${\\rm{Internal~Energy}}~u$", labelpad=0)
173plt.xlim(0, 0.5)
174plt.ylim(-0.05, 0.8)
175
176plt.tight_layout()
177
178fname = f"NohProfile{time:.4f}.png"
179plt.savefig(fname, dpi=200)
180print("saved", fname)
Reads in particle fields from a single .vtu file, or all .vtu at a given snapshot time specified in t...