autogenu-jupyter
An automatic code generator and the continuation/GMRES (C/GMRES) based numerical solvers for nonlinear MPC
Loading...
Searching...
No Matches
continuation_gmres.hpp
Go to the documentation of this file.
1#ifndef CGMRES__CONTINUATION_GMRES_HPP_
2#define CGMRES__CONTINUATION_GMRES_HPP_
3
4#include <stdexcept>
5
6#include "cgmres/types.hpp"
7
9
10
11namespace cgmres {
12namespace detail {
13
14template <class NLP>
16public:
17 static constexpr int nx = NLP::nx;
18 static constexpr int nu = NLP::nu;
19 static constexpr int nc = NLP::nc;
20 static constexpr int dim = NLP::dim;
21
22 ContinuationGMRES(const NLP& nlp, const Scalar finite_difference_epsilon,
23 const Scalar zeta)
24 : nlp_(nlp),
25 finite_difference_epsilon_(finite_difference_epsilon),
26 zeta_(zeta),
27 updated_solution_(Vector<dim>::Zero()),
28 fonc_(Vector<dim>::Zero()),
29 fonc_1_(Vector<dim>::Zero()),
30 fonc_2_(Vector<dim>::Zero()),
31 x_1_(Vector<nx>::Zero()),
32 dx_(Vector<nx>::Zero()) {
33 if (finite_difference_epsilon <= 0.0) {
34 throw std::invalid_argument("[ContinuationGMRES]: 'finite_difference_epsilon' must be positive!");
35 }
36 if (zeta <= 0.0) {
37 throw std::invalid_argument("[ContinuationGMRES]: 'zeta' must be positive!");
38 }
39 }
40
41 ContinuationGMRES() = default;
42
43 ~ContinuationGMRES() = default;
44
45 Scalar optError() const {
46 return fonc_.template lpNorm<2>();
47 }
48
49 template <typename VectorType>
50 void eval_fonc(const Scalar t, const MatrixBase<VectorType>& x, const Vector<dim>& solution) {
51 nlp_.eval_fonc_hu(t, x, solution, fonc_);
52 }
53
54 template <typename VectorType1, typename VectorType2, typename VectorType3, typename VectorType4>
55 void eval_b(const Scalar t, const MatrixBase<VectorType1>& x,
56 const MatrixBase<VectorType2>& solution,
57 const MatrixBase<VectorType3>& solution_update,
58 const MatrixBase<VectorType4>& b_vec) {
59 assert(solution.size() == dim);
60 assert(solution_update.size() == dim);
61 assert(b_vec.size() == dim);
62
63 const Scalar t1 = t + finite_difference_epsilon_;
64 nlp_.ocp().eval_f(t, x.derived().data(), solution.derived().data(), dx_.data());
65 x_1_ = x + finite_difference_epsilon_ * dx_;
66 updated_solution_ = solution + finite_difference_epsilon_ * solution_update;
67
68 nlp_.eval_fonc_hu(t, x, solution, fonc_);
69 nlp_.eval_fonc_hu(t1, x_1_, solution, fonc_1_);
70 nlp_.eval_fonc_hu(t1, x_1_, updated_solution_, fonc_2_);
71
72 CGMRES_EIGEN_CONST_CAST(VectorType4, b_vec) = (1/finite_difference_epsilon_ - zeta_) * fonc_
73 - fonc_2_ / finite_difference_epsilon_;
74 }
75
76 template <typename VectorType1, typename VectorType2, typename VectorType3, typename VectorType4>
78 const MatrixBase<VectorType2>& solution,
79 const MatrixBase<VectorType3>& solution_update,
80 const MatrixBase<VectorType4>& ax_vec) {
81 assert(solution.size() == dim);
82 assert(solution_update.size() == dim);
83 assert(ax_vec.size() == dim);
84 const Scalar t1 = t + finite_difference_epsilon_;
85 updated_solution_ = solution + finite_difference_epsilon_ * solution_update;
86 nlp_.eval_fonc_hu(t1, x_1_, updated_solution_, fonc_2_);
87 CGMRES_EIGEN_CONST_CAST(VectorType4, ax_vec) = (fonc_2_ - fonc_1_) / finite_difference_epsilon_;
88 }
89
90 void retrive_dummy(Vector<dim>& solution, const Scalar min_dummy) {
91 fonc_1_.setZero();
92 nlp_.retrive_dummy(solution, fonc_1_, min_dummy);
93 }
94
95 void retrive_mu(Vector<dim>& solution) {
96 fonc_1_.setZero();
97 nlp_.retrive_mu(solution, fonc_1_);
98 }
99
100 decltype(auto) x() const { return nlp_.x(); }
101
102 decltype(auto) lmd() const { return nlp_.lmd(); }
103
104 const NLP& get_nlp() const { return nlp_; }
105
106 void synchronize_ocp() { nlp_.synchronize_ocp(); }
107
108 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
109
110private:
111 NLP nlp_;
112 Scalar finite_difference_epsilon_, zeta_;
113 Vector<dim> updated_solution_, fonc_, fonc_1_, fonc_2_;
114 Vector<nx> x_1_, dx_;
115};
116
117} // namespace detail
118} // namespace cgmres
119
120#endif // CGMRES__CONTINUATION_GMRES_HPP_
Definition: continuation_gmres.hpp:15
void retrive_mu(Vector< dim > &solution)
Definition: continuation_gmres.hpp:95
ContinuationGMRES(const NLP &nlp, const Scalar finite_difference_epsilon, const Scalar zeta)
Definition: continuation_gmres.hpp:22
void eval_Ax(const Scalar t, const MatrixBase< VectorType1 > &x, const MatrixBase< VectorType2 > &solution, const MatrixBase< VectorType3 > &solution_update, const MatrixBase< VectorType4 > &ax_vec)
Definition: continuation_gmres.hpp:77
static constexpr int nc
Definition: continuation_gmres.hpp:19
decltype(auto) x() const
Definition: continuation_gmres.hpp:100
void eval_fonc(const Scalar t, const MatrixBase< VectorType > &x, const Vector< dim > &solution)
Definition: continuation_gmres.hpp:50
static constexpr int nu
Definition: continuation_gmres.hpp:18
void eval_b(const Scalar t, const MatrixBase< VectorType1 > &x, const MatrixBase< VectorType2 > &solution, const MatrixBase< VectorType3 > &solution_update, const MatrixBase< VectorType4 > &b_vec)
Definition: continuation_gmres.hpp:55
void synchronize_ocp()
Definition: continuation_gmres.hpp:106
static constexpr int dim
Definition: continuation_gmres.hpp:20
Scalar optError() const
Definition: continuation_gmres.hpp:45
const NLP & get_nlp() const
Definition: continuation_gmres.hpp:104
void retrive_dummy(Vector< dim > &solution, const Scalar min_dummy)
Definition: continuation_gmres.hpp:90
static constexpr int nx
Definition: continuation_gmres.hpp:17
decltype(auto) lmd() const
Definition: continuation_gmres.hpp:102
#define CGMRES_EIGEN_CONST_CAST(TYPE, OBJ)
Definition: macros.hpp:7
Definition: continuation_gmres.hpp:11
Eigen::Matrix< Scalar, size, 1 > Vector
Alias of Eigen::Vector.
Definition: types.hpp:23
double Scalar
Alias of double.
Definition: types.hpp:11
Eigen::MatrixBase< MatrixType > MatrixBase
Alias of Eigen::MatrixBase.
Definition: types.hpp:29