Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
stabilization.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2025 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
5
7
8template <int dim>
9void
10moe_scalar_limiter(const DoFHandler<dim> &dof_handler,
11 GlobalVectorType &locally_relevant_vector,
12 GlobalVectorType &locally_owned_vector)
13{
14 // The limiter requires a vector in which the value can be modified.
15 // For this we use the local_evaluation_point. We thus begin by making sure
16 // that the local_evaluation_point is set at the present value of the solution
17 // to limit.
18 locally_owned_vector = locally_relevant_vector;
19
20 // We define the cutoff function from the Moe paper using a lambda function.
21 // It's a bit weird this cutoff function, but YOLO I guess. Why do we divide y
22 // by 1.1? I do not know, I believe it to be one of the mysteries of life.
23 auto phi_limit = [](const double y) { return std::min(1., y / 1.1); };
24
25 // The strategy for limiting requires looping over the cells twice.
26 // First loop over all active cells (local and ghost):
27 // 1. Calculate the max and the min value of the field we wish to limit
28 // Loop over every local cells:
29 // 2. Calculate approximate upper and lower bounds using the neighbors
30 // 3. Calculate the value of the theta limiting parameter
31 // 4. Rescale the nodal values of the solution using the average value of the
32 // solution within the element and the theta limiter.
33
34 // We need the vertices-to-cell map to have access to the neighbors rapidly
35 // We obtain this map using the LetheGridTools functionnalities.
36 std::map<unsigned int,
37 std::set<typename DoFHandler<dim>::active_cell_iterator>>
38 vertices_to_cell;
39 LetheGridTools::vertices_cell_mapping(dof_handler, vertices_to_cell);
40
41 // Step 1. loop over every cell and calculate the max, the min and the mean.
42 // These values are stored in std::map for rapid access.
43 std::map<typename ::dealii::types::global_cell_index, double>
44 max_value_per_cells;
45 std::map<typename ::dealii::types::global_cell_index, double>
46 min_value_per_cells;
47 std::map<typename ::dealii::types::global_cell_index, double>
48 mean_value_per_cells;
49
50 std::vector<types::global_dof_index> local_dof_indices(
51 dof_handler.get_fe().n_dofs_per_cell());
52
53 for (const auto &cell : dof_handler.active_cell_iterators())
54 {
55 // We need to loop over cells that are either locally_owned or ghost to
56 // gather all the neighbors.
57 if (cell->is_locally_owned() || cell->is_ghost())
58 {
59 cell->get_dof_indices(local_dof_indices);
60 double max_value = -DBL_MAX;
61 double min_value = DBL_MAX;
62 double mean_value = 0;
63 for (const auto &i_dof : local_dof_indices)
64 {
65 // Get the max and the min value of the solution
66 double dof_value = locally_relevant_vector(i_dof);
67 max_value = std::max(max_value, dof_value);
68 min_value = std::min(min_value, dof_value);
69 // Calculate mean solution \bar{w}. Right now this is an average
70 // of the nodal value which is lazy as balls. A better one will be
71 // to do an integral, but this is dev and I am a lazy bum for now.
72 mean_value += dof_value;
73 }
74 mean_value /= local_dof_indices.size();
75 min_value_per_cells[cell->global_active_cell_index()] = min_value;
76 max_value_per_cells[cell->global_active_cell_index()] = max_value;
77 mean_value_per_cells[cell->global_active_cell_index()] = mean_value;
78 }
79 }
80
81 // Loop over every cell and do:
82 // 2. Calculate approximate upper and lower bounds using the neighbhors
83 // 3. Calculate the theta limiter
84 // 4. Rescale the nodal values of the solution
85
86 for (const auto &cell : dof_handler.active_cell_iterators())
87 {
88 if (cell->is_locally_owned())
89 {
90 cell->get_dof_indices(local_dof_indices);
91
92 // Active neighbors include the current cell as well
93 auto active_neighbors =
94 LetheGridTools::find_cells_around_cell<dim>(vertices_to_cell, cell);
95
96
97 // 2. Calculate approximate upper and lower bounds using the
98 // neighbhors
99
100 // Here we assume that alpha = 0 (see the Moe paper). The definition
101 // of alpha in the original paper is flaky in my opinion, let's use
102 // the more robust version as a starting point.
103 double upper_bound =
104 mean_value_per_cells.at(cell->global_active_cell_index());
105 double lower_bound =
106 mean_value_per_cells.at(cell->global_active_cell_index());
107
108
109 for (const auto &neighbor : active_neighbors)
110 {
111 // The shock capture mechanism theta calculation should not
112 // consider the cell itself.
113 if (cell->global_active_cell_index() ==
114 neighbor->global_active_cell_index())
115 continue;
116
117 // The original paper uses the max and the min of the neighbor
118 // solutions. I have also tested with the mean values of the
119 // neighbor. It seemed a bit more robust, but I'd rather follow
120 // the paper for now. Let's just keep in mind that this can be a
121 // fallback.
122
123 upper_bound = std::max(upper_bound,
124 max_value_per_cells.at(
125 neighbor->global_active_cell_index()));
126
127 lower_bound = std::min(lower_bound,
128 min_value_per_cells.at(
129 neighbor->global_active_cell_index()));
130 }
131
132 // 3. Calculate the value of the theta limiting using the max, min and
133 // mean values as well as the bounds.
134 const double max_value =
135 max_value_per_cells.at(cell->global_active_cell_index());
136 const double min_value =
137 min_value_per_cells.at(cell->global_active_cell_index());
138 const double mean_value =
139 mean_value_per_cells.at(cell->global_active_cell_index());
140 double y_max =
141 (upper_bound - mean_value) / ((max_value - mean_value) + 1e-16);
142 double y_min =
143 (lower_bound - mean_value) / ((min_value - mean_value) + 1e-16);
144
145 double theta = std::min(1., phi_limit(y_max));
146 theta = std::min(theta, phi_limit(y_min));
147
148 // 4. Rescale the solution within the element
149 for (const auto &i : local_dof_indices)
150 {
151 // Get the value of the solution at the DOFs and rescale it
152 // using the mean value and the theta limiter.
153 const double dof_value = locally_relevant_vector(i);
154 locally_owned_vector(i) =
155 mean_value + theta * (dof_value - mean_value);
156 }
157 }
158 }
159
160 // Reset the present solution and the evaluation point to the new updated
161 // solution.
162 locally_relevant_vector = locally_owned_vector;
163}
164
165
166template void
167moe_scalar_limiter(const DoFHandler<2> &dof_handler,
168 GlobalVectorType &locally_relevant_vector,
169 GlobalVectorType &locally_owned_vector);
170
171template void
172moe_scalar_limiter(const DoFHandler<3> &dof_handler,
173 GlobalVectorType &locally_relevant_vector,
174 GlobalVectorType &locally_owned_vector);
void vertices_cell_mapping(const DoFHandler< dim > &dof_handler, std::map< unsigned int, std::set< typename DoFHandler< dim >::active_cell_iterator > > &vertices_cell_map)
Map the vertex index to the cells that includes that vertex.
std::vector< typename DoFHandler< dim >::active_cell_iterator > find_cells_around_cell(std::map< unsigned int, std::set< typename DoFHandler< dim >::active_cell_iterator > > &vertices_cell_map, const typename DoFHandler< dim >::active_cell_iterator &cell)
Return a vector of cells around a cell. The vector of cells includes all the cells that share a verte...
void moe_scalar_limiter(const DoFHandler< dim > &dof_handler, GlobalVectorType &locally_relevant_vector, GlobalVectorType &locally_owned_vector)
Implements a Moe a posteriori shock capturing method to keep the field bounded. This limiter is only ...
dealii::TrilinosWrappers::MPI::Vector GlobalVectorType
Definition vector.h:17