www.mooseframework.org
Public Member Functions | Private Attributes | Static Private Attributes | List of all members
LinearInterpolation Class Reference

This class interpolates values given a set of data pairs and an abscissa. More...

#include <LinearInterpolation.h>

Public Member Functions

 LinearInterpolation (const std::vector< Real > &X, const std::vector< Real > &Y)
 
 LinearInterpolation ()
 
virtual ~LinearInterpolation ()=default
 
void setData (const std::vector< Real > &X, const std::vector< Real > &Y)
 Set the x and y values. More...
 
void errorCheck ()
 
Real sample (Real x) const
 This function will take an independent variable input and will return the dependent variable based on the generated fit. More...
 
Real sampleDerivative (Real x) const
 This function will take an independent variable input and will return the derivative of the dependent variable with respect to the independent variable based on the generated fit. More...
 
void dumpSampleFile (std::string base_name, std::string x_label="X", std::string y_label="Y", Real xmin=0, Real xmax=0, Real ymin=0, Real ymax=0)
 This function will dump GNUPLOT input files that can be run to show the data points and function fits. More...
 
unsigned int getSampleSize ()
 This function returns the size of the array holding the points, i.e. More...
 
Real integrate ()
 This function returns the integral of the function. More...
 
Real domain (int i) const
 
Real range (int i) const
 

Private Attributes

std::vector< Real > _x
 
std::vector< Real > _y
 

Static Private Attributes

static int _file_number = 0
 

Detailed Description

This class interpolates values given a set of data pairs and an abscissa.

Definition at line 21 of file LinearInterpolation.h.

Constructor & Destructor Documentation

◆ LinearInterpolation() [1/2]

LinearInterpolation::LinearInterpolation ( const std::vector< Real > &  X,
const std::vector< Real > &  Y 
)

Definition at line 18 of file LinearInterpolation.C.

19  : _x(x), _y(y)
20 {
21  errorCheck();
22 }
std::vector< Real > _x
std::vector< Real > _y
static PetscErrorCode Vec x

◆ LinearInterpolation() [2/2]

LinearInterpolation::LinearInterpolation ( )
inline

Definition at line 29 of file LinearInterpolation.h.

29 : _x(std::vector<Real>()), _y(std::vector<Real>()) {}
std::vector< Real > _x
std::vector< Real > _y

◆ ~LinearInterpolation()

virtual LinearInterpolation::~LinearInterpolation ( )
virtualdefault

Member Function Documentation

◆ domain()

Real LinearInterpolation::domain ( int  i) const

Definition at line 89 of file LinearInterpolation.C.

90 {
91  return _x[i];
92 }
std::vector< Real > _x

◆ dumpSampleFile()

void LinearInterpolation::dumpSampleFile ( std::string  base_name,
std::string  x_label = "X",
std::string  y_label = "Y",
Real  xmin = 0,
Real  xmax = 0,
Real  ymin = 0,
Real  ymax = 0 
)

This function will dump GNUPLOT input files that can be run to show the data points and function fits.

Definition at line 101 of file LinearInterpolation.C.

108 {
109  std::stringstream filename, filename_pts;
110  const unsigned char fill_character = '0';
111  const unsigned int field_width = 4;
112 
113  filename.fill(fill_character);
114  filename << base_name;
115  filename.width(field_width);
116  filename << _file_number << ".plt";
117 
118  filename_pts.fill(fill_character);
119  filename_pts << base_name << "_pts";
120  filename_pts.width(field_width);
121  filename_pts << _file_number << ".dat";
122 
123  /* First dump the GNUPLOT file with the Piecewise Linear Equations */
124  std::ofstream out(filename.str().c_str());
125  out.precision(15);
126  out.fill(fill_character);
127 
128  out << "set terminal postscript color enhanced\n"
129  << "set output \"" << base_name;
130  out.width(field_width);
131  out << _file_number << ".eps\"\n"
132  << "set xlabel \"" << x_label << "\"\n"
133  << "set ylabel \"" << y_label << "\"\n";
134  if (xmin != 0 && xmax != 0)
135  out << "set xrange [" << xmin << ":" << xmax << "]\n";
136  if (ymin != 0 && ymax != 0)
137  out << "set yrange [" << ymin << ":" << ymax << "]\n";
138  out << "set key left top\n"
139  << "f(x)=";
140 
141  for (unsigned int i = 1; i < _x.size(); ++i)
142  {
143  Real m = (_y[i] - _y[i - 1]) / (_x[i] - _x[i - 1]);
144  Real b = (_y[i] - m * _x[i]);
145 
146  out << _x[i - 1] << "<=x && x<" << _x[i] << " ? " << m << "*x+(" << b << ") : ";
147  }
148  out << " 1/0\n";
149 
150  out << "\nplot f(x) with lines, '" << filename_pts.str() << "' using 1:2 title \"Points\"\n";
151  out.close();
152 
153  assert(_x.size() == _y.size());
154 
155  out.open(filename_pts.str().c_str());
156  /* Next dump the data points into a separate file */
157  for (unsigned int i = 0; i < _x.size(); ++i)
158  out << _x[i] << " " << _y[i] << "\n";
159  out << std::endl;
160 
161  ++_file_number;
162  out.close();
163 }
std::vector< Real > _x
std::vector< Real > _y
PetscInt m

