https://mooseframework.inl.gov
DerivativeFunctionMaterialBase.C
Go to the documentation of this file.
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 
11 
12 #include "libmesh/quadrature.h"
13 
14 template <bool is_ad>
17 {
18 
20  params.addClassDescription("Material to provide a function (such as a free energy) and its "
21  "derivatives w.r.t. the coupled variables");
22  params.addDeprecatedParam<bool>("third_derivatives",
23  "Flag to indicate if third derivatives are needed",
24  "Use derivative_order instead.");
25  params.addRangeCheckedParam<unsigned int>("derivative_order",
26  3,
27  "derivative_order>=2 & derivative_order<=3",
28  "Maximum order of derivatives taken (2 or 3)");
29  return params;
30 }
31 
32 template <bool is_ad>
34  const InputParameters & parameters)
35  : FunctionMaterialBase<is_ad>(parameters),
36  _third_derivatives(this->template getParam<unsigned int>("derivative_order") == 3)
37 {
38  // reserve space for material properties and explicitly initialize to NULL
39  _prop_dF.resize(_nargs, NULL);
40  _prop_d2F.resize(_nargs);
41  _prop_d3F.resize(_nargs);
42  for (unsigned int i = 0; i < _nargs; ++i)
43  {
44  _prop_d2F[i].resize(_nargs, NULL);
45 
47  {
48  _prop_d3F[i].resize(_nargs);
49 
50  for (unsigned int j = 0; j < _nargs; ++j)
51  _prop_d3F[i][j].resize(_nargs, NULL);
52  }
53  }
54 
55  // initialize derivatives
56  for (unsigned int i = 0; i < _nargs; ++i)
57  {
59  _prop_dF[i] = &this->template declarePropertyDerivative<Real, is_ad>(_F_name, _arg_names[i]);
60 
61  // second derivatives
62  for (unsigned int j = i; j < _nargs; ++j)
63  {
65  _prop_d2F[i][j] = _prop_d2F[j][i] = &this->template declarePropertyDerivative<Real, is_ad>(
66  _F_name, _arg_names[i], _arg_names[j]);
67 
68  // third derivatives
70  {
71  for (unsigned int k = j; k < _nargs; ++k)
72  {
73  // filling all permutations does not cost us much and simplifies access
74  // (no need to check i<=j<=k)
76  _prop_d3F[i][j][k] = _prop_d3F[k][i][j] = _prop_d3F[j][k][i] = _prop_d3F[k][j][i] =
77  _prop_d3F[j][i][k] = _prop_d3F[i][k][j] =
78  &this->template declarePropertyDerivative<Real, is_ad>(
80  }
81  }
82  }
83  }
84 }
85 
86 template <bool is_ad>
87 void
89 {
90  // set the _prop_* pointers of all material properties that are not beeing used back to NULL
91  bool needs_third_derivatives = false;
92 
93  if (!this->_fe_problem.isMatPropRequested(_F_name))
94  _prop_F = NULL;
95 
96  for (unsigned int i = 0; i < _nargs; ++i)
97  {
98  if (!this->_fe_problem.isMatPropRequested(
99  this->derivativePropertyNameFirst(_F_name, _arg_names[i])))
100  _prop_dF[i] = NULL;
101 
102  // second derivatives
103  for (unsigned int j = i; j < _nargs; ++j)
104  {
105  if (!this->_fe_problem.isMatPropRequested(
106  this->derivativePropertyNameSecond(_F_name, _arg_names[i], _arg_names[j])))
107  _prop_d2F[i][j] = _prop_d2F[j][i] = NULL;
108 
109  // third derivatives
110  if (_third_derivatives)
111  {
112  for (unsigned int k = j; k < _nargs; ++k)
113  {
114  if (!this->_fe_problem.isMatPropRequested(this->derivativePropertyNameThird(
115  _F_name, _arg_names[i], _arg_names[j], _arg_names[k])))
116  _prop_d3F[i][j][k] = _prop_d3F[k][i][j] = _prop_d3F[j][k][i] = _prop_d3F[k][j][i] =
117  _prop_d3F[j][i][k] = _prop_d3F[i][k][j] = NULL;
118  else
119  needs_third_derivatives = true;
120  }
121 
122  if (!needs_third_derivatives)
123  mooseWarning("This simulation does not actually need the third derivatives of "
124  "DerivativeFunctionMaterialBaseTempl " +
125  name());
126  }
127  }
128  }
129 }
130 
131 template <bool is_ad>
132 void
134 {
135  for (_qp = 0; _qp < _qrule->n_points(); _qp++)
136  {
137  // set function value
138  if (_prop_F)
139  (*_prop_F)[_qp] = computeF();
140 
141  for (unsigned int i = 0; i < _nargs; ++i)
142  {
143  // set first derivatives
144  if (_prop_dF[i])
145  (*_prop_dF[i])[_qp] = computeDF(_arg_numbers[i]);
146 
147  // second derivatives
148  for (unsigned int j = i; j < _nargs; ++j)
149  {
150  if (_prop_d2F[i][j])
151  (*_prop_d2F[i][j])[_qp] = computeD2F(_arg_numbers[i], _arg_numbers[j]);
152 
153  // third derivatives
154  if (_third_derivatives)
155  {
156  for (unsigned int k = j; k < _nargs; ++k)
157  if (_prop_d3F[i][j][k])
158  (*_prop_d3F[i][j][k])[_qp] =
159  computeD3F(_arg_numbers[i], _arg_numbers[j], _arg_numbers[k]);
160  }
161  }
162  }
163  }
164 }
165 
166 // explicit instantiation
std::string name(const ElemQuality q)
virtual bool isCoupledConstant(const std::string &var_name) const
Returns true if a variable passed as a coupled value is really a constant.
Definition: Coupleable.C:152
void addDeprecatedParam(const std::string &name, const T &value, const std::string &doc_string, const std::string &deprecation_message)
Material base class, central to all Materials that provide a Function as a material property value...
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:336
std::string _F_name
Name of the function value material property and used as a base name to concatenate the material prop...
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::vector< std::string > _arg_names
String vector of all argument names.
DerivativeFunctionMaterialBaseTempl(const InputParameters &parameters)
std::vector< std::vector< GenericMaterialProperty< Real, is_ad > * > > _prop_d2F
Material properties to store the second derivatives.
static InputParameters validParams()
void initialSetup() override
Check if we got the right number of components in the &#39;coupled_variables&#39; vector. ...
bool _third_derivatives
Calculate (and allocate memory for) the third derivatives of the free energy.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
Material base class to compute a function and its derivatives.
std::vector< std::vector< std::vector< GenericMaterialProperty< Real, is_ad > * > > > _prop_d3F
Material properties to store the third derivatives.
void addRangeCheckedParam(const std::string &name, const T &value, const std::string &parsed_function, const std::string &doc_string)
void computeProperties() override
Performs the quadrature point loop, calling computeQpProperties.
unsigned int _nargs
Number of coupled arguments.
void ErrorVector unsigned int
std::vector< GenericMaterialProperty< Real, is_ad > * > _prop_dF
Material properties to store the derivatives of f with respect to arg[i].