https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LeastSquaresFit.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
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
18{
20
21 params.addRequiredParam<VectorPostprocessorName>(
22 "vectorpostprocessor",
23 "The vectorpostprocessor on whose values we perform a least squares fit");
24 params.addRequiredParam<std::string>("x_name", "The name of the independent variable");
25 params.addRequiredParam<std::string>("y_name", "The name of the dependent variable");
26 params.addRequiredParam<unsigned int>("order", "The order of the polynomial fit");
27 params.addParam<bool>(
28 "truncate_order",
29 true,
30 "Truncate the order of the fitted polynomial if an insufficient number of data points are "
31 "provided. If this is set to false, an error will be generated in that case.");
32 params.addParam<unsigned int>("num_samples", "The number of samples to be output");
33 params.addParam<Real>(
34 "x_scale", 1.0, "Value used to scale x values (scaling is done after shifting)");
35 params.addParam<Real>(
36 "x_shift", 0.0, "Value used to shift x values (shifting is done before scaling)");
37 params.addParam<Real>(
38 "y_scale", 1.0, "Value used to scale y values (scaling is done after shifting)");
39 params.addParam<Real>(
40 "y_shift", 0.0, "Value used to shift y values (shifting is done before scaling)");
41 params.addParam<Real>("sample_x_min", "The minimum x value of the of samples to be output");
42 params.addParam<Real>("sample_x_max", "The maximum x value of the of samples to be output");
43 MooseEnum output_type("Coefficients Samples", "Coefficients");
44 params.addParam<MooseEnum>(
45 "output", output_type, "The quantity to output. Options are: " + output_type.getRawNames());
46 params.addClassDescription("Performs a polynomial least squares fit on the data contained in "
47 "another VectorPostprocessor");
48
49 return params;
50}
51
53 : GeneralVectorPostprocessor(parameters),
54 _vpp_name(getParam<VectorPostprocessorName>("vectorpostprocessor")),
55 _order(parameters.get<unsigned int>("order")),
56 _truncate_order(parameters.get<bool>("truncate_order")),
57 _x_name(getParam<std::string>("x_name")),
58 _y_name(getParam<std::string>("y_name")),
59 _x_values(getVectorPostprocessorValue("vectorpostprocessor", _x_name)),
60 _y_values(getVectorPostprocessorValue("vectorpostprocessor", _y_name)),
61 _output_type(getParam<MooseEnum>("output")),
62 _num_samples(0),
63 _x_scale(parameters.get<Real>("x_scale")),
64 _x_shift(parameters.get<Real>("x_shift")),
65 _y_scale(parameters.get<Real>("y_scale")),
66 _y_shift(parameters.get<Real>("y_shift")),
67 _have_sample_x_min(isParamValid("sample_x_min")),
68 _have_sample_x_max(isParamValid("sample_x_max")),
69 _sample_x(NULL),
70 _sample_y(NULL),
71 _coeffs(NULL)
72{
73 if (_output_type == "Samples")
74 {
75 if (isParamValid("num_samples"))
76 _num_samples = getParam<unsigned int>("num_samples");
77 else
78 mooseError("In LeastSquaresFit num_samples parameter must be provided with output=Samples");
79
81 _sample_x_min = getParam<Real>("sample_x_min");
83 _sample_x_max = getParam<Real>("sample_x_max");
84
87 }
88 else
89 {
90 if (isParamValid("num_samples"))
91 mooseWarning("In LeastSquaresFit num_samples parameter is unused with output=Coefficients");
92 _coeffs = &declareVector("coefficients");
93 }
94
95 if (_output_type == "Samples")
96 {
97 _sample_x->resize(_num_samples);
98 _sample_y->resize(_num_samples);
99 }
100 else
101 _coeffs->resize(_order + 1);
102}
103
104void
106{
107 if (_output_type == "Samples")
108 {
109 _sample_x->clear();
110 _sample_y->clear();
111 }
112 else
113 _coeffs->clear();
114}
115
116void
118{
119 if (_x_values.size() != _y_values.size())
120 mooseError("Size of data in x_values and y_values must be equal");
121 if (_x_values.size() == 0)
122 mooseError("Size of data in x_values and y_values must be > 0");
123
124 // Create a copy of _x_values that we can modify.
125 std::vector<Real> x_values(_x_values.begin(), _x_values.end());
126 std::vector<Real> y_values(_y_values.begin(), _y_values.end());
127
128 for (MooseIndex(_x_values) i = 0; i < _x_values.size(); ++i)
129 {
130 x_values[i] = (x_values[i] + _x_shift) * _x_scale;
131 y_values[i] = (y_values[i] + _y_shift) * _y_scale;
132 }
133
134 PolynomialFit pf(x_values, y_values, _order, _truncate_order);
135 pf.generate();
136
137 if (_output_type == "Samples")
138 {
139 Real x_min;
141 x_min = _sample_x_min;
142 else
143 x_min = *(std::min_element(x_values.begin(), x_values.end()));
144
145 Real x_max;
147 x_max = _sample_x_max;
148 else
149 x_max = *(std::max_element(x_values.begin(), x_values.end()));
150
151 Real x_span = x_max - x_min;
152
153 for (unsigned int i = 0; i < _num_samples; ++i)
154 {
155 Real x = x_min + static_cast<Real>(i) / _num_samples * x_span;
156 _sample_x->push_back(x);
157 _sample_y->push_back(pf.sample(x));
158 }
159 }
160 else
161 *_coeffs = pf.getCoefficients();
162}
registerMooseObject("MooseApp", LeastSquaresFit)
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition MooseError.h:345
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
void ErrorVector unsigned int
This class is here to combine the VectorPostprocessor interface and the base class VectorPostprocesso...
static InputParameters validParams()
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
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...
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.
virtual void generate()
Generate the fit.
const std::vector< Real > & getCoefficients()
Const reference to the vector of coefficients of the least squares fit.
LeastSquaresFit is a VectorPostprocessor that performs a least squares fit on data calculated in anot...
static InputParameters validParams()
VectorPostprocessorValue * _sample_x
The variables used to write out samples of the least squares fit.
const Real _x_scale
Values used to scale and or shift x and y data.
LeastSquaresFit(const InputParameters &parameters)
Class constructor.
unsigned int _num_samples
The number of samples to be taken.
const Real _x_shift
const VectorPostprocessorValue & _y_values
Real _sample_x_min
The min and max x values for sampling.
bool _have_sample_x_min
Did the user specify the min and max x values for sampling?
virtual void initialize() override
Initialize, clears old results.
const VectorPostprocessorValue & _x_values
The variables with the x, y data to be fit.
const Real _y_scale
VectorPostprocessorValue * _coeffs
The variable used to write out the coefficients of the fit.
const std::string _x_name
The name of the variables storing the x, y data.
const Real _y_shift
const unsigned int _order
The order of the polynomial fit to be performed.
const bool _truncate_order
Whether to truncate the polynomial order if an insufficient number of points is provided.
virtual void execute() override
Perform the least squares fit.
const MooseEnum _output_type
The type of output.
VectorPostprocessorValue * _sample_y
const std::string _y_name
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
Least squares polynomial fit.
virtual Real sample(Real x) override
This function will take an independent variable input and will return the dependent variable based on...
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.