Skip to content

Commit 4b24072

Browse files
authored
add a method to the Euler class to plot the data (#36)
1 parent e95856d commit 4b24072

2 files changed

Lines changed: 205 additions & 24 deletions

File tree

examples/euler.ipynb

Lines changed: 157 additions & 24 deletions
Large diffs are not rendered by default.

ppmpy/euler.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55

6+
import matplotlib.pyplot as plt
67
import numpy as np
78

89
from ppmpy.eigen import eigen
@@ -28,6 +29,11 @@ def __init__(self):
2829
self.qu = 1
2930
self.qp = 2
3031

32+
self.prim_names = [""] * self.nvar
33+
self.prim_names[self.qrho] = "rho"
34+
self.prim_names[self.qu] = "u"
35+
self.prim_names[self.qp] = "p"
36+
3137

3238
class Euler:
3339
"""A 1D compressible Euler solver using the piecewise parabolic method
@@ -457,3 +463,45 @@ def draw_prim(self, gp, ivar):
457463
self.q_parabola[ivar].draw_parabola(gp)
458464
if not isinstance(self.q_parabola[ivar], HSEPPMInterpolant):
459465
self.q_parabola[ivar].mark_cubic(gp)
466+
467+
def plot_prim(self, *, ivar=None):
468+
"""Plot the primitive variable(s) for the current solution.
469+
470+
Parameters
471+
----------
472+
ivar : int
473+
the index of the variable to plot (None plots rho, u, p)
474+
475+
Returns
476+
-------
477+
matplotlib.pyplot.Figure
478+
"""
479+
480+
assert ivar is None or (0 <= ivar < self.v.nvar)
481+
482+
q = self.cons_to_prim()
483+
484+
if ivar is not None:
485+
fig = plt.figure()
486+
ax = fig.add_subplot()
487+
ax.plot(self.grid.x[self.grid.lo:self.grid.hi+1],
488+
q[self.grid.lo:self.grid.hi+1, ivar])
489+
ax.grid(color="0.5", linestyle=":")
490+
ax.set_xlabel("x")
491+
ax.set_ylabel(self.v.prim_names[ivar])
492+
493+
else:
494+
fig, ax = plt.subplots(self.v.nvar, 1, sharex=True)
495+
for idx in range(self.v.nvar):
496+
ax[idx].plot(self.grid.x[self.grid.lo:self.grid.hi+1],
497+
q[self.grid.lo:self.grid.hi+1, idx])
498+
ax[idx].grid(color="0.5", linestyle=":")
499+
if idx == self.v.nvar-1:
500+
ax[idx].set_xlabel("x")
501+
ax[idx].set_ylabel(self.v.prim_names[idx])
502+
503+
size = fig.get_size_inches()
504+
fig.set_size_inches(size[0], size[0]*self.v.nvar/2.5)
505+
506+
fig.tight_layout()
507+
return fig

0 commit comments

Comments
 (0)