Reference documentation for deal.II version 9.1.0-pre
mg_tools.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #include <deal.II/base/logstream.h>
17 #include <deal.II/base/mg_level_object.h>
18 #include <deal.II/base/thread_management.h>
19 
20 #include <deal.II/dofs/dof_accessor.h>
21 #include <deal.II/dofs/dof_handler.h>
22 #include <deal.II/dofs/dof_tools.h>
23 
24 #include <deal.II/fe/component_mask.h>
25 #include <deal.II/fe/fe.h>
26 
27 #include <deal.II/grid/tria.h>
28 #include <deal.II/grid/tria_iterator.h>
29 
30 #include <deal.II/lac/block_sparse_matrix.h>
31 #include <deal.II/lac/block_sparsity_pattern.h>
32 #include <deal.II/lac/block_vector.h>
33 #include <deal.II/lac/dynamic_sparsity_pattern.h>
34 #include <deal.II/lac/sparse_matrix.h>
35 #include <deal.II/lac/sparsity_pattern.h>
36 
37 #include <deal.II/multigrid/mg_base.h>
38 #include <deal.II/multigrid/mg_constrained_dofs.h>
39 #include <deal.II/multigrid/mg_tools.h>
40 
41 #include <deal.II/numerics/matrix_tools.h>
42 
43 #include <algorithm>
44 #include <numeric>
45 #include <vector>
46 
47 DEAL_II_NAMESPACE_OPEN
48 
49 
50 namespace MGTools
51 {
52  // specializations for 1D
53  template <>
54  void
56  const unsigned int,
57  std::vector<unsigned int> &,
58  const DoFTools::Coupling)
59  {
60  Assert(false, ExcNotImplemented());
61  }
62 
63 
64 
65  template <>
66  void
68  const unsigned int,
69  std::vector<unsigned int> &,
72  {
73  Assert(false, ExcNotImplemented());
74  }
75 
76 
77 
78  template <>
79  void
81  const unsigned int,
82  std::vector<unsigned int> &,
83  const DoFTools::Coupling)
84  {
85  Assert(false, ExcNotImplemented());
86  }
87 
88 
89  template <>
90  void
92  const unsigned int,
93  std::vector<unsigned int> &,
96  {
97  Assert(false, ExcNotImplemented());
98  }
99 
100 
101 
102  // Template for 2D and 3D. For 1D see specialization above
103  template <int dim, int spacedim>
104  void
106  const unsigned int level,
107  std::vector<unsigned int> & row_lengths,
108  const DoFTools::Coupling flux_coupling)
109  {
110  Assert(row_lengths.size() == dofs.n_dofs(),
111  ExcDimensionMismatch(row_lengths.size(), dofs.n_dofs()));
112 
113  // Function starts here by
114  // resetting the counters.
115  std::fill(row_lengths.begin(), row_lengths.end(), 0);
116  // We need the user flags, so we
117  // save them for later restoration
118  std::vector<bool> old_flags;
119  // We need a non-constant
120  // triangulation for the user
121  // flags. Since we restore them in
122  // the end, this cast is safe.
123  Triangulation<dim, spacedim> &user_flags_triangulation =
124  const_cast<Triangulation<dim, spacedim> &>(dofs.get_triangulation());
125  user_flags_triangulation.save_user_flags(old_flags);
126  user_flags_triangulation.clear_user_flags();
127 
128  const typename DoFHandler<dim, spacedim>::cell_iterator end =
129  dofs.end(level);
131  std::vector<types::global_dof_index> cell_indices;
132  std::vector<types::global_dof_index> neighbor_indices;
133 
134  // We loop over cells and go from
135  // cells to lower dimensional
136  // objects. This is the only way to
137  // cope with the fact, that an
138  // unknown number of cells may
139  // share an object of dimension
140  // smaller than dim-1.
141  for (cell = dofs.begin(level); cell != end; ++cell)
142  {
143  const FiniteElement<dim> &fe = cell->get_fe();
144  cell_indices.resize(fe.dofs_per_cell);
145  cell->get_mg_dof_indices(cell_indices);
146  unsigned int i = 0;
147  // First, dofs on
148  // vertices. We assume that
149  // each vertex dof couples
150  // with all dofs on
151  // adjacent grid cells.
152 
153  // Adding all dofs of the cells
154  // will add dofs of the faces
155  // of the cell adjacent to the
156  // vertex twice. Therefore, we
157  // subtract these here and add
158  // them in a loop over the
159  // faces below.
160 
161  // in 1d, faces and vertices
162  // are identical. Nevertheless,
163  // this will only work if
164  // dofs_per_face is zero and
165  // dofs_per_vertex is
166  // arbitrary, not the other way
167  // round.
168  // TODO: This assumes that even in hp context, the dofs per face
169  // coincide!
170  unsigned int increment = fe.dofs_per_cell - dim * fe.dofs_per_face;
171  while (i < fe.first_line_index)
172  row_lengths[cell_indices[i++]] += increment;
173  // From now on, if an object is
174  // a cell, its dofs only couple
175  // inside the cell. Since the
176  // faces are handled below, we
177  // have to subtract ALL faces
178  // in this case.
179 
180  // In all other cases we
181  // subtract adjacent faces to be
182  // added in the loop below.
183  increment = (dim > 1) ?
184  fe.dofs_per_cell - (dim - 1) * fe.dofs_per_face :
185  fe.dofs_per_cell -
187  while (i < fe.first_quad_index)
188  row_lengths[cell_indices[i++]] += increment;
189 
190  // Now quads in 2D and 3D
191  increment = (dim > 2) ?
192  fe.dofs_per_cell - (dim - 2) * fe.dofs_per_face :
193  fe.dofs_per_cell -
195  while (i < fe.first_hex_index)
196  row_lengths[cell_indices[i++]] += increment;
197  // Finally, cells in 3D
198  increment = fe.dofs_per_cell -
200  while (i < fe.dofs_per_cell)
201  row_lengths[cell_indices[i++]] += increment;
202 
203  // At this point, we have
204  // counted all dofs
205  // contributiong from cells
206  // coupled topologically to the
207  // adjacent cells, but we
208  // subtracted some faces.
209 
210  // Now, let's go by the faces
211  // and add the missing
212  // contribution as well as the
213  // flux contributions.
214  for (unsigned int iface = 0; iface < GeometryInfo<dim>::faces_per_cell;
215  ++iface)
216  {
217  bool level_boundary = cell->at_boundary(iface);
219  if (!level_boundary)
220  {
221  neighbor = cell->neighbor(iface);
222  if (static_cast<unsigned int>(neighbor->level()) != level)
223  level_boundary = true;
224  }
225 
226  if (level_boundary)
227  {
228  for (unsigned int local_dof = 0; local_dof < fe.dofs_per_cell;
229  ++local_dof)
230  row_lengths[cell_indices[local_dof]] += fe.dofs_per_face;
231  continue;
232  }
233 
234  const FiniteElement<dim> &nfe = neighbor->get_fe();
236  cell->face(iface);
237 
238  // Flux couplings are
239  // computed from both sides
240  // for simplicity.
241 
242  // The dofs on the common face
243  // will be handled below,
244  // therefore, we subtract them
245  // here.
246  if (flux_coupling != DoFTools::none)
247  {
248  const unsigned int dof_increment =
249  nfe.dofs_per_cell - nfe.dofs_per_face;
250  for (unsigned int local_dof = 0; local_dof < fe.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->user_flag_set())
258  continue;
259  face->set_user_flag();
260  // At this point, we assume
261  // that each cell added its
262  // dofs minus the face to
263  // the couplings of the
264  // face dofs. Since we
265  // subtracted two faces, we
266  // have to re-add one.
267 
268  // If one side of the face
269  // is refined, all the fine
270  // face dofs couple with
271  // the coarse one.
272  neighbor_indices.resize(nfe.dofs_per_cell);
273  neighbor->get_mg_dof_indices(neighbor_indices);
274  for (unsigned int local_dof = 0; local_dof < fe.dofs_per_cell;
275  ++local_dof)
276  row_lengths[cell_indices[local_dof]] += nfe.dofs_per_face;
277  for (unsigned int local_dof = 0; local_dof < nfe.dofs_per_cell;
278  ++local_dof)
279  row_lengths[neighbor_indices[local_dof]] += fe.dofs_per_face;
280  }
281  }
282  user_flags_triangulation.load_user_flags(old_flags);
283  }
284 
285 
286  // This is the template for 2D and 3D. See version for 1D above
287  template <int dim, int spacedim>
288  void
290  const unsigned int level,
291  std::vector<unsigned int> & row_lengths,
292  const Table<2, DoFTools::Coupling> &couplings,
293  const Table<2, DoFTools::Coupling> &flux_couplings)
294  {
295  Assert(row_lengths.size() == dofs.n_dofs(),
296  ExcDimensionMismatch(row_lengths.size(), dofs.n_dofs()));
297 
298  // Function starts here by
299  // resetting the counters.
300  std::fill(row_lengths.begin(), row_lengths.end(), 0);
301  // We need the user flags, so we
302  // save them for later restoration
303  std::vector<bool> old_flags;
304  // We need a non-constant
305  // triangulation for the user
306  // flags. Since we restore them in
307  // the end, this cast is safe.
308  Triangulation<dim, spacedim> &user_flags_triangulation =
309  const_cast<Triangulation<dim, spacedim> &>(dofs.get_triangulation());
310  user_flags_triangulation.save_user_flags(old_flags);
311  user_flags_triangulation.clear_user_flags();
312 
313  const typename DoFHandler<dim, spacedim>::cell_iterator end =
314  dofs.end(level);
316  std::vector<types::global_dof_index> cell_indices;
317  std::vector<types::global_dof_index> neighbor_indices;
318 
319  // We have to translate the
320  // couplings from components to
321  // blocks, so this works for
322  // nonprimitive elements as well.
323  std::vector<Table<2, DoFTools::Coupling>> couple_cell;
324  std::vector<Table<2, DoFTools::Coupling>> couple_face;
325  DoFTools::convert_couplings_to_blocks(dofs, couplings, couple_cell);
326  DoFTools::convert_couplings_to_blocks(dofs, flux_couplings, couple_face);
327 
328  // We loop over cells and go from
329  // cells to lower dimensional
330  // objects. This is the only way to
331  // cope with the fact, that an
332  // unknown number of cells may
333  // share an object of dimension
334  // smaller than dim-1.
335  for (cell = dofs.begin_active(); cell != end; ++cell)
336  {
337  const FiniteElement<dim> &fe = cell->get_fe();
338  const unsigned int fe_index = cell->active_fe_index();
339 
340  Assert(couplings.n_rows() == fe.n_components(),
341  ExcDimensionMismatch(couplings.n_rows(), fe.n_components()));
342  Assert(couplings.n_cols() == fe.n_components(),
343  ExcDimensionMismatch(couplings.n_cols(), fe.n_components()));
344  Assert(flux_couplings.n_rows() == fe.n_components(),
345  ExcDimensionMismatch(flux_couplings.n_rows(),
346  fe.n_components()));
347  Assert(flux_couplings.n_cols() == fe.n_components(),
348  ExcDimensionMismatch(flux_couplings.n_cols(),
349  fe.n_components()));
350 
351  cell_indices.resize(fe.dofs_per_cell);
352  cell->get_mg_dof_indices(cell_indices);
353  unsigned int i = 0;
354  // First, dofs on
355  // vertices. We assume that
356  // each vertex dof couples
357  // with all dofs on
358  // adjacent grid cells.
359 
360  // Adding all dofs of the cells
361  // will add dofs of the faces
362  // of the cell adjacent to the
363  // vertex twice. Therefore, we
364  // subtract these here and add
365  // them in a loop over the
366  // faces below.
367 
368  // in 1d, faces and vertices
369  // are identical. Nevertheless,
370  // this will only work if
371  // dofs_per_face is zero and
372  // dofs_per_vertex is
373  // arbitrary, not the other way
374  // round.
375  unsigned int increment;
376  while (i < fe.first_line_index)
377  {
378  for (unsigned int base = 0; base < fe.n_base_elements(); ++base)
379  for (unsigned int mult = 0; mult < fe.element_multiplicity(base);
380  ++mult)
381  if (couple_cell[fe_index](fe.system_to_block_index(i).first,
382  fe.first_block_of_base(base) +
383  mult) != DoFTools::none)
384  {
385  increment = fe.base_element(base).dofs_per_cell -
386  dim * fe.base_element(base).dofs_per_face;
387  row_lengths[cell_indices[i]] += increment;
388  }
389  ++i;
390  }
391  // From now on, if an object is
392  // a cell, its dofs only couple
393  // inside the cell. Since the
394  // faces are handled below, we
395  // have to subtract ALL faces
396  // in this case.
397 
398  // In all other cases we
399  // subtract adjacent faces to be
400  // added in the loop below.
401  while (i < fe.first_quad_index)
402  {
403  for (unsigned int base = 0; base < fe.n_base_elements(); ++base)
404  for (unsigned int mult = 0; mult < fe.element_multiplicity(base);
405  ++mult)
406  if (couple_cell[fe_index](fe.system_to_block_index(i).first,
407  fe.first_block_of_base(base) +
408  mult) != DoFTools::none)
409  {
410  increment =
411  fe.base_element(base).dofs_per_cell -
412  ((dim > 1) ? (dim - 1) :
414  fe.base_element(base).dofs_per_face;
415  row_lengths[cell_indices[i]] += increment;
416  }
417  ++i;
418  }
419 
420  // Now quads in 2D and 3D
421  while (i < fe.first_hex_index)
422  {
423  for (unsigned int base = 0; base < fe.n_base_elements(); ++base)
424  for (unsigned int mult = 0; mult < fe.element_multiplicity(base);
425  ++mult)
426  if (couple_cell[fe_index](fe.system_to_block_index(i).first,
427  fe.first_block_of_base(base) +
428  mult) != DoFTools::none)
429  {
430  increment =
431  fe.base_element(base).dofs_per_cell -
432  ((dim > 2) ? (dim - 2) :
434  fe.base_element(base).dofs_per_face;
435  row_lengths[cell_indices[i]] += increment;
436  }
437  ++i;
438  }
439 
440  // Finally, cells in 3D
441  while (i < fe.dofs_per_cell)
442  {
443  for (unsigned int base = 0; base < fe.n_base_elements(); ++base)
444  for (unsigned int mult = 0; mult < fe.element_multiplicity(base);
445  ++mult)
446  if (couple_cell[fe_index](fe.system_to_block_index(i).first,
447  fe.first_block_of_base(base) +
448  mult) != DoFTools::none)
449  {
450  increment = fe.base_element(base).dofs_per_cell -
452  fe.base_element(base).dofs_per_face;
453  row_lengths[cell_indices[i]] += increment;
454  }
455  ++i;
456  }
457 
458  // At this point, we have
459  // counted all dofs
460  // contributiong 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 (unsigned int iface = 0; iface < GeometryInfo<dim>::faces_per_cell;
470  ++iface)
471  {
472  bool level_boundary = cell->at_boundary(iface);
474  if (!level_boundary)
475  {
476  neighbor = cell->neighbor(iface);
477  if (static_cast<unsigned int>(neighbor->level()) != level)
478  level_boundary = true;
479  }
480 
481  if (level_boundary)
482  {
483  for (unsigned int local_dof = 0; local_dof < fe.dofs_per_cell;
484  ++local_dof)
485  row_lengths[cell_indices[local_dof]] += fe.dofs_per_face;
486  continue;
487  }
488 
489  const FiniteElement<dim> &nfe = neighbor->get_fe();
491  cell->face(iface);
492 
493  // Flux couplings are
494  // computed from both sides
495  // for simplicity.
496 
497  // The dofs on the common face
498  // will be handled below,
499  // therefore, we subtract them
500  // here.
501  for (unsigned int base = 0; base < nfe.n_base_elements(); ++base)
502  for (unsigned int mult = 0; mult < nfe.element_multiplicity(base);
503  ++mult)
504  for (unsigned int local_dof = 0; local_dof < fe.dofs_per_cell;
505  ++local_dof)
506  if (couple_face[fe_index](
507  fe.system_to_block_index(local_dof).first,
508  nfe.first_block_of_base(base) + mult) != DoFTools::none)
509  {
510  const unsigned int dof_increment =
511  nfe.base_element(base).dofs_per_cell -
512  nfe.base_element(base).dofs_per_face;
513  row_lengths[cell_indices[local_dof]] += dof_increment;
514  }
515 
516  // Do this only once per
517  // face and not on the
518  // hanging faces.
519  if (face->user_flag_set())
520  continue;
521  face->set_user_flag();
522  // At this point, we assume
523  // that each cell added its
524  // dofs minus the face to
525  // the couplings of the
526  // face dofs. Since we
527  // subtracted two faces, we
528  // have to re-add one.
529 
530  // If one side of the face
531  // is refined, all the fine
532  // face dofs couple with
533  // the coarse one.
534 
535  // Wolfgang, do they couple
536  // with each other by
537  // constraints?
538 
539  // This will not work with
540  // different couplings on
541  // different cells.
542  neighbor_indices.resize(nfe.dofs_per_cell);
543  neighbor->get_mg_dof_indices(neighbor_indices);
544  for (unsigned int base = 0; base < nfe.n_base_elements(); ++base)
545  for (unsigned int mult = 0; mult < nfe.element_multiplicity(base);
546  ++mult)
547  for (unsigned int local_dof = 0; local_dof < fe.dofs_per_cell;
548  ++local_dof)
549  if (couple_cell[fe_index](
550  fe.system_to_component_index(local_dof).first,
551  nfe.first_block_of_base(base) + mult) != DoFTools::none)
552  row_lengths[cell_indices[local_dof]] +=
553  nfe.base_element(base).dofs_per_face;
554  for (unsigned int base = 0; base < fe.n_base_elements(); ++base)
555  for (unsigned int mult = 0; mult < fe.element_multiplicity(base);
556  ++mult)
557  for (unsigned int local_dof = 0; local_dof < nfe.dofs_per_cell;
558  ++local_dof)
559  if (couple_cell[fe_index](
560  nfe.system_to_component_index(local_dof).first,
561  fe.first_block_of_base(base) + mult) != DoFTools::none)
562  row_lengths[neighbor_indices[local_dof]] +=
563  fe.base_element(base).dofs_per_face;
564  }
565  }
566  user_flags_triangulation.load_user_flags(old_flags);
567  }
568 
569 
570 
571  template <typename DoFHandlerType, typename SparsityPatternType>
572  void
573  make_sparsity_pattern(const DoFHandlerType &dof,
574  SparsityPatternType & sparsity,
575  const unsigned int level)
576  {
577  const types::global_dof_index n_dofs = dof.n_dofs(level);
578  (void)n_dofs;
579 
580  Assert(sparsity.n_rows() == n_dofs,
581  ExcDimensionMismatch(sparsity.n_rows(), n_dofs));
582  Assert(sparsity.n_cols() == n_dofs,
583  ExcDimensionMismatch(sparsity.n_cols(), n_dofs));
584 
585  const unsigned int dofs_per_cell = dof.get_fe().dofs_per_cell;
586  std::vector<types::global_dof_index> dofs_on_this_cell(dofs_per_cell);
587  typename DoFHandlerType::cell_iterator cell = dof.begin(level),
588  endc = dof.end(level);
589  for (; cell != endc; ++cell)
590  if (dof.get_triangulation().locally_owned_subdomain() ==
592  cell->level_subdomain_id() ==
593  dof.get_triangulation().locally_owned_subdomain())
594  {
595  cell->get_mg_dof_indices(dofs_on_this_cell);
596  // make sparsity pattern for this cell
597  for (unsigned int i = 0; i < dofs_per_cell; ++i)
598  for (unsigned int j = 0; j < dofs_per_cell; ++j)
599  sparsity.add(dofs_on_this_cell[i], dofs_on_this_cell[j]);
600  }
601  }
602 
603 
604 
605  template <int dim, typename SparsityPatternType, int spacedim>
606  void
608  SparsityPatternType & sparsity,
609  const unsigned int level)
610  {
611  const types::global_dof_index n_dofs = dof.n_dofs(level);
612  (void)n_dofs;
613 
614  Assert(sparsity.n_rows() == n_dofs,
615  ExcDimensionMismatch(sparsity.n_rows(), n_dofs));
616  Assert(sparsity.n_cols() == n_dofs,
617  ExcDimensionMismatch(sparsity.n_cols(), n_dofs));
618 
619  const unsigned int dofs_per_cell = dof.get_fe().dofs_per_cell;
620  std::vector<types::global_dof_index> dofs_on_this_cell(dofs_per_cell);
621  std::vector<types::global_dof_index> dofs_on_other_cell(dofs_per_cell);
622  typename DoFHandler<dim, spacedim>::cell_iterator cell = dof.begin(level),
623  endc = dof.end(level);
624  for (; cell != endc; ++cell)
625  {
626  if (!cell->is_locally_owned_on_level())
627  continue;
628 
629  cell->get_mg_dof_indices(dofs_on_this_cell);
630  // make sparsity pattern for this cell
631  for (unsigned int i = 0; i < dofs_per_cell; ++i)
632  for (unsigned int j = 0; j < dofs_per_cell; ++j)
633  sparsity.add(dofs_on_this_cell[i], dofs_on_this_cell[j]);
634 
635  // Loop over all interior neighbors
636  for (unsigned int face = 0; face < GeometryInfo<dim>::faces_per_cell;
637  ++face)
638  {
639  bool use_face = false;
640  if ((!cell->at_boundary(face)) &&
641  (static_cast<unsigned int>(cell->neighbor_level(face)) ==
642  level))
643  use_face = true;
644  else if (cell->has_periodic_neighbor(face) &&
645  (static_cast<unsigned int>(
646  cell->periodic_neighbor_level(face)) == level))
647  use_face = true;
648 
649  if (use_face)
650  {
651  typename DoFHandler<dim, spacedim>::cell_iterator neighbor =
652  cell->neighbor_or_periodic_neighbor(face);
653  neighbor->get_mg_dof_indices(dofs_on_other_cell);
654  // only add one direction The other is taken care of by
655  // neighbor (except when the neighbor is not owned by the same
656  // processor)
657  for (unsigned int i = 0; i < dofs_per_cell; ++i)
658  {
659  for (unsigned int j = 0; j < dofs_per_cell; ++j)
660  {
661  sparsity.add(dofs_on_this_cell[i],
662  dofs_on_other_cell[j]);
663  }
664  }
665  if (neighbor->is_locally_owned_on_level() == false)
666  for (unsigned int i = 0; i < dofs_per_cell; ++i)
667  for (unsigned int j = 0; j < dofs_per_cell; ++j)
668  {
669  sparsity.add(dofs_on_other_cell[i],
670  dofs_on_other_cell[j]);
671  sparsity.add(dofs_on_other_cell[i],
672  dofs_on_this_cell[j]);
673  }
674  }
675  }
676  }
677  }
678 
679 
680 
681  template <int dim, typename SparsityPatternType, int spacedim>
682  void
684  SparsityPatternType & sparsity,
685  const unsigned int level)
686  {
687  Assert((level >= 1) && (level < dof.get_triangulation().n_global_levels()),
688  ExcIndexRange(level, 1, dof.get_triangulation().n_global_levels()));
689 
690  const types::global_dof_index fine_dofs = dof.n_dofs(level);
691  const types::global_dof_index coarse_dofs = dof.n_dofs(level - 1);
692  (void)fine_dofs;
693  (void)coarse_dofs;
694 
695  // Matrix maps from fine level to coarse level
696 
697  Assert(sparsity.n_rows() == coarse_dofs,
698  ExcDimensionMismatch(sparsity.n_rows(), coarse_dofs));
699  Assert(sparsity.n_cols() == fine_dofs,
700  ExcDimensionMismatch(sparsity.n_cols(), fine_dofs));
701 
702  const unsigned int dofs_per_cell = dof.get_fe().dofs_per_cell;
703  std::vector<types::global_dof_index> dofs_on_this_cell(dofs_per_cell);
704  std::vector<types::global_dof_index> dofs_on_other_cell(dofs_per_cell);
705  typename DoFHandler<dim, spacedim>::cell_iterator cell = dof.begin(level),
706  endc = dof.end(level);
707  for (; cell != endc; ++cell)
708  {
709  if (!cell->is_locally_owned_on_level())
710  continue;
711 
712  cell->get_mg_dof_indices(dofs_on_this_cell);
713  // Loop over all interior neighbors
714  for (unsigned int face = 0; face < GeometryInfo<dim>::faces_per_cell;
715  ++face)
716  {
717  // Neighbor is coarser
718  bool use_face = false;
719  if ((!cell->at_boundary(face)) &&
720  (static_cast<unsigned int>(cell->neighbor_level(face)) !=
721  level))
722  use_face = true;
723  else if (cell->has_periodic_neighbor(face) &&
724  (static_cast<unsigned int>(
725  cell->periodic_neighbor_level(face)) != level))
726  use_face = true;
727 
728  if (use_face)
729  {
730  typename DoFHandler<dim, spacedim>::cell_iterator neighbor =
731  cell->neighbor_or_periodic_neighbor(face);
732  neighbor->get_mg_dof_indices(dofs_on_other_cell);
733 
734  for (unsigned int i = 0; i < dofs_per_cell; ++i)
735  {
736  for (unsigned int j = 0; j < dofs_per_cell; ++j)
737  {
738  sparsity.add(dofs_on_other_cell[i],
739  dofs_on_this_cell[j]);
740  sparsity.add(dofs_on_other_cell[j],
741  dofs_on_this_cell[i]);
742  }
743  }
744  }
745  }
746  }
747  }
748 
749 
750 
751  template <int dim, typename SparsityPatternType, int spacedim>
752  void
754  SparsityPatternType & sparsity,
755  const unsigned int level,
756  const Table<2, DoFTools::Coupling> &int_mask,
757  const Table<2, DoFTools::Coupling> &flux_mask)
758  {
759  const FiniteElement<dim> & fe = dof.get_fe();
760  const types::global_dof_index n_dofs = dof.n_dofs(level);
761  const unsigned int n_comp = fe.n_components();
762  (void)n_dofs;
763  (void)n_comp;
764 
765  Assert(sparsity.n_rows() == n_dofs,
766  ExcDimensionMismatch(sparsity.n_rows(), n_dofs));
767  Assert(sparsity.n_cols() == n_dofs,
768  ExcDimensionMismatch(sparsity.n_cols(), n_dofs));
769  Assert(int_mask.n_rows() == n_comp,
770  ExcDimensionMismatch(int_mask.n_rows(), n_comp));
771  Assert(int_mask.n_cols() == n_comp,
772  ExcDimensionMismatch(int_mask.n_cols(), n_comp));
773  Assert(flux_mask.n_rows() == n_comp,
774  ExcDimensionMismatch(flux_mask.n_rows(), n_comp));
775  Assert(flux_mask.n_cols() == n_comp,
776  ExcDimensionMismatch(flux_mask.n_cols(), n_comp));
777 
778  const unsigned int total_dofs = fe.dofs_per_cell;
779  std::vector<types::global_dof_index> dofs_on_this_cell(total_dofs);
780  std::vector<types::global_dof_index> dofs_on_other_cell(total_dofs);
781  Table<2, bool> support_on_face(total_dofs,
783 
784  typename DoFHandler<dim, spacedim>::cell_iterator cell = dof.begin(level),
785  endc = dof.end(level);
786 
788  int_dof_mask =
790  flux_dof_mask =
792 
793  for (unsigned int i = 0; i < total_dofs; ++i)
794  for (unsigned int f = 0; f < GeometryInfo<dim>::faces_per_cell; ++f)
795  support_on_face(i, f) = fe.has_support_on_face(i, f);
796 
797  // Clear user flags because we will
798  // need them. But first we save
799  // them and make sure that we
800  // restore them later such that at
801  // the end of this function the
802  // Triangulation will be in the
803  // same state as it was at the
804  // beginning of this function.
805  std::vector<bool> user_flags;
806  dof.get_triangulation().save_user_flags(user_flags);
808  .clear_user_flags();
809 
810  for (; cell != endc; ++cell)
811  {
812  if (!cell->is_locally_owned_on_level())
813  continue;
814 
815  cell->get_mg_dof_indices(dofs_on_this_cell);
816  // make sparsity pattern for this cell
817  for (unsigned int i = 0; i < total_dofs; ++i)
818  for (unsigned int j = 0; j < total_dofs; ++j)
819  if (int_dof_mask[i][j] != DoFTools::none)
820  sparsity.add(dofs_on_this_cell[i], dofs_on_this_cell[j]);
821 
822  // Loop over all interior neighbors
823  for (unsigned int face = 0; face < GeometryInfo<dim>::faces_per_cell;
824  ++face)
825  {
826  typename DoFHandler<dim, spacedim>::face_iterator cell_face =
827  cell->face(face);
828  if (cell_face->user_flag_set())
829  continue;
830 
831  if (cell->at_boundary(face) && !cell->has_periodic_neighbor(face))
832  {
833  for (unsigned int i = 0; i < total_dofs; ++i)
834  {
835  const bool i_non_zero_i = support_on_face(i, face);
836  for (unsigned int j = 0; j < total_dofs; ++j)
837  {
838  const bool j_non_zero_i = support_on_face(j, face);
839 
840  if (flux_dof_mask(i, j) == DoFTools::always)
841  sparsity.add(dofs_on_this_cell[i],
842  dofs_on_this_cell[j]);
843  if (flux_dof_mask(i, j) == DoFTools::nonzero &&
844  i_non_zero_i && j_non_zero_i)
845  sparsity.add(dofs_on_this_cell[i],
846  dofs_on_this_cell[j]);
847  }
848  }
849  }
850  else
851  {
852  typename DoFHandler<dim, spacedim>::cell_iterator neighbor =
853  cell->neighbor_or_periodic_neighbor(face);
854 
855  if (neighbor->level() < cell->level())
856  continue;
857 
858  unsigned int neighbor_face =
859  cell->has_periodic_neighbor(face) ?
860  cell->periodic_neighbor_of_periodic_neighbor(face) :
861  cell->neighbor_of_neighbor(face);
862 
863  neighbor->get_mg_dof_indices(dofs_on_other_cell);
864  for (unsigned int i = 0; i < total_dofs; ++i)
865  {
866  const bool i_non_zero_i = support_on_face(i, face);
867  const bool i_non_zero_e = support_on_face(i, neighbor_face);
868  for (unsigned int j = 0; j < total_dofs; ++j)
869  {
870  const bool j_non_zero_i = support_on_face(j, face);
871  const bool j_non_zero_e =
872  support_on_face(j, neighbor_face);
873  if (flux_dof_mask(i, j) == DoFTools::always)
874  {
875  sparsity.add(dofs_on_this_cell[i],
876  dofs_on_other_cell[j]);
877  sparsity.add(dofs_on_other_cell[i],
878  dofs_on_this_cell[j]);
879  sparsity.add(dofs_on_this_cell[i],
880  dofs_on_this_cell[j]);
881  sparsity.add(dofs_on_other_cell[i],
882  dofs_on_other_cell[j]);
883  }
884  if (flux_dof_mask(i, j) == DoFTools::nonzero)
885  {
886  if (i_non_zero_i && j_non_zero_e)
887  sparsity.add(dofs_on_this_cell[i],
888  dofs_on_other_cell[j]);
889  if (i_non_zero_e && j_non_zero_i)
890  sparsity.add(dofs_on_other_cell[i],
891  dofs_on_this_cell[j]);
892  if (i_non_zero_i && j_non_zero_i)
893  sparsity.add(dofs_on_this_cell[i],
894  dofs_on_this_cell[j]);
895  if (i_non_zero_e && j_non_zero_e)
896  sparsity.add(dofs_on_other_cell[i],
897  dofs_on_other_cell[j]);
898  }
899 
900  if (flux_dof_mask(j, i) == DoFTools::always)
901  {
902  sparsity.add(dofs_on_this_cell[j],
903  dofs_on_other_cell[i]);
904  sparsity.add(dofs_on_other_cell[j],
905  dofs_on_this_cell[i]);
906  sparsity.add(dofs_on_this_cell[j],
907  dofs_on_this_cell[i]);
908  sparsity.add(dofs_on_other_cell[j],
909  dofs_on_other_cell[i]);
910  }
911  if (flux_dof_mask(j, i) == DoFTools::nonzero)
912  {
913  if (j_non_zero_i && i_non_zero_e)
914  sparsity.add(dofs_on_this_cell[j],
915  dofs_on_other_cell[i]);
916  if (j_non_zero_e && i_non_zero_i)
917  sparsity.add(dofs_on_other_cell[j],
918  dofs_on_this_cell[i]);
919  if (j_non_zero_i && i_non_zero_i)
920  sparsity.add(dofs_on_this_cell[j],
921  dofs_on_this_cell[i]);
922  if (j_non_zero_e && i_non_zero_e)
923  sparsity.add(dofs_on_other_cell[j],
924  dofs_on_other_cell[i]);
925  }
926  }
927  }
928  neighbor->face(neighbor_face)->set_user_flag();
929  }
930  }
931  }
932 
933  // finally restore the user flags
935  .load_user_flags(user_flags);
936  }
937 
938 
939 
940  template <int dim, typename SparsityPatternType, int spacedim>
941  void
943  SparsityPatternType & sparsity,
944  const unsigned int level,
945  const Table<2, DoFTools::Coupling> &flux_mask)
946  {
947  const FiniteElement<dim> &fe = dof.get_fe();
948  const unsigned int n_comp = fe.n_components();
949  (void)n_comp;
950 
951  Assert((level >= 1) && (level < dof.get_triangulation().n_global_levels()),
952  ExcIndexRange(level, 1, dof.get_triangulation().n_global_levels()));
953 
954  const types::global_dof_index fine_dofs = dof.n_dofs(level);
955  const types::global_dof_index coarse_dofs = dof.n_dofs(level - 1);
956  (void)fine_dofs;
957  (void)coarse_dofs;
958 
959  // Matrix maps from fine level to coarse level
960 
961  Assert(sparsity.n_rows() == coarse_dofs,
962  ExcDimensionMismatch(sparsity.n_rows(), coarse_dofs));
963  Assert(sparsity.n_cols() == fine_dofs,
964  ExcDimensionMismatch(sparsity.n_cols(), fine_dofs));
965  Assert(flux_mask.n_rows() == n_comp,
966  ExcDimensionMismatch(flux_mask.n_rows(), n_comp));
967  Assert(flux_mask.n_cols() == n_comp,
968  ExcDimensionMismatch(flux_mask.n_cols(), n_comp));
969 
970  const unsigned int dofs_per_cell = dof.get_fe().dofs_per_cell;
971  std::vector<types::global_dof_index> dofs_on_this_cell(dofs_per_cell);
972  std::vector<types::global_dof_index> dofs_on_other_cell(dofs_per_cell);
973  Table<2, bool> support_on_face(dofs_per_cell,
975 
976  typename DoFHandler<dim, spacedim>::cell_iterator cell = dof.begin(level),
977  endc = dof.end(level);
978 
979  const Table<2, DoFTools::Coupling> flux_dof_mask =
981 
982  for (unsigned int i = 0; i < dofs_per_cell; ++i)
983  for (unsigned int f = 0; f < GeometryInfo<dim>::faces_per_cell; ++f)
984  support_on_face(i, f) = fe.has_support_on_face(i, f);
985 
986  for (; cell != endc; ++cell)
987  {
988  if (!cell->is_locally_owned_on_level())
989  continue;
990 
991  cell->get_mg_dof_indices(dofs_on_this_cell);
992  // Loop over all interior neighbors
993  for (unsigned int face = 0; face < GeometryInfo<dim>::faces_per_cell;
994  ++face)
995  {
996  // Neighbor is coarser
997  bool use_face = false;
998  if ((!cell->at_boundary(face)) &&
999  (static_cast<unsigned int>(cell->neighbor_level(face)) !=
1000  level))
1001  use_face = true;
1002  else if (cell->has_periodic_neighbor(face) &&
1003  (static_cast<unsigned int>(
1004  cell->periodic_neighbor_level(face)) != level))
1005  use_face = true;
1006 
1007  if (use_face)
1008  {
1009  typename DoFHandler<dim, spacedim>::cell_iterator neighbor =
1010  cell->neighbor_or_periodic_neighbor(face);
1011  neighbor->get_mg_dof_indices(dofs_on_other_cell);
1012 
1013  for (unsigned int i = 0; i < dofs_per_cell; ++i)
1014  {
1015  for (unsigned int j = 0; j < dofs_per_cell; ++j)
1016  {
1017  if (flux_dof_mask(i, j) != DoFTools::none)
1018  {
1019  sparsity.add(dofs_on_other_cell[i],
1020  dofs_on_this_cell[j]);
1021  sparsity.add(dofs_on_other_cell[j],
1022  dofs_on_this_cell[i]);
1023  }
1024  }
1025  }
1026  }
1027  }
1028  }
1029  }
1030 
1031 
1032 
1033  template <typename DoFHandlerType, typename SparsityPatternType>
1034  void
1035  make_interface_sparsity_pattern(const DoFHandlerType & dof,
1036  const MGConstrainedDoFs &mg_constrained_dofs,
1037  SparsityPatternType & sparsity,
1038  const unsigned int level)
1039  {
1040  const types::global_dof_index n_dofs = dof.n_dofs(level);
1041  (void)n_dofs;
1042 
1043  Assert(sparsity.n_rows() == n_dofs,
1044  ExcDimensionMismatch(sparsity.n_rows(), n_dofs));
1045  Assert(sparsity.n_cols() == n_dofs,
1046  ExcDimensionMismatch(sparsity.n_cols(), n_dofs));
1047 
1048  const unsigned int dofs_per_cell = dof.get_fe().dofs_per_cell;
1049  std::vector<types::global_dof_index> dofs_on_this_cell(dofs_per_cell);
1050  typename DoFHandlerType::cell_iterator cell = dof.begin(level),
1051  endc = dof.end(level);
1052  for (; cell != endc; ++cell)
1053  if (cell->is_locally_owned_on_level())
1054  {
1055  cell->get_mg_dof_indices(dofs_on_this_cell);
1056  for (unsigned int i = 0; i < dofs_per_cell; ++i)
1057  for (unsigned int j = 0; j < dofs_per_cell; ++j)
1058  if (mg_constrained_dofs.is_interface_matrix_entry(
1059  level, dofs_on_this_cell[i], dofs_on_this_cell[j]))
1060  sparsity.add(dofs_on_this_cell[i], dofs_on_this_cell[j]);
1061  }
1062  }
1063 
1064 
1065 
1066  template <int dim, int spacedim>
1067  void
1069  const DoFHandler<dim, spacedim> & dof_handler,
1070  std::vector<std::vector<types::global_dof_index>> &result,
1071  bool only_once,
1072  std::vector<unsigned int> target_component)
1073  {
1074  const FiniteElement<dim> &fe = dof_handler.get_fe();
1075  const unsigned int n_components = fe.n_components();
1076  const unsigned int nlevels =
1077  dof_handler.get_triangulation().n_global_levels();
1078 
1079  Assert(result.size() == nlevels,
1080  ExcDimensionMismatch(result.size(), nlevels));
1081 
1082  if (target_component.size() == 0)
1083  {
1084  target_component.resize(n_components);
1085  for (unsigned int i = 0; i < n_components; ++i)
1086  target_component[i] = i;
1087  }
1088 
1089  Assert(target_component.size() == n_components,
1090  ExcDimensionMismatch(target_component.size(), n_components));
1091 
1092  for (unsigned int l = 0; l < nlevels; ++l)
1093  {
1094  result[l].resize(n_components);
1095  std::fill(result[l].begin(), result[l].end(), 0U);
1096 
1097  // special case for only one
1098  // component. treat this first
1099  // since it does not require any
1100  // computations
1101  if (n_components == 1)
1102  {
1103  result[l][0] = dof_handler.n_dofs(l);
1104  }
1105  else
1106  {
1107  // otherwise determine the number
1108  // of dofs in each component
1109  // separately. do so in parallel
1110  std::vector<std::vector<bool>> dofs_in_component(
1111  n_components, std::vector<bool>(dof_handler.n_dofs(l), false));
1112  std::vector<ComponentMask> component_select(n_components);
1113  Threads::TaskGroup<> tasks;
1114  for (unsigned int i = 0; i < n_components; ++i)
1115  {
1116  void (*fun_ptr)(const unsigned int level,
1117  const DoFHandler<dim, spacedim> &,
1118  const ComponentMask &,
1119  std::vector<bool> &) =
1120  &DoFTools::extract_level_dofs<DoFHandler<dim, spacedim>>;
1121 
1122  std::vector<bool> tmp(n_components, false);
1123  tmp[i] = true;
1124  component_select[i] = ComponentMask(tmp);
1125 
1126  tasks += Threads::new_task(fun_ptr,
1127  l,
1128  dof_handler,
1129  component_select[i],
1130  dofs_in_component[i]);
1131  }
1132  tasks.join_all();
1133 
1134  // next count what we got
1135  unsigned int component = 0;
1136  for (unsigned int b = 0; b < fe.n_base_elements(); ++b)
1137  {
1138  const FiniteElement<dim> &base = fe.base_element(b);
1139  // Dimension of base element
1140  unsigned int d = base.n_components();
1141 
1142  for (unsigned int m = 0; m < fe.element_multiplicity(b); ++m)
1143  {
1144  for (unsigned int dd = 0; dd < d; ++dd)
1145  {
1146  if (base.is_primitive() || (!only_once || dd == 0))
1147  result[l][target_component[component]] +=
1148  std::count(dofs_in_component[component].begin(),
1149  dofs_in_component[component].end(),
1150  true);
1151  ++component;
1152  }
1153  }
1154  }
1155  // finally sanity check
1156  Assert(!dof_handler.get_fe().is_primitive() ||
1157  std::accumulate(result[l].begin(),
1158  result[l].end(),
1160  dof_handler.n_dofs(l),
1161  ExcInternalError());
1162  }
1163  }
1164  }
1165 
1166 
1167 
1168  template <typename DoFHandlerType>
1169  void
1171  const DoFHandlerType & dof_handler,
1172  std::vector<std::vector<types::global_dof_index>> &dofs_per_block,
1173  std::vector<unsigned int> target_block)
1174  {
1175  const FiniteElement<DoFHandlerType::dimension,
1176  DoFHandlerType::space_dimension> &fe =
1177  dof_handler.get_fe();
1178  const unsigned int n_blocks = fe.n_blocks();
1179  const unsigned int n_levels =
1180  dof_handler.get_triangulation().n_global_levels();
1181 
1182  AssertDimension(dofs_per_block.size(), n_levels);
1183 
1184  for (unsigned int l = 0; l < n_levels; ++l)
1185  std::fill(dofs_per_block[l].begin(), dofs_per_block[l].end(), 0U);
1186  // If the empty vector was given as
1187  // default argument, set up this
1188  // vector as identity.
1189  if (target_block.size() == 0)
1190  {
1191  target_block.resize(n_blocks);
1192  for (unsigned int i = 0; i < n_blocks; ++i)
1193  target_block[i] = i;
1194  }
1195  Assert(target_block.size() == n_blocks,
1196  ExcDimensionMismatch(target_block.size(), n_blocks));
1197 
1198  const unsigned int max_block =
1199  *std::max_element(target_block.begin(), target_block.end());
1200  const unsigned int n_target_blocks = max_block + 1;
1201  (void)n_target_blocks;
1202 
1203  for (unsigned int l = 0; l < n_levels; ++l)
1204  AssertDimension(dofs_per_block[l].size(), n_target_blocks);
1205 
1206  // special case for only one
1207  // block. treat this first
1208  // since it does not require any
1209  // computations
1210  if (n_blocks == 1)
1211  {
1212  for (unsigned int l = 0; l < n_levels; ++l)
1213  dofs_per_block[l][0] = dof_handler.n_dofs(l);
1214  return;
1215  }
1216  // otherwise determine the number
1217  // of dofs in each block
1218  // separately. do so in parallel
1219  for (unsigned int l = 0; l < n_levels; ++l)
1220  {
1221  std::vector<std::vector<bool>> dofs_in_block(
1222  n_blocks, std::vector<bool>(dof_handler.n_dofs(l), false));
1223  std::vector<BlockMask> block_select(n_blocks);
1224  Threads::TaskGroup<> tasks;
1225  for (unsigned int i = 0; i < n_blocks; ++i)
1226  {
1227  void (*fun_ptr)(const unsigned int level,
1228  const DoFHandlerType &,
1229  const BlockMask &,
1230  std::vector<bool> &) =
1231  &DoFTools::extract_level_dofs<DoFHandlerType>;
1232 
1233  std::vector<bool> tmp(n_blocks, false);
1234  tmp[i] = true;
1235  block_select[i] = tmp;
1236 
1237  tasks += Threads::new_task(
1238  fun_ptr, l, dof_handler, block_select[i], dofs_in_block[i]);
1239  }
1240  tasks.join_all();
1241 
1242  // next count what we got
1243  for (unsigned int block = 0; block < fe.n_blocks(); ++block)
1244  dofs_per_block[l][target_block[block]] +=
1245  std::count(dofs_in_block[block].begin(),
1246  dofs_in_block[block].end(),
1247  true);
1248  }
1249  }
1250 
1251 
1252 
1253  template <int dim, int spacedim>
1254  void
1256  const DoFHandler<dim, spacedim> &dof,
1257  const std::map<types::boundary_id, const Function<spacedim> *>
1258  & function_map,
1259  std::vector<std::set<types::global_dof_index>> &boundary_indices,
1260  const ComponentMask & component_mask)
1261  {
1262  Assert(boundary_indices.size() == dof.get_triangulation().n_global_levels(),
1263  ExcDimensionMismatch(boundary_indices.size(),
1264  dof.get_triangulation().n_global_levels()));
1265 
1266  std::set<types::boundary_id> boundary_ids;
1267  for (const auto &boundary_function : function_map)
1268  boundary_ids.insert(boundary_function.first);
1269 
1270  std::vector<IndexSet> boundary_indexset;
1271  make_boundary_list(dof, boundary_ids, boundary_indexset, component_mask);
1272  for (unsigned int i = 0; i < dof.get_triangulation().n_global_levels(); ++i)
1273  boundary_indices[i].insert(boundary_indexset[i].begin(),
1274  boundary_indexset[i].end());
1275  }
1276 
1277 
1278  template <int dim, int spacedim>
1279  void
1281  const std::map<types::boundary_id,
1282  const Function<spacedim> *> &function_map,
1283  std::vector<IndexSet> &boundary_indices,
1284  const ComponentMask & component_mask)
1285  {
1286  Assert(boundary_indices.size() == dof.get_triangulation().n_global_levels(),
1287  ExcDimensionMismatch(boundary_indices.size(),
1288  dof.get_triangulation().n_global_levels()));
1289 
1290  std::set<types::boundary_id> boundary_ids;
1291  for (const auto &boundary_function : function_map)
1292  boundary_ids.insert(boundary_function.first);
1293 
1294  make_boundary_list(dof, boundary_ids, boundary_indices, component_mask);
1295  }
1296 
1297 
1298 
1299  template <int dim, int spacedim>
1300  void
1302  const std::set<types::boundary_id> &boundary_ids,
1303  std::vector<IndexSet> & boundary_indices,
1304  const ComponentMask & component_mask)
1305  {
1306  boundary_indices.resize(dof.get_triangulation().n_global_levels());
1307 
1308  // if for whatever reason we were passed an empty set, return immediately
1309  if (boundary_ids.size() == 0)
1310  return;
1311 
1312  for (unsigned int i = 0; i < dof.get_triangulation().n_global_levels(); ++i)
1313  if (boundary_indices[i].size() == 0)
1314  boundary_indices[i] = IndexSet(dof.n_dofs(i));
1315 
1316  const unsigned int n_components = DoFTools::n_components(dof);
1317  const bool fe_is_system = (n_components != 1);
1318 
1319  std::vector<types::global_dof_index> local_dofs;
1320  local_dofs.reserve(DoFTools::max_dofs_per_face(dof));
1321  std::fill(local_dofs.begin(), local_dofs.end(), numbers::invalid_dof_index);
1322 
1323  // First, deal with the simpler case when we have to identify all boundary
1324  // dofs
1325  if (component_mask.n_selected_components(n_components) == n_components)
1326  {
1327  typename DoFHandler<dim, spacedim>::cell_iterator cell = dof.begin(),
1328  endc = dof.end();
1329  for (; cell != endc; ++cell)
1330  {
1331  if (dof.get_triangulation().locally_owned_subdomain() !=
1333  cell->level_subdomain_id() == numbers::artificial_subdomain_id)
1334  continue;
1335  const FiniteElement<dim> &fe = cell->get_fe();
1336  const unsigned int level = cell->level();
1337  local_dofs.resize(fe.dofs_per_face);
1338 
1339  for (unsigned int face_no = 0;
1340  face_no < GeometryInfo<dim>::faces_per_cell;
1341  ++face_no)
1342  if (cell->at_boundary(face_no) == true)
1343  {
1344  const typename DoFHandler<dim, spacedim>::face_iterator face =
1345  cell->face(face_no);
1346  const types::boundary_id bi = face->boundary_id();
1347  // Face is listed in boundary map
1348  if (boundary_ids.find(bi) != boundary_ids.end())
1349  {
1350  face->get_mg_dof_indices(level, local_dofs);
1351  boundary_indices[level].add_indices(local_dofs.begin(),
1352  local_dofs.end());
1353  }
1354  }
1355  }
1356  }
1357  else
1358  {
1359  Assert(component_mask.n_selected_components(n_components) > 0,
1360  ExcMessage(
1361  "It's probably worthwhile to select at least one component."));
1362 
1363  typename DoFHandler<dim, spacedim>::cell_iterator cell = dof.begin(),
1364  endc = dof.end();
1365  for (; cell != endc; ++cell)
1366  if (dof.get_triangulation().locally_owned_subdomain() ==
1368  cell->level_subdomain_id() != numbers::artificial_subdomain_id)
1369  for (unsigned int face_no = 0;
1370  face_no < GeometryInfo<dim>::faces_per_cell;
1371  ++face_no)
1372  {
1373  if (cell->at_boundary(face_no) == false)
1374  continue;
1375 
1376  const FiniteElement<dim> &fe = cell->get_fe();
1377  const unsigned int level = cell->level();
1378 
1380  cell->face(face_no);
1381  const types::boundary_id boundary_component =
1382  face->boundary_id();
1383  if (boundary_ids.find(boundary_component) != boundary_ids.end())
1384  // we want to constrain this boundary
1385  {
1386  for (unsigned int i = 0; i < cell->get_fe().dofs_per_cell;
1387  ++i)
1388  {
1389  const ComponentMask &nonzero_component_array =
1390  cell->get_fe().get_nonzero_components(i);
1391  // if we want to constrain one of the nonzero
1392  // components, we have to constrain all of them
1393 
1394  bool selected = false;
1395  for (unsigned int c = 0; c < n_components; ++c)
1396  if (nonzero_component_array[c] == true &&
1397  component_mask[c] == true)
1398  {
1399  selected = true;
1400  break;
1401  }
1402  if (selected)
1403  for (unsigned int c = 0; c < n_components; ++c)
1404  Assert(
1405  nonzero_component_array[c] == false ||
1406  component_mask[c] == true,
1407  ExcMessage(
1408  "You are using a non-primitive FiniteElement "
1409  "and try to constrain just some of its components!"));
1410  }
1411 
1412  // get indices, physical location and boundary values of
1413  // dofs on this face
1414  local_dofs.resize(fe.dofs_per_face);
1415  face->get_mg_dof_indices(level, local_dofs);
1416  if (fe_is_system)
1417  {
1418  for (unsigned int i = 0; i < local_dofs.size(); ++i)
1419  {
1420  unsigned int component =
1422  if (fe.is_primitive())
1423  component =
1424  fe.face_system_to_component_index(i).first;
1425  else
1426  {
1427  // Just pick the first of the components
1428  // We already know that either all or none
1429  // of the components are selected
1430  const ComponentMask &nonzero_component_array =
1431  cell->get_fe().get_nonzero_components(i);
1432  for (unsigned int c = 0; c < n_components; ++c)
1433  if (nonzero_component_array[c] == true)
1434  {
1435  component = c;
1436  break;
1437  }
1438  }
1440  ExcInternalError());
1441  if (component_mask[component] == true)
1442  boundary_indices[level].add_index(local_dofs[i]);
1443  }
1444  }
1445  else
1446  boundary_indices[level].add_indices(local_dofs.begin(),
1447  local_dofs.end());
1448  }
1449  }
1450  }
1451  }
1452 
1453 
1454  template <int dim, int spacedim>
1455  void
1456  extract_non_interface_dofs(
1457  const DoFHandler<dim, spacedim> & mg_dof_handler,
1458  std::vector<std::set<types::global_dof_index>> &non_interface_dofs)
1459  {
1460  Assert(non_interface_dofs.size() ==
1461  mg_dof_handler.get_triangulation().n_global_levels(),
1463  non_interface_dofs.size(),
1464  mg_dof_handler.get_triangulation().n_global_levels()));
1465 
1466  const FiniteElement<dim, spacedim> &fe = mg_dof_handler.get_fe();
1467 
1468  const unsigned int dofs_per_cell = fe.dofs_per_cell;
1469  const unsigned int dofs_per_face = fe.dofs_per_face;
1470 
1471  std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1472  std::vector<bool> cell_dofs(dofs_per_cell, false);
1473  std::vector<bool> cell_dofs_interface(dofs_per_cell, false);
1474 
1475  typename DoFHandler<dim>::cell_iterator cell = mg_dof_handler.begin(),
1476  endc = mg_dof_handler.end();
1477 
1478 
1479  for (; cell != endc; ++cell)
1480  {
1481  if (mg_dof_handler.get_triangulation().locally_owned_subdomain() !=
1483  cell->level_subdomain_id() !=
1484  mg_dof_handler.get_triangulation().locally_owned_subdomain())
1485  continue;
1486 
1487  std::fill(cell_dofs.begin(), cell_dofs.end(), false);
1488  std::fill(cell_dofs_interface.begin(),
1489  cell_dofs_interface.end(),
1490  false);
1491 
1492  for (unsigned int face_nr = 0;
1493  face_nr < GeometryInfo<dim>::faces_per_cell;
1494  ++face_nr)
1495  {
1496  const typename DoFHandler<dim, spacedim>::face_iterator face =
1497  cell->face(face_nr);
1498  if (!face->at_boundary())
1499  {
1500  // interior face
1501  const typename DoFHandler<dim>::cell_iterator neighbor =
1502  cell->neighbor(face_nr);
1503 
1504  if ((neighbor->level() < cell->level()))
1505  {
1506  for (unsigned int j = 0; j < dofs_per_face; ++j)
1507  cell_dofs_interface[fe.face_to_cell_index(j, face_nr)] =
1508  true;
1509  }
1510  else
1511  {
1512  for (unsigned int j = 0; j < dofs_per_face; ++j)
1513  cell_dofs[fe.face_to_cell_index(j, face_nr)] = true;
1514  }
1515  }
1516  else
1517  {
1518  // boundary face
1519  for (unsigned int j = 0; j < dofs_per_face; ++j)
1520  cell_dofs[fe.face_to_cell_index(j, face_nr)] = true;
1521  }
1522  }
1523 
1524  const unsigned int level = cell->level();
1525  cell->get_mg_dof_indices(local_dof_indices);
1526 
1527  for (unsigned int i = 0; i < dofs_per_cell; ++i)
1528  if (cell_dofs[i] && !cell_dofs_interface[i])
1529  non_interface_dofs[level].insert(local_dof_indices[i]);
1530  }
1531  }
1532 
1533 
1534  template <int dim, int spacedim>
1535  void
1537  std::vector<IndexSet> & interface_dofs)
1538  {
1539  Assert(interface_dofs.size() ==
1540  mg_dof_handler.get_triangulation().n_global_levels(),
1542  interface_dofs.size(),
1543  mg_dof_handler.get_triangulation().n_global_levels()));
1544 
1545  std::vector<std::vector<types::global_dof_index>> tmp_interface_dofs(
1546  interface_dofs.size());
1547 
1548  const FiniteElement<dim, spacedim> &fe = mg_dof_handler.get_fe();
1549 
1550  const unsigned int dofs_per_cell = fe.dofs_per_cell;
1551  const unsigned int dofs_per_face = fe.dofs_per_face;
1552 
1553  std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1554 
1555  std::vector<bool> cell_dofs(dofs_per_cell, false);
1556 
1557  typename DoFHandler<dim>::cell_iterator cell = mg_dof_handler.begin(),
1558  endc = mg_dof_handler.end();
1559 
1560  for (; cell != endc; ++cell)
1561  {
1562  // Do not look at artificial level cells (in a serial computation we
1563  // need to ignore the level_subdomain_id() because it is never set).
1564  if (mg_dof_handler.get_triangulation().locally_owned_subdomain() !=
1566  cell->level_subdomain_id() == numbers::artificial_subdomain_id)
1567  continue;
1568 
1569  bool has_coarser_neighbor = false;
1570 
1571  std::fill(cell_dofs.begin(), cell_dofs.end(), false);
1572 
1573  for (unsigned int face_nr = 0;
1574  face_nr < GeometryInfo<dim>::faces_per_cell;
1575  ++face_nr)
1576  {
1577  const typename DoFHandler<dim, spacedim>::face_iterator face =
1578  cell->face(face_nr);
1579  if (!face->at_boundary())
1580  {
1581  // interior face
1582  const typename DoFHandler<dim>::cell_iterator neighbor =
1583  cell->neighbor(face_nr);
1584 
1585  // only process cell pairs if one or both of them are owned by
1586  // me (ignore if running in serial)
1587  if (mg_dof_handler.get_triangulation()
1588  .locally_owned_subdomain() !=
1590  neighbor->level_subdomain_id() ==
1592  continue;
1593 
1594  // Do refinement face from the coarse side
1595  if (neighbor->level() < cell->level())
1596  {
1597  for (unsigned int j = 0; j < dofs_per_face; ++j)
1598  cell_dofs[fe.face_to_cell_index(j, face_nr)] = true;
1599 
1600  has_coarser_neighbor = true;
1601  }
1602  }
1603  }
1604 
1605  if (has_coarser_neighbor == false)
1606  continue;
1607 
1608  const unsigned int level = cell->level();
1609  cell->get_mg_dof_indices(local_dof_indices);
1610 
1611  for (unsigned int i = 0; i < dofs_per_cell; ++i)
1612  {
1613  if (cell_dofs[i])
1614  tmp_interface_dofs[level].push_back(local_dof_indices[i]);
1615  }
1616  }
1617 
1618  for (unsigned int l = 0;
1619  l < mg_dof_handler.get_triangulation().n_global_levels();
1620  ++l)
1621  {
1622  interface_dofs[l].clear();
1623  std::sort(tmp_interface_dofs[l].begin(), tmp_interface_dofs[l].end());
1624  interface_dofs[l].add_indices(tmp_interface_dofs[l].begin(),
1625  std::unique(tmp_interface_dofs[l].begin(),
1626  tmp_interface_dofs[l].end()));
1627  interface_dofs[l].compress();
1628  }
1629  }
1630 
1631 
1632 
1633  template <int dim, int spacedim>
1634  unsigned int
1636  {
1637  // Find minimum level for an active cell in
1638  // this locally owned subdomain
1639  // Note: with the way active cells are traversed,
1640  // the first locally owned cell we find will have
1641  // the lowest level in the particular subdomain.
1642  unsigned int min_level = tria.n_global_levels();
1644  cell = tria.begin_active(),
1645  endc = tria.end();
1646  for (; cell != endc; ++cell)
1647  if (cell->is_locally_owned())
1648  {
1649  min_level = cell->level();
1650  break;
1651  }
1652 
1653  unsigned int global_min = min_level;
1654  // If necessary, communicate to find minimum
1655  // level for an active cell over all subdomains
1657  dynamic_cast<const parallel::Triangulation<dim, spacedim> *>(&tria))
1658  global_min = Utilities::MPI::min(min_level, tr->get_communicator());
1659 
1660  AssertIndexRange(global_min, tria.n_global_levels());
1661 
1662  return global_min;
1663  }
1664 } // namespace MGTools
1665 
1666 
1667 // explicit instantiations
1668 #include "mg_tools.inst"
1669 
1670 DEAL_II_NAMESPACE_CLOSE
const unsigned int first_hex_index
Definition: fe_base.h:273
bool is_interface_matrix_entry(const unsigned int level, const types::global_dof_index i, const types::global_dof_index j) const
static const unsigned int invalid_unsigned_int
Definition: types.h:173
active_cell_iterator begin_active(const unsigned int level=0) const
Definition: dof_handler.cc:943
std::pair< unsigned int, types::global_dof_index > system_to_block_index(const unsigned int component) const
Definition: fe.h:3235
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1366
const types::subdomain_id invalid_subdomain_id
Definition: types.h:255
void clear_user_flags()
Definition: tria.cc:11096
cell_iterator end() const
Definition: tria.cc:12004
virtual unsigned int n_global_levels() const
Task< RT > new_task(const std::function< RT()> &function)
unsigned int n_selected_components(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
void load_user_flags(std::istream &in)
Definition: tria.cc:11156
void make_flux_sparsity_pattern(const DoFHandler< dim, spacedim > &dof_handler, SparsityPatternType &sparsity, const unsigned int level)
Definition: mg_tools.cc:607
#define AssertIndexRange(index, range)
Definition: exceptions.h:1407
void make_sparsity_pattern(const DoFHandlerType &dof_handler, SparsityPatternType &sparsity, const unsigned int level)
Definition: mg_tools.cc:573
const Triangulation< dim, spacedim > & get_triangulation() const
bool is_primitive() const
Definition: fe.h:3285
virtual bool has_support_on_face(const unsigned int shape_index, const unsigned int face_index) const
Definition: fe.cc:1123
static::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
Table< 2, Coupling > dof_couplings_from_component_couplings(const FiniteElement< dim, spacedim > &fe, const Table< 2, Coupling > &component_couplings)
typename ActiveSelector::face_iterator face_iterator
Definition: dof_handler.h:286
unsigned long long int global_dof_index
Definition: types.h:72
unsigned int element_multiplicity(const unsigned int index) const
Definition: fe.h:3111
void make_interface_sparsity_pattern(const DoFHandlerType &dof_handler, const MGConstrainedDoFs &mg_constrained_dofs, SparsityPatternType &sparsity, const unsigned int level)
Definition: mg_tools.cc:1035
static::ExceptionBase & ExcMessage(std::string arg1)
unsigned int max_dofs_per_face(const DoFHandler< dim, spacedim > &dh)
const unsigned int first_quad_index
Definition: fe_base.h:268
void convert_couplings_to_blocks(const hp::DoFHandler< dim, spacedim > &dof_handler, const Table< 2, Coupling > &table_by_component, std::vector< Table< 2, Coupling >> &tables_by_block)
Definition: dof_tools.cc:2387
#define Assert(cond, exc)
Definition: exceptions.h:1227
types::global_dof_index n_dofs() const
void save_user_flags(std::ostream &out) const
Definition: tria.cc:11107
static::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
unsigned int n_base_elements() const
Definition: fe.h:3102
unsigned int n_components() const
std::pair< unsigned int, unsigned int > face_system_to_component_index(const unsigned int index) const
Definition: fe.h:3148
cell_iterator end() const
Definition: dof_handler.cc:959
const unsigned int dofs_per_cell
Definition: fe_base.h:297
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=std::vector< unsigned int >())
Definition: mg_tools.cc:1068
const types::subdomain_id artificial_subdomain_id
Definition: types.h:272
cell_iterator begin(const unsigned int level=0) const
Definition: dof_handler.cc:930
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:105
virtual unsigned int face_to_cell_index(const unsigned int face_dof_index, const unsigned int face, const bool face_orientation=true, const bool face_flip=false, const bool face_rotation=false) const
Definition: fe.cc:551
const FiniteElement< dim, spacedim > & get_fe(const unsigned int index=0) const
std::pair< unsigned int, unsigned int > system_to_component_index(const unsigned int index) const
Definition: fe.h:3087
T min(const T &t, const MPI_Comm &mpi_communicator)
const unsigned int dofs_per_face
Definition: fe_base.h:290
const unsigned int first_line_index
Definition: fe_base.h:263
void extract_inner_interface_dofs(const DoFHandler< dim, spacedim > &mg_dof_handler, std::vector< IndexSet > &interface_dofs)
Definition: mg_tools.cc:1536
static::ExceptionBase & ExcNotImplemented()
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=ComponentMask())
Definition: mg_tools.cc:1255
Definition: table.h:37
typename ActiveSelector::cell_iterator cell_iterator
Definition: dof_handler.h:268
void extract_level_dofs(const unsigned int level, const DoFHandlerType &dof, const ComponentMask &component_mask, std::vector< bool > &selected_dofs)
Definition: dof_tools.cc:520
active_cell_iterator begin_active(const unsigned int level=0) const
Definition: tria.cc:11935
typename ActiveSelector::active_cell_iterator active_cell_iterator
Definition: dof_handler.h:240
void make_flux_sparsity_pattern_edge(const DoFHandler< dim, spacedim > &dof_handler, SparsityPatternType &sparsity, const unsigned int level)
Definition: mg_tools.cc:683
const types::global_dof_index invalid_dof_index
Definition: types.h:188
types::global_dof_index first_block_of_base(const unsigned int b) const
Definition: fe.h:3202
unsigned int n_components(const DoFHandler< dim, spacedim > &dh)
virtual const FiniteElement< dim, spacedim > & base_element(const unsigned int index) const
Definition: fe.cc:1287
void count_dofs_per_block(const DoFHandlerType &dof_handler, std::vector< std::vector< types::global_dof_index >> &dofs_per_block, std::vector< unsigned int > target_block=std::vector< unsigned int >())
Definition: mg_tools.cc:1170
unsigned int boundary_id
Definition: types.h:111
unsigned int max_level_for_coarse_mesh(const Triangulation< dim, spacedim > &tria)
Definition: mg_tools.cc:1635
static::ExceptionBase & ExcInternalError()