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 14743 : ADPiecewiseLinearInterpolationMaterial::validParams() 18 : { 19 14743 : InputParameters params = Material::validParams(); 20 14743 : params.addClassDescription("Compute a property using a piecewise linear interpolation to define " 21 : "its dependence on a variable"); 22 14743 : params.addRequiredParam<std::string>("property", 23 : "The name of the property this material will compute"); 24 14743 : params.addRequiredCoupledVar( 25 : "variable", 26 : "The name of the variable whose value is used as the abscissa in the interpolation"); 27 14743 : params.addParam<std::vector<Real>>("x", "The abscissa values"); 28 14743 : params.addParam<std::vector<Real>>("y", "The ordinate values"); 29 14743 : params.addParam<std::vector<Real>>("xy_data", 30 : "All function data, supplied in abscissa, ordinate pairs"); 31 14743 : params.addParam<Real>("scale_factor", 1.0, "Scale factor to be applied to the ordinate values"); 32 44229 : params.addParam<bool>( 33 : "extrapolation", 34 29486 : false, 35 : "Use linear extrapolation to evaluate points that lie outside given data set domain. "); 36 14743 : return params; 37 0 : } 38 : 39 364 : ADPiecewiseLinearInterpolationMaterial::ADPiecewiseLinearInterpolationMaterial( 40 364 : const InputParameters & parameters) 41 : : Material(parameters), 42 364 : _prop_name(getParam<std::string>("property")), 43 364 : _coupled_var(adCoupledValue("variable")), 44 364 : _scale_factor(getParam<Real>("scale_factor")), 45 364 : _extrap(getParam<bool>("extrapolation")), 46 728 : _property(declareADProperty<Real>(_prop_name)) 47 : { 48 364 : std::vector<Real> x; 49 364 : std::vector<Real> y; 50 : 51 364 : if ((parameters.isParamValid("x")) || (parameters.isParamValid("y"))) 52 : { 53 171 : 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 167 : if (parameters.isParamValid("xy_data")) 57 4 : mooseError("In ", _name, ": Cannot specify 'x', 'y', and 'xy_data' together."); 58 : 59 163 : x = getParam<std::vector<Real>>("x"); 60 163 : y = getParam<std::vector<Real>>("y"); 61 : } 62 193 : else if (parameters.isParamValid("xy_data")) 63 : { 64 193 : std::vector<Real> xy = getParam<std::vector<Real>>("xy_data"); 65 193 : unsigned int xy_size = xy.size(); 66 193 : 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 189 : unsigned int x_size = xy_size / 2; 70 189 : x.reserve(x_size); 71 189 : y.reserve(x_size); 72 567 : for (unsigned int i = 0; i < xy_size / 2; ++i) 73 : { 74 378 : x.push_back(xy[i * 2]); 75 378 : y.push_back(xy[i * 2 + 1]); 76 : } 77 189 : } 78 : 79 : try 80 : { 81 352 : _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 348 : } 88 : 89 : void 90 4896 : ADPiecewiseLinearInterpolationMaterial::computeQpProperties() 91 : { 92 4896 : _property[_qp] = _scale_factor * _linear_interp->sample(_coupled_var[_qp]); 93 4896 : }