Debian Codegraph @

include/deal.II/lac/relaxation_block.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2010 - 2025 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 #ifndef dealii_relaxation_block_templates_h
16 #define dealii_relaxation_block_templates_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/lac/full_matrix.h>
21 #include <deal.II/lac/relaxation_block.h>
22 #include <deal.II/lac/trilinos_vector.h>
23 #include <deal.II/lac/vector_memory.h>
24
25 DEAL_II_NAMESPACE_OPEN
26
27 template <typename MatrixType, typename InverseNumberType, typename VectorType>
28 inline RelaxationBlock<MatrixType, InverseNumberType, VectorType>::
29 AdditionalData::AdditionalData(
30 const double relaxation,
31 const bool invert_diagonal,
32 const bool same_diagonal,
33 const typename PreconditionBlockBase<InverseNumberType>::Inversion
34 inversion,
35 const double threshold,
36 VectorType *temp_ghost_vector)
37 : relaxation(relaxation)
38 , invert_diagonal(invert_diagonal)
39 , same_diagonal(same_diagonal)
40 , inversion(inversion)
41 , threshold(threshold)
42 , temp_ghost_vector(temp_ghost_vector)
43 {}
44
45
46 template <typename MatrixType, typename InverseNumberType, typename VectorType>
47 inline std::size_t
48 RelaxationBlock<MatrixType, InverseNumberType, VectorType>::AdditionalData::
49 memory_consumption() const
50 {
51 return sizeof(*this) + block_list.memory_consumption() - sizeof(block_list) +
52 MemoryConsumption::memory_consumption(order);
53 }
54
55
56 template <typename MatrixType, typename InverseNumberType, typename VectorType>
57 inline void
58 RelaxationBlock<MatrixType, InverseNumberType, VectorType>::initialize(
59 const MatrixType &M,
60 const AdditionalData &parameters)
61 {
62 Assert(parameters.invert_diagonal, ExcNotImplemented());
63
64 clear();
65 // Assert (M.m() == M.n(), ExcNotQuadratic());
66 A = &M;
67 additional_data = &parameters;
68 this->inversion = parameters.inversion;
69
70 this->reinit(additional_data->block_list.n_rows(),
71 0,
72 additional_data->same_diagonal,
73 additional_data->inversion);
74
75 if (additional_data->invert_diagonal)
76 invert_diagblocks();
77 }
78
79
80 template <typename MatrixType, typename InverseNumberType, typename VectorType>
81 inline void
82 RelaxationBlock<MatrixType, InverseNumberType, VectorType>::clear()
83 {
84 A = nullptr;
85 additional_data = nullptr;
86 PreconditionBlockBase<InverseNumberType>::clear();
87 }
88
89
90 template <typename MatrixType, typename InverseNumberType, typename VectorType>
91 inline void
92 RelaxationBlock<MatrixType, InverseNumberType, VectorType>::invert_diagblocks()
93 {
94 if (this->same_diagonal())
95 {
96 DEAL_II_NOT_IMPLEMENTED();
97 }
98 else
99 {
100 // compute blocks in parallel
101 parallel::apply_to_subranges(
102 0,
103 this->additional_data->block_list.n_rows(),
104 [this](const size_type block_begin, const size_type block_end) {
105 this->block_kernel(block_begin, block_end);
106 },
107 16);
108 }
109 this->inverses_computed(true);
110 }
111
112
113 template <typename MatrixType, typename InverseNumberType, typename VectorType>
114 inline void
115 RelaxationBlock<MatrixType, InverseNumberType, VectorType>::block_kernel(
116 const size_type block_begin,
117 const size_type block_end)
118 {
119 const MatrixType &M = *(this->A);
120 FullMatrix<InverseNumberType> M_cell;
121
122 for (size_type block = block_begin; block < block_end; ++block)
123 {
124 const size_type bs = this->additional_data->block_list.row_length(block);
125 M_cell.reinit(bs, bs);
126
127 // Copy rows for this block into the matrix for the diagonal block
128 SparsityPattern::iterator row =
129 this->additional_data->block_list.begin(block);
130 for (size_type row_cell = 0; row_cell < bs; ++row_cell, ++row)
131 {
132 for (typename MatrixType::const_iterator entry =
133 M.begin(row->column());
134 entry != M.end(row->column());
135 ++entry)
136 {
137 const size_type column = entry->column();
138 const size_type col_cell =
139 this->additional_data->block_list.row_position(block, column);
140 if (col_cell != numbers::invalid_size_type)
141 M_cell(row_cell, col_cell) = entry->value();
142 }
143 }
144 // Now M_cell contains the diagonal block. Now store it and its
145 // inverse, if so requested.
146 if (this->store_diagonals())
147 {
148 this->diagonal(block).reinit(bs, bs);
149 this->diagonal(block) = M_cell;
150 }
151 switch (this->inversion)
152 {
153 case PreconditionBlockBase<InverseNumberType>::gauss_jordan:
154 this->inverse(block).reinit(bs, bs);
155 this->inverse(block).invert(M_cell);
156 break;
157 case PreconditionBlockBase<InverseNumberType>::householder:
158 this->inverse_householder(block).initialize(M_cell);
159 break;
160 case PreconditionBlockBase<InverseNumberType>::svd:
161 this->inverse_svd(block).reinit(bs, bs);
162 this->inverse_svd(block) = M_cell;
163 if (this->additional_data->kernel_size > 0)
164 this->inverse_svd(block).compute_inverse_svd_with_kernel(
165 this->additional_data->kernel_size);
166 else
167 this->inverse_svd(block).compute_inverse_svd(
168 this->additional_data->threshold);
169 break;
170 default:
171 DEAL_II_NOT_IMPLEMENTED();
172 }
173 }
174 }
175
176 namespace internal
177 {
178 /**
179 * Default implementation for serial vectors. Here we don't need to make a
180 * copy into a ghosted vector, so just return a reference to @p prev.
181 */
182 template <typename VectorType>
183 const VectorType &
184 prepare_ghost_vector(const VectorType &prev, VectorType *other)
185 {
186 // If the following Assertion triggers, you either set temp_ghost_vector
187 // for a serial computation (don't!), or nobody implemented, instantiated,
188 // and tested the parallel version for your vector type.
189 Assert(other == nullptr, ExcNotImplemented());
190 return prev;
191 }
192
193 #ifdef DEAL_II_WITH_TRILINOS
194 /**
195 * Specialization for Trilinos. Use the ghosted vector.
196 */
197 template <>
198 inline const TrilinosWrappers::MPI::Vector &
199 prepare_ghost_vector(const TrilinosWrappers::MPI::Vector &prev,
200 TrilinosWrappers::MPI::Vector *other)
201 {
202 Assert(
203 other != nullptr,
204 ExcMessage(
205 "You need to provide a ghosted vector in RelaxationBlock::AdditionalData::temp_trilinos_ghost_vector."));
206 Assert(other->size() == prev.size(), ExcInternalError());
207
208 // import ghost values:
209 *other = prev;
210 return *other;
211 }
212 #endif // DEAL_II_WITH_TRILINOS
213 } // end namespace internal
214
215 template <typename MatrixType, typename InverseNumberType, typename VectorType>
216 inline void
217 RelaxationBlock<MatrixType, InverseNumberType, VectorType>::do_step(
218 VectorType &dst,
219 const VectorType &prev,
220 const VectorType &src,
221 const bool backward) const
222 {
223 Assert(additional_data->invert_diagonal, ExcNotImplemented());
224
225 const VectorType &ghosted_prev =
226 internal::prepare_ghost_vector(prev, additional_data->temp_ghost_vector);
227
228 const MatrixType &M = *this->A;
229 Vector<typename VectorType::value_type> b_cell, x_cell;
230
231 const bool permutation_empty = additional_data->order.empty();
232 const unsigned int n_permutations =
233 (permutation_empty) ? 1U : additional_data->order.size();
234 const size_type n_blocks = additional_data->block_list.n_rows();
235
236 if (!permutation_empty)
237 for (unsigned int i = 0; i < additional_data->order.size(); ++i)
238 AssertDimension(additional_data->order[i].size(), this->size());
239
240 for (unsigned int perm = 0; perm < n_permutations; ++perm)
241 {
242 for (unsigned int bi = 0; bi < n_blocks; ++bi)
243 {
244 const unsigned int raw_block = backward ? (n_blocks - bi - 1) : bi;
245 const unsigned int block =
246 permutation_empty ?
247 raw_block :
248 (backward ? (additional_data
249 ->order[n_permutations - 1 - perm][raw_block]) :
250 (additional_data->order[perm][raw_block]));
251
252 const size_type bs = additional_data->block_list.row_length(block);
253
254 b_cell.reinit(bs);
255 x_cell.reinit(bs);
256 // Collect off-diagonal parts
257 SparsityPattern::iterator row =
258 additional_data->block_list.begin(block);
259 for (size_type row_cell = 0; row_cell < bs; ++row_cell, ++row)
260 {
261 b_cell(row_cell) = src(row->column());
262 for (typename MatrixType::const_iterator entry =
263 M.begin(row->column());
264 entry != M.end(row->column());
265 ++entry)
266 b_cell(row_cell) -=
267 entry->value() * ghosted_prev(entry->column());
268 }
269 // Apply inverse diagonal
270 this->inverse_vmult(block, x_cell, b_cell);
271 if constexpr (running_in_debug_mode())
272 {
273 for (unsigned int i = 0; i < x_cell.size(); ++i)
274 {
275 AssertIsFinite(x_cell(i));
276 }
277 }
278 // Store in result vector
279 row = additional_data->block_list.begin(block);
280 for (size_type row_cell = 0; row_cell < bs; ++row_cell, ++row)
281 dst(row->column()) +=
282 additional_data->relaxation * x_cell(row_cell);
283 }
284 }
285 dst.compress(VectorOperation::add);
286 }
287
288
289 //----------------------------------------------------------------------//
290
291 template <typename MatrixType, typename InverseNumberType, typename VectorType>
292 void
293 RelaxationBlockJacobi<MatrixType, InverseNumberType, VectorType>::step(
294 VectorType &dst,
295 const VectorType &src) const
296 {
297 GrowingVectorMemory<VectorType> mem;
298 typename VectorMemory<VectorType>::Pointer aux(mem);
299 aux->reinit(dst, false);
300 *aux = dst;
301 this->do_step(dst, *aux, src, false);
302 }
303
304
305 template <typename MatrixType, typename InverseNumberType, typename VectorType>
306 void
307 RelaxationBlockJacobi<MatrixType, InverseNumberType, VectorType>::Tstep(
308 VectorType &dst,
309 const VectorType &src) const
310 {
311 GrowingVectorMemory<VectorType> mem;
312 typename VectorMemory<VectorType>::Pointer aux(mem);
313 aux->reinit(dst, false);
314 *aux = dst;
315 this->do_step(dst, *aux, src, true);
316 }
317
318
319 template <typename MatrixType, typename InverseNumberType, typename VectorType>
320 void
321 RelaxationBlockJacobi<MatrixType, InverseNumberType, VectorType>::vmult(
322 VectorType &dst,
323 const VectorType &src) const
324 {
325 GrowingVectorMemory<VectorType> mem;
326 typename VectorMemory<VectorType>::Pointer aux(mem);
327 dst = 0;
328 aux->reinit(dst);
329 this->do_step(dst, *aux, src, false);
330 }
331
332
333 template <typename MatrixType, typename InverseNumberType, typename VectorType>
334 void
335 RelaxationBlockJacobi<MatrixType, InverseNumberType, VectorType>::Tvmult(
336 VectorType &dst,
337 const VectorType &src) const
338 {
339 GrowingVectorMemory<VectorType> mem;
340 typename VectorMemory<VectorType>::Pointer aux(mem);
341 dst = 0;
342 aux->reinit(dst);
343 this->do_step(dst, *aux, src, true);
344 }
345
346
347 //----------------------------------------------------------------------//
348
349 template <typename MatrixType, typename InverseNumberType, typename VectorType>
350 void
351 RelaxationBlockSOR<MatrixType, InverseNumberType, VectorType>::step(
352 VectorType &dst,
353 const VectorType &src) const
354 {
355 this->do_step(dst, dst, src, false);
356 }
357
358
359 template <typename MatrixType, typename InverseNumberType, typename VectorType>
360 void
361 RelaxationBlockSOR<MatrixType, InverseNumberType, VectorType>::Tstep(
362 VectorType &dst,
363 const VectorType &src) const
364 {
365 this->do_step(dst, dst, src, true);
366 }
367
368
369 template <typename MatrixType, typename InverseNumberType, typename VectorType>
370 void
371 RelaxationBlockSOR<MatrixType, InverseNumberType, VectorType>::vmult(
372 VectorType &dst,
373 const VectorType &src) const
374 {
375 dst = 0;
376 this->do_step(dst, dst, src, false);
377 }
378
379
380 template <typename MatrixType, typename InverseNumberType, typename VectorType>
381 void
382 RelaxationBlockSOR<MatrixType, InverseNumberType, VectorType>::Tvmult(
383 VectorType &dst,
384 const VectorType &src) const
385 {
386 dst = 0;
387 this->do_step(dst, dst, src, true);
388 }
389
390
391 //----------------------------------------------------------------------//
392
393 template <typename MatrixType, typename InverseNumberType, typename VectorType>
394 void
395 RelaxationBlockSSOR<MatrixType, InverseNumberType, VectorType>::step(
396 VectorType &dst,
397 const VectorType &src) const
398 {
399 this->do_step(dst, dst, src, false);
400 this->do_step(dst, dst, src, true);
401 }
402
403
404 template <typename MatrixType, typename InverseNumberType, typename VectorType>
405 void
406 RelaxationBlockSSOR<MatrixType, InverseNumberType, VectorType>::Tstep(
407 VectorType &dst,
408 const VectorType &src) const
409 {
410 this->do_step(dst, dst, src, true);
411 this->do_step(dst, dst, src, false);
412 }
413
414
415 template <typename MatrixType, typename InverseNumberType, typename VectorType>
416 void
417 RelaxationBlockSSOR<MatrixType, InverseNumberType, VectorType>::vmult(
418 VectorType &dst,
419 const VectorType &src) const
420 {
421 dst = 0;
422 this->do_step(dst, dst, src, false);
423 this->do_step(dst, dst, src, true);
424 }
425
426
427 template <typename MatrixType, typename InverseNumberType, typename VectorType>
428 void
429 RelaxationBlockSSOR<MatrixType, InverseNumberType, VectorType>::Tvmult(
430 VectorType &dst,
431 const VectorType &src) const
432 {
433 dst = 0;
434 this->do_step(dst, dst, src, true);
435 this->do_step(dst, dst, src, false);
436 }
437
438
439
440 DEAL_II_NAMESPACE_CLOSE
441
442
443 #endif