LCOV - code coverage report
Current view: top level - src/materials - PiecewiseLinearInterpolationMaterial.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 56 58 96.6 %
Date: 2025-07-17 01:28:37 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 "PiecewiseLinearInterpolationMaterial.h"
      11             : 
      12             : // MOOSE includes
      13             : #include "MooseVariableFE.h"
      14             : 
      15             : registerMooseObject("MooseApp", PiecewiseLinearInterpolationMaterial);
      16             : 
      17             : InputParameters
      18       14684 : PiecewiseLinearInterpolationMaterial::validParams()
      19             : {
      20             : 
      21       14684 :   InputParameters params = Material::validParams();
      22       14684 :   params.addClassDescription("Compute a property using a piecewise linear interpolation to define "
      23             :                              "its dependence on a variable");
      24       14684 :   params.addRequiredParam<std::string>("property",
      25             :                                        "The name of the property this material will compute");
      26       14684 :   params.addRequiredCoupledVar(
      27             :       "variable",
      28             :       "The name of the variable whose value is used as the abscissa in the interpolation");
      29       14684 :   params.addParam<std::vector<Real>>("x", "The abscissa values");
      30       14684 :   params.addParam<std::vector<Real>>("y", "The ordinate values");
      31       14684 :   params.addParam<std::vector<Real>>("xy_data",
      32             :                                      "All function data, supplied in abscissa, ordinate pairs");
      33       14684 :   params.addParam<Real>("scale_factor", 1.0, "Scale factor to be applied to the ordinate values");
      34       44052 :   params.addParam<bool>(
      35             :       "extrapolation",
      36       29368 :       false,
      37             :       "Use linear extrapolation to evaluate points that lie outside given data set domain. ");
      38       14684 :   params.declareControllable("scale_factor");
      39       14684 :   return params;
      40           0 : }
      41             : 
      42         319 : PiecewiseLinearInterpolationMaterial::PiecewiseLinearInterpolationMaterial(
      43         319 :     const InputParameters & parameters)
      44             :   : DerivativeMaterialInterface<Material>(parameters),
      45         319 :     _prop_name(getParam<std::string>("property")),
      46         319 :     _coupled_var(coupledValue("variable")),
      47         319 :     _scale_factor(getParam<Real>("scale_factor")),
      48         319 :     _extrap(getParam<bool>("extrapolation")),
      49         319 :     _property(declareProperty<Real>(_prop_name)),
      50         638 :     _dproperty(isCoupledConstant("variable")
      51         319 :                    ? nullptr
      52         957 :                    : &declarePropertyDerivative<Real>(_prop_name, coupledName("variable", 0)))
      53             : {
      54         319 :   std::vector<Real> x;
      55         319 :   std::vector<Real> y;
      56             : 
      57         319 :   if ((parameters.isParamValid("x")) || (parameters.isParamValid("y")))
      58             :   {
      59         168 :     if (!((parameters.isParamValid("x")) && (parameters.isParamValid("y"))))
      60           4 :       mooseError("In PiecewiseLinearInterpolationMaterial ",
      61           4 :                  _name,
      62             :                  ": Both 'x' and 'y' must be specified if either one is specified.");
      63             : 
      64         164 :     if (parameters.isParamValid("xy_data"))
      65           4 :       mooseError("In PiecewiseLinearInterpolationMaterial ",
      66           4 :                  _name,
      67             :                  ": Cannot specify 'x', 'y', and 'xy_data' together.");
      68             : 
      69         160 :     x = getParam<std::vector<Real>>("x");
      70         160 :     y = getParam<std::vector<Real>>("y");
      71             :   }
      72         151 :   else if (parameters.isParamValid("xy_data"))
      73             :   {
      74         151 :     std::vector<Real> xy = getParam<std::vector<Real>>("xy_data");
      75         151 :     unsigned int xy_size = xy.size();
      76         151 :     if (xy_size % 2 != 0)
      77           4 :       mooseError("In PiecewiseLinearInterpolationMaterial ",
      78           4 :                  _name,
      79             :                  ": Length of data provided in 'xy_data' must be a multiple of 2.");
      80             : 
      81         147 :     unsigned int x_size = xy_size / 2;
      82         147 :     x.reserve(x_size);
      83         147 :     y.reserve(x_size);
      84         441 :     for (unsigned int i = 0; i < xy_size / 2; ++i)
      85             :     {
      86         294 :       x.push_back(xy[i * 2]);
      87         294 :       y.push_back(xy[i * 2 + 1]);
      88             :     }
      89         147 :   }
      90             : 
      91             :   try
      92             :   {
      93         307 :     _linear_interp = std::make_unique<LinearInterpolation>(x, y, _extrap);
      94             :   }
      95           4 :   catch (std::domain_error & e)
      96             :   {
      97           4 :     mooseError("In PiecewiseLinearInterpolationMaterial ", _name, ": ", e.what());
      98           0 :   }
      99         303 : }
     100             : 
     101             : void
     102        3776 : PiecewiseLinearInterpolationMaterial::computeQpProperties()
     103             : {
     104        3776 :   _property[_qp] = _scale_factor * _linear_interp->sample(_coupled_var[_qp]);
     105        3776 :   if (_dproperty)
     106        3776 :     (*_dproperty)[_qp] = _scale_factor * _linear_interp->sampleDerivative(_coupled_var[_qp]);
     107        3776 : }

Generated by: LCOV version 1.14