LCOV - code coverage report
Current view: top level - src/functions - CoarsenedPiecewiseLinear.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 419b9d Lines: 38 41 92.7 %
Date: 2025-08-08 20:01:16 Functions: 4 4 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 "CoarsenedPiecewiseLinear.h"
      11             : #include "PointReduction.h"
      12             : 
      13             : registerMooseObject("MooseApp", CoarsenedPiecewiseLinear);
      14             : 
      15             : InputParameters
      16       14292 : CoarsenedPiecewiseLinear::validParams()
      17             : {
      18       14292 :   InputParameters params = PiecewiseLinearBase::validParams();
      19       14292 :   params.addClassDescription("Perform a point reduction of the tabulated data upon initialization, "
      20             :                              "then evaluate using a linear interpolation.");
      21       14292 :   params.addRequiredParam<Real>(
      22             :       "epsilon",
      23             :       "Significant distance in the function below which points are considered removable noise");
      24       42876 :   params.addParam<Real>("y_scale",
      25       28584 :                         1.0,
      26             :                         "Scaling factor to apply to the function nodes for the purpose of "
      27             :                         "computing distances in the Douglas-Peucker point reduction algorithm. "
      28             :                         "This permits shifting the weight between x and y-direction distances.");
      29       42876 :   params.addParam<Real>("x_scale",
      30       28584 :                         1.0,
      31             :                         "Scaling factor to apply to the function nodes for the purpose of "
      32             :                         "computing distances in the Douglas-Peucker point reduction algorithm. "
      33             :                         "This permits shifting the weight between x and y-direction distances.");
      34       14292 :   return params;
      35           0 : }
      36             : 
      37          14 : CoarsenedPiecewiseLinear::CoarsenedPiecewiseLinear(const InputParameters & parameters)
      38          14 :   : PiecewiseLinearBase(parameters)
      39             : {
      40          14 :   if (isRawDataLoaded())
      41             :   {
      42          14 :     buildCoarsenedGrid();
      43          14 :     _interpolation_created = true;
      44             :   }
      45          14 : }
      46             : 
      47             : void
      48          14 : CoarsenedPiecewiseLinear::initialSetup()
      49             : {
      50          14 :   PiecewiseTabularBase::initialSetup();
      51          14 :   if (isRawDataLoaded() && !_interpolation_created)
      52           0 :     buildCoarsenedGrid();
      53          14 :   else if (!isRawDataLoaded())
      54           0 :     mooseError("Data has still not been loaded at setup time. Something has gone wrong during "
      55             :                "Function initialization, contact a developer");
      56          14 : }
      57             : 
      58             : void
      59          14 : CoarsenedPiecewiseLinear::buildCoarsenedGrid()
      60             : {
      61          14 :   const Real x_scale = getParam<Real>("x_scale");
      62          14 :   const Real y_scale = getParam<Real>("y_scale");
      63          14 :   const Real epsilon = getParam<Real>("epsilon");
      64             : 
      65             :   // create vector of pairs
      66          14 :   PointReduction::FunctionNodeList list;
      67          14 :   list.reserve(_raw_x.size());
      68      140014 :   for (MooseIndex(_raw_x) i = 0; i < _raw_x.size(); ++i)
      69      140000 :     list.emplace_back(_raw_x[i] * x_scale, _raw_y[i] * y_scale);
      70             : 
      71             :   // point reduction
      72          14 :   _console << "Reduced size for function '" << name() << "' from " << list.size();
      73          14 :   list = PointReduction::douglasPeucker(list, epsilon);
      74          14 :   _console << " to " << list.size() << " points." << std::endl;
      75             : 
      76             :   // unpack vector of pairs
      77          14 :   _raw_x.resize(list.size());
      78          14 :   _raw_y.resize(list.size());
      79         168 :   for (MooseIndex(list) i = 0; i < list.size(); ++i)
      80             :   {
      81         154 :     _raw_x[i] = list[i].first / x_scale;
      82         154 :     _raw_y[i] = list[i].second / y_scale;
      83             :   }
      84             : 
      85          14 :   buildInterpolation();
      86          14 : }

Generated by: LCOV version 1.14