5Create the PNG illustrations of the tutorial 41_postprocessing.ipynb
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:
12 python3 tutorials/exahype2/illustrations/41_postprocessing.py
14All builds and outputs go into a temporary directory (or --work-directory).
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"))
31import matplotlib.pyplot
as plt
34from vtk.util.numpy_support
import vtk_to_numpy
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")
42 module = importlib.util.module_from_spec(spec)
43 spec.loader.exec_module(module)
47def read_vtu(file_name):
48 reader = vtk.vtkXMLUnstructuredGridReader()
49 reader.SetFileName(file_name)
51 return reader.GetOutput()
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)
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")
66 ax.figure.colorbar(image, ax=ax)
69if __name__ == "__main__":
70 parser = argparse.ArgumentParser(
71 description="Create the PNGs of the tutorial 41_postprocessing.ipynb"
76 help="Directory for builds and outputs (default: temporary directory)",
78 args = parser.parse_args()
80 work_directory = args.work_directory or tempfile.mkdtemp(
81 prefix="41_postprocessing-"
83 print("build and run in {}".format(work_directory))
84 load_tsunami_illustrations().build_and_run(work_directory)
90 "tarch.plotter.api.render",
92 "solution-SWE.peano-patch-file",
95 env=dict(os.environ, PYTHONPATH=os.path.join(PeanoRoot, "src")),
97 stdout=subprocess.DEVNULL,
100 fig, ax = plt.subplots(figsize=(6, 5))
102 read_vtu(os.path.join(work_directory, "solution-SWE.SWEQ-4.vtu")), "hu", ax
105 os.path.join(Here, "41_postprocessing-hu.png"), dpi=100, bbox_inches="tight"
108 print("wrote PNG into {}".format(Here))
load_tsunami_illustrations()
The module 11_tsunami.py, whose name is no valid Python identifier.