www.mooseframework.org
LeastSquaresFit.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 */
6 /* ALL RIGHTS RESERVED */
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 "LeastSquaresFit.h"
17 #include "PolynomialFit.h"
18 
19 template <>
22 {
24 
25  params.addRequiredParam<VectorPostprocessorName>(
26  "vectorpostprocessor",
27  "The vectorpostprocessor on whose values we perform a least squares fit");
28  params.addRequiredParam<std::string>("x_name", "The name of the independent variable");
29  params.addRequiredParam<std::string>("y_name", "The name of the dependent variable");
30  params.addRequiredParam<unsigned int>("order", "The order of the polynomial fit");
31  params.addParam<unsigned int>("num_samples", "The number of samples to be output");
32  params.addParam<Real>(
33  "x_scale", 1.0, "Value used to scale x values (scaling is done after shifting)");
34  params.addParam<Real>(
35  "x_shift", 0.0, "Value used to shift x values (shifting is done before scaling)");
36  params.addParam<Real>(
37  "y_scale", 1.0, "Value used to scale y values (scaling is done after shifting)");
38  params.addParam<Real>(
39  "y_shift", 0.0, "Value used to shift y values (shifting is done before scaling)");
40  params.addParam<Real>("sample_x_min", "The minimum x value of the of samples to be output");
41  params.addParam<Real>("sample_x_max", "The maximum x value of the of samples to be output");
42  MooseEnum output_type("Coefficients Samples", "Coefficients");
43  params.addParam<MooseEnum>(
44  "output", output_type, "The quantity to output. Options are: " + output_type.getRawNames());
45  params.addClassDescription("Performs a polynomial least squares fit on the data contained in "
46  "another VectorPostprocessor");
47 
48  return params;
49 }
50 
52  : GeneralVectorPostprocessor(parameters),
53  _vpp_name(getParam<VectorPostprocessorName>("vectorpostprocessor")),
54  _order(parameters.get<unsigned int>("order")),
55  _x_name(getParam<std::string>("x_name")),
56  _y_name(getParam<std::string>("y_name")),
57  _x_values(getVectorPostprocessorValue("vectorpostprocessor", _x_name)),
58  _y_values(getVectorPostprocessorValue("vectorpostprocessor", _y_name)),
59  _output_type(getParam<MooseEnum>("output")),
60  _num_samples(0),
61  _x_scale(parameters.get<Real>("x_scale")),
62  _x_shift(parameters.get<Real>("x_shift")),
63  _y_scale(parameters.get<Real>("y_scale")),
64  _y_shift(parameters.get<Real>("y_shift")),
65  _have_sample_x_min(isParamValid("sample_x_min")),
66  _have_sample_x_max(isParamValid("sample_x_max")),
67  _sample_x(NULL),
68  _sample_y(NULL),
69  _coeffs(NULL)
70 {
71  if (_output_type == "Samples")
72  {
73  if (isParamValid("num_samples"))
74  _num_samples = getParam<unsigned int>("num_samples");
75  else
76  mooseError("In LeastSquaresFit num_samples parameter must be provided with output=Samples");
77 
79  _sample_x_min = getParam<Real>("sample_x_min");
81  _sample_x_max = getParam<Real>("sample_x_max");
82 
85  }
86  else
87  {
88  if (isParamValid("num_samples"))
89  mooseWarning("In LeastSquaresFit num_samples parameter is unused with output=Coefficients");
90  _coeffs = &declareVector("coefficients");
91  }
92 
93  if (_output_type == "Samples")
94  {
95  _sample_x->resize(_num_samples);
96  _sample_y->resize(_num_samples);
97  }
98  else
99  _coeffs->resize(_order + 1);
100 }
101 
102 void
104 {
105  if (_output_type == "Samples")
106  {
107  _sample_x->clear();
108  _sample_y->clear();
109  }
110  else
111  _coeffs->clear();
112 }
113 
114 void
116 {
117  if (_x_values.size() != _y_values.size())
118  mooseError("In LeastSquresFit size of data in x_values and y_values must be equal");
119  if (_x_values.size() == 0)
120  mooseError("In LeastSquresFit size of data in x_values and y_values must be > 0");
121 
122  // Create a copy of _x_values that we can modify.
123  std::vector<Real> x_values(_x_values.begin(), _x_values.end());
124  std::vector<Real> y_values(_y_values.begin(), _y_values.end());
125 
126  for (auto i = beginIndex(_x_values); i < _x_values.size(); ++i)
127  {
128  x_values[i] = (x_values[i] + _x_shift) * _x_scale;
129  y_values[i] = (y_values[i] + _y_shift) * _y_scale;
130  }
131 
132  PolynomialFit pf(x_values, y_values, _order, true);
133  pf.generate();
134 
135  if (_output_type == "Samples")
136  {
137  Real x_min;
138  if (_have_sample_x_min)
139  x_min = _sample_x_min;
140  else
141  x_min = *(std::min_element(x_values.begin(), x_values.end()));
142 
143  Real x_max;
144  if (_have_sample_x_max)
145  x_max = _sample_x_max;
146  else
147  x_max = *(std::max_element(x_values.begin(), x_values.end()));
148 
149  Real x_span = x_max - x_min;
150 
151  for (unsigned int i = 0; i < _num_samples; ++i)
152  {
153  Real x = x_min + static_cast<Real>(i) / _num_samples * x_span;
154  _sample_x->push_back(x);
155  _sample_y->push_back(pf.sample(x));
156  }
157  }
158  else
159  *_coeffs = pf.getCoefficients();
160 }
const Real _y_shift
void mooseWarning(Args &&...args) const
Definition: MooseObject.h:89
const std::string _x_name
The name of the variables storing the x, y data.
InputParameters validParams< LeastSquaresFit >()
const Real _y_scale
VectorPostprocessorValue * _sample_x
The variables used to write out samples of the least squares fit.
const VectorPostprocessorValue & _x_values
The variables with the x, y data to be fit.
This class is here to combine the VectorPostprocessor interface and the base class VectorPostprocesso...
const VectorPostprocessorValue & _y_values
const std::vector< Real > & getCoefficients()
Get a const reference to the coefficients of the least squares fit.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
VectorPostprocessorValue * _sample_y
unsigned int _num_samples
The number of samples to be taken.
static PetscErrorCode Vec x
LeastSquaresFit(const InputParameters &parameters)
Class constructor.
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
virtual void initialize() override
Initialize, clears old results.
const std::string _y_name
const Real _x_shift
const MooseEnum _output_type
The type of output.
void generate()
This function generates the polynomial fit.
Definition: PolynomialFit.C:48
bool _have_sample_x_min
Did the user specify the min and max x values for sampling?
InputParameters validParams< GeneralVectorPostprocessor >()
const Real _x_scale
Values used to scale and or shift x and y data.
Real _sample_x_min
The min and max x values for sampling.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:37
This class applies the Least Squares algorithm to a set of points to provide a smooth curve for sampl...
Definition: PolynomialFit.h:29
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:67
virtual void execute() override
Perform the least squares fit.
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
std::string getRawNames() const
Method for returning the raw name strings for this instance.
unsigned int _order
The order of the polynomial fit to be performed.
VectorPostprocessorValue * _coeffs
The variable used to write out the coefficients of the fit.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
Real sample(Real x)
This function will take an independent variable input and will return the dependent variable based on...