Reaktoro 
A unified framework for modeling chemically reactive systems
NonlinearSolver.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 #include <Reaktoro/Common/Outputter.hpp>
27 
28 namespace Reaktoro {
29 
32 {
35 
38 
43  bool succeeded = true;
44 };
45 
49 using NonlinearFunction = std::function<NonlinearResidual(const Vector& x)>;
50 
53 {
56 
59 
62 
66 
70 };
71 
74 {
76  std::string xprefix = "x";
77 
79  std::string fprefix = "f";
80 
83  std::vector<std::string> xnames;
84 
87  std::vector<std::string> fnames;
88 
90  auto operator=(bool activate) -> NonlinearOutput&;
91 };
92 
95 {
97  double tolerance = 1.0e-6;
98 
103  double tolerancex = 0.0;
104 
106  unsigned max_iterations = 100;
107 
109  double tau = 0.9999;
110 
112  double armijo = 1.0e-4;
113 
116 };
117 
120 {
122  bool succeeded = false;
123 
125  unsigned iterations = 0;
126 
128  unsigned num_function_evals = 0;
129 
131  double error = 0;
132 
134  double time = 0;
135 
137  double time_function_evals = 0;
138 
140  double time_linear_systems = 0;
141 };
142 
145 {
146 public:
148  NonlinearSolver();
149 
151  NonlinearSolver(const NonlinearSolver& other);
152 
154  virtual ~NonlinearSolver();
155 
157  auto operator=(NonlinearSolver other) -> NonlinearSolver&;
158 
162  auto solve(const NonlinearProblem& problem, Vector& x) -> NonlinearResult;
163 
168  auto solve(const NonlinearProblem& problem, Vector& x, const NonlinearOptions& options) -> NonlinearResult;
169 
170 private:
171  struct Impl;
172 
173  std::unique_ptr<Impl> pimpl;
174 };
175 
176 } // namespace Reaktoro
Eigen::MatrixXd Matrix
Define an alias to the matrix type of the Eigen library.
Definition: Matrix.hpp:387
std::function< NonlinearResidual(const Vector &x)> NonlinearFunction
A type that describes the functional signature of a non-linear residual function. ...
Definition: NonlinearSolver.hpp:49
A type that describes the evaluation result of a non-linear residual function.
Definition: NonlinearSolver.hpp:31
Index m
The number of non-linear functions.
Definition: NonlinearSolver.hpp:61
bool succeeded
The boolean flag that indicates if the non-linear function evaluation succeeded.
Definition: NonlinearSolver.hpp:43
A type that describes the non-linear problem.
Definition: NonlinearSolver.hpp:52
A type that implements the Newton algorithm for solving non-linear problems.
Definition: NonlinearSolver.hpp:144
Vector val
The residual vector of the non-linear residual function evaluated at x.
Definition: NonlinearSolver.hpp:34
NonlinearOutput output
The options for the output of the non-linear problem calculation.
Definition: NonlinearSolver.hpp:115
Vector b
The right-hand side vector of the linear equality constraint A*x = b.
Definition: NonlinearSolver.hpp:69
Index n
The number of unknowns in the non-linear problem.
Definition: NonlinearSolver.hpp:58
Matrix jacobian
The Jacobian matrix of the non-linear residual function evaluated at x.
Definition: NonlinearSolver.hpp:37
A type that describes the result of a non-linear problem calculation.
Definition: NonlinearSolver.hpp:119
Eigen::VectorXd Vector
Define an alias to the vector type of the Eigen library.
Definition: Matrix.hpp:384
A type that describes the options for the output of a non-linear problem calculation.
Definition: NonlinearSolver.hpp:73
A type that describes the options for the solution of a non-linear problem.
Definition: NonlinearSolver.hpp:94
std::vector< std::string > xnames
The names of the unknown variables x.
Definition: NonlinearSolver.hpp:83
Definition: Outputter.hpp:31
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
NonlinearFunction f
The non-linear residual function.
Definition: NonlinearSolver.hpp:55
std::vector< std::string > fnames
The names of the non-linear functions.
Definition: NonlinearSolver.hpp:87
Matrix A
The left-hand side matrix of the linear inequality constraint A*x = b.
Definition: NonlinearSolver.hpp:65
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
auto time() -> Time
Return the time point now.
Definition: TimeUtils.cpp:22