Debian Codegraph @

include/deal.II/lac/solver_minres.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2000 - 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_solver_minres_h
16 #define dealii_solver_minres_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/enable_observer_pointer.h>
22 #include <deal.II/base/logstream.h>
23 #include <deal.II/base/signaling_nan.h>
24 #include <deal.II/base/template_constraints.h>
25
26 #include <deal.II/lac/solver.h>
27 #include <deal.II/lac/solver_control.h>
28
29 #include <cmath>
30
31 DEAL_II_NAMESPACE_OPEN
32
33 /**
34 * @addtogroup Solvers
35 * @{
36 */
37
38 /**
39 * Minimal residual method for symmetric matrices.
40 *
41 * For the requirements on matrices and vectors in order to work with this
42 * class, see the documentation of the Solver base class.
43 *
44 * Like all other solver classes, this class has a local structure called @p
45 * AdditionalData which is used to pass additional parameters to the solver,
46 * like damping parameters or the number of temporary vectors. We use this
47 * additional structure instead of passing these values directly to the
48 * constructor because this makes the use of the @p SolverSelector and other
49 * classes much easier and guarantees that these will continue to work even if
50 * number or type of the additional parameters for a certain solver changes.
51 *
52 * However, since the MinRes method does not need additional data, the
53 * respective structure is empty and does not offer any functionality. The
54 * constructor has a default argument, so you may call it without the
55 * additional parameter.
56 *
57 * The preconditioner has to be positive definite and symmetric
58 *
59 * The algorithm is taken from the Master thesis of Astrid Battermann
60 * @cite Battermann1996 with some changes.
61 *
62 *
63 * <h3>Observing the progress of linear solver iterations</h3>
64 *
65 * The solve() function of this class uses the mechanism described in the
66 * Solver base class to determine convergence. This mechanism can also be used
67 * to observe the progress of the iteration.
68 */
69 template <typename VectorType = Vector<double>>
70 DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>)
71 class SolverMinRes : public SolverBase<VectorType>
72 {
73 public:
74 /**
75 * Standardized data struct to pipe additional data to the solver. This
76 * solver does not need additional data yet.
77 */
78 struct AdditionalData
79 {};
80
81 /**
82 * Constructor.
83 */
84 SolverMinRes(SolverControl &cn,
85 VectorMemory<VectorType> &mem,
86 const AdditionalData &data = AdditionalData());
87
88 /**
89 * Constructor. Use an object of type GrowingVectorMemory as a default to
90 * allocate memory.
91 */
92 SolverMinRes(SolverControl &cn,
93 const AdditionalData &data = AdditionalData());
94
95 /**
96 * Virtual destructor.
97 */
98 virtual ~SolverMinRes() override = default;
99
100 /**
101 * Solve the linear system $Ax=b$ for x.
102 */
103 template <typename MatrixType, typename PreconditionerType>
104 DEAL_II_CXX20_REQUIRES(
105 (concepts::is_linear_operator_on<MatrixType, VectorType> &&
106 concepts::is_linear_operator_on<PreconditionerType, VectorType>))
107 void solve(const MatrixType &A,
108 VectorType &x,
109 const VectorType &b,
110 const PreconditionerType &preconditioner);
111
112 /**
113 * @addtogroup Exceptions
114 * @{
115 */
116
117 /**
118 * Exception
119 */
120 DeclExceptionMsg(ExcPreconditionerNotDefinite,
121 "The preconditioner for MinRes must be a symmetric and "
122 "definite operator, even though MinRes can solve linear "
123 "systems with symmetric and *indefinite* operators. "
124 "During iterations, MinRes has detected that the "
125 "preconditioner is apparently not definite.");
126 /** @} */
127
128 protected:
129 /**
130 * Implementation of the computation of the norm of the residual.
131 */
132 virtual double
133 criterion();
134
135 /**
136 * Interface for derived class. This function gets the current iteration
137 * vector, the residual and the update vector in each step. It can be used
138 * for graphical output of the convergence history.
139 */
140 virtual void
141 print_vectors(const unsigned int step,
142 const VectorType &x,
143 const VectorType &r,
144 const VectorType &d) const;
145
146 /**
147 * Within the iteration loop, the square of the residual vector is stored in
148 * this variable. The function @p criterion uses this variable to compute
149 * the convergence value, which in this class is the norm of the residual
150 * vector and thus the square root of the @p res2 value.
151 */
152 double res2;
153 };
154
155 /** @} */
156 /*------------------------- Implementation ----------------------------*/
157
158 #ifndef DOXYGEN
159
160 template <typename VectorType>
161 DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>)
162 SolverMinRes<VectorType>::SolverMinRes(SolverControl &cn,
163 VectorMemory<VectorType> &mem,
164 const AdditionalData &)
165 : SolverBase<VectorType>(cn, mem)
166 , res2(numbers::signaling_nan<double>())
167 {}
168
169
170
171 template <typename VectorType>
172 DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>)
173 SolverMinRes<VectorType>::SolverMinRes(SolverControl &cn,
174 const AdditionalData &)
175 : SolverBase<VectorType>(cn)
176 , res2(numbers::signaling_nan<double>())
177 {}
178
179
180
181 template <typename VectorType>
182 DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>)
183 double SolverMinRes<VectorType>::criterion()
184 {
185 return res2;
186 }
187
188
189 template <typename VectorType>
190 DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>)
191 void SolverMinRes<VectorType>::print_vectors(const unsigned int,
192 const VectorType &,
193 const VectorType &,
194 const VectorType &) const
195 {}
196
197
198
199 template <typename VectorType>
200 DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>)
201 template <typename MatrixType, typename PreconditionerType>
202 DEAL_II_CXX20_REQUIRES(
203 (concepts::is_linear_operator_on<MatrixType, VectorType> &&
204 concepts::is_linear_operator_on<PreconditionerType, VectorType>))
205 void SolverMinRes<VectorType>::solve(const MatrixType &A,
206 VectorType &x,
207 const VectorType &b,
208 const PreconditionerType &preconditioner)
209 {
210 LogStream::Prefix prefix("minres");
211
212 // Memory allocation
213 typename VectorMemory<VectorType>::Pointer Vu0(this->memory);
214 typename VectorMemory<VectorType>::Pointer Vu1(this->memory);
215 typename VectorMemory<VectorType>::Pointer Vu2(this->memory);
216
217 typename VectorMemory<VectorType>::Pointer Vm0(this->memory);
218 typename VectorMemory<VectorType>::Pointer Vm1(this->memory);
219 typename VectorMemory<VectorType>::Pointer Vm2(this->memory);
220
221 typename VectorMemory<VectorType>::Pointer Vv(this->memory);
222
223 // define some aliases for simpler access
224 using vecptr = VectorType *;
225 vecptr u[3] = {Vu0.get(), Vu1.get(), Vu2.get()};
226 vecptr m[3] = {Vm0.get(), Vm1.get(), Vm2.get()};
227 VectorType &v = *Vv;
228
229 // resize the vectors, but do not set the values since they'd be overwritten
230 // soon anyway.
231 u[0]->reinit(b, true);
232 u[1]->reinit(b, true);
233 u[2]->reinit(b, true);
234 m[0]->reinit(b, true);
235 m[1]->reinit(b, true);
236 m[2]->reinit(b, true);
237 v.reinit(b, true);
238
239 // some values needed
240 double delta[3] = {0, 0, 0};
241 double f[2] = {0, 0};
242 double e[2] = {0, 0};
243
244 double r_l2 = 0;
245 double r0 = 0;
246 double tau = 0;
247 double c = 0;
248 double s = 0;
249 double d_ = 0;
250
251 // The iteration step.
252 unsigned int j = 1;
253
254
255 // Start of the solution process
256 A.vmult(*m[0], x);
257 *u[1] = b;
258 *u[1] -= *m[0];
259 // Precondition is applied.
260 // The preconditioner has to be
261 // positive definite and symmetric
262
263 // M v = u[1]
264 preconditioner.vmult(v, *u[1]);
265
266 delta[1] = v * (*u[1]);
267 // Preconditioner positive
268 Assert(delta[1] >= 0, ExcPreconditionerNotDefinite());
269
270 r0 = std::sqrt(delta[1]);
271 r_l2 = r0;
272
273
274 u[0]->reinit(b);
275 delta[0] = 1.;
276 m[0]->reinit(b);
277 m[1]->reinit(b);
278 m[2]->reinit(b);
279
280 SolverControl::State conv = this->iteration_status(0, r_l2, x);
281 while (conv == SolverControl::iterate)
282 {
283 if (delta[1] != 0)
284 v *= 1. / std::sqrt(delta[1]);
285 else
286 v.reinit(b);
287
288 A.vmult(*u[2], v);
289 u[2]->add(-std::sqrt(delta[1] / delta[0]), *u[0]);
290
291 const double gamma = *u[2] * v;
292 u[2]->add(-gamma / std::sqrt(delta[1]), *u[1]);
293 *m[0] = v;
294
295 // precondition: solve M v = u[2]
296 // Preconditioner has to be positive
297 // definite and symmetric.
298 preconditioner.vmult(v, *u[2]);
299
300 delta[2] = v * (*u[2]);
301
302 Assert(delta[2] >= 0, ExcPreconditionerNotDefinite());
303
304 if (j == 1)
305 {
306 d_ = gamma;
307 e[1] = std::sqrt(delta[2]);
308 }
309 if (j > 1)
310 {
311 d_ = s * e[0] - c * gamma;
312 e[0] = c * e[0] + s * gamma;
313 f[1] = s * std::sqrt(delta[2]);
314 e[1] = -c * std::sqrt(delta[2]);
315 }
316
317 const double d = std::sqrt(d_ * d_ + delta[2]);
318
319 if (j > 1)
320 tau *= s / c;
321 c = d_ / d;
322 tau *= c;
323
324 s = std::sqrt(delta[2]) / d;
325
326 if (j == 1)
327 tau = r0 * c;
328
329 m[0]->add(-e[0], *m[1]);
330 if (j > 1)
331 m[0]->add(-f[0], *m[2]);
332 *m[0] *= 1. / d;
333 x.add(tau, *m[0]);
334 r_l2 *= std::fabs(s);
335
336 conv = this->iteration_status(j, r_l2, x);
337
338 // next iteration step
339 ++j;
340 // All vectors have to be shifted
341 // one iteration step.
342 // This should be changed one time.
343 swap(*m[2], *m[1]);
344 swap(*m[1], *m[0]);
345
346 // likewise, but reverse direction:
347 // u[0] = u[1];
348 // u[1] = u[2];
349 swap(*u[0], *u[1]);
350 swap(*u[1], *u[2]);
351
352 // these are scalars, so need
353 // to bother
354 f[0] = f[1];
355 e[0] = e[1];
356 delta[0] = delta[1];
357 delta[1] = delta[2];
358 }
359
360 // in case of failure: throw exception
361 AssertThrow(conv == SolverControl::success,
362 SolverControl::NoConvergence(j, r_l2));
363
364 // otherwise exit as normal
365 }
366
367 #endif // DOXYGEN
368
369 DEAL_II_NAMESPACE_CLOSE
370
371 #endif