Debian Codegraph @

include/deal.II/multigrid/mg_coarse.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2002 - 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_mg_coarse_h
16 #define dealii_mg_coarse_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/logstream.h>
22
23 #include <deal.II/lac/full_matrix.h>
24 #include <deal.II/lac/householder.h>
25 #include <deal.II/lac/linear_operator.h>
26
27 #include <deal.II/multigrid/mg_base.h>
28
29 DEAL_II_NAMESPACE_OPEN
30
31 /**
32 * @addtogroup mg
33 * @{
34 */
35
36 /**
37 * Coarse grid solver using smoother only. This is a little wrapper,
38 * transforming a smoother into a coarse grid solver.
39 */
40 template <typename VectorType = Vector<double>>
41 class MGCoarseGridApplySmoother : public MGCoarseGridBase<VectorType>
42 {
43 public:
44 /**
45 * Default constructor.
46 */
47 MGCoarseGridApplySmoother();
48
49 /**
50 * Constructor. Store a pointer to the smoother for later use.
51 */
52 MGCoarseGridApplySmoother(const MGSmootherBase<VectorType> &coarse_smooth);
53
54 /**
55 * Clear the pointer.
56 */
57 void
58 clear();
59
60 /**
61 * Initialize new data.
62 */
63 void
64 initialize(const MGSmootherBase<VectorType> &coarse_smooth);
65
66 /**
67 * Implementation of the abstract function.
68 */
69 void
70 operator()(const unsigned int level,
71 VectorType &dst,
72 const VectorType &src) const override;
73
74 private:
75 /**
76 * Reference to the smoother.
77 */
78 ObserverPointer<const MGSmootherBase<VectorType>,
79 MGCoarseGridApplySmoother<VectorType>>
80 coarse_smooth;
81 };
82
83
84
85 /**
86 * Coarse grid solver using an operator, e.g., preconditioner, only.
87 * This is a little wrapper, transforming an operator into a coarse grid solver.
88 */
89 template <class VectorType, class MatrixType>
90 class MGCoarseGridApplyOperator : public MGCoarseGridBase<VectorType>
91 {
92 public:
93 /**
94 * Default constructor.
95 */
96 MGCoarseGridApplyOperator();
97
98 /**
99 * Constructor. Store a pointer to the operator for later use.
100 */
101 MGCoarseGridApplyOperator(const MatrixType &matrix);
102
103 /**
104 * Clear the pointer.
105 */
106 void
107 clear();
108
109 /**
110 * Initialize new data.
111 */
112 void
113 initialize(const MatrixType &matrix);
114
115 /**
116 * Implementation of the abstract function.
117 */
118 virtual void
119 operator()(const unsigned int level,
120 VectorType &dst,
121 const VectorType &src) const override;
122
123 private:
124 /**
125 * Reference to the operator.
126 */
127 ObserverPointer<const MatrixType,
128 MGCoarseGridApplyOperator<VectorType, MatrixType>>
129 matrix;
130 };
131
132
133
134 /**
135 * Coarse grid multigrid operator for an iterative solver.
136 *
137 * This class provides a wrapper for a deal.II iterative solver with a given
138 * matrix and preconditioner as a coarse grid operator.
139 */
140 template <typename VectorType,
141 typename SolverType,
142 typename MatrixType,
143 typename PreconditionerType>
144 class MGCoarseGridIterativeSolver : public MGCoarseGridBase<VectorType>
145 {
146 public:
147 /**
148 * Default constructor.
149 */
150 MGCoarseGridIterativeSolver();
151
152 /**
153 * Constructor. Only a reference to these objects is stored, so
154 * their lifetime needs to exceed the usage in this class.
155 */
156 MGCoarseGridIterativeSolver(SolverType &solver,
157 const MatrixType &matrix,
158 const PreconditionerType &precondition);
159
160 /**
161 * Initialize with new data, see the corresponding constructor for more
162 * details.
163 */
164 void
165 initialize(SolverType &solver,
166 const MatrixType &matrix,
167 const PreconditionerType &precondition);
168
169 /**
170 * Clear all pointers.
171 */
172 void
173 clear();
174
175 /**
176 * Implementation of the abstract function. Calls the solve method of the
177 * given solver with matrix, vectors, and preconditioner.
178 */
179 virtual void
180 operator()(const unsigned int level,
181 VectorType &dst,
182 const VectorType &src) const override;
183
184 private:
185 /**
186 * Reference to the solver.
187 */
188 ObserverPointer<SolverType,
189 MGCoarseGridIterativeSolver<VectorType,
190 SolverType,
191 MatrixType,
192 PreconditionerType>>
193 solver;
194
195 /**
196 * Reference to the matrix.
197 */
198 ObserverPointer<const MatrixType,
199 MGCoarseGridIterativeSolver<VectorType,
200 SolverType,
201 MatrixType,
202 PreconditionerType>>
203 matrix;
204
205 /**
206 * Reference to the preconditioner.
207 */
208 ObserverPointer<const PreconditionerType,
209 MGCoarseGridIterativeSolver<VectorType,
210 SolverType,
211 MatrixType,
212 PreconditionerType>>
213 preconditioner;
214 };
215
216
217
218 /**
219 * Coarse grid solver by QR factorization implemented in the class
220 * Householder.
221 *
222 * Upon initialization, the QR decomposition of the matrix is computed. then,
223 * the operator() uses Householder::least_squares() to compute the action of
224 * the inverse.
225 */
226 template <typename number = double, typename VectorType = Vector<number>>
227 class MGCoarseGridHouseholder : public MGCoarseGridBase<VectorType>
228 {
229 public:
230 /**
231 * Constructor, taking the coarse grid matrix.
232 */
233 MGCoarseGridHouseholder(const FullMatrix<number> *A = nullptr);
234
235 /**
236 * Initialize for a new matrix.
237 */
238 void
239 initialize(const FullMatrix<number> &A);
240
241 void
242 operator()(const unsigned int level,
243 VectorType &dst,
244 const VectorType &src) const override;
245
246 private:
247 /**
248 * Matrix for QR-factorization.
249 */
250 Householder<number> householder;
251 };
252
253 /**
254 * Coarse grid solver using singular value decomposition of LAPACK matrices.
255 *
256 * Upon initialization, the singular value decomposition of the matrix is
257 * computed. then, the operator() uses
258 */
259 template <typename number = double, typename VectorType = Vector<number>>
260 class MGCoarseGridSVD : public MGCoarseGridBase<VectorType>
261 {
262 public:
263 /**
264 * Constructor leaving an uninitialized object.
265 */
266 MGCoarseGridSVD() = default;
267
268 /**
269 * Initialize for a new matrix. This resets the dimensions to the
270 */
271 void
272 initialize(const FullMatrix<number> &A, const double threshold = 0);
273
274 void
275 operator()(const unsigned int level,
276 VectorType &dst,
277 const VectorType &src) const;
278
279 /**
280 * Write the singular values to @p deallog.
281 */
282 void
283 log() const;
284
285 private:
286 /**
287 * Matrix for singular value decomposition.
288 */
289 LAPACKFullMatrix<number> matrix;
290 };
291
292 /** @} */
293
294 #ifndef DOXYGEN
295 /* ------------------ Functions for MGCoarseGridApplySmoother -----------*/
296 template <typename VectorType>
297 MGCoarseGridApplySmoother<VectorType>::MGCoarseGridApplySmoother()
298 : coarse_smooth(nullptr)
299 {}
300
301 template <typename VectorType>
302 MGCoarseGridApplySmoother<VectorType>::MGCoarseGridApplySmoother(
303 const MGSmootherBase<VectorType> &coarse_smooth)
304 : coarse_smooth(nullptr)
305 {
306 initialize(coarse_smooth);
307 }
308
309
310 template <typename VectorType>
311 void
312 MGCoarseGridApplySmoother<VectorType>::initialize(
313 const MGSmootherBase<VectorType> &coarse_smooth_)
314 {
315 coarse_smooth = ObserverPointer<const MGSmootherBase<VectorType>,
316 MGCoarseGridApplySmoother<VectorType>>(
317 &coarse_smooth_, typeid(*this).name());
318 }
319
320
321 template <typename VectorType>
322 void
323 MGCoarseGridApplySmoother<VectorType>::clear()
324 {
325 coarse_smooth = nullptr;
326 }
327
328
329 template <typename VectorType>
330 void
331 MGCoarseGridApplySmoother<VectorType>::operator()(const unsigned int level,
332 VectorType &dst,
333 const VectorType &src) const
334 {
335 coarse_smooth->apply(level, dst, src);
336 }
337
338 /* ------------------ Functions for MGCoarseGridApplyOperator ----------*/
339
340 template <class VectorType, class PreconditionerType>
341 MGCoarseGridApplyOperator<VectorType,
342 PreconditionerType>::MGCoarseGridApplyOperator()
343 : matrix(0, typeid(*this).name())
344 {}
345
346
347
348 template <class VectorType, class PreconditionerType>
349 MGCoarseGridApplyOperator<VectorType, PreconditionerType>::
350 MGCoarseGridApplyOperator(const PreconditionerType &matrix)
351 : matrix(&matrix, typeid(*this).name())
352 {}
353
354
355
356 template <class VectorType, class PreconditionerType>
357 void
358 MGCoarseGridApplyOperator<VectorType, PreconditionerType>::initialize(
359 const PreconditionerType &matrix_)
360 {
361 matrix = &matrix_;
362 }
363
364
365
366 template <class VectorType, class PreconditionerType>
367 void
368 MGCoarseGridApplyOperator<VectorType, PreconditionerType>::clear()
369 {
370 matrix = 0;
371 }
372
373
374 template <class VectorType, class PreconditionerType>
375 void
376 MGCoarseGridApplyOperator<VectorType, PreconditionerType>::operator()(
377 const unsigned int /*level*/,
378 VectorType &dst,
379 const VectorType &src) const
380 {
381 Assert(matrix != nullptr, ExcNotInitialized());
382
383 matrix->vmult(dst, src);
384 }
385
386 /* ------------------ Functions for MGCoarseGridIterativeSolver ------------ */
387
388 template <typename VectorType,
389 typename SolverType,
390 typename MatrixType,
391 typename PreconditionerType>
392 MGCoarseGridIterativeSolver<VectorType,
393 SolverType,
394 MatrixType,
395 PreconditionerType>::MGCoarseGridIterativeSolver()
396 : solver(0, typeid(*this).name())
397 , matrix(0, typeid(*this).name())
398 , preconditioner(0, typeid(*this).name())
399 {}
400
401
402
403 template <typename VectorType,
404 typename SolverType,
405 typename MatrixType,
406 typename PreconditionerType>
407 MGCoarseGridIterativeSolver<VectorType,
408 SolverType,
409 MatrixType,
410 PreconditionerType>::
411 MGCoarseGridIterativeSolver(SolverType &solver,
412 const MatrixType &matrix,
413 const PreconditionerType &preconditioner)
414 : solver(&solver, typeid(*this).name())
415 , matrix(&matrix, typeid(*this).name())
416 , preconditioner(&preconditioner, typeid(*this).name())
417 {}
418
419
420
421 template <typename VectorType,
422 typename SolverType,
423 typename MatrixType,
424 typename PreconditionerType>
425 void
426 MGCoarseGridIterativeSolver<
427 VectorType,
428 SolverType,
429 MatrixType,
430 PreconditionerType>::initialize(SolverType &solver_,
431 const MatrixType &matrix_,
432 const PreconditionerType &preconditioner_)
433 {
434 solver = &solver_;
435 matrix = &matrix_;
436 preconditioner = &preconditioner_;
437 }
438
439
440
441 template <typename VectorType,
442 typename SolverType,
443 typename MatrixType,
444 typename PreconditionerType>
445 void
446 MGCoarseGridIterativeSolver<VectorType,
447 SolverType,
448 MatrixType,
449 PreconditionerType>::clear()
450 {
451 solver = 0;
452 matrix = 0;
453 preconditioner = 0;
454 }
455
456
457
458 template <typename VectorType,
459 typename SolverType,
460 typename MatrixType,
461 typename PreconditionerType>
462 void
463 MGCoarseGridIterativeSolver<
464 VectorType,
465 SolverType,
466 MatrixType,
467 PreconditionerType>::operator()(const unsigned int /*level*/,
468 VectorType &dst,
469 const VectorType &src) const
470 {
471 Assert(solver != nullptr, ExcNotInitialized());
472 Assert(matrix != nullptr, ExcNotInitialized());
473 Assert(preconditioner != nullptr, ExcNotInitialized());
474
475 dst = 0;
476
477 if constexpr (std::is_same_v<VectorType, typename SolverType::vector_type>)
478 {
479 solver->solve(*matrix, dst, src, *preconditioner);
480 }
481 else
482 {
483 typename SolverType::vector_type src_;
484 typename SolverType::vector_type dst_;
485
486 src_ = src;
487 dst_ = dst;
488
489 solver->solve(*matrix, dst_, src_, *preconditioner);
490
491 dst = dst_;
492 }
493 }
494
495
496
497 /* ------------------ Functions for MGCoarseGridHouseholder ------------ */
498
499 template <typename number, typename VectorType>
500 MGCoarseGridHouseholder<number, VectorType>::MGCoarseGridHouseholder(
501 const FullMatrix<number> *A)
502 {
503 if (A != nullptr)
504 householder.initialize(*A);
505 }
506
507
508
509 template <typename number, typename VectorType>
510 void
511 MGCoarseGridHouseholder<number, VectorType>::initialize(
512 const FullMatrix<number> &A)
513 {
514 householder.initialize(A);
515 }
516
517
518
519 template <typename number, typename VectorType>
520 void
521 MGCoarseGridHouseholder<number, VectorType>::operator()(
522 const unsigned int /*level*/,
523 VectorType &dst,
524 const VectorType &src) const
525 {
526 householder.least_squares(dst, src);
527 }
528
529 //---------------------------------------------------------------------------
530
531
532
533 template <typename number, typename VectorType>
534 void
535 MGCoarseGridSVD<number, VectorType>::initialize(const FullMatrix<number> &A,
536 double threshold)
537 {
538 matrix.reinit(A.n_rows(), A.n_cols());
539 matrix = A;
540 matrix.compute_inverse_svd(threshold);
541 }
542
543
544 template <typename number, typename VectorType>
545 void
546 MGCoarseGridSVD<number, VectorType>::operator()(const unsigned int /*level*/,
547 VectorType &dst,
548 const VectorType &src) const
549 {
550 matrix.vmult(dst, src);
551 }
552
553
554 template <typename number, typename VectorType>
555 void
556 MGCoarseGridSVD<number, VectorType>::log() const
557 {
558 const unsigned int n = std::min(matrix.n_rows(), matrix.n_cols());
559
560 for (unsigned int i = 0; i < n; ++i)
561 deallog << ' ' << matrix.singular_value(i);
562 deallog << std::endl;
563 }
564
565
566 #endif // DOXYGEN
567
568 DEAL_II_NAMESPACE_CLOSE
569
570 #endif