deal.II version GIT relicensing-2167-g9622207b8f 2024-11-21 12:40:00+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
tools.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2020 - 2024 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_matrix_free_tools_h
16#define dealii_matrix_free_tools_h
17
18#include <deal.II/base/config.h>
19
20#include <deal.II/grid/tria.h>
21
27
28
30
36{
37 namespace internal
38 {
39 template <int dim, typename Number, bool is_face_>
41 {
42 public:
44
45 std::vector<unsigned int> dof_numbers;
46 std::vector<unsigned int> quad_numbers;
47 std::vector<unsigned int> n_components;
48 std::vector<unsigned int> first_selected_components;
49 std::vector<unsigned int> batch_type;
50 static const bool is_face = is_face_;
51
52 std::function<std::vector<std::unique_ptr<FEEvalType>>(
53 const std::pair<unsigned int, unsigned int> &)>
55 std::function<void(std::vector<std::unique_ptr<FEEvalType>> &,
56 const unsigned int)>
58 std::function<void(std::vector<std::unique_ptr<FEEvalType>> &)>
60 };
61 } // namespace internal
62
68 template <int dim, typename AdditionalData>
69 void
71 AdditionalData &additional_data);
72
73
74
86 template <int dim,
87 int fe_degree,
88 int n_q_points_1d,
89 int n_components,
90 typename Number,
91 typename VectorizedArrayType,
92 typename VectorType>
93 void
96 VectorType &diagonal_global,
97 const std::function<void(FEEvaluation<dim,
98 fe_degree,
99 n_q_points_1d,
100 n_components,
101 Number,
102 VectorizedArrayType> &)>
103 &cell_operation,
104 const unsigned int dof_no = 0,
105 const unsigned int quad_no = 0,
106 const unsigned int first_selected_component = 0,
107 const unsigned int first_vector_component = 0);
108
112 template <int dim,
113 int fe_degree,
114 int n_q_points_1d,
115 int n_components,
116 typename Number,
117 typename MemorySpace,
118 typename QuadOperation>
119 void
121 const Portable::MatrixFree<dim, Number> &matrix_free,
123 const QuadOperation &quad_operation,
124 EvaluationFlags::EvaluationFlags evaluation_flags,
125 EvaluationFlags::EvaluationFlags integration_flags,
126 const unsigned int dof_no = 0,
127 const unsigned int quad_no = 0,
128 const unsigned int first_selected_component = 0,
129 const unsigned int first_vector_component = 0);
130
134 template <typename CLASS,
135 int dim,
136 int fe_degree,
137 int n_q_points_1d,
138 int n_components,
139 typename Number,
140 typename VectorizedArrayType,
141 typename VectorType>
142 void
145 VectorType &diagonal_global,
146 void (CLASS::*cell_operation)(FEEvaluation<dim,
147 fe_degree,
148 n_q_points_1d,
149 n_components,
150 Number,
151 VectorizedArrayType> &) const,
152 const CLASS *owning_class,
153 const unsigned int dof_no = 0,
154 const unsigned int quad_no = 0,
155 const unsigned int first_selected_component = 0,
156 const unsigned int first_vector_component = 0);
157
158
159
170 template <int dim,
171 int fe_degree,
172 int n_q_points_1d,
173 int n_components,
174 typename Number,
175 typename VectorizedArrayType,
176 typename VectorType>
177 void
180 VectorType &diagonal_global,
181 const std::function<void(FEEvaluation<dim,
182 fe_degree,
183 n_q_points_1d,
184 n_components,
185 Number,
186 VectorizedArrayType> &)>
187 &cell_operation,
188 const std::function<void(FEFaceEvaluation<dim,
189 fe_degree,
190 n_q_points_1d,
191 n_components,
192 Number,
193 VectorizedArrayType> &,
195 fe_degree,
196 n_q_points_1d,
197 n_components,
198 Number,
199 VectorizedArrayType> &)>
200 &face_operation,
201 const std::function<void(FEFaceEvaluation<dim,
202 fe_degree,
203 n_q_points_1d,
204 n_components,
205 Number,
206 VectorizedArrayType> &)>
207 &boundary_operation,
208 const unsigned int dof_no = 0,
209 const unsigned int quad_no = 0,
210 const unsigned int first_selected_component = 0,
211 const unsigned int first_vector_component = 0);
212
213
214
218 template <typename CLASS,
219 int dim,
220 int fe_degree,
221 int n_q_points_1d,
222 int n_components,
223 typename Number,
224 typename VectorizedArrayType,
225 typename VectorType>
226 void
229 VectorType &diagonal_global,
230 void (CLASS::*cell_operation)(FEEvaluation<dim,
231 fe_degree,
232 n_q_points_1d,
233 n_components,
234 Number,
235 VectorizedArrayType> &) const,
236 void (CLASS::*face_operation)(FEFaceEvaluation<dim,
237 fe_degree,
238 n_q_points_1d,
239 n_components,
240 Number,
241 VectorizedArrayType> &,
243 fe_degree,
244 n_q_points_1d,
245 n_components,
246 Number,
247 VectorizedArrayType> &)
248 const,
249 void (CLASS::*boundary_operation)(FEFaceEvaluation<dim,
250 fe_degree,
251 n_q_points_1d,
252 n_components,
253 Number,
254 VectorizedArrayType> &)
255 const,
256 const CLASS *owning_class,
257 const unsigned int dof_no = 0,
258 const unsigned int quad_no = 0,
259 const unsigned int first_selected_component = 0,
260 const unsigned int first_vector_component = 0);
261
262
263
272 template <int dim,
273 int fe_degree,
274 int n_q_points_1d,
275 int n_components,
276 typename Number,
277 typename VectorizedArrayType,
278 typename MatrixType>
279 void
282 const AffineConstraints<Number> &constraints,
283 MatrixType &matrix,
284 const std::function<void(FEEvaluation<dim,
285 fe_degree,
286 n_q_points_1d,
287 n_components,
288 Number,
289 VectorizedArrayType> &)>
290 &cell_operation,
291 const unsigned int dof_no = 0,
292 const unsigned int quad_no = 0,
293 const unsigned int first_selected_component = 0);
294
295
296
300 template <typename CLASS,
301 int dim,
302 int fe_degree,
303 int n_q_points_1d,
304 int n_components,
305 typename Number,
306 typename VectorizedArrayType,
307 typename MatrixType>
308 void
311 const AffineConstraints<Number> &constraints,
312 MatrixType &matrix,
313 void (CLASS::*cell_operation)(FEEvaluation<dim,
314 fe_degree,
315 n_q_points_1d,
316 n_components,
317 Number,
318 VectorizedArrayType> &) const,
319 const CLASS *owning_class,
320 const unsigned int dof_no = 0,
321 const unsigned int quad_no = 0,
322 const unsigned int first_selected_component = 0);
323
324
325 namespace internal
326 {
331 template <int dim,
332 typename Number,
333 typename VectorizedArrayType,
334 typename VectorType,
335 typename VectorType2>
336 void
340 &data_cell,
342 &data_face,
344 &data_boundary,
345 VectorType &diagonal_global,
346 std::vector<VectorType2 *> &diagonal_global_components);
347
352 template <int dim,
353 typename Number,
354 typename VectorizedArrayType,
355 typename MatrixType>
356 void
359 const AffineConstraints<Number> &constraints,
361 &cell_operation,
363 &face_operation,
365 &boundary_operation,
366 MatrixType &matrix);
367 } // namespace internal
368
369
370
380 template <int dim,
381 int fe_degree,
382 int n_q_points_1d,
383 int n_components,
384 typename Number,
385 typename VectorizedArrayType,
386 typename MatrixType>
387 void
390 const AffineConstraints<Number> &constraints,
391 MatrixType &matrix,
392 const std::function<void(FEEvaluation<dim,
393 fe_degree,
394 n_q_points_1d,
395 n_components,
396 Number,
397 VectorizedArrayType> &)>
398 &cell_operation,
399 const std::function<void(FEFaceEvaluation<dim,
400 fe_degree,
401 n_q_points_1d,
402 n_components,
403 Number,
404 VectorizedArrayType> &,
406 fe_degree,
407 n_q_points_1d,
408 n_components,
409 Number,
410 VectorizedArrayType> &)>
411 &face_operation,
412 const std::function<void(FEFaceEvaluation<dim,
413 fe_degree,
414 n_q_points_1d,
415 n_components,
416 Number,
417 VectorizedArrayType> &)>
418 &boundary_operation,
419 const unsigned int dof_no = 0,
420 const unsigned int quad_no = 0,
421 const unsigned int first_selected_component = 0);
422
423
424
428 template <typename CLASS,
429 int dim,
430 int fe_degree,
431 int n_q_points_1d,
432 int n_components,
433 typename Number,
434 typename VectorizedArrayType,
435 typename MatrixType>
436 void
439 const AffineConstraints<Number> &constraints,
440 MatrixType &matrix,
441 void (CLASS::*cell_operation)(FEEvaluation<dim,
442 fe_degree,
443 n_q_points_1d,
444 n_components,
445 Number,
446 VectorizedArrayType> &) const,
447 void (CLASS::*face_operation)(FEFaceEvaluation<dim,
448 fe_degree,
449 n_q_points_1d,
450 n_components,
451 Number,
452 VectorizedArrayType> &,
454 fe_degree,
455 n_q_points_1d,
456 n_components,
457 Number,
458 VectorizedArrayType> &)
459 const,
460 void (CLASS::*boundary_operation)(FEFaceEvaluation<dim,
461 fe_degree,
462 n_q_points_1d,
463 n_components,
464 Number,
465 VectorizedArrayType> &)
466 const,
467 const CLASS *owning_class,
468 const unsigned int dof_no = 0,
469 const unsigned int quad_no = 0,
470 const unsigned int first_selected_component = 0);
471
472
473
482 template <int dim,
483 typename Number,
484 typename VectorizedArrayType = VectorizedArray<Number>>
486 {
487 public:
493 {
497 AdditionalData(const unsigned int dof_index = 0)
499 {}
500
504 unsigned int dof_index;
505 };
506
516 void
518 const AdditionalData &additional_data = AdditionalData())
519 {
520 this->matrix_free = &matrix_free;
521
522 std::vector<unsigned int> valid_fe_indices;
523
524 const auto &fe_collection =
525 matrix_free.get_dof_handler(additional_data.dof_index)
526 .get_fe_collection();
527
528 for (unsigned int i = 0; i < fe_collection.size(); ++i)
529 if (fe_collection[i].n_dofs_per_cell() > 0)
530 valid_fe_indices.push_back(i);
531
532 // TODO: relax this so that arbitrary number of valid
533 // FEs can be accepted
534 AssertDimension(valid_fe_indices.size(), 1);
535
536 fe_index_valid = *valid_fe_indices.begin();
537 }
538
544 template <typename VectorTypeOut, typename VectorTypeIn>
545 void
546 cell_loop(const std::function<void(
548 VectorTypeOut &,
549 const VectorTypeIn &,
550 const std::pair<unsigned int, unsigned int> &)> &cell_operation,
551 VectorTypeOut &dst,
552 const VectorTypeIn &src,
553 const bool zero_dst_vector = false) const
554 {
555 const auto ebd_cell_operation = [&](const auto &matrix_free,
556 auto &dst,
557 const auto &src,
558 const auto &range) {
559 const auto category = matrix_free.get_cell_range_category(range);
560
561 if (category != fe_index_valid)
562 return;
563
564 cell_operation(matrix_free, dst, src, range);
565 };
566
567 matrix_free->template cell_loop<VectorTypeOut, VectorTypeIn>(
568 ebd_cell_operation, dst, src, zero_dst_vector);
569 }
570
578 template <typename VectorTypeOut, typename VectorTypeIn>
579 void
580 loop(const std::function<
582 VectorTypeOut &,
583 const VectorTypeIn &,
584 const std::pair<unsigned int, unsigned int> &)> &cell_operation,
585 const std::function<
587 VectorTypeOut &,
588 const VectorTypeIn &,
589 const std::pair<unsigned int, unsigned int> &)> &face_operation,
590 const std::function<
592 VectorTypeOut &,
593 const VectorTypeIn &,
594 const std::pair<unsigned int, unsigned int> &,
595 const bool)> &boundary_operation,
596 VectorTypeOut &dst,
597 const VectorTypeIn &src,
598 const bool zero_dst_vector = false) const
599 {
600 const auto ebd_cell_operation = [&](const auto &matrix_free,
601 auto &dst,
602 const auto &src,
603 const auto &range) {
604 const auto category = matrix_free.get_cell_range_category(range);
605
606 if (category != fe_index_valid)
607 return;
608
609 cell_operation(matrix_free, dst, src, range);
610 };
611
612 const auto ebd_internal_or_boundary_face_operation =
613 [&](const auto &matrix_free,
614 auto &dst,
615 const auto &src,
616 const auto &range) {
617 const auto category = matrix_free.get_face_range_category(range);
618
619 const unsigned int type =
620 static_cast<unsigned int>(category.first == fe_index_valid) +
621 static_cast<unsigned int>(category.second == fe_index_valid);
622
623 if (type == 0)
624 return; // deactivated face -> nothing to do
625
626 if (type == 1) // boundary face
627 boundary_operation(
628 matrix_free, dst, src, range, category.first == fe_index_valid);
629 else if (type == 2) // internal face
630 face_operation(matrix_free, dst, src, range);
631 };
632
633 matrix_free->template loop<VectorTypeOut, VectorTypeIn>(
634 ebd_cell_operation,
635 ebd_internal_or_boundary_face_operation,
636 ebd_internal_or_boundary_face_operation,
637 dst,
638 src,
639 zero_dst_vector);
640 }
641
642 private:
648
652 unsigned int fe_index_valid;
653 };
654
655 // implementations
656
657#ifndef DOXYGEN
658
659 template <int dim, typename AdditionalData>
660 void
662 AdditionalData &additional_data)
663 {
664 // ... determine if we are on an active or a multigrid level
665 const unsigned int level = additional_data.mg_level;
666 const bool is_mg = (level != numbers::invalid_unsigned_int);
667
668 // ... create empty list for the category of each cell
669 if (is_mg)
670 additional_data.cell_vectorization_category.assign(
671 std::distance(tria.begin(level), tria.end(level)), 0);
672 else
673 additional_data.cell_vectorization_category.assign(tria.n_active_cells(),
674 0);
675
676 // ... set up scaling factor
677 std::vector<unsigned int> factors(GeometryInfo<dim>::faces_per_cell);
678
679 auto bids = tria.get_boundary_ids();
680 std::sort(bids.begin(), bids.end());
681
682 {
683 unsigned int n_bids = bids.size() + 1;
684 int offset = 1;
685 for (unsigned int i = 0; i < GeometryInfo<dim>::faces_per_cell;
686 i++, offset = offset * n_bids)
687 factors[i] = offset;
688 }
689
690 const auto to_category = [&](const auto &cell) {
691 unsigned int c_num = 0;
692 for (unsigned int i = 0; i < GeometryInfo<dim>::faces_per_cell; ++i)
693 {
694 const auto face = cell->face(i);
695 if (face->at_boundary() && !cell->has_periodic_neighbor(i))
696 c_num +=
697 factors[i] * (1 + std::distance(bids.begin(),
698 std::find(bids.begin(),
699 bids.end(),
700 face->boundary_id())));
701 }
702 return c_num;
703 };
704
705 if (!is_mg)
706 {
707 for (auto cell = tria.begin_active(); cell != tria.end(); ++cell)
708 {
709 if (cell->is_locally_owned())
710 additional_data
711 .cell_vectorization_category[cell->active_cell_index()] =
712 to_category(cell);
713 }
714 }
715 else
716 {
717 for (auto cell = tria.begin(level); cell != tria.end(level); ++cell)
718 {
719 if (cell->is_locally_owned_on_level())
720 additional_data.cell_vectorization_category[cell->index()] =
721 to_category(cell);
722 }
723 }
724
725 // ... finalize set up of matrix_free
726 additional_data.hold_all_faces_to_owned_cells = true;
727 additional_data.cell_vectorization_categories_strict = true;
728 additional_data.mapping_update_flags_faces_by_cells =
729 additional_data.mapping_update_flags_inner_faces |
730 additional_data.mapping_update_flags_boundary_faces;
731 }
732
733 namespace internal
734 {
735 template <typename Number>
736 struct LocalCSR
737 {
738 std::vector<unsigned int> row_lid_to_gid;
739 std::vector<unsigned int> row;
740 std::vector<unsigned int> col;
741 std::vector<Number> val;
742
743 std::vector<unsigned int> inverse_lookup_rows;
744 std::vector<std::pair<unsigned int, unsigned int>> inverse_lookup_origins;
745 };
746
747 template <int dim, typename VectorizedArrayType, bool is_face>
748 class ComputeDiagonalHelper
749 {
750 public:
751 using FEEvaluationType =
753
754 using Number = typename VectorizedArrayType::value_type;
755 static const unsigned int n_lanes = VectorizedArrayType::size();
756
757 ComputeDiagonalHelper()
758 : phi(nullptr)
759 , matrix_free(nullptr)
760 , dofs_per_component(0)
761 , n_components(0)
762 {}
763
764 ComputeDiagonalHelper(const ComputeDiagonalHelper &)
765 : phi(nullptr)
766 , matrix_free(nullptr)
767 , dofs_per_component(0)
768 , n_components(0)
769 {}
770
771 void
772 initialize(
773 FEEvaluationType &phi,
775 const unsigned int n_components)
776 {
777 // if we are in hp mode and the number of unknowns changed, we must
778 // clear the map of entries
779 if (dofs_per_component !=
780 phi.get_shape_info().dofs_per_component_on_cell)
781 {
782 locally_relevant_constraints_hn_map.clear();
783 dofs_per_component =
784 phi.get_shape_info().dofs_per_component_on_cell;
785 }
786 this->n_components = n_components;
787 this->dofs_per_cell = n_components * dofs_per_component;
788 this->phi = &phi;
789 this->matrix_free = &matrix_free;
790 }
791
792 void
793 reinit(const unsigned int cell)
794 {
795 // STEP 1: get relevant information from FEEvaluation
796 const auto &matrix_free = *this->matrix_free;
797 const auto &dof_info = phi->get_dof_info();
798 const unsigned int n_fe_components = dof_info.start_components.back();
799
800 // if we have a block vector with components with the same DoFHandler,
801 // each component is described with same set of constraints and
802 // we consider the shift in components only during access of the global
803 // vector
804 const unsigned int first_selected_component =
805 n_fe_components == 1 ? 0 : phi->get_first_selected_component();
806
807 this->n_lanes_filled =
808 is_face ? matrix_free.n_active_entries_per_face_batch(cell) :
809 matrix_free.n_active_entries_per_cell_batch(cell);
810
811 // STEP 2: setup CSR storage of transposed locally-relevant
812 // constraint matrix
813
814 // (constrained local index, global index of dof
815 // constraints, weight)
816 std::vector<std::tuple<unsigned int, unsigned int, Number>>
817 locally_relevant_constraints, locally_relevant_constraints_tmp;
818 locally_relevant_constraints.reserve(dofs_per_cell);
819 std::vector<unsigned int> constraint_position;
820 std::vector<unsigned char> is_constrained_hn;
821
822 const std::array<unsigned int, n_lanes> &cells =
823 this->phi->get_cell_ids();
824
825 for (unsigned int v = 0; v < n_lanes_filled; ++v)
826 {
829
830 const unsigned int *dof_indices;
831 unsigned int index_indicators, next_index_indicators;
832
833 const unsigned int start =
834 cells[v] * n_fe_components + first_selected_component;
835 dof_indices =
836 dof_info.dof_indices.data() + dof_info.row_starts[start].first;
837 index_indicators = dof_info.row_starts[start].second;
838 next_index_indicators = dof_info.row_starts[start + 1].second;
839
840 // STEP 2a: setup locally-relevant constraint matrix in a
841 // coordinate list (COO)
842 locally_relevant_constraints.clear();
843
844 if (n_components == 1 || n_fe_components == 1)
845 {
846 unsigned int ind_local = 0;
847 for (; index_indicators != next_index_indicators;
848 ++index_indicators, ++ind_local)
849 {
850 const std::pair<unsigned short, unsigned short> indicator =
851 dof_info.constraint_indicator[index_indicators];
852
853 for (unsigned int j = 0; j < indicator.first;
854 ++j, ++ind_local)
855 locally_relevant_constraints.emplace_back(ind_local,
856 dof_indices[j],
857 1.0);
858
859 dof_indices += indicator.first;
860
861 const Number *data_val =
862 matrix_free.constraint_pool_begin(indicator.second);
863 const Number *end_pool =
864 matrix_free.constraint_pool_end(indicator.second);
865
866 for (; data_val != end_pool; ++data_val, ++dof_indices)
867 locally_relevant_constraints.emplace_back(ind_local,
868 *dof_indices,
869 *data_val);
870 }
871
872 AssertIndexRange(ind_local, dofs_per_component + 1);
873
874 for (; ind_local < dofs_per_component;
875 ++dof_indices, ++ind_local)
876 locally_relevant_constraints.emplace_back(ind_local,
877 *dof_indices,
878 1.0);
879 }
880 else
881 {
882 // case with vector-valued finite elements where all
883 // components are included in one single vector. Assumption:
884 // first come all entries to the first component, then all
885 // entries to the second one, and so on. This is ensured by
886 // the way MatrixFree reads out the indices.
887 for (unsigned int comp = 0; comp < n_components; ++comp)
888 {
889 unsigned int ind_local = 0;
890
891 // check whether there is any constraint on the current
892 // cell
893 for (; index_indicators != next_index_indicators;
894 ++index_indicators, ++ind_local)
895 {
896 const std::pair<unsigned short, unsigned short>
897 indicator =
898 dof_info.constraint_indicator[index_indicators];
899
900 // run through values up to next constraint
901 for (unsigned int j = 0; j < indicator.first;
902 ++j, ++ind_local)
903 locally_relevant_constraints.emplace_back(
904 comp * dofs_per_component + ind_local,
905 dof_indices[j],
906 1.0);
907 dof_indices += indicator.first;
908
909 const Number *data_val =
910 matrix_free.constraint_pool_begin(indicator.second);
911 const Number *end_pool =
912 matrix_free.constraint_pool_end(indicator.second);
913
914 for (; data_val != end_pool; ++data_val, ++dof_indices)
915 locally_relevant_constraints.emplace_back(
916 comp * dofs_per_component + ind_local,
917 *dof_indices,
918 *data_val);
919 }
920
921 AssertIndexRange(ind_local, dofs_per_component + 1);
922
923 // get the dof values past the last constraint
924 for (; ind_local < dofs_per_component;
925 ++dof_indices, ++ind_local)
926 locally_relevant_constraints.emplace_back(
927 comp * dofs_per_component + ind_local,
928 *dof_indices,
929 1.0);
930
931 if (comp + 1 < n_components)
932 next_index_indicators =
933 dof_info.row_starts[start + comp + 2].second;
934 }
935 }
936
937 // we only need partial sortedness for the algorithm below in that
938 // all entries for a particular row must be adjacent. this is
939 // ensured by the way we fill the field, but check it again
940 for (unsigned int i = 1; i < locally_relevant_constraints.size();
941 ++i)
942 Assert(std::get<0>(locally_relevant_constraints[i]) >=
943 std::get<0>(locally_relevant_constraints[i - 1]),
945
946 // STEP 2c: apply hanging-node constraints
947 if (dof_info.hanging_node_constraint_masks.size() > 0 &&
948 dof_info.hanging_node_constraint_masks_comp.size() > 0 &&
949 dof_info.hanging_node_constraint_masks_comp
950 [phi->get_active_fe_index()][first_selected_component])
951 {
952 const auto mask =
953 dof_info.hanging_node_constraint_masks[cells[v]];
954
955 // cell has hanging nodes
956 if (mask != ::internal::MatrixFreeFunctions::
958 {
959 // check if hanging node internpolation matrix has been set
960 // up
961 if (locally_relevant_constraints_hn_map.find(mask) ==
962 locally_relevant_constraints_hn_map.end())
963 fill_constraint_type_into_map(mask);
964
965 const auto &locally_relevant_constraints_hn =
966 locally_relevant_constraints_hn_map[mask];
967
968 locally_relevant_constraints_tmp.clear();
969 if (locally_relevant_constraints_tmp.capacity() <
970 locally_relevant_constraints.size())
971 locally_relevant_constraints_tmp.reserve(
972 locally_relevant_constraints.size() +
973 locally_relevant_constraints_hn.size());
974
975 // combine with other constraints: to avoid binary
976 // searches, we first build a list of where constraints
977 // are pointing to, and then merge the two lists
978 constraint_position.assign(dofs_per_cell,
980 for (auto &a : locally_relevant_constraints)
981 if (constraint_position[std::get<0>(a)] ==
982 numbers::invalid_unsigned_int)
983 constraint_position[std::get<0>(a)] =
984 std::distance(locally_relevant_constraints.data(),
985 &a);
986 is_constrained_hn.assign(dofs_per_cell, false);
987 for (auto &hn : locally_relevant_constraints_hn)
988 is_constrained_hn[std::get<0>(hn)] = 1;
989
990 // not constrained from hanging nodes
991 for (const auto &a : locally_relevant_constraints)
992 if (is_constrained_hn[std::get<0>(a)] == 0)
993 locally_relevant_constraints_tmp.push_back(a);
994
995 // dof is constrained by hanging nodes: build transitive
996 // closure
997 for (const auto &hn : locally_relevant_constraints_hn)
998 if (constraint_position[std::get<1>(hn)] !=
999 numbers::invalid_unsigned_int)
1000 {
1001 AssertIndexRange(constraint_position[std::get<1>(hn)],
1002 locally_relevant_constraints.size());
1003 auto other = locally_relevant_constraints.begin() +
1004 constraint_position[std::get<1>(hn)];
1005 AssertDimension(std::get<0>(*other), std::get<1>(hn));
1006
1007 for (; other != locally_relevant_constraints.end() &&
1008 std::get<0>(*other) == std::get<1>(hn);
1009 ++other)
1010 locally_relevant_constraints_tmp.emplace_back(
1011 std::get<0>(hn),
1012 std::get<1>(*other),
1013 std::get<2>(hn) * std::get<2>(*other));
1014 }
1015
1016 std::swap(locally_relevant_constraints,
1017 locally_relevant_constraints_tmp);
1018 }
1019 }
1020
1021 // STEP 2d: transpose COO
1022 std::sort(locally_relevant_constraints.begin(),
1023 locally_relevant_constraints.end(),
1024 [](const auto &a, const auto &b) {
1025 if (std::get<1>(a) < std::get<1>(b))
1026 return true;
1027 return (std::get<1>(a) == std::get<1>(b)) &&
1028 (std::get<0>(a) < std::get<0>(b));
1029 });
1030
1031 // STEP 2e: translate COO to CRS
1032 auto &c_pool = c_pools[v];
1033 {
1034 c_pool.row_lid_to_gid.clear();
1035 c_pool.row.clear();
1036 c_pool.row.push_back(0);
1037 c_pool.col.clear();
1038 c_pool.val.clear();
1039
1040 if (locally_relevant_constraints.size() > 0)
1041 c_pool.row_lid_to_gid.emplace_back(
1042 std::get<1>(locally_relevant_constraints.front()));
1043 for (const auto &j : locally_relevant_constraints)
1044 {
1045 if (c_pool.row_lid_to_gid.back() != std::get<1>(j))
1046 {
1047 c_pool.row_lid_to_gid.push_back(std::get<1>(j));
1048 c_pool.row.push_back(c_pool.val.size());
1049 }
1050
1051 c_pool.col.emplace_back(std::get<0>(j));
1052 c_pool.val.emplace_back(std::get<2>(j));
1053 }
1054
1055 if (c_pool.val.size() > 0)
1056 c_pool.row.push_back(c_pool.val.size());
1057
1058 c_pool.inverse_lookup_rows.clear();
1059 c_pool.inverse_lookup_rows.resize(1 + dofs_per_cell);
1060 for (const unsigned int i : c_pool.col)
1061 c_pool.inverse_lookup_rows[1 + i]++;
1062 // transform to offsets
1063 std::partial_sum(c_pool.inverse_lookup_rows.begin(),
1064 c_pool.inverse_lookup_rows.end(),
1065 c_pool.inverse_lookup_rows.begin());
1066 AssertDimension(c_pool.inverse_lookup_rows.back(),
1067 c_pool.col.size());
1068
1069 c_pool.inverse_lookup_origins.resize(c_pool.col.size());
1070 std::vector<unsigned int> inverse_lookup_count(dofs_per_cell);
1071 for (unsigned int row = 0; row < c_pool.row.size() - 1; ++row)
1072 for (unsigned int col = c_pool.row[row];
1073 col < c_pool.row[row + 1];
1074 ++col)
1075 {
1076 const unsigned int index = c_pool.col[col];
1077 c_pool.inverse_lookup_origins
1078 [c_pool.inverse_lookup_rows[index] +
1079 inverse_lookup_count[index]] = std::make_pair(row, col);
1080 ++inverse_lookup_count[index];
1081 }
1082 }
1083 }
1084
1085 // STEP 3: compute element matrix A_e, apply
1086 // locally-relevant constraints C_e^T * A_e * C_e, and get the
1087 // the diagonal entry
1088 // (C_e^T * A_e * C_e)(i,i)
1089 // or
1090 // C_e^T(i,:) * A_e * C_e(:,i).
1091 //
1092 // Since, we compute the element matrix column-by-column and as a
1093 // result never actually have the full element matrix, we actually
1094 // perform following steps:
1095 // 1) loop over all columns of the element matrix
1096 // a) compute column i
1097 // b) compute for each j (rows of C_e^T):
1098 // (C_e^T(j,:) * A_e(:,i)) * C_e(i,j)
1099 // or
1100 // (C_e^T(j,:) * A_e(:,i)) * C_e^T(j,i)
1101 // This gives a contribution the j-th entry of the
1102 // locally-relevant diagonal and comprises the multiplication
1103 // by the locally-relevant constraint matrix from the left and
1104 // the right. There is no contribution to the j-th vector
1105 // entry if the j-th row of C_e^T is empty or C_e^T(j,i) is
1106 // zero.
1107
1108 // set size locally-relevant diagonal
1109 for (unsigned int v = 0; v < n_lanes_filled; ++v)
1110 diagonals_local_constrained[v].assign(
1111 c_pools[v].row_lid_to_gid.size() *
1112 (n_fe_components == 1 ? n_components : 1),
1113 Number(0.0));
1114
1115 // check if fast path can be taken via FEEvaluation
1116 bool use_fast_path = true;
1117
1118 for (unsigned int v = 0; v < n_lanes_filled; ++v)
1119 {
1120 auto &c_pool = c_pools[v];
1121
1122 for (unsigned int i = 0; i < c_pool.row.size() - 1; ++i)
1123 {
1124 if ((c_pool.row[i + 1] - c_pool.row[i]) > 1)
1125 {
1126 use_fast_path = false;
1127 break;
1128 }
1129 else if (((c_pool.row[i + 1] - c_pool.row[i]) == 1) &&
1130 (c_pool.val[c_pool.row[i]] != 1.0))
1131 {
1132 use_fast_path = false;
1133 break;
1134 }
1135 }
1136
1137 if (use_fast_path == false)
1138 break;
1139 }
1140
1141 this->has_simple_constraints_ = use_fast_path;
1142 }
1143
1144 void
1145 fill_constraint_type_into_map(
1146 const ::internal::MatrixFreeFunctions::compressed_constraint_kind
1147 mask)
1148 {
1149 auto &constraints_hn = locally_relevant_constraints_hn_map[mask];
1150
1151 // assume that we constrain one face, i.e., (fe_degree + 1)^(dim-1)
1152 // unknowns - we might have more or less entries, but this is a good
1153 // first guess
1154 const unsigned int degree =
1155 phi->get_shape_info().data.front().fe_degree;
1156 constraints_hn.reserve(Utilities::pow(degree + 1, dim - 1));
1157
1158 // 1) collect hanging-node constraints for cell assuming
1159 // scalar finite element
1160 values_dofs.resize(dofs_per_component);
1161 std::array<
1163 VectorizedArrayType::size()>
1164 constraint_mask;
1165 constraint_mask.fill(::internal::MatrixFreeFunctions::
1167 constraint_mask[0] = mask;
1168
1169 for (unsigned int i = 0; i < dofs_per_component; ++i)
1170 {
1171 for (unsigned int j = 0; j < dofs_per_component; ++j)
1172 values_dofs[j] = VectorizedArrayType();
1173 values_dofs[i] = Number(1);
1174
1176 dim,
1177 Number,
1178 VectorizedArrayType>::apply(1,
1179 degree,
1180 phi->get_shape_info(),
1181 false,
1182 constraint_mask,
1183 values_dofs.data());
1184
1185 const Number tolerance =
1186 std::max(Number(1e-12),
1187 std::numeric_limits<Number>::epsilon() * 16);
1188 for (unsigned int j = 0; j < dofs_per_component; ++j)
1189 if (std::abs(values_dofs[j][0]) > tolerance &&
1190 (j != i ||
1191 std::abs(values_dofs[j][0] - Number(1)) > tolerance))
1192 constraints_hn.emplace_back(j, i, values_dofs[j][0]);
1193 }
1194
1195 // 2) extend for multiple components
1196 const unsigned int n_hn_constraints = constraints_hn.size();
1197 constraints_hn.resize(n_hn_constraints * n_components);
1198
1199 for (unsigned int c = 1; c < n_components; ++c)
1200 for (unsigned int i = 0; i < n_hn_constraints; ++i)
1201 constraints_hn[c * n_hn_constraints + i] = std::make_tuple(
1202 std::get<0>(constraints_hn[i]) + c * dofs_per_component,
1203 std::get<1>(constraints_hn[i]) + c * dofs_per_component,
1204 std::get<2>(constraints_hn[i]));
1205 }
1206
1207 void
1208 prepare_basis_vector(const unsigned int i)
1209 {
1210 this->i = i;
1211
1212 // compute i-th column of element stiffness matrix:
1213 // this could be simply performed as done at the moment with
1214 // matrix-free operator evaluation applied to a ith-basis vector
1215 VectorizedArrayType *dof_values = phi->begin_dof_values();
1216 for (unsigned int j = 0; j < dofs_per_cell; ++j)
1217 dof_values[j] = VectorizedArrayType();
1218 dof_values[i] = Number(1);
1219 }
1220
1221 void
1222 zero_basis_vector()
1223 {
1224 VectorizedArrayType *dof_values = phi->begin_dof_values();
1225 for (unsigned int j = 0; j < dofs_per_cell; ++j)
1226 dof_values[j] = VectorizedArrayType();
1227 }
1228
1229 void
1230 submit()
1231 {
1232 // if we have a block vector with components with the same DoFHandler,
1233 // we need to figure out which component and which DoF within the
1234 // component are we currently considering
1235 const unsigned int n_fe_components =
1236 phi->get_dof_info().start_components.back();
1237 const unsigned int comp =
1238 n_fe_components == 1 ? i / dofs_per_component : 0;
1239 const unsigned int i_comp =
1240 n_fe_components == 1 ? (i % dofs_per_component) : i;
1241
1242 // apply local constraint matrix from left and from right:
1243 // loop over all rows of transposed constrained matrix
1244 for (unsigned int v = 0; v < n_lanes_filled; ++v)
1245 {
1246 const auto &c_pool = c_pools[v];
1247
1248 for (unsigned int jj = c_pool.inverse_lookup_rows[i_comp];
1249 jj < c_pool.inverse_lookup_rows[i_comp + 1];
1250 ++jj)
1251 {
1252 const unsigned int j = c_pool.inverse_lookup_origins[jj].first;
1253 // apply constraint matrix from the left
1254 Number temp = 0.0;
1255 for (unsigned int k = c_pool.row[j]; k < c_pool.row[j + 1]; ++k)
1256 temp += c_pool.val[k] *
1257 phi->begin_dof_values()[comp * dofs_per_component +
1258 c_pool.col[k]][v];
1259
1260 // apply constraint matrix from the right
1261 diagonals_local_constrained
1262 [v][j + comp * c_pools[v].row_lid_to_gid.size()] +=
1263 temp * c_pool.val[c_pool.inverse_lookup_origins[jj].second];
1264 }
1265 }
1266 }
1267
1268 template <typename VectorType>
1269 inline void
1270 distribute_local_to_global(std::vector<VectorType *> &diagonal_global)
1271 {
1272 // STEP 4: assembly results: add into global vector
1273 const unsigned int n_fe_components =
1274 phi->get_dof_info().start_components.back();
1275
1276 if (n_fe_components == 1)
1277 AssertDimension(diagonal_global.size(), n_components);
1278
1279 for (unsigned int v = 0; v < n_lanes_filled; ++v)
1280 // if we have a block vector with components with the same
1281 // DoFHandler, we need to loop over all components manually and
1282 // need to apply the correct shift
1283 for (unsigned int j = 0; j < c_pools[v].row.size() - 1; ++j)
1284 for (unsigned int comp = 0;
1285 comp < (n_fe_components == 1 ?
1286 static_cast<unsigned int>(n_components) :
1287 1);
1288 ++comp)
1290 *diagonal_global[n_fe_components == 1 ? comp : 0],
1291 c_pools[v].row_lid_to_gid[j],
1292 diagonals_local_constrained
1293 [v][j + comp * c_pools[v].row_lid_to_gid.size()]);
1294 }
1295
1296 bool
1297 has_simple_constraints() const
1298 {
1299 return has_simple_constraints_;
1300 }
1301
1302 private:
1303 FEEvaluationType *phi;
1305
1306 unsigned int dofs_per_component;
1307 unsigned int dofs_per_cell;
1308 unsigned int n_components;
1309
1310 unsigned int i;
1311
1312 unsigned int n_lanes_filled;
1313
1314 std::array<internal::LocalCSR<Number>, n_lanes> c_pools;
1315
1316 // local storage: buffer so that we access the global vector once
1317 // note: may be larger then dofs_per_cell in the presence of
1318 // constraints!
1319 std::array<std::vector<Number>, n_lanes> diagonals_local_constrained;
1320
1321 std::map<
1323 std::vector<std::tuple<unsigned int, unsigned int, Number>>>
1324 locally_relevant_constraints_hn_map;
1325
1326 // scratch array
1328
1329 bool has_simple_constraints_;
1330 };
1331
1332 template <bool is_face,
1333 int dim,
1334 typename Number,
1335 typename VectorizedArrayType>
1336 bool
1337 is_fe_nothing(
1339 const std::pair<unsigned int, unsigned int> &range,
1340 const unsigned int dof_no,
1341 const unsigned int quad_no,
1342 const unsigned int first_selected_component,
1343 const unsigned int fe_degree,
1344 const unsigned int n_q_points_1d,
1345 const bool is_interior_face = true)
1346 {
1347 const unsigned int static_n_q_points =
1348 is_face ? Utilities::pow(n_q_points_1d, dim - 1) :
1349 Utilities::pow(n_q_points_1d, dim);
1350
1351 unsigned int active_fe_index = 0;
1352 if (!is_face)
1353 active_fe_index = matrix_free.get_cell_active_fe_index(range);
1354 else if (is_interior_face)
1355 active_fe_index = matrix_free.get_face_range_category(range).first;
1356 else
1357 active_fe_index = matrix_free.get_face_range_category(range).second;
1358
1359 const auto init_data = ::internal::
1360 extract_initialization_data<is_face, dim, Number, VectorizedArrayType>(
1361 matrix_free,
1362 dof_no,
1363 first_selected_component,
1364 quad_no,
1365 fe_degree,
1366 static_n_q_points,
1367 active_fe_index,
1368 numbers::invalid_unsigned_int /*active_quad_index*/,
1369 numbers::invalid_unsigned_int /*face_type*/);
1370
1371 return init_data.shape_info->dofs_per_component_on_cell == 0;
1372 }
1373
1374 } // namespace internal
1375
1376
1377 template <int dim,
1378 int fe_degree,
1379 int n_q_points_1d,
1380 typename Number,
1381 typename QuadOperation>
1382 class CellAction
1383 {
1384 public:
1385 CellAction(const QuadOperation &quad_operation,
1386 const EvaluationFlags::EvaluationFlags evaluation_flags,
1387 const EvaluationFlags::EvaluationFlags integration_flags)
1388 : m_quad_operation(quad_operation)
1389 , m_evaluation_flags(evaluation_flags)
1390 , m_integration_flags(integration_flags)
1391 {}
1392
1393 KOKKOS_FUNCTION void
1394 operator()(const unsigned int cell,
1395 const typename Portable::MatrixFree<dim, Number>::Data *gpu_data,
1397 const Number *,
1398 Number *dst) const
1399 {
1401 gpu_data, shared_data);
1402 m_quad_operation.set_matrix_free_data(*gpu_data);
1403 m_quad_operation.set_cell(cell);
1404 constexpr int dofs_per_cell = decltype(fe_eval)::tensor_dofs_per_cell;
1405 Number diagonal[dofs_per_cell] = {};
1406 for (unsigned int i = 0; i < dofs_per_cell; ++i)
1407 {
1408 Kokkos::parallel_for(
1409 Kokkos::TeamThreadRange(shared_data->team_member, dofs_per_cell),
1410 [&](int j) { fe_eval.submit_dof_value(i == j ? 1 : 0, j); });
1411
1412 Portable::internal::
1413 resolve_hanging_nodes<dim, fe_degree, false, Number>(
1414 shared_data->team_member,
1415 gpu_data->constraint_weights,
1416 gpu_data->constraint_mask(cell),
1417 Kokkos::subview(shared_data->values, Kokkos::ALL, 0));
1418
1419 fe_eval.evaluate(m_evaluation_flags);
1420 fe_eval.apply_for_each_quad_point(m_quad_operation);
1421 fe_eval.integrate(m_integration_flags);
1422
1423 Portable::internal::
1424 resolve_hanging_nodes<dim, fe_degree, true, Number>(
1425 shared_data->team_member,
1426 gpu_data->constraint_weights,
1427 gpu_data->constraint_mask(cell),
1428 Kokkos::subview(shared_data->values, Kokkos::ALL, 0));
1429
1430 Kokkos::single(Kokkos::PerTeam(shared_data->team_member),
1431 [&] { diagonal[i] = fe_eval.get_dof_value(i); });
1432 }
1433
1434 Kokkos::single(Kokkos::PerTeam(shared_data->team_member), [&] {
1435 for (unsigned int i = 0; i < dofs_per_cell; ++i)
1436 fe_eval.submit_dof_value(diagonal[i], i);
1437 });
1438
1439 // We need to do the same as distribute_local_to_global but without
1440 // constraints since we have already taken care of them earlier
1441 if (gpu_data->use_coloring)
1442 {
1443 Kokkos::parallel_for(Kokkos::TeamThreadRange(shared_data->team_member,
1444 dofs_per_cell),
1445 [&](const int &i) {
1446 dst[gpu_data->local_to_global(cell, i)] +=
1447 shared_data->values(i, 0);
1448 });
1449 }
1450 else
1451 {
1452 Kokkos::parallel_for(
1453 Kokkos::TeamThreadRange(shared_data->team_member, dofs_per_cell),
1454 [&](const int &i) {
1455 Kokkos::atomic_add(&dst[gpu_data->local_to_global(cell, i)],
1456 shared_data->values(i, 0));
1457 });
1458 }
1459 };
1460
1461 static constexpr unsigned int n_local_dofs = QuadOperation::n_local_dofs;
1462
1463 private:
1464 mutable QuadOperation m_quad_operation;
1465 const EvaluationFlags::EvaluationFlags m_evaluation_flags;
1466 const EvaluationFlags::EvaluationFlags m_integration_flags;
1467 };
1468
1469
1470 template <int dim,
1471 int fe_degree,
1472 int n_q_points_1d,
1473 int n_components,
1474 typename Number,
1475 typename MemorySpace,
1476 typename QuadOperation>
1477 void
1479 const Portable::MatrixFree<dim, Number> &matrix_free,
1481 const QuadOperation &quad_operation,
1482 EvaluationFlags::EvaluationFlags evaluation_flags,
1483 EvaluationFlags::EvaluationFlags integration_flags,
1484 const unsigned int dof_no,
1485 const unsigned int quad_no,
1486 const unsigned int first_selected_component,
1487 const unsigned int first_vector_component)
1488 {
1489 Assert(dof_no == 0, ExcNotImplemented());
1490 Assert(quad_no == 0, ExcNotImplemented());
1491 Assert(first_selected_component == 0, ExcNotImplemented());
1492 Assert(first_vector_component == 0, ExcNotImplemented());
1493
1494 matrix_free.initialize_dof_vector(diagonal_global);
1495
1496
1497 CellAction<dim, fe_degree, n_q_points_1d, Number, QuadOperation>
1498 cell_action(quad_operation, evaluation_flags, integration_flags);
1500 matrix_free.cell_loop(cell_action, dummy, diagonal_global);
1501
1502 matrix_free.set_constrained_values(Number(1.), diagonal_global);
1503 }
1504
1505 template <int dim,
1506 int fe_degree,
1507 int n_q_points_1d,
1508 int n_components,
1509 typename Number,
1510 typename VectorizedArrayType,
1511 typename VectorType>
1512 void
1515 VectorType &diagonal_global,
1516 const std::function<void(FEEvaluation<dim,
1517 fe_degree,
1518 n_q_points_1d,
1519 n_components,
1520 Number,
1521 VectorizedArrayType> &)>
1522 &cell_operation,
1523 const unsigned int dof_no,
1524 const unsigned int quad_no,
1525 const unsigned int first_selected_component,
1526 const unsigned int first_vector_component)
1527 {
1528 compute_diagonal<dim,
1529 fe_degree,
1530 n_q_points_1d,
1531 n_components,
1532 Number,
1533 VectorizedArrayType,
1534 VectorType>(matrix_free,
1535 diagonal_global,
1536 cell_operation,
1537 {},
1538 {},
1539 dof_no,
1540 quad_no,
1541 first_selected_component,
1542 first_vector_component);
1543 }
1544
1545 template <typename CLASS,
1546 int dim,
1547 int fe_degree,
1548 int n_q_points_1d,
1549 int n_components,
1550 typename Number,
1551 typename VectorizedArrayType,
1552 typename VectorType>
1553 void
1556 VectorType &diagonal_global,
1557 void (CLASS::*cell_operation)(FEEvaluation<dim,
1558 fe_degree,
1559 n_q_points_1d,
1560 n_components,
1561 Number,
1562 VectorizedArrayType> &) const,
1563 const CLASS *owning_class,
1564 const unsigned int dof_no,
1565 const unsigned int quad_no,
1566 const unsigned int first_selected_component,
1567 const unsigned int first_vector_component)
1568 {
1569 compute_diagonal<dim,
1570 fe_degree,
1571 n_q_points_1d,
1572 n_components,
1573 Number,
1574 VectorizedArrayType,
1575 VectorType>(
1576 matrix_free,
1577 diagonal_global,
1578 [&](auto &phi) { (owning_class->*cell_operation)(phi); },
1579 dof_no,
1580 quad_no,
1581 first_selected_component,
1582 first_vector_component);
1583 }
1584
1585 template <int dim,
1586 int fe_degree,
1587 int n_q_points_1d,
1588 int n_components,
1589 typename Number,
1590 typename VectorizedArrayType,
1591 typename VectorType>
1592 void
1595 VectorType &diagonal_global,
1596 const std::function<void(FEEvaluation<dim,
1597 fe_degree,
1598 n_q_points_1d,
1599 n_components,
1600 Number,
1601 VectorizedArrayType> &)>
1602 &cell_operation,
1603 const std::function<void(FEFaceEvaluation<dim,
1604 fe_degree,
1605 n_q_points_1d,
1606 n_components,
1607 Number,
1608 VectorizedArrayType> &,
1609 FEFaceEvaluation<dim,
1610 fe_degree,
1611 n_q_points_1d,
1612 n_components,
1613 Number,
1614 VectorizedArrayType> &)>
1615 &face_operation,
1616 const std::function<void(FEFaceEvaluation<dim,
1617 fe_degree,
1618 n_q_points_1d,
1619 n_components,
1620 Number,
1621 VectorizedArrayType> &)>
1622 &boundary_operation,
1623 const unsigned int dof_no,
1624 const unsigned int quad_no,
1625 const unsigned int first_selected_component,
1626 const unsigned int first_vector_component)
1627 {
1628 std::vector<typename ::internal::BlockVectorSelector<
1629 VectorType,
1630 IsBlockVector<VectorType>::value>::BaseVectorType *>
1631 diagonal_global_components(n_components);
1632
1633 for (unsigned int d = 0; d < n_components; ++d)
1634 diagonal_global_components[d] = ::internal::
1635 BlockVectorSelector<VectorType, IsBlockVector<VectorType>::value>::
1636 get_vector_component(diagonal_global, d + first_vector_component);
1637
1638 const auto &dof_info = matrix_free.get_dof_info(dof_no);
1639
1640 if (dof_info.start_components.back() == 1)
1641 for (unsigned int comp = 0; comp < n_components; ++comp)
1642 {
1643 Assert(diagonal_global_components[comp] != nullptr,
1644 ExcMessage("The finite element underlying this FEEvaluation "
1645 "object is scalar, but you requested " +
1646 std::to_string(n_components) +
1647 " components via the template argument in "
1648 "FEEvaluation. In that case, you must pass an "
1649 "std::vector<VectorType> or a BlockVector to " +
1650 "read_dof_values and distribute_local_to_global."));
1652 *diagonal_global_components[comp], matrix_free, dof_info);
1653 }
1654 else
1655 {
1657 *diagonal_global_components[0], matrix_free, dof_info);
1658 }
1659
1660 using FEEvalType = FEEvaluation<dim,
1661 fe_degree,
1662 n_q_points_1d,
1663 n_components,
1664 Number,
1665 VectorizedArrayType>;
1666
1667 using FEFaceEvalType = FEFaceEvaluation<dim,
1668 fe_degree,
1669 n_q_points_1d,
1670 n_components,
1671 Number,
1672 VectorizedArrayType>;
1673
1674 internal::ComputeMatrixScratchData<dim, VectorizedArrayType, false>
1675 data_cell;
1676
1677 data_cell.dof_numbers = {dof_no};
1678 data_cell.quad_numbers = {quad_no};
1679 data_cell.n_components = {n_components};
1680 data_cell.first_selected_components = {first_selected_component};
1681 data_cell.batch_type = {0};
1682
1683 data_cell.op_create =
1684 [&](const std::pair<unsigned int, unsigned int> &range) {
1685 std::vector<
1686 std::unique_ptr<FEEvaluationData<dim, VectorizedArrayType, false>>>
1687 phi;
1688
1689 if (!internal::is_fe_nothing<false>(matrix_free,
1690 range,
1691 dof_no,
1692 quad_no,
1693 first_selected_component,
1694 fe_degree,
1695 n_q_points_1d))
1696 phi.emplace_back(std::make_unique<FEEvalType>(
1697 matrix_free, range, dof_no, quad_no, first_selected_component));
1698
1699 return phi;
1700 };
1701
1702 data_cell.op_reinit = [](auto &phi, const unsigned batch) {
1703 if (phi.size() == 1)
1704 static_cast<FEEvalType &>(*phi[0]).reinit(batch);
1705 };
1706
1707 if (cell_operation)
1708 data_cell.op_compute = [&](auto &phi) {
1709 cell_operation(static_cast<FEEvalType &>(*phi[0]));
1710 };
1711
1712 internal::ComputeMatrixScratchData<dim, VectorizedArrayType, true>
1713 data_face;
1714
1715 data_face.dof_numbers = {dof_no, dof_no};
1716 data_face.quad_numbers = {quad_no, quad_no};
1717 data_face.n_components = {n_components, n_components};
1718 data_face.first_selected_components = {first_selected_component,
1719 first_selected_component};
1720 data_face.batch_type = {1, 2};
1721
1722 data_face.op_create =
1723 [&](const std::pair<unsigned int, unsigned int> &range) {
1724 std::vector<
1725 std::unique_ptr<FEEvaluationData<dim, VectorizedArrayType, true>>>
1726 phi;
1727
1728 if (!internal::is_fe_nothing<true>(matrix_free,
1729 range,
1730 dof_no,
1731 quad_no,
1732 first_selected_component,
1733 fe_degree,
1734 n_q_points_1d,
1735 true) &&
1736 !internal::is_fe_nothing<true>(matrix_free,
1737 range,
1738 dof_no,
1739 quad_no,
1740 first_selected_component,
1741 fe_degree,
1742 n_q_points_1d,
1743 false))
1744 {
1745 phi.emplace_back(
1746 std::make_unique<FEFaceEvalType>(matrix_free,
1747 range,
1748 true,
1749 dof_no,
1750 quad_no,
1751 first_selected_component));
1752 phi.emplace_back(
1753 std::make_unique<FEFaceEvalType>(matrix_free,
1754 range,
1755 false,
1756 dof_no,
1757 quad_no,
1758 first_selected_component));
1759 }
1760
1761 return phi;
1762 };
1763
1764 data_face.op_reinit = [](auto &phi, const unsigned batch) {
1765 if (phi.size() == 2)
1766 {
1767 static_cast<FEFaceEvalType &>(*phi[0]).reinit(batch);
1768 static_cast<FEFaceEvalType &>(*phi[1]).reinit(batch);
1769 }
1770 };
1771
1772 if (face_operation)
1773 data_face.op_compute = [&](auto &phi) {
1774 face_operation(static_cast<FEFaceEvalType &>(*phi[0]),
1775 static_cast<FEFaceEvalType &>(*phi[1]));
1776 };
1777
1778 internal::ComputeMatrixScratchData<dim, VectorizedArrayType, true>
1779 data_boundary;
1780
1781 data_boundary.dof_numbers = {dof_no};
1782 data_boundary.quad_numbers = {quad_no};
1783 data_boundary.n_components = {n_components};
1784 data_boundary.first_selected_components = {first_selected_component};
1785 data_boundary.batch_type = {1};
1786
1787 data_boundary
1788 .op_create = [&](const std::pair<unsigned int, unsigned int> &range) {
1789 std::vector<
1790 std::unique_ptr<FEEvaluationData<dim, VectorizedArrayType, true>>>
1791 phi;
1792
1793 if (!internal::is_fe_nothing<true>(matrix_free,
1794 range,
1795 dof_no,
1796 quad_no,
1797 first_selected_component,
1798 fe_degree,
1799 n_q_points_1d,
1800 true))
1801 phi.emplace_back(std::make_unique<FEFaceEvalType>(
1802 matrix_free, range, true, dof_no, quad_no, first_selected_component));
1803
1804 return phi;
1805 };
1806
1807 data_boundary.op_reinit = [](auto &phi, const unsigned batch) {
1808 if (phi.size() == 1)
1809 static_cast<FEFaceEvalType &>(*phi[0]).reinit(batch);
1810 };
1811
1812 if (boundary_operation)
1813 data_boundary.op_compute = [&](auto &phi) {
1814 boundary_operation(static_cast<FEFaceEvalType &>(*phi[0]));
1815 };
1816
1817 internal::compute_diagonal(matrix_free,
1818 data_cell,
1819 data_face,
1820 data_boundary,
1821 diagonal_global,
1822 diagonal_global_components);
1823 }
1824
1825 namespace internal
1826 {
1827 template <int dim,
1828 typename Number,
1829 typename VectorizedArrayType,
1830 typename VectorType,
1831 typename VectorType2>
1832 void
1835 const internal::ComputeMatrixScratchData<dim, VectorizedArrayType, false>
1836 &data_cell,
1837 const internal::ComputeMatrixScratchData<dim, VectorizedArrayType, true>
1838 &data_face,
1839 const internal::ComputeMatrixScratchData<dim, VectorizedArrayType, true>
1840 &data_boundary,
1841 VectorType &diagonal_global,
1842 std::vector<VectorType2 *> &diagonal_global_components)
1843 {
1844 // TODO: can we remove diagonal_global_components as argument?
1845
1846 int dummy = 0;
1847
1848 using Helper =
1849 internal::ComputeDiagonalHelper<dim, VectorizedArrayType, false>;
1850
1851 using HelperFace =
1852 internal::ComputeDiagonalHelper<dim, VectorizedArrayType, true>;
1853
1856 scratch_data_internal;
1858
1859 const auto batch_operation =
1860 [&](auto &data,
1861 auto &scratch_data,
1862 const std::pair<unsigned int, unsigned int> &range) {
1863 if (!data.op_compute)
1864 return; // nothing to do
1865
1866 auto phi = data.op_create(range);
1867
1868 const unsigned int n_blocks = phi.size();
1869
1870 auto &helpers = scratch_data.get();
1871 helpers.resize(n_blocks);
1872
1873 for (unsigned int b = 0; b < n_blocks; ++b)
1874 helpers[b].initialize(*phi[b], matrix_free, data.n_components[b]);
1875
1876 for (unsigned int batch = range.first; batch < range.second; ++batch)
1877 {
1878 data.op_reinit(phi, batch);
1879
1880 for (unsigned int b = 0; b < n_blocks; ++b)
1881 helpers[b].reinit(batch);
1882
1883 if (n_blocks > 1)
1884 {
1885 Assert(std::all_of(helpers.begin(),
1886 helpers.end(),
1887 [](const auto &helper) {
1888 return helper.has_simple_constraints();
1889 }),
1891 }
1892
1893 for (unsigned int b = 0; b < n_blocks; ++b)
1894 {
1895 for (unsigned int i = 0;
1896 i < phi[b]->get_shape_info().dofs_per_component_on_cell *
1897 data.n_components[b];
1898 ++i)
1899 {
1900 for (unsigned int bb = 0; bb < n_blocks; ++bb)
1901 if (b == bb)
1902 helpers[bb].prepare_basis_vector(i);
1903 else
1904 helpers[bb].zero_basis_vector();
1905
1906 data.op_compute(phi);
1907 helpers[b].submit();
1908 }
1909
1910 helpers[b].distribute_local_to_global(
1911 diagonal_global_components);
1912 }
1913 }
1914 };
1915
1916 const auto cell_operation_wrapped =
1917 [&](const auto &, auto &, const auto &, const auto range) {
1918 batch_operation(data_cell, scratch_data, range);
1919 };
1920
1921 const auto face_operation_wrapped =
1922 [&](const auto &, auto &, const auto &, const auto range) {
1923 batch_operation(data_face, scratch_data_internal, range);
1924 };
1925
1926 const auto boundary_operation_wrapped =
1927 [&](const auto &, auto &, const auto &, const auto range) {
1928 batch_operation(data_boundary, scratch_data_bc, range);
1929 };
1930
1931 if (data_face.op_compute || data_boundary.op_compute)
1932 matrix_free.template loop<VectorType, int>(cell_operation_wrapped,
1933 face_operation_wrapped,
1934 boundary_operation_wrapped,
1935 diagonal_global,
1936 dummy,
1937 false);
1938 else
1939 matrix_free.template cell_loop<VectorType, int>(cell_operation_wrapped,
1940 diagonal_global,
1941 dummy,
1942 false);
1943 }
1944 } // namespace internal
1945
1946 template <typename CLASS,
1947 int dim,
1948 int fe_degree,
1949 int n_q_points_1d,
1950 int n_components,
1951 typename Number,
1952 typename VectorizedArrayType,
1953 typename VectorType>
1954 void
1957 VectorType &diagonal_global,
1958 void (CLASS::*cell_operation)(FEEvaluation<dim,
1959 fe_degree,
1960 n_q_points_1d,
1961 n_components,
1962 Number,
1963 VectorizedArrayType> &) const,
1964 void (CLASS::*face_operation)(FEFaceEvaluation<dim,
1965 fe_degree,
1966 n_q_points_1d,
1967 n_components,
1968 Number,
1969 VectorizedArrayType> &,
1970 FEFaceEvaluation<dim,
1971 fe_degree,
1972 n_q_points_1d,
1973 n_components,
1974 Number,
1975 VectorizedArrayType> &)
1976 const,
1977 void (CLASS::*boundary_operation)(FEFaceEvaluation<dim,
1978 fe_degree,
1979 n_q_points_1d,
1980 n_components,
1981 Number,
1982 VectorizedArrayType> &)
1983 const,
1984 const CLASS *owning_class,
1985 const unsigned int dof_no,
1986 const unsigned int quad_no,
1987 const unsigned int first_selected_component,
1988 const unsigned int first_vector_component)
1989 {
1990 compute_diagonal<dim,
1991 fe_degree,
1992 n_q_points_1d,
1993 n_components,
1994 Number,
1995 VectorizedArrayType,
1996 VectorType>(
1997 matrix_free,
1998 diagonal_global,
1999 [&](auto &phi) { (owning_class->*cell_operation)(phi); },
2000 [&](auto &phi_m, auto &phi_p) {
2001 (owning_class->*face_operation)(phi_m, phi_p);
2002 },
2003 [&](auto &phi) { (owning_class->*boundary_operation)(phi); },
2004 dof_no,
2005 quad_no,
2006 first_selected_component,
2007 first_vector_component);
2008 }
2009
2010 namespace internal
2011 {
2016 template <
2017 typename MatrixType,
2018 typename Number,
2019 std::enable_if_t<std::is_same_v<
2020 std::remove_const_t<
2021 std::remove_reference_t<typename MatrixType::value_type>>,
2022 std::remove_const_t<std::remove_reference_t<Number>>>> * = nullptr>
2024 create_new_affine_constraints_if_needed(
2025 const MatrixType &,
2026 const AffineConstraints<Number> &constraints,
2028 {
2029 return constraints;
2030 }
2031
2037 template <
2038 typename MatrixType,
2039 typename Number,
2040 std::enable_if_t<!std::is_same_v<
2041 std::remove_const_t<
2042 std::remove_reference_t<typename MatrixType::value_type>>,
2043 std::remove_const_t<std::remove_reference_t<Number>>>> * = nullptr>
2045 create_new_affine_constraints_if_needed(
2046 const MatrixType &,
2047 const AffineConstraints<Number> &constraints,
2049 &new_constraints)
2050 {
2051 new_constraints =
2052 std::make_unique<AffineConstraints<typename MatrixType::value_type>>();
2053 new_constraints->copy_from(constraints);
2054
2055 return *new_constraints;
2056 }
2057 } // namespace internal
2058
2059 template <int dim,
2060 int fe_degree,
2061 int n_q_points_1d,
2062 int n_components,
2063 typename Number,
2064 typename VectorizedArrayType,
2065 typename MatrixType>
2066 void
2069 const AffineConstraints<Number> &constraints_in,
2070 MatrixType &matrix,
2071 const std::function<void(FEEvaluation<dim,
2072 fe_degree,
2073 n_q_points_1d,
2074 n_components,
2075 Number,
2076 VectorizedArrayType> &)>
2077 &cell_operation,
2078 const unsigned int dof_no,
2079 const unsigned int quad_no,
2080 const unsigned int first_selected_component)
2081 {
2082 compute_matrix<dim,
2083 fe_degree,
2084 n_q_points_1d,
2085 n_components,
2086 Number,
2087 VectorizedArrayType,
2088 MatrixType>(matrix_free,
2089 constraints_in,
2090 matrix,
2091 cell_operation,
2092 {},
2093 {},
2094 dof_no,
2095 quad_no,
2096 first_selected_component);
2097 }
2098
2099 template <typename CLASS,
2100 int dim,
2101 int fe_degree,
2102 int n_q_points_1d,
2103 int n_components,
2104 typename Number,
2105 typename VectorizedArrayType,
2106 typename MatrixType>
2107 void
2110 const AffineConstraints<Number> &constraints,
2111 MatrixType &matrix,
2112 void (CLASS::*cell_operation)(FEEvaluation<dim,
2113 fe_degree,
2114 n_q_points_1d,
2115 n_components,
2116 Number,
2117 VectorizedArrayType> &) const,
2118 const CLASS *owning_class,
2119 const unsigned int dof_no,
2120 const unsigned int quad_no,
2121 const unsigned int first_selected_component)
2122 {
2123 compute_matrix<dim,
2124 fe_degree,
2125 n_q_points_1d,
2126 n_components,
2127 Number,
2128 VectorizedArrayType,
2129 MatrixType>(
2130 matrix_free,
2131 constraints,
2132 matrix,
2133 [&](auto &phi) { (owning_class->*cell_operation)(phi); },
2134 dof_no,
2135 quad_no,
2136 first_selected_component);
2137 }
2138
2139 template <int dim,
2140 int fe_degree,
2141 int n_q_points_1d,
2142 int n_components,
2143 typename Number,
2144 typename VectorizedArrayType,
2145 typename MatrixType>
2146 void
2149 const AffineConstraints<Number> &constraints_in,
2150 MatrixType &matrix,
2151 const std::function<void(FEEvaluation<dim,
2152 fe_degree,
2153 n_q_points_1d,
2154 n_components,
2155 Number,
2156 VectorizedArrayType> &)>
2157 &cell_operation,
2158 const std::function<void(FEFaceEvaluation<dim,
2159 fe_degree,
2160 n_q_points_1d,
2161 n_components,
2162 Number,
2163 VectorizedArrayType> &,
2164 FEFaceEvaluation<dim,
2165 fe_degree,
2166 n_q_points_1d,
2167 n_components,
2168 Number,
2169 VectorizedArrayType> &)>
2170 &face_operation,
2171 const std::function<void(FEFaceEvaluation<dim,
2172 fe_degree,
2173 n_q_points_1d,
2174 n_components,
2175 Number,
2176 VectorizedArrayType> &)>
2177 &boundary_operation,
2178 const unsigned int dof_no,
2179 const unsigned int quad_no,
2180 const unsigned int first_selected_component)
2181 {
2182 using FEEvalType = FEEvaluation<dim,
2183 fe_degree,
2184 n_q_points_1d,
2185 n_components,
2186 Number,
2187 VectorizedArrayType>;
2188
2189 using FEFaceEvalType = FEFaceEvaluation<dim,
2190 fe_degree,
2191 n_q_points_1d,
2192 n_components,
2193 Number,
2194 VectorizedArrayType>;
2195
2196 internal::ComputeMatrixScratchData<dim, VectorizedArrayType, false>
2197 data_cell;
2198
2199 data_cell.dof_numbers = {dof_no};
2200 data_cell.quad_numbers = {quad_no};
2201 data_cell.n_components = {n_components};
2202 data_cell.first_selected_components = {first_selected_component};
2203 data_cell.batch_type = {0};
2204
2205 data_cell.op_create =
2206 [&](const std::pair<unsigned int, unsigned int> &range) {
2207 std::vector<
2208 std::unique_ptr<FEEvaluationData<dim, VectorizedArrayType, false>>>
2209 phi;
2210
2211 if (!internal::is_fe_nothing<false>(matrix_free,
2212 range,
2213 dof_no,
2214 quad_no,
2215 first_selected_component,
2216 fe_degree,
2217 n_q_points_1d))
2218 phi.emplace_back(std::make_unique<FEEvalType>(
2219 matrix_free, range, dof_no, quad_no, first_selected_component));
2220
2221 return phi;
2222 };
2223
2224 data_cell.op_reinit = [](auto &phi, const unsigned batch) {
2225 if (phi.size() == 1)
2226 static_cast<FEEvalType &>(*phi[0]).reinit(batch);
2227 };
2228
2229 if (cell_operation)
2230 data_cell.op_compute = [&](auto &phi) {
2231 cell_operation(static_cast<FEEvalType &>(*phi[0]));
2232 };
2233
2234 internal::ComputeMatrixScratchData<dim, VectorizedArrayType, true>
2235 data_face;
2236
2237 data_face.dof_numbers = {dof_no, dof_no};
2238 data_face.quad_numbers = {quad_no, quad_no};
2239 data_face.n_components = {n_components, n_components};
2240 data_face.first_selected_components = {first_selected_component,
2241 first_selected_component};
2242 data_face.batch_type = {1, 2};
2243
2244 data_face.op_create =
2245 [&](const std::pair<unsigned int, unsigned int> &range) {
2246 std::vector<
2247 std::unique_ptr<FEEvaluationData<dim, VectorizedArrayType, true>>>
2248 phi;
2249
2250 if (!internal::is_fe_nothing<true>(matrix_free,
2251 range,
2252 dof_no,
2253 quad_no,
2254 first_selected_component,
2255 fe_degree,
2256 n_q_points_1d,
2257 true) &&
2258 !internal::is_fe_nothing<true>(matrix_free,
2259 range,
2260 dof_no,
2261 quad_no,
2262 first_selected_component,
2263 fe_degree,
2264 n_q_points_1d,
2265 false))
2266 {
2267 phi.emplace_back(
2268 std::make_unique<FEFaceEvalType>(matrix_free,
2269 range,
2270 true,
2271 dof_no,
2272 quad_no,
2273 first_selected_component));
2274 phi.emplace_back(
2275 std::make_unique<FEFaceEvalType>(matrix_free,
2276 range,
2277 false,
2278 dof_no,
2279 quad_no,
2280 first_selected_component));
2281 }
2282
2283 return phi;
2284 };
2285
2286 data_face.op_reinit = [](auto &phi, const unsigned batch) {
2287 if (phi.size() == 2)
2288 {
2289 static_cast<FEFaceEvalType &>(*phi[0]).reinit(batch);
2290 static_cast<FEFaceEvalType &>(*phi[1]).reinit(batch);
2291 }
2292 };
2293
2294 if (face_operation)
2295 data_face.op_compute = [&](auto &phi) {
2296 face_operation(static_cast<FEFaceEvalType &>(*phi[0]),
2297 static_cast<FEFaceEvalType &>(*phi[1]));
2298 };
2299
2300 internal::ComputeMatrixScratchData<dim, VectorizedArrayType, true>
2301 data_boundary;
2302
2303 data_boundary.dof_numbers = {dof_no};
2304 data_boundary.quad_numbers = {quad_no};
2305 data_boundary.n_components = {n_components};
2306 data_boundary.first_selected_components = {first_selected_component};
2307 data_boundary.batch_type = {1};
2308
2309 data_boundary
2310 .op_create = [&](const std::pair<unsigned int, unsigned int> &range) {
2311 std::vector<
2312 std::unique_ptr<FEEvaluationData<dim, VectorizedArrayType, true>>>
2313 phi;
2314
2315 if (!internal::is_fe_nothing<true>(matrix_free,
2316 range,
2317 dof_no,
2318 quad_no,
2319 first_selected_component,
2320 fe_degree,
2321 n_q_points_1d,
2322 true))
2323 phi.emplace_back(std::make_unique<FEFaceEvalType>(
2324 matrix_free, range, true, dof_no, quad_no, first_selected_component));
2325
2326 return phi;
2327 };
2328
2329 data_boundary.op_reinit = [](auto &phi, const unsigned batch) {
2330 if (phi.size() == 1)
2331 static_cast<FEFaceEvalType &>(*phi[0]).reinit(batch);
2332 };
2333
2334 if (boundary_operation)
2335 data_boundary.op_compute = [&](auto &phi) {
2336 boundary_operation(static_cast<FEFaceEvalType &>(*phi[0]));
2337 };
2338
2340 matrix_free, constraints_in, data_cell, data_face, data_boundary, matrix);
2341 }
2342
2343 namespace internal
2344 {
2345 template <int dim,
2346 typename Number,
2347 typename VectorizedArrayType,
2348 typename MatrixType>
2349 void
2352 const AffineConstraints<Number> &constraints_in,
2353 const internal::ComputeMatrixScratchData<dim, VectorizedArrayType, false>
2354 &data_cell,
2355 const internal::ComputeMatrixScratchData<dim, VectorizedArrayType, true>
2356 &data_face,
2357 const internal::ComputeMatrixScratchData<dim, VectorizedArrayType, true>
2358 &data_boundary,
2359 MatrixType &matrix)
2360 {
2361 std::unique_ptr<AffineConstraints<typename MatrixType::value_type>>
2362 constraints_for_matrix;
2364 internal::create_new_affine_constraints_if_needed(
2365 matrix, constraints_in, constraints_for_matrix);
2366
2367 const auto batch_operation =
2368 [&matrix_free, &constraints, &matrix](
2369 auto &data, const std::pair<unsigned int, unsigned int> &range) {
2370 if (!data.op_compute)
2371 return; // nothing to do
2372
2373 auto phi = data.op_create(range);
2374
2375 const unsigned int n_blocks = phi.size();
2376
2377 if (n_blocks == 0)
2378 return;
2379
2380 Table<1, unsigned int> dofs_per_cell(n_blocks);
2381
2382 Table<1, std::vector<types::global_dof_index>> dof_indices(n_blocks);
2384 n_blocks, VectorizedArrayType::size());
2385 Table<1, std::vector<unsigned int>> lexicographic_numbering(n_blocks);
2386 Table<2,
2387 std::array<FullMatrix<typename MatrixType::value_type>,
2388 VectorizedArrayType::size()>>
2389 matrices(n_blocks, n_blocks);
2390
2391 for (unsigned int b = 0; b < n_blocks; ++b)
2392 {
2393 const auto &fe = matrix_free.get_dof_handler(data.dof_numbers[b])
2394 .get_fe(phi[b]->get_active_fe_index());
2395
2396 const auto component_base =
2397 matrix_free.get_dof_info(data.dof_numbers[b])
2398 .component_to_base_index[data.first_selected_components[b]];
2399 const auto component_in_base =
2400 data.first_selected_components[b] -
2401 matrix_free.get_dof_info(data.dof_numbers[b])
2402 .start_components[component_base];
2403
2404 const auto &shape_info = matrix_free.get_shape_info(
2405 data.dof_numbers[b],
2406 data.quad_numbers[b],
2407 component_base,
2408 phi[b]->get_active_fe_index(),
2409 phi[b]->get_active_quadrature_index());
2410
2411 dofs_per_cell[b] =
2412 shape_info.dofs_per_component_on_cell * data.n_components[b];
2413
2414 dof_indices[b].resize(fe.n_dofs_per_cell());
2415
2416 for (unsigned int v = 0; v < VectorizedArrayType::size(); ++v)
2417 dof_indices_mf[b][v].resize(dofs_per_cell[b]);
2418
2419 lexicographic_numbering[b].insert(
2420 lexicographic_numbering[b].begin(),
2421 shape_info.lexicographic_numbering.begin() +
2422 component_in_base * shape_info.dofs_per_component_on_cell,
2423 shape_info.lexicographic_numbering.begin() +
2424 (component_in_base + data.n_components[b]) *
2425 shape_info.dofs_per_component_on_cell);
2426 }
2427
2428 for (unsigned int bj = 0; bj < n_blocks; ++bj)
2429 for (unsigned int bi = 0; bi < n_blocks; ++bi)
2430 std::fill_n(matrices[bi][bj].begin(),
2431 VectorizedArrayType::size(),
2433 dofs_per_cell[bi], dofs_per_cell[bj]));
2434
2435 for (auto batch = range.first; batch < range.second; ++batch)
2436 {
2437 data.op_reinit(phi, batch);
2438
2439 const unsigned int n_filled_lanes =
2440 data.is_face ?
2441 matrix_free.n_active_entries_per_face_batch(batch) :
2442 matrix_free.n_active_entries_per_cell_batch(batch);
2443
2444 for (unsigned int v = 0; v < n_filled_lanes; ++v)
2445 for (unsigned int b = 0; b < n_blocks; ++b)
2446 {
2447 unsigned int const cell_index =
2448 (data.batch_type[b] == 0) ?
2449 (batch * VectorizedArrayType::size() + v) :
2450 ((data.batch_type[b] == 1) ?
2451 matrix_free.get_face_info(batch).cells_interior[v] :
2452 matrix_free.get_face_info(batch).cells_exterior[v]);
2453
2454 const auto cell_iterator = matrix_free.get_cell_iterator(
2455 cell_index / VectorizedArrayType::size(),
2456 cell_index % VectorizedArrayType::size(),
2457 data.dof_numbers[b]);
2458
2459 if (matrix_free.get_mg_level() !=
2461 cell_iterator->get_mg_dof_indices(dof_indices[b]);
2462 else
2463 cell_iterator->get_dof_indices(dof_indices[b]);
2464
2465 for (unsigned int j = 0; j < dofs_per_cell[b]; ++j)
2466 dof_indices_mf[b][v][j] =
2467 dof_indices[b][lexicographic_numbering[b][j]];
2468 }
2469
2470 for (unsigned int bj = 0; bj < n_blocks; ++bj)
2471 {
2472 for (unsigned int j = 0; j < dofs_per_cell[bj]; ++j)
2473 {
2474 for (unsigned int bi = 0; bi < n_blocks; ++bi)
2475 for (unsigned int i = 0; i < dofs_per_cell[bi]; ++i)
2476 phi[bi]->begin_dof_values()[i] =
2477 (bj == bi) ? static_cast<Number>(i == j) : 0.0;
2478
2479 data.op_compute(phi);
2480
2481 for (unsigned int bi = 0; bi < n_blocks; ++bi)
2482 for (unsigned int i = 0; i < dofs_per_cell[bi]; ++i)
2483 for (unsigned int v = 0; v < n_filled_lanes; ++v)
2484 matrices[bi][bj][v](i, j) =
2485 phi[bi]->begin_dof_values()[i][v];
2486 }
2487
2488 for (unsigned int v = 0; v < n_filled_lanes; ++v)
2489 for (unsigned int bi = 0; bi < n_blocks; ++bi)
2490 if (bi == bj)
2491 // specialization for blocks on the diagonal
2492 // to writing into diagonal elements of the
2493 // matrix if the corresponding degree of freedom
2494 // is constrained, see also the documentation
2495 // of AffineConstraints::distribute_local_to_global()
2496 constraints.distribute_local_to_global(
2497 matrices[bi][bi][v], dof_indices_mf[bi][v], matrix);
2498 else
2499 constraints.distribute_local_to_global(
2500 matrices[bi][bj][v],
2501 dof_indices_mf[bi][v],
2502 dof_indices_mf[bj][v],
2503 matrix);
2504 }
2505 }
2506 };
2507
2508 const auto cell_operation_wrapped =
2509 [&](const auto &, auto &, const auto &, const auto range) {
2510 batch_operation(data_cell, range);
2511 };
2512
2513 const auto face_operation_wrapped =
2514 [&](const auto &, auto &, const auto &, const auto range) {
2515 batch_operation(data_face, range);
2516 };
2517
2518 const auto boundary_operation_wrapped =
2519 [&](const auto &, auto &, const auto &, const auto range) {
2520 batch_operation(data_boundary, range);
2521 };
2522
2523 if (data_face.op_compute || data_boundary.op_compute)
2524 {
2525 matrix_free.template loop<MatrixType, MatrixType>(
2526 cell_operation_wrapped,
2527 face_operation_wrapped,
2528 boundary_operation_wrapped,
2529 matrix,
2530 matrix);
2531 }
2532 else
2533 matrix_free.template cell_loop<MatrixType, MatrixType>(
2534 cell_operation_wrapped, matrix, matrix);
2535
2536 matrix.compress(VectorOperation::add);
2537 }
2538 } // namespace internal
2539
2540 template <typename CLASS,
2541 int dim,
2542 int fe_degree,
2543 int n_q_points_1d,
2544 int n_components,
2545 typename Number,
2546 typename VectorizedArrayType,
2547 typename MatrixType>
2548 void
2551 const AffineConstraints<Number> &constraints,
2552 MatrixType &matrix,
2553 void (CLASS::*cell_operation)(FEEvaluation<dim,
2554 fe_degree,
2555 n_q_points_1d,
2556 n_components,
2557 Number,
2558 VectorizedArrayType> &) const,
2559 void (CLASS::*face_operation)(FEFaceEvaluation<dim,
2560 fe_degree,
2561 n_q_points_1d,
2562 n_components,
2563 Number,
2564 VectorizedArrayType> &,
2565 FEFaceEvaluation<dim,
2566 fe_degree,
2567 n_q_points_1d,
2568 n_components,
2569 Number,
2570 VectorizedArrayType> &)
2571 const,
2572 void (CLASS::*boundary_operation)(FEFaceEvaluation<dim,
2573 fe_degree,
2574 n_q_points_1d,
2575 n_components,
2576 Number,
2577 VectorizedArrayType> &)
2578 const,
2579 const CLASS *owning_class,
2580 const unsigned int dof_no,
2581 const unsigned int quad_no,
2582 const unsigned int first_selected_component)
2583 {
2584 compute_matrix<dim,
2585 fe_degree,
2586 n_q_points_1d,
2587 n_components,
2588 Number,
2589 VectorizedArrayType,
2590 MatrixType>(
2591 matrix_free,
2592 constraints,
2593 matrix,
2594 [&](auto &phi) { (owning_class->*cell_operation)(phi); },
2595 [&](auto &phi_m, auto &phi_p) {
2596 (owning_class->*face_operation)(phi_m, phi_p);
2597 },
2598 [&](auto &phi) { (owning_class->*boundary_operation)(phi); },
2599 dof_no,
2600 quad_no,
2601 first_selected_component);
2602 }
2603
2604#endif // DOXYGEN
2605
2606} // namespace MatrixFreeTools
2607
2609
2610
2611#endif
void distribute_local_to_global(const InVector &local_vector, const std::vector< size_type > &local_dof_indices, OutVector &global_vector) const
void copy_from(const AffineConstraints< other_number > &other)
void loop(const std::function< void(const MatrixFree< dim, Number, VectorizedArrayType > &, VectorTypeOut &, const VectorTypeIn &, const std::pair< unsigned int, unsigned int > &)> &cell_operation, const std::function< void(const MatrixFree< dim, Number, VectorizedArrayType > &, VectorTypeOut &, const VectorTypeIn &, const std::pair< unsigned int, unsigned int > &)> &face_operation, const std::function< void(const MatrixFree< dim, Number, VectorizedArrayType > &, VectorTypeOut &, const VectorTypeIn &, const std::pair< unsigned int, unsigned int > &, const bool)> &boundary_operation, VectorTypeOut &dst, const VectorTypeIn &src, const bool zero_dst_vector=false) const
Definition tools.h:580
ObserverPointer< const MatrixFree< dim, Number, VectorizedArrayType > > matrix_free
Definition tools.h:647
void cell_loop(const std::function< void(const MatrixFree< dim, Number, VectorizedArrayType > &, VectorTypeOut &, const VectorTypeIn &, const std::pair< unsigned int, unsigned int > &)> &cell_operation, VectorTypeOut &dst, const VectorTypeIn &src, const bool zero_dst_vector=false) const
Definition tools.h:546
void reinit(const MatrixFree< dim, Number, VectorizedArrayType > &matrix_free, const AdditionalData &additional_data=AdditionalData())
Definition tools.h:517
std::vector< unsigned int > n_components
Definition tools.h:47
std::vector< unsigned int > quad_numbers
Definition tools.h:46
std::vector< unsigned int > batch_type
Definition tools.h:49
std::vector< unsigned int > dof_numbers
Definition tools.h:45
std::function< void(std::vector< std::unique_ptr< FEEvalType > > &, const unsigned int)> op_reinit
Definition tools.h:57
std::function< void(std::vector< std::unique_ptr< FEEvalType > > &)> op_compute
Definition tools.h:59
std::function< std::vector< std::unique_ptr< FEEvalType > >(const std::pair< unsigned int, unsigned int > &)> op_create
Definition tools.h:54
std::vector< unsigned int > first_selected_components
Definition tools.h:48
unsigned int get_cell_active_fe_index(const std::pair< unsigned int, unsigned int > range) const
unsigned int n_active_entries_per_cell_batch(const unsigned int cell_batch_index) const
unsigned int get_mg_level() const
void clear()
const internal::MatrixFreeFunctions::DoFInfo & get_dof_info(const unsigned int dof_handler_index_component=0) const
const Number * constraint_pool_begin(const unsigned int pool_index) const
const DoFHandler< dim > & get_dof_handler(const unsigned int dof_handler_index=0) const
DoFHandler< dim >::cell_iterator get_cell_iterator(const unsigned int cell_batch_index, const unsigned int lane_index, const unsigned int dof_handler_index=0) const
std::pair< unsigned int, unsigned int > get_face_range_category(const std::pair< unsigned int, unsigned int > face_batch_range) const
const Number * constraint_pool_end(const unsigned int pool_index) const
unsigned int n_active_entries_per_face_batch(const unsigned int face_batch_index) const
const internal::MatrixFreeFunctions::ShapeInfo< Number > & get_shape_info(const unsigned int dof_handler_index_component=0, const unsigned int quad_index=0, const unsigned int fe_base_element=0, const unsigned int hp_active_fe_index=0, const unsigned int hp_active_quad_index=0) const
void initialize_dof_vector(LinearAlgebra::distributed::Vector< Number, MemorySpaceType > &vec) const
void set_constrained_values(const Number val, VectorType &dst) const
void cell_loop(const Functor &func, const VectorType &src, VectorType &dst) const
A class that provides a separate storage location on each thread that accesses the object.
cell_iterator begin(const unsigned int level=0) const
unsigned int n_active_cells() const
cell_iterator end() const
virtual std::vector< types::boundary_id > get_boundary_ids() const
active_cell_iterator begin_active(const unsigned int level=0) const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
unsigned int level
Definition grid_out.cc:4626
unsigned int cell_index
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
void cell_action(IteratorType cell, DoFInfoBox< dim, DOFINFO > &dof_info, INFOBOX &info, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &cell_worker, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &boundary_worker, const std::function< void(DOFINFO &, DOFINFO &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, const LoopControl &loop_control)
Definition loop.h:316
std::vector< index_type > data
Definition mpi.cc:735
std::size_t size
Definition mpi.cc:734
EvaluationFlags
The EvaluationFlags enum.
@ matrix
Contents is actually a matrix.
@ diagonal
Matrix is diagonal.
Tpetra::Vector< Number, LO, GO, NodeType< MemorySpace > > VectorType
Tpetra::CrsMatrix< Number, LO, GO, NodeType< MemorySpace > > MatrixType
std::enable_if_t< IsBlockVector< VectorType >::value, unsigned int > n_blocks(const VectorType &vector)
Definition operators.h:49
void compute_diagonal(const MatrixFree< dim, Number, VectorizedArrayType > &matrix_free, const internal::ComputeMatrixScratchData< dim, VectorizedArrayType, false > &data_cell, const internal::ComputeMatrixScratchData< dim, VectorizedArrayType, true > &data_face, const internal::ComputeMatrixScratchData< dim, VectorizedArrayType, true > &data_boundary, VectorType &diagonal_global, std::vector< VectorType2 * > &diagonal_global_components)
void compute_matrix(const MatrixFree< dim, Number, VectorizedArrayType > &matrix_free, const AffineConstraints< Number > &constraints, const internal::ComputeMatrixScratchData< dim, VectorizedArrayType, false > &cell_operation, const internal::ComputeMatrixScratchData< dim, VectorizedArrayType, true > &face_operation, const internal::ComputeMatrixScratchData< dim, VectorizedArrayType, true > &boundary_operation, MatrixType &matrix)
void compute_diagonal(const MatrixFree< dim, Number, VectorizedArrayType > &matrix_free, VectorType &diagonal_global, const std::function< void(FEEvaluation< dim, fe_degree, n_q_points_1d, n_components, Number, VectorizedArrayType > &)> &cell_operation, const unsigned int dof_no=0, const unsigned int quad_no=0, const unsigned int first_selected_component=0, const unsigned int first_vector_component=0)
void compute_matrix(const MatrixFree< dim, Number, VectorizedArrayType > &matrix_free, const AffineConstraints< Number > &constraints, MatrixType &matrix, const std::function< void(FEEvaluation< dim, fe_degree, n_q_points_1d, n_components, Number, VectorizedArrayType > &)> &cell_operation, const unsigned int dof_no=0, const unsigned int quad_no=0, const unsigned int first_selected_component=0)
void categorize_by_boundary_ids(const Triangulation< dim > &tria, AdditionalData &additional_data)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
VectorType::value_type * begin(VectorType &V)
constexpr T pow(const T base, const int iexp)
Definition utilities.h:966
std::uint8_t compressed_constraint_kind
Definition dof_info.h:86
constexpr compressed_constraint_kind unconstrained_compressed_constraint_kind
void vector_access_add(VectorType &vec, const unsigned int entry, const typename VectorType::value_type &val)
void check_vector_compatibility(const VectorType &vec, const MatrixFree< dim, Number, VectorizedArrayType > &matrix_free, const internal::MatrixFreeFunctions::DoFInfo &dof_info)
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
static const unsigned int invalid_unsigned_int
Definition types.h:220
STL namespace.
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
Kokkos::View< Number *, MemorySpace::Default::kokkos_space > constraint_weights
Kokkos::View<::internal::MatrixFreeFunctions::ConstraintKinds *, MemorySpace::Default::kokkos_space > constraint_mask
std::vector< unsigned int > component_to_base_index
Definition dof_info.h:677
std::vector< unsigned int > start_components
Definition dof_info.h:671