Reference documentation for deal.II version 9.1.0-pre
mg_transfer_prebuilt.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2003 - 2017 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 
17 #include <deal.II/base/function.h>
18 #include <deal.II/base/logstream.h>
19 
20 #include <deal.II/dofs/dof_accessor.h>
21 #include <deal.II/dofs/dof_tools.h>
22 
23 #include <deal.II/fe/fe.h>
24 
25 #include <deal.II/grid/tria.h>
26 #include <deal.II/grid/tria_iterator.h>
27 
28 #include <deal.II/lac/block_sparse_matrix.h>
29 #include <deal.II/lac/block_vector.h>
30 #include <deal.II/lac/dynamic_sparsity_pattern.h>
31 #include <deal.II/lac/la_parallel_block_vector.h>
32 #include <deal.II/lac/la_parallel_vector.h>
33 #include <deal.II/lac/sparse_matrix.h>
34 #include <deal.II/lac/sparsity_tools.h>
35 #include <deal.II/lac/trilinos_epetra_vector.h>
36 #include <deal.II/lac/trilinos_parallel_block_vector.h>
37 #include <deal.II/lac/trilinos_vector.h>
38 #include <deal.II/lac/vector.h>
39 
40 #include <deal.II/multigrid/mg_tools.h>
41 #include <deal.II/multigrid/mg_transfer.h>
42 
43 #include <algorithm>
44 
45 DEAL_II_NAMESPACE_OPEN
46 
47 
48 template <typename VectorType>
50  const MGConstrainedDoFs &mg_c)
51 {
52  this->mg_constrained_dofs = &mg_c;
53 }
54 
55 
56 
57 template <typename VectorType>
59  const AffineConstraints<double> & /*c*/,
60  const MGConstrainedDoFs &mg_c)
61 {
62  this->mg_constrained_dofs = &mg_c;
63 }
64 
65 
66 
67 template <typename VectorType>
68 void
70  const MGConstrainedDoFs &mg_c)
71 {
72  this->mg_constrained_dofs = &mg_c;
73 }
74 
75 
76 
77 template <typename VectorType>
78 void
80  const AffineConstraints<double> & /*c*/,
81  const MGConstrainedDoFs &mg_c)
82 {
83  initialize_constraints(mg_c);
84 }
85 
86 
87 
88 template <typename VectorType>
89 void
91 {
93  prolongation_matrices.resize(0);
94  prolongation_sparsities.resize(0);
95  interface_dofs.resize(0);
96 }
97 
98 
99 
100 template <typename VectorType>
101 void
102 MGTransferPrebuilt<VectorType>::prolongate(const unsigned int to_level,
103  VectorType & dst,
104  const VectorType & src) const
105 {
106  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
107  ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
108 
109  prolongation_matrices[to_level - 1]->vmult(dst, src);
110 }
111 
112 
113 
114 template <typename VectorType>
115 void
117  VectorType & dst,
118  const VectorType & src) const
119 {
120  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
121  ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
122  (void)from_level;
123 
124  prolongation_matrices[from_level - 1]->Tvmult_add(dst, src);
125 }
126 
127 
128 namespace
129 {
134  void
135  replace(const MGConstrainedDoFs * mg_constrained_dofs,
136  const unsigned int level,
137  std::vector<types::global_dof_index> &dof_indices)
138  {
139  if (mg_constrained_dofs != nullptr &&
140  mg_constrained_dofs->get_level_constraint_matrix(level)
141  .n_constraints() > 0)
142  for (auto &ind : dof_indices)
143  if (mg_constrained_dofs->get_level_constraint_matrix(level)
145  {
146  Assert(mg_constrained_dofs->get_level_constraint_matrix(level)
148  ->size() == 1,
149  ExcInternalError());
150  ind = mg_constrained_dofs->get_level_constraint_matrix(level)
152  ->front()
153  .first;
154  }
155  }
156 } // namespace
157 
158 template <typename VectorType>
159 template <int dim, int spacedim>
160 void
162  const DoFHandler<dim, spacedim> &mg_dof)
163 {
164  const unsigned int n_levels = mg_dof.get_triangulation().n_global_levels();
165  const unsigned int dofs_per_cell = mg_dof.get_fe().dofs_per_cell;
166 
167  this->sizes.resize(n_levels);
168  for (unsigned int l = 0; l < n_levels; ++l)
169  this->sizes[l] = mg_dof.n_dofs(l);
170 
171  // reset the size of the array of
172  // matrices. call resize(0) first,
173  // in order to delete all elements
174  // and clear their memory. then
175  // repopulate these arrays
176  //
177  // note that on resize(0), the
178  // shared_ptr class takes care of
179  // deleting the object it points to
180  // by itself
181  prolongation_matrices.resize(0);
182  prolongation_sparsities.resize(0);
183  prolongation_matrices.reserve(n_levels - 1);
184  prolongation_sparsities.reserve(n_levels - 1);
185 
186  for (unsigned int i = 0; i < n_levels - 1; ++i)
187  {
188  prolongation_sparsities.emplace_back(
190  prolongation_matrices.emplace_back(
192  }
193 
194  // two fields which will store the
195  // indices of the multigrid dofs
196  // for a cell and one of its children
197  std::vector<types::global_dof_index> dof_indices_parent(dofs_per_cell);
198  std::vector<types::global_dof_index> dof_indices_child(dofs_per_cell);
199  std::vector<types::global_dof_index> entries(dofs_per_cell);
200 
201  // for each level: first build the sparsity
202  // pattern of the matrices and then build the
203  // matrices themselves. note that we only
204  // need to take care of cells on the coarser
205  // level which have children
206  for (unsigned int level = 0; level < n_levels - 1; ++level)
207  {
208  // reset the dimension of the structure. note that for the number of
209  // entries per row, the number of parent dofs coupling to a child dof is
210  // necessary. this, of course, is the number of degrees of freedom per
211  // cell
212  //
213  // increment dofs_per_cell since a useless diagonal element will be
214  // stored
215  IndexSet level_p1_relevant_dofs;
217  level + 1,
218  level_p1_relevant_dofs);
219  DynamicSparsityPattern dsp(this->sizes[level + 1],
220  this->sizes[level],
221  level_p1_relevant_dofs);
222  typename DoFHandler<dim>::cell_iterator cell, endc = mg_dof.end(level);
223  for (cell = mg_dof.begin(level); cell != endc; ++cell)
224  if (cell->has_children() &&
225  (mg_dof.get_triangulation().locally_owned_subdomain() ==
227  cell->level_subdomain_id() ==
228  mg_dof.get_triangulation().locally_owned_subdomain()))
229  {
230  cell->get_mg_dof_indices(dof_indices_parent);
231 
232  replace(this->mg_constrained_dofs, level, dof_indices_parent);
233 
234  Assert(cell->n_children() ==
237  for (unsigned int child = 0; child < cell->n_children(); ++child)
238  {
239  // set an alias to the prolongation matrix for this child
240  const FullMatrix<double> &prolongation =
241  mg_dof.get_fe().get_prolongation_matrix(
242  child, cell->refinement_case());
243 
244  Assert(prolongation.n() != 0, ExcNoProlongation());
245 
246  cell->child(child)->get_mg_dof_indices(dof_indices_child);
247 
248  replace(this->mg_constrained_dofs,
249  level + 1,
250  dof_indices_child);
251 
252  // now tag the entries in the
253  // matrix which will be used
254  // for this pair of parent/child
255  for (unsigned int i = 0; i < dofs_per_cell; ++i)
256  {
257  entries.resize(0);
258  for (unsigned int j = 0; j < dofs_per_cell; ++j)
259  if (prolongation(i, j) != 0)
260  entries.push_back(dof_indices_parent[j]);
261  dsp.add_entries(dof_indices_child[i],
262  entries.begin(),
263  entries.end());
264  }
265  }
266  }
267 
268 #ifdef DEAL_II_WITH_MPI
269  if (internal::MatrixSelector<
270  VectorType>::requires_distributed_sparsity_pattern)
271  {
272  // Since PETSc matrices do not offer the functionality to fill up in-
273  // complete sparsity patterns on their own, the sparsity pattern must
274  // be manually distributed.
275 
276  // Retrieve communicator from triangulation if it is parallel
277  const parallel::Triangulation<dim, spacedim> *dist_tria =
278  dynamic_cast<const parallel::Triangulation<dim, spacedim> *>(
279  &(mg_dof.get_triangulation()));
280 
281  MPI_Comm communicator = dist_tria != nullptr ?
282  dist_tria->get_communicator() :
283  MPI_COMM_SELF;
284 
285  // Compute # of locally owned MG dofs / processor for distribution
286  const std::vector<::IndexSet>
287  &locally_owned_mg_dofs_per_processor =
288  mg_dof.locally_owned_mg_dofs_per_processor(level + 1);
289  std::vector<::types::global_dof_index>
290  n_locally_owned_mg_dofs_per_processor(
291  locally_owned_mg_dofs_per_processor.size(), 0);
292 
293  for (size_t index = 0;
294  index < n_locally_owned_mg_dofs_per_processor.size();
295  ++index)
296  {
297  n_locally_owned_mg_dofs_per_processor[index] =
298  locally_owned_mg_dofs_per_processor[index].n_elements();
299  }
300 
301  // Distribute sparsity pattern
302  ::SparsityTools::distribute_sparsity_pattern(
303  dsp,
304  n_locally_owned_mg_dofs_per_processor,
305  communicator,
306  dsp.row_index_set());
307  }
308 #endif
309 
310  internal::MatrixSelector<VectorType>::reinit(
311  *prolongation_matrices[level],
312  *prolongation_sparsities[level],
313  level,
314  dsp,
315  mg_dof);
316  dsp.reinit(0, 0);
317 
318  // In the end, the entries in this object will only be real valued.
319  // Nevertheless, we have to take the underlying scalar type of the
320  // vector we want to use this class with. The global matrix the entries
321  // of this matrix are copied into has to be able to perform a
322  // matrix-vector multiplication and this is in general only implemented if
323  // the scalar type for matrix and vector is the same. Furthermore,
324  // copying entries between this local object and the global matrix is only
325  // implemented if the objects have the same scalar type.
327 
328  // now actually build the matrices
329  for (cell = mg_dof.begin(level); cell != endc; ++cell)
330  if (cell->has_children() &&
331  (mg_dof.get_triangulation().locally_owned_subdomain() ==
333  cell->level_subdomain_id() ==
334  mg_dof.get_triangulation().locally_owned_subdomain()))
335  {
336  cell->get_mg_dof_indices(dof_indices_parent);
337 
338  replace(this->mg_constrained_dofs, level, dof_indices_parent);
339 
340  Assert(cell->n_children() ==
343  for (unsigned int child = 0; child < cell->n_children(); ++child)
344  {
345  // set an alias to the prolongation matrix for this child
346  prolongation = mg_dof.get_fe().get_prolongation_matrix(
347  child, cell->refinement_case());
348 
349  if (this->mg_constrained_dofs != nullptr &&
350  this->mg_constrained_dofs->have_boundary_indices())
351  for (unsigned int j = 0; j < dofs_per_cell; ++j)
352  if (this->mg_constrained_dofs->is_boundary_index(
353  level, dof_indices_parent[j]))
354  for (unsigned int i = 0; i < dofs_per_cell; ++i)
355  prolongation(i, j) = 0.;
356 
357  cell->child(child)->get_mg_dof_indices(dof_indices_child);
358 
359  replace(this->mg_constrained_dofs,
360  level + 1,
361  dof_indices_child);
362 
363  // now set the entries in the matrix
364  for (unsigned int i = 0; i < dofs_per_cell; ++i)
365  prolongation_matrices[level]->set(dof_indices_child[i],
366  dofs_per_cell,
367  dof_indices_parent.data(),
368  &prolongation(i, 0),
369  true);
370  }
371  }
372  prolongation_matrices[level]->compress(VectorOperation::insert);
373  }
374 
375  this->fill_and_communicate_copy_indices(mg_dof);
376 }
377 
378 
379 
380 template <typename VectorType>
381 void
383 {
384  for (unsigned int level = 0; level < prolongation_matrices.size(); ++level)
385  {
386  os << "Level " << level << std::endl;
387  prolongation_matrices[level]->print(os);
388  os << std::endl;
389  }
390 }
391 
392 
393 
394 template <typename VectorType>
395 std::size_t
397 {
399  for (unsigned int i = 0; i < prolongation_matrices.size(); ++i)
400  result += prolongation_matrices[i]->memory_consumption() +
401  prolongation_sparsities[i]->memory_consumption();
402 
403  return result;
404 }
405 
406 
407 // explicit instantiation
408 #include "mg_transfer_prebuilt.inst"
409 
410 
411 DEAL_II_NAMESPACE_CLOSE
void initialize_constraints(const MGConstrainedDoFs &mg_constrained_dofs)
std::size_t memory_consumption() const
MGTransferPrebuilt()=default
void add_entries(const size_type row, ForwardIterator begin, ForwardIterator end, const bool indices_are_unique_and_sorted=false)
const AffineConstraints< double > & get_level_constraint_matrix(const unsigned int level) const
const types::subdomain_id invalid_subdomain_id
Definition: types.h:255
void build_matrices(const DoFHandler< dim, spacedim > &mg_dof)
const Triangulation< dim, spacedim > & get_triangulation() const
static::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
void extract_locally_relevant_level_dofs(const DoFHandlerType &dof_handler, const unsigned int level, IndexSet &dof_set)
Definition: dof_tools.cc:1185
virtual void restrict_and_add(const unsigned int from_level, VectorType &dst, const VectorType &src) const override
virtual MPI_Comm get_communicator() const
Definition: tria_base.cc:155
size_type n() const
void print_matrices(std::ostream &os) const
const IndexSet & row_index_set() const
#define Assert(cond, exc)
Definition: exceptions.h:1227
types::global_dof_index n_dofs() const
void reinit(const size_type m, const size_type n, const IndexSet &rowset=IndexSet())
cell_iterator end() const
Definition: dof_handler.cc:959
virtual void prolongate(const unsigned int to_level, VectorType &dst, const VectorType &src) const override
const std::vector< IndexSet > & locally_owned_mg_dofs_per_processor(const unsigned int level) const
cell_iterator begin(const unsigned int level=0) const
Definition: dof_handler.cc:930
const FiniteElement< dim, spacedim > & get_fe(const unsigned int index=0) const
bool is_identity_constrained(const size_type index) const
static::ExceptionBase & ExcNotImplemented()
const std::vector< std::pair< size_type, number > > * get_constraint_entries(const size_type line) const
std::size_t memory_consumption() const
static::ExceptionBase & ExcInternalError()
size_type n_constraints() const