www.mooseframework.org
ADPiecewiseLinearInterpolationMaterial.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
11 
12 #include "MooseVariableFE.h"
13 
15 
18  ADMaterial,
19  params.addClassDescription(
20  "Compute a property using a piecewise linear interpolation to define "
21  "its dependence on a variable");
22  params.addRequiredParam<std::string>("property",
23  "The name of the property this material will compute");
24  params.addRequiredCoupledVar(
25  "variable",
26  "The name of the variable whose value is used as the abscissa in the interpolation");
27  params.addParam<std::vector<Real>>("x", "The abscissa values");
28  params.addParam<std::vector<Real>>("y", "The ordinate values");
29  params.addParam<std::vector<Real>>("xy_data",
30  "All function data, supplied in abscissa, ordinate pairs");
31  params.addParam<Real>("scale_factor",
32  1.0,
33  "Scale factor to be applied to the ordinate values"););
34 
35 template <ComputeStage compute_stage>
37  const InputParameters & parameters)
38  : ADMaterial<compute_stage>(parameters),
39  _prop_name(adGetParam<std::string>("property")),
40  _coupled_var(adCoupledValue("variable")),
41  _scale_factor(adGetParam<Real>("scale_factor")),
42  _property(adDeclareADProperty<Real>(_prop_name))
43 {
44  std::vector<Real> x;
45  std::vector<Real> y;
46 
47  if ((parameters.isParamValid("x")) || (parameters.isParamValid("y")))
48  {
49  if (!((parameters.isParamValid("x")) && (parameters.isParamValid("y"))))
50  mooseError("In ", _name, ": Both 'x' and 'y' must be specified if either one is specified.");
51 
52  if (parameters.isParamValid("xy_data"))
53  mooseError("In ", _name, ": Cannot specify 'x', 'y', and 'xy_data' together.");
54 
55  x = adGetParam<std::vector<Real>>("x");
56  y = adGetParam<std::vector<Real>>("y");
57  }
58  else if (parameters.isParamValid("xy_data"))
59  {
60  std::vector<Real> xy = adGetParam<std::vector<Real>>("xy_data");
61  unsigned int xy_size = xy.size();
62  if (xy_size % 2 != 0)
63  mooseError("In ", _name, ": Length of data provided in 'xy_data' must be a multiple of 2.");
64 
65  unsigned int x_size = xy_size / 2;
66  x.reserve(x_size);
67  y.reserve(x_size);
68  for (unsigned int i = 0; i < xy_size / 2; ++i)
69  {
70  x.push_back(xy[i * 2]);
71  y.push_back(xy[i * 2 + 1]);
72  }
73  }
74 
75  try
76  {
77  _linear_interp = libmesh_make_unique<LinearInterpolation>(x, y);
78  }
79  catch (std::domain_error & e)
80  {
81  mooseError("In ", _name, ": ", e.what());
82  }
83 }
84 
85 template <ComputeStage compute_stage>
86 void
88 {
89  _property[_qp].value() = _scale_factor * _linear_interp->sample(_coupled_var[_qp].value());
90  _property[_qp].derivatives() = _scale_factor *
91  _linear_interp->sampleDerivative(_coupled_var[_qp].value()) *
92  _coupled_var[_qp].derivatives();
93 }
94 
95 template <>
96 void
98 {
99  _property[_qp] = _scale_factor * _linear_interp->sample(_coupled_var[_qp]);
100 }
std::unique_ptr< LinearInterpolation > _linear_interp
LinearInterpolation object.
This material uses a LinearInterpolation object to define the dependence of the material&#39;s value on a...
defineADValidParams(ADPiecewiseLinearInterpolationMaterial, ADMaterial, params.addClassDescription("Compute a property using a piecewise linear interpolation to define " "its dependence on a variable");params.addRequiredParam< std::string >("property", "The name of the property this material will compute");params.addRequiredCoupledVar("variable", "The name of the variable whose value is used as the abscissa in the interpolation");params.addParam< std::vector< Real >>("x", "The abscissa values");params.addParam< std::vector< Real >>("y", "The ordinate values");params.addParam< std::vector< Real >>("xy_data", "All function data, supplied in abscissa, ordinate pairs");params.addParam< Real >("scale_factor", 1.0, "Scale factor to be applied to the ordinate values");)
ADPiecewiseLinearInterpolationMaterial(const InputParameters &parameters)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
static PetscErrorCode Vec x
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseObject.h:65
const std::string & _name
The name of this object, reference to value stored in InputParameters.
Definition: MooseObject.h:183
registerADMooseObject("MooseApp", ADPiecewiseLinearInterpolationMaterial)
virtual void computeQpProperties() override
Users must override this method.
ADMaterials compute ADMaterialProperties.
Definition: ADMaterial.h:41
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.