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-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 #include <Reaktoro/Common/Outputter.hpp>
27 
28 namespace Reaktoro {
29 
32 {
34  Vector val;
35 
38 
43  bool succeeded = true;
44 };
45 
50 
53 {
56 
59 
62 
66 
69  Vector b;
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 
138 
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, VectorRef x) -> NonlinearResult;
163 
168  auto solve(const NonlinearProblem& problem, VectorRef x, const NonlinearOptions& options) -> NonlinearResult;
169 
170 private:
171  struct Impl;
172 
173  std::unique_ptr<Impl> pimpl;
174 };
175 
176 } // namespace Reaktoro
auto operator=(NonlinearSolver other) -> NonlinearSolver &
Assign an NonlinearSolver instance to this.
Definition: NonlinearSolver.cpp:259
std::vector< std::string > fnames
The names of the non-linear functions.
Definition: NonlinearSolver.hpp:87
auto solve(const NonlinearProblem &problem, VectorRef x) -> NonlinearResult
Solve a non-linear problem.
Definition: NonlinearSolver.cpp:265
Vector b
The right-hand side vector of the linear equality constraint A*x = b.
Definition: NonlinearSolver.hpp:69
double time_function_evals
The wall time spent for all function evaluations (in units of s).
Definition: NonlinearSolver.hpp:137
A type that implements the Newton algorithm for solving non-linear problems.
Definition: NonlinearSolver.hpp:145
NonlinearOutput output
The options for the output of the non-linear problem calculation.
Definition: NonlinearSolver.hpp:115
NonlinearFunction f
The non-linear residual function.
Definition: NonlinearSolver.hpp:55
Index n
The number of unknowns in the non-linear problem.
Definition: NonlinearSolver.hpp:58
std::string xprefix
The prefix for the unknown variables x.
Definition: NonlinearSolver.hpp:76
double tolerance
The tolerance for the residual of the non-linear function.
Definition: NonlinearSolver.hpp:97
std::function< NonlinearResidual(VectorConstRef x)> NonlinearFunction
A type that describes the functional signature of a non-linear residual function.
Definition: NonlinearSolver.hpp:49
NonlinearSolver()
Construct a default NonlinearSolver instance.
Definition: NonlinearSolver.cpp:248
Matrix A
The left-hand side matrix of the linear inequality constraint A*x = b.
Definition: NonlinearSolver.hpp:65
double tolerancex
The tolerance for the variation in variables x.
Definition: NonlinearSolver.hpp:103
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
double tau
The boundary to the fraction parameter.
Definition: NonlinearSolver.hpp:109
A type that describes the evaluation result of a non-linear residual function.
Definition: NonlinearSolver.hpp:32
unsigned num_function_evals
The number of evaluations of the residual non-linear function in the calculation.
Definition: NonlinearSolver.hpp:128
double time
The wall time spent for the calculation (in units of s).
Definition: NonlinearSolver.hpp:134
std::string fprefix
The prefix for the residual functions.
Definition: NonlinearSolver.hpp:79
virtual ~NonlinearSolver()
Destroy this NonlinearSolver instance.
Definition: NonlinearSolver.cpp:256
bool succeeded
The boolean flag that indicates if the non-linear function evaluation succeeded.
Definition: NonlinearSolver.hpp:43
Index m
The number of non-linear functions.
Definition: NonlinearSolver.hpp:61
A type that describes the options for the solution of a non-linear problem.
Definition: NonlinearSolver.hpp:95
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
double armijo
The Armijo parameter used in the backtracking line search algorithm.
Definition: NonlinearSolver.hpp:112
A type that describes the non-linear problem.
Definition: NonlinearSolver.hpp:53
double time_linear_systems
The wall time spent for all linear system solutions (in units of s).
Definition: NonlinearSolver.hpp:140
A type that describes the result of a non-linear problem calculation.
Definition: NonlinearSolver.hpp:120
Eigen::MatrixXd Matrix
Alias to Eigen type MatrixXd.
Definition: Matrix.hpp:42
double error
The final residual error of the calculation.
Definition: NonlinearSolver.hpp:131
unsigned iterations
The number of iterations in the calculation.
Definition: NonlinearSolver.hpp:125
Definition: Outputter.hpp:32
Matrix jacobian
The Jacobian matrix of the non-linear residual function evaluated at x.
Definition: NonlinearSolver.hpp:37
Eigen::Ref< Eigen::VectorXd > VectorRef
< Alias to Eigen type VectorXd.
Definition: Matrix.hpp:29
Vector val
The residual vector of the non-linear residual function evaluated at x.
Definition: NonlinearSolver.hpp:34
bool succeeded
The flag that indicates if the calculation converged.
Definition: NonlinearSolver.hpp:122
auto operator=(bool activate) -> NonlinearOutput &
Activate or deactivate output.
Definition: NonlinearSolver.cpp:33
A type that describes the options for the output of a non-linear problem calculation.
Definition: NonlinearSolver.hpp:74
Eigen::Ref< const Eigen::VectorXd > VectorConstRef
< Alias to Eigen type Ref<VectorXd>.
Definition: Matrix.hpp:31
unsigned max_iterations
The maximum number of iterations in the solution of the non-linear problem.
Definition: NonlinearSolver.hpp:106
std::vector< std::string > xnames
The names of the unknown variables x.
Definition: NonlinearSolver.hpp:83