www.mooseframework.org
LeastSquaresFit.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "LeastSquaresFit.h"
12 #include "PolynomialFit.h"
13 
15 
16 template <>
19 {
21 
22  params.addRequiredParam<VectorPostprocessorName>(
23  "vectorpostprocessor",
24  "The vectorpostprocessor on whose values we perform a least squares fit");
25  params.addRequiredParam<std::string>("x_name", "The name of the independent variable");
26  params.addRequiredParam<std::string>("y_name", "The name of the dependent variable");
27  params.addRequiredParam<unsigned int>("order", "The order of the polynomial fit");
28  params.addParam<unsigned int>("num_samples", "The number of samples to be output");
29  params.addParam<Real>(
30  "x_scale", 1.0, "Value used to scale x values (scaling is done after shifting)");
31  params.addParam<Real>(
32  "x_shift", 0.0, "Value used to shift x values (shifting is done before scaling)");
33  params.addParam<Real>(
34  "y_scale", 1.0, "Value used to scale y values (scaling is done after shifting)");
35  params.addParam<Real>(
36  "y_shift", 0.0, "Value used to shift y values (shifting is done before scaling)");
37  params.addParam<Real>("sample_x_min", "The minimum x value of the of samples to be output");
38  params.addParam<Real>("sample_x_max", "The maximum x value of the of samples to be output");
39  MooseEnum output_type("Coefficients Samples", "Coefficients");
40  params.addParam<MooseEnum>(
41  "output", output_type, "The quantity to output. Options are: " + output_type.getRawNames());
42  params.addClassDescription("Performs a polynomial least squares fit on the data contained in "
43  "another VectorPostprocessor");
44 
45  return params;
46 }
47 
49  : GeneralVectorPostprocessor(parameters),
50  _vpp_name(getParam<VectorPostprocessorName>("vectorpostprocessor")),
51  _order(parameters.get<unsigned int>("order")),
52  _x_name(getParam<std::string>("x_name")),
53  _y_name(getParam<std::string>("y_name")),
54  _x_values(getVectorPostprocessorValue("vectorpostprocessor", _x_name)),
55  _y_values(getVectorPostprocessorValue("vectorpostprocessor", _y_name)),
56  _output_type(getParam<MooseEnum>("output")),
57  _num_samples(0),
58  _x_scale(parameters.get<Real>("x_scale")),
59  _x_shift(parameters.get<Real>("x_shift")),
60  _y_scale(parameters.get<Real>("y_scale")),
61  _y_shift(parameters.get<Real>("y_shift")),
62  _have_sample_x_min(isParamValid("sample_x_min")),
63  _have_sample_x_max(isParamValid("sample_x_max")),
64  _sample_x(NULL),
65  _sample_y(NULL),
66  _coeffs(NULL)
67 {
68  if (_output_type == "Samples")
69  {
70  if (isParamValid("num_samples"))
71  _num_samples = getParam<unsigned int>("num_samples");
72  else
73  mooseError("In LeastSquaresFit num_samples parameter must be provided with output=Samples");
74 
76  _sample_x_min = getParam<Real>("sample_x_min");
78  _sample_x_max = getParam<Real>("sample_x_max");
79 
82  }
83  else
84  {
85  if (isParamValid("num_samples"))
86  mooseWarning("In LeastSquaresFit num_samples parameter is unused with output=Coefficients");
87  _coeffs = &declareVector("coefficients");
88  }
89 
90  if (_output_type == "Samples")
91  {
92  _sample_x->resize(_num_samples);
93  _sample_y->resize(_num_samples);
94  }
95  else
96  _coeffs->resize(_order + 1);
97 }
98 
99 void
101 {
102  if (_output_type == "Samples")
103  {
104  _sample_x->clear();
105  _sample_y->clear();
106  }
107  else
108  _coeffs->clear();
109 }
110 
111 void
113 {
114  if (_x_values.size() != _y_values.size())
115  mooseError("In LeastSquresFit size of data in x_values and y_values must be equal");
116  if (_x_values.size() == 0)
117  mooseError("In LeastSquresFit size of data in x_values and y_values must be > 0");
118 
119  // Create a copy of _x_values that we can modify.
120  std::vector<Real> x_values(_x_values.begin(), _x_values.end());
121  std::vector<Real> y_values(_y_values.begin(), _y_values.end());
122 
123  for (MooseIndex(_x_values) i = 0; i < _x_values.size(); ++i)
124  {
125  x_values[i] = (x_values[i] + _x_shift) * _x_scale;
126  y_values[i] = (y_values[i] + _y_shift) * _y_scale;
127  }
128 
129  PolynomialFit pf(x_values, y_values, _order, true);
130  pf.generate();
131 
132  if (_output_type == "Samples")
133  {
134  Real x_min;
135  if (_have_sample_x_min)
136  x_min = _sample_x_min;
137  else
138  x_min = *(std::min_element(x_values.begin(), x_values.end()));
139 
140  Real x_max;
141  if (_have_sample_x_max)
142  x_max = _sample_x_max;
143  else
144  x_max = *(std::max_element(x_values.begin(), x_values.end()));
145 
146  Real x_span = x_max - x_min;
147 
148  for (unsigned int i = 0; i < _num_samples; ++i)
149  {
150  Real x = x_min + static_cast<Real>(i) / _num_samples * x_span;
151  _sample_x->push_back(x);
152  _sample_y->push_back(pf.sample(x));
153  }
154  }
155  else
156  *_coeffs = pf.getCoefficients();
157 }
const Real _y_shift
const std::string _x_name
The name of the variables storing the x, y data.
void mooseWarning(Args &&... args) const
Definition: MooseObject.h:155
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
virtual Real sample(Real x) override
This function will take an independent variable input and will return the dependent variable based on...
Definition: PolynomialFit.C:61
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.
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
static PetscErrorCode Vec x
std::string getRawNames() const
Method for returning the raw name strings for this instance.
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...
registerMooseObject("MooseApp", LeastSquaresFit)
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.
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.
virtual void generate()
Generate the fit.
LeastSquaresFit is a VectorPostprocessor that performs a least squares fit on data calculated in anot...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
Least squares polynomial fit.
Definition: PolynomialFit.h:19
virtual void execute() override
Perform the least squares fit.
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
unsigned int _order
The order of the polynomial fit to be performed.
const std::vector< Real > & getCoefficients()
Const reference to the vector of coefficients of the least squares fit.
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89