24#ifndef DUMUX_COMMON_MONOTONE_CUBIC_SPLINE_HH
25#define DUMUX_COMMON_MONOTONE_CUBIC_SPLINE_HH
33#include <dune/common/float_cmp.hh>
49template<
class Scalar =
double>
75 void updatePoints(
const std::vector<Scalar>& x,
const std::vector<Scalar>& y)
78 assert (x.size() == y.size());
79 assert (x.size() >=2);
80 assert (std::is_sorted(x.begin(), x.end()) || std::is_sorted(x.rbegin(), x.rend()));
87 numPoints_ = x.size();
90 increasingX_ = x_.back() > x_.front();
91 increasingY_ = y_.back() > y_.front();
94 m_.resize(numPoints_);
97 Scalar deltaX = (x[1]-x[0]);
98 Scalar secant = m_.front() = (y[1]-y[0])/deltaX;
99 Scalar prevDeltaX = deltaX;
100 Scalar prevSecant = secant;
101 for (
int i = 1; i < numPoints_-1; ++i, prevSecant = secant, prevDeltaX = deltaX)
103 deltaX = (x[i+1]-x[i]);
104 secant = (y[i+1]-y[i])/deltaX;
105 const auto alpha = (prevDeltaX + 2*deltaX)/(3*(prevDeltaX + deltaX));
106 m_[i] = prevSecant*secant > 0.0 ? prevSecant*secant/(alpha*secant + (1.0-alpha)*prevSecant) : 0.0;
116 Scalar
eval(
const Scalar x)
const
118 if ((x <= x_.front() && increasingX_) || (x >= x_.front() && !increasingX_))
119 return y_.front() + m_.front()*(x - x_.front());
120 else if ((x > x_.back() && increasingX_) || (x < x_.back() && !increasingX_))
121 return y_.back() + m_.back()*(x - x_.back());
133 if ((x <= x_.front() && increasingX_) || (x >= x_.front() && !increasingX_))
135 else if ((x > x_.back() && increasingX_) || (x < x_.back() && !increasingX_))
138 return evalDerivative_(x);
149 if ((y <= y_.front() && increasingY_) || (y >= y_.front() && !increasingY_))
150 return x_.front() + (y - y_.front())/m_.front();
151 else if ((y > y_.back() && increasingY_) || (y < y_.back() && !increasingY_))
152 return x_.back() + (y - y_.back())/m_.back();
154 return evalInverse_(y);
158 Scalar eval_(
const Scalar x)
const
161 const auto lookUpIndex = lookUpIndex_(x_, x, increasingX_);
162 const auto h = (x_[lookUpIndex] - x_[lookUpIndex-1]);
163 const auto t = (x - x_[lookUpIndex-1])/h;
168 Scalar evalDerivative_(
const Scalar x)
const
171 const auto lookUpIndex = lookUpIndex_(x_, x, increasingX_);
172 const auto h = (x_[lookUpIndex] - x_[lookUpIndex-1]);
173 const auto t = (x - x_[lookUpIndex-1])/h;
174 const auto dtdx = 1.0/h;
179 Scalar evalInverse_(
const Scalar y)
const
181 const auto lookUpIndex = lookUpIndex_(y_, y, increasingY_);
182 auto localPolynomial = [&](
const auto x) {
184 const auto h = (x_[lookUpIndex] - x_[lookUpIndex-1]);
185 const auto t = (x - x_[lookUpIndex-1])/h;
191 const auto eps = (x_[lookUpIndex]-x_[lookUpIndex-1])*1e-5;
195 auto lookUpIndex_(
const std::vector<Scalar>& vec,
const Scalar v,
bool increasing)
const
197 return increasing ? lookUpIndexIncreasing_(vec, v) : lookUpIndexDecreasing_(vec, v);
200 auto lookUpIndexIncreasing_(
const std::vector<Scalar>& vec,
const Scalar v)
const
202 const auto lookUpIndex =
std::distance(vec.begin(), std::lower_bound(vec.begin(), vec.end(), v));
203 assert(lookUpIndex != 0 && lookUpIndex < vec.size());
207 auto lookUpIndexDecreasing_(
const std::vector<Scalar>& vec,
const Scalar v)
const
209 const auto lookUpIndex = vec.size() -
std::distance(vec.rbegin(), std::upper_bound(vec.rbegin(), vec.rend(), v));
210 assert(lookUpIndex != 0 && lookUpIndex < vec.size());
214 std::vector<Scalar> x_;
215 std::vector<Scalar> y_;
216 std::vector<Scalar> m_;
217 std::size_t numPoints_;
The cubic hermite spline basis.
Root finding algorithms for scalar functions.
static ctype distance(const Dune::FieldVector< ctype, dimWorld > &a, const Dune::FieldVector< ctype, dimWorld > &b)
Compute the shortest distance between two points.
Definition: distance.hh:292
Scalar findScalarRootBrent(Scalar a, Scalar b, const ResFunc &residual, const Scalar tol=1e-13, const int maxIter=200)
Brent's root finding algorithm for scalar functions.
Definition: findscalarroot.hh:111
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:51
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:75
Scalar evalInverse(const Scalar y) const
Evaluate the inverse function.
Definition: monotonecubicspline.hh:147
Scalar eval(const Scalar x) const
Evaluate the y value at a given x value.
Definition: monotonecubicspline.hh:116
Scalar evalDerivative(const Scalar x) const
Evaluate the first derivative dy/dx at a given x value.
Definition: monotonecubicspline.hh:131
MonotoneCubicSpline(const std::vector< Scalar > &x, const std::vector< Scalar > &y)
Construct a monotone cubic spline from the control points (x[i], y[i])
Definition: monotonecubicspline.hh:65