12#ifndef DUMUX_COMMON_CUBIC_SPLINE_HH
13#define DUMUX_COMMON_CUBIC_SPLINE_HH
19#include <dune/common/fvector.hh>
20#include <dune/common/fmatrix.hh>
21#include <dune/istl/btdmatrix.hh>
22#include <dune/istl/bvector.hh>
31template<
class Scalar =
double>
45 CubicSpline(
const std::vector<Scalar>& x,
const std::vector<Scalar> y)
48 assert (x.size() == y.size());
49 assert (x.size() >=2);
50 assert (std::is_sorted(x.begin(), x.end()));
61 void updatePoints(
const std::vector<Scalar>& x,
const std::vector<Scalar>& y)
67 numPoints_ = x.size();
69 const auto numSegments = numPoints_-1;
70 coeff_.resize(numSegments*4+2);
73 Dune::BTDMatrix<Dune::FieldMatrix<double, 1, 1>> matrix(numPoints_);
74 Dune::BlockVector<Dune::FieldVector<double, 1>> rhs(numPoints_);
75 Dune::BlockVector<Dune::FieldVector<double, 1>> d(numPoints_);
80 rhs[0] = 3.0*(y[1] - y[0]);
81 for (
int i = 1; i < numPoints_-1; ++i)
86 rhs[i] = 3.0*(y[i+1] - y[i-1]);
88 matrix[numPoints_-1][numPoints_-1] = 2.0;
89 matrix[numPoints_-1][numPoints_-2] = 1.0;
90 rhs[numPoints_-1] = 3.0*(y[numPoints_-1] - y[numPoints_-2]);
96 std::size_t offset = 0;
97 for (
int i = 0; i < numSegments; ++i, offset += 4)
99 coeff_[offset+0] = y[i];
100 coeff_[offset+1] = d[i];
101 coeff_[offset+2] = 3.0*(y[i+1]-y[i]) - 2.0*d[i] - d[i+1];
102 coeff_[offset+3] = 2.0*(y[i]-y[i+1]) + d[i] + d[i+1];
104 coeff_[offset+0] = y[numPoints_-1];
105 coeff_[offset+1] = d[numPoints_-1];
113 Scalar
eval(
const Scalar x)
const
117 else if (x > x_[numPoints_-1])
118 return coeff_[(numPoints_-1)*4] +
evalDerivative(x_[numPoints_-1])*(x - x_[numPoints_-1]);
121 const auto lookUpIndex =
std::distance(x_.begin(), std::lower_bound(x_.begin(), x_.end(), x));
122 assert(lookUpIndex != 0);
125 const auto* coeff = coeff_.data() + (lookUpIndex-1)*4;
128 const auto t = (x - x_[lookUpIndex-1])/(x_[lookUpIndex] - x_[lookUpIndex-1]);
129 return coeff[0] + t*(coeff[1] + t*coeff[2] + t*t*coeff[3]);
141 return coeff_[1]/(x_[1] - x_[0]);
142 else if (x > x_[numPoints_-1])
143 return coeff_[(numPoints_-1)*4 + 1]/(x_[numPoints_-1] - x_[numPoints_-2]);
146 const auto lookUpIndex =
std::distance(x_.begin(), std::lower_bound(x_.begin(), x_.end(), x));
147 assert(lookUpIndex != 0);
150 const auto* coeff = coeff_.data() + (lookUpIndex-1)*4;
153 const auto t = (x - x_[lookUpIndex-1])/(x_[lookUpIndex] - x_[lookUpIndex-1]);
154 const auto dtdx = 1.0/(x_[lookUpIndex] - x_[lookUpIndex-1]);
155 return dtdx*(coeff[1] + t*(2.0*coeff[2] + t*3.0*coeff[3]));
160 std::vector<Scalar> x_;
161 std::size_t numPoints_;
162 std::vector<Scalar> coeff_;
A simple implementation of a natural cubic spline.
Definition: cubicspline.hh:33
CubicSpline(const std::vector< Scalar > &x, const std::vector< Scalar > y)
Construct a natural cubic spline from the control points (x[i], y[i])
Definition: cubicspline.hh:45
CubicSpline()=default
Default constructor.
Scalar eval(const Scalar x) const
Evaluate the y value at a given x value.
Definition: cubicspline.hh:113
Scalar evalDerivative(const Scalar x) const
Evaluate the first derivative dy/dx at a given x value.
Definition: cubicspline.hh:138
void updatePoints(const std::vector< Scalar > &x, const std::vector< Scalar > &y)
Create a natural cubic spline from the control points (x[i], y[i])
Definition: cubicspline.hh:61
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:282