Reference documentation for deal.II version 9.1.0-pre
multigrid.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 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/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  // Multiplicate with prolongation
98  // matrix, but only those blocks
99  // selected.
100  for (unsigned int b = 0; b < this->mg_block.size(); ++b)
101  {
102  if (this->selected[b])
103  prolongation_matrices[to_level - 1]->block(b, b).vmult(
104  dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
105  }
106 }
107 
108 
109 template <typename number>
110 void
111 MGTransferBlock<number>::restrict_and_add(const unsigned int from_level,
112  BlockVector<number> & dst,
113  const BlockVector<number> &src) const
114 {
115  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
116  ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
117  Assert(src.n_blocks() == this->n_mg_blocks,
119  Assert(dst.n_blocks() == this->n_mg_blocks,
121 
122  for (unsigned int b = 0; b < this->mg_block.size(); ++b)
123  {
124  if (this->selected[b])
125  {
126  if (factors.size() != 0)
127  {
128  Assert(memory != nullptr, ExcNotInitialized());
129  Vector<number> *aux = memory->alloc();
130  aux->reinit(dst.block(this->mg_block[b]));
131  prolongation_matrices[from_level - 1]->block(b, b).Tvmult(
132  *aux, src.block(this->mg_block[b]));
133 
134  dst.block(this->mg_block[b]).add(factors[b], *aux);
135  memory->free(aux);
136  }
137  else
138  {
139  prolongation_matrices[from_level - 1]->block(b, b).Tvmult_add(
140  dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
141  }
142  }
143  }
144 }
145 
146 
147 
148 std::size_t
150 {
151  std::size_t result = sizeof(*this);
152  result += MemoryConsumption::memory_consumption(component_mask) -
153  sizeof(ComponentMask);
154  result += MemoryConsumption::memory_consumption(target_component) -
155  sizeof(mg_target_component);
157  result += MemoryConsumption::memory_consumption(component_start) -
158  sizeof(component_start);
159  result += MemoryConsumption::memory_consumption(mg_component_start) -
160  sizeof(mg_component_start);
161  result += MemoryConsumption::memory_consumption(prolongation_sparsities) -
162  sizeof(prolongation_sparsities);
164  sizeof(prolongation_matrices);
165  // TODO:[GK] Add this.
166  // result += MemoryConsumption::memory_consumption(copy_to_and_from_indices)
167  // - sizeof(copy_to_and_from_indices);
168  return result;
169 }
170 
171 
172 // TODO:[GK] Add all those little vectors.
173 std::size_t
175 {
176  std::size_t result = sizeof(*this);
177  result += sizeof(unsigned int) * sizes.size();
180  result +=
183  sizeof(mg_block_start);
184  result += MemoryConsumption::memory_consumption(prolongation_sparsities) -
185  sizeof(prolongation_sparsities);
187  sizeof(prolongation_matrices);
188  // TODO:[GK] Add this.
189  // result += MemoryConsumption::memory_consumption(copy_indices)
190  // - sizeof(copy_indices);
191  return result;
192 }
193 
194 
195 //----------------------------------------------------------------------//
196 
197 template <typename number>
199  : selected_component(0)
200  , mg_selected_component(0)
201 {}
202 
203 
204 template <typename number>
206  : selected_component(0)
208  , constraints(&c)
209 {}
210 
211 
212 
213 template <typename number>
214 void
215 MGTransferSelect<number>::prolongate(const unsigned int to_level,
216  Vector<number> & dst,
217  const Vector<number> &src) const
218 {
219  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
220  ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
221 
222  prolongation_matrices[to_level - 1]
224  mg_target_component[mg_selected_component])
225  .vmult(dst, src);
226 }
227 
228 
229 
230 template <typename number>
231 void
232 MGTransferSelect<number>::restrict_and_add(const unsigned int from_level,
233  Vector<number> & dst,
234  const Vector<number> &src) const
235 {
236  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
237  ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
238 
239  prolongation_matrices[from_level - 1]
241  mg_target_component[mg_selected_component])
242  .Tvmult_add(dst, src);
243 }
244 
245 
246 //----------------------------------------------------------------------//
247 
248 template <typename number>
250  : selected_block(0)
251 {}
252 
253 
254 
255 template <typename number>
257  const MGConstrainedDoFs &mg_c)
258  : MGTransferBlockBase(mg_c)
259  , selected_block(0)
260 {}
261 
262 
263 
264 template <typename number>
266  const AffineConstraints<double> & /*c*/,
267  const MGConstrainedDoFs &mg_c)
268  : MGTransferBlockBase(mg_c)
269  , selected_block(0)
270 {}
271 
272 
273 
274 template <typename number>
275 void
276 MGTransferBlockSelect<number>::prolongate(const unsigned int to_level,
277  Vector<number> & dst,
278  const Vector<number> &src) const
279 {
280  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
281  ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
282 
283  prolongation_matrices[to_level - 1]
285  .vmult(dst, src);
286 }
287 
288 
289 template <typename number>
290 void
292  Vector<number> & dst,
293  const Vector<number> &src) const
294 {
295  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
296  ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
297 
298  prolongation_matrices[from_level - 1]
300  .Tvmult_add(dst, src);
301 }
302 
303 
304 
305 // Explicit instantiations
306 
307 #include "multigrid.inst"
308 
309 template class MGTransferBlock<float>;
310 template class MGTransferBlock<double>;
311 template class MGTransferSelect<float>;
312 template class MGTransferSelect<double>;
313 template class MGTransferBlockSelect<float>;
314 template class MGTransferBlockSelect<double>;
315 
316 
317 DEAL_II_NAMESPACE_CLOSE
std::vector< std::shared_ptr< BlockSparseMatrix< double > > > prolongation_matrices
virtual VectorType * alloc()=0
virtual ~MGTransferBlock() override
Definition: multigrid.cc:67
std::size_t memory_consumption() const
Definition: multigrid.cc:149
static::ExceptionBase & ExcNotInitialized()
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const override
Definition: multigrid.cc:276
unsigned int n_blocks() const
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
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:1227
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:232
virtual void restrict_and_add(const unsigned int from_level, BlockVector< number > &dst, const BlockVector< number > &src) const override
Definition: multigrid.cc:111
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const override
Definition: multigrid.cc:291
std::size_t memory_consumption() const
Definition: multigrid.cc:174
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const override
Definition: multigrid.cc:215
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
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
std::vector< number > factors
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