Skip to content

Commit

Permalink
Add ADReaction (idaholab#21009)
Browse files Browse the repository at this point in the history
  • Loading branch information
cticenhour committed May 13, 2022
1 parent 0dde90f commit 4171269
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 18 deletions.
13 changes: 6 additions & 7 deletions framework/doc/content/source/kernels/Reaction.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Reaction
# Reaction / ADReaction

## Description

`Reaction` implements a simple first-order reaction term where the rate of
reaction is directly proportional to the governing variable $u$. Its weak form
is given by
`Reaction` (and its automatic differentiation version, `ADReaction`) implements
a simple first-order reaction term where the rate of reaction is directly proportional
to the governing variable $u$. Its weak form is given by
\begin{equation}
(\psi_i, \lambda u_h)
\end{equation}
Expand All @@ -21,9 +21,8 @@ parameters. An example block is shown below for a diffusion-reaction equation:
!listing test/tests/fvkernels/fv_coupled_var/coupled.i block=Kernels

!alert note
There is no AD (automatic differentiation) or FV (finite volume) version of `Reaction`.
If you wish to use AD / FV, use [ADCoupledForce](/ADCoupledForce.md) /
[FVCoupledForce](/FVCoupledForce.md) respectively.
There is no FV (finite volume) version of `Reaction`. If you wish to use FV, use
[/FVCoupledForce.md].

!syntax parameters /Kernels/Reaction

Expand Down
19 changes: 15 additions & 4 deletions framework/include/kernels/Reaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,29 @@

#pragma once

#include "Kernel.h"
#include "GenericKernel.h"

class Reaction : public Kernel
/**
* Implements a simple consuming reaction term with weak form $(\\psi_i, \\lambda u_h)$.
*/

template <bool is_ad>
class ReactionTempl : public GenericKernel<is_ad>
{
public:
static InputParameters validParams();

Reaction(const InputParameters & parameters);
ReactionTempl(const InputParameters & parameters);

protected:
virtual Real computeQpResidual() override;
virtual GenericReal<is_ad> computeQpResidual() override;
virtual Real computeQpJacobian() override;

/// Scalar coefficient representing the relative amount consumed per unit time
const Real & _rate;

usingGenericKernelMembers;
};

typedef ReactionTempl<false> Reaction;
typedef ReactionTempl<true> ADReaction;
25 changes: 18 additions & 7 deletions framework/src/kernels/Reaction.C
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,42 @@
#include "Reaction.h"

registerMooseObject("MooseApp", Reaction);
registerMooseObject("MooseApp", ADReaction);

template <bool is_ad>
InputParameters
Reaction::validParams()
ReactionTempl<is_ad>::validParams()
{
InputParameters params = Kernel::validParams();
InputParameters params = GenericKernel<is_ad>::validParams();
params.addClassDescription(
"Implements a simple consuming reaction term with weak form $(\\psi_i, \\lambda u_h)$.");
params.addParam<Real>(
"rate", 1.0, "The $(\\lambda)$ multiplier, the relative amount consumed per unit time.");
return params;
}

Reaction::Reaction(const InputParameters & parameters)
: Kernel(parameters), _rate(getParam<Real>("rate"))
template <bool is_ad>
ReactionTempl<is_ad>::ReactionTempl(const InputParameters & parameters)
: GenericKernel<is_ad>(parameters), _rate(this->template getParam<Real>("rate"))
{
}

Real
Reaction::computeQpResidual()
template <bool is_ad>
GenericReal<is_ad>
ReactionTempl<is_ad>::computeQpResidual()
{
return _test[_i][_qp] * _rate * _u[_qp];
}

template <bool is_ad>
Real
Reaction::computeQpJacobian()
ReactionTempl<is_ad>::computeQpJacobian()
{
// This function will never be called for the AD version. But because C++ does
// not support an optional function declaration based on a template parameter,
// we must keep this this template for all cases.
return _test[_i][_qp] * _rate * _phi[_j][_qp];
}

template class ReactionTempl<false>;
template class ReactionTempl<true>;
46 changes: 46 additions & 0 deletions test/tests/kernels/ad_reaction/ad_reaction.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[Mesh]
type = GeneratedMesh
dim = 2
nx = 5
ny = 5
[]

[Variables]
[u]
[]
[]

[Kernels]
[diffusion]
type = ADDiffusion
variable = u
[]

[reaction]
type = ADReaction
variable = u
[]

[force]
type = ADBodyForce
variable = u
[]
[]

[BCs]
[left]
type = ADDirichletBC
boundary = left
variable = u
value = 0
[]
[]

[Executioner]
type = Steady
solve_type = NEWTON
[]

[Outputs]
exodus = true
[]
Binary file not shown.
17 changes: 17 additions & 0 deletions test/tests/kernels/ad_reaction/tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Tests]
[ad_reaction]
type = 'Exodiff'
input = 'ad_reaction.i'
exodiff = 'ad_reaction_out.e'
requirement = 'The system shall contain a consuming reaction term object whose Jacobian is calculated via forward automatic differentiation.'
design = 'Reaction.md'
issues = '#21009'
[]
[ad_reaction_jac]
type = PetscJacobianTester
input = 'ad_reaction.i'
requirement = 'The system shall produce a perfect Jacobian for a consuming reaction term object using forward automatic differentiation.'
design = 'Reaction.md'
issues = '#21009'
[]
[]

0 comments on commit 4171269

Please sign in to comment.