LCOV - code coverage report
Current view: top level - src/utils - LeastSquaresFitBase.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 19 25 76.0 %
Date: 2025-07-17 01:28:37 Functions: 5 7 71.4 %
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 "LeastSquaresFitBase.h"
      11             : #include "MooseError.h"
      12             : 
      13             : #include "libmesh/int_range.h"
      14             : // Ignore warnings from Eigen related to deprecated declarations (C++17)
      15             : #include "libmesh/ignore_warnings.h"
      16             : #include <Eigen/Dense>
      17             : #include "libmesh/restore_warnings.h"
      18             : 
      19           0 : LeastSquaresFitBase::LeastSquaresFitBase() {}
      20             : 
      21         202 : LeastSquaresFitBase::LeastSquaresFitBase(const std::vector<Real> & x, const std::vector<Real> & y)
      22         202 :   : _x(x), _y(y)
      23             : {
      24         202 : }
      25             : 
      26             : void
      27           0 : LeastSquaresFitBase::setVariables(const std::vector<Real> & x, const std::vector<Real> & y)
      28             : {
      29           0 :   _x = x;
      30           0 :   _y = y;
      31           0 : }
      32             : 
      33             : void
      34         196 : LeastSquaresFitBase::generate()
      35             : {
      36         196 :   if (_x.empty())
      37           0 :     mooseError("Empty variables in LeastSquaresFitBase. x and y must be set in the constructor or "
      38             :                "using setVariables(x, y)");
      39             : 
      40         196 :   fillMatrix();
      41         196 :   doLeastSquares();
      42         196 : }
      43             : 
      44             : void
      45         196 : LeastSquaresFitBase::doLeastSquares()
      46             : {
      47         196 :   _coeffs.resize(_num_coeff);
      48             : 
      49             :   typedef Eigen::Matrix<Real, Eigen::Dynamic, 1> SolveVec;
      50         196 :   auto b = Eigen::Map<SolveVec, Eigen::Unaligned>(_y.data(), _y.size());
      51         196 :   auto x = Eigen::Map<SolveVec, Eigen::Unaligned>(_coeffs.data(), _num_coeff);
      52             :   typedef Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> SolveMatrix;
      53         196 :   auto A = Eigen::Map<SolveMatrix, Eigen::Unaligned>(_matrix.data(), _y.size(), _num_coeff);
      54         196 :   x = A.colPivHouseholderQr().solve(b);
      55         196 : }
      56             : 
      57             : unsigned int
      58           2 : LeastSquaresFitBase::getSampleSize()
      59             : {
      60           2 :   return _x.size();
      61             : }
      62             : 
      63             : const std::vector<Real> &
      64         162 : LeastSquaresFitBase::getCoefficients()
      65             : {
      66         162 :   return _coeffs;
      67             : }

Generated by: LCOV version 1.14