Line data Source code
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 : #pragma once 11 : 12 : #include <vector> 13 : #include <string> 14 : 15 : #include "Moose.h" 16 : 17 : /** 18 : * Base class for linear least squares fit method. Derived classes must implement 19 : * the fillMatrix() and sample() methods with the functional form of the fit. 20 : * 21 : * Requires: LAPACK 22 : */ 23 : class LeastSquaresFitBase 24 : { 25 : public: 26 : LeastSquaresFitBase(); 27 : LeastSquaresFitBase(const std::vector<Real> & x, const std::vector<Real> & y); 28 : 29 198 : virtual ~LeastSquaresFitBase() = default; 30 : 31 : /** 32 : * Generate the fit. This function must be called prior to using sample. 33 : * Note: If you pass a vector that contains duplicate independent measures 34 : * the call to LAPACK will fail 35 : */ 36 : virtual void generate(); 37 : 38 : /** 39 : * This function will take an independent variable input and will return the 40 : * dependent variable based on the generated fit 41 : * @param x independent variable 42 : * @return dependent variable 43 : */ 44 : virtual Real sample(Real x) = 0; 45 : 46 : /** 47 : * Size of the array holding the points 48 : * @return number of sample points 49 : */ 50 : unsigned int getSampleSize(); 51 : 52 : /** 53 : * Const reference to the vector of coefficients of the least squares fit 54 : * @param return vector of coefficients 55 : */ 56 : const std::vector<Real> & getCoefficients(); 57 : 58 : void setVariables(const std::vector<Real> & x, const std::vector<Real> & y); 59 : 60 : protected: 61 : /** 62 : * Helper function that creates the matrix necessary for the least squares algorithm 63 : */ 64 : virtual void fillMatrix() = 0; 65 : 66 : /** 67 : * Wrapper for the LAPACK dgels function. Called by generate() to perform the 68 : * least squares fit 69 : */ 70 : void doLeastSquares(); 71 : 72 : /// Independent variable 73 : std::vector<Real> _x; 74 : /// Dependent variable 75 : std::vector<Real> _y; 76 : /// Basis functions evaluated at each independent variable (note: actually a vector) 77 : std::vector<Real> _matrix; 78 : /// Vector of coefficients of the least squares fit 79 : std::vector<Real> _coeffs; 80 : /// The number of coefficients 81 : unsigned int _num_coeff; 82 : };