deal.II version GIT relicensing-7182-g951947b1c4 2026-11-26 10:30:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
vector_tools_evaluate.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2021 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13
14#ifndef dealii_vector_tools_evaluation_h
15#define dealii_vector_tools_evaluation_h
16
17#include <deal.II/base/config.h>
18
20
22
24
26#include <deal.II/lac/vector.h>
27
29
31
32namespace VectorTools
33{
38 {
43 {
47 avg = 0,
53 max = 1,
59 min = 2,
63 insert = 3
64 };
65 } // namespace EvaluationFlags
66
137 template <int n_components,
138 template <int, int> class MeshType,
139 int dim,
140 int spacedim,
141 typename VectorType>
144 concepts::is_triangulation_or_dof_handler<MeshType<dim, spacedim>>))
145 std::vector<
146 typename FEPointEvaluation<n_components,
147 dim,
148 spacedim,
149 typename VectorType::value_type>::
150 value_type> point_values(const Mapping<dim> &mapping,
151 const MeshType<dim, spacedim> &mesh,
152 const VectorType &vector,
153 const std::vector<Point<spacedim>>
154 &evaluation_points,
155 Utilities::MPI::RemotePointEvaluation<dim,
156 spacedim>
157 &cache,
158 const EvaluationFlags::EvaluationFlags flags =
159 EvaluationFlags::avg,
160 const unsigned int first_selected_component = 0);
161
177 template <int n_components,
178 template <int, int> class MeshType,
179 int dim,
180 int spacedim,
181 typename VectorType>
183 (concepts::is_dealii_vector_type<VectorType> &&
184 concepts::is_triangulation_or_dof_handler<MeshType<dim, spacedim>>))
185 std::vector<
186 typename FEPointEvaluation<n_components,
187 dim,
188 spacedim,
189 typename VectorType::value_type>::
190 value_type> point_values(const Utilities::MPI::
191 RemotePointEvaluation<dim, spacedim> &cache,
192 const MeshType<dim, spacedim> &mesh,
193 const VectorType &vector,
194 const EvaluationFlags::EvaluationFlags flags =
195 EvaluationFlags::avg,
196 const unsigned int first_selected_component = 0);
197
211 template <int n_components,
212 template <int, int> class MeshType,
213 int dim,
214 int spacedim,
215 typename VectorType>
217 (concepts::is_dealii_vector_type<VectorType> &&
218 concepts::is_triangulation_or_dof_handler<MeshType<dim, spacedim>>))
219 std::vector<
220 typename FEPointEvaluation<n_components,
221 dim,
222 spacedim,
223 typename VectorType::value_type>::
224 gradient_type> point_gradients(const Mapping<dim> &mapping,
225 const MeshType<dim, spacedim> &mesh,
226 const VectorType &vector,
227 const std::vector<Point<spacedim>>
228 &evaluation_points,
229 Utilities::MPI::RemotePointEvaluation<
230 dim,
231 spacedim> &cache,
233 flags = EvaluationFlags::avg,
234 const unsigned int
235 first_selected_component = 0);
236
251 template <int n_components,
252 template <int, int> class MeshType,
253 int dim,
254 int spacedim,
255 typename VectorType>
257 (concepts::is_dealii_vector_type<VectorType> &&
258 concepts::is_triangulation_or_dof_handler<MeshType<dim, spacedim>>))
259 std::vector<
260 typename FEPointEvaluation<n_components,
261 dim,
262 spacedim,
263 typename VectorType::value_type>::
264 gradient_type> point_gradients(const Utilities::MPI::
265 RemotePointEvaluation<dim, spacedim>
266 &cache,
267 const MeshType<dim, spacedim> &mesh,
268 const VectorType &vector,
270 flags = EvaluationFlags::avg,
271 const unsigned int
272 first_selected_component = 0);
273
274
275
276 // inlined functions
277
278
279#ifndef DOXYGEN
280 template <int n_components,
281 template <int, int> class MeshType,
282 int dim,
283 int spacedim,
284 typename VectorType>
287 concepts::is_triangulation_or_dof_handler<MeshType<dim, spacedim>>))
288 inline std::vector<
289 typename FEPointEvaluation<n_components,
290 dim,
291 spacedim,
292 typename VectorType::value_type>::
293 value_type> point_values(const Mapping<dim> &mapping,
294 const MeshType<dim, spacedim> &mesh,
295 const VectorType &vector,
296 const std::vector<Point<spacedim>>
297 &evaluation_points,
298 Utilities::MPI::RemotePointEvaluation<dim,
299 spacedim>
300 &cache,
301 const EvaluationFlags::EvaluationFlags flags,
302 const unsigned int first_selected_component)
303 {
304 cache.reinit(evaluation_points, mesh.get_triangulation(), mapping);
305
306 return point_values<n_components>(
307 cache, mesh, vector, flags, first_selected_component);
308 }
309
310
311
312 template <int n_components,
313 template <int, int> class MeshType,
314 int dim,
315 int spacedim,
316 typename VectorType>
319 concepts::is_triangulation_or_dof_handler<MeshType<dim, spacedim>>))
320 inline std::vector<
321 typename FEPointEvaluation<n_components,
322 dim,
323 spacedim,
324 typename VectorType::value_type>::
325 gradient_type> point_gradients(const Mapping<dim> &mapping,
326 const MeshType<dim, spacedim> &mesh,
327 const VectorType &vector,
328 const std::vector<Point<spacedim>>
329 &evaluation_points,
330 Utilities::MPI::RemotePointEvaluation<
331 dim,
332 spacedim> &cache,
334 flags,
335 const unsigned int
336 first_selected_component)
337 {
338 cache.reinit(evaluation_points, mesh.get_triangulation(), mapping);
339
340 return point_gradients<n_components>(
341 cache, mesh, vector, flags, first_selected_component);
342 }
343
344
345
346 namespace internal
347 {
351 template <typename T>
352 T
354 const ArrayView<const T> &values)
355 {
356 switch (flags)
357 {
358 case EvaluationFlags::avg:
359 {
360 return std::accumulate(values.begin(), values.end(), T{}) /
361 (T(1.0) * values.size());
362 }
363 case EvaluationFlags::max:
364 return *std::max_element(values.begin(), values.end());
365 case EvaluationFlags::min:
366 return *std::min_element(values.begin(), values.end());
367 case EvaluationFlags::insert:
368 return values[0];
369 default:
371 return values[0];
372 }
373 }
374
375
376
380 template <int rank, int dim, typename Number>
383 const ArrayView<const Tensor<rank, dim, Number>> &values)
384 {
385 switch (flags)
386 {
387 case EvaluationFlags::avg:
388 {
389 return std::accumulate(values.begin(),
390 values.end(),
392 (Number(1.0) * values.size());
393 }
394 case EvaluationFlags::insert:
395 return values[0];
396 default:
398 return values[0];
399 }
400 }
401
402
403
408 template <int n_components, int rank, int dim, typename Number>
410 reduce(
412 const ArrayView<const Tensor<1, n_components, Tensor<rank, dim, Number>>>
413 &values)
414 {
415 switch (flags)
416 {
417 case EvaluationFlags::avg:
418 {
420
421 for (unsigned int j = 0; j < values.size(); ++j)
422 for (unsigned int i = 0; i < n_components; ++i)
423 temp[i] = temp[i] + values[j][i];
424
425 for (unsigned int i = 0; i < n_components; ++i)
426 temp[i] /= Number(values.size());
427
428 return temp;
429 }
430 case EvaluationFlags::insert:
431 return values[0];
432 default:
434 return values[0];
435 }
436 }
437
438
439
440 template <int n_components,
441 int dim,
442 int spacedim,
443 typename VectorType,
444 typename value_type>
445 void
446 process_cell(
447 const unsigned int i,
448 const typename Utilities::MPI::RemotePointEvaluation<dim,
449 spacedim>::CellData
450 &cell_data,
452 const DoFHandler<dim, spacedim> &dof_handler,
453 const VectorType &vector,
454 const UpdateFlags update_flags,
455 const ::EvaluationFlags::EvaluationFlags evaluation_flags,
456 const unsigned int first_selected_component,
457 const std::function<
458 value_type(const FEPointEvaluation<n_components,
459 dim,
460 spacedim,
461 typename VectorType::value_type> &,
462 const unsigned int &)> process_quadrature_point,
463 const ArrayView<value_type> &values,
464 std::vector<typename VectorType::value_type> &solution_values,
465 std::vector<
466 std::unique_ptr<FEPointEvaluation<n_components,
467 dim,
468 spacedim,
469 typename VectorType::value_type>>>
470 &evaluators)
471 {
472 if (evaluators.empty())
473 evaluators.resize(dof_handler.get_fe_collection().size());
474
476 &cache.get_triangulation(),
477 cell_data.cells[i].first,
478 cell_data.cells[i].second,
479 &dof_handler};
480
481 const ArrayView<const Point<dim>> unit_points(
482 cell_data.reference_point_values.data() +
483 cell_data.reference_point_ptrs[i],
484 cell_data.reference_point_ptrs[i + 1] -
485 cell_data.reference_point_ptrs[i]);
486
487 solution_values.resize(
488 dof_handler.get_fe(cell->active_fe_index()).n_dofs_per_cell());
489 cell->get_dof_values(vector,
490 solution_values.begin(),
491 solution_values.end());
492
493 if (evaluators[cell->active_fe_index()] == nullptr)
494 evaluators[cell->active_fe_index()] =
495 std::make_unique<FEPointEvaluation<n_components,
496 dim,
497 spacedim,
498 typename VectorType::value_type>>(
499 cache.get_mapping(),
500 cell->get_fe(),
501 update_flags,
502 first_selected_component);
503 auto &evaluator = *evaluators[cell->active_fe_index()];
504
505 evaluator.reinit(cell, unit_points);
506 evaluator.evaluate(solution_values, evaluation_flags);
507
508 for (unsigned int q = 0; q < unit_points.size(); ++q)
509 values[q + cell_data.reference_point_ptrs[i]] =
510 process_quadrature_point(evaluator, q);
511 }
512
513
514
515 template <int dim, int spacedim, typename Number>
516 Number
517 get_value(
519 const Vector<Number> &vector,
521 {
522 AssertDimension(tria.n_active_cells(), vector.size());
523 return vector[cell->active_cell_index()];
524 }
525
526
527
528 template <int dim, int spacedim, typename Number>
529 Number
530 get_value(
534 {
535 const auto distributed_tria =
536 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(&tria);
537
538 const bool use_distributed_path =
539 (distributed_tria == nullptr) ?
540 false :
541 (vector.get_partitioner().get() ==
542 distributed_tria->global_active_cell_index_partitioner()
543 .lock()
544 .get());
545
546 if (use_distributed_path)
547 {
548 return vector[cell->global_active_cell_index()];
549 }
550 else
551 {
553 return vector[cell->active_cell_index()];
554 }
555 }
556
557
558
559 template <typename Number, typename Number2>
560 void
561 set_value(Number &dst, const Number2 &src)
562 {
563 dst = src;
564 }
565
566
567
568 template <typename Number, int rank, int dim, typename Number2>
569 void
570 set_value(Tensor<rank, dim, Number> &, const Number2 &)
571 {
572 Assert(false,
574 "A cell-data vector can only have a single component."));
575 }
576
577
578
579 template <int n_components,
580 int dim,
581 int spacedim,
582 typename VectorType,
583 typename value_type>
584 void
585 process_cell(
586 const unsigned int i,
587 const typename Utilities::MPI::RemotePointEvaluation<dim,
588 spacedim>::CellData
589 &cell_data,
591 const Triangulation<dim, spacedim> &triangulation,
592 const VectorType &vector,
593 const UpdateFlags,
594 const ::EvaluationFlags::EvaluationFlags evaluation_flags,
595 const unsigned int first_selected_component,
596 const std::function<
597 value_type(const FEPointEvaluation<n_components,
598 dim,
599 spacedim,
600 typename VectorType::value_type> &,
601 const unsigned int &)>,
602 const ArrayView<value_type> &values,
603 std::vector<typename VectorType::value_type> &,
604 std::vector<
605 std::unique_ptr<FEPointEvaluation<n_components,
606 dim,
607 spacedim,
608 typename VectorType::value_type>>> &)
609 {
610 Assert(n_components == 1 && first_selected_component == 0,
612 "A cell-data vector can only have a single component."));
613
614 Assert(evaluation_flags ==
616 ExcMessage("For cell-data vectors, only values can be queried."));
617
619 &triangulation, cell_data.cells[i].first, cell_data.cells[i].second};
620
621 const auto value = get_value(triangulation, vector, cell);
622
623 for (unsigned int q = cell_data.reference_point_ptrs[i];
624 q < cell_data.reference_point_ptrs[i + 1];
625 ++q)
626 set_value(values[q], value);
627 }
628
629
630
631 template <int n_components,
632 int dim,
633 int spacedim,
634 typename MeshType,
635 typename VectorType,
636 typename value_type>
640 inline std::vector<value_type> evaluate_at_points(
642 const MeshType &mesh,
643 const VectorType &vector,
645 const unsigned int first_selected_component,
646 const UpdateFlags update_flags,
647 const ::EvaluationFlags::EvaluationFlags evaluation_flags,
648 const std::function<
649 value_type(const FEPointEvaluation<n_components,
650 dim,
651 spacedim,
652 typename VectorType::value_type> &,
653 const unsigned int &)> process_quadrature_point)
654 {
655 Assert(cache.is_ready(),
657 "Utilities::MPI::RemotePointEvaluation is not ready yet! "
658 "Please call Utilities::MPI::RemotePointEvaluation::reinit() "
659 "yourself or another function that does this for you."));
660
661 Assert(
662 &mesh.get_triangulation() == &cache.get_triangulation(),
664 "The provided Utilities::MPI::RemotePointEvaluation and DoFHandler "
665 "object have been set up with different Triangulation objects, "
666 "a scenario not supported!"));
667
668 // evaluate values at points if possible
669 const auto evaluation_point_results = [&]() {
670 // helper function for accessing the global vector and interpolating
671 // the results onto the points
672 const auto evaluation_function = [&](auto &values,
673 const auto &cell_data) {
674 std::vector<typename VectorType::value_type> solution_values;
675
676 std::vector<
677 std::unique_ptr<FEPointEvaluation<n_components,
678 dim,
679 spacedim,
680 typename VectorType::value_type>>>
681 evaluators;
682
683 for (unsigned int i = 0; i < cell_data.cells.size(); ++i)
684 process_cell<n_components, dim, spacedim, VectorType, value_type>(
685 i,
686 cell_data,
687 cache,
688 mesh,
689 vector,
690 update_flags,
691 evaluation_flags,
692 first_selected_component,
693 process_quadrature_point,
694 values,
695 solution_values,
696 evaluators);
697 };
698
699 std::vector<value_type> evaluation_point_results;
700 std::vector<value_type> buffer;
701
702 cache.template evaluate_and_process<value_type>(
703 evaluation_point_results, buffer, evaluation_function);
704
705 return evaluation_point_results;
706 }();
707
708 if (cache.is_map_unique())
709 {
710 // each point has exactly one result (unique map)
711 return evaluation_point_results;
712 }
713 else
714 {
715 // map is not unique (multiple or no results): postprocessing is
716 // needed
717 std::vector<value_type> unique_evaluation_point_results(
718 cache.get_point_ptrs().size() - 1);
719
720 const auto &ptr = cache.get_point_ptrs();
721
722 for (unsigned int i = 0; i < ptr.size() - 1; ++i)
723 {
724 const auto n_entries = ptr[i + 1] - ptr[i];
725 if (n_entries == 0)
726 continue;
727
728 unique_evaluation_point_results[i] =
729 reduce(flags,
731 evaluation_point_results.data() + ptr[i], n_entries));
732 }
733
734 return unique_evaluation_point_results;
735 }
736 }
737 } // namespace internal
738
739 template <int n_components,
740 template <int, int> class MeshType,
741 int dim,
742 int spacedim,
743 typename VectorType>
746 concepts::is_triangulation_or_dof_handler<MeshType<dim, spacedim>>))
747 inline std::vector<
748 typename FEPointEvaluation<n_components,
749 dim,
750 spacedim,
751 typename VectorType::value_type>::
753 RemotePointEvaluation<dim, spacedim> &cache,
754 const MeshType<dim, spacedim> &mesh,
755 const VectorType &vector,
756 const EvaluationFlags::EvaluationFlags flags,
757 const unsigned int first_selected_component)
758 {
759 return internal::evaluate_at_points<
760 n_components,
761 dim,
762 spacedim,
763 MeshType<dim, spacedim>,
765 typename FEPointEvaluation<n_components,
766 dim,
767 spacedim,
768 typename VectorType::value_type>::value_type>(
769 cache,
770 mesh,
771 vector,
772 flags,
773 first_selected_component,
776 [](const auto &evaluator, const auto &q) {
777 return evaluator.get_value(q);
778 });
779 }
780
781
782
783 template <int n_components,
784 template <int, int> class MeshType,
785 int dim,
786 int spacedim,
787 typename VectorType>
790 concepts::is_triangulation_or_dof_handler<MeshType<dim, spacedim>>))
791 inline std::vector<
792 typename FEPointEvaluation<n_components,
793 dim,
794 spacedim,
795 typename VectorType::value_type>::
796 gradient_type> point_gradients(const Utilities::MPI::
797 RemotePointEvaluation<dim, spacedim>
798 &cache,
799 const MeshType<dim, spacedim> &mesh,
800 const VectorType &vector,
802 flags,
803 const unsigned int
804 first_selected_component)
805 {
806 return internal::evaluate_at_points<
807 n_components,
808 dim,
809 spacedim,
810 MeshType<dim, spacedim>,
812 typename FEPointEvaluation<
813 n_components,
814 dim,
815 spacedim,
816 typename VectorType::value_type>::gradient_type>(
817 cache,
818 mesh,
819 vector,
820 flags,
821 first_selected_component,
824 [](const auto &evaluator, const unsigned &q) {
825 return evaluator.get_gradient(q);
826 });
827 }
828
829#endif
830} // namespace VectorTools
831
833
834#endif // dealii_vector_tools_boundary_h
const hp::FECollection< dim, spacedim > & get_fe_collection() const
const FiniteElement< dim, spacedim > & get_fe(const types::fe_index index=0) const
void reinit(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const ArrayView< const Point< dim > > &unit_points)
unsigned int n_dofs_per_cell() const
size_type locally_owned_size() const
Abstract base class for mapping classes.
Definition mapping.h:318
Definition point.h:111
unsigned int n_active_cells() const
Communicate values between a mesh and arbitrary points.
const Triangulation< dim, spacedim > & get_triangulation() const
const std::vector< unsigned int > & get_point_ptrs() const
const Mapping< dim, spacedim > & get_mapping() const
virtual size_type size() const override
unsigned int size() const
Definition collection.h:314
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_CXX20_REQUIRES(condition)
Definition config.h:249
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define DEAL_II_NOT_IMPLEMENTED()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcMessage(std::string arg1)
typename ActiveSelector::active_cell_iterator active_cell_iterator
UpdateFlags
@ update_values
Shape function values.
@ update_gradients
Shape function gradients.
The namespace for the EvaluationFlags enum.
EvaluationFlags
The EvaluationFlags enum.
constexpr char T
Tpetra::Vector< Number, LO, GO, NodeType< MemorySpace > > VectorType
*  *  *  *  ValueType TimeRateRequest< ValueType, dim, Number >  get_value() const
*  *  *  RotationFunction< dim, Number >::RotationFunction Number(dim)
*  *  *  *  void TimeRateRequest< ValueType, dim, Number >  set_value(const ValueType &value)
T reduce(const T &local_value, const MPI_Comm comm, const std::function< T(const T &, const T &)> &combiner, const unsigned int root_process=0)
std::vector< typename FEPointEvaluation< n_components, dim, spacedim, typename VectorType::value_type >::value_type > point_values(const Mapping< dim > &mapping, const MeshType< dim, spacedim > &mesh, const VectorType &vector, const std::vector< Point< spacedim > > &evaluation_points, Utilities::MPI::RemotePointEvaluation< dim, spacedim > &cache, const EvaluationFlags::EvaluationFlags flags=EvaluationFlags::avg, const unsigned int first_selected_component=0)
std::vector< typename FEPointEvaluation< n_components, dim, spacedim, typename VectorType::value_type >::gradient_type > point_gradients(const Mapping< dim > &mapping, const MeshType< dim, spacedim > &mesh, const VectorType &vector, const std::vector< Point< spacedim > > &evaluation_points, Utilities::MPI::RemotePointEvaluation< dim, spacedim > &cache, const EvaluationFlags::EvaluationFlags flags=EvaluationFlags::avg, const unsigned int first_selected_component=0)
STL namespace.