◆ errorCheck()

void LinearInterpolation::errorCheck ( )

Definition at line 25 of file LinearInterpolation.C.

Referenced by LinearInterpolation(), and setData().

26 {
27  if (_x.size() != _y.size())
28  throw std::domain_error("Vectors are not the same length");
29 
30  for (unsigned int i = 0; i + 1 < _x.size(); ++i)
31  if (_x[i] >= _x[i + 1])
32  {
33  std::ostringstream oss;
34  oss << "x-values are not strictly increasing: x[" << i << "]: " << _x[i] << " x[" << i + 1
35  << "]: " << _x[i + 1];
36  throw std::domain_error(oss.str());
37  }
38 }
std::vector< Real > _x
std::vector< Real > _y

◆ getSampleSize()

unsigned int LinearInterpolation::getSampleSize ( )

This function returns the size of the array holding the points, i.e.

the number of sample points

Definition at line 166 of file LinearInterpolation.C.

167 {
168  return _x.size();
169 }
std::vector< Real > _x

◆ integrate()

Real LinearInterpolation::integrate ( )

This function returns the integral of the function.

Definition at line 79 of file LinearInterpolation.C.

80 {
81  Real answer = 0;
82  for (unsigned int i = 1; i < _x.size(); ++i)
83  answer += 0.5 * (_y[i] + _y[i - 1]) * (_x[i] - _x[i - 1]);
84 
85  return answer;
86 }
std::vector< Real > _x
std::vector< Real > _y

◆ range()

Real LinearInterpolation::range ( int  i) const

Definition at line 95 of file LinearInterpolation.C.

96 {
97  return _y[i];
98 }
std::vector< Real > _y

◆ sample()

Real LinearInterpolation::sample ( Real  x) const

This function will take an independent variable input and will return the dependent variable based on the generated fit.

Definition at line 41 of file LinearInterpolation.C.

Referenced by IterationAdaptiveDT::computeDT(), and IterationAdaptiveDT::computeInterpolationDT().

42 {
43  // sanity check (empty LinearInterpolations get constructed in many places
44  // so we cannot put this into the errorCheck)
45  assert(_x.size() > 0);
46 
47  // endpoint cases
48  if (x <= _x[0])
49  return _y[0];
50  if (x >= _x.back())
51  return _y.back();
52 
53  for (unsigned int i = 0; i + 1 < _x.size(); ++i)
54  if (x >= _x[i] && x < _x[i + 1])
55  return _y[i] + (_y[i + 1] - _y[i]) * (x - _x[i]) / (_x[i + 1] - _x[i]);
56 
57  throw std::out_of_range("Unreachable");
58  return 0;
59 }
std::vector< Real > _x
std::vector< Real > _y
static PetscErrorCode Vec x

◆ sampleDerivative()

Real LinearInterpolation::sampleDerivative ( Real  x) const

This function will take an independent variable input and will return the derivative of the dependent variable with respect to the independent variable based on the generated fit.

Definition at line 62 of file LinearInterpolation.C.

63 {
64  // endpoint cases
65  if (x < _x[0])
66  return 0.0;
67  if (x >= _x[_x.size() - 1])
68  return 0.0;
69 
70  for (unsigned int i = 0; i + 1 < _x.size(); ++i)
71  if (x >= _x[i] && x < _x[i + 1])
72  return (_y[i + 1] - _y[i]) / (_x[i + 1] - _x[i]);
73 
74  throw std::out_of_range("Unreachable");
75  return 0;
76 }
std::vector< Real > _x
std::vector< Real > _y
static PetscErrorCode Vec x

◆ setData()

void LinearInterpolation::setData ( const std::vector< Real > &  X,
const std::vector< Real > &  Y 
)
inline

Set the x and y values.

Definition at line 36 of file LinearInterpolation.h.

37  {
38  _x = X;
39  _y = Y;
40  errorCheck();
41  }
std::vector< Real > _x
std::vector< Real > _y

Member Data Documentation

◆ _file_number

int LinearInterpolation::_file_number = 0
staticprivate

Definition at line 88 of file LinearInterpolation.h.

Referenced by dumpSampleFile().

◆ _x

std::vector<Real> LinearInterpolation::_x
private

◆ _y

std::vector<Real> LinearInterpolation::_y
private

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