Peano
Loading...
Searching...
No Matches
test_fv_amr_setup.py
Go to the documentation of this file.
1"""Unit tests for the setup with user-coded refinement criterion
2(@ref exahype2.api.fv.SetupSimulationWithUserCodedRefinementCriterion)."""
3
4import types
5
6import peano4.api
8import exahype2.api
9import pytest
10
13
14
15def _solver(with_criterion):
16 solver = exahype2.api.Solver("Probe", 4, 0)
17 solver.add_pde_term(exahype2.api.Flux(implementation=""))
18 solver.add_pde_term(exahype2.api.MaxEigenvalue(implementation=""))
19 solver.add_pde_term(exahype2.api.InitPointWisely(implementation=""))
20 if with_criterion:
21 solver.add_pde_term(
23 implementation="if (Q[0] > 0.8) *refinementCommand = ::exahype2::RefinementCommand::Refine;"
24 )
25 )
26 solver.numerical_scheme = exahype2.api.fv.RusanovWithExplicitEuler(solver, 9)
27 solver.grid_construction = SetupWithCriterion(
28 solver, volume_size=0.02, min_volume_size=0.002
29 )
30 return solver
31
32
33def _setup_graph(solver, tmp_path):
34 peano_project = peano4.api.Project(["probe"], "probe", directory=str(tmp_path))
35 exahype2_project = types.SimpleNamespace(periodic_BC=[True, True, True])
37 solver.grid_construction.construct_output(exahype2_project, peano_project, graph)
38 return graph
39
40
41def _task(graph, suffix):
42 matches = [task for task in graph.tasks if task.name.endswith(suffix)]
43 assert len(matches) == 1, [task.name for task in graph.tasks]
44 return matches[0]
45
46
48 graph = _setup_graph(_solver(with_criterion=False), tmp_path)
49 tagged = graph.find_tasks(SetupOverRegularGrid.FinishedInitialRegularMeshSetup)
50 assert [task.name for task in tagged] == ["Probe Initial condition"]
51
52
54 graph = _setup_graph(_solver(with_criterion=False), tmp_path)
55 assert not any(task.name.endswith("Adapt mesh") for task in graph.tasks)
56 assert _task(graph, "Project on faces").dependencies == [
57 _task(graph, "Initial condition")
58 ]
59
60
62 graph = _setup_graph(_solver(with_criterion=True), tmp_path)
63 adapt = _task(graph, "Adapt mesh")
64 assert adapt.dependencies == [_task(graph, "Initial condition")]
65 assert _task(graph, "Project on faces").dependencies == [adapt]
66 assert adapt.has_to_repeat == SetupWithCriterion.RepeatCondition
67 assert "isGridStationary" in adapt.has_to_repeat
68 assert "RefinementControlService" in adapt.has_to_repeat
69 # The projection is the very last task
70 assert not any(
71 _task(graph, "Project on faces") in task.dependencies for task in graph.tasks
72 )
73
74
76 graph = _setup_graph(_solver(with_criterion=True), tmp_path)
77 action_sets = _task(graph, "Adapt mesh").action_sets
78 assert [type(action_set) for action_set in action_sets] == [
81 ]
82 criterion, initialisation = action_sets
84 body = criterion.get_body_of_operation(ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME)
85 assert "refineErasePointwisely(" in body
86 assert (
87 "::exahype2::RefinementCommand command = ::exahype2::RefinementCommand::Keep;"
88 in body
89 )
90 # Volumes of 0.002 within patches of 9 volumes, regular mesh volumes of 0.02
91 assert ">= 0.018" in body
92 # The setup never erases
93 assert "RefinementCommand::Erase," not in body
94 assert "finishStep()" in criterion.get_body_of_unprepareTraversal()
95 assert (
96 "createGridControlEventsForNextTraversal()"
97 in criterion.get_body_of_createGridControlEventsForNextTraversal()
98 )
99 create_cell = initialisation.get_body_of_operation(ActionSet.OPERATION_CREATE_CELL)
100 assert "initPointWisely(" in create_cell
101 assert (
102 initialisation.get_body_of_operation(ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME)
103 == ""
104 )
105
106
108 solver = _solver(with_criterion=True)
109 with pytest.raises(AssertionError):
110 SetupWithCriterion(solver, volume_size=0.002, min_volume_size=0.02)
111
112
114 solver = exahype2.api.Solver("Probe", 4, 0)
115 solver.add_pde_term(exahype2.api.Flux(implementation=""))
116 solver.add_pde_term(exahype2.api.MaxEigenvalue(implementation=""))
117 solver.add_pde_term(exahype2.api.InitPointWisely(implementation=""))
118 solver.add_pde_term(exahype2.api.GradientRefinement(implementation=""))
119 solver.numerical_scheme = exahype2.api.fv.RusanovWithExplicitEuler(solver, 9)
120 solver.grid_construction = SetupWithCriterion(
121 solver, volume_size=0.02, min_volume_size=0.002
122 )
123 criterion, _ = _task(_setup_graph(solver, tmp_path), "Adapt mesh").action_sets
124 assert isinstance(
126 )
127 assert (
128 "createGridControlEventsForNextTraversal()"
129 in criterion.get_body_of_createGridControlEventsForNextTraversal()
130 )
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 ...
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.
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-...
Setup phase counterpart to RusanovWithExplicitEuler: sets the initial condition for a Finite Volume s...
Set the initial condition within cells which the mesh refinement creates.
Represents a Peano 4 project.
Definition Project.py:23
Action set (reactions to events)
Definition ActionSet.py:6
Holds a sequence of Task instances.
Definition Graph.py:9
The task graph abstraction allows users to formalise their solver steps over small task graphs which ...
Definition __init__.py:1
test_adapt_mesh_task_evaluates_gradient_criterion(tmp_path)
test_min_volume_size_must_not_exceed_volume_size()
test_adapt_mesh_task_hosts_criterion_and_initialisation(tmp_path)
test_adapt_mesh_task_sits_between_initial_condition_and_projection(tmp_path)
_task(graph, suffix)
_solver(with_criterion)
_setup_graph(solver, tmp_path)
test_regular_grid_tags_initial_condition(tmp_path)
test_without_criterion_setup_equals_regular_grid(tmp_path)