www.mooseframework.org
CahnHilliardBase.h
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 #ifndef CAHNHILLIARDBASE_H
8 #define CAHNHILLIARDBASE_H
9 
10 #include "CHBulk.h"
11 
17 template <typename T>
18 class CahnHilliardBase : public CHBulk<T>
19 {
20 public:
21  CahnHilliardBase(const InputParameters & parameters);
22 
23  static InputParameters validParams();
24  virtual void initialSetup();
25 
26 protected:
28  virtual RealGradient computeGradDFDCons(PFFunctionType type);
29  virtual Real computeQpOffDiagJacobian(unsigned int jvar);
30 
31  // Explicitly declare the use of the following members of the parent class
32  // https://isocpp.org/wiki/faq/templates#nondependent-name-lookup-members
33  using CHBulk<T>::_M;
34  using CHBulk<T>::_i;
35  using CHBulk<T>::_j;
36  using CHBulk<T>::_qp;
37  using CHBulk<T>::_var;
38  using CHBulk<T>::_phi;
39  using CHBulk<T>::_grad_u;
43 
44 private:
45  const unsigned int _nvar;
46  std::vector<const MaterialProperty<Real> *> _second_derivatives;
47  std::vector<const MaterialProperty<Real> *> _third_derivatives;
48  std::vector<std::vector<const MaterialProperty<Real> *>> _third_cross_derivatives;
49  std::vector<const VariableGradient *> _grad_vars;
50 };
51 
52 template <typename T>
53 InputParameters
55 {
56  InputParameters params = CHBulk<Real>::validParams();
57  params.addClassDescription("Cahn-Hilliard Kernel that uses a DerivativeMaterial Free Energy");
58  params.addRequiredParam<MaterialPropertyName>(
59  "f_name", "Base name of the free energy function F defined in a DerivativeParsedMaterial");
60  params.addCoupledVar("displacement_gradients",
61  "Vector of displacement gradient variables (see "
62  "Modules/PhaseField/DisplacementGradients "
63  "action)");
64  return params;
65 }
66 
67 template <typename T>
68 CahnHilliardBase<T>::CahnHilliardBase(const InputParameters & parameters)
69  : CHBulk<T>(parameters),
70  _nvar(_coupled_moose_vars.size()),
74  _grad_vars(_nvar + 1)
75 {
76  // derivatives w.r.t. and gradients of the kernel variable
78  &this->template getMaterialPropertyDerivative<Real>("f_name", _var.name(), _var.name());
79  _third_derivatives[0] = &this->template getMaterialPropertyDerivative<Real>(
80  "f_name", _var.name(), _var.name(), _var.name());
81  _grad_vars[0] = &(_grad_u);
82 
83  // Iterate over all coupled variables
84  for (unsigned int i = 0; i < _nvar; ++i)
85  {
86  const VariableName iname = _coupled_moose_vars[i]->name();
87  if (iname == _var.name())
88  mooseError("The kernel variable should not be specified in the coupled `args` parameter.");
89 
90  _second_derivatives[i + 1] =
91  &this->template getMaterialPropertyDerivative<Real>("f_name", _var.name(), iname);
92  _third_derivatives[i + 1] = &this->template getMaterialPropertyDerivative<Real>(
93  "f_name", _var.name(), _var.name(), iname);
94 
95  _third_cross_derivatives[i].resize(_nvar);
96  for (unsigned int j = 0; j < _nvar; ++j)
97  {
98  VariableName jname = _coupled_moose_vars[j]->name();
100  &this->template getMaterialPropertyDerivative<Real>("f_name", _var.name(), iname, jname);
101  }
102 
103  _grad_vars[i + 1] = &(_coupled_moose_vars[i]->gradSln());
104  }
105 }
106 
107 template <typename T>
108 void
110 {
116  this->template validateCoupling<Real>("f_name", _var.name());
117  this->template validateDerivativeMaterialPropertyBase<Real>("f_name");
118 }
119 
120 template <typename T>
121 RealGradient
123 {
124  RealGradient res = 0.0;
125 
126  switch (type)
127  {
128  case CHBulk<T>::Residual:
129  for (unsigned int i = 0; i <= _nvar; ++i)
130  res += (*_grad_vars[i])[_qp] * (*_second_derivatives[i])[_qp];
131  return res;
132 
133  case CHBulk<T>::Jacobian:
134  res = _grad_phi[_j][_qp] * (*_second_derivatives[0])[_qp];
135  for (unsigned int i = 0; i <= _nvar; ++i)
136  res += _phi[_j][_qp] * (*_grad_vars[i])[_qp] * (*_third_derivatives[i])[_qp];
137  return res;
138  }
139 
140  mooseError("Internal error");
141 }
142 
143 template <typename T>
144 Real
146 {
147  // get the coupled variable jvar is referring to
148  const unsigned int cvar = this->mapJvarToCvar(jvar);
149 
150  RealGradient J = _grad_u[_qp] * _phi[_j][_qp] * (*_third_derivatives[cvar + 1])[_qp] +
151  _grad_phi[_j][_qp] * (*_second_derivatives[cvar + 1])[_qp];
152 
153  for (unsigned int i = 0; i < _nvar; ++i)
154  J += _phi[_j][_qp] * (*_grad_vars[i + 1])[_qp] * (*_third_cross_derivatives[i][cvar])[_qp];
155 
156  return CHBulk<T>::computeQpOffDiagJacobian(jvar) + _M[_qp] * _grad_test[_i][_qp] * J;
157 }
158 
159 #endif // CAHNHILLIARDBASE_H
virtual void initialSetup()
CahnHilliardBase implements the residual of the Cahn-Hilliard equation in a general way that can be t...
virtual RealGradient computeGradDFDCons(PFFunctionType type)
This is the Cahn-Hilliard equation base class that implements the bulk or local energy term of the eq...
Definition: CHBulk.h:23
CHBulk< T >::PFFunctionType PFFunctionType
const MaterialProperty< T > & _M
Mobility.
Definition: CHBulk.h:45
virtual Real computeQpOffDiagJacobian(unsigned int jvar)
Definition: CHBulk.h:108
virtual Real computeQpOffDiagJacobian(unsigned int jvar)
static InputParameters validParams()
const unsigned int _nvar
std::vector< const MaterialProperty< Real > * > _second_derivatives
std::vector< const VariableGradient * > _grad_vars
std::vector< const MaterialProperty< Real > * > _third_derivatives
PFFunctionType
Definition: CHBulk.h:36
CahnHilliardBase(const InputParameters &parameters)
std::vector< std::vector< const MaterialProperty< Real > * > > _third_cross_derivatives
static InputParameters validParams()
Definition: CHBulk.h:73