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 26 of file LinearInterpolation.h.

Constructor & Destructor Documentation

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

Definition at line 23 of file LinearInterpolation.C.

24  : _x(x), _y(y)
25 {
26  errorCheck();
27 }
std::vector< Real > _x
std::vector< Real > _y
static PetscErrorCode Vec x
LinearInterpolation::LinearInterpolation ( )
inline

Definition at line 34 of file LinearInterpolation.h.

34 : _x(std::vector<Real>()), _y(std::vector<Real>()) {}
std::vector< Real > _x
std::vector< Real > _y
virtual LinearInterpolation::~LinearInterpolation ( )
virtualdefault

Referenced by LinearInterpolation().

Member Function Documentation

Real LinearInterpolation::domain ( int  i) const

Definition at line 94 of file LinearInterpolation.C.

Referenced by setData().

95 {
96  return _x[i];
97 }
std::vector< Real > _x
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 106 of file LinearInterpolation.C.

Referenced by setData().

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

Definition at line 30 of file LinearInterpolation.C.

Referenced by LinearInterpolation(), and setData().

31 {
32  if (_x.size() != _y.size())
33  throw std::domain_error("Vectors are not the same length");
34 
35  for (unsigned int i = 0; i + 1 < _x.size(); ++i)
36  if (_x[i] >= _x[i + 1])
37  {
38  std::ostringstream oss;
39  oss << "x-values are not strictly increasing: x[" << i << "]: " << _x[i] << " x[" << i + 1
40  << "]: " << _x[i + 1];
41  throw std::domain_error(oss.str());
42  }
43 }
std::vector< Real > _x
std::vector< Real > _y
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 171 of file LinearInterpolation.C.

Referenced by setData().

172 {
173  return _x.size();
174 }
std::vector< Real > _x
Real LinearInterpolation::integrate ( )

This function returns the integral of the function.

Definition at line 84 of file LinearInterpolation.C.

Referenced by setData().

85 {
86  Real answer = 0;
87  for (unsigned int i = 1; i < _x.size(); ++i)
88  answer += 0.5 * (_y[i] + _y[i - 1]) * (_x[i] - _x[i - 1]);
89 
90  return answer;
91 }
std::vector< Real > _x
std::vector< Real > _y
Real LinearInterpolation::range ( int  i) const

Definition at line 100 of file LinearInterpolation.C.

Referenced by setData().

101 {
102  return _y[i];
103 }
std::vector< Real > _y
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 46 of file LinearInterpolation.C.

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

47 {
48  // sanity check (empty LinearInterpolations get constructed in many places
49  // so we cannot put this into the errorCheck)
50  assert(_x.size() > 0);
51 
52  // endpoint cases
53  if (x <= _x[0])
54  return _y[0];
55  if (x >= _x.back())
56  return _y.back();
57 
58  for (unsigned int i = 0; i + 1 < _x.size(); ++i)
59  if (x >= _x[i] && x < _x[i + 1])
60  return _y[i] + (_y[i + 1] - _y[i]) * (x - _x[i]) / (_x[i + 1] - _x[i]);
61 
62  throw std::out_of_range("Unreachable");
63  return 0;
64 }
std::vector< Real > _x
std::vector< Real > _y
static PetscErrorCode Vec x
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 67 of file LinearInterpolation.C.

Referenced by setData().

68 {
69  // endpoint cases
70  if (x < _x[0])
71  return 0.0;
72  if (x >= _x[_x.size() - 1])
73  return 0.0;
74 
75  for (unsigned int i = 0; i + 1 < _x.size(); ++i)
76  if (x >= _x[i] && x < _x[i + 1])
77  return (_y[i + 1] - _y[i]) / (_x[i + 1] - _x[i]);
78 
79  throw std::out_of_range("Unreachable");
80  return 0;
81 }
std::vector< Real > _x
std::vector< Real > _y
static PetscErrorCode Vec x
void LinearInterpolation::setData ( const std::vector< Real > &  X,
const std::vector< Real > &  Y 
)
inline

Set the x and y values.

Definition at line 41 of file LinearInterpolation.h.

42  {
43  _x = X;
44  _y = Y;
45  errorCheck();
46  }
std::vector< Real > _x
std::vector< Real > _y

Member Data Documentation

int LinearInterpolation::_file_number = 0
staticprivate

Definition at line 93 of file LinearInterpolation.h.

Referenced by dumpSampleFile().

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

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