CoolProp 8.0.0
An open-source fluid property and humid air property database
Hermite1D.h
Go to the documentation of this file.
1#ifndef COOLPROP_SVD_HERMITE1D_H
2#define COOLPROP_SVD_HERMITE1D_H
3
4namespace CoolProp {
5namespace svd {
6
7// Cubic-Hermite interpolation on a single interval.
8//
9// Given values y0,y1 and slopes m0,m1 at the two ends of [x0, x1] with
10// width h = x1 - x0 and t = (x - x0)/h in [0, 1], the interpolant is
11//
12// p(t) = h00(t) y0 + h*h10(t) m0 + h01(t) y1 + h*h11(t) m1
13//
14// with the four Hermite basis polynomials
15//
16// h00(t) = 2 t^3 - 3 t^2 + 1
17// h10(t) = t^3 - 2 t^2 + t
18// h01(t) = -2 t^3 + 3 t^2
19// h11(t) = t^3 - t^2
20//
21// Slopes are an *input*: this header has no opinion on where they come
22// from. Use natural-cubic-spline slopes, FD slopes, PCHIP slopes,
23// analytic derivatives — anything that gives one number per knot.
24
26{
27 double h00;
28 double h10;
29 double h01;
30 double h11;
31};
32
33inline HermiteBasis hermite_basis(double t) noexcept {
34 const double t2 = t * t;
35 const double t3 = t2 * t;
36 return {2.0 * t3 - 3.0 * t2 + 1.0, t3 - 2.0 * t2 + t, -2.0 * t3 + 3.0 * t2, t3 - t2};
37}
38
39inline double hermite_eval(double y0, double y1, double m0, double m1, double h, double t) noexcept {
40 const HermiteBasis b = hermite_basis(t);
41 return b.h00 * y0 + b.h10 * h * m0 + b.h01 * y1 + b.h11 * h * m1;
42}
43
44// Derivative dp/dx at parameter t (note dt/dx = 1/h, so we divide by h).
45inline double hermite_eval_deriv(double y0, double y1, double m0, double m1, double h, double t) noexcept {
46 const double t2 = t * t;
47 // d/dt of (h00, h10, h01, h11)
48 const double dh00 = 6.0 * t2 - 6.0 * t;
49 const double dh10 = 3.0 * t2 - 4.0 * t + 1.0;
50 const double dh01 = -6.0 * t2 + 6.0 * t;
51 const double dh11 = 3.0 * t2 - 2.0 * t;
52 const double dp_dt = dh00 * y0 + dh10 * h * m0 + dh01 * y1 + dh11 * h * m1;
53 return dp_dt / h;
54}
55
56} // namespace svd
57} // namespace CoolProp
58
59#endif // COOLPROP_SVD_HERMITE1D_H