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