Debian Codegraph @

dune/python/istl/solvers.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 #ifndef DUNE_PYTHON_ISTL_SOLVER_HH
4 #define DUNE_PYTHON_ISTL_SOLVER_HH
5
6 #include <dune/common/typeutilities.hh>
7
8 #include <dune/istl/solver.hh>
9 #include <dune/istl/solvers.hh>
10 #include <dune/istl/preconditioners.hh>
11
12 #include <dune/python/istl/preconditioners.hh>
13
14 #include <dune/python/pybind11/pybind11.h>
15
16 namespace Dune
17 {
18
19 namespace Python
20 {
21
22 // registerInverseOperator
23 // -----------------------
24
25 template< class Solver, class... options >
26 inline void registerInverseOperator ( pybind11::class_< Solver, options... > cls )
27 {
28 typedef typename Solver::domain_type Domain;
29 typedef typename Solver::range_type Range;
30
31 using pybind11::operator""_a;
32
33 cls.def( "__call__", [] ( Solver &self, Domain &x, Range &b, double reduction ) {
34 InverseOperatorResult result;
35 self.apply( x, b, reduction, result );
36 return std::make_tuple( result.iterations, result.reduction, result.converged, result.conv_rate, result.elapsed );
37 }, "x"_a, "b"_a, "reduction"_a,
38 R"doc(
39 Solve linear system
40
41 Args:
42 x: solution of linear system
43 b: right hand side of the system
44 reduction: factor to reduce the defect by
45
46 Returns: (iterations, reduction, converged, conv_rate, elapsed)
47 iterations: number of iterations performed
48 reduction: actual factor, the error has been reduced by
49 converged: True, if the solver has achieved its reduction requirements
50 conv_rate: rate of convergence
51 elapsed: time in seconds used to solve the linear system
52
53 Note:
54 - If the reduction is omitted, the default value of the solver is used.
55 - For iterative solvers, the solution must be initialized to the starting point.
56 - The right hand side b will be replaced by the residual.
57 )doc" );
58
59 cls.def( "__call__", [] ( Solver &self, Domain &x, Range &b ) {
60 InverseOperatorResult result;
61 self.apply( x, b, result );
62 return std::make_tuple( result.iterations, result.reduction, result.converged, result.conv_rate, result.elapsed );
63 }, "x"_a, "b"_a );
64
65 cls.def_property_readonly( "category", [] ( const Solver &self ) { return self.category(); },
66 R"doc(
67 Obtain category of the linear solver
68 )doc" );
69
70 cls.def( "asPreconditioner", [] ( Solver &self ) {
71 return new InverseOperator2Preconditioner< Solver >( self );
72 }, pybind11::keep_alive< 0, 1 >(),
73 R"doc(
74 Convert linear solver into preconditioner
75 )doc" );
76 }
77
78
79
80 namespace detail
81 {
82
83 // registerEndomorphismSolvers
84 // ---------------------------
85
86 template< class X, class Y, class... options >
87 inline std::enable_if_t< std::is_same< X, Y >::value >
88 registerEndomorphismSolvers ( pybind11::module module, pybind11::class_< LinearOperator< X, Y >, options... >, PriorityTag< 1 > )
89 {
90 typedef Dune::InverseOperator< X, Y > Solver;
91
92 using pybind11::operator""_a;
93
94 pybind11::options opts;
95 opts.disable_function_signatures();
96
97 module.def( "LoopSolver", [] ( LinearOperator< X, X > &op, Preconditioner< X, X > &prec, double reduction, int maxit, int verbose ) {
98 return static_cast< Solver * >( new Dune::LoopSolver< X >( op, prec, reduction, maxit, verbose ) );
99 }, "operator"_a, "preconditioner"_a, "reduction"_a, "maxIterations"_a = std::numeric_limits< int >::max(), "verbose"_a = 0, pybind11::keep_alive< 0, 1 >(), pybind11::keep_alive< 0, 2 >(),
100 R"doc(
101 Loop solver
102
103 Args:
104 operator: operator to invert
105 preconditioner: preconditioner to use (i.e., apprixmate inverse of the operator)
106 reduction: factor to reduce the defect by
107 maxIterations: maximum number of iterations to perform
108 verbose: verbosity level (0 = quiet, 1 = summary, 2 = verbose)
109
110 Returns:
111 ISTL Loop solver
112
113 Note:
114 The loop solver will apply the preconditioner once in each step.
115 )doc" );
116
117 module.def( "GradientSolver", [] ( LinearOperator< X, X > &op, Preconditioner< X, X > &prec, double reduction, int maxit, int verbose ) {
118 return static_cast< Solver * >( new Dune::GradientSolver< X >( op, prec, reduction, maxit, verbose ) );
119 }, "operator"_a, "preconditioner"_a, "reduction"_a, "maxIterations"_a = std::numeric_limits< int >::max(), "verbose"_a = 0, pybind11::keep_alive< 0, 1 >(), pybind11::keep_alive< 0, 2 >(),
120 R"doc(
121 Gradient iterative solver
122
123 Args:
124 operator: operator to invert
125 preconditioner: preconditioner to use (i.e., apprixmate inverse of the operator)
126 reduction: factor to reduce the defect by
127 maxIterations: maximum number of iterations to perform
128 verbose: verbosity level (0 = quiet, 1 = summary, 2 = verbose)
129
130 Returns:
131 ISTL Gradient solver
132
133 Note:
134 This method is also know as steepest descend method.
135 )doc" );
136
137 module.def( "CGSolver", [] ( LinearOperator< X, X > &op, Preconditioner< X, X > &prec, double reduction, int maxit, int verbose ) {
138 return static_cast< Solver * >( new Dune::CGSolver< X >( op, prec, reduction, maxit, verbose ) );
139 }, "operator"_a, "preconditioner"_a, "reduction"_a, "maxIterations"_a = std::numeric_limits< int >::max(), "verbose"_a = 0, pybind11::keep_alive< 0, 1 >(), pybind11::keep_alive< 0, 2 >(),
140 R"doc(
141 Conjugate gradient iterative solver
142
143 Args:
144 operator: operator to invert
145 preconditioner: preconditioner to use (i.e., apprixmate inverse of the operator)
146 reduction: factor to reduce the defect by
147 maxIterations: maximum number of iterations to perform
148 verbose: verbosity level (0 = quiet, 1 = summary, 2 = verbose)
149
150 Returns:
151 ISTL Conjugate gradient solver
152
153 Note:
154 The conjucate gradient method can only be applied if the operator and the preconditioner are both symmetric and positive definite.
155 )doc" );
156
157 module.def( "BiCGSTABSolver", [] ( LinearOperator< X, X > &op, Preconditioner< X, X > &prec, double reduction, int maxit, int verbose ) {
158 return static_cast< Solver * >( new Dune::CGSolver< X >( op, prec, reduction, maxit, verbose ) );
159 }, "operator"_a, "preconditioner"_a, "reduction"_a, "maxIterations"_a = std::numeric_limits< int >::max(), "verbose"_a = 0, pybind11::keep_alive< 0, 1 >(), pybind11::keep_alive< 0, 2 >(),
160 R"doc(
161 Biconjugate gradient stabilized iterative solver
162
163 Args:
164 operator: operator to invert
165 preconditioner: preconditioner to use (i.e., apprixmate inverse of the operator)
166 reduction: factor to reduce the defect by
167 maxIterations: maximum number of iterations to perform
168 verbose: verbosity level (0 = quiet, 1 = summary, 2 = verbose)
169
170 Returns:
171 ISTL Biconjugate gradient stabilized solver
172 )doc" );
173
174 module.def( "MinResSolver", [] ( LinearOperator< X, X > &op, Preconditioner< X, X > &prec, double reduction, int maxit, int verbose ) {
175 return static_cast< Solver * >( new Dune::CGSolver< X >( op, prec, reduction, maxit, verbose ) );
176 }, "operator"_a, "preconditioner"_a, "reduction"_a, "maxIterations"_a = std::numeric_limits< int >::max(), "verbose"_a = 0, pybind11::keep_alive< 0, 1 >(), pybind11::keep_alive< 0, 2 >(),
177 R"doc(
178 Minimal residual iterative solver
179
180 Args:
181 operator: operator to invert
182 preconditioner: preconditioner to use (i.e., apprixmate inverse of the operator)
183 reduction: factor to reduce the defect by
184 maxIterations: maximum number of iterations to perform
185 verbose: verbosity level (0 = quiet, 1 = summary, 2 = verbose)
186
187 Returns:
188 ISTL Minimal residual solver
189
190 Note:
191 The minimal residual method can only be applied if the operator and the preconditioner are both symmetric.
192 )doc" );
193 }
194
195 template< class X, class Y, class... options >
196 inline std::enable_if_t< std::is_same< X, Y >::value >
197 registerEndomorphismSolvers ( pybind11::module module, pybind11::class_< LinearOperator< X, Y >, options... >, PriorityTag< 0 > )
198 {}
199
200 template< class X, class Y, class... options >
201 inline void registerEndomorphismSolvers ( pybind11::module module, pybind11::class_< LinearOperator< X, Y >, options... > cls )
202 {
203 registerEndomorphismSolvers( module, cls, PriorityTag< 42 >() );
204 }
205
206 } // namespace detail
207
208
209
210 // registerSolvers
211 // ---------------
212
213 template< class X, class Y, class... options >
214 inline void registerSolvers ( pybind11::module module, pybind11::class_< LinearOperator< X, Y >, options... > cls )
215 {
216 typedef Dune::InverseOperator< X, Y > Solver;
217
218 using pybind11::operator""_a;
219
220 pybind11::options opts;
221 opts.disable_function_signatures();
222
223 pybind11::class_< Solver > clsSolver( module, "InverseOperator" );
224 registerInverseOperator( clsSolver );
225
226 detail::registerEndomorphismSolvers( module, cls );
227
228 module.def( "RestartedGMResSolver", [] ( LinearOperator< X, Y > &op, Preconditioner< X, Y > &prec, double reduction, int restart, int maxit, int verbose ) {
229 return static_cast< Solver * >( new Dune::RestartedGMResSolver< X, Y >( op, prec, reduction, restart, maxit, verbose ) );
230 }, "operator"_a, "preconditioner"_a, "reduction"_a, "restart"_a, "maxIterations"_a = std::numeric_limits< int >::max(), "verbose"_a = 0, pybind11::keep_alive< 0, 1 >(), pybind11::keep_alive< 0, 2 >(),
231 R"doc(
232 Restarted generalized minimal residual iterative solver
233
234 Args:
235 operator: operator to invert
236 preconditioner: preconditioner to use (i.e., apprixmate inverse of the operator)
237 reduction: factor to reduce the defect by
238 restart: number of iterations before restart
239 maxIterations: maximum number of iterations to perform
240 verbose: verbosity level (0 = quiet, 1 = summary, 2 = verbose)
241
242 Returns:
243 ISTL Restarted generalized minimal residual solver
244
245 Note:
246 The restarted generalized minimal residual method holds restart many vectors in memory during application.
247 This can lead to a large memory consumption.
248 )doc" );
249 }
250
251 } // namespace Python
252
253 } // namespace Dune
254
255 #endif // #ifndef DUNE_PYTHON_ISTL_SOLVER_HH