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 3466 : PiecewiseLinearInterpolationMaterial::validParams() 19 : { 20 : 21 3466 : InputParameters params = Material::validParams(); 22 6932 : params.addClassDescription("Compute a property using a piecewise linear interpolation to define " 23 : "its dependence on a variable"); 24 13864 : params.addRequiredParam<std::string>("property", 25 : "The name of the property this material will compute"); 26 13864 : params.addRequiredCoupledVar( 27 : "variable", 28 : "The name of the variable whose value is used as the abscissa in the interpolation"); 29 13864 : params.addParam<std::vector<Real>>("x", "The abscissa values"); 30 13864 : params.addParam<std::vector<Real>>("y", "The ordinate values"); 31 13864 : params.addParam<std::vector<Real>>("xy_data", 32 : "All function data, supplied in abscissa, ordinate pairs"); 33 13864 : params.addParam<Real>("scale_factor", 1.0, "Scale factor to be applied to the ordinate values"); 34 6932 : params.addParam<bool>( 35 : "extrapolation", 36 6932 : false, 37 : "Use linear extrapolation to evaluate points that lie outside given data set domain. "); 38 10398 : params.declareControllable("scale_factor"); 39 3466 : return params; 40 0 : } 41 : 42 309 : PiecewiseLinearInterpolationMaterial::PiecewiseLinearInterpolationMaterial( 43 309 : const InputParameters & parameters) 44 : : DerivativeMaterialInterface<Material>(parameters), 45 309 : _prop_name(getParam<std::string>("property")), 46 618 : _coupled_var(coupledValue("variable")), 47 618 : _scale_factor(getParam<Real>("scale_factor")), 48 618 : _extrap(getParam<bool>("extrapolation")), 49 309 : _property(declareProperty<Real>(_prop_name)), 50 618 : _dproperty(isCoupledConstant("variable") 51 618 : ? nullptr 52 2781 : : &declarePropertyDerivative<Real>(_prop_name, coupledName("variable", 0))) 53 : { 54 309 : std::vector<Real> x; 55 309 : std::vector<Real> y; 56 : 57 1215 : if ((parameters.isParamValid("x")) || (parameters.isParamValid("y"))) 58 : { 59 825 : if (!((parameters.isParamValid("x")) && (parameters.isParamValid("y")))) 60 3 : mooseError("In PiecewiseLinearInterpolationMaterial ", 61 3 : _name, 62 : ": Both 'x' and 'y' must be specified if either one is specified."); 63 : 64 324 : if (parameters.isParamValid("xy_data")) 65 3 : mooseError("In PiecewiseLinearInterpolationMaterial ", 66 3 : _name, 67 : ": Cannot specify 'x', 'y', and 'xy_data' together."); 68 : 69 318 : x = getParam<std::vector<Real>>("x"); 70 477 : y = getParam<std::vector<Real>>("y"); 71 : } 72 288 : else if (parameters.isParamValid("xy_data")) 73 : { 74 288 : std::vector<Real> xy = getParam<std::vector<Real>>("xy_data"); 75 144 : unsigned int xy_size = xy.size(); 76 144 : if (xy_size % 2 != 0) 77 3 : mooseError("In PiecewiseLinearInterpolationMaterial ", 78 3 : _name, 79 : ": Length of data provided in 'xy_data' must be a multiple of 2."); 80 : 81 141 : unsigned int x_size = xy_size / 2; 82 141 : x.reserve(x_size); 83 141 : y.reserve(x_size); 84 423 : for (unsigned int i = 0; i < xy_size / 2; ++i) 85 : { 86 282 : x.push_back(xy[i * 2]); 87 282 : y.push_back(xy[i * 2 + 1]); 88 : } 89 141 : } 90 : 91 : try 92 : { 93 300 : _linear_interp = std::make_unique<LinearInterpolation>(x, y, _extrap); 94 : } 95 3 : catch (std::domain_error & e) 96 : { 97 3 : mooseError("In PiecewiseLinearInterpolationMaterial ", _name, ": ", e.what()); 98 0 : } 99 297 : } 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 : }