OGS  master
FunctionHandles.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include "ODESolverTypes.h"
14 
15 namespace MathLib
16 {
17 namespace ODE
18 {
19 namespace detail
20 {
23 
32 {
33 public:
36  virtual bool call(const double t, double const* const y,
37  double* const ydot) = 0;
38 
42  virtual bool callJacobian(const double t,
43  double const* const y,
44  double* const ydot,
45  double* const jac) = 0;
46 
48  virtual bool hasJacobian() const = 0;
49 
51  virtual unsigned getNumberOfEquations() const = 0;
52 
53  virtual ~FunctionHandles() = default;
54 };
55 
57 template <unsigned N>
58 struct FunctionHandlesImpl final : public FunctionHandles
59 {
61  {
62  }
63 
72  bool call(const double t, const double* const y,
73  double* const ydot) override
74  {
75  if (f)
76  {
77  MappedVector<N> ydot_mapped{ydot};
78  return f(t, MappedConstVector<N>{y}, ydot_mapped);
79  }
80  return false;
81  }
82 
89  bool callJacobian(const double t, const double* const y, double* const ydot,
90  double* const jac) override
91  {
92  if (df)
93  {
94  MappedMatrix<N, N> jac_mapped{jac};
95  return df(t,
98  jac_mapped);
99  }
100  return false;
101  }
102 
103  bool hasJacobian() const override { return df != nullptr; }
104  unsigned getNumberOfEquations() const override { return N; }
107 };
108 
110 
111 } // namespace detail
112 } // namespace ODE
113 } // namespace MathLib
virtual unsigned getNumberOfEquations() const =0
Returns the number of equations in the ODE system.
virtual bool call(const double t, double const *const y, double *const ydot)=0
virtual bool hasJacobian() const =0
Tells whether a Jacobian function has been set.
virtual bool callJacobian(const double t, double const *const y, double *const ydot, double *const jac)=0
std::function< bool(const double t, MappedConstVector< N > const &y, MappedConstVector< N > const &ydot, MappedMatrix< N, N > &jac)> JacobianFunction
MappedConstMatrix< N, 1 > MappedConstVector
Eigen::Map< Eigen::Matrix< double, N, M, Eigen::ColMajor > > MappedMatrix
std::function< bool(const double t, MappedConstVector< N > const &y, MappedVector< N > &ydot)> Function
MappedMatrix< N, 1 > MappedVector
Function handles for an ODE system of N equations.
bool hasJacobian() const override
Tells whether a Jacobian function has been set.
bool callJacobian(const double t, const double *const y, double *const ydot, double *const jac) override
unsigned getNumberOfEquations() const override
Returns the number of equations in the ODE system.
bool call(const double t, const double *const y, double *const ydot) override
FunctionHandlesImpl(Function< N > &f, JacobianFunction< N > &df)