Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
solid_constraints.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
5
6#include <deal.II/base/exceptions.h>
7#include <deal.II/base/index_set.h>
8
9#include <type_traits>
10#include <unordered_set>
11
12using namespace dealii;
13
14namespace
15{
16 /**
17 * @brief Flag DOFs connected to fluid cells.
18 */
19 template <int dim>
20 void
21 flag_dofs_connected_to_fluid(
22 const FiniteElement<dim> &fe,
23 const std::vector<types::global_dof_index> &local_dof_indices,
24 std::unordered_set<types::global_dof_index> &dofs_are_connected_to_fluid)
25 {
26 Assert(fe.n_components() == dim + 1,
27 ExcMessage("The finite element passed to "
28 "flag_dofs_connected_to_fluid must have dim+1 "
29 "components (velocity + pressure). This routine is "
30 "intended for fluid dynamics problems only."));
31 Assert(local_dof_indices.size() == fe.dofs_per_cell,
32 ExcDimensionMismatch(local_dof_indices.size(), fe.dofs_per_cell));
33
34 for (unsigned int i = 0; i < local_dof_indices.size(); ++i)
35 {
36 const int component = fe.system_to_component_index(i).first;
37 if (component == dim)
38 {
39 dofs_are_connected_to_fluid.insert(local_dof_indices[i]);
40 }
41 }
42 }
43
44 /**
45 * @brief Check if the cell is connected to a fluid cell.
46 */
47 bool
48 check_cell_is_connected_to_fluid(
49 const std::unordered_set<types::global_dof_index>
50 &dofs_are_connected_to_fluid,
51 const std::vector<types::global_dof_index> &local_dof_indices)
52 {
53 for (const auto &dof_index : local_dof_indices)
54 {
55 auto search = dofs_are_connected_to_fluid.find(dof_index);
56 if (search != dofs_are_connected_to_fluid.end())
57 return true;
58 }
59 return false;
60 }
61
62 /**
63 * @brief Constrain pressure DOFs if cells are not connected to fluid and
64 * DOFs are locally owned.
65 */
66 template <int dim, typename DofsType>
67 void
68 constrain_pressure(
69 const FiniteElement<dim> &fe,
70 const bool &non_zero_constraints,
71 const DofsType &locally_owned_dofs,
72 const std::vector<types::global_dof_index> &local_dof_indices,
73 AffineConstraints<double> &constraints)
74 {
75 Assert(fe.n_components() == dim + 1,
76 ExcMessage("The finite element passed to constrain_pressure must "
77 "have dim+1 components (velocity + pressure). This "
78 "routine is intended for fluid dynamics problems only."));
79 Assert(local_dof_indices.size() == fe.dofs_per_cell,
80 ExcDimensionMismatch(local_dof_indices.size(), fe.dofs_per_cell));
81 static_assert(std::is_same_v<DofsType, IndexSet> ||
82 std::is_same_v<DofsType, std::vector<IndexSet>>,
83 "constrain_pressure only supports IndexSet (GLS family) or "
84 "std::vector<IndexSet> (GD family) for the locally owned "
85 "DOFs container.");
86 if constexpr (std::is_same_v<DofsType, std::vector<IndexSet>>)
87 {
88 Assert(locally_owned_dofs.size() == 2,
89 ExcMessage("For block (GD-family) solvers, the locally owned "
90 "DOFs container must contain exactly two index "
91 "sets: velocities (block 0) and pressure "
92 "(block 1)."));
93 }
94
95 for (unsigned int i = 0; i < local_dof_indices.size(); ++i)
96 {
97 const int component = fe.system_to_component_index(i).first;
98
99 // Only pressure DOFs have an additional Dirichlet condition
100 if (component == dim) // pressure DOFs
101 {
102 // We only apply the constraint on the locally owned pressure DOFs
103 // since we have no way of verifying if the locally relevant DOFs
104 // are connected to a fluid cell.
105 bool dof_is_locally_owned = false;
106
107 // For the GLS-family of solvers, we only have a single index set
108 if constexpr (std::is_same_v<DofsType, IndexSet>)
109 {
110 dof_is_locally_owned =
111 locally_owned_dofs.is_element(local_dof_indices[i]);
112 }
113
114 // For the GD-family of solvers, we have two index sets. One for
115 // velocities and one for pressure.
116 if constexpr (std::is_same_v<DofsType, std::vector<IndexSet>>)
117 {
118 dof_is_locally_owned =
119 locally_owned_dofs[1].is_element(local_dof_indices[i]);
120 }
121
122 if (dof_is_locally_owned)
123 {
124 if (non_zero_constraints)
125 {
126 constraints.add_line(local_dof_indices[i]);
127 constraints.set_inhomogeneity(local_dof_indices[i], 0);
128 }
129 else
130 {
131 constraints.add_line(local_dof_indices[i]);
132 }
133 }
134 }
135 }
136 }
137} // namespace
138
139template <int dim>
140void
142 const FiniteElement<dim> &fe,
143 const std::vector<types::global_dof_index> &local_dof_indices,
144 const bool &non_zero_constraints,
145 AffineConstraints<double> &constraints)
146{
147 Assert(fe.n_components() == dim + 1,
148 ExcMessage("The finite element passed to "
149 "constrain_solid_cell_velocity_dofs must have dim+1 "
150 "components (velocity + pressure). This routine is "
151 "intended for fluid dynamics problems only."));
152 Assert(local_dof_indices.size() == fe.dofs_per_cell,
153 ExcDimensionMismatch(local_dof_indices.size(), fe.dofs_per_cell));
154
155 for (unsigned int i = 0; i < local_dof_indices.size(); ++i)
156 {
157 const int component = fe.system_to_component_index(i).first;
158 if (component < dim) // velocity DOFs
159 {
160 // We apply a constraint to all DOFs in the solid region, whether
161 // they are locally owned or not.
162 if (non_zero_constraints)
163 {
164 constraints.add_line(local_dof_indices[i]);
165 constraints.set_inhomogeneity(local_dof_indices[i], 0);
166 }
167 else
168 constraints.add_line(local_dof_indices[i]);
169 }
170 }
171}
172
173template <int dim, typename DofsType>
174void
175establish_solid_domain(const DoFHandler<dim> &dof_handler,
176 const DofsType &locally_owned_dofs,
177 const bool non_zero_constraints,
178 AffineConstraints<double> &constraints)
179{
180 Assert(dof_handler.get_fe().n_components() == dim + 1,
181 ExcMessage("The DoFHandler passed to establish_solid_domain must "
182 "use a finite element with dim+1 components (velocity + "
183 "pressure). This routine is intended for fluid dynamics "
184 "problems only."));
185
186 const unsigned int dofs_per_cell = dof_handler.get_fe().dofs_per_cell;
187 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
188
189 // We will need to identify which pressure degrees of freedom are connected to
190 // fluid region. For these, we won't establish a zero pressure constraint.
191 std::unordered_set<types::global_dof_index> dofs_are_connected_to_fluid;
192
193 // Loop through all cells to identify which cells are solid. This first step
194 // is used to 1) constraint the velocity degree of freedom to be zero in the
195 // solid region and 2) to identify which pressure degrees of freedom are
196 // connected to fluid cells
197 for (const auto &cell : dof_handler.active_cell_iterators())
198 {
199 if (cell->is_locally_owned() || cell->is_ghost())
200 {
201 cell->get_dof_indices(local_dof_indices);
202 // If the material_id is higher than 0, the region is a solid region.
203 // Constrain the velocity DOFs to be zero.
204 if (cell->material_id() > 0)
205 {
206 constrain_solid_cell_velocity_dofs(dof_handler.get_fe(),
207 local_dof_indices,
208 non_zero_constraints,
209 constraints);
210 }
211 else
212 {
213 // Cell is a fluid cell and as such all the pressure DOFs of that
214 // cell are connected to the fluid. This will be used later on to
215 // identify which pressure cells to constrain.
216 flag_dofs_connected_to_fluid(dof_handler.get_fe(),
217 local_dof_indices,
218 dofs_are_connected_to_fluid);
219 }
220 }
221 }
222
223 // All pressure DOFs that are not connected to a fluid cell are constrained
224 // to ensure that the system matrix has adequate conditioning.
225 for (const auto &cell : dof_handler.active_cell_iterators())
226 {
227 if (cell->is_locally_owned() || cell->is_ghost())
228 {
229 cell->get_dof_indices(local_dof_indices);
230 // If the material_id is > 0, the region is a solid region
231 if (cell->material_id() > 0)
232 {
233 // First check if the cell is connected to a fluid cell by
234 // checking if one of the DOF of the cell is connected to a fluid
235 // cell.
236 bool connected_to_fluid =
237 check_cell_is_connected_to_fluid(dofs_are_connected_to_fluid,
238 local_dof_indices);
239
240 // All the pressure DOFs with the cell are not connected to the
241 // fluid. Consequently, we fix a constraint on these pressure
242 // DOFs.
243 if (!connected_to_fluid)
244 {
245 constrain_pressure(dof_handler.get_fe(),
246 non_zero_constraints,
247 locally_owned_dofs,
248 local_dof_indices,
249 constraints);
250 }
251 }
252 }
253 }
254}
255
256template <int dim>
257void
258establish_solid_domain_lsmg(const DoFHandler<dim> &dof_handler,
259 const unsigned int level,
260 const IndexSet &locally_owned_mg_dofs,
261 const bool non_zero_constraints,
262 AffineConstraints<double> &constraints)
263{
264 Assert(dof_handler.get_fe().n_components() == dim + 1,
265 ExcMessage("The DoFHandler passed to establish_solid_domain_lsmg "
266 "must use a finite element with dim+1 components "
267 "(velocity + pressure). This routine is intended for "
268 "fluid dynamics problems only."));
269
270 const unsigned int dofs_per_cell = dof_handler.get_fe().dofs_per_cell;
271 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
272
273 // Pressure DOFs (in MG numbering on this level) that touch a fluid cell.
274 std::unordered_set<types::global_dof_index> dofs_are_connected_to_fluid;
275
276 // First pass: classify cells on this level. We must include both
277 // owned-on-level and ghost-on-level cells so that the connectivity
278 // information for pressure DOFs at the fluid/solid interface is complete.
279 for (const auto &cell : dof_handler.mg_cell_iterators_on_level(level))
280 {
281 if (cell->level_subdomain_id() == numbers::artificial_subdomain_id)
282 continue;
283
284 cell->get_mg_dof_indices(local_dof_indices);
285
286 if (cell->material_id() > 0)
287 {
288 constrain_solid_cell_velocity_dofs(dof_handler.get_fe(),
289 local_dof_indices,
290 non_zero_constraints,
291 constraints);
292 }
293 else
294 {
295 flag_dofs_connected_to_fluid(dof_handler.get_fe(),
296 local_dof_indices,
297 dofs_are_connected_to_fluid);
298 }
299 }
300
301 // Second pass: for solid cells fully disconnected from the fluid, pin the
302 // pressure to keep the system well posed.
303 for (const auto &cell : dof_handler.mg_cell_iterators_on_level(level))
304 {
305 if (cell->level_subdomain_id() == numbers::artificial_subdomain_id)
306 continue;
307
308 if (cell->material_id() > 0)
309 {
310 cell->get_mg_dof_indices(local_dof_indices);
311
312 const bool connected_to_fluid =
313 check_cell_is_connected_to_fluid(dofs_are_connected_to_fluid,
314 local_dof_indices);
315
316 if (!connected_to_fluid)
317 {
318 constrain_pressure(dof_handler.get_fe(),
319 non_zero_constraints,
320 locally_owned_mg_dofs,
321 local_dof_indices,
322 constraints);
323 }
324 }
325 }
326}
327
328// Explicit template instantiations
329template void
331 const FiniteElement<2> &fe,
332 const std::vector<types::global_dof_index> &local_dof_indices,
333 const bool &non_zero_constraints,
334 AffineConstraints<double> &constraints);
335template void
337 const FiniteElement<3> &fe,
338 const std::vector<types::global_dof_index> &local_dof_indices,
339 const bool &non_zero_constraints,
340 AffineConstraints<double> &constraints);
341
342template void
343establish_solid_domain<2, IndexSet>(const DoFHandler<2> &dof_handler,
344 const IndexSet &locally_owned_dofs,
345 const bool non_zero_constraints,
346 AffineConstraints<double> &constraints);
347template void
348establish_solid_domain<3, IndexSet>(const DoFHandler<3> &dof_handler,
349 const IndexSet &locally_owned_dofs,
350 const bool non_zero_constraints,
351 AffineConstraints<double> &constraints);
352template void
354 const DoFHandler<2> &dof_handler,
355 const std::vector<IndexSet> &locally_owned_dofs,
356 const bool non_zero_constraints,
357 AffineConstraints<double> &constraints);
358template void
359establish_solid_domain_lsmg<2>(const DoFHandler<2> &dof_handler,
360 const unsigned int level,
361 const IndexSet &locally_owned_mg_dofs,
362 const bool non_zero_constraints,
363 AffineConstraints<double> &constraints);
364template void
365establish_solid_domain_lsmg<3>(const DoFHandler<3> &dof_handler,
366 const unsigned int level,
367 const IndexSet &locally_owned_mg_dofs,
368 const bool non_zero_constraints,
369 AffineConstraints<double> &constraints);
370
371template void
373 const DoFHandler<3> &dof_handler,
374 const std::vector<IndexSet> &locally_owned_dofs,
375 const bool non_zero_constraints,
376 AffineConstraints<double> &constraints);
void constrain_solid_cell_velocity_dofs(const FiniteElement< dim > &fe, const std::vector< types::global_dof_index > &local_dof_indices, const bool &non_zero_constraints, AffineConstraints< double > &constraints)
template void constrain_solid_cell_velocity_dofs< 3 >(const FiniteElement< 3 > &fe, const std::vector< types::global_dof_index > &local_dof_indices, const bool &non_zero_constraints, AffineConstraints< double > &constraints)
template void constrain_solid_cell_velocity_dofs< 2 >(const FiniteElement< 2 > &fe, const std::vector< types::global_dof_index > &local_dof_indices, const bool &non_zero_constraints, AffineConstraints< double > &constraints)
void establish_solid_domain_lsmg(const DoFHandler< dim > &dof_handler, const unsigned int level, const IndexSet &locally_owned_mg_dofs, const bool non_zero_constraints, AffineConstraints< double > &constraints)
template void establish_solid_domain_lsmg< 2 >(const DoFHandler< 2 > &dof_handler, const unsigned int level, const IndexSet &locally_owned_mg_dofs, const bool non_zero_constraints, AffineConstraints< double > &constraints)
template void establish_solid_domain< 3, IndexSet >(const DoFHandler< 3 > &dof_handler, const IndexSet &locally_owned_dofs, const bool non_zero_constraints, AffineConstraints< double > &constraints)
template void establish_solid_domain< 2, IndexSet >(const DoFHandler< 2 > &dof_handler, const IndexSet &locally_owned_dofs, const bool non_zero_constraints, AffineConstraints< double > &constraints)
template void establish_solid_domain_lsmg< 3 >(const DoFHandler< 3 > &dof_handler, const unsigned int level, const IndexSet &locally_owned_mg_dofs, const bool non_zero_constraints, AffineConstraints< double > &constraints)
void establish_solid_domain(const DoFHandler< dim > &dof_handler, const DofsType &locally_owned_dofs, const bool non_zero_constraints, AffineConstraints< double > &constraints)
void establish_solid_domain(const dealii::DoFHandler< dim > &dof_handler, const DofsType &locally_owned_dofs, const bool non_zero_constraints, dealii::AffineConstraints< double > &constraints)
Turn regions of the mesh where the material_id>0 into a solid block by injecting velocity and pressur...