24#ifndef DUMUX_COMMON_MONOTONE_CUBIC_SPLINE_HH
25#define DUMUX_COMMON_MONOTONE_CUBIC_SPLINE_HH
33#include <dune/common/float_cmp.hh>
46template<
class Scalar =
double>
65 assert (x.size() == y.size());
66 assert (x.size() >=2);
67 assert (std::is_sorted(x.begin(), x.end()));
77 void updatePoints(
const std::vector<Scalar>& x,
const std::vector<Scalar>& y)
84 numPoints_ = x.size();
87 m_.resize(numPoints_);
90 Scalar deltaX = (x[1]-x[0]);
91 Scalar secant = m_.front() = (y[1]-y[0])/deltaX;
92 Scalar prevDeltaX = deltaX;
93 Scalar prevSecant = secant;
94 for (
int i = 1; i < numPoints_-1; ++i, prevSecant = secant, prevDeltaX = deltaX)
96 deltaX = (x[i+1]-x[i]);
97 secant = (y[i+1]-y[i])/deltaX;
98 const auto alpha = (prevDeltaX + 2*deltaX)/(3*(prevDeltaX + deltaX));
99 m_[i] = prevSecant*secant > 0.0 ? prevSecant*secant/(alpha*secant + (1.0-alpha)*prevSecant) : 0.0;
109 Scalar
eval(
const Scalar x)
const
112 return y_.front() + m_.front()*(x - x_.front());
113 else if (x > x_.back())
114 return y_.back() + m_.back()*(x - x_.back());
117 const auto lookUpIndex = std::distance(x_.begin(), std::lower_bound(x_.begin(), x_.end(), x));
118 assert(lookUpIndex != 0);
121 const auto h = (x_[lookUpIndex] - x_[lookUpIndex-1]);
122 const auto t = (x - x_[lookUpIndex-1])/h;
137 else if (x > x_.back())
141 const auto lookUpIndex = std::distance(x_.begin(), std::lower_bound(x_.begin(), x_.end(), x));
142 assert(lookUpIndex != 0);
145 const auto h = (x_[lookUpIndex] - x_[lookUpIndex-1]);
146 const auto t = (x - x_[lookUpIndex-1])/h;
147 const auto dtdx = 1.0/h;
154 std::vector<Scalar> x_;
155 std::vector<Scalar> y_;
156 std::vector<Scalar> m_;
157 std::size_t numPoints_;
The cubic hermite spline basis.
The cubic spline hermite basis.
Definition: cubicsplinehermitebasis.hh:36
static constexpr Scalar h01(const Scalar t)
Definition: cubicsplinehermitebasis.hh:43
static constexpr Scalar dh01(const Scalar t)
Definition: cubicsplinehermitebasis.hh:55
static constexpr Scalar dh11(const Scalar t)
Definition: cubicsplinehermitebasis.hh:58
static constexpr Scalar h11(const Scalar t)
Definition: cubicsplinehermitebasis.hh:46
static constexpr Scalar h00(const Scalar t)
Definition: cubicsplinehermitebasis.hh:37
static constexpr Scalar dh10(const Scalar t)
Definition: cubicsplinehermitebasis.hh:52
static constexpr Scalar h10(const Scalar t)
Definition: cubicsplinehermitebasis.hh:40
static constexpr Scalar dh00(const Scalar t)
Definition: cubicsplinehermitebasis.hh:49
A monotone cubic spline.
Definition: monotonecubicspline.hh:48
MonotoneCubicSpline()=default
Default constructor.
void updatePoints(const std::vector< Scalar > &x, const std::vector< Scalar > &y)
Create a monotone cubic spline from the control points (x[i], y[i])
Definition: monotonecubicspline.hh:77
Scalar eval(const Scalar x) const
Evaluate the y value at a given x value.
Definition: monotonecubicspline.hh:109
MonotoneCubicSpline(const std::vector< Scalar > &x, const std::vector< Scalar > y)
Contruct a monotone cubic spline from the control points (x[i], y[i])
Definition: monotonecubicspline.hh:62
Scalar evalDerivative(const Scalar x) const
Evaluate the first derivative dy/dx at a given x value.
Definition: monotonecubicspline.hh:133