Arcane  v4.1.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
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 params& prm = params(),
92 const backend_params& bprm = backend_params())
93 : prm(prm)
94 , n(backend::nbRow(A))
95 , P(A, prm.precond, bprm)
96 , S(backend::nbRow(A), prm.solver, bprm)
97 {}
98
99 // Constructs the preconditioner and creates iterative solver.
100 // Takes shared pointer to the matrix in internal format.
101 PreconditionedSolver(std::shared_ptr<build_matrix> A,
102 const params& prm = params(),
103 const backend_params& bprm = backend_params())
104 : prm(prm)
105 , n(backend::nbRow(*A))
106 , P(A, prm.precond, bprm)
107 , S(backend::nbRow(*A), prm.solver, bprm)
108 {}
109
124 template <class Matrix, class Vec1, class Vec2>
125 SolverResult operator()(const Matrix& A, const Vec1& rhs, Vec2&& x) const
126 {
127 return S(A, P, rhs, x);
128 }
129
135 template <class Vec1, class Vec2>
136 SolverResult operator()(const Vec1& rhs, Vec2&& x) const
137 {
138 return S(P, rhs, x);
139 }
140
166 template <class Vec1, class Vec2>
167 void apply(const Vec1& rhs, Vec2&& x) const
168 {
169 backend::clear(x);
170 (*this)(rhs, x);
171 }
172
174 const Precond& precond() const
175 {
176 return P;
177 }
178
180 Precond& precond()
181 {
182 return P;
183 }
184
186 const IterativeSolver& solver() const
187 {
188 return S;
189 }
190
192 IterativeSolver& solver()
193 {
194 return S;
195 }
196
198 std::shared_ptr<typename Precond::matrix> system_matrix_ptr() const
199 {
200 return P.system_matrix_ptr();
201 }
202
203 typename Precond::matrix const& system_matrix() const
204 {
205 return P.system_matrix();
206 }
207
210 {
211 prm.get(p);
212 }
213
215 size_t size() const
216 {
217 return n;
218 }
219
220 size_t bytes() const
221 {
222 return backend::bytes(S) + backend::bytes(P);
223 }
224
225 friend std::ostream& operator<<(std::ostream& os, const PreconditionedSolver& p)
226 {
227 return os << "Solver\n======\n"
228 << p.S << std::endl
229 << "Preconditioner\n==============\n"
230 << p.P;
231 }
232
233 private:
234
235 size_t n;
236 Precond P;
237 IterativeSolver S;
238};
239
240/*---------------------------------------------------------------------------*/
241/*---------------------------------------------------------------------------*/
242
243} // namespace Arcane::Alina
244
245/*---------------------------------------------------------------------------*/
246/*---------------------------------------------------------------------------*/
247
248#endif
249
250/*---------------------------------------------------------------------------*/
251/*---------------------------------------------------------------------------*/
PreconditionedSolver(const Matrix &A, const params &prm=params(), const backend_params &bprm=backend_params())
Sets up the preconditioner and creates the iterative solver.
IterativeSolver & solver()
Returns reference to the constructed 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
Result of a solving.
Definition AlinaUtils.h:52
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.