5Create the PNG illustrations of the tutorial 44_localisation.ipynb
7The script builds the tutorial's setup with the two Euler solvers EulerFV
8and CoarseEuler, runs it, and writes two pictures into the directory of
9this script: the density of both solvers over time at the centre of the
10domain, and the density of both solvers side by side at t=0.1. The
11implementation of EulerFV is taken from the tutorial's %%writefile cell.
12Call the script from anywhere:
14 python3 tutorials/exahype2/illustrations/44_localisation.py
16All builds and outputs go into a temporary directory (or --work-directory).
17The diagram 44_localisation.png is not created by this script.
28Here = os.path.dirname(os.path.abspath(__file__))
29PeanoRoot = os.path.abspath(os.path.join(Here,
"..",
"..",
".."))
30sys.path.insert(0, os.path.join(PeanoRoot,
"src"))
35import matplotlib.pyplot
as plt
41DomainSize = [0.2, 0.2]
43Solvers = [
"EulerFV",
"CoarseEuler"]
47 """! Body of the tutorial's cell which writes EulerFV.cpp"""
48 with open(os.path.join(Here,
"..",
"44_localisation.ipynb"))
as notebook_file:
49 cells = json.load(notebook_file)[
"cells"]
51 source =
"".join(cell[
"source"])
52 if source.startswith(
"%%writefile 44_localisation/EulerFV.cpp"):
53 return source.split(
"\n", 1)[1]
54 raise Exception(
"tutorial has no cell writing EulerFV.cpp")
59 solver, first_plot_time_stamp=0.0, time_in_between_plots=0.005
64 """! Generate, compile and run the tutorial setup within directory"""
66 namespace=[
"tutorials",
"exahype2",
"localisation"],
77 fine_solver, PatchSize
80 fine_solver, volume_size=0.001
85 coarse_solver.add_user_solver_includes(
86 '#include "repositories/SolverRepository.h"\n'
88 coarse_solver.add_pde_term(
90 implementation=
"repositories::instanceOfEulerFV.flux(Q, x, normal, F);",
94 coarse_solver.add_pde_term(
96 implementation=
"repositories::instanceOfEulerFV.maxEigenvalue(Q, x, normal, lambdaMax);",
100 coarse_solver.add_pde_term(
102 implementation=
"repositories::instanceOfEulerFV.initPointWisely(x, Q);",
110 coarse_solver, PatchSize
113 coarse_solver, volume_size=0.005
117 project.add_solver(fine_solver)
118 project.add_solver(coarse_solver)
119 project.set_global_simulation_parameters(
125 periodic_BC=[
True,
True,
True],
127 project.set_Peano4_installation(PeanoRoot, peano4.api.output.CompileMode.RELEASE)
128 project.generate_Peano4_project().generate()
129 with open(os.path.join(directory,
"EulerFV.cpp"),
"w")
as implementation_file:
131 subprocess.run([
"make",
"-j"], cwd=directory, check=
True, stdout=subprocess.DEVNULL)
132 subprocess.run([
"./euler"], cwd=directory, check=
True, stdout=subprocess.DEVNULL)
136 """! Time stamps and density of solver name at point"""
141 "tarch.plotter.api.probe",
143 *[str(c)
for c
in point],
146 f
"solution-{name}.peano-patch-file",
149 env=dict(os.environ, PYTHONPATH=os.path.join(PeanoRoot,
"src")),
151 stdout=subprocess.DEVNULL,
153 with open(os.path.join(directory, f
"probe-{name}.csv"))
as csv_file:
154 rows = list(csv.reader(csv_file))
155 return [float(row[0])
for row
in rows[1:]], [float(row[1])
for row
in rows[1:]]
160 return min(range(len(snapshots)), key=
lambda i: abs(snapshots[i][0] - time_stamp))
164 fig, ax = plt.subplots(figsize=(7, 4))
166 t, rho =
probe(directory, name, (0.0, 0.0))
167 ax.plot(t, rho, label=name)
169 ax.set_ylabel(
"density at x=(0,0)")
172 os.path.join(Here,
"44_localisation-probe.png"), dpi=100, bbox_inches=
"tight"
178 fig, axes = plt.subplots(1, 2, figsize=(12, 5))
179 for ax, name
in zip(axes, Solvers):
180 file_name = os.path.join(directory, f
"solution-{name}.peano-patch-file")
187 label=f
"{name}: density",
190 os.path.join(Here,
"44_localisation-comparison.png"),
197if __name__ ==
"__main__":
198 parser = argparse.ArgumentParser(
199 description=
"Create the PNGs of the tutorial 44_localisation.ipynb"
204 help=
"Directory for builds and outputs (default: temporary directory)",
206 args = parser.parse_args()
208 work_directory = args.work_directory
or tempfile.mkdtemp(prefix=
"44_localisation-")
209 print(
"build and run in {}".format(work_directory))
213 print(
"wrote PNGs into {}".format(Here))
The conservative flux F(Q) of a hyperbolic PDE.
Point-wise initial condition: given the physical position of a single degree of freedom,...
The maximum absolute eigenvalue of the flux Jacobian in a given direction, i.e.
Abstract base class for all ExaHyPE 2 solvers.
Plotting counterpart to RusanovWithExplicitEuler: maps the patch data of a Finite Volume scheme with ...
Finite Volumes with a Rusanov Riemann solver and explicit Euler time stepping, realised over a block-...
Setup phase counterpart to RusanovWithExplicitEuler: sets the initial condition for a Finite Volume s...
build_and_run(directory)
Generate, compile and run the tutorial setup within directory.
plot_comparison(directory)
closest_snapshot(file_name, time_stamp)
euler_implementation()
Body of the tutorial's cell which writes EulerFV.cpp.
Interactive visualisation of Peano patch files within Jupyter notebooks.
plot_snapshot(file_name, snapshot=-1, unknown=0, clim=None, ax=None, cmap="viridis", label=None)
Plot one unknown of one snapshot.
read_snapshots(file_name)
Return list of (time stamp, list of patch files) from a patch meta file.