opensurgsim
OdeSolver.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_MATH_ODESOLVER_H
17 #define SURGSIM_MATH_ODESOLVER_H
18 
19 #include <functional>
20 #include <unordered_map>
21 
22 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
23 
24 #include "SurgSim/Math/LinearSparseSolveAndInverse.h"
25 #include "SurgSim/Math/Matrix.h"
26 #include "SurgSim/Math/OdeEquation.h"
27 
28 namespace SurgSim
29 {
30 
31 namespace Math
32 {
33 
36 enum IntegrationScheme
37 {
38  INTEGRATIONSCHEME_STATIC = 0,
39  INTEGRATIONSCHEME_LINEAR_STATIC,
40  INTEGRATIONSCHEME_EULER_EXPLICIT,
41  INTEGRATIONSCHEME_LINEAR_EULER_EXPLICIT,
42  INTEGRATIONSCHEME_EULER_EXPLICIT_MODIFIED,
43  INTEGRATIONSCHEME_LINEAR_EULER_EXPLICIT_MODIFIED,
44  INTEGRATIONSCHEME_EULER_IMPLICIT,
45  INTEGRATIONSCHEME_LINEAR_EULER_IMPLICIT,
46  INTEGRATIONSCHEME_RUNGE_KUTTA_4,
47  INTEGRATIONSCHEME_LINEAR_RUNGE_KUTTA_4,
48  MAX_INTEGRATIONSCHEMES
49 };
50 
51 const std::unordered_map<IntegrationScheme, std::string, std::hash<int>> IntegrationSchemeNames =
52  boost::assign::map_list_of
53  (INTEGRATIONSCHEME_STATIC, "INTEGRATIONSCHEME_STATIC")
54  (INTEGRATIONSCHEME_LINEAR_STATIC, "INTEGRATIONSCHEME_LINEAR_STATIC")
55  (INTEGRATIONSCHEME_EULER_EXPLICIT, "INTEGRATIONSCHEME_EULER_EXPLICIT")
56  (INTEGRATIONSCHEME_LINEAR_EULER_EXPLICIT, "INTEGRATIONSCHEME_LINEAR_EULER_EXPLICIT")
57  (INTEGRATIONSCHEME_EULER_EXPLICIT_MODIFIED, "INTEGRATIONSCHEME_EULER_EXPLICIT_MODIFIED")
58  (INTEGRATIONSCHEME_LINEAR_EULER_EXPLICIT_MODIFIED, "INTEGRATIONSCHEME_LINEAR_EULER_EXPLICIT_MODIFIED")
59  (INTEGRATIONSCHEME_EULER_IMPLICIT, "INTEGRATIONSCHEME_EULER_IMPLICIT")
60  (INTEGRATIONSCHEME_LINEAR_EULER_IMPLICIT, "INTEGRATIONSCHEME_LINEAR_EULER_IMPLICIT")
61  (INTEGRATIONSCHEME_RUNGE_KUTTA_4, "INTEGRATIONSCHEME_RUNGE_KUTTA_4")
62  (INTEGRATIONSCHEME_LINEAR_RUNGE_KUTTA_4, "INTEGRATIONSCHEME_LINEAR_RUNGE_KUTTA_4");
63 
78 class OdeSolver
79 {
80 public:
83  explicit OdeSolver(OdeEquation* equation);
84 
86  virtual ~OdeSolver()
87  {}
88 
91  const std::string getName() const;
92 
95  void setLinearSolver(std::shared_ptr<LinearSparseSolveAndInverse> linearSolver);
96 
99  std::shared_ptr<LinearSparseSolveAndInverse> getLinearSolver() const;
100 
106  virtual void solve(double dt, const OdeState& currentState, OdeState* newState, bool computeCompliance = true) = 0;
107 
112  void computeMatrices(double dt, const OdeState& state, bool computeCompliance = true);
113 
116  const SparseMatrix& getSystemMatrix() const;
117 
119  const Matrix& getComplianceMatrix() const;
120 
121 protected:
129  virtual void assembleLinearSystem(double dt, const OdeState& state, const OdeState& newState,
130  bool computeRHS = true) = 0;
131 
137 
140  std::string m_name;
141 
144 
146  std::shared_ptr<LinearSparseSolveAndInverse> m_linearSolver;
147 
153 
156 
159 };
160 
161 }; // namespace Math
162 
163 }; // namespace SurgSim
164 
165 #endif // SURGSIM_MATH_ODESOLVER_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
std::string m_name
Name for this solver.
Definition: OdeSolver.h:140
virtual void solve(double dt, const OdeState &currentState, OdeState *newState, bool computeCompliance=true)=0
Solves the equation.
void computeComplianceMatrixFromSystemMatrix(const OdeState &state)
Helper method computing the compliance matrix from the system matrix and setting the boundary conditi...
Definition: OdeSolver.cpp:69
Eigen::SparseMatrix< double > SparseMatrix
A sparse matrix.
Definition: SparseMatrix.h:32
Ode equation of 2nd order of the form with for initial conditions and a set of boundary conditions...
Definition: OdeEquation.h:54
The state of an ode of 2nd order of the form with boundary conditions.
Definition: OdeState.h:38
Vector m_solution
Linear system solution and rhs vectors (including boundary conditions)
Definition: OdeSolver.h:155
virtual void assembleLinearSystem(double dt, const OdeState &state, const OdeState &newState, bool computeRHS=true)=0
Assemble the linear system (A.x=b) to be solved for the state and new states (useful for certain ode ...
const std::string getName() const
Gets the solver&#39;s name.
Definition: OdeSolver.cpp:31
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
A dynamic size column vector.
Definition: Vector.h:68
OdeSolver(OdeEquation *equation)
Constructor.
Definition: OdeSolver.cpp:25
virtual ~OdeSolver()
Virtual destructor.
Definition: OdeSolver.h:86
Definitions of small fixed-size square matrix types.
OdeEquation & m_equation
The ode equation (API providing the necessary evaluation methods and the initial state) ...
Definition: OdeSolver.h:143
const Matrix & getComplianceMatrix() const
Definition: OdeSolver.cpp:51
Base class for all solvers of ode equation of order 2 of the form .
Definition: OdeSolver.h:78
const SparseMatrix & getSystemMatrix() const
Queries the current system matrix.
Definition: OdeSolver.cpp:46
SparseMatrix m_systemMatrix
Linear system matrix (can be M, K, combination of MDK depending on the solver), including boundary co...
Definition: OdeSolver.h:152
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65
Matrix m_complianceMatrix
Compliance matrix which is the inverse of the system matrix, including boundary conditions.
Definition: OdeSolver.h:158
std::shared_ptr< LinearSparseSolveAndInverse > getLinearSolver() const
Gets the specialized linear solver used with this Ode solver.
Definition: OdeSolver.cpp:41
void computeMatrices(double dt, const OdeState &state, bool computeCompliance=true)
Computes the system and compliance matrices for a given state.
Definition: OdeSolver.cpp:57
std::shared_ptr< LinearSparseSolveAndInverse > m_linearSolver
The specialized linear solver to use when solving the ode equation.
Definition: OdeSolver.h:146
void setLinearSolver(std::shared_ptr< LinearSparseSolveAndInverse > linearSolver)
Sets the specialized linear solver to use with this Ode solver.
Definition: OdeSolver.cpp:36