Peano
Loading...
Searching...
No Matches
test_solver_couplings.py
Go to the documentation of this file.
1"""Unit tests for the registration of couplings of solvers and for the
2flag add_global_tasks, with which a coupling leaves the load balancing switch and the
3dynamic mesh refinement to one of its solvers."""
4
5import types
6
7import pytest
8
9import peano4.api
11import exahype2.api
12import toolbox.loadbalancing.api.actionsets
13
15
16SCHEMES = {
17 "fv": lambda solver: exahype2.api.fv.RusanovWithExplicitEuler(solver, 4),
18 "fv-optimistic": lambda solver: exahype2.api.fv.RusanovWithOptimisticExplicitEuler(
19 solver, 4
20 ),
22 solver, patch_size=4, rk_order=2
23 ),
24}
25
26
27def _solver(scheme, name="Probe"):
28 solver = exahype2.api.Solver(name, 2)
29 solver.add_pde_term(exahype2.api.Flux(implementation="F[0] = Q[0]; F[1] = Q[1];"))
30 solver.add_pde_term(exahype2.api.MaxEigenvalue(implementation="*lambdaMax = 1.0;"))
31 solver.add_pde_term(exahype2.api.RefineErasePointwisely(implementation=""))
32 solver.add_pde_term(
33 exahype2.api.InitPointWisely(implementation="Q[0] = 0.0; Q[1] = 0.0;")
34 )
35 if scheme is not None:
36 solver.numerical_scheme = SCHEMES[scheme](solver)
37 return solver
38
39
40def _construct(solver, tmp_path, add_global_tasks):
41 exahype2_project = types.SimpleNamespace(periodic_BC=[True, True, True], _dimensions=3)
42 peano_project = peano4.api.Project(["probe"], "probe", str(tmp_path))
43 time_step = peano4.api.taskgraph.Graph()
44 solver.numerical_scheme.construct_output(
45 exahype2_project, peano_project, time_step, add_global_tasks
46 )
47 return time_step
48
49
51 return any(
52 isinstance(action_set, toolbox.loadbalancing.api.actionsets.ToggleLoadBalancing)
53 or isinstance(action_set, toolbox.loadbalancing.api.actionsets.LoadBalancing)
54 for action_set in task.action_sets
55 )
56
57
59 first, second = (peano4.api.taskgraph.Graph() for _ in range(2))
60 a, b, c, d, e = (Task(name=n, entity=Task.GridEntity.CELL) for n in "abcde")
61 b.add_dependency(a) # first: a -> b, and c alone (two sinks: b and c)
62 d.add_dependency(e) # second: e -> d, i.e. the source is e
63 for task in (a, b, c):
64 first.add_task(task)
65 for task in (d, e):
66 second.add_task(task)
67 first.append(second)
68 assert first.tasks == [a, b, c, d, e]
69 # The source of the second graph waits for all sinks of the first graph
70 assert set(e.dependencies) == {b, c}
71 # Nothing else changes
72 assert d.dependencies == [e] and b.dependencies == [a] and a.dependencies == []
73 assert second.tasks == [d, e]
74
75
77 first, second = (peano4.api.taskgraph.Graph() for _ in range(2))
78 a = Task(name="a", entity=Task.GridEntity.CELL)
79 end = Task(name="end", entity=Task.GridEntity.GLOBAL_END_OF_STEP)
80 b = Task(name="b", entity=Task.GridEntity.CELL)
81 end.add_dependency(a)
82 for task in (a, end):
83 first.add_task(task)
84 second.add_task(b)
85 first.append(second)
86 # The task compiler makes sure that b does not run in the sweep of the end-of-step task
87 assert b.dependencies == [end]
88 assert first.tasks == [a, end, b]
89
90
92 first, second = (peano4.api.taskgraph.Graph() for _ in range(2))
93 a = Task(name="a", entity=Task.GridEntity.CELL)
94 first.add_task(a)
95 second.add_task(a)
96 with pytest.raises(ValueError):
97 first.append(second)
98
99
101 first, second, merged = (peano4.api.taskgraph.Graph() for _ in range(3))
102 a = Task(name="a", entity=Task.GridEntity.CELL)
103 b = Task(name="b", entity=Task.GridEntity.CELL)
104 c = Task(name="c", entity=Task.GridEntity.CELL)
105 b.add_dependency(a)
106 for task in (a, b):
107 first.add_task(task)
108 second.add_task(c)
109 merged.merge(first)
110 merged.merge(second)
111 merged.merge(first)
112 assert merged.tasks == [a, b, c]
113 assert b.dependencies == [a]
114 assert first.tasks == [a, b]
115
116
117@pytest.mark.parametrize("scheme", SCHEMES)
119 name = exahype2.api.Solver.NumericalScheme.DYNAMIC_MESH_REFINEMENT_TASK_NAME
120 graph = _construct(_solver(scheme), tmp_path, True)
121 assert f"Probe_{name}" in [task.name for task in graph.tasks]
122 assert any(_toggles_load_balancing(task) for task in graph.tasks)
123
124
125@pytest.mark.parametrize("scheme", SCHEMES)
127 graph = _construct(_solver(scheme), tmp_path, False)
128 assert not any("Dynamic mesh refinement" in task.name for task in graph.tasks)
129 assert not any("Disable load balancing" in task.name for task in graph.tasks)
130 assert not any(_toggles_load_balancing(task) for task in graph.tasks)
131 # The solver proper is intact
132 assert any("Reconstruct" in task.name for task in graph.tasks)
133
134
136 tmp_path,
137):
138 solver = _solver("fv")
139 exahype2_project = exahype2.api.Project(
140 ["probe"], "probe", "probe", directory=str(tmp_path)
141 )
142 exahype2_project.set_global_simulation_parameters(
143 dimensions=2,
144 offset=[0.0, 0.0],
145 size=[1.0, 1.0],
146 min_end_time=1.0,
147 periodic_BC=[True, True],
148 )
149 solver.grid_construction = exahype2.api.fv.SetupSimulationOverRegularGrid(
150 solver, volume_size=0.1
151 )
152 peano_project = peano4.api.Project(["probe"], "probe", str(tmp_path))
153
154 def realise(add_global_tasks):
155 graphs = [peano4.api.taskgraph.Graph() for _ in range(4)]
156 solver.generate_solver_realisation(
157 exahype2_project, peano_project, *graphs, add_global_tasks=add_global_tasks
158 )
159 return graphs
160
161 assert len(realise(True)[3].tasks) > 0
162 assert len(realise(False)[3].tasks) == 0
163
164
166 """! Minimal coupling: it brings its solvers along and realises them itself """
167
168 def __init__(self, solvers):
169 self.solvers = solvers
170 self.realised = False
171
173 self.realised = True
174
175
177 tmp_path,
178):
179 first, second = _solver(None, "First"), _solver(None, "Second")
180 third = _solver("fv", "Third")
181 coupling = _Coupling([first, second])
182 project = exahype2.api.Project(["probe"], "probe", "probe", directory=str(tmp_path))
183 project.add_solver(third)
184 project.add_solver(coupling)
185 assert project._solvers == [third, first, second]
186 assert project._realisations == [third, coupling]
187
188
191 a, b, c = (Task(name=n, entity=Task.GridEntity.CELL) for n in "abc")
192 b.add_dependency(a)
193 for task in (a, b, c):
194 graph.add_task(task)
195 assert graph.source_tasks() == [a, c]
196 assert graph.sink_tasks() == [b, c]
The conservative flux F(Q) of a hyperbolic PDE.
Definition Flux.py:8
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.
ExaHyPE 2 project.
Definition Project.py:15
Point-wise refinement criterion: given the solution Q at a single degree of freedom,...
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-...
Finite Volumes with a Rusanov Riemann solver and optimistic explicit Euler time stepping,...
Setup phase counterpart to RusanovWithExplicitEuler: sets the initial condition for a Finite Volume s...
Fourth-order central Finite Differences with explicit Runge-Kutta time stepping, realised over a bloc...
Represents a Peano 4 project.
Definition Project.py:26
Holds a sequence of Task instances.
Definition Graph.py:9
One task within a peano4.api.taskgraph.Graph.
Definition Task.py:8
Minimal coupling: it brings its solvers along and realises them itself.
The task graph abstraction allows users to formalise their solver steps over small task graphs which ...
Definition __init__.py:1
_construct(solver, tmp_path, add_global_tasks)
test_project_registers_the_solvers_of_a_coupling_but_realises_the_coupling(tmp_path)
test_solver_without_global_tasks_does_not_contribute_to_the_mesh_adaptation(tmp_path)
_solver(scheme, name="Probe")
test_schemes_leave_the_global_tasks_to_the_host_on_request(scheme, tmp_path)
test_graph_append_connects_the_end_of_one_graph_to_the_start_of_the_other()
test_graph_source_tasks_are_the_counterpart_of_the_sink_tasks()
test_graph_append_makes_the_sources_wait_for_global_sinks_too()
test_schemes_add_the_global_tasks_by_default(scheme, tmp_path)