Debian Codegraph @

include/deal.II/numerics/vector_tools_point_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_point_value_templates_h
16 #define dealii_vector_tools_point_value_templates_h
17
18
19 #include <deal.II/fe/fe_values.h>
20 #include <deal.II/fe/mapping_q1.h>
21
22 #include <deal.II/grid/grid_tools.h>
23
24 #include <deal.II/hp/fe_values.h>
25
26 #include <deal.II/lac/block_vector.h>
27 #include <deal.II/lac/la_parallel_block_vector.h>
28 #include <deal.II/lac/la_parallel_vector.h>
29 #include <deal.II/lac/petsc_block_vector.h>
30 #include <deal.II/lac/petsc_vector.h>
31 #include <deal.II/lac/trilinos_parallel_block_vector.h>
32 #include <deal.II/lac/trilinos_tpetra_block_vector.h>
33 #include <deal.II/lac/trilinos_tpetra_vector.h>
34 #include <deal.II/lac/trilinos_vector.h>
35
36 #include <deal.II/numerics/vector_tools_common.h>
37 #include <deal.II/numerics/vector_tools_point_value.h>
38
39
40 DEAL_II_NAMESPACE_OPEN
41
42 namespace VectorTools
43 {
44 template <int dim, typename VectorType, int spacedim>
45 DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>)
46 void point_value(const DoFHandler<dim, spacedim> &dof,
47 const VectorType &fe_function,
48 const Point<spacedim> &point,
49 Vector<typename VectorType::value_type> &value)
50 {
51 if (dof.has_hp_capabilities() == false)
52 point_value(get_default_linear_mapping(dof.get_triangulation()),
53 dof,
54 fe_function,
55 point,
56 value);
57 else
58 point_value(hp::StaticMappingQ1<dim, spacedim>::mapping_collection,
59 dof,
60 fe_function,
61 point,
62 value);
63 }
64
65
66 template <int dim, typename VectorType, int spacedim>
67 DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>)
68 typename VectorType::value_type
69 point_value(const DoFHandler<dim, spacedim> &dof,
70 const VectorType &fe_function,
71 const Point<spacedim> &point)
72 {
73 if (dof.has_hp_capabilities() == false)
74 return point_value(get_default_linear_mapping(dof.get_triangulation()),
75 dof,
76 fe_function,
77 point);
78 else
79 return point_value(hp::StaticMappingQ1<dim, spacedim>::mapping_collection,
80 dof,
81 fe_function,
82 point);
83 }
84
85
86 template <int dim, typename VectorType, int spacedim>
87 DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>)
88 void point_value(const Mapping<dim, spacedim> &mapping,
89 const DoFHandler<dim, spacedim> &dof,
90 const VectorType &fe_function,
91 const Point<spacedim> &point,
92 Vector<typename VectorType::value_type> &value)
93 {
94 using Number = typename VectorType::value_type;
95 const FiniteElement<dim> &fe = dof.get_fe();
96
97 Assert(value.size() == fe.n_components(),
98 ExcDimensionMismatch(value.size(), fe.n_components()));
99
100 // first find the cell in which this point
101 // is, initialize a quadrature rule with
102 // it, and then a FEValues object
103 const std::pair<typename DoFHandler<dim, spacedim>::active_cell_iterator,
104 Point<spacedim>>
105 cell_point =
106 GridTools::find_active_cell_around_point(mapping, dof, point);
107
108 AssertThrow(cell_point.first.state() == IteratorState::valid &&
109 cell_point.first->is_locally_owned(),
110 ExcPointNotAvailableHere());
111 Assert(GeometryInfo<dim>::distance_to_unit_cell(cell_point.second) < 1e-10,
112 ExcInternalError());
113
114 const Quadrature<dim> quadrature(
115 cell_point.first->reference_cell().closest_point(cell_point.second));
116
117 FEValues<dim> fe_values(mapping, fe, quadrature, update_values);
118 fe_values.reinit(cell_point.first);
119
120 // then use this to get at the values of
121 // the given fe_function at this point
122 std::vector<Vector<Number>> u_value(1, Vector<Number>(fe.n_components()));
123 fe_values.get_function_values(fe_function, u_value);
124
125 value = u_value[0];
126 }
127
128
129 template <int dim, typename VectorType, int spacedim>
130 DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>)
131 void point_value(const hp::MappingCollection<dim, spacedim> &mapping,
132 const DoFHandler<dim, spacedim> &dof,
133 const VectorType &fe_function,
134 const Point<spacedim> &point,
135 Vector<typename VectorType::value_type> &value)
136 {
137 using Number = typename VectorType::value_type;
138 const hp::FECollection<dim, spacedim> &fe = dof.get_fe_collection();
139
140 Assert(value.size() == fe.n_components(),
141 ExcDimensionMismatch(value.size(), fe.n_components()));
142
143 // first find the cell in which this point
144 // is, initialize a quadrature rule with
145 // it, and then a FEValues object
146 const std::pair<typename DoFHandler<dim, spacedim>::active_cell_iterator,
147 Point<spacedim>>
148 cell_point =
149 GridTools::find_active_cell_around_point(mapping, dof, point);
150
151 AssertThrow(cell_point.first.state() == IteratorState::valid &&
152 cell_point.first->is_locally_owned(),
153 ExcPointNotAvailableHere());
154 Assert(GeometryInfo<dim>::distance_to_unit_cell(cell_point.second) < 1e-10,
155 ExcInternalError());
156
157 const Quadrature<dim> quadrature(
158 cell_point.first->reference_cell().closest_point(cell_point.second));
159 hp::FEValues<dim, spacedim> hp_fe_values(mapping,
160 fe,
161 hp::QCollection<dim>(quadrature),
162 update_values);
163 hp_fe_values.reinit(cell_point.first);
164 const FEValues<dim, spacedim> &fe_values =
165 hp_fe_values.get_present_fe_values();
166
167 // then use this to get at the values of
168 // the given fe_function at this point
169 std::vector<Vector<Number>> u_value(1, Vector<Number>(fe.n_components()));
170 fe_values.get_function_values(fe_function, u_value);
171
172 value = u_value[0];
173 }
174
175
176 template <int dim, typename VectorType, int spacedim>
177 DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>)
178 typename VectorType::value_type
179 point_value(const Mapping<dim, spacedim> &mapping,
180 const DoFHandler<dim, spacedim> &dof,
181 const VectorType &fe_function,
182 const Point<spacedim> &point)
183 {
184 Assert(dof.get_fe(0).n_components() == 1,
185 ExcMessage(
186 "Finite element is not scalar as is necessary for this function"));
187
188 Vector<typename VectorType::value_type> value(1);
189 point_value(mapping, dof, fe_function, point, value);
190
191 return value(0);
192 }
193
194
195 template <int dim, typename VectorType, int spacedim>
196 DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>)
197 typename VectorType::value_type
198 point_value(const hp::MappingCollection<dim, spacedim> &mapping,
199 const DoFHandler<dim, spacedim> &dof,
200 const VectorType &fe_function,
201 const Point<spacedim> &point)
202 {
203 Assert(dof.get_fe(0).n_components() == 1,
204 ExcMessage(
205 "Finite element is not scalar as is necessary for this function"));
206
207 Vector<typename VectorType::value_type> value(1);
208 point_value(mapping, dof, fe_function, point, value);
209
210 return value(0);
211 }
212
213
214 template <int dim, typename VectorType, int spacedim>
215 DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>)
216 void point_difference(
217 const DoFHandler<dim, spacedim> &dof,
218 const VectorType &fe_function,
219 const Function<spacedim, typename VectorType::value_type> &exact_function,
220 Vector<typename VectorType::value_type> &difference,
221 const Point<spacedim> &point)
222 {
223 point_difference(StaticMappingQ1<dim>::mapping,
224 dof,
225 fe_function,
226 exact_function,
227 difference,
228 point);
229 }
230
231
232 template <int dim, typename VectorType, int spacedim>
233 DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>)
234 void point_difference(
235 const Mapping<dim, spacedim> &mapping,
236 const DoFHandler<dim, spacedim> &dof,
237 const VectorType &fe_function,
238 const Function<spacedim, typename VectorType::value_type> &exact_function,
239 Vector<typename VectorType::value_type> &difference,
240 const Point<spacedim> &point)
241 {
242 using Number = typename VectorType::value_type;
243 const FiniteElement<dim> &fe = dof.get_fe();
244
245 Assert(difference.size() == fe.n_components(),
246 ExcDimensionMismatch(difference.size(), fe.n_components()));
247
248 // first find the cell in which this point
249 // is, initialize a quadrature rule with
250 // it, and then a FEValues object
251 const std::pair<typename DoFHandler<dim, spacedim>::active_cell_iterator,
252 Point<spacedim>>
253 cell_point =
254 GridTools::find_active_cell_around_point(mapping, dof, point);
255
256 AssertThrow(cell_point.first.state() == IteratorState::valid &&
257 cell_point.first->is_locally_owned(),
258 ExcPointNotAvailableHere());
259 Assert(GeometryInfo<dim>::distance_to_unit_cell(cell_point.second) < 1e-10,
260 ExcInternalError());
261
262 const Quadrature<dim> quadrature(
263 cell_point.first->reference_cell().closest_point(cell_point.second));
264 FEValues<dim> fe_values(mapping, fe, quadrature, update_values);
265 fe_values.reinit(cell_point.first);
266
267 // then use this to get at the values of
268 // the given fe_function at this point
269 std::vector<Vector<Number>> u_value(1, Vector<Number>(fe.n_components()));
270 fe_values.get_function_values(fe_function, u_value);
271
272 if (fe.n_components() == 1)
273 difference(0) = exact_function.value(point);
274 else
275 exact_function.vector_value(point, difference);
276
277 for (unsigned int i = 0; i < difference.size(); ++i)
278 difference(i) -= u_value[0](i);
279 }
280
281 template <int dim, int spacedim>
282 void
283 create_point_source_vector(const Mapping<dim, spacedim> &mapping,
284 const DoFHandler<dim, spacedim> &dof_handler,
285 const Point<spacedim> &p,
286 Vector<double> &rhs_vector)
287 {
288 Assert(rhs_vector.size() == dof_handler.n_dofs(),
289 ExcDimensionMismatch(rhs_vector.size(), dof_handler.n_dofs()));
290 Assert(dof_handler.get_fe(0).n_components() == 1,
291 ExcMessage("This function only works for scalar finite elements"));
292
293 rhs_vector = 0;
294
295 std::pair<typename DoFHandler<dim, spacedim>::active_cell_iterator,
296 Point<spacedim>>
297 cell_point =
298 GridTools::find_active_cell_around_point(mapping, dof_handler, p);
299
300 AssertThrow(cell_point.first.state() == IteratorState::valid,
301 ExcPointNotAvailableHere());
302
303 const Quadrature<dim> quadrature(
304 cell_point.first->reference_cell().closest_point(cell_point.second));
305
306 FEValues<dim, spacedim> fe_values(mapping,
307 dof_handler.get_fe(),
308 quadrature,
309 UpdateFlags(update_values));
310 fe_values.reinit(cell_point.first);
311
312 const unsigned int dofs_per_cell = dof_handler.get_fe().n_dofs_per_cell();
313
314 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
315 cell_point.first->get_dof_indices(local_dof_indices);
316
317 for (unsigned int i = 0; i < dofs_per_cell; ++i)
318 rhs_vector(local_dof_indices[i]) = fe_values.shape_value(i, 0);
319 }
320
321
322
323 template <int dim, int spacedim>
324 void
325 create_point_source_vector(const DoFHandler<dim, spacedim> &dof_handler,
326 const Point<spacedim> &p,
327 Vector<double> &rhs_vector)
328 {
329 if (dof_handler.has_hp_capabilities())
330 create_point_source_vector(
331 hp::StaticMappingQ1<dim, spacedim>::mapping_collection,
332 dof_handler,
333 p,
334 rhs_vector);
335 else
336 create_point_source_vector(get_default_linear_mapping(
337 dof_handler.get_triangulation()),
338 dof_handler,
339 p,
340 rhs_vector);
341 }
342
343
344 template <int dim, int spacedim>
345 void
346 create_point_source_vector(
347 const hp::MappingCollection<dim, spacedim> &mapping,
348 const DoFHandler<dim, spacedim> &dof_handler,
349 const Point<spacedim> &p,
350 Vector<double> &rhs_vector)
351 {
352 Assert(rhs_vector.size() == dof_handler.n_dofs(),
353 ExcDimensionMismatch(rhs_vector.size(), dof_handler.n_dofs()));
354 Assert(dof_handler.get_fe(0).n_components() == 1,
355 ExcMessage("This function only works for scalar finite elements"));
356
357 rhs_vector = 0;
358
359 std::pair<typename DoFHandler<dim, spacedim>::active_cell_iterator,
360 Point<spacedim>>
361 cell_point =
362 GridTools::find_active_cell_around_point(mapping, dof_handler, p);
363
364 AssertThrow(cell_point.first.state() == IteratorState::valid,
365 ExcPointNotAvailableHere());
366
367 const Quadrature<dim> quadrature(
368 cell_point.first->reference_cell().closest_point(cell_point.second));
369
370 FEValues<dim> fe_values(mapping[cell_point.first->active_fe_index()],
371 cell_point.first->get_fe(),
372 quadrature,
373 UpdateFlags(update_values));
374 fe_values.reinit(cell_point.first);
375
376 const unsigned int dofs_per_cell =
377 cell_point.first->get_fe().n_dofs_per_cell();
378
379 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
380 cell_point.first->get_dof_indices(local_dof_indices);
381
382 for (unsigned int i = 0; i < dofs_per_cell; ++i)
383 rhs_vector(local_dof_indices[i]) = fe_values.shape_value(i, 0);
384 }
385
386
387
388 template <int dim, int spacedim>
389 void
390 create_point_source_vector(const Mapping<dim, spacedim> &mapping,
391 const DoFHandler<dim, spacedim> &dof_handler,
392 const Point<spacedim> &p,
393 const Point<dim> &orientation,
394 Vector<double> &rhs_vector)
395 {
396 Assert(rhs_vector.size() == dof_handler.n_dofs(),
397 ExcDimensionMismatch(rhs_vector.size(), dof_handler.n_dofs()));
398 Assert(dof_handler.get_fe(0).n_components() == dim,
399 ExcMessage(
400 "This function only works for vector-valued finite elements."));
401
402 rhs_vector = 0;
403
404 const std::pair<typename DoFHandler<dim, spacedim>::active_cell_iterator,
405 Point<spacedim>>
406 cell_point =
407 GridTools::find_active_cell_around_point(mapping, dof_handler, p);
408
409 AssertThrow(cell_point.first.state() == IteratorState::valid,
410 ExcPointNotAvailableHere());
411
412 const Quadrature<dim> quadrature(
413 cell_point.first->reference_cell().closest_point(cell_point.second));
414
415 const FEValuesExtractors::Vector vec(0);
416 FEValues<dim, spacedim> fe_values(mapping,
417 dof_handler.get_fe(),
418 quadrature,
419 UpdateFlags(update_values));
420 fe_values.reinit(cell_point.first);
421
422 const unsigned int dofs_per_cell = dof_handler.get_fe().n_dofs_per_cell();
423
424 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
425 cell_point.first->get_dof_indices(local_dof_indices);
426
427 for (unsigned int i = 0; i < dofs_per_cell; ++i)
428 rhs_vector(local_dof_indices[i]) =
429 orientation * fe_values[vec].value(i, 0);
430 }
431
432
433
434 template <int dim, int spacedim>
435 void
436 create_point_source_vector(const DoFHandler<dim, spacedim> &dof_handler,
437 const Point<spacedim> &p,
438 const Point<dim> &orientation,
439 Vector<double> &rhs_vector)
440 {
441 if (dof_handler.has_hp_capabilities())
442 create_point_source_vector(
443 hp::StaticMappingQ1<dim, spacedim>::mapping_collection,
444 dof_handler,
445 p,
446 orientation,
447 rhs_vector);
448 else
449 create_point_source_vector(get_default_linear_mapping(
450 dof_handler.get_triangulation()),
451 dof_handler,
452 p,
453 orientation,
454 rhs_vector);
455 }
456
457
458 template <int dim, int spacedim>
459 void
460 create_point_source_vector(
461 const hp::MappingCollection<dim, spacedim> &mapping,
462 const DoFHandler<dim, spacedim> &dof_handler,
463 const Point<spacedim> &p,
464 const Point<dim> &orientation,
465 Vector<double> &rhs_vector)
466 {
467 Assert(rhs_vector.size() == dof_handler.n_dofs(),
468 ExcDimensionMismatch(rhs_vector.size(), dof_handler.n_dofs()));
469 Assert(dof_handler.get_fe(0).n_components() == dim,
470 ExcMessage(
471 "This function only works for vector-valued finite elements."));
472
473 rhs_vector = 0;
474
475 std::pair<typename DoFHandler<dim, spacedim>::active_cell_iterator,
476 Point<spacedim>>
477 cell_point =
478 GridTools::find_active_cell_around_point(mapping, dof_handler, p);
479
480 AssertThrow(cell_point.first.state() == IteratorState::valid,
481 ExcPointNotAvailableHere());
482
483 const Quadrature<dim> quadrature(
484 cell_point.first->reference_cell().closest_point(cell_point.second));
485
486 const FEValuesExtractors::Vector vec(0);
487 FEValues<dim> fe_values(mapping[cell_point.first->active_fe_index()],
488 cell_point.first->get_fe(),
489 quadrature,
490 UpdateFlags(update_values));
491 fe_values.reinit(cell_point.first);
492
493 const unsigned int dofs_per_cell =
494 cell_point.first->get_fe().n_dofs_per_cell();
495
496 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
497 cell_point.first->get_dof_indices(local_dof_indices);
498
499 for (unsigned int i = 0; i < dofs_per_cell; ++i)
500 rhs_vector(local_dof_indices[i]) =
501 orientation * fe_values[vec].value(i, 0);
502 }
503 } // namespace VectorTools
504
505 DEAL_II_NAMESPACE_CLOSE
506
507 #endif // dealii_vector_tools_point_value_templates_h