Arcane  4.2.3.0
Developer documentation
Loading...
Searching...
No Matches
PreconditionedSolver.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* PreconditionedSolver.h (C) 2000-2026 */
9/* */
10/* Tie an iterative solver and a preconditioner in a single class. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_ALINA_PRECONDITIONEDSOLVER_H
13#define ARCCORE_ALINA_PRECONDITIONEDSOLVER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16/*
17 * This file is based on the work on AMGCL library (version march 2026)
18 * which can be found at https://github.com/ddemidov/amgcl.
19 *
20 * Copyright (c) 2012-2022 Denis Demidov <dennis.demidov@gmail.com>
21 * SPDX-License-Identifier: MIT
22 */
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26#include "arccore/alina/BuiltinBackend.h"
27#include "arccore/alina/AlinaUtils.h"
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32namespace Arcane::Alina
33{
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
40template <class Precond, class IterativeSolver>
43{
44 static_assert(backend::backends_compatible<
45 typename IterativeSolver::backend_type,
46 typename Precond::backend_type>::value,
47 "Backends for preconditioner and iterative solver should be compatible");
48
49 public:
50
51 typedef typename IterativeSolver::backend_type backend_type;
52 typedef typename backend_type::matrix matrix;
53
54 typedef typename backend_type::value_type value_type;
55 typedef typename backend_type::col_type col_type;
56 typedef typename backend_type::ptr_type ptr_type;
57 typedef typename backend_type::params backend_params;
58 typedef typename BuiltinBackend<value_type, col_type, ptr_type>::matrix build_matrix;
59
60 typedef typename math::scalar_of<value_type>::type scalar_type;
61
65 struct params
66 {
67 typename Precond::params precond;
68 typename IterativeSolver::params solver;
69
70 params() {}
71
72 params(const PropertyTree& p)
73 : ARCCORE_ALINA_PARAMS_IMPORT_CHILD(p, precond)
74 , ARCCORE_ALINA_PARAMS_IMPORT_CHILD(p, solver)
75 {
76 p.check_params( { "precond", "solver" });
77 }
78
79 void get(PropertyTree& p, const std::string& path = "") const
80 {
81 ARCCORE_ALINA_PARAMS_EXPORT_CHILD(p, path, precond);
82 ARCCORE_ALINA_PARAMS_EXPORT_CHILD(p, path, solver);
83 }
84 } prm;
85
89 template <class Matrix>
91 const Matrix& A,
92 const params& prm = params(),
93 const backend_params& bprm = backend_params())
94 : prm(prm)
95 , n(backend::nbRow(A))
96 , P(context, A, prm.precond, bprm)
97 , S(context, backend::nbRow(A), prm.solver, bprm)
98 {}
99
100 // Constructs the preconditioner and creates iterative solver.
101 // Takes shared pointer to the matrix in internal format.
102 PreconditionedSolver(const AlinaContext& context,
103 std::shared_ptr<build_matrix> A,
104 const params& prm = params(),
105 const backend_params& bprm = backend_params())
106 : prm(prm)
107 , n(backend::nbRow(*A))
108 , P(context, A, prm.precond, bprm)
109 , S(context, backend::nbRow(*A), prm.solver, bprm)
110 {}
111
126 template <class Matrix, class Vec1, class Vec2>
127 SolverResult operator()(const Matrix& A, const Vec1& rhs, Vec2&& x) const
128 {
129 return S(A, P, rhs, x);
130 }
131
137 template <class Vec1, class Vec2>
138 SolverResult operator()(const Vec1& rhs, Vec2&& x) const
139 {
140 return S(P, rhs, x);
141 }
142
168 template <class Vec1, class Vec2>
169 void apply(const Vec1& rhs, Vec2&& x) const
170 {
171 backend::clear(x);
172 (*this)(rhs, x);
173 }
174
176 const Precond& precond() const
177 {
178 return P;
179 }
180
182 Precond& precond()
183 {
184 return P;
185 }
186
188 const IterativeSolver& solver() const
189 {
190 return S;
191 }
192
194 IterativeSolver& solver()
195 {
196 return S;
197 }
198
200 std::shared_ptr<typename Precond::matrix> system_matrix_ptr() const
201 {
202 return P.system_matrix_ptr();
203 }
204
205 typename Precond::matrix const& system_matrix() const
206 {
207 return P.system_matrix();
208 }
209
212 {
213 prm.get(p);
214 }
215
217 size_t size() const
218 {
219 return n;
220 }
221
222 size_t bytes() const
223 {
224 return backend::bytes(S) + backend::bytes(P);
225 }
226
227 friend std::ostream& operator<<(std::ostream& os, const PreconditionedSolver& p)
228 {
229 return os << "Solver\n======\n"
230 << p.S << std::endl
231 << "Preconditioner\n==============\n"
232 << p.P;
233 }
234
235 private:
236
237 size_t n;
238 Precond P;
239 IterativeSolver S;
240};
241
242/*---------------------------------------------------------------------------*/
243/*---------------------------------------------------------------------------*/
244
245} // namespace Arcane::Alina
246
247/*---------------------------------------------------------------------------*/
248/*---------------------------------------------------------------------------*/
249
250#endif
251
252/*---------------------------------------------------------------------------*/
253/*---------------------------------------------------------------------------*/
IterativeSolver & solver()
Returns reference to the constructed iterative solver.
PreconditionedSolver(const AlinaContext &context, const Matrix &A, const params &prm=params(), const backend_params &bprm=backend_params())
Sets up the preconditioner and creates the iterative solver.
SolverResult operator()(const Vec1 &rhs, Vec2 &&x) const
const Precond & precond() const
Returns reference to the constructed preconditioner.
std::shared_ptr< typename Precond::matrix > system_matrix_ptr() const
Returns the system matrix in the backend format.
Precond & precond()
Returns reference to the constructed preconditioner.
void get_params(Alina::PropertyTree &p) const
Stores the parameters used during construction into the property tree p.
size_t size() const
Returns the size of the system matrix.
const IterativeSolver & solver() const
Returns reference to the constructed iterative solver.
void apply(const Vec1 &rhs, Vec2 &&x) const
SolverResult operator()(const Matrix &A, const Vec1 &rhs, Vec2 &&x) const
Class to store parameters as a hierarchical key/value tree.
Definition AlinaUtils.h:113
Result of a solution.
Definition AlinaUtils.h:56
Matrix class, to be used by user.
Combined parameters of the bundled preconditioner and the iterative solver.
Precond::params precond
Preconditioner parameters.
IterativeSolver::params solver
Iterative solver parameters.
Metafunction that checks if two backends are compatible.