proteus 1.9.0
C/C++/Fortran libraries
Loading...
Searching...
No Matches
jet2.h
Go to the documentation of this file.
1#ifndef JET2_H
2#define JET2_H
3// Second-order forward-mode AD in two independent variables (p, z). A Jet2
4// carries (v, dp, dz, dpp, dpz, dzz) -- value plus exact first and second
5// partials -- so threading it through the EOS/flash arithmetic yields
6// machine-precision analytic derivatives with NO finite differencing.
7//
8// References:
9// Griewank, A. and Walther, A., "Evaluating Derivatives: Principles and
10// Techniques of Algorithmic Differentiation", 2nd ed., SIAM, Philadelphia,
11// 2008 -- forward mode and the propagation of truncated Taylor (jet)
12// coefficients that this struct implements for order 2 in two variables.
13// Fike, J.A. and Alonso, J.J., "The Development of Hyper-Dual Numbers for
14// Exact Second-Derivative Calculations", AIAA Paper 2011-886, 49th AIAA
15// Aerospace Sciences Meeting, Orlando, 2011 -- the same second-order
16// forward-mode idea, specialised to one independent variable.
17#include <cmath>
18
19namespace m_comp_co2 {
20
21struct Jet2 {
22 double v, dp, dz, dpp, dpz, dzz;
23 Jet2(double v_=0, double dp_=0, double dz_=0,
24 double dpp_=0, double dpz_=0, double dzz_=0)
25 : v(v_), dp(dp_), dz(dz_), dpp(dpp_), dpz(dpz_), dzz(dzz_) {}
26 static Jet2 cst(double c) { return Jet2(c); }
27 static Jet2 var_p(double p) { return Jet2(p, 1.0, 0.0); }
28 static Jet2 var_z(double z) { return Jet2(z, 0.0, 1.0); }
29};
30
31inline Jet2 operator+(const Jet2& a, const Jet2& b) {
32 return Jet2(a.v+b.v, a.dp+b.dp, a.dz+b.dz, a.dpp+b.dpp, a.dpz+b.dpz, a.dzz+b.dzz);
33}
34inline Jet2 operator+(const Jet2& a, double c) { return Jet2(a.v+c, a.dp, a.dz, a.dpp, a.dpz, a.dzz); }
35inline Jet2 operator+(double c, const Jet2& a) { return a + c; }
36inline Jet2 operator-(const Jet2& a) { return Jet2(-a.v, -a.dp, -a.dz, -a.dpp, -a.dpz, -a.dzz); }
37inline Jet2 operator-(const Jet2& a, const Jet2& b) { return a + (-b); }
38inline Jet2 operator-(const Jet2& a, double c) { return Jet2(a.v-c, a.dp, a.dz, a.dpp, a.dpz, a.dzz); }
39inline Jet2 operator-(double c, const Jet2& a) { return Jet2(c-a.v, -a.dp, -a.dz, -a.dpp, -a.dpz, -a.dzz); }
40
41inline Jet2 operator*(const Jet2& a, const Jet2& b) {
42 return Jet2(a.v*b.v,
43 a.dp*b.v + a.v*b.dp,
44 a.dz*b.v + a.v*b.dz,
45 a.dpp*b.v + 2.0*a.dp*b.dp + a.v*b.dpp,
46 a.dpz*b.v + a.dp*b.dz + a.dz*b.dp + a.v*b.dpz,
47 a.dzz*b.v + 2.0*a.dz*b.dz + a.v*b.dzz);
48}
49inline Jet2 operator*(const Jet2& a, double c) { return Jet2(a.v*c, a.dp*c, a.dz*c, a.dpp*c, a.dpz*c, a.dzz*c); }
50inline Jet2 operator*(double c, const Jet2& a) { return a*c; }
51
52inline Jet2 recip(const Jet2& s) {
53 double b = s.v, r = 1.0/b, b2 = b*b, b3 = b2*b;
54 return Jet2(r, -s.dp/b2, -s.dz/b2,
55 (2.0*s.dp*s.dp - b*s.dpp)/b3,
56 (2.0*s.dp*s.dz - b*s.dpz)/b3,
57 (2.0*s.dz*s.dz - b*s.dzz)/b3);
58}
59inline Jet2 operator/(const Jet2& a, const Jet2& b) { return a*recip(b); }
60inline Jet2 operator/(const Jet2& a, double c) { return Jet2(a.v/c, a.dp/c, a.dz/c, a.dpp/c, a.dpz/c, a.dzz/c); }
61inline Jet2 operator/(double c, const Jet2& a) { return Jet2::cst(c)*recip(a); }
62
63inline Jet2 jsqrt(const Jet2& s) {
64 double a = s.v, rt = std::sqrt(a), t2 = 2.0*rt, t3 = 4.0*rt*rt*rt;
65 return Jet2(rt, s.dp/t2, s.dz/t2,
66 s.dpp/t2 - s.dp*s.dp/t3,
67 s.dpz/t2 - s.dp*s.dz/t3,
68 s.dzz/t2 - s.dz*s.dz/t3);
69}
70inline Jet2 jexp(const Jet2& s) {
71 double e = std::exp(s.v);
72 return Jet2(e, e*s.dp, e*s.dz,
73 e*(s.dpp + s.dp*s.dp),
74 e*(s.dpz + s.dp*s.dz),
75 e*(s.dzz + s.dz*s.dz));
76}
77inline Jet2 jlog(const Jet2& s) {
78 double a = s.v, a2 = a*a;
79 return Jet2(std::log(a), s.dp/a, s.dz/a,
80 s.dpp/a - s.dp*s.dp/a2,
81 s.dpz/a - s.dp*s.dz/a2,
82 s.dzz/a - s.dz*s.dz/a2);
83}
84
85} // namespace m_comp_co2
86#endif
Double r
Definition Headers.h:83
Double s
Definition Headers.h:84
Double * z
Definition Headers.h:49
#define c(i)
Definition jf.h:21
Jet2 jsqrt(const Jet2 &s)
Definition jet2.h:63
Jet2 operator*(const Jet2 &a, const Jet2 &b)
Definition jet2.h:41
Jet2 recip(const Jet2 &s)
Definition jet2.h:52
Jet2 operator-(const Jet2 &a)
Definition jet2.h:36
Jet2 jexp(const Jet2 &s)
Definition jet2.h:70
Jet2 operator/(const Jet2 &a, const Jet2 &b)
Definition jet2.h:59
Jet2 jlog(const Jet2 &s)
Definition jet2.h:77
Jet2 operator+(const Jet2 &a, const Jet2 &b)
Definition jet2.h:31
double dp
Definition jet2.h:22
double dpp
Definition jet2.h:22
static Jet2 var_z(double z)
Definition jet2.h:28
Jet2(double v_=0, double dp_=0, double dz_=0, double dpp_=0, double dpz_=0, double dzz_=0)
Definition jet2.h:23
double dz
Definition jet2.h:22
double dpz
Definition jet2.h:22
double dzz
Definition jet2.h:22
static Jet2 var_p(double p)
Definition jet2.h:27
static Jet2 cst(double c)
Definition jet2.h:26
double v
Definition jet2.h:22