SplineInterpolation.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14
15 #include "SplineInterpolation.h"
16
17 // MOOSE includes
18 #include "MooseError.h"
19
20 // C++ includes
21 #include <fstream>
22
24
26
27 SplineInterpolation::SplineInterpolation(const std::vector<Real> & x,
28  const std::vector<Real> & y,
29  Real yp1 /* = _deriv_bound*/,
30  Real ypn /* = _deriv_bound*/)
31  : SplineInterpolationBase(), _x(x), _y(y), _yp1(yp1), _ypn(ypn)
32 {
33  errorCheck();
34  solve();
35 }
36
37 void
38 SplineInterpolation::setData(const std::vector<Real> & x,
39  const std::vector<Real> & y,
40  Real yp1 /* = _deriv_bound*/,
41  Real ypn /* = _deriv_bound*/)
42 {
43  _x = x;
44  _y = y;
45  _yp1 = yp1;
46  _ypn = ypn;
47  errorCheck();
48  solve();
49 }
50
51 void
53 {
54  if (_x.size() != _y.size())
55  mooseError("SplineInterpolation: vectors are not the same length");
56
57  bool error = false;
58  for (unsigned i = 0; !error && i + 1 < _x.size(); ++i)
59  if (_x[i] >= _x[i + 1])
60  error = true;
61
62  if (error)
63  mooseError("x-values are not strictly increasing");
64 }
65
66 void
68 {
69  spline(_x, _y, _y2, _yp1, _ypn);
70 }
71
72 Real
74 {
76 }
77
78 Real
80 {
82 }
83
84 Real
86 {
88 }
89
90 Real
92 {
93  return _x[i];
94 }
95
96 Real
98 {
99  return _y[i];
100 }
101
102 void
104  std::string x_label,
105  std::string y_label,
106  float xmin,
107  float xmax,
108  float ymin,
109  float ymax)
110 {
111  std::stringstream filename, filename_pts;
112  const unsigned char fill_character = '0';
113  const unsigned int field_width = 4;
114
115  filename.fill(fill_character);
116  filename << base_name;
117  filename.width(field_width);
118  filename << _file_number << ".plt";
119
120  filename_pts.fill(fill_character);
121  filename_pts << base_name << "_pts";
122  filename_pts.width(field_width);
123  filename_pts << _file_number << ".dat";
124
125  /* First dump the GNUPLOT file with the Piecewise Linear Equations */
126  std::ofstream out(filename.str().c_str());
127  out.precision(15);
128  out.fill(fill_character);
129
130  out << "set terminal postscript color enhanced\n"
131  << "set output \"" << base_name;
132  out.width(field_width);
133  out << _file_number << ".eps\"\n"
134  << "set xlabel \"" << x_label << "\"\n"
135  << "set ylabel \"" << y_label << "\"\n";
136  if (xmin != 0 && xmax != 0)
137  out << "set xrange [" << xmin << ":" << xmax << "]\n";
138  if (ymin != 0 && ymax != 0)
139  out << "set yrange [" << ymin << ":" << ymax << "]\n";
140  out << "set key left top\n"
141  << "f(x)=";
142
143  for (unsigned int i = 1; i < _x.size(); ++i)
144  {
145  Real m = (_y[i] - _y[i - 1]) / (_x[i] - _x[i - 1]);
146  Real b = (_y[i] - m * _x[i]);
147
148  out << _x[i - 1] << "<=x && x<" << _x[i] << " ? " << m << "*x+(" << b << ") : ";
149  }
150  out << " 1/0\n";
151
152  out << "\nplot f(x) with lines, '" << filename_pts.str() << "' using 1:2 title \"Points\"\n";
153  out.close();
154
155  libmesh_assert(_x.size() == _y.size());
156
157  out.open(filename_pts.str().c_str());
158  /* Next dump the data points into a seperate file */
159  for (unsigned int i = 0; i < _x.size(); ++i)
160  out << _x[i] << " " << _y[i] << "\n";
161  out << std::endl;
162
163  ++_file_number;
164  out.close();
165 }
166
167 unsigned int
169 {
170  return _x.size();
171 }
Real range(int i) const
Real _yp1
boundary conditions
Real sample2ndDerivative(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
Real sample(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
static PetscErrorCode Vec x
Real domain(int i) const
void setData(const std::vector< Real > &x, const std::vector< Real > &y, Real yp1=_deriv_bound, Real ypn=_deriv_bound)
Set the x-, y- values and first derivatives.
PetscInt m
unsigned int getSampleSize()
This function returns the size of the array holding the points, i.e.
std::vector< Real > _y2
Second derivatives.
std::vector< Real > _x
Real sample(Real x) const
This function will take an independent variable input and will return the dependent variable based on...
std::vector< Real > _y
Real sample2ndDerivative(Real x) const
void dumpSampleFile(std::string base_name, std::string x_label="X", std::string y_label="Y", float xmin=0, float xmax=0, float ymin=0, float ymax=0)
This function will dump GNUPLOT input files that can be run to show the data points and function fits...
void spline(const std::vector< Real > &x, const std::vector< Real > &y, std::vector< Real > &y2, Real yp1=_deriv_bound, Real ypn=_deriv_bound)
This function calculates the second derivatives based on supplied x and y-vectors.
Real sampleDerivative(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &y2, Real x_int) const
Real sampleDerivative(Real x) const