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 14712 : PiecewiseLinearInterpolationMaterial::validParams() 19 : { 20 : 21 14712 : InputParameters params = Material::validParams(); 22 14712 : params.addClassDescription("Compute a property using a piecewise linear interpolation to define " 23 : "its dependence on a variable"); 24 14712 : params.addRequiredParam<std::string>("property", 25 : "The name of the property this material will compute"); 26 14712 : params.addRequiredCoupledVar( 27 : "variable", 28 : "The name of the variable whose value is used as the abscissa in the interpolation"); 29 14712 : params.addParam<std::vector<Real>>("x", "The abscissa values"); 30 14712 : params.addParam<std::vector<Real>>("y", "The ordinate values"); 31 14712 : params.addParam<std::vector<Real>>("xy_data", 32 : "All function data, supplied in abscissa, ordinate pairs"); 33 14712 : params.addParam<Real>("scale_factor", 1.0, "Scale factor to be applied to the ordinate values"); 34 44136 : params.addParam<bool>( 35 : "extrapolation", 36 29424 : false, 37 : "Use linear extrapolation to evaluate points that lie outside given data set domain. "); 38 14712 : params.declareControllable("scale_factor"); 39 14712 : return params; 40 0 : } 41 : 42 340 : PiecewiseLinearInterpolationMaterial::PiecewiseLinearInterpolationMaterial( 43 340 : const InputParameters & parameters) 44 : : DerivativeMaterialInterface<Material>(parameters), 45 340 : _prop_name(getParam<std::string>("property")), 46 340 : _coupled_var(coupledValue("variable")), 47 340 : _scale_factor(getParam<Real>("scale_factor")), 48 340 : _extrap(getParam<bool>("extrapolation")), 49 340 : _property(declareProperty<Real>(_prop_name)), 50 680 : _dproperty(isCoupledConstant("variable") 51 340 : ? nullptr 52 1020 : : &declarePropertyDerivative<Real>(_prop_name, coupledName("variable", 0))) 53 : { 54 340 : std::vector<Real> x; 55 340 : std::vector<Real> y; 56 : 57 340 : if ((parameters.isParamValid("x")) || (parameters.isParamValid("y"))) 58 : { 59 180 : 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 176 : if (parameters.isParamValid("xy_data")) 65 4 : mooseError("In PiecewiseLinearInterpolationMaterial ", 66 4 : _name, 67 : ": Cannot specify 'x', 'y', and 'xy_data' together."); 68 : 69 172 : x = getParam<std::vector<Real>>("x"); 70 172 : y = getParam<std::vector<Real>>("y"); 71 : } 72 160 : else if (parameters.isParamValid("xy_data")) 73 : { 74 160 : std::vector<Real> xy = getParam<std::vector<Real>>("xy_data"); 75 160 : unsigned int xy_size = xy.size(); 76 160 : 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 156 : unsigned int x_size = xy_size / 2; 82 156 : x.reserve(x_size); 83 156 : y.reserve(x_size); 84 468 : for (unsigned int i = 0; i < xy_size / 2; ++i) 85 : { 86 312 : x.push_back(xy[i * 2]); 87 312 : y.push_back(xy[i * 2 + 1]); 88 : } 89 156 : } 90 : 91 : try 92 : { 93 328 : _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 324 : } 100 : 101 : void 102 4248 : PiecewiseLinearInterpolationMaterial::computeQpProperties() 103 : { 104 4248 : _property[_qp] = _scale_factor * _linear_interp->sample(_coupled_var[_qp]); 105 4248 : if (_dproperty) 106 4248 : (*_dproperty)[_qp] = _scale_factor * _linear_interp->sampleDerivative(_coupled_var[_qp]); 107 4248 : }