Caribou
ConjugateGradientSolver.h
1 #pragma once
2 
3 #include <Caribou/config.h>
4 #include <sofa/core/behavior/LinearSolver.h>
5 #include <sofa/core/behavior/MultiVec.h>
6 #include <SofaBaseLinearSolver/DefaultMultiMatrixAccessor.h>
7 #include <sofa/helper/OptionsGroup.h>
8 #include <Eigen/Core>
9 
10 #include <Eigen/IterativeLinearSolvers>
11 
12 namespace SofaCaribou::solver {
13 
14 using sofa::core::objectmodel::Data;
15 using namespace sofa::component::linearsolver;
16 using namespace sofa::core::behavior;
17 
34 
35 public:
37  using SparseMatrix = Eigen::SparseMatrix<FLOATING_POINT_TYPE, Eigen::ColMajor>;
38  using Vector = Eigen::Matrix<FLOATING_POINT_TYPE, Eigen::Dynamic, 1>;
39 
41  enum class PreconditioningMethod : unsigned int {
43  None = 0,
44 
46  Identity = 1,
47 
49  Diagonal = 2,
50 
51 #if EIGEN_VERSION_AT_LEAST(3,3,0)
52  IncompleteCholesky = 4,
54 #endif
55 
57  IncompleteLU = 5
58  };
59 
68  void resetSystem() final;
69 
82  void setSystemMBKMatrix(const sofa::core::MechanicalParams* mparams) final;
83 
89  void setSystemRHVector(sofa::core::MultiVecDerivId b_id) final;
90 
96  void setSystemLHVector(sofa::core::MultiVecDerivId x_id) final;
97 
102  void solveSystem() final;
103 
107  auto squared_residuals() const -> const std::vector<FLOATING_POINT_TYPE> & {
108  return p_squared_residuals;
109  }
110 
114  auto squared_initial_residual() const -> const FLOATING_POINT_TYPE & {
115  return p_squared_initial_residual;
116  }
117 
121  auto system_matrix () const -> const SparseMatrix & {
122  return p_A;
123  }
124 
129  void assemble (const sofa::core::MechanicalParams* mparams);
130 
131 protected:
134 
136 
147  void solve(sofa::core::behavior::MultiVecDeriv & b, sofa::core::behavior::MultiVecDeriv & x);
148 
160  template <typename Matrix, typename Preconditioner>
161  void solve(const Preconditioner & precond, const Matrix & A, const Vector & b, Vector & x);
162 
164  Data<bool> d_verbose;
165  Data<unsigned int> d_maximum_number_of_iterations;
166  Data<FLOATING_POINT_TYPE> d_residual_tolerance_threshold;
167  Data< sofa::helper::OptionsGroup > d_preconditioning_method;
168 
169 private:
171 
174  PreconditioningMethod get_preconditioning_method_from_string(const std::string & preconditioner_name) const;
175 
178  sofa::core::MechanicalParams p_mechanical_params;
179 
181  DefaultMultiMatrixAccessor p_accessor;
182 
184  sofa::core::MultiVecDerivId p_b_id;
185 
187  sofa::core::MultiVecDerivId p_x_id;
188 
190  SparseMatrix p_A;
191 
193  Vector p_x;
194 
196  Vector p_b;
197 
199  Eigen::IdentityPreconditioner p_identity;
200 
202  Eigen::DiagonalPreconditioner<FLOATING_POINT_TYPE> p_diag;
203 
204 #if EIGEN_VERSION_AT_LEAST(3,3,0)
205  Eigen::IncompleteCholesky<FLOATING_POINT_TYPE> p_ichol;
207 #endif
208 
210  Eigen::IncompleteLUT<FLOATING_POINT_TYPE> p_iLU;
211 
213  std::vector<std::pair<std::string, PreconditioningMethod>> p_preconditioners;
214 
216  std::vector<FLOATING_POINT_TYPE> p_squared_residuals;
217 
219  FLOATING_POINT_TYPE p_squared_initial_residual;
220 };
221 
222 } // namespace SofaCaribou::solver
223 
SofaCaribou::solver::ConjugateGradientSolver
Implements a Conjugate Gradient method for solving a linear system of the type Ax = b.
Definition: ConjugateGradientSolver.h:33
SofaCaribou::solver::ConjugateGradientSolver::system_matrix
auto system_matrix() const -> const SparseMatrix &
Get the system matrix.
Definition: ConjugateGradientSolver.h:121
SofaCaribou::solver::ConjugateGradientSolver::d_verbose
Data< bool > d_verbose
INPUTS.
Definition: ConjugateGradientSolver.h:164
SofaCaribou::solver::ConjugateGradientSolver::squared_initial_residual
auto squared_initial_residual() const -> const FLOATING_POINT_TYPE &
Squared residual norm (||r||^2) of the last right-hand side term (b in Ax=b) of the last solve call.
Definition: ConjugateGradientSolver.h:114
SofaCaribou::solver::ConjugateGradientSolver::PreconditioningMethod
PreconditioningMethod
Preconditioning methods.
Definition: ConjugateGradientSolver.h:41
SofaCaribou::solver::LinearSolver
Base interface for linear solvers.
Definition: LinearSolver.h:14