Reference documentation for deal.II version Git c064f80013 2020-07-04 08:24:28 -0400
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
multigrid.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2020 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 
20 #include <deal.II/lac/la_vector.h>
26 #include <deal.II/lac/vector.h>
27 
31 #include <deal.II/multigrid/mg_transfer_block.templates.h>
33 #include <deal.II/multigrid/mg_transfer_component.templates.h>
34 #include <deal.II/multigrid/multigrid.templates.h>
35 
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 
52 template <typename number>
54  : memory(nullptr, typeid(*this).name())
55 {}
56 
57 
58 template <typename number>
60 {
61  if (memory != nullptr)
62  memory = nullptr;
63 }
64 
65 
66 template <typename number>
67 void
68 MGTransferBlock<number>::initialize(const std::vector<number> & f,
70 {
71  factors = f;
72  memory = &mem;
73 }
74 
75 
76 template <typename number>
77 void
78 MGTransferBlock<number>::prolongate(const unsigned int to_level,
79  BlockVector<number> & dst,
80  const BlockVector<number> &src) const
81 {
82  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
83  ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
84  Assert(src.n_blocks() == this->n_mg_blocks,
86  Assert(dst.n_blocks() == this->n_mg_blocks,
88 
89 #ifdef DEBUG
90  if (this->mg_constrained_dofs != nullptr)
93  .size() == 0,
95 #endif
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);
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);
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>
265 void
266 MGTransferBlockSelect<number>::prolongate(const unsigned int to_level,
267  Vector<number> & dst,
268  const Vector<number> &src) const
269 {
270  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
271  ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
272 
273 #ifdef DEBUG
274  if (this->mg_constrained_dofs != nullptr)
276  .get_local_lines()
277  .size() == 0,
279 #endif
280 
281  prolongation_matrices[to_level - 1]
283  .vmult(dst, src);
284 }
285 
286 
287 template <typename number>
288 void
290  Vector<number> & dst,
291  const Vector<number> &src) const
292 {
293  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
294  ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
295 
296  prolongation_matrices[from_level - 1]
298  .Tvmult_add(dst, src);
299 }
300 
301 
302 
303 // Explicit instantiations
304 
305 #include "multigrid.inst"
306 
307 template class MGTransferBlock<float>;
308 template class MGTransferBlock<double>;
309 template class MGTransferSelect<float>;
310 template class MGTransferSelect<double>;
311 template class MGTransferBlockSelect<float>;
312 template class MGTransferBlockSelect<double>;
313 
314 
std::vector< std::shared_ptr< BlockSparseMatrix< double > > > prolongation_matrices
std::size_t memory_consumption() const
Definition: multigrid.cc:174
virtual VectorType * alloc()=0
virtual ~MGTransferBlock() override
Definition: multigrid.cc:59
static ::ExceptionBase & ExcNotInitialized()
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const override
Definition: multigrid.cc:266
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:1632
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:1403
std::size_t memory_consumption() const
Definition: multigrid.cc:149
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
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:363
virtual void restrict_and_add(const unsigned int from_level, BlockVector< number > &dst, const BlockVector< number > &src) const override
Definition: multigrid.cc:111
std::vector< std::shared_ptr< BlockSparsityPattern > > prolongation_sparsities
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const override
Definition: multigrid.cc:289
const IndexSet & get_local_lines() const
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
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
unsigned int n_blocks() const
SmartPointer< const AffineConstraints< double > > constraints
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:362
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:78
void initialize(const std::vector< number > &factors, VectorMemory< Vector< number >> &memory)
Definition: multigrid.cc:68
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