https://mooseframework.inl.gov
Loading...
Searching...
No Matches
DerivativeSumMaterial.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
16
17template <bool is_ad>
20{
22 params.addClassDescription("Meta-material to sum up multiple derivative materials");
23 params.addParam<std::vector<std::string>>("sum_materials",
24 "Base name of the parsed sum material property");
25
26 // All arguments of the parsed expression (free energy) being summed
27 params.addRequiredCoupledVar("coupled_variables", "Vector of names of variables being summed");
28
29 params.addCoupledVar("displacement_gradients",
30 "Vector of displacement gradient variables (see "
31 "Modules/PhaseField/DisplacementGradients "
32 "action)");
33
34 // Advanced arguments to construct a sum of the form \f$ c+\gamma\sum_iF_i \f$
35 params.addParam<std::vector<Real>>("prefactor", {}, "Prefactor to multiply the sum term with.");
36 params.addParam<Real>("constant", 0.0, "Constant to be added to the prefactor multiplied sum.");
37
38 params.addParam<bool>("validate_coupling",
39 true,
40 "Check if all variables the specified materials depend on are listed in "
41 "the `coupled_variables` parameter.");
42 params.addParamNamesToGroup("prefactor constant", "Advanced");
43
44 return params;
45}
46
47template <bool is_ad>
49 : DerivativeFunctionMaterialBaseTempl<is_ad>(parameters),
50 _sum_materials(this->template getParam<std::vector<std::string>>("sum_materials")),
51 _num_materials(_sum_materials.size()),
52 _prefactor(_num_materials, 1.0),
53 _constant(this->template getParam<Real>("constant")),
54 _validate_coupling(this->template getParam<bool>("validate_coupling"))
55{
56 // we need at least one material in the sum
57 if (_num_materials == 0)
58 mooseError("Please supply at least one material to sum in DerivativeSumMaterial ", name());
59
60 // get prefactor values if not 1.0
61 std::vector<Real> p = this->template getParam<std::vector<Real>>("prefactor");
62
63 // if prefactor is used we need the same number of prefactors as sum materials
64 if (_num_materials == p.size())
65 _prefactor = p;
66 else if (p.size() != 0)
67 mooseError("Supply the same number of sum materials and prefactors.");
68
69 // reserve space for summand material properties
74
75 for (unsigned int n = 0; n < _num_materials; ++n)
76 {
77 _summand_F[n] = &this->template getGenericMaterialProperty<Real, is_ad>(_sum_materials[n]);
78 _summand_dF[n].resize(_nargs);
79 _summand_d2F[n].resize(_nargs);
80 _summand_d3F[n].resize(_nargs);
81
82 for (unsigned int i = 0; i < _nargs; ++i)
83 {
84 _summand_dF[n][i] = &this->template getMaterialPropertyDerivative<Real, is_ad>(
86 _summand_d2F[n][i].resize(_nargs);
87
89 _summand_d3F[n][i].resize(_nargs);
90
91 for (unsigned int j = 0; j < _nargs; ++j)
92 {
93 _summand_d2F[n][i][j] = &this->template getMaterialPropertyDerivative<Real, is_ad>(
95
97 {
98 _summand_d3F[n][i][j].resize(_nargs);
99
100 for (unsigned int k = 0; k < _nargs; ++k)
101 _summand_d3F[n][i][j][k] = &this->template getMaterialPropertyDerivative<Real, is_ad>(
103 }
104 }
105 }
106 }
107}
108
109template <bool is_ad>
110void
112{
113 if (_validate_coupling)
114 for (unsigned int n = 0; n < _num_materials; ++n)
115 this->template validateCoupling<Real>(_sum_materials[n]);
116}
117
118template <bool is_ad>
119void
121{
122 unsigned int i, j, k;
123
124 for (_qp = 0; _qp < _qrule->n_points(); _qp++)
125 {
126 // set function value
127 if (_prop_F)
128 {
129 (*_prop_F)[_qp] = (*_summand_F[0])[_qp] * _prefactor[0];
130 for (unsigned int n = 1; n < _num_materials; ++n)
131 (*_prop_F)[_qp] += (*_summand_F[n])[_qp] * _prefactor[n];
132 }
133
134 for (i = 0; i < _nargs; ++i)
135 {
136 // set first derivatives
137 if (_prop_dF[i])
138 {
139 (*_prop_dF[i])[_qp] = (*_summand_dF[0][i])[_qp] * _prefactor[0];
140 for (unsigned int n = 1; n < _num_materials; ++n)
141 (*_prop_dF[i])[_qp] += (*_summand_dF[n][i])[_qp] * _prefactor[n];
142 }
143
144 // second derivatives
145 for (j = i; j < _nargs; ++j)
146 {
147 if (_prop_d2F[i][j])
148 {
149 (*_prop_d2F[i][j])[_qp] = (*_summand_d2F[0][i][j])[_qp] * _prefactor[0];
150 for (unsigned int n = 1; n < _num_materials; ++n)
151 (*_prop_d2F[i][j])[_qp] += (*_summand_d2F[n][i][j])[_qp] * _prefactor[n];
152 }
153
154 // third derivatives
155 if (_third_derivatives)
156 {
157 for (k = j; k < _nargs; ++k)
158 if (_prop_d3F[i][j][k])
159 {
160 (*_prop_d3F[i][j][k])[_qp] = (*_summand_d3F[0][i][j][k])[_qp] * _prefactor[0];
161 for (unsigned int n = 1; n < _num_materials; ++n)
162 (*_prop_d3F[i][j][k])[_qp] += (*_summand_d3F[n][i][j][k])[_qp] * _prefactor[n];
163 }
164 }
165 }
166 }
167 }
168}
registerMooseObject("MooseApp", DerivativeSumMaterial)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
Material base class to compute a function and its derivatives.
static InputParameters validParams()
bool _third_derivatives
Calculate (and allocate memory for) the third derivatives of the free energy.
std::vector< std::vector< std::vector< std::vector< const GenericMaterialProperty< Real, is_ad > * > > > > _summand_d3F
Third derivatives of the summands.
DerivativeSumMaterialTempl(const InputParameters &parameters)
std::vector< std::vector< const GenericMaterialProperty< Real, is_ad > * > > _summand_dF
Derivatives of the summands with respect to arg[i].
virtual void computeProperties()
Performs the quadrature point loop, calling computeQpProperties.
std::vector< std::vector< std::vector< const GenericMaterialProperty< Real, is_ad > * > > > _summand_d2F
Second derivatives of the summands.
std::vector< std::string > _sum_materials
std::vector< Real > _prefactor
arguments to construct a sum of the form
virtual void initialSetup()
Check if we got the right number of components in the 'coupled_variables' vector.
static InputParameters validParams()
std::vector< const GenericMaterialProperty< Real, is_ad > * > _summand_F
Function values of the summands.
std::vector< std::string > _arg_names
String vector of all argument names.
unsigned int _nargs
Number of coupled arguments.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...
void addRequiredCoupledVar(const std::string &name, const std::string &doc_string)
This method adds a coupled variable name pair.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
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.
void addCoupledVar(const std::string &name, const std::string &doc_string)
This method adds a coupled variable name pair.
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103