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