Peano
Loading...
Searching...
No Matches
41_postprocessing.py
Go to the documentation of this file.
1# This file is part of the Peano project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3"""!
4
5Create the PNG illustrations of the tutorial 41_postprocessing.ipynb
6
7The script builds the tutorial's shallow water setup over a flat sea floor
8(the same one as 11_tsunami.py), runs it, converts the output into VTK files
9with --split-unknowns, and plots the array hu of the snapshot at t=0.2 into
10the directory of this script. Call it from anywhere:
11
12 python3 tutorials/exahype2/illustrations/41_postprocessing.py
13
14All builds and outputs go into a temporary directory (or --work-directory).
15
16"""
17import argparse
18import importlib.util
19import os
20import subprocess
21import sys
22import tempfile
23
24Here = os.path.dirname(os.path.abspath(__file__))
25PeanoRoot = os.path.abspath(os.path.join(Here, "..", "..", ".."))
26sys.path.insert(0, os.path.join(PeanoRoot, "src"))
27
28import matplotlib
29
30matplotlib.use("Agg")
31import matplotlib.pyplot as plt
32
33import vtk
34from vtk.util.numpy_support import vtk_to_numpy
35
36
38 """! The module 11_tsunami.py, whose name is no valid Python identifier"""
39 spec = importlib.util.spec_from_file_location(
40 "tsunami_illustrations", os.path.join(Here, "11_tsunami.py")
41 )
42 module = importlib.util.module_from_spec(spec)
43 spec.loader.exec_module(module)
44 return module
45
46
47def read_vtu(file_name):
48 reader = vtk.vtkXMLUnstructuredGridReader()
49 reader.SetFileName(file_name)
50 reader.Update()
51 return reader.GetOutput()
52
53
54def plot_array(grid, name, ax):
55 """! Plot the cell data array name of grid, as the tutorial does"""
56 centres = vtk.vtkCellCenters()
57 centres.SetInputData(grid)
58 centres.Update()
59 xyz = vtk_to_numpy(centres.GetOutput().GetPoints().GetData())
60 values = vtk_to_numpy(grid.GetCellData().GetArray(name))
61 image = ax.tripcolor(xyz[:, 0], xyz[:, 1], values, shading="gouraud")
62 ax.set_aspect("equal")
63 ax.set_xlabel("x")
64 ax.set_ylabel("y")
65 ax.set_title(name)
66 ax.figure.colorbar(image, ax=ax)
67
68
69if __name__ == "__main__":
70 parser = argparse.ArgumentParser(
71 description="Create the PNGs of the tutorial 41_postprocessing.ipynb"
72 )
73 parser.add_argument(
74 "--work-directory",
75 default=None,
76 help="Directory for builds and outputs (default: temporary directory)",
77 )
78 args = parser.parse_args()
79
80 work_directory = args.work_directory or tempfile.mkdtemp(
81 prefix="41_postprocessing-"
82 )
83 print("build and run in {}".format(work_directory))
84 load_tsunami_illustrations().build_and_run(work_directory)
85
86 subprocess.run(
87 [
88 sys.executable,
89 "-m",
90 "tarch.plotter.api.render",
91 "--split-unknowns",
92 "solution-SWE.peano-patch-file",
93 ],
94 cwd=work_directory,
95 env=dict(os.environ, PYTHONPATH=os.path.join(PeanoRoot, "src")),
96 check=True,
97 stdout=subprocess.DEVNULL,
98 )
99
100 fig, ax = plt.subplots(figsize=(6, 5))
101 plot_array(
102 read_vtu(os.path.join(work_directory, "solution-SWE.SWEQ-4.vtu")), "hu", ax
103 )
104 fig.savefig(
105 os.path.join(Here, "41_postprocessing-hu.png"), dpi=100, bbox_inches="tight"
106 )
107 plt.close(fig)
108 print("wrote PNG into {}".format(Here))
load_tsunami_illustrations()
The module 11_tsunami.py, whose name is no valid Python identifier.