deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20: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
mg_tools.cc
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) 1999 - 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
15#include <deal.II/base/mpi.h>
17
19
23
25#include <deal.II/fe/fe.h>
26
28#include <deal.II/grid/tria.h>
30
35
39
41
42#include <algorithm>
43#include <numeric>
44#include <set>
45#include <vector>
46
47
49
50
51namespace MGTools
52{
53 // specializations for 1d
54 template <>
55 void
57 const unsigned int,
58 std::vector<unsigned int> &,
60 {
62 }
63
64
65
66 template <>
67 void
69 const unsigned int,
70 std::vector<unsigned int> &,
73 {
75 }
76
77
78
79 template <>
80 void
82 const unsigned int,
83 std::vector<unsigned int> &,
85 {
87 }
88
89
90 template <>
91 void
93 const unsigned int,
94 std::vector<unsigned int> &,
97 {
99 }
100
101
102
103 // Template for 2d and 3d. For 1d see specialization above
104 template <int dim, int spacedim>
105 void
107 const unsigned int level,
108 std::vector<unsigned int> &row_lengths,
109 const DoFTools::Coupling flux_coupling)
110 {
111 Assert(row_lengths.size() == dofs.n_dofs(),
112 ExcDimensionMismatch(row_lengths.size(), dofs.n_dofs()));
113
114 // Function starts here by
115 // resetting the counters.
116 std::fill(row_lengths.begin(), row_lengths.end(), 0);
117
118 std::vector<bool> face_touched(dim == 2 ?
121
122 std::vector<types::global_dof_index> cell_indices;
123 std::vector<types::global_dof_index> neighbor_indices;
124
125 // We loop over cells and go from
126 // cells to lower dimensional
127 // objects. This is the only way to
128 // cope with the fact, that an
129 // unknown number of cells may
130 // share an object of dimension
131 // smaller than dim-1.
132 for (const auto &cell : dofs.cell_iterators_on_level(level))
133 {
134 const FiniteElement<dim> &fe = cell->get_fe();
135 cell_indices.resize(fe.n_dofs_per_cell());
136 cell->get_mg_dof_indices(cell_indices);
137 unsigned int i = 0;
138 // First, dofs on
139 // vertices. We assume that
140 // each vertex dof couples
141 // with all dofs on
142 // adjacent grid cells.
143
144 // Adding all dofs of the cells
145 // will add dofs of the faces
146 // of the cell adjacent to the
147 // vertex twice. Therefore, we
148 // subtract these here and add
149 // them in a loop over the
150 // faces below.
151
152 // in 1d, faces and vertices
153 // are identical. Nevertheless,
154 // this will only work if
155 // dofs_per_face is zero and
156 // n_dofs_per_vertex() is
157 // arbitrary, not the other way
158 // round.
159 // TODO: This assumes that the dofs per face on all faces coincide!
160 const unsigned int face_no = 0;
161
162 Assert(fe.reference_cell() == ReferenceCells::get_hypercube<dim>(),
164
165 unsigned int increment =
166 fe.n_dofs_per_cell() - dim * fe.n_dofs_per_face(face_no);
167 while (i < fe.get_first_line_index())
168 row_lengths[cell_indices[i++]] += increment;
169 // From now on, if an object is
170 // a cell, its dofs only couple
171 // inside the cell. Since the
172 // faces are handled below, we
173 // have to subtract ALL faces
174 // in this case.
175
176 // In all other cases we
177 // subtract adjacent faces to be
178 // added in the loop below.
179 increment =
180 (dim > 1) ?
181 fe.n_dofs_per_cell() - (dim - 1) * fe.n_dofs_per_face(face_no) :
182 fe.n_dofs_per_cell() -
184 while (i < fe.get_first_quad_index(face_no))
185 row_lengths[cell_indices[i++]] += increment;
186
187 // Now quads in 2d and 3d
188 increment =
189 (dim > 2) ?
190 fe.n_dofs_per_cell() - (dim - 2) * fe.n_dofs_per_face(face_no) :
191 fe.n_dofs_per_cell() -
193 while (i < fe.get_first_hex_index())
194 row_lengths[cell_indices[i++]] += increment;
195 // Finally, cells in 3d
197 fe.n_dofs_per_face(face_no);
198 while (i < fe.n_dofs_per_cell())
199 row_lengths[cell_indices[i++]] += increment;
200
201 // At this point, we have
202 // counted all dofs
203 // contributing from cells
204 // coupled topologically to the
205 // adjacent cells, but we
206 // subtracted some faces.
207
208 // Now, let's go by the faces
209 // and add the missing
210 // contribution as well as the
211 // flux contributions.
212 for (const unsigned int iface : GeometryInfo<dim>::face_indices())
213 {
214 bool level_boundary = cell->at_boundary(iface);
216 if (!level_boundary)
217 {
218 neighbor = cell->neighbor(iface);
219 if (static_cast<unsigned int>(neighbor->level()) != level)
220 level_boundary = true;
221 }
222
223 if (level_boundary)
224 {
225 for (unsigned int local_dof = 0;
226 local_dof < fe.n_dofs_per_cell();
227 ++local_dof)
228 row_lengths[cell_indices[local_dof]] +=
229 fe.n_dofs_per_face(face_no);
230 continue;
231 }
232
233 const FiniteElement<dim> &nfe = neighbor->get_fe();
235 cell->face(iface);
236
237 // Flux couplings are
238 // computed from both sides
239 // for simplicity.
240
241 // The dofs on the common face
242 // will be handled below,
243 // therefore, we subtract them
244 // here.
245 if (flux_coupling != DoFTools::none)
246 {
247 const unsigned int dof_increment =
248 nfe.n_dofs_per_cell() - nfe.n_dofs_per_face(face_no);
249 for (unsigned int local_dof = 0;
250 local_dof < fe.n_dofs_per_cell();
251 ++local_dof)
252 row_lengths[cell_indices[local_dof]] += dof_increment;
253 }
254
255 // Do this only once per
256 // face.
257 if (face_touched[face->index()])
258 continue;
259 face_touched[face->index()] = true;
260
261 // At this point, we assume
262 // that each cell added its
263 // dofs minus the face to
264 // the couplings of the
265 // face dofs. Since we
266 // subtracted two faces, we
267 // have to re-add one.
268
269 // If one side of the face
270 // is refined, all the fine
271 // face dofs couple with
272 // the coarse one.
273 neighbor_indices.resize(nfe.n_dofs_per_cell());
274 neighbor->get_mg_dof_indices(neighbor_indices);
275 for (unsigned int local_dof = 0; local_dof < fe.n_dofs_per_cell();
276 ++local_dof)
277 row_lengths[cell_indices[local_dof]] +=
278 nfe.n_dofs_per_face(face_no);
279 for (unsigned int local_dof = 0; local_dof < nfe.n_dofs_per_cell();
280 ++local_dof)
281 row_lengths[neighbor_indices[local_dof]] +=
282 fe.n_dofs_per_face(face_no);
283 }
284 }
285 }
286
287
288 // This is the template for 2d and 3d. See version for 1d above
289 template <int dim, int spacedim>
290 void
292 const unsigned int level,
293 std::vector<unsigned int> &row_lengths,
294 const Table<2, DoFTools::Coupling> &couplings,
295 const Table<2, DoFTools::Coupling> &flux_couplings)
296 {
297 Assert(row_lengths.size() == dofs.n_dofs(),
298 ExcDimensionMismatch(row_lengths.size(), dofs.n_dofs()));
299
300 // Function starts here by
301 // resetting the counters.
302 std::fill(row_lengths.begin(), row_lengths.end(), 0);
303
304 std::vector<bool> face_touched(dim == 2 ?
307
308 std::vector<types::global_dof_index> cell_indices;
309 std::vector<types::global_dof_index> neighbor_indices;
310
311 // We have to translate the
312 // couplings from components to
313 // blocks, so this works for
314 // nonprimitive elements as well.
315 std::vector<Table<2, DoFTools::Coupling>> couple_cell;
316 std::vector<Table<2, DoFTools::Coupling>> couple_face;
317 DoFTools::convert_couplings_to_blocks(dofs, couplings, couple_cell);
318 DoFTools::convert_couplings_to_blocks(dofs, flux_couplings, couple_face);
319
320 // We loop over cells and go from
321 // cells to lower dimensional
322 // objects. This is the only way to
323 // cope with the fact, that an
324 // unknown number of cells may
325 // share an object of dimension
326 // smaller than dim-1.
327 for (const auto &cell : dofs.cell_iterators_on_level(level))
328 {
329 const FiniteElement<dim> &fe = cell->get_fe();
330 const unsigned int fe_index = cell->active_fe_index();
331
332
333 // TODO: This assumes that the dofs per face on all faces coincide!
334 const unsigned int face_no = 0;
335 Assert(fe.reference_cell() == ReferenceCells::get_hypercube<dim>(),
337
338 Assert(couplings.n_rows() == fe.n_components(),
339 ExcDimensionMismatch(couplings.n_rows(), fe.n_components()));
340 Assert(couplings.n_cols() == fe.n_components(),
341 ExcDimensionMismatch(couplings.n_cols(), fe.n_components()));
342 Assert(flux_couplings.n_rows() == fe.n_components(),
343 ExcDimensionMismatch(flux_couplings.n_rows(),
344 fe.n_components()));
345 Assert(flux_couplings.n_cols() == fe.n_components(),
346 ExcDimensionMismatch(flux_couplings.n_cols(),
347 fe.n_components()));
348
349 cell_indices.resize(fe.n_dofs_per_cell());
350 cell->get_mg_dof_indices(cell_indices);
351 unsigned int i = 0;
352 // First, dofs on
353 // vertices. We assume that
354 // each vertex dof couples
355 // with all dofs on
356 // adjacent grid cells.
357
358 // Adding all dofs of the cells
359 // will add dofs of the faces
360 // of the cell adjacent to the
361 // vertex twice. Therefore, we
362 // subtract these here and add
363 // them in a loop over the
364 // faces below.
365
366 // in 1d, faces and vertices
367 // are identical. Nevertheless,
368 // this will only work if
369 // dofs_per_face is zero and
370 // n_dofs_per_vertex() is
371 // arbitrary, not the other way
372 // round.
373 unsigned int increment;
374 while (i < fe.get_first_line_index())
375 {
376 for (unsigned int base = 0; base < fe.n_base_elements(); ++base)
377 for (unsigned int mult = 0; mult < fe.element_multiplicity(base);
378 ++mult)
379 if (couple_cell[fe_index](fe.system_to_block_index(i).first,
380 fe.first_block_of_base(base) +
381 mult) != DoFTools::none)
382 {
383 increment =
384 fe.base_element(base).n_dofs_per_cell() -
385 dim * fe.base_element(base).n_dofs_per_face(face_no);
386 row_lengths[cell_indices[i]] += increment;
387 }
388 ++i;
389 }
390 // From now on, if an object is
391 // a cell, its dofs only couple
392 // inside the cell. Since the
393 // faces are handled below, we
394 // have to subtract ALL faces
395 // in this case.
396
397 // In all other cases we
398 // subtract adjacent faces to be
399 // added in the loop below.
400 while (i < fe.get_first_quad_index(face_no))
401 {
402 for (unsigned int base = 0; base < fe.n_base_elements(); ++base)
403 for (unsigned int mult = 0; mult < fe.element_multiplicity(base);
404 ++mult)
405 if (couple_cell[fe_index](fe.system_to_block_index(i).first,
406 fe.first_block_of_base(base) +
407 mult) != DoFTools::none)
408 {
409 increment =
410 fe.base_element(base).n_dofs_per_cell() -
411 ((dim > 1) ? (dim - 1) :
413 fe.base_element(base).n_dofs_per_face(face_no);
414 row_lengths[cell_indices[i]] += increment;
415 }
416 ++i;
417 }
418
419 // Now quads in 2d and 3d
420 while (i < fe.get_first_hex_index())
421 {
422 for (unsigned int base = 0; base < fe.n_base_elements(); ++base)
423 for (unsigned int mult = 0; mult < fe.element_multiplicity(base);
424 ++mult)
425 if (couple_cell[fe_index](fe.system_to_block_index(i).first,
426 fe.first_block_of_base(base) +
427 mult) != DoFTools::none)
428 {
429 increment =
430 fe.base_element(base).n_dofs_per_cell() -
431 ((dim > 2) ? (dim - 2) :
433 fe.base_element(base).n_dofs_per_face(face_no);
434 row_lengths[cell_indices[i]] += increment;
435 }
436 ++i;
437 }
438
439 // Finally, cells in 3d
440 while (i < fe.n_dofs_per_cell())
441 {
442 for (unsigned int base = 0; base < fe.n_base_elements(); ++base)
443 for (unsigned int mult = 0; mult < fe.element_multiplicity(base);
444 ++mult)
445 if (couple_cell[fe_index](fe.system_to_block_index(i).first,
446 fe.first_block_of_base(base) +
447 mult) != DoFTools::none)
448 {
449 increment =
450 fe.base_element(base).n_dofs_per_cell() -
452 fe.base_element(base).n_dofs_per_face(face_no);
453 row_lengths[cell_indices[i]] += increment;
454 }
455 ++i;
456 }
457
458 // At this point, we have
459 // counted all dofs
460 // contributing from cells
461 // coupled topologically to the
462 // adjacent cells, but we
463 // subtracted some faces.
464
465 // Now, let's go by the faces
466 // and add the missing
467 // contribution as well as the
468 // flux contributions.
469 for (const unsigned int iface : GeometryInfo<dim>::face_indices())
470 {
471 bool level_boundary = cell->at_boundary(iface);
473 if (!level_boundary)
474 {
475 neighbor = cell->neighbor(iface);
476 if (static_cast<unsigned int>(neighbor->level()) != level)
477 level_boundary = true;
478 }
479
480 if (level_boundary)
481 {
482 for (unsigned int local_dof = 0;
483 local_dof < fe.n_dofs_per_cell();
484 ++local_dof)
485 row_lengths[cell_indices[local_dof]] +=
486 fe.n_dofs_per_face(face_no);
487 continue;
488 }
489
490 const FiniteElement<dim> &nfe = neighbor->get_fe();
492 cell->face(iface);
493
494 // Flux couplings are
495 // computed from both sides
496 // for simplicity.
497
498 // The dofs on the common face
499 // will be handled below,
500 // therefore, we subtract them
501 // here.
502 for (unsigned int base = 0; base < nfe.n_base_elements(); ++base)
503 for (unsigned int mult = 0; mult < nfe.element_multiplicity(base);
504 ++mult)
505 for (unsigned int local_dof = 0;
506 local_dof < fe.n_dofs_per_cell();
507 ++local_dof)
508 if (couple_face[fe_index](
509 fe.system_to_block_index(local_dof).first,
510 nfe.first_block_of_base(base) + mult) != DoFTools::none)
511 {
512 const unsigned int dof_increment =
513 nfe.base_element(base).n_dofs_per_cell() -
514 nfe.base_element(base).n_dofs_per_face(face_no);
515 row_lengths[cell_indices[local_dof]] += dof_increment;
516 }
517
518 // Do this only once per face and not on the hanging faces.
519 if (face_touched[face->index()])
520 continue;
521 face_touched[face->index()] = true;
522
523 // At this point, we assume
524 // that each cell added its
525 // dofs minus the face to
526 // the couplings of the
527 // face dofs. Since we
528 // subtracted two faces, we
529 // have to re-add one.
530
531 // If one side of the face
532 // is refined, all the fine
533 // face dofs couple with
534 // the coarse one.
535
536 // Wolfgang, do they couple
537 // with each other by
538 // constraints?
539
540 // This will not work with
541 // different couplings on
542 // different cells.
543 neighbor_indices.resize(nfe.n_dofs_per_cell());
544 neighbor->get_mg_dof_indices(neighbor_indices);
545 for (unsigned int base = 0; base < nfe.n_base_elements(); ++base)
546 for (unsigned int mult = 0; mult < nfe.element_multiplicity(base);
547 ++mult)
548 for (unsigned int local_dof = 0;
549 local_dof < fe.n_dofs_per_cell();
550 ++local_dof)
551 if (couple_cell[fe_index](
552 fe.system_to_component_index(local_dof).first,
553 nfe.first_block_of_base(base) + mult) != DoFTools::none)
554 row_lengths[cell_indices[local_dof]] +=
555 nfe.base_element(base).n_dofs_per_face(face_no);
556 for (unsigned int base = 0; base < fe.n_base_elements(); ++base)
557 for (unsigned int mult = 0; mult < fe.element_multiplicity(base);
558 ++mult)
559 for (unsigned int local_dof = 0;
560 local_dof < nfe.n_dofs_per_cell();
561 ++local_dof)
562 if (couple_cell[fe_index](
563 nfe.system_to_component_index(local_dof).first,
564 fe.first_block_of_base(base) + mult) != DoFTools::none)
565 row_lengths[neighbor_indices[local_dof]] +=
566 fe.base_element(base).n_dofs_per_face(face_no);
567 }
568 }
569 }
570
571
572
573 template <int dim, int spacedim, typename number>
574 void
576 SparsityPatternBase &sparsity,
577 const unsigned int level,
578 const AffineConstraints<number> &constraints,
579 const bool keep_constrained_dofs)
580 {
581 const types::global_dof_index n_dofs = dof.n_dofs(level);
582
583 Assert(sparsity.n_rows() == n_dofs,
584 ExcDimensionMismatch(sparsity.n_rows(), n_dofs));
585 Assert(sparsity.n_cols() == n_dofs,
586 ExcDimensionMismatch(sparsity.n_cols(), n_dofs));
587
588 const unsigned int dofs_per_cell = dof.get_fe().n_dofs_per_cell();
589 std::vector<types::global_dof_index> dofs_on_this_cell(dofs_per_cell);
590 for (const auto &cell : dof.cell_iterators_on_level(level))
591 if (cell->is_locally_owned_on_level())
592 {
593 cell->get_mg_dof_indices(dofs_on_this_cell);
594 constraints.add_entries_local_to_global(dofs_on_this_cell,
595 sparsity,
596 keep_constrained_dofs);
597 }
598 }
599
600
601
602 template <int dim, int spacedim, typename number>
603 void
605 SparsityPatternBase &sparsity,
606 const unsigned int level,
607 const AffineConstraints<number> &constraints,
608 const bool keep_constrained_dofs)
609 {
610 const types::global_dof_index n_dofs = dof.n_dofs(level);
611
612 Assert(sparsity.n_rows() == n_dofs,
613 ExcDimensionMismatch(sparsity.n_rows(), n_dofs));
614 Assert(sparsity.n_cols() == n_dofs,
615 ExcDimensionMismatch(sparsity.n_cols(), n_dofs));
616
617 const unsigned int dofs_per_cell = dof.get_fe().n_dofs_per_cell();
618 std::vector<types::global_dof_index> dofs_on_this_cell(dofs_per_cell);
619 std::vector<types::global_dof_index> dofs_on_other_cell(dofs_per_cell);
620 for (const auto &cell : dof.cell_iterators_on_level(level))
621 {
622 if (!cell->is_locally_owned_on_level())
623 continue;
624
625 cell->get_mg_dof_indices(dofs_on_this_cell);
626 // make sparsity pattern for this cell
627 constraints.add_entries_local_to_global(dofs_on_this_cell,
628 sparsity,
629 keep_constrained_dofs);
630
631 // Loop over all interior neighbors
632 for (const unsigned int face : GeometryInfo<dim>::face_indices())
633 {
634 bool use_face = false;
635 if ((!cell->at_boundary(face)) &&
636 (static_cast<unsigned int>(cell->neighbor_level(face)) ==
637 level))
638 use_face = true;
639 else if (cell->has_periodic_neighbor(face) &&
640 (static_cast<unsigned int>(
641 cell->periodic_neighbor_level(face)) == level))
642 use_face = true;
643
644 if (use_face)
645 {
647 cell->neighbor_or_periodic_neighbor(face);
648 neighbor->get_mg_dof_indices(dofs_on_other_cell);
649 // only add one direction The other is taken care of by
650 // neighbor (except when the neighbor is not owned by the same
651 // processor)
652 constraints.add_entries_local_to_global(dofs_on_this_cell,
653 dofs_on_other_cell,
654 sparsity,
655 keep_constrained_dofs);
656
657 if (neighbor->is_locally_owned_on_level() == false)
658 {
659 constraints.add_entries_local_to_global(
660 dofs_on_other_cell, sparsity, keep_constrained_dofs);
661
662 constraints.add_entries_local_to_global(
663 dofs_on_other_cell,
664 dofs_on_this_cell,
665 sparsity,
666 keep_constrained_dofs);
667 }
668 }
669 }
670 }
671 }
672
673
674
675 template <int dim, int spacedim>
676 void
678 SparsityPatternBase &sparsity,
679 const unsigned int level)
680 {
681 Assert((level >= 1) && (level < dof.get_triangulation().n_global_levels()),
683
684 const types::global_dof_index fine_dofs = dof.n_dofs(level);
685 const types::global_dof_index coarse_dofs = dof.n_dofs(level - 1);
686
687 // Matrix maps from fine level to coarse level
688 Assert(sparsity.n_rows() == coarse_dofs,
689 ExcDimensionMismatch(sparsity.n_rows(), coarse_dofs));
690 Assert(sparsity.n_cols() == fine_dofs,
691 ExcDimensionMismatch(sparsity.n_cols(), fine_dofs));
692
693 const unsigned int dofs_per_cell = dof.get_fe().n_dofs_per_cell();
694 std::vector<types::global_dof_index> dofs_on_this_cell(dofs_per_cell);
695 std::vector<types::global_dof_index> dofs_on_other_cell(dofs_per_cell);
696 for (const auto &cell : dof.cell_iterators_on_level(level))
697 {
698 if (!cell->is_locally_owned_on_level())
699 continue;
700
701 cell->get_mg_dof_indices(dofs_on_this_cell);
702 // Loop over all interior neighbors
703 for (const unsigned int face : GeometryInfo<dim>::face_indices())
704 {
705 // Neighbor is coarser
706 bool use_face = false;
707 if ((!cell->at_boundary(face)) &&
708 (static_cast<unsigned int>(cell->neighbor_level(face)) !=
709 level))
710 use_face = true;
711 else if (cell->has_periodic_neighbor(face) &&
712 (static_cast<unsigned int>(
713 cell->periodic_neighbor_level(face)) != level))
714 use_face = true;
715
716 if (use_face)
717 {
719 cell->neighbor_or_periodic_neighbor(face);
720 neighbor->get_mg_dof_indices(dofs_on_other_cell);
721
722 std::sort(dofs_on_this_cell.begin(), dofs_on_this_cell.end());
723 for (unsigned int i = 0; i < dofs_per_cell; ++i)
724 sparsity.add_row_entries(dofs_on_other_cell[i],
725 make_array_view(dofs_on_this_cell),
726 true);
727 }
728 }
729 }
730 }
731
732
733
734 template <int dim, int spacedim>
735 void
737 SparsityPatternBase &sparsity,
738 const unsigned int level,
739 const Table<2, DoFTools::Coupling> &int_mask,
740 const Table<2, DoFTools::Coupling> &flux_mask)
741 {
742 const FiniteElement<dim> &fe = dof.get_fe();
743 const types::global_dof_index n_dofs = dof.n_dofs(level);
744 const unsigned int n_comp = fe.n_components();
745
746 Assert(sparsity.n_rows() == n_dofs,
747 ExcDimensionMismatch(sparsity.n_rows(), n_dofs));
748 Assert(sparsity.n_cols() == n_dofs,
749 ExcDimensionMismatch(sparsity.n_cols(), n_dofs));
750 Assert(int_mask.n_rows() == n_comp,
751 ExcDimensionMismatch(int_mask.n_rows(), n_comp));
752 Assert(int_mask.n_cols() == n_comp,
753 ExcDimensionMismatch(int_mask.n_cols(), n_comp));
754 Assert(flux_mask.n_rows() == n_comp,
755 ExcDimensionMismatch(flux_mask.n_rows(), n_comp));
756 Assert(flux_mask.n_cols() == n_comp,
757 ExcDimensionMismatch(flux_mask.n_cols(), n_comp));
758
759 const unsigned int total_dofs = fe.n_dofs_per_cell();
760 std::vector<types::global_dof_index> dofs_on_this_cell(total_dofs);
761 std::vector<types::global_dof_index> dofs_on_other_cell(total_dofs);
762 std::vector<std::pair<types::global_dof_index, types::global_dof_index>>
763 cell_entries;
764
765 Table<2, bool> support_on_face(total_dofs,
767
769 int_dof_mask =
771 flux_dof_mask =
773
774 for (unsigned int i = 0; i < total_dofs; ++i)
775 for (auto f : GeometryInfo<dim>::face_indices())
776 support_on_face(i, f) = fe.has_support_on_face(i, f);
777
778 std::vector<bool> face_touched(dim == 2 ?
781
782 for (const auto &cell : dof.cell_iterators_on_level(level))
783 {
784 if (!cell->is_locally_owned_on_level())
785 continue;
786
787 cell->get_mg_dof_indices(dofs_on_this_cell);
788 // make sparsity pattern for this cell
789 for (unsigned int i = 0; i < total_dofs; ++i)
790 for (unsigned int j = 0; j < total_dofs; ++j)
791 if (int_dof_mask[i][j] != DoFTools::none)
792 cell_entries.emplace_back(dofs_on_this_cell[i],
793 dofs_on_this_cell[j]);
794
795 // Loop over all interior neighbors
796 for (const unsigned int face : GeometryInfo<dim>::face_indices())
797 {
799 cell->face(face);
800 if (face_touched[cell_face->index()])
801 continue;
802
803 if (cell->at_boundary(face) && !cell->has_periodic_neighbor(face))
804 {
805 for (unsigned int i = 0; i < total_dofs; ++i)
806 {
807 const bool i_non_zero_i = support_on_face(i, face);
808 for (unsigned int j = 0; j < total_dofs; ++j)
809 {
810 const bool j_non_zero_i = support_on_face(j, face);
811
812 if (flux_dof_mask(i, j) == DoFTools::always)
813 cell_entries.emplace_back(dofs_on_this_cell[i],
814 dofs_on_this_cell[j]);
815 if (flux_dof_mask(i, j) == DoFTools::nonzero &&
816 i_non_zero_i && j_non_zero_i)
817 cell_entries.emplace_back(dofs_on_this_cell[i],
818 dofs_on_this_cell[j]);
819 }
820 }
821 }
822 else
823 {
825 cell->neighbor_or_periodic_neighbor(face);
826
827 if (neighbor->level() < cell->level())
828 continue;
829
830 unsigned int neighbor_face =
831 cell->has_periodic_neighbor(face) ?
832 cell->periodic_neighbor_of_periodic_neighbor(face) :
833 cell->neighbor_of_neighbor(face);
834
835 neighbor->get_mg_dof_indices(dofs_on_other_cell);
836 for (unsigned int i = 0; i < total_dofs; ++i)
837 {
838 const bool i_non_zero_i = support_on_face(i, face);
839 const bool i_non_zero_e = support_on_face(i, neighbor_face);
840 for (unsigned int j = 0; j < total_dofs; ++j)
841 {
842 const bool j_non_zero_i = support_on_face(j, face);
843 const bool j_non_zero_e =
844 support_on_face(j, neighbor_face);
845 if (flux_dof_mask(i, j) == DoFTools::always)
846 {
847 cell_entries.emplace_back(dofs_on_this_cell[i],
848 dofs_on_other_cell[j]);
849 cell_entries.emplace_back(dofs_on_other_cell[i],
850 dofs_on_this_cell[j]);
851 cell_entries.emplace_back(dofs_on_this_cell[i],
852 dofs_on_this_cell[j]);
853 cell_entries.emplace_back(dofs_on_other_cell[i],
854 dofs_on_other_cell[j]);
855 }
856 if (flux_dof_mask(i, j) == DoFTools::nonzero)
857 {
858 if (i_non_zero_i && j_non_zero_e)
859 cell_entries.emplace_back(dofs_on_this_cell[i],
860 dofs_on_other_cell[j]);
861 if (i_non_zero_e && j_non_zero_i)
862 cell_entries.emplace_back(dofs_on_other_cell[i],
863 dofs_on_this_cell[j]);
864 if (i_non_zero_i && j_non_zero_i)
865 cell_entries.emplace_back(dofs_on_this_cell[i],
866 dofs_on_this_cell[j]);
867 if (i_non_zero_e && j_non_zero_e)
868 cell_entries.emplace_back(dofs_on_other_cell[i],
869 dofs_on_other_cell[j]);
870 }
871
872 if (flux_dof_mask(j, i) == DoFTools::always)
873 {
874 cell_entries.emplace_back(dofs_on_this_cell[j],
875 dofs_on_other_cell[i]);
876 cell_entries.emplace_back(dofs_on_other_cell[j],
877 dofs_on_this_cell[i]);
878 cell_entries.emplace_back(dofs_on_this_cell[j],
879 dofs_on_this_cell[i]);
880 cell_entries.emplace_back(dofs_on_other_cell[j],
881 dofs_on_other_cell[i]);
882 }
883 if (flux_dof_mask(j, i) == DoFTools::nonzero)
884 {
885 if (j_non_zero_i && i_non_zero_e)
886 cell_entries.emplace_back(dofs_on_this_cell[j],
887 dofs_on_other_cell[i]);
888 if (j_non_zero_e && i_non_zero_i)
889 cell_entries.emplace_back(dofs_on_other_cell[j],
890 dofs_on_this_cell[i]);
891 if (j_non_zero_i && i_non_zero_i)
892 cell_entries.emplace_back(dofs_on_this_cell[j],
893 dofs_on_this_cell[i]);
894 if (j_non_zero_e && i_non_zero_e)
895 cell_entries.emplace_back(dofs_on_other_cell[j],
896 dofs_on_other_cell[i]);
897 }
898 }
899 }
900 face_touched[neighbor->face(neighbor_face)->index()] = true;
901 }
902 }
903 sparsity.add_entries(make_array_view(cell_entries));
904 cell_entries.clear();
905 }
906 }
907
908
909
910 template <int dim, int spacedim>
911 void
913 SparsityPatternBase &sparsity,
914 const unsigned int level,
915 const Table<2, DoFTools::Coupling> &flux_mask)
916 {
917 const FiniteElement<dim> &fe = dof.get_fe();
918 const unsigned int n_comp = fe.n_components();
919 (void)n_comp;
920
921 Assert((level >= 1) && (level < dof.get_triangulation().n_global_levels()),
923
924 const types::global_dof_index fine_dofs = dof.n_dofs(level);
925 const types::global_dof_index coarse_dofs = dof.n_dofs(level - 1);
926
927 // Matrix maps from fine level to coarse level
928 Assert(sparsity.n_rows() == coarse_dofs,
929 ExcDimensionMismatch(sparsity.n_rows(), coarse_dofs));
930 Assert(sparsity.n_cols() == fine_dofs,
931 ExcDimensionMismatch(sparsity.n_cols(), fine_dofs));
932 Assert(flux_mask.n_rows() == n_comp,
933 ExcDimensionMismatch(flux_mask.n_rows(), n_comp));
934 Assert(flux_mask.n_cols() == n_comp,
935 ExcDimensionMismatch(flux_mask.n_cols(), n_comp));
936
937 const unsigned int dofs_per_cell = dof.get_fe().n_dofs_per_cell();
938 std::vector<types::global_dof_index> dofs_on_this_cell(dofs_per_cell);
939 std::vector<types::global_dof_index> dofs_on_other_cell(dofs_per_cell);
940 std::vector<std::pair<types::global_dof_index, types::global_dof_index>>
941 cell_entries;
942
943 Table<2, bool> support_on_face(dofs_per_cell,
945
946 const Table<2, DoFTools::Coupling> flux_dof_mask =
948
949 for (unsigned int i = 0; i < dofs_per_cell; ++i)
950 for (auto f : GeometryInfo<dim>::face_indices())
951 support_on_face(i, f) = fe.has_support_on_face(i, f);
952
953 for (const auto &cell : dof.cell_iterators_on_level(level))
954 {
955 if (!cell->is_locally_owned_on_level())
956 continue;
957
958 cell->get_mg_dof_indices(dofs_on_this_cell);
959 // Loop over all interior neighbors
960 for (const unsigned int face : GeometryInfo<dim>::face_indices())
961 {
962 // Neighbor is coarser
963 bool use_face = false;
964 if ((!cell->at_boundary(face)) &&
965 (static_cast<unsigned int>(cell->neighbor_level(face)) !=
966 level))
967 use_face = true;
968 else if (cell->has_periodic_neighbor(face) &&
969 (static_cast<unsigned int>(
970 cell->periodic_neighbor_level(face)) != level))
971 use_face = true;
972
973 if (use_face)
974 {
976 cell->neighbor_or_periodic_neighbor(face);
977 neighbor->get_mg_dof_indices(dofs_on_other_cell);
978
979 for (unsigned int i = 0; i < dofs_per_cell; ++i)
980 {
981 for (unsigned int j = 0; j < dofs_per_cell; ++j)
982 {
983 if (flux_dof_mask(i, j) != DoFTools::none)
984 {
985 cell_entries.emplace_back(dofs_on_other_cell[i],
986 dofs_on_this_cell[j]);
987 cell_entries.emplace_back(dofs_on_other_cell[j],
988 dofs_on_this_cell[i]);
989 }
990 }
991 }
992 }
993 }
994 sparsity.add_entries(make_array_view(cell_entries));
995 cell_entries.clear();
996 }
997 }
998
999
1000
1001 template <int dim, int spacedim>
1002 void
1004 const MGConstrainedDoFs &mg_constrained_dofs,
1005 SparsityPatternBase &sparsity,
1006 const unsigned int level)
1007 {
1008 const types::global_dof_index n_dofs = dof.n_dofs(level);
1009 Assert(sparsity.n_rows() == n_dofs,
1010 ExcDimensionMismatch(sparsity.n_rows(), n_dofs));
1011 Assert(sparsity.n_cols() == n_dofs,
1012 ExcDimensionMismatch(sparsity.n_cols(), n_dofs));
1013
1014 const unsigned int dofs_per_cell = dof.get_fe().n_dofs_per_cell();
1015 std::vector<types::global_dof_index> dofs_on_this_cell(dofs_per_cell);
1016 std::vector<types::global_dof_index> cols;
1017 cols.reserve(dofs_per_cell);
1018
1019 for (const auto &cell : dof.cell_iterators_on_level(level))
1020 if (cell->is_locally_owned_on_level())
1021 {
1022 cell->get_mg_dof_indices(dofs_on_this_cell);
1023 std::sort(dofs_on_this_cell.begin(), dofs_on_this_cell.end());
1024 for (unsigned int i = 0; i < dofs_per_cell; ++i)
1025 {
1026 for (unsigned int j = 0; j < dofs_per_cell; ++j)
1027 if (mg_constrained_dofs.is_interface_matrix_entry(
1028 level, dofs_on_this_cell[i], dofs_on_this_cell[j]))
1029 cols.push_back(dofs_on_this_cell[j]);
1030 sparsity.add_row_entries(dofs_on_this_cell[i],
1031 make_array_view(cols),
1032 true);
1033 cols.clear();
1034 }
1035 }
1036 }
1037
1038
1039
1040 template <int dim, int spacedim>
1041 void
1043 const DoFHandler<dim, spacedim> &dof_handler,
1044 std::vector<std::vector<types::global_dof_index>> &result,
1045 bool only_once,
1046 std::vector<unsigned int> target_component)
1047 {
1048 const FiniteElement<dim> &fe = dof_handler.get_fe();
1049 const unsigned int n_components = fe.n_components();
1050 const unsigned int nlevels =
1051 dof_handler.get_triangulation().n_global_levels();
1052
1053 Assert(result.size() == nlevels,
1054 ExcDimensionMismatch(result.size(), nlevels));
1055
1056 if (target_component.empty())
1057 {
1058 target_component.resize(n_components);
1059 for (unsigned int i = 0; i < n_components; ++i)
1060 target_component[i] = i;
1061 }
1062
1063 Assert(target_component.size() == n_components,
1064 ExcDimensionMismatch(target_component.size(), n_components));
1065
1066 for (unsigned int l = 0; l < nlevels; ++l)
1067 {
1068 result[l].resize(n_components);
1069 std::fill(result[l].begin(), result[l].end(), 0U);
1070
1071 // special case for only one
1072 // component. treat this first
1073 // since it does not require any
1074 // computations
1075 if (n_components == 1)
1076 {
1077 result[l][0] = dof_handler.n_dofs(l);
1078 }
1079 else
1080 {
1081 // otherwise determine the number
1082 // of dofs in each component
1083 // separately. do so in parallel
1084 std::vector<std::vector<bool>> dofs_in_component(
1085 n_components, std::vector<bool>(dof_handler.n_dofs(l), false));
1086 std::vector<ComponentMask> component_select(n_components);
1088 for (unsigned int i = 0; i < n_components; ++i)
1089 {
1090 void (*fun_ptr)(const unsigned int level,
1092 const ComponentMask &,
1093 std::vector<bool> &) =
1094 &DoFTools::extract_level_dofs<dim, spacedim>;
1095
1096 std::vector<bool> tmp(n_components, false);
1097 tmp[i] = true;
1098 component_select[i] = ComponentMask(tmp);
1099
1100 tasks += Threads::new_task(fun_ptr,
1101 l,
1102 dof_handler,
1103 component_select[i],
1104 dofs_in_component[i]);
1105 }
1106 tasks.join_all();
1107
1108 // next count what we got
1109 unsigned int component = 0;
1110 for (unsigned int b = 0; b < fe.n_base_elements(); ++b)
1111 {
1112 const FiniteElement<dim> &base = fe.base_element(b);
1113 // Dimension of base element
1114 unsigned int d = base.n_components();
1115
1116 for (unsigned int m = 0; m < fe.element_multiplicity(b); ++m)
1117 {
1118 for (unsigned int dd = 0; dd < d; ++dd)
1119 {
1120 if (base.is_primitive() || (!only_once || dd == 0))
1121 result[l][target_component[component]] +=
1122 std::count(dofs_in_component[component].begin(),
1123 dofs_in_component[component].end(),
1124 true);
1125 ++component;
1126 }
1127 }
1128 }
1129 // finally sanity check
1130 Assert(!dof_handler.get_fe().is_primitive() ||
1131 std::accumulate(result[l].begin(),
1132 result[l].end(),
1134 dof_handler.n_dofs(l),
1136 }
1137 }
1138 }
1139
1140
1141
1142 template <int dim, int spacedim>
1143 void
1145 const DoFHandler<dim, spacedim> &dof_handler,
1146 std::vector<std::vector<types::global_dof_index>> &dofs_per_block,
1147 std::vector<unsigned int> target_block)
1148 {
1149 const FiniteElement<dim, spacedim> &fe = dof_handler.get_fe();
1150 const unsigned int n_blocks = fe.n_blocks();
1151 const unsigned int n_levels =
1152 dof_handler.get_triangulation().n_global_levels();
1153
1154 AssertDimension(dofs_per_block.size(), n_levels);
1155
1156 for (unsigned int l = 0; l < n_levels; ++l)
1157 std::fill(dofs_per_block[l].begin(), dofs_per_block[l].end(), 0U);
1158 // If the empty vector was given as
1159 // default argument, set up this
1160 // vector as identity.
1161 if (target_block.empty())
1162 {
1163 target_block.resize(n_blocks);
1164 for (unsigned int i = 0; i < n_blocks; ++i)
1165 target_block[i] = i;
1166 }
1167 Assert(target_block.size() == n_blocks,
1168 ExcDimensionMismatch(target_block.size(), n_blocks));
1169
1170 const unsigned int max_block =
1171 *std::max_element(target_block.begin(), target_block.end());
1172 const unsigned int n_target_blocks = max_block + 1;
1173 (void)n_target_blocks;
1174
1175 for (unsigned int l = 0; l < n_levels; ++l)
1176 AssertDimension(dofs_per_block[l].size(), n_target_blocks);
1177
1178 // special case for only one
1179 // block. treat this first
1180 // since it does not require any
1181 // computations
1182 if (n_blocks == 1)
1183 {
1184 for (unsigned int l = 0; l < n_levels; ++l)
1185 dofs_per_block[l][0] = dof_handler.n_dofs(l);
1186 return;
1187 }
1188 // otherwise determine the number
1189 // of dofs in each block
1190 // separately. do so in parallel
1191 for (unsigned int l = 0; l < n_levels; ++l)
1192 {
1193 std::vector<std::vector<bool>> dofs_in_block(
1194 n_blocks, std::vector<bool>(dof_handler.n_dofs(l), false));
1195 std::vector<BlockMask> block_select(n_blocks);
1197 for (unsigned int i = 0; i < n_blocks; ++i)
1198 {
1199 void (*fun_ptr)(const unsigned int level,
1201 const BlockMask &,
1202 std::vector<bool> &) =
1203 &DoFTools::extract_level_dofs<dim, spacedim>;
1204
1205 std::vector<bool> tmp(n_blocks, false);
1206 tmp[i] = true;
1207 block_select[i] = BlockMask(tmp);
1208
1209 tasks += Threads::new_task(
1210 fun_ptr, l, dof_handler, block_select[i], dofs_in_block[i]);
1211 }
1212 tasks.join_all();
1213
1214 // next count what we got
1215 for (unsigned int block = 0; block < fe.n_blocks(); ++block)
1216 dofs_per_block[l][target_block[block]] +=
1217 std::count(dofs_in_block[block].begin(),
1218 dofs_in_block[block].end(),
1219 true);
1220 }
1221 }
1222
1223
1224
1225 template <int dim, int spacedim>
1226 void
1228 const DoFHandler<dim, spacedim> &dof,
1229 const std::map<types::boundary_id, const Function<spacedim> *>
1230 &function_map,
1231 std::vector<std::set<types::global_dof_index>> &boundary_indices,
1232 const ComponentMask &component_mask)
1233 {
1234 Assert(boundary_indices.size() == dof.get_triangulation().n_global_levels(),
1235 ExcDimensionMismatch(boundary_indices.size(),
1237
1238 std::set<types::boundary_id> boundary_ids;
1239 for (const auto &boundary_function : function_map)
1240 boundary_ids.insert(boundary_function.first);
1241
1242 std::vector<IndexSet> boundary_indexset;
1243 make_boundary_list(dof, boundary_ids, boundary_indexset, component_mask);
1244 for (unsigned int i = 0; i < dof.get_triangulation().n_global_levels(); ++i)
1245 boundary_indices[i].insert(boundary_indexset[i].begin(),
1246 boundary_indexset[i].end());
1247 }
1248
1249
1250 template <int dim, int spacedim>
1251 void
1253 const std::map<types::boundary_id,
1254 const Function<spacedim> *> &function_map,
1255 std::vector<IndexSet> &boundary_indices,
1256 const ComponentMask &component_mask)
1257 {
1258 Assert(boundary_indices.size() == dof.get_triangulation().n_global_levels(),
1259 ExcDimensionMismatch(boundary_indices.size(),
1261
1262 std::set<types::boundary_id> boundary_ids;
1263 for (const auto &boundary_function : function_map)
1264 boundary_ids.insert(boundary_function.first);
1265
1266 make_boundary_list(dof, boundary_ids, boundary_indices, component_mask);
1267 }
1268
1269
1270
1271 template <int dim, int spacedim>
1272 void
1274 const std::set<types::boundary_id> &boundary_ids,
1275 std::vector<IndexSet> &boundary_indices,
1276 const ComponentMask &component_mask)
1277 {
1278 boundary_indices.resize(dof.get_triangulation().n_global_levels());
1279
1280 // if for whatever reason we were passed an empty set, return immediately
1281 if (boundary_ids.empty())
1282 return;
1283
1284 for (unsigned int i = 0; i < dof.get_triangulation().n_global_levels(); ++i)
1285 if (boundary_indices[i].size() == 0)
1286 boundary_indices[i] = IndexSet(dof.n_dofs(i));
1287
1288 const unsigned int n_components = dof.get_fe_collection().n_components();
1289 const bool fe_is_system = (n_components != 1);
1290
1291 std::vector<types::global_dof_index> local_dofs;
1292 local_dofs.reserve(dof.get_fe_collection().max_dofs_per_face());
1293 std::fill(local_dofs.begin(), local_dofs.end(), numbers::invalid_dof_index);
1294
1295 std::vector<std::vector<types::global_dof_index>> dofs_by_level(
1296 dof.get_triangulation().n_levels());
1297
1298 // First, deal with the simpler case when we have to identify all boundary
1299 // dofs
1300 if (component_mask.n_selected_components(n_components) == n_components)
1301 {
1302 for (const auto &cell : dof.cell_iterators())
1303 {
1304 if (cell->is_artificial_on_level())
1305 continue;
1306 const FiniteElement<dim> &fe = cell->get_fe();
1307 const unsigned int level = cell->level();
1308
1309 for (const unsigned int face_no : GeometryInfo<dim>::face_indices())
1310 if (cell->at_boundary(face_no) == true)
1311 {
1312 const typename DoFHandler<dim, spacedim>::face_iterator face =
1313 cell->face(face_no);
1314 const types::boundary_id bi = face->boundary_id();
1315 // Face is listed in boundary map
1316 if (boundary_ids.find(bi) != boundary_ids.end())
1317 {
1318 local_dofs.resize(fe.n_dofs_per_face(face_no));
1319 face->get_mg_dof_indices(level, local_dofs);
1320 dofs_by_level[level].insert(dofs_by_level[level].end(),
1321 local_dofs.begin(),
1322 local_dofs.end());
1323 }
1324 }
1325 }
1326 }
1327 else
1328 {
1329 Assert(component_mask.n_selected_components(n_components) > 0,
1330 ExcMessage(
1331 "It's probably worthwhile to select at least one component."));
1332
1333 for (const auto &cell : dof.cell_iterators())
1334 if (!cell->is_artificial_on_level())
1335 for (const unsigned int face_no : GeometryInfo<dim>::face_indices())
1336 {
1337 if (cell->at_boundary(face_no) == false)
1338 continue;
1339
1340 const FiniteElement<dim> &fe = cell->get_fe();
1341 const unsigned int level = cell->level();
1342
1344 cell->face(face_no);
1345 const types::boundary_id boundary_component =
1346 face->boundary_id();
1347 if (boundary_ids.find(boundary_component) != boundary_ids.end())
1348 // we want to constrain this boundary
1349 {
1350 for (unsigned int i = 0;
1351 i < cell->get_fe().n_dofs_per_cell();
1352 ++i)
1353 {
1354 const ComponentMask &nonzero_component_array =
1355 cell->get_fe().get_nonzero_components(i);
1356 // if we want to constrain one of the nonzero
1357 // components, we have to constrain all of them
1358
1359 bool selected = false;
1360 for (unsigned int c = 0; c < n_components; ++c)
1361 if (nonzero_component_array[c] == true &&
1362 component_mask[c] == true)
1363 {
1364 selected = true;
1365 break;
1366 }
1367 if (selected)
1368 for (unsigned int c = 0; c < n_components; ++c)
1369 Assert(
1370 nonzero_component_array[c] == false ||
1371 component_mask[c] == true,
1372 ExcMessage(
1373 "You are using a non-primitive FiniteElement "
1374 "and try to constrain just some of its components!"));
1375 }
1376
1377 // get indices, physical location and boundary values of
1378 // dofs on this face
1379 local_dofs.resize(fe.n_dofs_per_face(face_no));
1380 face->get_mg_dof_indices(level, local_dofs);
1381 if (fe_is_system)
1382 {
1383 for (unsigned int i = 0; i < local_dofs.size(); ++i)
1384 {
1385 unsigned int component =
1387 if (fe.is_primitive())
1388 component =
1389 fe.face_system_to_component_index(i, face_no)
1390 .first;
1391 else
1392 {
1393 // Just pick the first of the components
1394 // We already know that either all or none
1395 // of the components are selected
1396 const ComponentMask &nonzero_component_array =
1397 cell->get_fe().get_nonzero_components(i);
1398 for (unsigned int c = 0; c < n_components; ++c)
1399 if (nonzero_component_array[c] == true)
1400 {
1401 component = c;
1402 break;
1403 }
1404 }
1407 if (component_mask[component] == true)
1408 dofs_by_level[level].push_back(local_dofs[i]);
1409 }
1410 }
1411 else
1412 dofs_by_level[level].insert(dofs_by_level[level].end(),
1413 local_dofs.begin(),
1414 local_dofs.end());
1415 }
1416 }
1417 }
1418 for (unsigned int level = 0; level < dof.get_triangulation().n_levels();
1419 ++level)
1420 {
1421 std::sort(dofs_by_level[level].begin(), dofs_by_level[level].end());
1422 boundary_indices[level].add_indices(dofs_by_level[level].begin(),
1423 dofs_by_level[level].end());
1424 }
1425 }
1426
1427
1428
1429 template <int dim, int spacedim>
1430 void
1432 std::vector<IndexSet> &interface_dofs)
1433 {
1434 Assert(interface_dofs.size() ==
1435 mg_dof_handler.get_triangulation().n_global_levels(),
1437 interface_dofs.size(),
1438 mg_dof_handler.get_triangulation().n_global_levels()));
1439
1440 std::vector<std::vector<types::global_dof_index>> tmp_interface_dofs(
1441 interface_dofs.size());
1442
1443 const FiniteElement<dim, spacedim> &fe = mg_dof_handler.get_fe();
1444
1445 const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
1446
1447 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1448
1449 std::vector<bool> cell_dofs(dofs_per_cell, false);
1450
1451 for (const auto &cell : mg_dof_handler.cell_iterators())
1452 {
1453 // Do not look at artificial level cells (in a serial computation we
1454 // need to ignore the level_subdomain_id() because it is never set).
1455 if (cell->is_artificial_on_level())
1456 continue;
1457
1458 bool has_coarser_neighbor = false;
1459
1460 std::fill(cell_dofs.begin(), cell_dofs.end(), false);
1461
1462 for (const unsigned int face_nr : GeometryInfo<dim>::face_indices())
1463 {
1464 const typename DoFHandler<dim, spacedim>::face_iterator face =
1465 cell->face(face_nr);
1466 if (!face->at_boundary() || cell->has_periodic_neighbor(face_nr))
1467 {
1468 // interior face
1469 const typename DoFHandler<dim>::cell_iterator neighbor =
1470 cell->neighbor_or_periodic_neighbor(face_nr);
1471
1472 // only process cell pairs if one or both of them are owned by
1473 // me (ignore if running in serial)
1474 if (neighbor->is_artificial_on_level())
1475 continue;
1476
1477 // Do refinement face from the coarse side
1478 if (neighbor->level() < cell->level())
1479 {
1480 for (unsigned int j = 0; j < fe.n_dofs_per_face(face_nr);
1481 ++j)
1482 cell_dofs[fe.face_to_cell_index(j, face_nr)] = true;
1483
1484 has_coarser_neighbor = true;
1485 }
1486 }
1487 }
1488
1489 if (has_coarser_neighbor == false)
1490 continue;
1491
1492 const unsigned int level = cell->level();
1493 cell->get_mg_dof_indices(local_dof_indices);
1494
1495 for (unsigned int i = 0; i < dofs_per_cell; ++i)
1496 {
1497 if (cell_dofs[i])
1498 tmp_interface_dofs[level].push_back(local_dof_indices[i]);
1499 }
1500 }
1501
1502 for (unsigned int l = 0;
1503 l < mg_dof_handler.get_triangulation().n_global_levels();
1504 ++l)
1505 {
1506 interface_dofs[l].clear();
1507 std::sort(tmp_interface_dofs[l].begin(), tmp_interface_dofs[l].end());
1508 interface_dofs[l].add_indices(tmp_interface_dofs[l].begin(),
1509 tmp_interface_dofs[l].end());
1510 interface_dofs[l].compress();
1511 }
1512 }
1513
1514
1515
1516 template <int dim, int spacedim>
1517 unsigned int
1519 {
1520 // Find minimum level for an active cell in
1521 // this locally owned subdomain
1522 // Note: with the way active cells are traversed,
1523 // the first locally owned cell we find will have
1524 // the lowest level in the particular subdomain.
1525 unsigned int min_level = tria.n_global_levels();
1526 for (const auto &cell : tria.active_cell_iterators())
1527 if (cell->is_locally_owned())
1528 {
1529 min_level = cell->level();
1530 break;
1531 }
1532
1533 unsigned int global_min = min_level;
1534 // If necessary, communicate to find minimum
1535 // level for an active cell over all subdomains
1537 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
1538 &tria))
1539 global_min = Utilities::MPI::min(min_level, tr->get_mpi_communicator());
1540
1541 AssertIndexRange(global_min, tria.n_global_levels());
1542
1543 return global_min;
1544 }
1545
1546
1547
1548 namespace internal
1549 {
1550 double
1552 const std::vector<types::global_dof_index> &n_cells_on_levels,
1553 const MPI_Comm comm)
1554 {
1555 std::vector<types::global_dof_index> n_cells_on_levels_max(
1556 n_cells_on_levels.size());
1557 std::vector<types::global_dof_index> n_cells_on_levels_sum(
1558 n_cells_on_levels.size());
1559
1560 Utilities::MPI::max(n_cells_on_levels, comm, n_cells_on_levels_max);
1561 Utilities::MPI::sum(n_cells_on_levels, comm, n_cells_on_levels_sum);
1562
1563 const unsigned int n_proc = Utilities::MPI::n_mpi_processes(comm);
1564
1565 const double ideal_work = std::accumulate(n_cells_on_levels_sum.begin(),
1566 n_cells_on_levels_sum.end(),
1567 0) /
1568 static_cast<double>(n_proc);
1569 const double workload_imbalance =
1570 std::accumulate(n_cells_on_levels_max.begin(),
1571 n_cells_on_levels_max.end(),
1572 0) /
1573 ideal_work;
1574
1575 return workload_imbalance;
1576 }
1577
1578
1579
1580 double
1582 const std::vector<
1583 std::pair<types::global_dof_index, types::global_dof_index>> &cells,
1584 const MPI_Comm comm)
1585 {
1586 std::vector<types::global_dof_index> cells_local(cells.size());
1587 std::vector<types::global_dof_index> cells_remote(cells.size());
1588
1589 for (unsigned int i = 0; i < cells.size(); ++i)
1590 {
1591 cells_local[i] = cells[i].first;
1592 cells_remote[i] = cells[i].second;
1593 }
1594
1595 std::vector<types::global_dof_index> cells_local_sum(cells_local.size());
1596 Utilities::MPI::sum(cells_local, comm, cells_local_sum);
1597
1598 std::vector<types::global_dof_index> cells_remote_sum(
1599 cells_remote.size());
1600 Utilities::MPI::sum(cells_remote, comm, cells_remote_sum);
1601
1602 const auto n_cells_local =
1603 std::accumulate(cells_local_sum.begin(), cells_local_sum.end(), 0);
1604 const auto n_cells_remote =
1605 std::accumulate(cells_remote_sum.begin(), cells_remote_sum.end(), 0);
1606
1607 return static_cast<double>(n_cells_local) /
1608 (n_cells_local + n_cells_remote);
1609 }
1610 } // namespace internal
1611
1612
1613
1614 template <int dim, int spacedim>
1615 std::vector<types::global_dof_index>
1617 {
1619 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
1620 &tria))
1621 Assert(
1622 tr->is_multilevel_hierarchy_constructed(),
1623 ExcMessage(
1624 "We can only compute the workload imbalance if the multilevel hierarchy has been constructed!"));
1625
1626 const unsigned int n_global_levels = tria.n_global_levels();
1627
1628 std::vector<types::global_dof_index> n_cells_on_levels(n_global_levels);
1629
1630 for (unsigned int lvl = 0; lvl < n_global_levels; ++lvl)
1631 for (const auto &cell : tria.cell_iterators_on_level(lvl))
1632 if (cell->is_locally_owned_on_level())
1633 ++n_cells_on_levels[lvl];
1634
1635 return n_cells_on_levels;
1636 }
1637
1638
1639
1640 template <int dim, int spacedim>
1641 std::vector<types::global_dof_index>
1643 const std::vector<std::shared_ptr<const Triangulation<dim, spacedim>>>
1644 &trias)
1645 {
1646 const unsigned int n_global_levels = trias.size();
1647
1648 std::vector<types::global_dof_index> n_cells_on_levels(n_global_levels);
1649
1650 for (unsigned int lvl = 0; lvl < n_global_levels; ++lvl)
1651 for (const auto &cell : trias[lvl]->active_cell_iterators())
1652 if (cell->is_locally_owned())
1653 ++n_cells_on_levels[lvl];
1654
1655 return n_cells_on_levels;
1656 }
1657
1658
1659
1660 template <int dim, int spacedim>
1661 double
1667
1668
1669
1670 template <int dim, int spacedim>
1671 double
1673 const std::vector<std::shared_ptr<const Triangulation<dim, spacedim>>>
1674 &trias)
1675 {
1677 trias.back()->get_mpi_communicator());
1678 }
1679
1680
1681
1682 template <int dim, int spacedim>
1683 std::vector<std::pair<types::global_dof_index, types::global_dof_index>>
1685 {
1686 const unsigned int n_global_levels = tria.n_global_levels();
1687
1688 std::vector<std::pair<types::global_dof_index, types::global_dof_index>>
1689 cells(n_global_levels);
1690
1691 const MPI_Comm communicator = tria.get_mpi_communicator();
1692
1693 const unsigned int my_rank = Utilities::MPI::this_mpi_process(communicator);
1694
1695 for (unsigned int lvl = 0; lvl < n_global_levels - 1; ++lvl)
1696 for (const auto &cell : tria.cell_iterators_on_level(lvl))
1697 if (cell->is_locally_owned_on_level() && cell->has_children())
1698 for (unsigned int i = 0; i < GeometryInfo<dim>::max_children_per_cell;
1699 ++i)
1700 {
1701 const auto level_subdomain_id =
1702 cell->child(i)->level_subdomain_id();
1703 if (level_subdomain_id == my_rank)
1704 ++cells[lvl + 1].first;
1705 else if (level_subdomain_id != numbers::invalid_unsigned_int)
1706 ++cells[lvl + 1].second;
1707 else
1709 }
1710
1711 return cells;
1712 }
1713
1714
1715
1716 template <int dim, int spacedim>
1717 std::vector<std::pair<types::global_dof_index, types::global_dof_index>>
1719 const std::vector<std::shared_ptr<const Triangulation<dim, spacedim>>>
1720 &trias)
1721 {
1722 const unsigned int n_global_levels = trias.size();
1723
1724 std::vector<std::pair<types::global_dof_index, types::global_dof_index>>
1725 cells(n_global_levels);
1726
1727 const MPI_Comm communicator = trias.back()->get_mpi_communicator();
1728
1729 const unsigned int my_rank = Utilities::MPI::this_mpi_process(communicator);
1730
1731 for (unsigned int lvl = 0; lvl < n_global_levels - 1; ++lvl)
1732 {
1733 const auto &tria_coarse = *trias[lvl];
1734 const auto &tria_fine = *trias[lvl + 1];
1735
1736 const ::internal::CellIDTranslator<dim> cell_id_translator(
1737 tria_fine);
1738
1739 IndexSet is_fine_owned(cell_id_translator.size());
1740 IndexSet is_fine_required(cell_id_translator.size());
1741
1742 for (const auto &cell : tria_fine.active_cell_iterators())
1743 if (!cell->is_artificial() && cell->is_locally_owned())
1744 is_fine_owned.add_index(cell_id_translator.translate(cell));
1745
1746 for (const auto &cell : tria_coarse.active_cell_iterators())
1747 if (!cell->is_artificial() && cell->is_locally_owned())
1748 {
1749 if (cell->level() + 1u == tria_fine.n_global_levels())
1750 continue;
1751
1752 for (unsigned int i = 0;
1753 i < GeometryInfo<dim>::max_children_per_cell;
1754 ++i)
1755 is_fine_required.add_index(
1756 cell_id_translator.translate(cell, i));
1757 }
1758
1759 const std::vector<unsigned int> is_fine_required_ranks =
1761 is_fine_required,
1762 communicator);
1763
1764 for (unsigned i = 0; i < is_fine_required.n_elements(); ++i)
1765 if (is_fine_required_ranks[i] == my_rank)
1766 ++cells[lvl + 1].first;
1767 else if (is_fine_required_ranks[i] != numbers::invalid_unsigned_int)
1768 ++cells[lvl + 1].second;
1769 }
1770
1771 return cells;
1772 }
1773
1774
1775
1776 template <int dim, int spacedim>
1777 double
1783
1784
1785
1786 template <int dim, int spacedim>
1787 double
1789 const std::vector<std::shared_ptr<const Triangulation<dim, spacedim>>>
1790 &trias)
1791 {
1794 trias.back()->get_mpi_communicator());
1795 }
1796
1797} // namespace MGTools
1798
1799
1800// explicit instantiations
1801#include "multigrid/mg_tools.inst"
1802
*  iterator end()
*  *  iterator begin()
ArrayView< std::remove_reference_t< typename std::iterator_traits< Iterator >::reference >, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
void add_entries_local_to_global(const std::vector< size_type > &local_dof_indices, SparsityPatternBase &sparsity_pattern, const bool keep_constrained_entries=true, const Table< 2, bool > &dof_mask=Table< 2, bool >()) const
unsigned int n_selected_components(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
const hp::FECollection< dim, spacedim > & get_fe_collection() const
const FiniteElement< dim, spacedim > & get_fe(const types::fe_index index=0) const
const Triangulation< dim, spacedim > & get_triangulation() const
types::global_dof_index n_dofs() const
unsigned int get_first_line_index() const
unsigned int n_dofs_per_cell() const
unsigned int get_first_quad_index(const unsigned int quad_no=0) const
unsigned int n_dofs_per_face(unsigned int face_no=0, unsigned int child=0) const
unsigned int n_blocks() const
unsigned int n_components() const
ReferenceCell< dim > reference_cell() const
unsigned int get_first_hex_index() const
std::pair< unsigned int, types::global_dof_index > system_to_block_index(const unsigned int component) const
virtual const FiniteElement< dim, spacedim > & base_element(const unsigned int index) const
bool is_primitive() const
virtual bool has_support_on_face(const unsigned int shape_index, const unsigned int face_index) const
std::pair< unsigned int, unsigned int > system_to_component_index(const unsigned int index) const
unsigned int element_multiplicity(const unsigned int index) const
virtual unsigned int face_to_cell_index(const unsigned int face_dof_index, const unsigned int face, const types::geometric_orientation combined_orientation=numbers::default_geometric_orientation) const
unsigned int n_base_elements() const
std::pair< unsigned int, unsigned int > face_system_to_component_index(const unsigned int index, const unsigned int face_no=0) const
types::global_dof_index first_block_of_base(const unsigned int b) const
size_type n_elements() const
Definition index_set.h:1917
void add_index(const size_type index)
Definition index_set.h:1778
bool is_interface_matrix_entry(const unsigned int level, const types::global_dof_index i, const types::global_dof_index j) const
size_type n_rows() const
virtual void add_entries(const ArrayView< const std::pair< size_type, size_type > > &entries)
virtual void add_row_entries(const size_type &row, const ArrayView< const size_type > &columns, const bool indices_are_sorted=false)=0
size_type n_cols() const
virtual MPI_Comm get_mpi_communicator() const
unsigned int n_raw_lines() const
unsigned int n_levels() const
virtual unsigned int n_global_levels() const
unsigned int n_raw_quads() const
unsigned int max_dofs_per_face() const
unsigned int n_components() const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define DEAL_II_NOT_IMPLEMENTED()
unsigned int level
Definition grid_out.cc:4642
IteratorRange< cell_iterator > cell_iterators_on_level(const unsigned int level) const
IteratorRange< cell_iterator > cell_iterators() const
IteratorRange< active_cell_iterator > active_cell_iterators() const
IteratorRange< cell_iterator > cell_iterators_on_level(const unsigned int level) const
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcIndexRange(std::size_t arg1, std::size_t arg2, std::size_t arg3)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
typename ActiveSelector::cell_iterator cell_iterator
typename ActiveSelector::face_iterator face_iterator
Task< RT > new_task(const std::function< RT()> &function)
const unsigned int my_rank
Definition mpi.cc:917
std::size_t size
Definition mpi.cc:733
const MPI_Comm comm
Definition mpi.cc:912
void convert_couplings_to_blocks(const DoFHandler< dim, spacedim > &dof_handler, const Table< 2, Coupling > &table_by_component, std::vector< Table< 2, Coupling > > &tables_by_block)
Table< 2, Coupling > dof_couplings_from_component_couplings(const FiniteElement< dim, spacedim > &fe, const Table< 2, Coupling > &component_couplings)
double workload_imbalance(const std::vector< types::global_dof_index > &n_cells_on_levels, const MPI_Comm comm)
Definition mg_tools.cc:1551
double vertical_communication_efficiency(const std::vector< std::pair< types::global_dof_index, types::global_dof_index > > &cells, const MPI_Comm comm)
Definition mg_tools.cc:1581
void make_flux_sparsity_pattern(const DoFHandler< dim, spacedim > &dof_handler, SparsityPatternBase &sparsity, const unsigned int level, const AffineConstraints< number > &constraints={}, const bool keep_constrained_dofs=true)
Definition mg_tools.cc:604
void count_dofs_per_component(const DoFHandler< dim, spacedim > &mg_dof, std::vector< std::vector< types::global_dof_index > > &result, const bool only_once=false, std::vector< unsigned int > target_component={})
Definition mg_tools.cc:1042
std::vector< std::pair< types::global_dof_index, types::global_dof_index > > local_vertical_communication_cost(const Triangulation< dim, spacedim > &tria)
Definition mg_tools.cc:1684
void make_boundary_list(const DoFHandler< dim, spacedim > &mg_dof, const std::map< types::boundary_id, const Function< spacedim > * > &function_map, std::vector< std::set< types::global_dof_index > > &boundary_indices, const ComponentMask &component_mask={})
Definition mg_tools.cc:1227
void make_interface_sparsity_pattern(const DoFHandler< dim, spacedim > &dof_handler, const MGConstrainedDoFs &mg_constrained_dofs, SparsityPatternBase &sparsity, const unsigned int level)
Definition mg_tools.cc:1003
std::vector< types::global_dof_index > local_workload(const Triangulation< dim, spacedim > &tria)
Definition mg_tools.cc:1616
void make_flux_sparsity_pattern_edge(const DoFHandler< dim, spacedim > &dof_handler, SparsityPatternBase &sparsity, const unsigned int level)
Definition mg_tools.cc:677
void compute_row_length_vector(const DoFHandler< dim, spacedim > &dofs, const unsigned int level, std::vector< unsigned int > &row_lengths, const DoFTools::Coupling flux_couplings=DoFTools::none)
Definition mg_tools.cc:106
void extract_inner_interface_dofs(const DoFHandler< dim, spacedim > &mg_dof_handler, std::vector< IndexSet > &interface_dofs)
Definition mg_tools.cc:1431
double workload_imbalance(const Triangulation< dim, spacedim > &tria)
Definition mg_tools.cc:1662
void count_dofs_per_block(const DoFHandler< dim, spacedim > &dof_handler, std::vector< std::vector< types::global_dof_index > > &dofs_per_block, std::vector< unsigned int > target_block={})
Definition mg_tools.cc:1144
double vertical_communication_efficiency(const Triangulation< dim, spacedim > &tria)
Definition mg_tools.cc:1778
void make_sparsity_pattern(const DoFHandler< dim, spacedim > &dof_handler, SparsityPatternBase &sparsity, const unsigned int level, const AffineConstraints< number > &constraints={}, const bool keep_constrained_dofs=true)
Definition mg_tools.cc:575
unsigned int max_level_for_coarse_mesh(const Triangulation< dim, spacedim > &tria)
Definition mg_tools.cc:1518
T sum(const T &t, const MPI_Comm mpi_communicator)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
Definition mpi.cc:103
T max(const T &t, const MPI_Comm mpi_communicator)
T min(const T &t, const MPI_Comm mpi_communicator)
std::vector< unsigned int > compute_index_owner(const IndexSet &owned_indices, const IndexSet &indices_to_look_up, const MPI_Comm comm)
Definition mpi.cc:1820
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
Definition mpi.cc:118
constexpr types::global_dof_index invalid_dof_index
Definition types.h:259
constexpr unsigned int invalid_unsigned_int
Definition types.h:228
static std_cxx20::ranges::iota_view< unsigned int, unsigned int > face_indices()