Debian Codegraph @

include/deal.II/numerics/vector_tools_mean_value.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2020 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_vector_tools_mean_value_templates_h
16 #define dealii_vector_tools_mean_value_templates_h
17
18 #include <deal.II/distributed/tria_base.h>
19
20 #include <deal.II/fe/fe_dgp.h>
21 #include <deal.II/fe/fe_dgq.h>
22 #include <deal.II/fe/fe_q.h>
23 #include <deal.II/fe/fe_simplex_p.h>
24 #include <deal.II/fe/fe_values.h>
25
26 #include <deal.II/grid/filtered_iterator.h>
27
28 #include <deal.II/hp/fe_values.h>
29 #include <deal.II/hp/mapping_collection.h>
30 #include <deal.II/hp/q_collection.h>
31
32 #include <deal.II/lac/block_vector.h>
33 #include <deal.II/lac/la_parallel_block_vector.h>
34 #include <deal.II/lac/la_parallel_vector.h>
35 #include <deal.II/lac/petsc_block_vector.h>
36 #include <deal.II/lac/petsc_vector.h>
37 #include <deal.II/lac/trilinos_parallel_block_vector.h>
38 #include <deal.II/lac/trilinos_vector.h>
39
40 #include <deal.II/numerics/vector_tools_mean_value.h>
41
42
43 DEAL_II_NAMESPACE_OPEN
44
45 namespace VectorTools
46 {
47 namespace internal
48 {
49 template <typename VectorType>
50 std::enable_if_t<dealii::is_serial_vector<VectorType>::value == true>
51 subtract_mean_value(VectorType &v, const std::vector<bool> &p_select)
52 {
53 if (p_select.empty())
54 {
55 // In case of an empty boolean mask operate on the whole vector:
56 v.add(-v.mean_value());
57 }
58 else
59 {
60 const unsigned int n = v.size();
61
62 Assert(p_select.size() == n,
63 ExcDimensionMismatch(p_select.size(), n));
64
65 typename VectorType::value_type s = 0.;
66 unsigned int counter = 0;
67 for (unsigned int i = 0; i < n; ++i)
68 if (p_select[i])
69 {
70 typename VectorType::value_type vi = v(i);
71 s += vi;
72 ++counter;
73 }
74 // Error out if we have not constrained anything. Note that in this
75 // case the vector v is always nonempty.
76 Assert(n == 0 || counter > 0,
77 ComponentMask::ExcNoComponentSelected());
78
79 s /= counter;
80
81 for (unsigned int i = 0; i < n; ++i)
82 if (p_select[i])
83 v(i) -= s;
84 }
85 }
86
87
88
89 template <typename VectorType>
90 std::enable_if_t<dealii::is_serial_vector<VectorType>::value == false>
91 subtract_mean_value(VectorType &v, const std::vector<bool> &p_select)
92 {
93 Assert(p_select.empty(), ExcNotImplemented());
94 // In case of an empty boolean mask operate on the whole vector:
95 v.add(-v.mean_value());
96 }
97 } // namespace internal
98
99
100 template <typename VectorType>
101 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
102 void subtract_mean_value(VectorType &v, const std::vector<bool> &p_select)
103 {
104 internal::subtract_mean_value(v, p_select);
105 }
106
107 namespace internal
108 {
109 template <typename Number>
110 void
111 set_possibly_complex_number(const double r, const double, Number &n)
112 {
113 n = r;
114 }
115
116
117
118 template <typename Type>
119 void
120 set_possibly_complex_number(const double r,
121 const double i,
122 std::complex<Type> &n)
123 {
124 n = std::complex<Type>(r, i);
125 }
126 } // namespace internal
127
128
129
130 template <typename VectorType, int dim, int spacedim>
131 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
132 void add_constant(VectorType &solution,
133 const DoFHandler<dim, spacedim> &dof_handler,
134 const unsigned int component,
135 const typename VectorType::value_type constant_adjustment)
136 {
137 Assert(dof_handler.has_hp_capabilities() == false, ExcNotImplemented());
138
139 AssertDimension(solution.size(), dof_handler.n_dofs());
140 AssertIndexRange(component, dof_handler.get_fe().n_components());
141
142 const FiniteElement<dim, spacedim> &fe_system = dof_handler.get_fe();
143 const FiniteElement<dim, spacedim> &fe = fe_system.get_sub_fe(component, 1);
144
145 if ((dynamic_cast<const FE_DGP<dim, spacedim> *>(&fe) != nullptr))
146 {
147 // The FE to modify is an FE_DGP, which is not a nodal
148 // element. The first shape function of a DGP element happens
149 // to be the constant function, so we just have to adjust the
150 // corresponding DoF on each cell:
151 std::vector<types::global_dof_index> local_dof_indices(
152 dof_handler.get_fe().dofs_per_cell);
153
154 for (const auto &cell : dof_handler.active_cell_iterators())
155 if (cell->is_locally_owned())
156 {
157 cell->get_dof_indices(local_dof_indices);
158 const unsigned int first_pressure_dof =
159 fe_system.component_to_system_index(component, 0);
160
161 // Make sure that this DoF is really owned by the
162 // current processor:
163 Assert(dof_handler.locally_owned_dofs().is_element(
164 local_dof_indices[first_pressure_dof]),
165 ExcInternalError());
166
167 // Then adjust its value:
168 solution(local_dof_indices[first_pressure_dof]) +=
169 constant_adjustment;
170 }
171
172 solution.compress(VectorOperation::add);
173 }
174 else if ((dynamic_cast<const FE_Q<dim, spacedim> *>(&fe) != nullptr) ||
175 (dynamic_cast<const FE_SimplexP<dim, spacedim> *>(&fe) != nullptr))
176 {
177 // We need to make sure to not touch DoFs shared between cells more
178 // than once. Instead of counting or limiting the number of times
179 // we touch an individual vector entry, we make a copy of the vector
180 // and use that as the input and add the constant to it. This way
181 // it does not matter how often an individual entry is touched.
182
183 VectorType copy(solution);
184 copy = solution;
185
186 std::vector<types::global_dof_index> local_dof_indices(
187 dof_handler.get_fe().dofs_per_cell);
188
189 for (const auto &cell : dof_handler.active_cell_iterators())
190 if (cell->is_locally_owned())
191 {
192 cell->get_dof_indices(local_dof_indices);
193 for (unsigned i = 0; i < dof_handler.get_fe().dofs_per_cell; ++i)
194 {
195 if (!fe_system.is_primitive(i))
196 continue;
197
198 const auto component_and_index =
199 fe_system.system_to_component_index(i);
200 if (component_and_index.first == component)
201 {
202 const types::global_dof_index idx = local_dof_indices[i];
203 // Make sure that this DoF is really owned by the
204 // current processor:
205 if (!dof_handler.locally_owned_dofs().is_element(idx))
206 continue;
207
208 // Then adjust its value:
209 solution(idx) =
210 typename VectorType::value_type(copy(idx)) +
211 constant_adjustment;
212 }
213 }
214 }
215
216 solution.compress(VectorOperation::insert);
217 }
218 else if ((dynamic_cast<const FE_DGQ<dim, spacedim> *>(&fe) != nullptr) ||
219 (dynamic_cast<const FE_SimplexDGP<dim, spacedim> *>(&fe) !=
220 nullptr))
221 {
222 // Add the constant to every single shape function per cell
223
224 std::vector<types::global_dof_index> local_dof_indices(
225 dof_handler.get_fe().dofs_per_cell);
226
227 for (const auto &cell : dof_handler.active_cell_iterators())
228 if (cell->is_locally_owned())
229 {
230 cell->get_dof_indices(local_dof_indices);
231 for (unsigned i = 0; i < dof_handler.get_fe().dofs_per_cell; ++i)
232 {
233 if (!fe_system.is_primitive(i))
234 continue;
235
236 const auto component_and_index =
237 fe_system.system_to_component_index(i);
238 if (component_and_index.first == component)
239 {
240 // Make sure that this DoF is really owned by the
241 // current processor:
242 Assert(dof_handler.locally_owned_dofs().is_element(
243 local_dof_indices[i]),
244 ExcInternalError());
245
246 // Then adjust its value:
247 solution(local_dof_indices[i]) += constant_adjustment;
248 }
249 }
250 }
251
252 solution.compress(VectorOperation::add);
253 }
254 else
255 AssertThrow(false, ExcNotImplemented());
256 }
257
258
259
260 #ifdef DEAL_II_WITH_TRILINOS
261 template <int dim, int spacedim>
262 void
263 add_constant(LinearAlgebra::EpetraWrappers::Vector &,
264 const DoFHandler<dim, spacedim> &,
265 const unsigned int,
266 const double)
267 {
268 // TODO: no vector access using operator()
269 AssertThrow(false, ExcNotImplemented());
270 }
271
272
273
274 # ifdef DEAL_II_TRILINOS_WITH_TPETRA
275 template <int dim, int spacedim, typename ValueType, typename MemorySpace>
276 void
277 add_constant(LinearAlgebra::TpetraWrappers::Vector<ValueType, MemorySpace> &,
278 const DoFHandler<dim, spacedim> &,
279 const unsigned int,
280 const ValueType)
281 {
282 // TODO: no vector access using operator()
283 AssertThrow(false, ExcNotImplemented());
284 }
285 # endif
286 #endif
287
288
289
290 template <int dim, typename Number, int spacedim>
291 Number
292 compute_mean_value(
293 const hp::MappingCollection<dim, spacedim> &mapping_collection,
294 const DoFHandler<dim, spacedim> &dof,
295 const hp::QCollection<dim> &q_collection,
296 const ReadVector<Number> &v,
297 const unsigned int component)
298 {
299 const hp::FECollection<dim, spacedim> &fe_collection =
300 dof.get_fe_collection();
301 const unsigned int n_components = fe_collection.n_components();
302
303 AssertDimension(v.size(), dof.n_dofs());
304 AssertIndexRange(component, n_components);
305
306 hp::FEValues<dim, spacedim> fe_values_collection(
307 mapping_collection,
308 fe_collection,
309 q_collection,
310 UpdateFlags(update_JxW_values | update_values));
311
312 std::vector<Vector<Number>> values;
313
314 Number mean = Number();
315 typename numbers::NumberTraits<Number>::real_type area = 0.;
316 // Compute mean value
317 for (const auto &cell :
318 dof.active_cell_iterators() | IteratorFilters::LocallyOwnedCell())
319 {
320 fe_values_collection.reinit(cell);
321 const FEValues<dim, spacedim> &fe_values =
322 fe_values_collection.get_present_fe_values();
323
324 values.resize(fe_values.n_quadrature_points,
325 Vector<Number>(n_components));
326 fe_values.get_function_values(v, values);
327 for (unsigned int k = 0; k < fe_values.n_quadrature_points; ++k)
328 {
329 mean += fe_values.JxW(k) * values[k](component);
330 area += fe_values.JxW(k);
331 }
332 }
333
334 #ifdef DEAL_II_WITH_MPI
335 // if this was a distributed DoFHandler, we need to do the reduction
336 // over the entire domain
337 if (const parallel::TriangulationBase<dim, spacedim> *p_triangulation =
338 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
339 &dof.get_triangulation()))
340 {
341 // The type used to store the elements of the global vector may be a
342 // real or a complex number. Do the global reduction always with real
343 // and imaginary types so that we don't have to distinguish, and to
344 // this end just copy everything into a complex number and, later,
345 // back into the original data type.
346 std::complex<double> mean_double = mean;
347 double my_values[3] = {mean_double.real(), mean_double.imag(), area};
348 double global_values[3];
349
350 const int ierr = MPI_Allreduce(my_values,
351 global_values,
352 3,
353 MPI_DOUBLE,
354 MPI_SUM,
355 p_triangulation->get_mpi_communicator());
356 AssertThrowMPI(ierr);
357
358 internal::set_possibly_complex_number(global_values[0],
359 global_values[1],
360 mean);
361 area = global_values[2];
362 }
363 #endif
364
365 return (mean / area);
366 }
367
368
369 template <int dim, typename Number, int spacedim>
370 Number
371 compute_mean_value(const Mapping<dim, spacedim> &mapping,
372 const DoFHandler<dim, spacedim> &dof,
373 const Quadrature<dim> &quadrature,
374 const ReadVector<Number> &v,
375 const unsigned int component)
376 {
377 return compute_mean_value(hp::MappingCollection<dim, spacedim>(mapping),
378 dof,
379 hp::QCollection<dim>(quadrature),
380 v,
381 component);
382 }
383
384
385 template <int dim, typename Number, int spacedim>
386 Number
387 compute_mean_value(const DoFHandler<dim, spacedim> &dof,
388 const Quadrature<dim> &quadrature,
389 const ReadVector<Number> &v,
390 const unsigned int component)
391 {
392 return compute_mean_value(get_default_linear_mapping(
393 dof.get_triangulation()),
394 dof,
395 quadrature,
396 v,
397 component);
398 }
399 } // namespace VectorTools
400
401 DEAL_II_NAMESPACE_CLOSE
402
403 #endif // dealii_vector_tools_mean_value_templates_h