Loading [MathJax]/extensions/TeX/newcommand.js
 deal.II version GIT relicensing-3083-g7b89508ac7 2025-04-18 12:50:00+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\}}
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 if constexpr (running_in_debug_mode())
88 {
89 if (this->mg_constrained_dofs != nullptr)
90 Assert(this->mg_constrained_dofs
91 ->get_user_constraint_matrix(to_level - 1)
92 .get_local_lines()
93 .size() == 0,
95 }
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
109template <typename number>
110void
111MGTransferBlock<number>::restrict_and_add(const unsigned int from_level,
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,
118 ExcDimensionMismatch(src.n_blocks(), this->n_mg_blocks));
119 Assert(dst.n_blocks() == this->n_mg_blocks,
120 ExcDimensionMismatch(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
148std::size_t
150{
151 std::size_t result = sizeof(*this);
153 sizeof(ComponentMask);
155 sizeof(mg_target_component);
158 sizeof(component_start);
160 sizeof(mg_component_start);
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.
173std::size_t
175{
176 std::size_t result = sizeof(*this);
177 result += sizeof(unsigned int) * sizes.size();
180 result +=
183 sizeof(mg_block_start);
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
197template <typename number>
199 : selected_component(0)
200 , mg_selected_component(0)
201{}
202
203
204template <typename number>
206 : selected_component(0)
207 , mg_selected_component(0)
208 , constraints(&c)
209{}
210
211
212
213template <typename number>
214void
215MGTransferSelect<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]
223 ->block(mg_target_component[mg_selected_component],
224 mg_target_component[mg_selected_component])
225 .vmult(dst, src);
226}
227
228
229
230template <typename number>
231void
232MGTransferSelect<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]
240 ->block(mg_target_component[mg_selected_component],
241 mg_target_component[mg_selected_component])
242 .Tvmult_add(dst, src);
243}
244
245
246//----------------------------------------------------------------------//
247
248template <typename number>
252
253
254
255template <typename number>
257 const MGConstrainedDoFs &mg_c)
258 : MGTransferBlockBase(mg_c)
259 , selected_block(0)
260{}
261
262
263
264template <typename number>
265void
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 if constexpr (running_in_debug_mode())
274 {
275 if (this->mg_constrained_dofs != nullptr)
276 Assert(this->mg_constrained_dofs
277 ->get_user_constraint_matrix(to_level - 1)
278 .get_local_lines()
279 .size() == 0,
281 }
282
283 prolongation_matrices[to_level - 1]
284 ->block(selected_block, selected_block)
285 .vmult(dst, src);
286}
287
288
289template <typename number>
290void
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]
299 ->block(selected_block, selected_block)
300 .Tvmult_add(dst, src);
301}
302
303
304
305// Explicit instantiations
306
307#include "multigrid/multigrid.inst"
308
309template class MGTransferBlock<float>;
310template class MGTransferBlock<double>;
311template class MGTransferSelect<float>;
312template class MGTransferSelect<double>;
313template class MGTransferBlockSelect<float>;
314template class MGTransferBlockSelect<double>;
315
316
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:174
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const override
Definition multigrid.cc:266
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const override
Definition multigrid.cc:291
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:111
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:149
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const override
Definition multigrid.cc:215
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 reinit(const size_type N, const bool omit_zeroing_entries=false)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:35
constexpr bool running_in_debug_mode()
Definition config.h:73
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:36
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::size_t size
Definition mpi.cc:745
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)