Skip to content

Commit

Permalink
Combine CoupledForce and ADCoupledForce to reduce code duplication (i…
Browse files Browse the repository at this point in the history
…daholab#21009)

Also restore ADCoupledForce error check test for using the same coupled and object variables
  • Loading branch information
cticenhour committed May 12, 2022
1 parent 7244c9e commit 0dde90f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 143 deletions.
38 changes: 0 additions & 38 deletions framework/doc/content/source/kernels/ADCoupledForce.md

This file was deleted.

6 changes: 3 additions & 3 deletions framework/doc/content/source/kernels/CoupledForce.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# CoupledForce
# CoupledForce / ADCoupledForce

## Description

`CoupledForce` implements a source term within the domain $\Omega$ proportional to a coupled
`CoupledForce` (and the AD version, `ADCoupledForce`) implements a source term within the domain $\Omega$ proportional to a coupled
variable:
\begin{equation}
\underbrace{-\sigma v}_{\textrm{CoupledForce}} + \sum_{i=1}^n \beta_i = 0 \in \Omega,
Expand All @@ -19,7 +19,7 @@ R_i(u_h) = (\psi_i, -\sigma v) \quad \forall \psi_i,
where $\psi_i$ are the test functions and $u_h \in \mathcal{S}^h$
is the finite element solution of the weak formulation.

The corresponding Jacobian is
The corresponding Jacobian (used in `CoupledForce`) is
\begin{equation}
\frac{\partial R_i(u_h)}{\partial u_j} = (\psi_i, -\sigma \phi_j).
\end{equation}
Expand Down
35 changes: 0 additions & 35 deletions framework/include/kernels/ADCoupledForce.h

This file was deleted.

24 changes: 16 additions & 8 deletions framework/include/kernels/CoupledForce.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,35 @@

#pragma once

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

/**
* Simple class to demonstrate off diagonal Jacobian contributions.
* Implements a source term proportional to the value of a coupled variable. Weak form: $(\\psi_i,
* -\\sigma v)$.
*/
class CoupledForce : public Kernel
template <bool is_ad>
class CoupledForceTempl : public GenericKernel<is_ad>
{
public:
static InputParameters validParams();

CoupledForce(const InputParameters & parameters);
CoupledForceTempl(const InputParameters & parameters);

protected:
virtual Real computeQpResidual() override;

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

virtual Real computeQpOffDiagJacobian(unsigned int jvar) override;

usingGenericKernelMembers;

private:
/// Coupled variable number
unsigned int _v_var;
const VariableValue & _v;
/// Coupled variable
const GenericVariableValue<is_ad> & _v;
/// Multiplier for the coupled force term
Real _coef;
};

typedef CoupledForceTempl<false> CoupledForce;
typedef CoupledForceTempl<true> ADCoupledForce;
40 changes: 0 additions & 40 deletions framework/src/kernels/ADCoupledForce.C

This file was deleted.

40 changes: 23 additions & 17 deletions framework/src/kernels/CoupledForce.C
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

#include "CoupledForce.h"

#include "MooseVariable.h"

registerMooseObject("MooseApp", CoupledForce);
registerMooseObject("MooseApp", ADCoupledForce);

template <bool is_ad>
InputParameters
CoupledForce::validParams()
CoupledForceTempl<is_ad>::validParams()
{
InputParameters params = Kernel::validParams();
InputParameters params = GenericKernel<is_ad>::validParams();

params.addClassDescription("Implements a source term proportional to the value of a coupled "
"variable. Weak form: $(\\psi_i, -\\sigma v)$.");
Expand All @@ -27,30 +27,36 @@ CoupledForce::validParams()
return params;
}

CoupledForce::CoupledForce(const InputParameters & parameters)
: Kernel(parameters), _v_var(coupled("v")), _v(coupledValue("v")), _coef(getParam<Real>("coef"))
template <bool is_ad>
CoupledForceTempl<is_ad>::CoupledForceTempl(const InputParameters & parameters)
: GenericKernel<is_ad>(parameters),
_v_var(coupled("v")),
_v(this->template coupledGenericValue<is_ad>("v")),
_coef(this->template getParam<Real>("coef"))
{
if (_var.number() == _v_var)
mooseError("Coupled variable 'v' needs to be different from 'variable' with CoupledForce, "
"consider using the CoefReaction kernel or something similar");
mooseError("Coupled variable 'v' needs to be different from 'variable' with CoupledForce / "
"ADCoupledForce, consider using the CoefReaction kernel or something similar");
}

Real
CoupledForce::computeQpResidual()
template <bool is_ad>
GenericReal<is_ad>
CoupledForceTempl<is_ad>::computeQpResidual()
{
return -_coef * _v[_qp] * _test[_i][_qp];
}

template <bool is_ad>
Real
CoupledForce::computeQpJacobian()
{
return 0;
}

Real
CoupledForce::computeQpOffDiagJacobian(unsigned int jvar)
CoupledForceTempl<is_ad>::computeQpOffDiagJacobian(unsigned int jvar)
{
// 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.
if (jvar == _v_var)
return -_coef * _phi[_j][_qp] * _test[_i][_qp];
return 0.0;
}

template class CoupledForceTempl<false>;
template class CoupledForceTempl<true>;
2 changes: 1 addition & 1 deletion test/tests/kernels/ad_coupled_force/tests
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Tests]
issues = "#18214"
design = "ADCoupledForce.md"
design = "CoupledForce.md"
[jacobians]
requirement = "The system shall provide a source term proportional to the value of"
[aux]
Expand Down
9 changes: 8 additions & 1 deletion test/tests/misc/check_error/tests
Original file line number Diff line number Diff line change
Expand Up @@ -884,10 +884,17 @@
type = 'RunException'
input = 'coupling_itself.i'
expect_err = "Coupled variable 'v' needs to be different from 'variable' with CoupledForce"

requirement = 'The system shall report an error when an attempt is made to couple a variable with itself.'
issues = '#10398'
[../]
[coupling_itself_ad]
type = 'RunException'
input = 'coupling_itself.i'
cli_args = 'Kernels/coupled/type=ADCoupledForce'
expect_err = "Coupled variable 'v' needs to be different from 'variable' with CoupledForce / ADCoupledForce"
requirement = 'The system shall report an error when an attempt is made to couple a variable with itself in coupled force term using automated differentiation.'
issues = '#18214'
[]

[./missing_input]
type = 'RunException'
Expand Down

0 comments on commit 0dde90f

Please sign in to comment.