Reference documentation for deal.II version Git 840703ecf3 2019-10-14 17:15:20 -0400
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
multigrid.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 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 
17 #include <deal.II/lac/block_sparse_matrix.h>
18 #include <deal.II/lac/la_parallel_block_vector.h>
19 #include <deal.II/lac/la_parallel_vector.h>
20 #include <deal.II/lac/la_vector.h>
21 #include <deal.II/lac/petsc_block_vector.h>
22 #include <deal.II/lac/petsc_vector.h>
23 #include <deal.II/lac/sparse_matrix.h>
24 #include <deal.II/lac/trilinos_parallel_block_vector.h>
25 #include <deal.II/lac/trilinos_vector.h>
26 #include <deal.II/lac/vector.h>
27 
28 #include <deal.II/multigrid/mg_smoother.h>
29 #include <deal.II/multigrid/mg_transfer.h>
30 #include <deal.II/multigrid/mg_transfer_block.h>
31 #include <deal.II/multigrid/mg_transfer_block.templates.h>
32 #include <deal.II/multigrid/mg_transfer_component.h>
33 #include <deal.II/multigrid/mg_transfer_component.templates.h>
34 #include <deal.II/multigrid/multigrid.templates.h>
35 
36 DEAL_II_NAMESPACE_OPEN
37 
38 
40  : n_mg_blocks(0)
41 {}
42 
43 
44 
46  : n_mg_blocks(0)
47  , mg_constrained_dofs(&mg_c)
48 {}
49 
50 
51 
53  const AffineConstraints<double> & /*c*/,
54  const MGConstrainedDoFs &mg_c)
55  : n_mg_blocks(0)
56  , mg_constrained_dofs(&mg_c)
57 {}
58 
59 
60 template <typename number>
62  : memory(nullptr, typeid(*this).name())
63 {}
64 
65 
66 template <typename number>
68 {
69  if (memory != nullptr)
70  memory = nullptr;
71 }
72 
73 
74 template <typename number>
75 void
76 MGTransferBlock<number>::initialize(const std::vector<number> & f,
78 {
79  factors = f;
80  memory = &mem;
81 }
82 
83 
84 template <typename number>
85 void
86 MGTransferBlock<number>::prolongate(const unsigned int to_level,
87  BlockVector<number> & dst,
88  const BlockVector<number> &src) const
89 {
90  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
91  ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
92  Assert(src.n_blocks() == this->n_mg_blocks,
94  Assert(dst.n_blocks() == this->n_mg_blocks,
96 
97 #ifdef DEBUG
98  if (this->mg_constrained_dofs != nullptr)
100  .get_local_lines()
101  .size() == 0,
103 #endif
104 
105  // Multiplicate with prolongation
106  // matrix, but only those blocks
107  // selected.
108  for (unsigned int b = 0; b < this->mg_block.size(); ++b)
109  {
110  if (this->selected[b])
111  prolongation_matrices[to_level - 1]->block(b, b).vmult(
112  dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
113  }
114 }
115 
116 
117 template <typename number>
118 void
119 MGTransferBlock<number>::restrict_and_add(const unsigned int from_level,
120  BlockVector<number> & dst,
121  const BlockVector<number> &src) const
122 {
123  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
124  ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
125  Assert(src.n_blocks() == this->n_mg_blocks,
127  Assert(dst.n_blocks() == this->n_mg_blocks,
129 
130  for (unsigned int b = 0; b < this->mg_block.size(); ++b)
131  {
132  if (this->selected[b])
133  {
134  if (factors.size() != 0)
135  {
136  Assert(memory != nullptr, ExcNotInitialized());
137  Vector<number> *aux = memory->alloc();
138  aux->reinit(dst.block(this->mg_block[b]));
139  prolongation_matrices[from_level - 1]->block(b, b).Tvmult(
140  *aux, src.block(this->mg_block[b]));
141 
142  dst.block(this->mg_block[b]).add(factors[b], *aux);
143  memory->free(aux);
144  }
145  else
146  {
147  prolongation_matrices[from_level - 1]->block(b, b).Tvmult_add(
148  dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
149  }
150  }
151  }
152 }
153 
154 
155 
156 std::size_t
158 {
159  std::size_t result = sizeof(*this);
160  result += MemoryConsumption::memory_consumption(component_mask) -
161  sizeof(ComponentMask);
162  result += MemoryConsumption::memory_consumption(target_component) -
163  sizeof(mg_target_component);
165  result += MemoryConsumption::memory_consumption(component_start) -
166  sizeof(component_start);
167  result += MemoryConsumption::memory_consumption(mg_component_start) -
168  sizeof(mg_component_start);
169  result += MemoryConsumption::memory_consumption(prolongation_sparsities) -
170  sizeof(prolongation_sparsities);
172  sizeof(prolongation_matrices);
173  // TODO:[GK] Add this.
174  // result += MemoryConsumption::memory_consumption(copy_to_and_from_indices)
175  // - sizeof(copy_to_and_from_indices);
176  return result;
177 }
178 
179 
180 // TODO:[GK] Add all those little vectors.
181 std::size_t
183 {
184  std::size_t result = sizeof(*this);
185  result += sizeof(unsigned int) * sizes.size();
188  result +=
191  sizeof(mg_block_start);
192  result += MemoryConsumption::memory_consumption(prolongation_sparsities) -
193  sizeof(prolongation_sparsities);
195  sizeof(prolongation_matrices);
196  // TODO:[GK] Add this.
197  // result += MemoryConsumption::memory_consumption(copy_indices)
198  // - sizeof(copy_indices);
199  return result;
200 }
201 
202 
203 //----------------------------------------------------------------------//
204 
205 template <typename number>
207  : selected_component(0)
208  , mg_selected_component(0)
209 {}
210 
211 
212 template <typename number>
214  : selected_component(0)
216  , constraints(&c)
217 {}
218 
219 
220 
221 template <typename number>
222 void
223 MGTransferSelect<number>::prolongate(const unsigned int to_level,
224  Vector<number> & dst,
225  const Vector<number> &src) const
226 {
227  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
228  ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
229 
230  prolongation_matrices[to_level - 1]
232  mg_target_component[mg_selected_component])
233  .vmult(dst, src);
234 }
235 
236 
237 
238 template <typename number>
239 void
240 MGTransferSelect<number>::restrict_and_add(const unsigned int from_level,
241  Vector<number> & dst,
242  const Vector<number> &src) const
243 {
244  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
245  ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
246 
247  prolongation_matrices[from_level - 1]
249  mg_target_component[mg_selected_component])
250  .Tvmult_add(dst, src);
251 }
252 
253 
254 //----------------------------------------------------------------------//
255 
256 template <typename number>
258  : selected_block(0)
259 {}
260 
261 
262 
263 template <typename number>
265  const MGConstrainedDoFs &mg_c)
266  : MGTransferBlockBase(mg_c)
267  , selected_block(0)
268 {}
269 
270 
271 
272 template <typename number>
274  const AffineConstraints<double> & /*c*/,
275  const MGConstrainedDoFs &mg_c)
276  : MGTransferBlockBase(mg_c)
277  , selected_block(0)
278 {}
279 
280 
281 
282 template <typename number>
283 void
284 MGTransferBlockSelect<number>::prolongate(const unsigned int to_level,
285  Vector<number> & dst,
286  const Vector<number> &src) const
287 {
288  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
289  ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
290 
291 #ifdef DEBUG
292  if (this->mg_constrained_dofs != nullptr)
294  .get_local_lines()
295  .size() == 0,
297 #endif
298 
299  prolongation_matrices[to_level - 1]
301  .vmult(dst, src);
302 }
303 
304 
305 template <typename number>
306 void
308  Vector<number> & dst,
309  const Vector<number> &src) const
310 {
311  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
312  ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
313 
314  prolongation_matrices[from_level - 1]
316  .Tvmult_add(dst, src);
317 }
318 
319 
320 
321 // Explicit instantiations
322 
323 #include "multigrid.inst"
324 
325 template class MGTransferBlock<float>;
326 template class MGTransferBlock<double>;
327 template class MGTransferSelect<float>;
328 template class MGTransferSelect<double>;
329 template class MGTransferBlockSelect<float>;
330 template class MGTransferBlockSelect<double>;
331 
332 
333 DEAL_II_NAMESPACE_CLOSE
std::vector< std::shared_ptr< BlockSparseMatrix< double > > > prolongation_matrices
std::size_t memory_consumption() const
Definition: multigrid.cc:182
virtual VectorType * alloc()=0
virtual ~MGTransferBlock() override
Definition: multigrid.cc:67
static ::ExceptionBase & ExcNotInitialized()
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const override
Definition: multigrid.cc:284
std::vector< types::global_dof_index > block_start
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
SmartPointer< const MGConstrainedDoFs, MGTransferBlockBase > mg_constrained_dofs
virtual void free(const VectorType *const)=0
size_type size() const
Definition: index_set.h:1600
void add(const std::vector< size_type > &indices, const std::vector< OtherNumber > &values)
std::vector< std::vector< types::global_dof_index > > sizes
std::vector< std::shared_ptr< BlockSparseMatrix< double > > > prolongation_matrices
#define Assert(cond, exc)
Definition: exceptions.h:1407
std::size_t memory_consumption() const
Definition: multigrid.cc:157
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const override
Definition: multigrid.cc:240
virtual void restrict_and_add(const unsigned int from_level, BlockVector< number > &dst, const BlockVector< number > &src) const override
Definition: multigrid.cc:119
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const override
Definition: multigrid.cc:307
const IndexSet & get_local_lines() const
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const override
Definition: multigrid.cc:223
std::vector< unsigned int > mg_block
unsigned int mg_selected_component
std::vector< unsigned int > mg_target_component
std::vector< std::vector< types::global_dof_index > > mg_block_start
unsigned int n_blocks() const
SmartPointer< const AffineConstraints< double > > constraints
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
SmartPointer< VectorMemory< Vector< number > >, MGTransferBlock< number > > memory
static ::ExceptionBase & ExcNotImplemented()
std::vector< number > factors
const AffineConstraints< double > & get_user_constraint_matrix(const unsigned int level) const
BlockType & block(const unsigned int i)
virtual void prolongate(const unsigned int to_level, BlockVector< number > &dst, const BlockVector< number > &src) const override
Definition: multigrid.cc:86
void initialize(const std::vector< number > &factors, VectorMemory< Vector< number >> &memory)
Definition: multigrid.cc:76
std::vector< bool > selected
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
unsigned int selected_component