suanPan
Loading...
Searching...
No Matches
fgmres.hpp
Go to the documentation of this file.
1
2/*******************************************************************************
3 * Copyright (C) 2017-2025 Theodore Chang
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 ******************************************************************************/
18
19#ifndef FGMRES_HPP
20#define FGMRES_HPP
21
22#include <mkl_rci.h>
23#include <suanPan.h>
24
25template<typename T> requires requires(const T& a, const mat& b) { { a * b } -> std::same_as<mat>; a.n_rows; } int fgmres_solve(const T& kernel, const vec& diagonal, double* left, double* right, const double tolerance) {
26 const auto N = static_cast<MKL_INT>(kernel.n_rows);
27 // ReSharper disable once CppRedundantCastExpression
28 const auto R = std::min(static_cast<MKL_INT>(150), N);
29
30 std::vector work((2 * R + 1) * N + R * (R + 9) / 2 + 1, 0.);
31
32 MKL_INT ipar[128]{};
33 double dpar[128]{};
34
35 MKL_INT info{};
36
37 dfgmres_init(&N, nullptr, nullptr, &info, ipar, dpar, work.data());
38
39 ipar[8] = 1; // residual stopping test
40 ipar[9] = 0; // no user-defined stopping test
41 ipar[10] = 1; // use preconditioner
42 ipar[11] = 1; // automatic test
43 dpar[0] = tolerance;
44
45 while(true) {
46 dfgmres(&N, left, right, &info, ipar, dpar, work.data());
47 if(-1 == info || -10 == info || -11 == info || -12 == info) {
48 suanpan_error("Error code {} received.\n", info);
49 return -1;
50 }
51 if(0 == info || 4 == info && dpar[6] <= dpar[0]) {
52 MKL_INT counter;
53 dfgmres_get(&N, left, right, &info, ipar, dpar, work.data(), &counter);
54 suanpan_debug("Converged in {} iterations.\n", counter);
55 return 0;
56 }
57 const vec xn(&work[ipar[21] - 1], N);
58 // ReSharper disable once CppInitializedValueIsAlwaysRewritten
59 // ReSharper disable once CppEntityAssignedButNoRead
60 vec yn(&work[ipar[22] - 1], N, false, true);
61 // ReSharper disable once CppDFAUnusedValue
62 if(1 == info) yn = kernel * xn;
63 // ReSharper disable once CppDFAUnusedValue
64 else if(3 == info) yn = xn / diagonal;
65 }
66}
67
68#endif
int fgmres_solve(const T &kernel, const vec &diagonal, double *left, double *right, const double tolerance)
Definition fgmres.hpp:25
void info(const std::string_view format_sv, const T &... args)
Definition suanPan.h:314
#define suanpan_debug(...)
Definition suanPan.h:374
#define suanpan_error(...)
Definition suanPan.h:376