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