LCOV - code coverage report
Current view: top level - src/materials - ADPiecewiseLinearInterpolationMaterial.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 47 49 95.9 %
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 "ADPiecewiseLinearInterpolationMaterial.h"
      11             : 
      12             : #include "MooseVariableFE.h"
      13             : 
      14             : registerMooseObject("MooseApp", ADPiecewiseLinearInterpolationMaterial);
      15             : 
      16             : InputParameters
      17       14719 : ADPiecewiseLinearInterpolationMaterial::validParams()
      18             : {
      19       14719 :   InputParameters params = Material::validParams();
      20       14719 :   params.addClassDescription("Compute a property using a piecewise linear interpolation to define "
      21             :                              "its dependence on a variable");
      22       14719 :   params.addRequiredParam<std::string>("property",
      23             :                                        "The name of the property this material will compute");
      24       14719 :   params.addRequiredCoupledVar(
      25             :       "variable",
      26             :       "The name of the variable whose value is used as the abscissa in the interpolation");
      27       14719 :   params.addParam<std::vector<Real>>("x", "The abscissa values");
      28       14719 :   params.addParam<std::vector<Real>>("y", "The ordinate values");
      29       14719 :   params.addParam<std::vector<Real>>("xy_data",
      30             :                                      "All function data, supplied in abscissa, ordinate pairs");
      31       14719 :   params.addParam<Real>("scale_factor", 1.0, "Scale factor to be applied to the ordinate values");
      32       44157 :   params.addParam<bool>(
      33             :       "extrapolation",
      34       29438 :       false,
      35             :       "Use linear extrapolation to evaluate points that lie outside given data set domain. ");
      36       14719 :   return params;
      37           0 : }
      38             : 
      39         346 : ADPiecewiseLinearInterpolationMaterial::ADPiecewiseLinearInterpolationMaterial(
      40         346 :     const InputParameters & parameters)
      41             :   : Material(parameters),
      42         346 :     _prop_name(getParam<std::string>("property")),
      43         346 :     _coupled_var(adCoupledValue("variable")),
      44         346 :     _scale_factor(getParam<Real>("scale_factor")),
      45         346 :     _extrap(getParam<bool>("extrapolation")),
      46         692 :     _property(declareADProperty<Real>(_prop_name))
      47             : {
      48         346 :   std::vector<Real> x;
      49         346 :   std::vector<Real> y;
      50             : 
      51         346 :   if ((parameters.isParamValid("x")) || (parameters.isParamValid("y")))
      52             :   {
      53         162 :     if (!((parameters.isParamValid("x")) && (parameters.isParamValid("y"))))
      54           4 :       mooseError("In ", _name, ": Both 'x' and 'y' must be specified if either one is specified.");
      55             : 
      56         158 :     if (parameters.isParamValid("xy_data"))
      57           4 :       mooseError("In ", _name, ": Cannot specify 'x', 'y', and 'xy_data' together.");
      58             : 
      59         154 :     x = getParam<std::vector<Real>>("x");
      60         154 :     y = getParam<std::vector<Real>>("y");
      61             :   }
      62         184 :   else if (parameters.isParamValid("xy_data"))
      63             :   {
      64         184 :     std::vector<Real> xy = getParam<std::vector<Real>>("xy_data");
      65         184 :     unsigned int xy_size = xy.size();
      66         184 :     if (xy_size % 2 != 0)
      67           4 :       mooseError("In ", _name, ": Length of data provided in 'xy_data' must be a multiple of 2.");
      68             : 
      69         180 :     unsigned int x_size = xy_size / 2;
      70         180 :     x.reserve(x_size);
      71         180 :     y.reserve(x_size);
      72         540 :     for (unsigned int i = 0; i < xy_size / 2; ++i)
      73             :     {
      74         360 :       x.push_back(xy[i * 2]);
      75         360 :       y.push_back(xy[i * 2 + 1]);
      76             :     }
      77         180 :   }
      78             : 
      79             :   try
      80             :   {
      81         334 :     _linear_interp = std::make_unique<LinearInterpolation>(x, y, _extrap);
      82             :   }
      83           4 :   catch (std::domain_error & e)
      84             :   {
      85           4 :     mooseError("In ", _name, ": ", e.what());
      86           0 :   }
      87         330 : }
      88             : 
      89             : void
      90        4464 : ADPiecewiseLinearInterpolationMaterial::computeQpProperties()
      91             : {
      92        4464 :   _property[_qp] = _scale_factor * _linear_interp->sample(_coupled_var[_qp]);
      93        4464 : }

Generated by: LCOV version 1.14