Reference documentation for deal.II version GIT relicensing-468-ge3ce94fd06 2024-04-23 15:40: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\}}\)
Loading...
Searching...
No Matches
multigrid.cc
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2000 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15
24#include <deal.II/lac/vector.h>
25
29#include <deal.II/multigrid/mg_transfer_block.templates.h>
31#include <deal.II/multigrid/mg_transfer_component.templates.h>
32#include <deal.II/multigrid/multigrid.templates.h>
33
35
36
40
41
42
44 : n_mg_blocks(0)
45 , mg_constrained_dofs(&mg_c)
46{}
47
48
49
50template <typename number>
52 : memory(nullptr, typeid(*this).name())
53{}
54
55
56template <typename number>
58{
59 if (memory != nullptr)
60 memory = nullptr;
61}
62
63
64template <typename number>
65void
66MGTransferBlock<number>::initialize(const std::vector<number> &f,
68{
69 factors = f;
70 memory = &mem;
71}
72
73
74template <typename number>
75void
76MGTransferBlock<number>::prolongate(const unsigned int to_level,
78 const BlockVector<number> &src) const
80 Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
81 ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
82 Assert(src.n_blocks() == this->n_mg_blocks,
83 ExcDimensionMismatch(src.n_blocks(), this->n_mg_blocks));
84 Assert(dst.n_blocks() == this->n_mg_blocks,
85 ExcDimensionMismatch(dst.n_blocks(), this->n_mg_blocks));
86
87#ifdef DEBUG
88 if (this->mg_constrained_dofs != nullptr)
89 Assert(this->mg_constrained_dofs->get_user_constraint_matrix(to_level - 1)
90 .get_local_lines()
91 .size() == 0,
93#endif
94
95 // Multiplicate with prolongation
96 // matrix, but only those blocks
97 // selected.
98 for (unsigned int b = 0; b < this->mg_block.size(); ++b)
99 {
100 if (this->selected[b])
101 prolongation_matrices[to_level - 1]->block(b, b).vmult(
102 dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
103 }
104}
105
106
107template <typename number>
108void
109MGTransferBlock<number>::restrict_and_add(const unsigned int from_level,
111 const BlockVector<number> &src) const
112{
113 Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
114 ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
115 Assert(src.n_blocks() == this->n_mg_blocks,
116 ExcDimensionMismatch(src.n_blocks(), this->n_mg_blocks));
117 Assert(dst.n_blocks() == this->n_mg_blocks,
118 ExcDimensionMismatch(dst.n_blocks(), this->n_mg_blocks));
119
120 for (unsigned int b = 0; b < this->mg_block.size(); ++b)
121 {
122 if (this->selected[b])
123 {
124 if (factors.size() != 0)
125 {
126 Assert(memory != nullptr, ExcNotInitialized());
127 Vector<number> *aux = memory->alloc();
128 aux->reinit(dst.block(this->mg_block[b]));
129 prolongation_matrices[from_level - 1]->block(b, b).Tvmult(
130 *aux, src.block(this->mg_block[b]));
131
132 dst.block(this->mg_block[b]).add(factors[b], *aux);
133 memory->free(aux);
134 }
135 else
136 {
137 prolongation_matrices[from_level - 1]->block(b, b).Tvmult_add(
138 dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
139 }
140 }
141 }
142}
143
144
145
146std::size_t
148{
149 std::size_t result = sizeof(*this);
151 sizeof(ComponentMask);
153 sizeof(mg_target_component);
156 sizeof(component_start);
158 sizeof(mg_component_start);
162 sizeof(prolongation_matrices);
163 // TODO:[GK] Add this.
164 // result += MemoryConsumption::memory_consumption(copy_to_and_from_indices)
165 // - sizeof(copy_to_and_from_indices);
166 return result;
167}
168
169
170// TODO:[GK] Add all those little vectors.
171std::size_t
173{
174 std::size_t result = sizeof(*this);
175 result += sizeof(unsigned int) * sizes.size();
178 result +=
181 sizeof(mg_block_start);
185 sizeof(prolongation_matrices);
186 // TODO:[GK] Add this.
187 // result += MemoryConsumption::memory_consumption(copy_indices)
188 // - sizeof(copy_indices);
189 return result;
190}
191
192
193//----------------------------------------------------------------------//
194
195template <typename number>
197 : selected_component(0)
198 , mg_selected_component(0)
199{}
200
201
202template <typename number>
204 : selected_component(0)
205 , mg_selected_component(0)
206 , constraints(&c)
207{}
208
209
210
211template <typename number>
212void
213MGTransferSelect<number>::prolongate(const unsigned int to_level,
214 Vector<number> &dst,
215 const Vector<number> &src) const
216{
217 Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
218 ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
219
220 prolongation_matrices[to_level - 1]
221 ->block(mg_target_component[mg_selected_component],
222 mg_target_component[mg_selected_component])
223 .vmult(dst, src);
224}
225
226
227
228template <typename number>
229void
230MGTransferSelect<number>::restrict_and_add(const unsigned int from_level,
231 Vector<number> &dst,
232 const Vector<number> &src) const
233{
234 Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
235 ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
236
237 prolongation_matrices[from_level - 1]
238 ->block(mg_target_component[mg_selected_component],
239 mg_target_component[mg_selected_component])
240 .Tvmult_add(dst, src);
241}
242
243
244//----------------------------------------------------------------------//
245
246template <typename number>
250
251
252
253template <typename number>
255 const MGConstrainedDoFs &mg_c)
256 : MGTransferBlockBase(mg_c)
257 , selected_block(0)
258{}
259
260
261
262template <typename number>
263void
265 Vector<number> &dst,
266 const Vector<number> &src) const
267{
268 Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
269 ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
270
271#ifdef DEBUG
272 if (this->mg_constrained_dofs != nullptr)
273 Assert(this->mg_constrained_dofs->get_user_constraint_matrix(to_level - 1)
274 .get_local_lines()
275 .size() == 0,
277#endif
278
279 prolongation_matrices[to_level - 1]
280 ->block(selected_block, selected_block)
281 .vmult(dst, src);
282}
283
284
285template <typename number>
286void
288 Vector<number> &dst,
289 const Vector<number> &src) const
290{
291 Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
292 ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
293
294 prolongation_matrices[from_level - 1]
295 ->block(selected_block, selected_block)
296 .Tvmult_add(dst, src);
297}
298
299
300
301// Explicit instantiations
302
303#include "multigrid.inst"
304
305template class MGTransferBlock<float>;
306template class MGTransferBlock<double>;
307template class MGTransferSelect<float>;
308template class MGTransferSelect<double>;
309template class MGTransferBlockSelect<float>;
310template class MGTransferBlockSelect<double>;
311
312
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:172
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const override
Definition multigrid.cc:264
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const override
Definition multigrid.cc:287
virtual void prolongate(const unsigned int to_level, BlockVector< number > &dst, const BlockVector< number > &src) const override
Definition multigrid.cc:76
virtual void restrict_and_add(const unsigned int from_level, BlockVector< number > &dst, const BlockVector< number > &src) const override
Definition multigrid.cc:109
virtual ~MGTransferBlock() override
Definition multigrid.cc:57
void initialize(const std::vector< number > &factors, VectorMemory< Vector< number > > &memory)
Definition multigrid.cc:66
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:147
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const override
Definition multigrid.cc:213
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const override
Definition multigrid.cc:230
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
static ::ExceptionBase & ExcIndexRange(std::size_t arg1, std::size_t arg2, std::size_t arg3)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcNotInitialized()
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)