Peano
Loading...
Searching...
No Matches
test_dynamic_mesh_refinement.py
Go to the documentation of this file.
1import types
2
3import pytest
4
5import peano4.api
8
9import exahype2.api
10import toolbox.loadbalancing.api.actionsets
11
14
15SCHEMES = {
16 "fv": lambda solver, **bounds: exahype2.api.fv.RusanovWithExplicitEuler(
17 solver, 4, **bounds
18 ),
19 "fv-optimistic": lambda solver, **bounds: exahype2.api.fv.RusanovWithOptimisticExplicitEuler(
20 solver, 4, **bounds
21 ),
22 "rkfd4": lambda solver, **bounds: exahype2.api.rkfd4.CentralDifferencesWithRungeKutta(
23 solver, patch_size=4, rk_order=2, **bounds
24 ),
25}
26
27
28def _solver(scheme, criterion=True, **bounds):
29 solver = exahype2.api.Solver("Probe", 2)
30 solver.add_pde_term(exahype2.api.Flux(implementation="F[0] = Q[0]; F[1] = Q[1];"))
31 solver.add_pde_term(exahype2.api.MaxEigenvalue(implementation="*lambdaMax = 1.0;"))
32 if criterion:
33 solver.add_pde_term(exahype2.api.RefineErasePointwisely(implementation=""))
34 solver.numerical_scheme = SCHEMES[scheme](solver, **bounds)
35 return solver
36
37
38def _last_task(solver, tmp_path):
39 """!
40 The one task of the time step on which no other task depends, apart from
41 the tasks that act at the end of the time step (such as the one that
42 computes the time step size)
43 """
44 exahype2_project = types.SimpleNamespace(periodic_BC=[True, True, True], _dimensions=3)
45 peano_project = peano4.api.Project(["probe"], "probe", str(tmp_path))
46 time_step = peano4.api.taskgraph.Graph()
47 solver.numerical_scheme.construct_output(exahype2_project, peano_project, time_step)
48 dependencies = {
49 id(dependency) for task in time_step.tasks for dependency in task.dependencies
50 }
51 [last] = [
52 task
53 for task in time_step.tasks
54 if id(task) not in dependencies
55 and task.entity != Task.GridEntity.GLOBAL_END_OF_STEP
56 ]
57 return last
58
59
60def _criterion_body(action_set):
61 return action_set.get_body_of_operation(ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME)
62
63
64@pytest.mark.parametrize("scheme", SCHEMES)
66 last = _last_task(_solver(scheme), tmp_path)
67 assert last.name == f"Probe_{exahype2.api.Solver.NumericalScheme.DYNAMIC_MESH_REFINEMENT_TASK_NAME}"
68 assert last.entity == Task.GridEntity.CELL
69 dynamic_mesh_refinement, load_balancing = last.action_sets
70 assert isinstance(
72 )
73 assert isinstance(
74 load_balancing, toolbox.loadbalancing.api.actionsets.LoadBalancing
75 )
76 body = load_balancing.get_body_of_unprepareTraversal()
77 assert body.index("enable(true)") < body.index("finishStep()")
78
79
80@pytest.mark.parametrize("scheme", SCHEMES)
82 [dynamic_mesh_refinement, _] = _last_task(_solver(scheme), tmp_path).action_sets
83 assert "RefinementControlService" not in (
84 dynamic_mesh_refinement.get_body_of_createGridControlEventsForNextTraversal()
85 )
86 assert "refineErasePointwisely" in _criterion_body(dynamic_mesh_refinement)
87 assert "RefinementControlService::getInstance().finishStep()" in (
88 dynamic_mesh_refinement.get_body_of_unprepareTraversal()
89 )
90
91
92@pytest.mark.parametrize("scheme", SCHEMES)
94 last = _last_task(_solver(scheme, criterion=False), tmp_path)
95 assert last.name == f"Probe_{exahype2.api.Solver.NumericalScheme.DYNAMIC_MESH_REFINEMENT_TASK_NAME}"
96 [load_balancing] = last.action_sets
97 assert isinstance(
98 load_balancing, toolbox.loadbalancing.api.actionsets.LoadBalancing
99 )
100
101
102@pytest.mark.parametrize("scheme", SCHEMES)
104 solver = _solver(scheme, criterion=False)
105 solver.add_pde_term(exahype2.api.GradientRefinement(implementation=""))
106 [dynamic_mesh_refinement, _] = _last_task(solver, tmp_path).action_sets
107 assert isinstance(
108 dynamic_mesh_refinement,
110 )
111 body = _criterion_body(dynamic_mesh_refinement)
112 assert "refineErasePointwisely" not in body
113 assert "for (int normal=0; normal<Dimensions; normal++)" in body
114 assert "for (int side=0; side<2; side++)" in body
115 assert "::exahype2::fv::computeOneSidedGradient(" in body
116 assert "refineEraseWithGradient(" in body
117 assert "double gradient[2 + 0];" in body
118 assert "anyVolumeWantsToRefine or command" in body
119 assert "allVolumesWantToErase and command" in body
120 assert "finishStep()" in dynamic_mesh_refinement.get_body_of_unprepareTraversal()
121
122
123@pytest.mark.parametrize("scheme", SCHEMES)
125 [dynamic_mesh_refinement, _] = _last_task(_solver(scheme), tmp_path).action_sets
126 assert "RefinementCommand::Erase," in _criterion_body(dynamic_mesh_refinement)
127
128
130 solver = _solver("fv")
131 solver.add_pde_term(exahype2.api.GradientRefinement(implementation=""))
132 with pytest.raises(ValueError):
133 _last_task(solver, tmp_path)
134
135
137 [dynamic_mesh_refinement, _] = _last_task(_solver("fv"), tmp_path).action_sets
138 body = _criterion_body(dynamic_mesh_refinement)
139 assert "tarch::la::max( marker.h() ) >=" not in body
140 assert "3.0 * tarch::la::max( marker.h() ) <" not in body
141
142
143@pytest.mark.parametrize("scheme", SCHEMES)
145 solver = _solver(scheme, min_volume_size=0.25, max_volume_size=1.0)
146 [dynamic_mesh_refinement, _] = _last_task(solver, tmp_path).action_sets
147 body = _criterion_body(dynamic_mesh_refinement)
148 # Volume sizes times patch size 4
149 assert "tarch::la::max( marker.h() ) >= 1.0" in body
150 assert "3.0 * tarch::la::max( marker.h() ) < 4.0" in body
151
152
153@pytest.mark.parametrize("scheme", SCHEMES)
155 with pytest.raises(AssertionError):
156 _solver(scheme, min_volume_size=1.0, max_volume_size=0.5)
The conservative flux F(Q) of a hyperbolic PDE.
Definition Flux.py:8
Gradient-based refinement criterion: given a one-sided difference quotient of the solution along one ...
The maximum absolute eigenvalue of the flux Jacobian in a given direction, i.e.
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
Evaluate the gradient-based refinement criterion and collect the outcome as refine and erase requests...
Evaluate the point-wise refinement criterion and collect the outcome as refine and erase requests.
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,...
Fourth-order central Finite Differences with explicit Runge-Kutta time stepping, realised over a bloc...
Represents a Peano 4 project.
Definition Project.py:26
Action set (reactions to events).
Definition ActionSet.py:6
Holds a sequence of Task instances.
Definition Graph.py:9
One task within a peano4.api.taskgraph.Graph.
Definition Task.py:8
The Python-side model of "what happens during one mesh sweep".
Definition __init__.py:1
The task graph abstraction allows users to formalise their solver steps over small task graphs which ...
Definition __init__.py:1
test_gradient_criterion_yields_gradient_action_set(scheme, tmp_path)
_last_task(solver, tmp_path)
The one task of the time step on which no other task depends, apart from the tasks that act at the en...
test_last_task_evaluates_criterion_and_switches_on_load_balancing(scheme, tmp_path)
_solver(scheme, criterion=True, **bounds)
test_time_step_never_hands_events_to_peano(scheme, tmp_path)