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-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 <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 {
47  PartialPivLU,
48 
51  FullPivLU,
52 
56  Nullspace,
57 
61  Rangespace,
62 
67  Automatic,
68 };
69 
71 struct KktOptions
72 {
75 };
76 
79 struct KktMatrix
80 {
83  : H(H), A(A), x(x), z(z)
84  {}
85 
88  : H(H), A(A), x(x), z(z), gamma(gamma), delta(delta)
89  {}
90 
92  const Hessian& H;
93 
96 
99 
102 
104  const double gamma = 0.0;
105 
107  const double delta = 0.0;
108 };
109 
113 {
115  Vector dx;
116 
118  Vector dy;
119 
121  Vector dz;
122 };
123 
126 struct KktVector
127 {
129  Vector rx;
130 
132  Vector ry;
133 
135  Vector rz;
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
KktMatrix(const Hessian &H, MatrixConstRef A, VectorConstRef x, VectorConstRef z)
Construct a custom KktMatrix instance.
Definition: KktSolver.hpp:82
auto decompose(const KktMatrix &lhs) -> void
Decompose the KKT matrix before solving it.
Definition: KktSolver.cpp:575
KktMethod method
The method for the solution of the KKT equations.
Definition: KktSolver.hpp:74
KktMatrix(const Hessian &H, MatrixConstRef A, VectorConstRef x, VectorConstRef z, double gamma, double delta)
Construct a custom KktMatrix instance.
Definition: KktSolver.hpp:87
const double delta
The regularization parameter .
Definition: KktSolver.hpp:107
Vector rx
The top vector of the right-hand side KKT vector.
Definition: KktSolver.hpp:129
Vector rz
The bottom vector of the right-hand side KKT vector.
Definition: KktSolver.hpp:135
virtual ~KktSolver()
Destroy this KktSolver instance.
Definition: KktSolver.cpp:556
VectorConstRef z
The vector of dual variables z
Definition: KktSolver.hpp:101
Vector dy
The step vector of the dual variables y
Definition: KktSolver.hpp:118
KktSolver()
Construct a default KktSolver instance.
Definition: KktSolver.cpp:548
VectorConstRef x
The vector of primal variables x
Definition: KktSolver.hpp:98
KktMethod
An enumeration of possible methods for the solution of a KKT equation.
Definition: KktSolver.hpp:44
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
double time_solve
The wall time spent for the solution of the KKT problem (in units of s)
Definition: KktSolver.hpp:39
Vector ry
The middle vector of the right-hand side KKT vector.
Definition: KktSolver.hpp:132
auto setOptions(const KktOptions &options) -> void
Set the options for the KKT calculations.
Definition: KktSolver.cpp:570
A type to describe a solver for a KKT equation.
Definition: KktSolver.hpp:140
A type to describe the options for the KKT calculation.
Definition: KktSolver.hpp:72
const double gamma
The regularization parameter .
Definition: KktSolver.hpp:104
auto operator=(KktSolver other) -> KktSolver &
Assign a KktSolver instance to this.
Definition: KktSolver.cpp:559
A type to represent the solution vector of a KKT equation.
Definition: KktSolver.hpp:113
auto result() const -> const KktResult &
Return the result of the last calculation.
Definition: KktSolver.cpp:565
@ PartialPivLU
Use a partial pivoting LU algorithm on the full KKT equation.
A type to describe the result of a KKT calculation.
Definition: KktSolver.hpp:31
A type to represent the left-hand side matrix of a KKT equation.
Definition: KktSolver.hpp:80
auto solve(const KktVector &rhs, KktSolution &sol) -> void
Solve the KKT equation using an appropriate and efficient approach according to a priori decompositio...
Definition: KktSolver.cpp:580
Vector dx
The step vector of the primal variables x
Definition: KktSolver.hpp:115
A type to represent the right-hand side vector of a KKT equation.
Definition: KktSolver.hpp:127
Vector dz
The step vector of the dual variables z
Definition: KktSolver.hpp:121
const Hessian & H
The Hessian matrix H of the KKT matrix equation.
Definition: KktSolver.hpp:92
Eigen::Ref< const Eigen::MatrixXd > MatrixConstRef
Alias to Eigen type Ref<const MatrixXd>.
Definition: Matrix.hpp:44
double time_decompose
The wall time spent for the decomposition of the KKT problem (in units of s)
Definition: KktSolver.hpp:36
bool succeeded
The flag that indicates if the KKT calculation succeeded.
Definition: KktSolver.hpp:33
MatrixConstRef A
The coefficient matrix A of the KKT matrix equation.
Definition: KktSolver.hpp:95
Eigen::Ref< const Eigen::VectorXd > VectorConstRef
< Alias to Eigen type Ref<VectorXd>.
Definition: Matrix.hpp:31
A type to describe the Hessian of an objective function.
Definition: Hessian.hpp:27