Alien  1.3.0
User documentation
Loading...
Searching...
No Matches
CxrPreconditioner.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#pragma once
8
9namespace Alien
10{
11 template<typename AlgebraT,
12 typename MatrixT,
13 typename VectorT,
14 typename CxrSolverT,
15 typename CxrOpT,
16 typename RelaxSolverT>
17
18 class CxrPreconditioner
19 {
20 public:
21 using AlgebraType = AlgebraT;
22 using MatrixType = MatrixT;
23 using VectorType = VectorT;
24 using RelaxSolverType = RelaxSolverT;
25 using CxrSolverType = CxrSolverT;
26 using CxrOpType = CxrOpT;
27
28
29 CxrPreconditioner(AlgebraType& alg,
30 MatrixType const& matrix,
31 CxrOpType* cxr_op,
32 CxrSolverType* cxr_solver,
33 RelaxSolverType* relax_solver,
34 ITraceMng* trace_mng = nullptr
35 )
36 : m_algebra(alg)
37 , m_matrix(matrix)
38 , m_relax_solver(relax_solver)
39 , m_cxr_solver(cxr_solver)
40 , m_cxr_op(cxr_op)
41 , m_trace_mng(trace_mng)
42 {
43 }
44
45 virtual ~CxrPreconditioner()
46 {
47 end() ;
48 }
49
50 void init()
51 {
52 m_cxr_op->computeCxrMatrix(m_algebra) ;
53 auto& cxr_matrix = m_cxr_op->getCxrMatrix() ;
54 if(m_relax_solver)
55 m_relax_solver->init() ;
56 if(m_cxr_solver)
57 {
58 m_cxr_solver->init() ;
59 m_cxr_solver->init(cxr_matrix) ;
60 m_cxr_solver->start() ;
61 }
62 m_algebra.allocate(AlgebraType::resource(cxr_matrix),m_x_Cxr,m_y_Cxr,m_r_Cxr);
63 }
64
65 void init(VectorType const& diag_scale)
66 {
67 m_cxr_op->computeCxrMatrix(m_algebra, diag_scale) ;
68 auto& cxr_matrix = m_cxr_op->getCxrMatrix() ;
69 if(m_relax_solver)
70 m_relax_solver->init() ;
71 if(m_cxr_solver)
72 {
73 m_cxr_solver->init() ;
74 m_cxr_solver->init(cxr_matrix) ;
75 m_cxr_solver->start() ;
76 }
77 m_algebra.allocate(AlgebraType::resource(cxr_matrix),m_x_Cxr,m_y_Cxr,m_r_Cxr);
78 }
79
80 void end()
81 {
82 if(m_cxr_solver)
83 m_cxr_solver->end() ;
84 }
85
86 void solve(AlgebraType& alg,
87 VectorType const& y,
88 VectorType& x) const
89 {
90 // x input, y output
91 /*
92 | A11 A12 | | X1 | |Y1|
93 A= | A12 A22 | X=| X2 | Y= |Y2|
94 */
95
96 // solve A.Y=X
97 if(m_relax_solver)
98 {
99 m_relax_solver->solve(alg,y,x);
100
101 // X_Cpr = A11.Y1 + A12.Y2
102 m_cxr_op->apply(alg,x,m_x_Cxr);
103
104 m_cxr_op->get(alg,y,m_r_Cxr);
105
106 // R_Cpr = X1-X_Cpr
107 //m_kernel->xmy(m_r_Cxr,m_x_Cxr);
108 alg.scal(-1.,m_x_Cxr) ;
109 alg.axpy(1.,m_x_Cxr,m_r_Cxr) ;
110
111 m_cxr_op->scal(alg,m_r_Cxr) ;
112 }
113 else
114 {
115 alg.copy(y,x) ;
116 m_cxr_op->get(alg,x,m_r_Cxr);
117 m_cxr_op->scal(alg,m_r_Cxr) ;
118 }
119
120
121 if(m_cxr_solver)
122 {
123 // Solve A11.Y_Cpr = R_Cpr
124
125 alg.copy(m_r_Cxr,m_x_Cxr) ;
126 m_cxr_solver->solve(m_x_Cxr,m_y_Cxr);
127
128 // combine m_y_Cpr with y
129 if(m_relax_solver)
130 m_cxr_op->combine(alg,m_y_Cxr,x);
131 else
132 m_cxr_op->copy(alg,m_y_Cxr,x);
133 }
134 }
135
136 private :
137 bool m_use_diag_scale = false ;
138 AlgebraType& m_algebra ;
139 MatrixType const& m_matrix ;
140
141 mutable VectorType m_x_Cxr;
142
143 mutable VectorType m_y;
144 mutable VectorType m_y_Cxr;
145
146 mutable VectorType m_r;
147 mutable VectorType m_r_Cxr;
148
149 RelaxSolverType* m_relax_solver = nullptr;
150 CxrSolverType* m_cxr_solver = nullptr;
151 CxrOpType* m_cxr_op = nullptr;
152
153 ITraceMng* m_trace_mng = nullptr ;
154 };
155
156}
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17