teqp 0.19.1
Loading...
Searching...
No Matches
iteration.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "teqp/cpp/derivs.hpp"
5
6namespace teqp {
7namespace iteration {
8
9using namespace cppinterface;
15private:
16 const AbstractModel *ar, *aig;
17 const std::vector<char> vars;
18 const Eigen::Ref<const Eigen::ArrayXd> vals;
19 double T, rho;
20 const Eigen::Ref<const Eigen::ArrayXd> z;
21
22public:
23 NRIterator(const AbstractModel* ar, const AbstractModel* aig, const std::vector<char>& vars, const Eigen::Ref<const Eigen::ArrayXd>& vals, double T, double rho, const Eigen::Ref<const Eigen::ArrayXd>& z) : ar(ar), aig(aig), vars(vars), vals(vals), T(T), rho(rho), z(z){}
24
26 std::vector<char> get_vars() const { return vars; }
28 Eigen::ArrayXd get_vals() const { return vals; }
30 auto get_T() const { return T; }
32 auto get_rho() const { return rho; }
34 Eigen::ArrayXd get_molefrac() const { return z; }
35
41 auto calc_step(double T, double rho){
42 auto Ar = ar->get_deriv_mat2(T, rho, z);
43 auto Aig = aig->get_deriv_mat2(T, rho, z);
44 auto R = ar->get_R(z);
45 auto im = build_iteration_Jv(vars, Ar, Aig, R, T, rho, z);
46 return std::make_tuple((im.J.matrix().colPivHouseholderQr().solve((-(im.v-vals)).matrix())).eval(), im);
47 }
48
50 auto take_step(){
51 auto [dx, im] = calc_step(T, rho);
52 T += dx(0);
53 rho += dx(1);
54 return (im.v-vals).eval();
55 }
56
60 auto take_steps(int N){
61 if (N <= 0){
62 throw teqp::InvalidArgument("N must be greater than 0");
63 }
64 for (auto i = 0; i < N; ++i){
65 take_step();
66 }
67 }
68};
69
70
71}
72}
virtual double get_R(const EArrayd &) const =0
virtual EArray33d get_deriv_mat2(const double T, double rho, const EArrayd &z) const =0
Eigen::ArrayXd get_molefrac() const
Return the current mole fractions.
Definition iteration.hpp:34
std::vector< char > get_vars() const
Return the variables that are being used in the iteration.
Definition iteration.hpp:26
auto take_step()
Take one step, return the residuals.
Definition iteration.hpp:50
auto get_T() const
Return the current temperature.
Definition iteration.hpp:30
auto get_rho() const
Return the current molar density.
Definition iteration.hpp:32
auto calc_step(double T, double rho)
Definition iteration.hpp:41
Eigen::ArrayXd get_vals() const
Return the target values to be obtained.
Definition iteration.hpp:28
NRIterator(const AbstractModel *ar, const AbstractModel *aig, const std::vector< char > &vars, const Eigen::Ref< const Eigen::ArrayXd > &vals, double T, double rho, const Eigen::Ref< const Eigen::ArrayXd > &z)
Definition iteration.hpp:23
auto build_iteration_Jv(const std::vector< char > &vars, const Eigen::Array< double, 3, 3 > &Ar, const Eigen::Array< double, 3, 3 > &Aig, const double R, const double T, const double rho, const Array &z)
A convenience function for calculation of Jacobian terms of the form and where is one of the therm...
Definition derivs.hpp:27