www.mooseframework.org
LeastSquaresFitHistory.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 "LeastSquaresFitHistory.h"
12 #include "PolynomialFit.h"
13 #include "Conversion.h"
14 
16 
17 template <>
20 {
22 
23  params.addRequiredParam<VectorPostprocessorName>(
24  "vectorpostprocessor",
25  "The vectorpostprocessor on whose values we perform a least squares fit");
26  params.addRequiredParam<std::string>("x_name", "The name of the independent variable");
27  params.addRequiredParam<std::string>("y_name", "The name of the dependent variable");
28  params.addRequiredParam<unsigned int>("order", "The order of the polynomial fit");
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.addClassDescription(
38  "Performs a polynomial least squares fit on the data contained in "
39  "another VectorPostprocessor and stores the full time history of the coefficients");
40 
41  params.set<bool>("contains_complete_history") = true;
42  params.addParamNamesToGroup("contains_complete_history", "Advanced");
43 
44  return params;
45 }
46 
48  : GeneralVectorPostprocessor(parameters),
49  _vpp_name(getParam<VectorPostprocessorName>("vectorpostprocessor")),
50  _order(parameters.get<unsigned int>("order")),
51  _x_name(getParam<std::string>("x_name")),
52  _y_name(getParam<std::string>("y_name")),
53  _x_values(getVectorPostprocessorValue("vectorpostprocessor", _x_name)),
54  _y_values(getVectorPostprocessorValue("vectorpostprocessor", _y_name)),
55  _x_scale(parameters.get<Real>("x_scale")),
56  _x_shift(parameters.get<Real>("x_shift")),
57  _y_scale(parameters.get<Real>("y_scale")),
58  _y_shift(parameters.get<Real>("y_shift"))
59 {
60  _coeffs.resize(_order + 1);
61  for (unsigned int i = 0; i < _coeffs.size(); ++i)
62  _coeffs[i] = &declareVector("coef_" + Moose::stringify(i));
63  _times = &declareVector("time");
64 }
65 
66 void
68 {
69 }
70 
71 void
73 {
74  if (_x_values.size() != _y_values.size())
75  mooseError("In LeastSquresFitTimeHistory size of data in x_values and y_values must be equal");
76  if (_x_values.size() == 0)
77  mooseError("In LeastSquresFitTimeHistory size of data in x_values and y_values must be > 0");
78 
79  // Create a copy of _x_values that we can modify.
80  std::vector<Real> x_values(_x_values.begin(), _x_values.end());
81  std::vector<Real> y_values(_y_values.begin(), _y_values.end());
82 
83  for (MooseIndex(_x_values) i = 0; i < _x_values.size(); ++i)
84  {
85  x_values[i] = (x_values[i] + _x_shift) * _x_scale;
86  y_values[i] = (y_values[i] + _y_shift) * _y_scale;
87  }
88 
89  PolynomialFit pf(x_values, y_values, _order, true);
90  pf.generate();
91 
92  std::vector<Real> coeffs = pf.getCoefficients();
93  mooseAssert(coeffs.size() == _coeffs.size(),
94  "Sizes of current coefficients and vector of coefficient vectors must match");
95  for (MooseIndex(coeffs) i = 0; i < coeffs.size(); ++i)
96  _coeffs[i]->push_back(coeffs[i]);
97 
98  _times->push_back(_t);
99 }
registerMooseObject("MooseApp", LeastSquaresFitHistory)
This class is here to combine the VectorPostprocessor interface and the base class VectorPostprocesso...
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
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...
LeastSquaresFitHistory(const InputParameters &parameters)
Class constructor.
std::vector< VectorPostprocessorValue * > _coeffs
Vector of vectors with the individual coefficients.
const VectorPostprocessorValue & _x_values
The variables with the x, y data to be fit.
const Real _x_scale
Values used to scale and or shift x and y data.
const VectorPostprocessorValue & _y_values
InputParameters validParams< GeneralVectorPostprocessor >()
virtual void generate()
Generate the fit.
Least squares polynomial fit.
Definition: PolynomialFit.h:19
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:60
virtual void execute() override
Perform the least squares fit.
InputParameters validParams< LeastSquaresFitHistory >()
LeastSquaresFitHistory is a VectorPostprocessor that performs a least squares fit on data calculated ...
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.
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...
VectorPostprocessorValue * _times
Vector of times.
virtual void initialize() override
Initialize, clears old results.
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...