3 #include <Caribou/config.h>
4 #include <SofaCaribou/Algebra/EigenMatrix.h>
5 #include <SofaCaribou/Algebra/EigenVector.h>
6 #include <SofaCaribou/Solver/LinearSolver.h>
8 #include <sofa/core/behavior/LinearSolver.h>
9 #include <SofaBaseLinearSolver/DefaultMultiMatrixAccessor.h>
10 #include <Eigen/Sparse>
12 namespace SofaCaribou::solver {
16 struct solver_traits {};
28 template <
class EigenSolver_t>
31 SOFA_CLASS(SOFA_TEMPLATE(
EigenSparseSolver, EigenSolver_t), sofa::core::behavior::LinearSolver);
32 using EigenSolver = EigenSolver_t;
33 using SparseMatrix = std::remove_cv_t<typename EigenSolver::MatrixType>;
34 using Vector = Eigen::Matrix<FLOATING_POINT_TYPE, Eigen::Dynamic, 1>;
40 virtual void assemble (
const sofa::core::MechanicalParams* mparams);
81 inline virtual auto symmetric() const ->
bool {
return p_is_symmetric;}
84 inline virtual void set_symmetric(
bool is_symmetric) { p_is_symmetric = is_symmetric; }
87 auto solver() const -> const EigenSolver & {
return p_solver; }
90 auto solver() -> EigenSolver & {
return p_solver; }
93 auto mechanical_params() const -> const sofa::core::MechanicalParams & {
return p_mechanical_params; }
103 auto matrix_accessor() const -> const sofa::component::linearsolver::DefaultMultiMatrixAccessor & {
return p_accessor; }
106 auto A() const -> const SparseMatrix & {
return p_A; }
109 auto b_id() const -> const sofa::core::MultiVecDerivId & {
return p_b_id; }
112 auto x_id() const -> const sofa::core::MultiVecDerivId & {
return p_x_id; }
118 static auto GetCustomTemplateName() -> std::string;
120 template<
class Derived>
121 static auto canCreate(Derived* o, sofa::core::objectmodel::BaseContext* context, sofa::core::objectmodel::BaseObjectDescription* arg) -> bool;
127 sofa::defaulttype::BaseMatrix * create_new_matrix(sofa::Size rows, sofa::Size cols)
const override {
138 sofa::defaulttype::BaseVector * create_new_vector(sofa::Size n)
const override {
145 bool solve(
const sofa::defaulttype::BaseMatrix *
A,
146 const sofa::defaulttype::BaseVector * F,
147 sofa::defaulttype::BaseVector * X)
const override;
153 EigenSolver p_solver;
156 sofa::core::MechanicalParams p_mechanical_params;
159 sofa::component::linearsolver::DefaultMultiMatrixAccessor p_accessor;
162 sofa::core::MultiVecDerivId p_b_id;
165 sofa::core::MultiVecDerivId p_x_id;
177 bool p_A_is_factorized;
181 bool p_is_symmetric =
false;
void solveSystem() override
Solves the system using the Eigen solver.
Definition: EigenSparseSolver.inl:189
void setSystemMBKMatrix(const sofa::core::MechanicalParams *mparams) final
Set the linear system matrix A = (mM + bB + kK), storing the coefficients m, b and k of the mechanica...
Definition: EigenSparseSolver.inl:106
void resetSystem() final
Reset the complete system (A, x and b are cleared).
Definition: EigenSparseSolver.inl:20
This class can be use to represent any Eigen vector within SOFA.
Definition: EigenVector.h:23
virtual void set_symmetric(bool is_symmetric)
Explicitly states if this matrix is symmetric.
Definition: EigenSparseSolver.h:84
auto x_id() const -> const sofa::core::MultiVecDerivId &
Get a readonly reference to the left-hand side unknown vector identifier.
Definition: EigenSparseSolver.h:112
auto matrix_accessor() const -> const sofa::component::linearsolver::DefaultMultiMatrixAccessor &
Get a readonly reference to the multi-matrix accessor.
Definition: EigenSparseSolver.h:103
virtual void assemble(const sofa::core::MechanicalParams *mparams)
Assemble the system matrix A = (mM + bB + kK) inside the SparseMatrix p_A.
Definition: EigenSparseSolver.inl:28
auto solver() const -> const EigenSolver &
Get a readonly reference to the backend solver.
Definition: EigenSparseSolver.h:87
void setSystemLHVector(sofa::core::MultiVecDerivId x_id) final
Gives the identifier of the left-hand side vector x.
Definition: EigenSparseSolver.inl:169
virtual auto symmetric() const -> bool
States if the system matrix is symmetric.
Definition: EigenSparseSolver.h:81
auto A_is_factorized() const -> bool
True if the solver has successfully factorize the system matrix.
Definition: EigenSparseSolver.h:115
Base interface for linear solvers.
Definition: LinearSolver.h:14
auto b_id() const -> const sofa::core::MultiVecDerivId &
Get a readonly reference to the right-hand side vector identifier.
Definition: EigenSparseSolver.h:109
auto solver() -> EigenSolver &
Get a reference to the backend solver.
Definition: EigenSparseSolver.h:90
auto A() const -> const SparseMatrix &
Get a readonly reference to the global assembled system matrix.
Definition: EigenSparseSolver.h:106
This class can be use to represent any Eigen matrix within SOFA.
Definition: EigenMatrix.h:51
void setSystemRHVector(sofa::core::MultiVecDerivId b_id) final
Gives the identifier of the right-hand side vector b.
Definition: EigenSparseSolver.inl:150
auto mechanical_params() const -> const sofa::core::MechanicalParams &
Get a readonly reference to the mechanical parameters.
Definition: EigenSparseSolver.h:93
Base class for sparse direct solvers using Eigen as a backend solver.
Definition: EigenSparseSolver.h:29