LCOV - code coverage report
Current view: top level - src/utils - PolynomialFit.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 26 28 92.9 %
Date: 2026-05-29 20:35:17 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          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             : #include "PolynomialFit.h"
      11             : 
      12             : #include "MooseError.h"
      13             : #include "MathUtils.h"
      14             : 
      15             : // C++ includes
      16             : #include <fstream>
      17             : #include <algorithm>
      18             : 
      19             : int PolynomialFit::_file_number = 0;
      20             : 
      21         207 : PolynomialFit::PolynomialFit(const std::vector<Real> & x,
      22             :                              const std::vector<Real> & y,
      23             :                              unsigned int order,
      24         207 :                              bool truncate_order)
      25         207 :   : LeastSquaresFitBase(x, y), _order(order), _truncate_order(truncate_order)
      26             : {
      27         207 :   if (_x.size() == 0)
      28           0 :     mooseError("PolynomialFit does not allow empty input vectors");
      29         207 :   if (_truncate_order)
      30             :   {
      31         168 :     if (_x.size() <= _order)
      32           0 :       _order = _x.size() - 1;
      33             :   }
      34          39 :   else if (_x.size() <= order)
      35           3 :     mooseError("PolynomialFit requires an order less than the size of the input vector");
      36             : 
      37         204 :   _num_coeff = _order + 1;
      38         204 : }
      39             : 
      40             : void
      41         200 : PolynomialFit::fillMatrix()
      42             : {
      43         200 :   unsigned int num_rows = _x.size();
      44         200 :   unsigned int num_cols = _order + 1;
      45         200 :   _matrix.resize(num_rows * num_cols);
      46             : 
      47         602 :   for (unsigned int col = 0; col < num_cols; ++col)
      48        4256 :     for (unsigned int row = 0; row < num_rows; ++row)
      49        3854 :       _matrix[(col * num_rows) + row] = MathUtils::pow(_x[row], col);
      50         200 : }
      51             : 
      52             : Real
      53         670 : PolynomialFit::sample(Real x)
      54             : {
      55         670 :   unsigned int size = _coeffs.size();
      56         670 :   Real value = 0;
      57             : 
      58         670 :   Real curr_x = 1;
      59        2020 :   for (unsigned int i = 0; i < size; ++i)
      60             :   {
      61        1350 :     value += _coeffs[i] * curr_x;
      62        1350 :     curr_x *= x;
      63             :   }
      64         670 :   return value;
      65             : }

Generated by: LCOV version 1.14