Peano
Loading...
Searching...
No Matches
manipulate-solver-steps-illustrations.py
Go to the documentation of this file.
1# This file is part of the ExaHyPE2 project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3"""!
4
5Create the illustrations of the page "Manipulate an existing solver's steps"
6
7Run from anywhere via
8
9~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10python3 documentation/exahype2/programmers-guide/manipulate-solver-steps-illustrations.py
11~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12
13The script writes two PNGs next to it:
14
15- manipulate-solver-steps-rusanov-original.png: the time stepping task graph
16 of exahype2.api.fv.RusanovWithExplicitEuler as the scheme builds it;
17- manipulate-solver-steps-rusanov-with-step.png: the same graph built by
18 the subclass RusanovWithPostprocessing, which inserts one step after the
19 task carrying exahype2.api.Solver.SolutionUpdateTaskTag. The subclass is
20 the example of the page.
21
22Both graphs are constructed from the actual numerical scheme, so the
23pictures always match the code. The kernels this requires are generated
24into a temporary directory.
25
26"""
27import os
28import sys
29import tempfile
30import types
31
32Here = os.path.dirname(os.path.abspath(__file__))
33sys.path.insert(0, os.path.abspath(os.path.join(Here, "..", "..", "..", "src")))
34
35import matplotlib
36
37matplotlib.use("Agg")
38import matplotlib.pyplot as plt
39
40import peano4.api
42import exahype2.api
44
45
47 """! The example of the page: insert a step after each solution update"""
48
49 def construct_output(self, exahype2_project, peano_project, time_step):
50 super().construct_output(exahype2_project, peano_project, time_step)
51 solver = self._solver_solver
52 for anchor in time_step.find_tasks(exahype2.api.Solver.SolutionUpdateTaskTag):
53 if not anchor.name.startswith(solver.name + "_"):
54 continue
56 name=f"{solver.name}_Postprocess solution",
57 entity=peano4.api.taskgraph.Task.GridEntity.CELL,
58 )
59 task.use_cell(
60 solver._patch, peano4.api.taskgraph.Task.DataAccess.READ_AND_WRITE
61 )
62 task.add_action_set(
63 toolbox.blockstructured.api.ApplyPointWiselyToPatch(
64 patch=solver._patch,
65 implementation="Q[0] = std::max(Q[0], 0.9);",
66 guard=solver.grid_selector.createCellGuard(),
67 additional_includes=solver._get_default_includes()
68 + solver.user_solver_includes,
69 )
70 )
71 time_step.insert_after(anchor, task)
72
73
74def plot_time_step(scheme, file_name, title):
75 # The Python API writes a log file when the program terminates. Keep it
76 # out of the working directory.
77 peano4.api.Logger().file_name = os.path.join(
78 tempfile.gettempdir(), "manipulate-solver-steps-illustrations-log.md"
79 )
80 with tempfile.TemporaryDirectory() as directory:
81 peano_project = peano4.api.Project(
82 ["illustration"], "illustration", directory=directory
83 )
84 solver = exahype2.api.Solver("Euler", 4, 0)
85 solver.add_pde_term(exahype2.api.Flux(stateless=False))
86 solver.add_pde_term(exahype2.api.MaxEigenvalue(stateless=False))
87 solver.numerical_scheme = scheme(solver, 9)
88 exahype2_project = types.SimpleNamespace(periodic_BC=[True, True])
89 time_step = peano4.api.taskgraph.Graph()
90 solver.numerical_scheme.construct_output(
91 exahype2_project, peano_project, time_step
92 )
93 time_step.visualise(title=title)
94 output = os.path.join(Here, file_name)
95 plt.gcf().savefig(output, dpi=90)
96 plt.close(plt.gcf())
97 print("Wrote", output)
98
99
100if __name__ == "__main__":
103 "manipulate-solver-steps-rusanov-original.png",
104 "RusanovWithExplicitEuler",
105 )
107 RusanovWithPostprocessing,
108 "manipulate-solver-steps-rusanov-with-step.png",
109 "RusanovWithPostprocessing",
110 )
The conservative flux F(Q) of a hyperbolic PDE.
Definition Flux.py:8
The maximum absolute eigenvalue of the flux Jacobian in a given direction, i.e.
Abstract base class for all ExaHyPE 2 solvers.
Definition Solver.py:17
Finite Volumes with a Rusanov Riemann solver and explicit Euler time stepping, realised over a block-...
_solver
Build the reconstruction tasks befilling the patch_with_halo, which is logically temporary.
The example of the page: insert a step after each solution update.
construct_output(self, exahype2_project, peano_project, time_step)
Has to be implemented by subclass.
Singleton logger for the Peano 4 Python API.
Definition Logger.py:37
Represents a Peano 4 project.
Definition Project.py:23
Holds a sequence of Task instances.
Definition Graph.py:9
One task within a peano4.api.taskgraph.Graph.
Definition Task.py:8
The task graph abstraction allows users to formalise their solver steps over small task graphs which ...
Definition __init__.py:1