Skip to content
Permalink
Browse files

Merge pull request #13485 from dschwen/admatreaction_13484

Add ADMatReaction
  • Loading branch information...
lindsayad committed Jun 4, 2019
2 parents 50cc285 + f293f6a commit 99559df74d843adf128ad33911509a692a241ab1
@@ -11,7 +11,7 @@

#include "ADDiffusion.h"

template <ComputeStage compute_stage>
template <ComputeStage>
class ADHeatConduction;

declareADValidParams(ADHeatConduction);
@@ -29,4 +29,3 @@ class ADHeatConduction : public ADDiffusion<compute_stage>

usingKernelGradMembers;
};

@@ -0,0 +1,24 @@
# ADMatReaction

!syntax description /Kernels/ADMatReaction

Implements
\begin{equation}
(-L(v,a,b,\dots) v, \psi),
\end{equation}
where $L$ (`mob_name`) is a reaction rate, $v$ is either a coupled variable
(`v`) or - if not explicitly specified - the non-linear variable the kernel is
operating on.

Forward automatic differentiation is used to compute all on and off-diagonal
Jacobian contributions.

!alert warning
Note the negative sign, which does *not* appear in [Reaction](/Reaction.md) or
[CoefReaction](/CoefReaction.md).

!syntax parameters /Kernels/ADMatReaction

!syntax inputs /Kernels/ADMatReaction

!syntax children /Kernels/ADMatReaction
@@ -0,0 +1,44 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

#include "ADKernel.h"

// Forward Declaration
template <ComputeStage>
class ADMatReaction;

declareADValidParams(ADMatReaction);

/**
* This kernel adds to the residual a contribution of \f$ -L*v \f$ where \f$ L \f$ is a material
* property and \f$ v \f$ is a variable (nonlinear or coupled).
*/
template <ComputeStage compute_stage>
class ADMatReaction : public ADKernel<compute_stage>
{
public:
ADMatReaction(const InputParameters & parameters);

protected:
virtual ADReal computeQpResidual();

/**
* Kernel variable (can be nonlinear or coupled variable)
* (For constrained Allen-Cahn problems, v = lambda
* where lambda is the Lagrange multiplier)
*/
const ADVariableValue & _v;

/// Reaction rate
const ADMaterialProperty(Real) & _mob;

usingKernelMembers;
};
@@ -0,0 +1,38 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "ADMatReaction.h"

registerADMooseObject("PhaseFieldApp", ADMatReaction);

defineADValidParams(
ADMatReaction,
ADKernel,
params.addCoupledVar("v",
"Set this to make v a coupled variable, otherwise it will use the "
"kernel's nonlinear variable for v");
params.addClassDescription("Kernel to add -L*v, where L=reaction rate, v=variable");
params.addParam<MaterialPropertyName>("mob_name",
"L",
"The reaction rate used with the kernel"););

template <ComputeStage compute_stage>
ADMatReaction<compute_stage>::ADMatReaction(const InputParameters & parameters)
: ADKernel<compute_stage>(parameters),
_v(isCoupled("v") ? adCoupledValue("v") : _u),
_mob(adGetADMaterialProperty<Real>("mob_name"))
{
}

template <ComputeStage compute_stage>
ADReal
ADMatReaction<compute_stage>::computeQpResidual()
{
return -_mob[_qp] * _test[_i][_qp] * _v[_qp];
}
@@ -35,7 +35,8 @@ class ADTestDerivativeFunction : public ADMaterial<compute_stage>,
enum class FunctionEnum
{
F1,
F2
F2,
F3
} _function;

/// Coupled variable value for the order parameter
@@ -52,4 +53,3 @@ class ADTestDerivativeFunction : public ADMaterial<compute_stage>,

usingMaterialMembers;
};

@@ -16,10 +16,12 @@ defineADValidParams(
ADMaterial,
params.addClassDescription(
"Material that implements the a function of one variable and its first derivative.");
MooseEnum functionEnum("F1 F2");
MooseEnum functionEnum("F1 F2 F3");
params.addRequiredParam<MooseEnum>("function",
functionEnum,
"F1 = 2 op^2 (1 - op)^2 - 0.2 op; F2 = 0.1 * op^2 + 0.1");
"F1 = 2 op[0]^2 (1 - op[0])^2 - 0.2 op[0]; "
"F2 = 0.1 op[0]^2 + op[1]^2; "
"F3 = op[0] * op[1]");
params.addParam<MaterialPropertyName>("f_name", "F", "function property name");
params.addRequiredCoupledVar("op", "Order parameter variables"););

@@ -44,6 +46,8 @@ ADTestDerivativeFunction<compute_stage>::ADTestDerivativeFunction(
paramError("op", "Specify exactly one variable to an F1 type function.");
if (_function == FunctionEnum::F2 && _op.size() != 2)
paramError("op", "Specify exactly two variables to an F2 type function.");
if (_function == FunctionEnum::F3 && _op.size() != 2)
paramError("op", "Specify exactly two variables to an F3 type function.");
}

template <ComputeStage compute_stage>
@@ -68,6 +72,15 @@ ADTestDerivativeFunction<compute_stage>::computeQpProperties()
break;
}

case FunctionEnum::F3:
{
const ADReal & b = (*_op[1])[_qp];
_prop_F[_qp] = a * b;
(*_prop_dFdop[0])[_qp] = b;
(*_prop_dFdop[1])[_qp] = a;
break;
}

default:
mooseError("Invalid function enum value");
}
@@ -0,0 +1,71 @@
[Mesh]
type = GeneratedMesh
dim = 1
nx = 20
[]

[Variables]
[./u]
[./InitialCondition]
type = FunctionIC
function = cos(x*2*pi)
[../]
[../]
[./v]
[./InitialCondition]
type = FunctionIC
function = sin(x*2*pi)
[../]
[../]
[]

[Kernels]
[./dudt]
type = ADTimeDerivative
variable = u
[../]
[./dvdt]
type = ADTimeDerivative
variable = v
[../]
[./u]
type = ADMatReaction
variable = u
#v = v
[../]
[./v]
type = ADMatReaction
variable = v
[../]
[]

[Materials]
[./L]
type = ADTestDerivativeFunction
function = F3
f_name = L
op = 'u v'
[../]
[]

[Preconditioning]
[./smp]
type = SMP
full = true
[../]
[]

[Debug]
show_var_residual_norms = true
[]

[Executioner]
type = Transient
solve_type = NEWTON
dt = 0.1
num_steps = 5
[]

[Outputs]
exodus = true
[]
Binary file not shown.
@@ -0,0 +1,19 @@
[Tests]
issues = '#13484'
design = 'ADMatReaction.md'
[./admatreaction]
type = Exodiff
input = 'admatreaction.i'
exodiff = 'admatreaction_out.e'
allow_test_objects = true
requirement = 'MOOSE shall provide an automatic differentiation mat reaction kernel'
[../]
[./admatreaction-jac]
type = 'PetscJacobianTester'
input = 'admatreaction.i'
run_sim = 'True'
cli_args = 'Outputs/exodus=false'
allow_test_objects = true
requirement = 'The Jacobian for the automatic differentiation mat reaction kernel shall be perfect'
[../]
[]

0 comments on commit 99559df

Please sign in to comment.
You can’t perform that action at this time.