Skip to content

Commit

Permalink
Add ADCoefReaction (idaholab#21009)
Browse files Browse the repository at this point in the history
  • Loading branch information
cticenhour committed May 11, 2022
1 parent 27fc7d6 commit b7d8501
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
7 changes: 3 additions & 4 deletions framework/doc/content/source/kernels/CoefReaction.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# CoefReaction
# CoefReaction / ADCoefReaction

!syntax description /Kernels/CoefReaction

Expand All @@ -9,9 +9,8 @@
where $v$ (`v`) is a coupled variable.

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

!syntax parameters /Kernels/CoefReaction

Expand Down
10 changes: 7 additions & 3 deletions framework/include/kernels/CoefReaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@

#include "Reaction.h"

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

CoefReaction(const InputParameters & parameters);
CoefReactionTempl(const InputParameters & parameters);

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

/// input parameter multiplied by the reaction kernel
const Real _coef;
};

typedef CoefReactionTempl<false> CoefReaction;
typedef CoefReactionTempl<true> ADCoefReaction;
29 changes: 20 additions & 9 deletions framework/src/kernels/CoefReaction.C
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,40 @@
#include "CoefReaction.h"

registerMooseObject("MooseApp", CoefReaction);
registerMooseObject("MooseApp", ADCoefReaction);

template <bool is_ad>
InputParameters
CoefReaction::validParams()
CoefReactionTempl<is_ad>::validParams()
{
InputParameters params = Reaction::validParams();
InputParameters params = ReactionTempl<is_ad>::validParams();
params.addClassDescription("Implements the residual term (p*u, test)");
params.addParam<Real>("coefficient", 1.0, "Coefficient of the term");
return params;
}

CoefReaction::CoefReaction(const InputParameters & parameters)
: Reaction(parameters), _coef(getParam<Real>("coefficient"))
template <bool is_ad>
CoefReactionTempl<is_ad>::CoefReactionTempl(const InputParameters & parameters)
: ReactionTempl<is_ad>(parameters), _coef(this->template getParam<Real>("coefficient"))
{
}

Real
CoefReaction::computeQpResidual()
template <bool is_ad>
GenericReal<is_ad>
CoefReactionTempl<is_ad>::computeQpResidual()
{
return _coef * Reaction::computeQpResidual();
return _coef * ReactionTempl<is_ad>::computeQpResidual();
}

template <bool is_ad>
Real
CoefReaction::computeQpJacobian()
CoefReactionTempl<is_ad>::computeQpJacobian()
{
return _coef * Reaction::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 _coef * ReactionTempl<is_ad>::computeQpJacobian();
}

template class CoefReactionTempl<false>;
template class CoefReactionTempl<true>;
Binary file not shown.
18 changes: 18 additions & 0 deletions test/tests/kernels/ad_reaction/tests
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,22 @@
design = 'Reaction.md'
issues = '#21009'
[]

[ad_coef_reaction]
type = 'Exodiff'
input = 'ad_reaction.i'
cli_args = 'Kernels/reaction/type=ADCoefReaction Kernels/reaction/coefficient=2 Outputs/file_base=ad_coef_reaction_out'
exodiff = 'ad_coef_reaction_out.e'
requirement = 'The system shall contain a consuming reaction term object with a scalar coefficient whose Jacobian is calculated via automatic forward differentiation.'
design = 'CoefReaction.md'
issues = '#21009'
[]
[ad_coef_reaction_jac]
type = PetscJacobianTester
input = 'ad_reaction.i'
cli_args = 'Kernels/reaction/type=ADCoefReaction Kernels/reaction/coefficient=2'
requirement = 'The system shall produce a perfect Jacobian for a consuming reaction term object with a scalar coefficient using automatic forward differentiation.'
design = 'CoefReaction.md'
issues = '#21009'
[]
[]

0 comments on commit b7d8501

Please sign in to comment.