Reaktoro 
A unified framework for modeling chemically reactive systems
KktSolver.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 <memory>
22 
23 // Reaktoro includes
24 #include <Reaktoro/Math/Matrix.hpp>
25 #include <Reaktoro/Optimization/Hessian.hpp>
26 
27 namespace Reaktoro {
28 
30 struct KktResult
31 {
33  bool succeeded = false;
34 
36  double time_decompose = 0;
37 
39  double time_solve = 0;
40 };
41 
43 enum class KktMethod
44 {
48 
51  FullPivLU,
52 
56  Nullspace,
57 
61  Rangespace,
62 
67  Automatic,
68 };
69 
71 struct KktOptions
72 {
75 };
76 
79 struct KktMatrix
80 {
82  KktMatrix(const Hessian& H, const Matrix& A, const Vector& x, const Vector& z)
83  : H(H), A(A), x(x), z(z)
84  {}
85 
87  KktMatrix(const Hessian& H, const Matrix& A, const Vector& x, const Vector& z, double gamma, double delta)
88  : H(H), A(A), x(x), z(z), gamma(gamma), delta(delta)
89  {}
90 
92  const Hessian& H;
93 
95  const Matrix& A;
96 
98  const Vector& x;
99 
101  const Vector& z;
102 
104  const double gamma = 0.0;
105 
107  const double delta = 0.0;
108 };
109 
113 {
116 
119 
122 };
123 
126 struct KktVector
127 {
130 
133 
136 };
137 
140 {
141 public:
143  KktSolver();
144 
146  KktSolver(const KktSolver& other);
147 
149  virtual ~KktSolver();
150 
152  auto operator=(KktSolver other) -> KktSolver&;
153 
155  auto result() const -> const KktResult&;
156 
158  auto setOptions(const KktOptions& options) -> void;
159 
161  auto decompose(const KktMatrix& lhs) -> void;
162 
173  auto solve(const KktVector& rhs, KktSolution& sol) -> void;
174 
175 private:
177  struct Impl;
178 
180  std::unique_ptr<Impl> pimpl;
181 };
182 
183 } // namespace Reaktoro
Eigen::MatrixXd Matrix
Define an alias to the matrix type of the Eigen library.
Definition: Matrix.hpp:387
Vector dy
The step vector of the dual variables y
Definition: KktSolver.hpp:118
Vector rx
The top vector of the right-hand side KKT vector.
Definition: KktSolver.hpp:129
KktMatrix(const Hessian &H, const Matrix &A, const Vector &x, const Vector &z)
Construct a custom KktMatrix instance.
Definition: KktSolver.hpp:82
const Vector & x
The vector of primal variables x
Definition: KktSolver.hpp:98
Use a full pivoting LU algorithm on the full KKT equation.
Vector rz
The bottom vector of the right-hand side KKT vector.
Definition: KktSolver.hpp:135
A type to describe the options for the KKT calculation.
Definition: KktSolver.hpp:71
const Vector & z
The vector of dual variables z
Definition: KktSolver.hpp:101
const Matrix & A
The coefficient matrix A of the KKT matrix equation.
Definition: KktSolver.hpp:95
const Hessian & H
The Hessian matrix H of the KKT matrix equation.
Definition: KktSolver.hpp:92
Use a nullspace method to solve the KKT equation.
A type to describe a solver for a KKT equation.
Definition: KktSolver.hpp:139
A type to describe the result of a KKT calculation.
Definition: KktSolver.hpp:30
Eigen::VectorXd Vector
Define an alias to the vector type of the Eigen library.
Definition: Matrix.hpp:384
A type to represent the right-hand side vector of a KKT equation.
Definition: KktSolver.hpp:126
Use a method that fits better to the type of KKT equation.
double time_decompose
The wall time spent for the decomposition of the KKT problem (in units of s)
Definition: KktSolver.hpp:36
Vector dx
The step vector of the primal variables x
Definition: KktSolver.hpp:115
A type to describe the Hessian of an objective function.
Definition: Hessian.hpp:26
A type to represent the solution vector of a KKT equation.
Definition: KktSolver.hpp:112
A type to represent the left-hand side matrix of a KKT equation.
Definition: KktSolver.hpp:79
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
KktMethod
An enumeration of possible methods for the solution of a KKT equation.
Definition: KktSolver.hpp:43
Vector ry
The middle vector of the right-hand side KKT vector.
Definition: KktSolver.hpp:132
KktMatrix(const Hessian &H, const Matrix &A, const Vector &x, const Vector &z, double gamma, double delta)
Construct a custom KktMatrix instance.
Definition: KktSolver.hpp:87
bool succeeded
The flag that indicates if the KKT calculation succeeded.
Definition: KktSolver.hpp:33
Use a partial pivoting LU algorithm on the full KKT equation.
Use a rangespace method to solve the KKT equation.
double time_solve
The wall time spent for the solution of the KKT problem (in units of s)
Definition: KktSolver.hpp:39
Vector dz
The step vector of the dual variables z
Definition: KktSolver.hpp:121