www.mooseframework.org
DerivativeFunctionMaterialBase.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
3 /* */
4 /* All contents are licensed under LGPL V2.1 */
5 /* See LICENSE for full restrictions */
6 /****************************************************************/
7 
9 
10 #include "libmesh/quadrature.h"
11 
12 template <>
13 InputParameters
15 {
16  InputParameters params = validParams<FunctionMaterialBase>();
17  params.addClassDescription("Material to provide a function (such as a free energy) and its "
18  "derivatives w.r.t. the coupled variables");
19  params.addDeprecatedParam<bool>("third_derivatives",
20  "Flag to indicate if third derivatives are needed",
21  "Use derivative_order instead.");
22  params.addRangeCheckedParam<unsigned int>("derivative_order",
23  3,
24  "derivative_order>=2 & derivative_order<=3",
25  "Maximum order of derivatives taken (2 or 3)");
26  return params;
27 }
28 
30  : FunctionMaterialBase(parameters),
31  _third_derivatives(isParamValid("third_derivatives")
32  ? getParam<bool>("third_derivatives")
33  : (getParam<unsigned int>("derivative_order") == 3))
34 {
35  // reserve space for material properties and explicitly initialize to NULL
36  _prop_dF.resize(_nargs, NULL);
37  _prop_d2F.resize(_nargs);
38  _prop_d3F.resize(_nargs);
39  for (unsigned int i = 0; i < _nargs; ++i)
40  {
41  _prop_d2F[i].resize(_nargs, NULL);
42 
44  {
45  _prop_d3F[i].resize(_nargs);
46 
47  for (unsigned int j = 0; j < _nargs; ++j)
48  _prop_d3F[i][j].resize(_nargs, NULL);
49  }
50  }
51 
52  // initialize derivatives
53  for (unsigned int i = 0; i < _nargs; ++i)
54  {
55  // first derivatives
56  _prop_dF[i] = &declarePropertyDerivative<Real>(_F_name, _arg_names[i]);
57 
58  // second derivatives
59  for (unsigned int j = i; j < _nargs; ++j)
60  {
61  _prop_d2F[i][j] = _prop_d2F[j][i] =
62  &declarePropertyDerivative<Real>(_F_name, _arg_names[i], _arg_names[j]);
63 
64  // third derivatives
66  {
67  for (unsigned int k = j; k < _nargs; ++k)
68  {
69  // filling all permutations does not cost us much and simplifies access
70  // (no need to check i<=j<=k)
71  _prop_d3F[i][j][k] = _prop_d3F[k][i][j] = _prop_d3F[j][k][i] = _prop_d3F[k][j][i] =
72  _prop_d3F[j][i][k] = _prop_d3F[i][k][j] = &declarePropertyDerivative<Real>(
73  _F_name, _arg_names[i], _arg_names[j], _arg_names[k]);
74  }
75  }
76  }
77  }
78 }
79 
80 void
82 {
83  // set the _prop_* pointers of all material properties that are not beeing used back to NULL
84  bool needs_third_derivatives = false;
85 
86  if (!_fe_problem.isMatPropRequested(_F_name))
87  _prop_F = NULL;
88 
89  for (unsigned int i = 0; i < _nargs; ++i)
90  {
91  if (!_fe_problem.isMatPropRequested(propertyNameFirst(_F_name, _arg_names[i])))
92  _prop_dF[i] = NULL;
93 
94  // second derivatives
95  for (unsigned int j = i; j < _nargs; ++j)
96  {
97  if (!_fe_problem.isMatPropRequested(
98  propertyNameSecond(_F_name, _arg_names[i], _arg_names[j])))
99  _prop_d2F[i][j] = _prop_d2F[j][i] = NULL;
100 
101  // third derivatives
102  if (_third_derivatives)
103  {
104  for (unsigned int k = j; k < _nargs; ++k)
105  {
106  if (!_fe_problem.isMatPropRequested(
107  propertyNameThird(_F_name, _arg_names[i], _arg_names[j], _arg_names[k])))
108  _prop_d3F[i][j][k] = _prop_d3F[k][i][j] = _prop_d3F[j][k][i] = _prop_d3F[k][j][i] =
109  _prop_d3F[j][i][k] = _prop_d3F[i][k][j] = NULL;
110  else
111  needs_third_derivatives = true;
112  }
113 
114  if (!needs_third_derivatives)
115  mooseWarning("This simulation does not actually need the third derivatives of "
116  "DerivativeFunctionMaterialBase " +
117  name());
118  }
119  }
120  }
121 }
122 
123 void
125 {
126  for (_qp = 0; _qp < _qrule->n_points(); _qp++)
127  {
128  // set function value
129  if (_prop_F)
130  (*_prop_F)[_qp] = computeF();
131 
132  for (unsigned int i = 0; i < _nargs; ++i)
133  {
134  // set first derivatives
135  if (_prop_dF[i])
136  (*_prop_dF[i])[_qp] = computeDF(_arg_numbers[i]);
137 
138  // second derivatives
139  for (unsigned int j = i; j < _nargs; ++j)
140  {
141  if (_prop_d2F[i][j])
142  (*_prop_d2F[i][j])[_qp] = computeD2F(_arg_numbers[i], _arg_numbers[j]);
143 
144  // third derivatives
145  if (_third_derivatives)
146  {
147  for (unsigned int k = j; k < _nargs; ++k)
148  if (_prop_d3F[i][j][k])
149  (*_prop_d3F[i][j][k])[_qp] =
151  }
152  }
153  }
154  }
155 }
std::vector< std::vector< MaterialProperty< Real > * > > _prop_d2F
Material properties to store the second derivatives.
DerivativeFunctionMaterialBase(const InputParameters &parameters)
virtual Real computeD2F(unsigned int arg1, unsigned int arg2)
Override this method to calculate the second derivatives.
Material base class central for all Materials that provide a Function as a material property value...
std::vector< std::vector< std::vector< MaterialProperty< Real > * > > > _prop_d3F
Material properties to store the third derivatives.
MaterialProperty< Real > * _prop_F
Material property to store the function value.
virtual Real computeD3F(unsigned int, unsigned int, unsigned int)
Override this method to calculate the third derivatives.
virtual Real computeF()
Override this method to provide the free energy function.
bool _third_derivatives
Calculate (and allocate memory for) the third derivatives of the free energy.
InputParameters validParams< DerivativeFunctionMaterialBase >()
unsigned int _nargs
Number of coupled arguments.
InputParameters validParams< FunctionMaterialBase >()
std::vector< MaterialProperty< Real > * > _prop_dF
Material properties to store the derivatives of f with respect to arg[i].
virtual Real computeDF(unsigned int arg)
Override this method for calculating the first derivatives.
std::string _F_name
Name of the function value material property and used as a base name to concatenate the material prop...
std::vector< std::string > _arg_names
String vector of all argument names.
virtual void initialSetup()
Check if we got the right number of components in the &#39;args&#39; coupled variable vector.
std::vector< unsigned int > _arg_numbers
Vector of all argument MOOSE variable numbers.