| 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 |
#ifndef DUNE_PYTHON_ISTL_PRECONDITIONER_HH |
| 4 |
#define DUNE_PYTHON_ISTL_PRECONDITIONER_HH |
| 5 |
|
| 6 |
#include <dune/common/typeutilities.hh> |
| 7 |
#include <dune/istl/operators.hh> |
| 8 |
#include <dune/istl/preconditioner.hh> |
| 9 |
#include <dune/istl/preconditioners.hh> |
| 10 |
|
| 11 |
#include <dune/python/pybind11/pybind11.h> |
| 12 |
|
| 13 |
namespace Dune |
| 14 |
{ |
| 15 |
|
| 16 |
namespace Python |
| 17 |
{ |
| 18 |
|
| 19 |
// registerPreconditioner |
| 20 |
// ---------------------- |
| 21 |
|
| 22 |
template< class Preconditioner, class... options > |
| 23 |
inline void registerPreconditioner ( pybind11::class_< Preconditioner, options... > cls ) |
| 24 |
{ |
| 25 |
typedef typename Preconditioner::domain_type domain_type; |
| 26 |
typedef typename Preconditioner::range_type range_type; |
| 27 |
|
| 28 |
using pybind11::operator""_a; |
| 29 |
|
| 30 |
pybind11::options opts; |
| 31 |
opts.disable_function_signatures(); |
| 32 |
|
| 33 |
cls.def( "__call__", [] ( Preconditioner &self, domain_type &v, const range_type &d ) { |
| 34 |
self.apply( v, d ); |
| 35 |
}, "update", "defect" ); |
| 36 |
|
| 37 |
cls.def( "pre", [] ( Preconditioner &self, domain_type &x, range_type &b ) { |
| 38 |
self.pre( x, b ); |
| 39 |
}, "x"_a, "rhs"_a ); |
| 40 |
|
| 41 |
cls.def( "post", [] ( Preconditioner &self, domain_type &x ) { |
| 42 |
self.post( x ); |
| 43 |
}, "x"_a ); |
| 44 |
|
| 45 |
cls.def_property_readonly( "category", [] ( const Preconditioner &self ) { return self.category(); } ); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
// registerPreconditioners |
| 51 |
// ----------------------- |
| 52 |
|
| 53 |
template< class X, class Y, class... options > |
| 54 |
inline void registerPreconditioners ( pybind11::module module, pybind11::class_< LinearOperator< X, Y >, options... > /*cls*/ ) |
| 55 |
{ |
| 56 |
typedef Dune::Preconditioner< X, Y > Preconditioner; |
| 57 |
|
| 58 |
typedef typename Preconditioner::field_type field_type; |
| 59 |
|
| 60 |
using pybind11::operator""_a; |
| 61 |
|
| 62 |
pybind11::class_< Preconditioner > clsPreconditioner( module, "Preconditioner" ); |
| 63 |
registerPreconditioner( clsPreconditioner ); |
| 64 |
|
| 65 |
module.def( "Richardson", [] ( field_type w ) { |
| 66 |
return static_cast< Preconditioner * >( new Richardson< X, Y >( w ) ); |
| 67 |
}, "relaxation"_a = field_type( 1 ), |
| 68 |
R"doc( |
| 69 |
Richardson preconditioner |
| 70 |
|
| 71 |
Args: |
| 72 |
relaxation: factor to relax the input by (default: 1) |
| 73 |
|
| 74 |
Returns: |
| 75 |
ISTL Richardson preconditioner |
| 76 |
|
| 77 |
Note: |
| 78 |
Use this preconditioner with default parameters if you do not want to apply preconditioning. |
| 79 |
This is a sequential preconditioner. |
| 80 |
)doc" ); |
| 81 |
} |
| 82 |
|
| 83 |
template< class M, class X, class Y, class... options > |
| 84 |
inline void registerMatrixPreconditioners ( pybind11::module module, pybind11::class_< LinearOperator< X, Y >, options... > /*cls*/ ) |
| 85 |
{ |
| 86 |
typedef Dune::Preconditioner< X, Y > Preconditioner; |
| 87 |
|
| 88 |
typedef typename Preconditioner::field_type field_type; |
| 89 |
|
| 90 |
using pybind11::operator""_a; |
| 91 |
|
| 92 |
pybind11::options opts; |
| 93 |
opts.disable_function_signatures(); |
| 94 |
|
| 95 |
module.def( "SeqSSOR", [] ( const M &A, int n, field_type w ) { |
| 96 |
return static_cast< Preconditioner * >( new SeqSSOR< M, X, Y >( A, n, w ) ); |
| 97 |
}, "matrix"_a, "iterations"_a = 1, "relaxation"_a = field_type( 1 ), |
| 98 |
R"doc( |
| 99 |
Symmetric successive over-relaxation preconditioner |
| 100 |
|
| 101 |
Args: |
| 102 |
matrix: matrix to precondition |
| 103 |
iterations: number of iterations to perform |
| 104 |
relaxation: factor to relax the iterations |
| 105 |
|
| 106 |
Returns: |
| 107 |
ISTL Sequential symmetric successive over-relaxation preconditioner |
| 108 |
|
| 109 |
Note: |
| 110 |
The symmetric successive over-relaxation iteration can only be applied if the matrix is symmetric and the diagonal entries are all non-zero. |
| 111 |
)doc" ); |
| 112 |
|
| 113 |
module.def( "SeqSOR", [] ( const M &A, int n, field_type w ) { |
| 114 |
return static_cast< Preconditioner * >( new SeqSOR< M, X, Y >( A, n, w ) ); |
| 115 |
}, "matrix"_a, "iterations"_a = 1, "relaxation"_a = field_type( 1 ), |
| 116 |
R"doc( |
| 117 |
Successive over-relaxation preconditioner |
| 118 |
|
| 119 |
Args: |
| 120 |
matrix: matrix to precondition |
| 121 |
iterations: number of iterations to perform |
| 122 |
relaxation: factor to relax the iterations (default: 1) |
| 123 |
|
| 124 |
Returns: |
| 125 |
ISTL Sequential successive over-relaxation preconditioner |
| 126 |
|
| 127 |
Note: |
| 128 |
The successive over-relaxation iteration can only be applied if the matrix diagonal entries are all non-zero. |
| 129 |
)doc" ); |
| 130 |
|
| 131 |
module.def( "SeqGaussSeidel", [] ( const M &A, int n, field_type w ) { |
| 132 |
return static_cast< Preconditioner * >( new SeqGS< M, X, Y >( A, n, w ) ); |
| 133 |
}, "matrix"_a, "iterations"_a = 1, "relaxation"_a = field_type( 1 ), |
| 134 |
R"doc( |
| 135 |
Gauss-Seidel preconditioner |
| 136 |
|
| 137 |
Args: |
| 138 |
matrix: matrix to precondition |
| 139 |
iterations: number of iterations to perform |
| 140 |
relaxation: factor to relax the iterations (default: 1) |
| 141 |
|
| 142 |
Returns: |
| 143 |
ISTL Sequential Gauss-Seidel preconditioner |
| 144 |
|
| 145 |
Note: |
| 146 |
The Gauss-Seidel iteration can only be applied if the matrix diagonal entries are all non-zeros. |
| 147 |
)doc" ); |
| 148 |
|
| 149 |
module.def( "SeqJacobi", [] ( const M &A, int n, field_type w ) { |
| 150 |
return static_cast< Preconditioner * >( new SeqJac< M, X, Y >( A, n, w ) ); |
| 151 |
}, "matrix"_a, "iterations"_a = 1, "relaxation"_a = field_type( 1 ), |
| 152 |
R"doc( |
| 153 |
Jacobi preconditioner |
| 154 |
|
| 155 |
Args: |
| 156 |
matrix: matrix to precondition |
| 157 |
iterations: number of iterations to perform |
| 158 |
relaxation: factor to relax the iterations (default: 1) |
| 159 |
|
| 160 |
Returns: |
| 161 |
ISTL Sequential Jacobi preconditioner |
| 162 |
|
| 163 |
Note: |
| 164 |
The Jacobi iteration can only be applied if the matrix diagonal entries are all non-zeros. |
| 165 |
)doc" ); |
| 166 |
|
| 167 |
module.def( "SeqDILU", [] ( const M &A, field_type w ) { |
| 168 |
return static_cast< Preconditioner * >( new SeqDILU< M, X, Y >( A, w ) ); |
| 169 |
}, "matrix"_a, "relaxation"_a = field_type( 1 ), |
| 170 |
R"doc(Diagonal incomplete LU factorization preconditioner |
| 171 |
|
| 172 |
Args: |
| 173 |
matrix: matrix to precondition |
| 174 |
relaxation: factor to relax the iterations (default: 1) |
| 175 |
|
| 176 |
Returns: |
| 177 |
ISTL Sequential diagonal incomplete LU factorization preconditioner |
| 178 |
|
| 179 |
)doc" ); |
| 180 |
|
| 181 |
module.def( "SeqILU", [] ( const M &A, int n, field_type w ) { |
| 182 |
return static_cast< Preconditioner * >( new SeqILU< M, X, Y >( A, n, w ) ); |
| 183 |
}, "matrix"_a, "iterations"_a = 1, "relaxation"_a = field_type( 1 ), |
| 184 |
R"doc(Incomplete LU factorization (with fill-in) preconditioner |
| 185 |
|
| 186 |
Args: |
| 187 |
matrix: matrix to precondition |
| 188 |
iterations: number of fill-in iterations |
| 189 |
relaxation: factor to relax the iterations (default: 1) |
| 190 |
|
| 191 |
Returns: |
| 192 |
ISTL Sequential incomplete LU factorization preconditioner |
| 193 |
|
| 194 |
Note: |
| 195 |
The incomplete LU factorization with fill-in has the same sparsity pattern as the given matrix. |
| 196 |
)doc" ); |
| 197 |
|
| 198 |
module.def( "SeqILDL", [] ( const M &A, field_type w ) { |
| 199 |
return static_cast< Preconditioner * >( new SeqILDL< M, X, Y >( A, w ) ); |
| 200 |
}, "matrix"_a, "relaxation"_a = field_type( 1 ), |
| 201 |
R"doc( |
| 202 |
Incomplete LDL factorization preconditioner |
| 203 |
|
| 204 |
Args: |
| 205 |
matrix: matrix to precondition |
| 206 |
relaxation: factor to relax the iterations (default: 1) |
| 207 |
|
| 208 |
Returns: |
| 209 |
ISTL Sequential incomplete LDL factorization preconditioner |
| 210 |
|
| 211 |
Note: |
| 212 |
The matrix is assumed to by symmetric, so the upper triangular matrix is ignored |
| 213 |
and need not be assembled. |
| 214 |
The incomplete LDL factorization with fill-in has the same sparsity pattern as the |
| 215 |
given matrix, however only L and D are actually stored. |
| 216 |
)doc" ); |
| 217 |
} |
| 218 |
|
| 219 |
} // namespace Python |
| 220 |
|
| 221 |
} // namespace Dune |
| 222 |
|
| 223 |
#endif // #ifndef DUNE_PYTHON_ISTL_PRECONDITIONER_HH |