Reaktoro
A unified framework for modeling chemically reactive systems
DaeSolver.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright (C) 2014-2018 Allan Leal
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // C++ includes
21 #include <functional>
22 #include <memory>
23 
24 // Reaktoro includes
25 #include <Reaktoro/Math/Matrix.hpp>
26 
27 namespace Reaktoro {
28 
30 using DaeFunction = std::function<int(double t, VectorConstRef u, VectorConstRef udot, VectorRef F)>;
31 
33 using DaeJacobian = std::function<int(double t, VectorConstRef u, VectorConstRef udot, MatrixRef J)>;
34 
37 struct DaeOptions
38 {
39 };
40 
44 {
45 public:
47  DaeProblem();
48 
50  DaeProblem(const DaeProblem& other);
51 
53  virtual ~DaeProblem();
54 
56  auto operator=(DaeProblem other) -> DaeProblem&;
57 
59  auto setNumEquations(unsigned num) -> void;
60 
62  auto setFunction(const DaeFunction& f) -> void;
63 
65  auto setJacobian(const DaeJacobian& J) -> void;
66 
68  auto initialized() const -> bool;
69 
71  auto numEquations() const -> unsigned;
72 
74  auto function() const -> const DaeFunction&;
75 
77  auto jacobian() const -> const DaeJacobian&;
78 
85  auto function(double t, VectorConstRef y, VectorConstRef ydot, VectorRef f) const -> int;
86 
93  auto jacobian(double t, VectorConstRef y, VectorConstRef ydot, MatrixRef J) const -> int;
94 
95 private:
96  struct Impl;
97 
98  std::unique_ptr<Impl> pimpl;
99 };
100 
104 {
105 public:
107  DaeSolver();
108 
110  DaeSolver(const DaeSolver& other);
111 
113  virtual ~DaeSolver();
114 
116  auto operator=(DaeSolver other) -> DaeSolver&;
117 
120  auto setOptions(const DaeOptions& options) -> void;
121 
124  auto setProblem(const DaeProblem& problem) -> void;
125 
130  auto initialize(double tstart, VectorConstRef y) -> void;
131 
135  auto integrate(double& t, VectorRef y) -> void;
136 
141  auto integrate(double& t, VectorRef y, double tfinal) -> void;
142 
147  auto solve(double& t, double dt, VectorRef y) -> void;
148 
149 private:
150  struct Impl;
151 
152  std::unique_ptr<Impl> pimpl;
153 };
154 
155 } // namespace Reaktoro
auto integrate(double &t, VectorRef y) -> void
Integrate the ODE performing a single step.
Definition: DaeSolver.cpp:186
auto numEquations() const -> unsigned
Return the number of ordinary differential equations.
Definition: DaeSolver.cpp:129
Eigen::Ref< Eigen::MatrixXd > MatrixRef
Alias to Eigen type Ref<MatrixXd>.
Definition: Matrix.hpp:43
auto jacobian() const -> const DaeJacobian &
Return the Jacobian of the right-hand side function of the system of ordinary differential equations.
Definition: DaeSolver.cpp:139
A class that defines a system of differential-algebraic equations (DAE) problem.
Definition: DaeSolver.hpp:44
virtual ~DaeSolver()
Destroy this DaeSolver instance.
Definition: DaeSolver.cpp:162
std::function< int(double t, VectorConstRef u, VectorConstRef udot, MatrixRef J)> DaeJacobian
The function signature of the Jacobian of the right-hand side function of a system of ordinary differ...
Definition: DaeSolver.hpp:33
auto setFunction(const DaeFunction &f) -> void
Set the right-hand side function of the system of ordinary differential equations.
Definition: DaeSolver.cpp:114
A class for solving differential-algebraic equations (DAE).
Definition: DaeSolver.hpp:104
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
auto setProblem(const DaeProblem &problem) -> void
Set the ODE problem.
Definition: DaeSolver.cpp:176
virtual ~DaeProblem()
Destroy this DaeProblem instance.
Definition: DaeSolver.cpp:100
auto setJacobian(const DaeJacobian &J) -> void
Set the Jacobian of the right-hand side function of the system of ordinary differential equations.
Definition: DaeSolver.cpp:119
DaeSolver()
Construct a default DaeSolver instance.
Definition: DaeSolver.cpp:154
auto setOptions(const DaeOptions &options) -> void
Set the options for the ODE solver.
Definition: DaeSolver.cpp:171
std::function< int(double t, VectorConstRef u, VectorConstRef udot, VectorRef F)> DaeFunction
The function signature of the right-hand side function of a system of ordinary differential equations...
Definition: DaeSolver.hpp:30
DaeProblem()
Construct a default DaeProblem instance.
Definition: DaeSolver.cpp:92
auto operator=(DaeSolver other) -> DaeSolver &
Assign a DaeSolver instance to this instance.
Definition: DaeSolver.cpp:165
auto solve(double &t, double dt, VectorRef y) -> void
Solve the ODE equations from a given start time to a final one.
Definition: DaeSolver.cpp:196
auto initialize(double tstart, VectorConstRef y) -> void
Initializes the ODE solver.
Definition: DaeSolver.cpp:181
A struct that defines the options for the DaeSolver.
Definition: DaeSolver.hpp:38
Eigen::Ref< Eigen::VectorXd > VectorRef
< Alias to Eigen type VectorXd.
Definition: Matrix.hpp:29
auto setNumEquations(unsigned num) -> void
Set the number of ordinary differential equations.
Definition: DaeSolver.cpp:109
auto operator=(DaeProblem other) -> DaeProblem &
Assign a DaeProblem instance to this instance.
Definition: DaeSolver.cpp:103
Eigen::Ref< const Eigen::VectorXd > VectorConstRef
< Alias to Eigen type Ref<VectorXd>.
Definition: Matrix.hpp:31
auto initialized() const -> bool
Return true if the problem has bee initialized.
Definition: DaeSolver.cpp:124