Reference documentation for deal.II version GIT e22cb6a53e 2023-09-21 14:00:02+00:00
\(\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 
25 #include <deal.II/lac/vector.h>
26 
30 #include <deal.II/multigrid/mg_transfer_block.templates.h>
32 #include <deal.II/multigrid/mg_transfer_component.templates.h>
33 #include <deal.II/multigrid/multigrid.templates.h>
34 
36 
37 
39  : n_mg_blocks(0)
40 {}
41 
42 
43 
45  : n_mg_blocks(0)
46  , mg_constrained_dofs(&mg_c)
47 {}
48 
49 
50 
51 template <typename number>
53  : memory(nullptr, typeid(*this).name())
54 {}
55 
56 
57 template <typename number>
59 {
60  if (memory != nullptr)
61  memory = nullptr;
62 }
63 
64 
65 template <typename number>
66 void
67 MGTransferBlock<number>::initialize(const std::vector<number> &f,
69 {
70  factors = f;
71  memory = &mem;
72 }
73 
74 
75 template <typename number>
76 void
77 MGTransferBlock<number>::prolongate(const unsigned int to_level,
79  const BlockVector<number> &src) const
80 {
81  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
82  ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
83  Assert(src.n_blocks() == this->n_mg_blocks,
84  ExcDimensionMismatch(src.n_blocks(), this->n_mg_blocks));
85  Assert(dst.n_blocks() == this->n_mg_blocks,
86  ExcDimensionMismatch(dst.n_blocks(), this->n_mg_blocks));
87 
88 #ifdef DEBUG
89  if (this->mg_constrained_dofs != nullptr)
90  Assert(this->mg_constrained_dofs->get_user_constraint_matrix(to_level - 1)
91  .get_local_lines()
92  .size() == 0,
94 #endif
95 
96  // Multiplicate with prolongation
97  // matrix, but only those blocks
98  // selected.
99  for (unsigned int b = 0; b < this->mg_block.size(); ++b)
100  {
101  if (this->selected[b])
102  prolongation_matrices[to_level - 1]->block(b, b).vmult(
103  dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
104  }
105 }
106 
107 
108 template <typename number>
109 void
110 MGTransferBlock<number>::restrict_and_add(const unsigned int from_level,
111  BlockVector<number> &dst,
112  const BlockVector<number> &src) const
113 {
114  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
115  ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
116  Assert(src.n_blocks() == this->n_mg_blocks,
117  ExcDimensionMismatch(src.n_blocks(), this->n_mg_blocks));
118  Assert(dst.n_blocks() == this->n_mg_blocks,
119  ExcDimensionMismatch(dst.n_blocks(), this->n_mg_blocks));
120 
121  for (unsigned int b = 0; b < this->mg_block.size(); ++b)
122  {
123  if (this->selected[b])
124  {
125  if (factors.size() != 0)
126  {
127  Assert(memory != nullptr, ExcNotInitialized());
128  Vector<number> *aux = memory->alloc();
129  aux->reinit(dst.block(this->mg_block[b]));
130  prolongation_matrices[from_level - 1]->block(b, b).Tvmult(
131  *aux, src.block(this->mg_block[b]));
132 
133  dst.block(this->mg_block[b]).add(factors[b], *aux);
134  memory->free(aux);
135  }
136  else
137  {
138  prolongation_matrices[from_level - 1]->block(b, b).Tvmult_add(
139  dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
140  }
141  }
142  }
143 }
144 
145 
146 
147 std::size_t
149 {
150  std::size_t result = sizeof(*this);
152  sizeof(ComponentMask);
154  sizeof(mg_target_component);
157  sizeof(component_start);
159  sizeof(mg_component_start);
161  sizeof(prolongation_sparsities);
163  sizeof(prolongation_matrices);
164  // TODO:[GK] Add this.
165  // result += MemoryConsumption::memory_consumption(copy_to_and_from_indices)
166  // - sizeof(copy_to_and_from_indices);
167  return result;
168 }
169 
170 
171 // TODO:[GK] Add all those little vectors.
172 std::size_t
174 {
175  std::size_t result = sizeof(*this);
176  result += sizeof(unsigned int) * sizes.size();
179  result +=
182  sizeof(mg_block_start);
184  sizeof(prolongation_sparsities);
186  sizeof(prolongation_matrices);
187  // TODO:[GK] Add this.
188  // result += MemoryConsumption::memory_consumption(copy_indices)
189  // - sizeof(copy_indices);
190  return result;
191 }
192 
193 
194 //----------------------------------------------------------------------//
195 
196 template <typename number>
198  : selected_component(0)
199  , mg_selected_component(0)
200 {}
201 
202 
203 template <typename number>
205  : selected_component(0)
206  , mg_selected_component(0)
207  , constraints(&c)
208 {}
209 
210 
211 
212 template <typename number>
213 void
214 MGTransferSelect<number>::prolongate(const unsigned int to_level,
215  Vector<number> &dst,
216  const Vector<number> &src) const
217 {
218  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
219  ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
220 
221  prolongation_matrices[to_level - 1]
222  ->block(mg_target_component[mg_selected_component],
223  mg_target_component[mg_selected_component])
224  .vmult(dst, src);
225 }
226 
227 
228 
229 template <typename number>
230 void
231 MGTransferSelect<number>::restrict_and_add(const unsigned int from_level,
232  Vector<number> &dst,
233  const Vector<number> &src) const
234 {
235  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
236  ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
237 
238  prolongation_matrices[from_level - 1]
239  ->block(mg_target_component[mg_selected_component],
240  mg_target_component[mg_selected_component])
241  .Tvmult_add(dst, src);
242 }
243 
244 
245 //----------------------------------------------------------------------//
246 
247 template <typename number>
249  : selected_block(0)
250 {}
251 
252 
253 
254 template <typename number>
256  const MGConstrainedDoFs &mg_c)
257  : MGTransferBlockBase(mg_c)
258  , selected_block(0)
259 {}
260 
261 
262 
263 template <typename number>
264 void
265 MGTransferBlockSelect<number>::prolongate(const unsigned int to_level,
266  Vector<number> &dst,
267  const Vector<number> &src) const
268 {
269  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
270  ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
271 
272 #ifdef DEBUG
273  if (this->mg_constrained_dofs != nullptr)
274  Assert(this->mg_constrained_dofs->get_user_constraint_matrix(to_level - 1)
275  .get_local_lines()
276  .size() == 0,
278 #endif
279 
280  prolongation_matrices[to_level - 1]
281  ->block(selected_block, selected_block)
282  .vmult(dst, src);
283 }
284 
285 
286 template <typename number>
287 void
289  Vector<number> &dst,
290  const Vector<number> &src) const
291 {
292  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
293  ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
294 
295  prolongation_matrices[from_level - 1]
296  ->block(selected_block, selected_block)
297  .Tvmult_add(dst, src);
298 }
299 
300 
301 
302 // Explicit instantiations
303 
304 #include "multigrid.inst"
305 
306 template class MGTransferBlock<float>;
307 template class MGTransferBlock<double>;
308 template class MGTransferSelect<float>;
309 template class MGTransferSelect<double>;
310 template class MGTransferBlockSelect<float>;
311 template class MGTransferBlockSelect<double>;
312 
313 
unsigned int n_blocks() const
BlockType & block(const unsigned int i)
std::vector< unsigned int > mg_block
std::vector< std::vector< types::global_dof_index > > sizes
std::vector< bool > selected
std::vector< types::global_dof_index > block_start
std::vector< std::shared_ptr< BlockSparseMatrix< double > > > prolongation_matrices
std::vector< std::shared_ptr< BlockSparsityPattern > > prolongation_sparsities
std::vector< std::vector< types::global_dof_index > > mg_block_start
std::size_t memory_consumption() const
Definition: multigrid.cc:173
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const override
Definition: multigrid.cc:265
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const override
Definition: multigrid.cc:288
virtual void prolongate(const unsigned int to_level, BlockVector< number > &dst, const BlockVector< number > &src) const override
Definition: multigrid.cc:77
virtual void restrict_and_add(const unsigned int from_level, BlockVector< number > &dst, const BlockVector< number > &src) const override
Definition: multigrid.cc:110
virtual ~MGTransferBlock() override
Definition: multigrid.cc:58
void initialize(const std::vector< number > &factors, VectorMemory< Vector< number >> &memory)
Definition: multigrid.cc:67
std::vector< unsigned int > mg_target_component
std::vector< unsigned int > target_component
std::vector< types::global_dof_index > component_start
std::vector< std::shared_ptr< BlockSparseMatrix< double > > > prolongation_matrices
std::vector< std::vector< types::global_dof_index > > mg_component_start
std::vector< std::shared_ptr< BlockSparsityPattern > > prolongation_sparsities
std::vector< std::vector< types::global_dof_index > > sizes
std::size_t memory_consumption() const
Definition: multigrid.cc:148
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const override
Definition: multigrid.cc:214
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const override
Definition: multigrid.cc:231
Definition: vector.h:110
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:477
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:478
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
#define Assert(cond, exc)
Definition: exceptions.h:1616
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcIndexRange(std::size_t arg1, std::size_t arg2, std::size_t arg3)
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)