Caribou
NewtonRaphsonSolver.h
1 #pragma once
2 
3 #include <Caribou/config.h>
4 #include <sofa/core/behavior/OdeSolver.h>
5 #include <sofa/core/behavior/LinearSolver.h>
6 #include <sofa/core/objectmodel/Data.h>
7 #include <sofa/defaulttype/BaseMatrix.h>
8 #include <sofa/defaulttype/BaseVector.h>
9 #include <sofa/core/objectmodel/Link.h>
10 #include <SofaBaseLinearSolver/DefaultMultiMatrixAccessor.h>
11 #include <memory>
12 
13 namespace SofaCaribou::ode {
14 
40 class NewtonRaphsonSolver : public sofa::core::behavior::OdeSolver {
41 public:
42  SOFA_CLASS(NewtonRaphsonSolver, sofa::core::behavior::OdeSolver);
43 
44  template <typename T>
45  using Data = sofa::core::objectmodel::Data<T>;
46 
47  template <typename T>
48  using Link = sofa::core::objectmodel::SingleLink<NewtonRaphsonSolver, T, sofa::core::objectmodel::BaseLink::FLAG_STRONGLINK>;
49 
51 
52  void init() override;
53 
54  void solve (const sofa::core::ExecParams* params, SReal dt, sofa::core::MultiVecCoordId x_id, sofa::core::MultiVecDerivId v_id) override;
55 
57  auto iteration_times() const -> const std::vector<UNSIGNED_INTEGER_TYPE> & { return p_times; }
58 
60  auto squared_residuals() const -> const std::vector<FLOATING_POINT_TYPE> & { return p_squared_residuals; }
61 
63  auto squared_initial_residual() const -> const FLOATING_POINT_TYPE & { return p_squared_initial_residual; }
64 
65 private:
66 
84  virtual void assemble_rhs_vector(const sofa::core::MechanicalParams & mechanical_parameters,
85  const sofa::core::behavior::MultiMatrixAccessor & matrix_accessor,
86  sofa::core::MultiVecDerivId & f_id,
87  sofa::defaulttype::BaseVector * f) = 0;
88 
108  virtual void assemble_system_matrix(const sofa::core::MechanicalParams & mechanical_parameters,
109  sofa::component::linearsolver::DefaultMultiMatrixAccessor & matrix_accessor,
110  sofa::defaulttype::BaseMatrix * A) = 0;
111 
131  virtual void propagate_position_increment(const sofa::core::MechanicalParams & mechanical_parameters,
132  const sofa::core::behavior::MultiMatrixAccessor & matrix_accessor,
133  const sofa::defaulttype::BaseVector * dx,
134  sofa::core::MultiVecCoordId & x_id,
135  sofa::core::MultiVecDerivId & v_id,
136  sofa::core::MultiVecDerivId & dx_id) = 0;
137 
139  bool has_valid_linear_solver () const;
140 
142  Data<unsigned> d_newton_iterations;
143  Data<double> d_correction_tolerance_threshold;
144  Data<double> d_residual_tolerance_threshold;
145 
146  Link<sofa::core::behavior::LinearSolver> l_linear_solver;
147 
150  Data<bool> d_converged;
151 
153 
155  std::unique_ptr<sofa::defaulttype::BaseMatrix> p_A;
156 
158  std::unique_ptr<sofa::defaulttype::BaseVector> p_DX;
159 
161  std::unique_ptr<sofa::defaulttype::BaseVector> p_F;
162 
164  sofa::core::MultiVecDerivId p_U_id;
165 
167  std::vector<UNSIGNED_INTEGER_TYPE> p_times;
168 
170  std::vector<FLOATING_POINT_TYPE> p_squared_residuals;
171 
173  FLOATING_POINT_TYPE p_squared_initial_residual;
174 };
175 }
SofaCaribou::ode::NewtonRaphsonSolver::squared_residuals
auto squared_residuals() const -> const std::vector< FLOATING_POINT_TYPE > &
The list of squared residual norms (||r||^2) of every newton iterations of the last solve call.
Definition: NewtonRaphsonSolver.h:60
SofaCaribou::ode::NewtonRaphsonSolver::squared_initial_residual
auto squared_initial_residual() const -> const FLOATING_POINT_TYPE &
The initial squared residual (||r0||^2) of the last solve call.
Definition: NewtonRaphsonSolver.h:63
SofaCaribou::ode::NewtonRaphsonSolver
This class implements a generic Newton-Raphson solver for SOFA.
Definition: NewtonRaphsonSolver.h:40
SofaCaribou::ode::NewtonRaphsonSolver::iteration_times
auto iteration_times() const -> const std::vector< UNSIGNED_INTEGER_TYPE > &
List of times (in nanoseconds) that each Newton-Raphson iteration took to compute in the last call to...
Definition: NewtonRaphsonSolver.h:57