Debian Codegraph @

dune/istl/preconditioners.hh

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 #ifndef DUNE_ISTL_PRECONDITIONERS_HH
6 #define DUNE_ISTL_PRECONDITIONERS_HH
7
8 #include <cmath>
9 #include <complex>
10 #include <iostream>
11 #include <iomanip>
12 #include <memory>
13 #include <string>
14
15 #include <dune/common/simd/simd.hh>
16 #include <dune/common/parametertree.hh>
17
18 #include <dune/istl/solverregistry.hh>
19 #include "preconditioner.hh"
20 #include "solver.hh"
21 #include "solvercategory.hh"
22 #include "istlexception.hh"
23 #include "matrixutils.hh"
24 #include "gsetc.hh"
25 #include "dilu.hh"
26 #include "ildl.hh"
27 #include "ilu.hh"
28
29
30 namespace Dune {
31 /** @defgroup ISTL_Prec Preconditioners
32 * @ingroup ISTL_Solvers
33 *
34 * All of our \ref ISTL_Solvers "Krylow solvers" are preconditioned versions.
35 * There are sequential preconditioners (e,g. SeqJacobi, SeqSOR, SeqSSOR) as well as parallel preconditioners
36 * (e.g. AMG, BlockPreconditioner) available for plugging them into the solvers
37 * together with matching ScalarProducts.
38 *
39 * Some of the available preconditioners (e.g. SeqJacobi, SeqSOR, SeqSSOR))
40 * may be given an additional int as a template parameter, the block recursion level.
41 * These preconditioners
42 * can be used on block-recursive matrices with an arbitrary hierarchy depth
43 * (eg. BCRSMatrix<BCRSMatrix<FieldMatrix,n,m> > >. Given a block recursion level
44 * \f$k\f$ those preconditioners work as
45 * normal on the offdiagonal blocks, treating them as traditional matrix
46 * entries. For the diagonal values a special procedure applies: If
47 * \f$k>1\f$ the diagonal is treated as a matrix itself and the preconditioner
48 * is applied recursively on the matrix representing the diagonal value
49 * \f$D=A_{ii}\f$ with block level \f$k-1\f$. For the case that \f$k=1\f$ the diagonal
50 * is treated as a
51 * matrix entry resulting in a linear solve or an identity operation
52 * depending on the algorithm.
53 */
54
55 /** @addtogroup ISTL_Prec
56 @{
57 */
58 /** \file
59
60 \brief Define general preconditioner interface
61
62 Wrap the methods implemented by ISTL in this interface.
63 However, the interface is extensible such that new preconditioners
64 can be implemented and used with the solvers.
65 */
66
67
68
69 /**
70 * @brief Turns an InverseOperator into a Preconditioner.
71 * @tparam O The type of the inverse operator to wrap.
72 */
73 template<class O, int c = -1>
74 class InverseOperator2Preconditioner :
75 public Preconditioner<typename O::domain_type, typename O::range_type>
76 {
77 public:
78 //! \brief The domain type of the preconditioner.
79 typedef typename O::domain_type domain_type;
80 //! \brief The range type of the preconditioner.
81 typedef typename O::range_type range_type;
82 //! \brief The field type of the preconditioner.
83 typedef typename range_type::field_type field_type;
84 //! \brief scalar type underlying the field_type
85 typedef Simd::Scalar<field_type> scalar_field_type;
86 //! \brief real scalar type underlying the field_type
87 typedef typename FieldTraits<scalar_field_type>::real_type real_field_type;
88 //! \brief type of the wrapped inverse operator
89 typedef O InverseOperator;
90
91 /**
92 * @brief Construct the preconditioner from the solver
93 * @param inverse_operator The inverse operator to wrap.
94 */
95 InverseOperator2Preconditioner(InverseOperator& inverse_operator)
96 : inverse_operator_(inverse_operator)
97 {
98 if(c != -1 && SolverCategory::category(inverse_operator_) != c)
99 DUNE_THROW(InvalidStateException, "User-supplied solver category does not match that of the given inverse operator");
100 }
101
102 void pre([[maybe_unused]] domain_type&,[[maybe_unused]] range_type&) override
103 {}
104
105 void apply(domain_type& v, const range_type& d) override
106 {
107 InverseOperatorResult res;
108 range_type copy(d);
109 inverse_operator_.apply(v, copy, res);
110 }
111
112 void post([[maybe_unused]] domain_type&) override
113 {}
114
115 //! Category of the preconditioner (see SolverCategory::Category)
116 SolverCategory::Category category() const override
117 {
118 return SolverCategory::category(inverse_operator_);
119 }
120
121 private:
122 InverseOperator& inverse_operator_;
123 };
124
125 //=====================================================================
126 // Implementation of this interface for sequential ISTL-preconditioners
127 //=====================================================================
128
129
130 /*!
131 \brief Sequential SSOR preconditioner.
132
133 Wraps the naked ISTL generic SSOR preconditioner into the
134 solver framework.
135
136 \tparam M The matrix type to operate on
137 \tparam X Type of the update
138 \tparam Y Type of the defect
139 \tparam l The block level to invert. Default is 1
140 */
141 template<class M, class X, class Y, int l=1>
142 class SeqSSOR : public Preconditioner<X,Y> {
143 public:
144 //! \brief The matrix type the preconditioner is for.
145 typedef M matrix_type;
146 //! \brief The domain type of the preconditioner.
147 typedef X domain_type;
148 //! \brief The range type of the preconditioner.
149 typedef Y range_type;
150 //! \brief The field type of the preconditioner.
151 typedef typename X::field_type field_type;
152 //! \brief scalar type underlying the field_type
153 typedef Simd::Scalar<field_type> scalar_field_type;
154 //! \brief real scalar type underlying the field_type
155 typedef typename FieldTraits<scalar_field_type>::real_type real_field_type;
156
157 /*! \brief Constructor.
158
159 constructor gets all parameters to operate the prec.
160 \param A The matrix to operate on.
161 \param n The number of iterations to perform.
162 \param w The relaxation factor.
163 */
164 SeqSSOR (const M& A, int n, real_field_type w)
165 : _A_(A), _n(n), _w(w)
166 {
167 CheckIfDiagonalPresent<M,l>::check(_A_);
168 }
169
170 /*!
171 \brief Constructor.
172
173 \param A The assembled linear operator to use.
174 \param configuration ParameterTree containing preconditioner parameters.
175
176 ParameterTree Key | Meaning
177 ------------------|------------
178 iterations | The number of iterations to perform. default=1
179 relaxation | The relaxation factor. default=1.0
180
181 See \ref ISTL_Factory for the ParameterTree layout and examples.
182 */
183 SeqSSOR (const std::shared_ptr<const AssembledLinearOperator<M,X,Y>>& A, const ParameterTree& configuration)
184 : SeqSSOR(A->getmat(), configuration)
185 {}
186
187 /*!
188 \brief Constructor.
189
190 \param A The matrix to operate on.
191 \param configuration ParameterTree containing preconditioner parameters.
192
193 ParameterTree Key | Meaning
194 ------------------|------------
195 iterations | The number of iterations to perform. default=1
196 relaxation | The relaxation factor. default=1.0
197
198 See \ref ISTL_Factory for the ParameterTree layout and examples.
199 */
200 SeqSSOR (const M& A, const ParameterTree& configuration)
201 : SeqSSOR(A, configuration.get<int>("iterations",1), configuration.get<real_field_type>("relaxation",1.0))
202 {}
203
204 /*!
205 \brief Prepare the preconditioner.
206
207 \copydoc Preconditioner::pre(X&,Y&)
208 */
209 void pre ([[maybe_unused]] X& x, [[maybe_unused]] Y& b) override
210 {}
211
212 /*!
213 \brief Apply the preconditioner
214
215 \copydoc Preconditioner::apply(X&,const Y&)
216 */
217 void apply (X& v, const Y& d) override
218 {
219 for (int i=0; i<_n; i++) {
220 bsorf(_A_,v,d,_w,BL<l>());
221 bsorb(_A_,v,d,_w,BL<l>());
222 }
223 }
224
225 /*!
226 \brief Clean up.
227
228 \copydoc Preconditioner::post(X&)
229 */
230 void post ([[maybe_unused]] X& x) override
231 {}
232
233 //! Category of the preconditioner (see SolverCategory::Category)
234 SolverCategory::Category category() const override
235 {
236 return SolverCategory::sequential;
237 }
238
239 private:
240 //! \brief The matrix we operate on.
241 const M& _A_;
242 //! \brief The number of steps to do in apply
243 int _n;
244 //! \brief The relaxation factor to use
245 real_field_type _w;
246 };
247 DUNE_REGISTER_PRECONDITIONER("ssor", defaultPreconditionerBlockLevelCreator<Dune::SeqSSOR>());
248
249
250 /*!
251 \brief Sequential SOR preconditioner.
252
253 Wraps the naked ISTL generic SOR preconditioner into the
254 solver framework.
255
256 \tparam M The matrix type to operate on
257 \tparam X Type of the update
258 \tparam Y Type of the defect
259 \tparam l The block level to invert. Default is 1
260 */
261 template<class M, class X, class Y, int l=1>
262 class SeqSOR : public Preconditioner<X,Y> {
263 public:
264 //! \brief The matrix type the preconditioner is for.
265 typedef M matrix_type;
266 //! \brief The domain type of the preconditioner.
267 typedef X domain_type;
268 //! \brief The range type of the preconditioner.
269 typedef Y range_type;
270 //! \brief The field type of the preconditioner.
271 typedef typename X::field_type field_type;
272 //! \brief scalar type underlying the field_type
273 typedef Simd::Scalar<field_type> scalar_field_type;
274 //! \brief real scalar type underlying the field_type
275 typedef typename FieldTraits<scalar_field_type>::real_type real_field_type;
276
277 /*! \brief Constructor.
278
279 constructor gets all parameters to operate the prec.
280 \param A The matrix to operate on.
281 \param n The number of iterations to perform.
282 \param w The relaxation factor.
283 */
284 SeqSOR (const M& A, int n, real_field_type w)
285 : _A_(A), _n(n), _w(w)
286 {
287 CheckIfDiagonalPresent<M,l>::check(_A_);
288 }
289
290 /*!
291 \brief Constructor.
292
293 \param A The assembled linear operator to use.
294 \param configuration ParameterTree containing preconditioner parameters.
295
296 ParameterTree Key | Meaning
297 ------------------|------------
298 iterations | The number of iterations to perform. default=1
299 relaxation | The relaxation factor. default=1.0
300
301 See \ref ISTL_Factory for the ParameterTree layout and examples.
302 */
303 SeqSOR (const std::shared_ptr<const AssembledLinearOperator<M,X,Y>>& A, const ParameterTree& configuration)
304 : SeqSOR(A->getmat(), configuration)
305 {}
306
307 /*!
308 \brief Constructor.
309
310 \param A The matrix to operate on.
311 \param configuration ParameterTree containing preconditioner parameters.
312
313 ParameterTree Key | Meaning
314 ------------------|------------
315 iterations | The number of iterations to perform. default=1
316 relaxation | The relaxation factor. default=1.0
317
318 See \ref ISTL_Factory for the ParameterTree layout and examples.
319 */
320 SeqSOR (const M& A, const ParameterTree& configuration)
321 : SeqSOR(A, configuration.get<int>("iterations",1), configuration.get<real_field_type>("relaxation",1.0))
322 {}
323
324 /*!
325 \brief Prepare the preconditioner.
326
327 \copydoc Preconditioner::pre(X&,Y&)
328 */
329 void pre ([[maybe_unused]] X& x, [[maybe_unused]] Y& b) override
330 {}
331
332 /*!
333 \brief Apply the preconditioner.
334
335 \copydoc Preconditioner::apply(X&,const Y&)
336 */
337 void apply (X& v, const Y& d) override
338 {
339 this->template apply<true>(v,d);
340 }
341
342 /*!
343 \brief Apply the preconditioner in a special direction.
344
345 The template parameter forward indications the direction
346 the smoother is applied. If true The application is
347 started at the lowest index in the vector v, if false at
348 the highest index of vector v.
349 */
350 template<bool forward>
351 void apply(X& v, const Y& d)
352 {
353 if(forward)
354 for (int i=0; i<_n; i++) {
355 bsorf(_A_,v,d,_w,BL<l>());
356 }
357 else
358 for (int i=0; i<_n; i++) {
359 bsorb(_A_,v,d,_w,BL<l>());
360 }
361 }
362
363 /*!
364 \brief Clean up.
365
366 \copydoc Preconditioner::post(X&)
367 */
368 void post ([[maybe_unused]] X& x) override
369 {}
370
371 //! Category of the preconditioner (see SolverCategory::Category)
372 SolverCategory::Category category() const override
373 {
374 return SolverCategory::sequential;
375 }
376
377 private:
378 //! \brief the matrix we operate on.
379 const M& _A_;
380 //! \brief The number of steps to perform in apply.
381 int _n;
382 //! \brief The relaxation factor to use.
383 real_field_type _w;
384 };
385 DUNE_REGISTER_PRECONDITIONER("sor", defaultPreconditionerBlockLevelCreator<Dune::SeqSOR>());
386
387
388 /*! \brief Sequential Gauss Seidel preconditioner
389
390 Wraps the naked ISTL generic block Gauss-Seidel preconditioner into the
391 solver framework.
392
393 \tparam M The matrix type to operate on
394 \tparam X Type of the update
395 \tparam Y Type of the defect
396 \tparam l The block level to invert. Default is 1
397 */
398 template<class M, class X, class Y, int l=1>
399 using SeqGS = SeqSOR<M,X,Y,l>;
400 DUNE_REGISTER_PRECONDITIONER("gs", defaultPreconditionerBlockLevelCreator<Dune::SeqGS>());
401
402 /*! \brief The sequential jacobian preconditioner.
403
404 Wraps the naked ISTL generic block Jacobi preconditioner into the
405 solver framework.
406
407 \tparam M The matrix type to operate on
408 \tparam X Type of the update
409 \tparam Y Type of the defect
410 \tparam l The block level to invert. Default is 1
411 */
412 template<class M, class X, class Y, int l=1>
413 class SeqJac : public Preconditioner<X,Y> {
414 public:
415 //! \brief The matrix type the preconditioner is for.
416 typedef M matrix_type;
417 //! \brief The domain type of the preconditioner.
418 typedef X domain_type;
419 //! \brief The range type of the preconditioner.
420 typedef Y range_type;
421 //! \brief The field type of the preconditioner.
422 typedef typename X::field_type field_type;
423 //! \brief scalar type underlying the field_type
424 typedef Simd::Scalar<field_type> scalar_field_type;
425 //! \brief real scalar type underlying the field_type
426 typedef typename FieldTraits<scalar_field_type>::real_type real_field_type;
427
428 /*! \brief Constructor.
429
430 Constructor gets all parameters to operate the prec.
431 \param A The matrix to operate on.
432 \param n The number of iterations to perform.
433 \param w The relaxation factor.
434 */
435 SeqJac (const M& A, int n, real_field_type w)
436 : _A_(A), _n(n), _w(w)
437 {
438 CheckIfDiagonalPresent<M,l>::check(_A_);
439 }
440
441 /*!
442 \brief Constructor.
443
444 \param A The assembled linear operator to use.
445 \param configuration ParameterTree containing preconditioner parameters.
446
447 ParameterTree Key | Meaning
448 ------------------|------------
449 iterations | The number of iterations to perform. default=1
450 relaxation | The relaxation factor. default=1.0
451
452 See \ref ISTL_Factory for the ParameterTree layout and examples.
453 */
454 SeqJac (const std::shared_ptr<const AssembledLinearOperator<M,X,Y>>& A, const ParameterTree& configuration)
455 : SeqJac(A->getmat(), configuration)
456 {}
457
458 /*!
459 \brief Constructor.
460
461 \param A The matrix to operate on.
462 \param configuration ParameterTree containing preconditioner parameters.
463
464 ParameterTree Key | Meaning
465 ------------------|------------
466 iterations | The number of iterations to perform. default=1
467 relaxation | The relaxation factor. default=1.0
468
469 See \ref ISTL_Factory for the ParameterTree layout and examples.
470 */
471 SeqJac (const M& A, const ParameterTree& configuration)
472 : SeqJac(A, configuration.get<int>("iterations",1), configuration.get<real_field_type>("relaxation",1.0))
473 {}
474
475 /*!
476 \brief Prepare the preconditioner.
477
478 \copydoc Preconditioner::pre(X&,Y&)
479 */
480 void pre ([[maybe_unused]] X& x, [[maybe_unused]] Y& b) override
481 {}
482
483 /*!
484 \brief Apply the preconditioner.
485
486 \copydoc Preconditioner::apply(X&,const Y&)
487 */
488 void apply (X& v, const Y& d) override
489 {
490 for (int i=0; i<_n; i++) {
491 dbjac(_A_,v,d,_w,BL<l>());
492 }
493 }
494
495 /*!
496 \brief Clean up.
497
498 \copydoc Preconditioner::post(X&)
499 */
500 void post ([[maybe_unused]] X& x) override
501 {}
502
503 //! Category of the preconditioner (see SolverCategory::Category)
504 SolverCategory::Category category() const override
505 {
506 return SolverCategory::sequential;
507 }
508
509 private:
510 //! \brief The matrix we operate on.
511 const M& _A_;
512 //! \brief The number of steps to perform during apply.
513 int _n;
514 //! \brief The relaxation parameter to use.
515 real_field_type _w;
516 };
517 DUNE_REGISTER_PRECONDITIONER("jac", defaultPreconditionerBlockLevelCreator<Dune::SeqJac>());
518
519 /*!
520 \brief Sequential DILU preconditioner.
521
522 Wraps the naked ISTL generic DILU preconditioner into the solver framework.
523
524 The Diagonal Incomplete LU factorization (DILU) is a simplified variant of the ILU(0) factorization,
525 where only the diagonal elements are factorised. This preconditioner can be written as
526
527 M = (D + L_A) D^{-1} (D + U_A),
528
529 where L_A and U_A are the strictly lower and upper parts of A and D is the diagonal matrix containing
530 the generated pivot values. The matrix M has the property
531
532 diag(A) = diag(M) = diag((D + L_A) D^{-1} (D + U_A)) = diag(D + L_A D^{-1} U_A)
533
534 such that the diagonal matrix D can be constructed:
535
536 D_11 = A_11
537 D_22 = A22 - L_A_{21} D_{11}^{-1} U_A_{12}
538 and etc.
539
540 Note that when applying the preconditioner, M is never explicitly created but instead a lower and
541 upper triangualr solve is performed using the values of A and D^-1. When applying the preconditioner,
542 we are working with the residual d = b - Ax and update v = x_{n+1} - x_n, such that:
543
544 v = M^{-1} d = (D + U_A)^{-1} D (D + L_A)^{-1} d
545
546 define y = (D + L_A)^{-1} d
547
548 lower triangular solve: (D + L_A) y = d
549 upper triangular solve: (D + U_A) v = D y
550
551 This means that the preconditioner only requires an additional storage of the diagonal matrix D^{-1}
552
553 For more details, see: R. Barrett et al., "Templates for the Solution of Linear Systems:
554 Building Blocks for Iterative Methods", 1994. Available from: https://www.netlib.org/templates/templates.pdf
555
556 \tparam M The matrix type to operate on
557 \tparam X Type of the update
558 \tparam Y Type of the defect
559 \tparam l Ignored. Just there to have the same number of template arguments
560 as other preconditioners.
561 */
562 template <class M, class X, class Y, int l = 1>
563 class SeqDILU : public Preconditioner<X, Y>
564 {
565 public:
566 //! \brief The matrix type the preconditioner is for.
567 using matrix_type = M;
568 //! block type of matrix
569 using block_type = typename matrix_type::block_type;
570 //! \brief The domain type of the preconditioner.
571 using domain_type = X;
572 //! \brief The range type of the preconditioner.
573 using range_type = Y;
574
575 //! \brief The field type of the preconditioner.
576 using field_type = typename X::field_type;
577
578 //! \brief scalar type underlying the field_type
579 using scalar_field_type = Simd::Scalar<field_type>;
580 //! \brief real scalar type underlying the field_type
581 using real_field_type = typename FieldTraits<scalar_field_type>::real_type;
582
583 /*! \brief Constructor.
584
585 Constructor invoking DILU gets all parameters to operate the prec.
586 \param A The matrix to operate on.
587 \param w The relaxation factor.
588 */
589 SeqDILU(const M &A, real_field_type w)
590 : _A_(A),
591 _w(w),
592 wNotIdentity_([w]
593 {using std::abs; return abs(w - real_field_type(1)) > 1e-15; }())
594 {
595 Dinv_.resize(_A_.N());
596 CheckIfDiagonalPresent<M, l>::check(_A_);
597 DILU::blockDILUDecomposition(_A_, Dinv_);
598 }
599
600 /*!
601 \brief Constructor.
602
603 \param A The assembled linear operator to use.
604 \param configuration ParameterTree containing preconditioner parameters.
605
606 ParameterTree Key | Meaning
607 ------------------|------------
608 relaxation | The relaxation factor. default=1.0
609
610 See \ref ISTL_Factory for the ParameterTree layout and examples.
611 */
612 SeqDILU(const std::shared_ptr<const AssembledLinearOperator<M, X, Y>> &A, const ParameterTree &configuration)
613 : SeqDILU(A->getmat(), configuration)
614 {
615 }
616
617 /*!
618 \brief Constructor.
619
620 \param A The matrix to operate on.
621 \param config ParameterTree containing preconditioner parameters.
622
623 ParameterTree Key | Meaning
624 ------------------|------------
625 relaxation | The relaxation factor. default=1.0
626
627 See \ref ISTL_Factory for the ParameterTree layout and examples.
628 */
629 SeqDILU(const M &A, const ParameterTree &config)
630 : SeqDILU(A, config.get<real_field_type>("relaxation", 1.0))
631 {
632 }
633
634 /*!
635 \brief Prepare the preconditioner.
636
637 \copydoc Preconditioner::pre(X&,Y&)
638 */
639 void pre([[maybe_unused]] X &x, [[maybe_unused]] Y &b) override
640 {
641 }
642
643 /*!
644 \brief Apply the preconditioner.
645
646 \copydoc Preconditioner::apply(X&,const Y&)
647 */
648 void apply(X &v, const Y &d) override
649 {
650
651 DILU::blockDILUBacksolve(_A_, Dinv_, v, d);
652
653 if (wNotIdentity_)
654 {
655 v *= _w;
656 }
657 }
658
659 /*!
660 \brief Clean up.
661
662 \copydoc Preconditioner::post(X&)
663 */
664 void post([[maybe_unused]] X &x) override
665 {
666 }
667
668 //! Category of the preconditioner (see SolverCategory::Category)
669 SolverCategory::Category category() const override
670 {
671 return SolverCategory::sequential;
672 }
673
674 protected:
675 std::vector<block_type> Dinv_;
676 //! \brief The matrix we operate on.
677 const M &_A_;
678 //! \brief The relaxation factor to use.
679 const real_field_type _w;
680 //! \brief true if w != 1.0
681 const bool wNotIdentity_;
682 };
683 DUNE_REGISTER_PRECONDITIONER("dilu", defaultPreconditionerBlockLevelCreator<Dune::SeqDILU>());
684
685 /*!
686 \brief Sequential ILU preconditioner.
687
688 Wraps the naked ISTL generic ILU preconditioner into the solver framework.
689
690 \tparam M The matrix type to operate on
691 \tparam X Type of the update
692 \tparam Y Type of the defect
693 \tparam l Ignored. Just there to have the same number of template arguments
694 as other preconditioners.
695 */
696 template<class M, class X, class Y, int l=1>
697 class SeqILU : public Preconditioner<X,Y> {
698 public:
699 //! \brief The matrix type the preconditioner is for.
700 typedef typename std::remove_const<M>::type matrix_type;
701 //! block type of matrix
702 typedef typename matrix_type :: block_type block_type;
703 //! \brief The domain type of the preconditioner.
704 typedef X domain_type;
705 //! \brief The range type of the preconditioner.
706 typedef Y range_type;
707
708 //! \brief The field type of the preconditioner.
709 typedef typename X::field_type field_type;
710
711 //! \brief scalar type underlying the field_type
712 typedef Simd::Scalar<field_type> scalar_field_type;
713 //! \brief real scalar type underlying the field_type
714 typedef typename FieldTraits<scalar_field_type>::real_type real_field_type;
715
716 //! \brief type of ILU storage
717 typedef typename ILU::CRS< block_type , typename M::allocator_type> CRS;
718
719 /*! \brief Constructor.
720
721 Constructor invoking ILU(0) gets all parameters to operate the prec.
722 \param A The matrix to operate on.
723 \param w The relaxation factor.
724 \param resort true if a resort of the computed ILU for improved performance should be done.
725 */
726 SeqILU (const M& A, real_field_type w, const bool resort = false )
727 : SeqILU( A, 0, w, resort ) // construct ILU(0)
728 {
729 }
730
731 /*!
732 \brief Constructor.
733
734 \param A The assembled linear operator to use.
735 \param configuration ParameterTree containing preconditioner parameters.
736
737 ParameterTree Key | Meaning
738 ------------------|------------
739 n | The order of the ILU decomposition. default=0
740 relaxation | The relaxation factor. default=1.0
741 resort | True if a resort of the computed ILU for improved performance should be done. default=false
742
743 See \ref ISTL_Factory for the ParameterTree layout and examples.
744 */
745 SeqILU (const std::shared_ptr<const AssembledLinearOperator<M,X,Y>>& A, const ParameterTree& configuration)
746 : SeqILU(A->getmat(), configuration)
747 {}
748
749 /*!
750 \brief Constructor.
751
752 \param A The matrix to operate on.
753 \param config ParameterTree containing preconditioner parameters.
754
755 ParameterTree Key | Meaning
756 ------------------|------------
757 n | The order of the ILU decomposition. default=0
758 relaxation | The relaxation factor. default=1.0
759 resort | True if a resort of the computed ILU for improved performance should be done. default=false
760
761 See \ref ISTL_Factory for the ParameterTree layout and examples.
762 */
763 SeqILU(const M& A, const ParameterTree& config)
764 : SeqILU(A, config.get("n", 0),
765 config.get<real_field_type>("relaxation", 1.0),
766 config.get("resort", false))
767 {}
768
769 /*! \brief Constructor.
770
771 Constructor invoking ILU(n).
772 \param A The matrix to operate on.
773 \param n The order of the ILU decomposition.
774 \param w The relaxation factor.
775 \param resort true if a resort of the computed ILU for improved performance should be done.
776 */
777 SeqILU (const M& A, int n, real_field_type w, const bool resort = false )
778 : ILU_(),
779 lower_(),
780 upper_(),
781 inv_(),
782 w_(w),
783 wNotIdentity_([w]{using std::abs; return abs(w - real_field_type(1)) > 1e-15;}() )
784 {
785 if( n == 0 )
786 {
787 // copy A
788 ILU_.reset( new matrix_type( A ) );
789 // create ILU(0) decomposition
790 ILU::blockILU0Decomposition( *ILU_ );
791 }
792 else
793 {
794 // create matrix in build mode
795 ILU_.reset( new matrix_type( A.N(), A.M(), matrix_type::row_wise) );
796 // create ILU(n) decomposition
797 ILU::blockILUDecomposition( A, n, *ILU_ );
798 }
799
800 if( resort )
801 {
802 // store ILU in simple CRS format
803 ILU::convertToCRS( *ILU_, lower_, upper_, inv_ );
804 ILU_.reset();
805 }
806 }
807
808 /*!
809 \brief Prepare the preconditioner.
810
811 \copydoc Preconditioner::pre(X&,Y&)
812 */
813 void pre ([[maybe_unused]] X& x, [[maybe_unused]] Y& b) override
814 {}
815
816 /*!
817 \brief Apply the preconditioner.
818
819 \copydoc Preconditioner::apply(X&,const Y&)
820 */
821 void apply (X& v, const Y& d) override
822 {
823 if( ILU_ )
824 {
825 ILU::blockILUBacksolve( *ILU_, v, d);
826 }
827 else
828 {
829 ILU::blockILUBacksolve(lower_, upper_, inv_, v, d);
830 }
831
832 if( wNotIdentity_ )
833 {
834 v *= w_;
835 }
836 }
837
838 /*!
839 \brief Clean up.
840
841 \copydoc Preconditioner::post(X&)
842 */
843 void post ([[maybe_unused]] X& x) override
844 {}
845
846 //! Category of the preconditioner (see SolverCategory::Category)
847 SolverCategory::Category category() const override
848 {
849 return SolverCategory::sequential;
850 }
851
852 protected:
853 //! \brief The ILU(n) decomposition of the matrix. As storage a BCRSMatrix is used.
854 std::unique_ptr< matrix_type > ILU_;
855
856 //! \brief The ILU(n) decomposition of the matrix. As storage a CRS structure is used.
857 CRS lower_;
858 CRS upper_;
859 std::vector< block_type, typename matrix_type::allocator_type > inv_;
860
861 //! \brief The relaxation factor to use.
862 const real_field_type w_;
863 //! \brief true if w != 1.0
864 const bool wNotIdentity_;
865 };
866 DUNE_REGISTER_PRECONDITIONER("ilu", defaultPreconditionerBlockLevelCreator<Dune::SeqILU>());
867
868
869 /*!
870 \brief Richardson preconditioner.
871
872 Multiply simply by a constant.
873
874 \tparam X Type of the update
875 \tparam Y Type of the defect
876 */
877 template<class X, class Y>
878 class Richardson : public Preconditioner<X,Y> {
879 public:
880 //! \brief The domain type of the preconditioner.
881 typedef X domain_type;
882 //! \brief The range type of the preconditioner.
883 typedef Y range_type;
884 //! \brief The field type of the preconditioner.
885 typedef typename X::field_type field_type;
886 //! \brief scalar type underlying the field_type
887 typedef Simd::Scalar<field_type> scalar_field_type;
888 //! \brief real scalar type underlying the field_type
889 typedef typename FieldTraits<scalar_field_type>::real_type real_field_type;
890
891 /*! \brief Constructor.
892
893 Constructor gets all parameters to operate the prec.
894 \param w The relaxation factor.
895 */
896 Richardson (real_field_type w=1.0) :
897 _w(w)
898 {}
899
900 /*!
901 \brief Constructor.
902
903 \param configuration ParameterTree containing preconditioner parameters.
904
905 ParameterTree Key | Meaning
906 ------------------|------------
907 relaxation | The relaxation factor. default=1.0
908
909 See \ref ISTL_Factory for the ParameterTree layout and examples.
910 */
911 Richardson (const ParameterTree& configuration)
912 : Richardson(configuration.get<real_field_type>("relaxation", 1.0))
913 {}
914
915 /*!
916 \brief Prepare the preconditioner.
917
918 \copydoc Preconditioner::pre(X&,Y&)
919 */
920 void pre ([[maybe_unused]] X& x, [[maybe_unused]] Y& b) override
921 {}
922
923 /*!
924 \brief Apply the precondioner.
925
926 \copydoc Preconditioner::apply(X&,const Y&)
927 */
928 void apply (X& v, const Y& d) override
929 {
930 v = d;
931 v *= _w;
932 }
933
934 /*!
935 \brief Clean up.
936
937 \copydoc Preconditioner::post(X&)
938 */
939 void post ([[maybe_unused]] X& x) override
940 {}
941
942 //! Category of the preconditioner (see SolverCategory::Category)
943 SolverCategory::Category category() const override
944 {
945 return SolverCategory::sequential;
946 }
947
948 private:
949 //! \brief The relaxation factor to use.
950 real_field_type _w;
951 };
952 DUNE_REGISTER_PRECONDITIONER("richardson", [](auto opTraits, const auto& /*op*/, const ParameterTree& config){
953 using OpTraits = std::decay_t<decltype(opTraits)>;
954 using D = typename OpTraits::domain_type;
955 using R = typename OpTraits::range_type;
956 return std::make_shared<Richardson<D,R>>(config);
957 });
958
959
960 /**
961 * \brief sequential ILDL preconditioner
962 * \author Martin Nolte
963 *
964 * Wraps the naked ISTL generic ILDL preconditioner into the solver framework.
965 *
966 * \tparam M type of matrix to operate on
967 * \tparam X type of update
968 * \tparam Y type of defect
969 **/
970 template< class M, class X, class Y >
971 class SeqILDL
972 : public Preconditioner< X, Y >
973 {
974 typedef SeqILDL< M, X, Y > This;
975 typedef Preconditioner< X, Y > Base;
976
977 public:
978 /** \brief type of matrix the preconditioner is for **/
979 typedef std::remove_const_t< M > matrix_type;
980 /** \brief domain type of the preconditioner **/
981 typedef X domain_type;
982 /** \brief range type of the preconditioner **/
983 typedef Y range_type;
984 /** \brief field type of the preconditioner **/
985 typedef typename X::field_type field_type;
986 //! \brief scalar type underlying the field_type
987 typedef Simd::Scalar<field_type> scalar_field_type;
988 //! \brief real scalar type underlying the field_type
989 typedef typename FieldTraits<scalar_field_type>::real_type real_field_type;
990
991 /*!
992 \brief Constructor.
993
994 \param A The linear operator to use.
995 \param configuration ParameterTree containing preconditioner parameters.
996
997 ParameterTree Key | Meaning
998 ------------------|------------
999 relaxation | relaxation factor
1000
1001 See \ref ISTL_Factory for the ParameterTree layout and examples.
1002 */
1003 SeqILDL (const std::shared_ptr<const AssembledLinearOperator<M,X,Y>>& A, const ParameterTree& configuration)
1004 : SeqILDL(A->getmat(), configuration)
1005 {}
1006
1007 /*!
1008 \brief Constructor.
1009
1010 \param A The matrix to operate on.
1011 \param config ParameterTree containing preconditioner parameters.
1012
1013 ParameterTree Key | Meaning
1014 ------------------|------------
1015 relaxation | relaxation factor. default=1.0
1016
1017 See \ref ISTL_Factory for the ParameterTree layout and examples.
1018 */
1019 SeqILDL(const matrix_type& A, const ParameterTree& config)
1020 : SeqILDL(A, config.get<real_field_type>("relaxation", 1.0))
1021 {}
1022
1023 /**
1024 * \brief constructor
1025 *
1026 * The constructor copies the matrix A and computes its ILDL decomposition.
1027 *
1028 * \param[in] A matrix to operate on
1029 * \param[in] relax relaxation factor
1030 **/
1031 explicit SeqILDL ( const matrix_type &A, real_field_type relax = real_field_type( 1 ) )
1032 : decomposition_( A.N(), A.M(), matrix_type::random ),
1033 relax_( relax )
1034 {
1035 // setup row sizes for lower triangular matrix
1036 for( auto i = A.begin(), iend = A.end(); i != iend; ++i )
1037 {
1038 const auto &A_i = *i;
1039 const auto ij = A_i.find( i.index() );
1040 if( ij != A_i.end() )
1041 decomposition_.setrowsize( i.index(), ij.offset()+1 );
1042 else
1043 DUNE_THROW( ISTLError, "diagonal entry missing" );
1044 }
1045 decomposition_.endrowsizes();
1046
1047 // setup row indices for lower triangular matrix
1048 for( auto i = A.begin(), iend = A.end(); i != iend; ++i )
1049 {
1050 const auto &A_i = *i;
1051 for( auto ij = A_i.begin(); ij.index() < i.index() ; ++ij )
1052 decomposition_.addindex( i.index(), ij.index() );
1053 decomposition_.addindex( i.index(), i.index() );
1054 }
1055 decomposition_.endindices();
1056
1057 // copy values of lower triangular matrix
1058 auto i = A.begin();
1059 for( auto row = decomposition_.begin(), rowend = decomposition_.end(); row != rowend; ++row, ++i )
1060 {
1061 auto ij = i->begin();
1062 for( auto col = row->begin(), colend = row->end(); col != colend; ++col, ++ij )
1063 *col = *ij;
1064 }
1065
1066 // perform ILDL decomposition
1067 bildl_decompose( decomposition_ );
1068 }
1069
1070 /** \copydoc Preconditioner::pre(X&,Y&) **/
1071 void pre ([[maybe_unused]] X &x, [[maybe_unused]] Y &b) override
1072 {}
1073
1074 /** \copydoc Preconditioner::apply(X&,const Y&) **/
1075 void apply ( X &v, const Y &d ) override
1076 {
1077 bildl_backsolve( decomposition_, v, d, true );
1078 v *= relax_;
1079 }
1080
1081 /** \copydoc Preconditioner::post(X&) **/
1082 void post ([[maybe_unused]] X &x) override
1083 {}
1084
1085 /** \copydoc Preconditioner::category() **/
1086 SolverCategory::Category category () const override { return SolverCategory::sequential; }
1087
1088 private:
1089 matrix_type decomposition_;
1090 real_field_type relax_;
1091 };
1092 DUNE_REGISTER_PRECONDITIONER("ildl", defaultPreconditionerCreator<Dune::SeqILDL>());
1093
1094 /** @} end documentation */
1095
1096 } // end namespace
1097
1098
1099 #endif