OGS
MathLib::PiecewiseLinearMonotonicCurve Class Referencefinal

Detailed Description

Class for strong monotonic curves.

Definition at line 20 of file PiecewiseLinearMonotonicCurve.h.

#include <PiecewiseLinearMonotonicCurve.h>

Inheritance diagram for MathLib::PiecewiseLinearMonotonicCurve:
[legend]
Collaboration diagram for MathLib::PiecewiseLinearMonotonicCurve:
[legend]

Public Member Functions

 PiecewiseLinearMonotonicCurve (std::vector< double > x, std::vector< double > y)
 
 ~PiecewiseLinearMonotonicCurve ()=default
 
double getInverseVariable (const double y) const
 
- Public Member Functions inherited from MathLib::PiecewiseLinearInterpolation
 PiecewiseLinearInterpolation (std::vector< double > supporting_points, std::vector< double > values_at_supp_pnts, bool supp_pnts_sorted=false)
 
double getValue (double pnt_to_interpolate) const
 Calculates the interpolation value. More...
 
double getDerivative (double const pnt_to_interpolate) const
 Calculates derivative using quadratic interpolation and central difference quotient. More...
 
double getSupportMax () const
 
double getSupportMin () const
 

Private Member Functions

bool isStrongMonotonic () const
 

Additional Inherited Members

- Protected Attributes inherited from MathLib::PiecewiseLinearInterpolation
std::vector< double > supp_pnts_
 
std::vector< double > values_at_supp_pnts_
 

Constructor & Destructor Documentation

◆ PiecewiseLinearMonotonicCurve()

MathLib::PiecewiseLinearMonotonicCurve::PiecewiseLinearMonotonicCurve ( std::vector< double >  x,
std::vector< double >  y 
)

@ x x coordinates of curve points @ y y coordinates of curve points

Definition at line 23 of file PiecewiseLinearMonotonicCurve.cpp.

25  : PiecewiseLinearInterpolation(std::move(x), std::move(y), false)
26 {
27  if (!isStrongMonotonic())
28  {
29  OGS_FATAL("The given curve is not strong monotonic.");
30  }
31 }
#define OGS_FATAL(...)
Definition: Error.h:26
PiecewiseLinearInterpolation(std::vector< double > supporting_points, std::vector< double > values_at_supp_pnts, bool supp_pnts_sorted=false)

References isStrongMonotonic(), and OGS_FATAL.

◆ ~PiecewiseLinearMonotonicCurve()

MathLib::PiecewiseLinearMonotonicCurve::~PiecewiseLinearMonotonicCurve ( )
default

Member Function Documentation

◆ getInverseVariable()

double MathLib::PiecewiseLinearMonotonicCurve::getInverseVariable ( const double  y) const

Get variable, x, or abscissa, by a given value y, the ordinate. If this curve is not monotonic, this function gives a fatal error.

Definition at line 47 of file PiecewiseLinearMonotonicCurve.cpp.

48 {
49  std::size_t interval_idx = 0;
50  if (values_at_supp_pnts_.front() < values_at_supp_pnts_.back())
51  {
52  if (y <= values_at_supp_pnts_.front())
53  {
54  return supp_pnts_[0];
55  }
56  if (y >= values_at_supp_pnts_.back())
57  {
58  return supp_pnts_[supp_pnts_.size() - 1];
59  }
60 
61  // search interval that has the point inside
62  auto const& it(std::lower_bound(values_at_supp_pnts_.begin(),
63  values_at_supp_pnts_.end(), y));
64  interval_idx = std::distance(values_at_supp_pnts_.begin(), it) - 1;
65  }
66  else
67  {
68  if (y >= values_at_supp_pnts_.front())
69  {
70  return supp_pnts_[0];
71  }
72  if (y <= values_at_supp_pnts_.back())
73  {
74  return supp_pnts_[supp_pnts_.size() - 1];
75  }
76 
77  // search interval in the reverse direction for the point inside
78  auto const& it(std::lower_bound(values_at_supp_pnts_.rbegin(),
79  values_at_supp_pnts_.rend(), y));
80  interval_idx = values_at_supp_pnts_.size() -
81  (std::distance(values_at_supp_pnts_.rbegin(), it)) - 1;
82  }
83 
84  const double xi_1 = supp_pnts_[interval_idx + 1];
85  const double xi = supp_pnts_[interval_idx];
86  const double yi_1 = values_at_supp_pnts_[interval_idx + 1];
87  const double yi = values_at_supp_pnts_[interval_idx];
88 
89  // compute gradient: m = (x_{i+1} - x_i) / (y_{i+1} - y_i)
90  const double m = (xi_1 - xi) / (yi_1 - yi);
91 
92  // compute the variable by linear interpolation: x = m * (y - y_i) + x_i,
93  // and then return the result.
94  return m * (y - yi) + xi;
95 }

References MathLib::PiecewiseLinearInterpolation::supp_pnts_, and MathLib::PiecewiseLinearInterpolation::values_at_supp_pnts_.

◆ isStrongMonotonic()

bool MathLib::PiecewiseLinearMonotonicCurve::isStrongMonotonic ( ) const
private

Check monotonicity of this curve such as for any \(i\), \(y(x_{i+1})>y(x_{i})\) if \(x_{i+1}>x_{i}\) or for any \(i\) \(y(x_{i+1})<y(x_{i})\) if \(x_{i+1}>x_{i}\)

Returns
True if the curve exhibits strong monotonic.

Definition at line 33 of file PiecewiseLinearMonotonicCurve.cpp.

34 {
35  const double gradient0 = getDerivative(supp_pnts_[0]);
36 
37  if (std::abs(gradient0) < std::numeric_limits<double>::min())
38  {
39  return false;
40  }
41 
42  return std::none_of(supp_pnts_.begin(), supp_pnts_.end(),
43  [&](const double p)
44  { return this->getDerivative(p) * gradient0 <= 0.; });
45 }
double getDerivative(double const pnt_to_interpolate) const
Calculates derivative using quadratic interpolation and central difference quotient.
static const double p

References MathLib::PiecewiseLinearInterpolation::getDerivative(), MathLib::p, and MathLib::PiecewiseLinearInterpolation::supp_pnts_.

Referenced by PiecewiseLinearMonotonicCurve().


The documentation for this class was generated from the following files: