Reaktoro 
A unified framework for modeling chemically reactive systems
ODE.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright (C) 2014-2015 Allan Leal
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. 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 ODEFunction = std::function<int(double, const Vector&, Vector&)>;
31 
33 using ODEJacobian = std::function<int(double, const Vector&, Matrix&)>;
34 
36 enum class ODEStepMode { Adams, BDF };
37 
39 enum class ODEIterationMode { Functional, Newton };
40 
43 struct ODEOptions
44 {
46  ODEStepMode step = ODEStepMode::BDF;
47 
49  ODEIterationMode iteration = ODEIterationMode::Newton;
50 
56  bool stability_limit_detection = false;
57 
60  double initial_step = 0.0;
61 
64  double stop_time = 0.0;
65 
67  double min_step = 0.0;
68 
70  double max_step = 0.0;
71 
73  double reltol = 1e-4;
74 
76  double abstol = 1.0e-6;
77 
80  unsigned max_order_bdf = 5;
81 
84  unsigned max_order_adams = 5;
85 
87  unsigned max_num_steps = 500;
88 
90  unsigned max_hnil_warnings = 10;
91 
93  unsigned max_num_error_test_failures = 20;
94 
96  unsigned max_num_nonlinear_iterations = 3;
97 
99  unsigned max_num_convergence_failures = 10;
100 
102  double nonlinear_convergence_coefficient = 0.1;
103 
106 };
107 
111 {
112 public:
114  ODEProblem();
115 
117  ODEProblem(const ODEProblem& other);
118 
120  virtual ~ODEProblem();
121 
123  auto operator=(ODEProblem other) -> ODEProblem&;
124 
126  auto setNumEquations(unsigned num) -> void;
127 
129  auto setFunction(const ODEFunction& f) -> void;
130 
132  auto setJacobian(const ODEJacobian& J) -> void;
133 
135  auto initialized() const -> bool;
136 
138  auto numEquations() const -> unsigned;
139 
141  auto function() const -> const ODEFunction&;
142 
144  auto jacobian() const -> const ODEJacobian&;
145 
151  auto function(double t, const Vector& y, Vector& f) const -> int;
152 
158  auto jacobian(double t, const Vector& y, Matrix& J) const -> int;
159 
160 private:
161  struct Impl;
162 
163  std::unique_ptr<Impl> pimpl;
164 };
165 
169 {
170 public:
172  ODESolver();
173 
175  ODESolver(const ODESolver& other);
176 
178  virtual ~ODESolver();
179 
181  auto operator=(ODESolver other) -> ODESolver&;
182 
185  auto setOptions(const ODEOptions& options) -> void;
186 
189  auto setProblem(const ODEProblem& problem) -> void;
190 
195  auto initialize(double tstart, const Vector& y) -> void;
196 
200  auto integrate(double& t, Vector& y) -> void;
201 
206  auto integrate(double& t, Vector& y, double tfinal) -> void;
207 
212  auto solve(double& t, double dt, Vector& y) -> void;
213 
214 private:
215  struct Impl;
216 
217  std::unique_ptr<Impl> pimpl;
218 };
219 
220 } // namespace Reaktoro
Eigen::MatrixXd Matrix
Define an alias to the matrix type of the Eigen library.
Definition: Matrix.hpp:387
A struct that defines the options for the ODESolver.
Definition: ODE.hpp:43
A wrapper class for CVODE, a library for solving ordinary differential equations. ...
Definition: ODE.hpp:168
ODEIterationMode
The type of nonlinear solver iteration to be used in ODESolver.
Definition: ODE.hpp:39
std::function< int(double, const Vector &, Matrix &)> ODEJacobian
The function signature of the Jacobian of the right-hand side function of a system of ordinary differ...
Definition: ODE.hpp:33
Vector abstols
The vector of absolute error tolerances for each component.
Definition: ODE.hpp:105
Eigen::VectorXd Vector
Define an alias to the vector type of the Eigen library.
Definition: Matrix.hpp:384
ODEStepMode
The linear multistep method to be used in ODESolver.
Definition: ODE.hpp:36
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
std::function< int(double, const Vector &, Vector &)> ODEFunction
The function signature of the right-hand side function of a system of ordinary differential equations...
Definition: ODE.hpp:30
A class that defines a system of ordinary differential equations (ODE) problem.
Definition: ODE.hpp:110