OGS
ConcreteODESolver.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <memory>
14 
15 #include "FunctionHandles.h"
16 #include "ODESolver.h"
17 
18 namespace BaseLib
19 {
20 class ConfigTree;
21 }
22 
23 namespace MathLib
24 {
25 namespace ODE
26 {
27 template <unsigned NumEquations>
28 std::unique_ptr<ODESolver<NumEquations>> createODESolver(
29  BaseLib::ConfigTree const& config);
30 
33 
54 template <typename Implementation, unsigned NumEquations>
55 class ConcreteODESolver final : public ODESolver<NumEquations>,
56  private Implementation
57 {
58 public:
61  {
62  Implementation::setFunction(
63  std::make_unique<detail::FunctionHandlesImpl<NumEquations>>(f, df));
64  }
65 
66  void setTolerance(const std::array<double, NumEquations>& abstol,
67  const double reltol) override
68  {
69  Implementation::setTolerance(abstol.data(), reltol);
70  }
71 
72  void setTolerance(const double abstol, const double reltol) override
73  {
74  Implementation::setTolerance(abstol, reltol);
75  }
76 
77  void setIC(const double t0,
78  std::initializer_list<double> const& y0) override
79  {
80  assert(y0.size() == NumEquations);
81  Implementation::setIC(t0, y0.begin());
82  }
83 
84  void setIC(const double t0,
85  Eigen::Matrix<double, NumEquations, 1, Eigen::ColMajor> const&
86  y0) override
87  {
88  Implementation::setIC(t0, y0.data());
89  }
90 
91  void preSolve() override { Implementation::preSolve(); }
92  bool solve(const double t) override { return Implementation::solve(t); }
94  {
95  return MappedConstVector<NumEquations>{Implementation::getSolution()};
96  }
97  double getTime() const override { return Implementation::getTime(); }
98  Eigen::Matrix<double, NumEquations, 1, Eigen::ColMajor> getYDot(
99  const double t, const MappedConstVector<NumEquations>& y) const override
100  {
101  Eigen::Matrix<double, NumEquations, 1, Eigen::ColMajor> y_dot;
102  Implementation::getYDot(t, y.data(), y_dot.data());
103  return y_dot;
104  }
105 
106 private:
108  explicit ConcreteODESolver(BaseLib::ConfigTree const& config)
109  : Implementation{config, NumEquations}
110  {
111  }
112 
113  friend std::unique_ptr<ODESolver<NumEquations>>
114  createODESolver<NumEquations>(BaseLib::ConfigTree const& config);
115 };
116 
118 
119 } // namespace ODE
120 } // namespace MathLib
void setIC(const double t0, Eigen::Matrix< double, NumEquations, 1, Eigen::ColMajor > const &y0) override
This is an overloaded member function, provided for convenience. It differs from the above function o...
void setIC(const double t0, std::initializer_list< double > const &y0) override
double getTime() const override
void setFunction(Function< NumEquations > f, JacobianFunction< NumEquations > df) override
Eigen::Matrix< double, NumEquations, 1, Eigen::ColMajor > getYDot(const double t, const MappedConstVector< NumEquations > &y) const override
void setTolerance(const std::array< double, NumEquations > &abstol, const double reltol) override
bool solve(const double t) override
ConcreteODESolver(BaseLib::ConfigTree const &config)
Instances of this class shall only be constructed by createODESolver().
MappedConstVector< NumEquations > getSolution() const override
Returns the solution vector y.
void setTolerance(const double abstol, const double reltol) override
std::function< bool(const double t, MappedConstVector< N > const &y, MappedConstVector< N > const &ydot, MappedMatrix< N, N > &jac)> JacobianFunction
MappedConstMatrix< N, 1 > MappedConstVector
std::unique_ptr< ODESolver< NumEquations > > createODESolver(BaseLib::ConfigTree const &config)
std::function< bool(const double t, MappedConstVector< N > const &y, MappedVector< N > &ydot)> Function
static const double t
Function handles for an ODE system of N equations.