Skip to content

Commit 4e5abd1

Browse files
authored
Merge pull request #29888 from tophmatthews/combine_ad_nonad_mat_diffusion_15915
Combine ad non-AD mat diffusion
2 parents 8593683 + 7d3d0b5 commit 4e5abd1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+566
-868
lines changed

framework/doc/content/source/kernels/ADMatDiffusion.md

Lines changed: 0 additions & 26 deletions
This file was deleted.

framework/include/interfaces/Coupleable.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
using Coupleable::_zero; \
2727
using Coupleable::_grad_zero; \
2828
using Coupleable::_ad_zero; \
29-
using Coupleable::_ad_grad_zero
29+
using Coupleable::_ad_grad_zero; \
30+
using Coupleable::coupled; \
31+
using Coupleable::isCoupled; \
32+
using Coupleable::coupledComponents
3033

3134
// Forward declarations
3235
class MooseVariableScalar;

framework/include/kernels/ADMatDiffusion.h

Lines changed: 0 additions & 25 deletions
This file was deleted.

framework/include/kernels/ADMatDiffusionBase.h

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 "KernelGrad.h"
13+
#include "ADKernelGrad.h"
14+
15+
template <bool is_ad>
16+
using GenericKernelGrad = std::conditional_t<is_ad, ADKernelGrad, KernelGrad>;
17+
18+
#define usingGenericKernelGradMembers \
19+
usingFunctionInterfaceMembers; \
20+
usingPostprocessorInterfaceMembers; \
21+
usingMooseObjectMembers; \
22+
usingTransientInterfaceMembers; \
23+
usingTaggingInterfaceMembers; \
24+
usingBlockRestrictableMembers; \
25+
usingCoupleableMembers; \
26+
using GenericKernelGrad<is_ad>::_qp; \
27+
using GenericKernelGrad<is_ad>::_grad_u

framework/include/kernels/KernelGrad.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ class KernelGrad : public Kernel
4646
/**
4747
* Called before forming the jacobian for an element
4848
*/
49-
virtual RealGradient precomputeQpJacobian();
49+
virtual RealGradient precomputeQpJacobian() { return RealGradient(0.0); }
5050

51-
virtual Real computeQpResidual() override;
51+
virtual Real computeQpResidual() override final { return 0.0; }
52+
53+
virtual Real computeQpJacobian() override final { return 0.0; }
5254
};

framework/include/kernels/MatDiffusion.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@
1111

1212
#include "MatDiffusionBase.h"
1313

14+
template <bool is_ad>
15+
using MatDiffusionBaseParent = typename std::
16+
conditional<is_ad, MatDiffusionBaseTempl<Real, true>, MatDiffusionBase<Real>>::type;
17+
1418
/**
1519
* Isotropic diffusion kernel that takes a diffusion coefficient of type
1620
* Real. All logic is implemnted in the MatDiffusionBase class
1721
* template.
1822
*/
19-
class MatDiffusion : public MatDiffusionBase<Real>
23+
template <bool is_ad>
24+
class MatDiffusionTempl : public MatDiffusionBaseParent<is_ad>
2025
{
2126
public:
2227
static InputParameters validParams();
2328

24-
MatDiffusion(const InputParameters & parameters);
29+
MatDiffusionTempl(const InputParameters & parameters);
2530
};
31+
32+
typedef MatDiffusionTempl<false> MatDiffusion;
33+
typedef MatDiffusionTempl<true> ADMatDiffusion;

framework/include/kernels/MatDiffusionBase.h

Lines changed: 39 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#pragma once
1111

12-
#include "Kernel.h"
12+
#include "GenericKernelGrad.h"
1313
#include "JvarMapInterface.h"
1414
#include "DerivativeMaterialInterface.h"
1515

@@ -22,132 +22,63 @@
2222
* \tparam T Type of the diffusion coefficient parameter. This can be Real for
2323
* isotropic diffusion or RealTensorValue for the general anisotropic case.
2424
*/
25-
template <typename T>
26-
class MatDiffusionBase : public DerivativeMaterialInterface<JvarMapKernelInterface<Kernel>>
25+
template <typename T, bool is_ad>
26+
class MatDiffusionBaseTempl
27+
: public DerivativeMaterialInterface<JvarMapKernelInterface<GenericKernelGrad<is_ad>>>
2728
{
2829
public:
2930
static InputParameters validParams();
3031

31-
MatDiffusionBase(const InputParameters & parameters);
32-
33-
virtual void initialSetup() override;
32+
MatDiffusionBaseTempl(const InputParameters & parameters);
3433

3534
protected:
36-
virtual Real computeQpResidual() override;
37-
virtual Real computeQpJacobian() override;
38-
virtual Real computeQpOffDiagJacobian(unsigned int jvar) override;
39-
virtual Real computeQpCJacobian();
35+
virtual GenericRealVectorValue<is_ad> precomputeQpResidual() override;
4036

4137
/// diffusion coefficient
42-
const MaterialProperty<T> & _D;
43-
44-
/// diffusion coefficient derivative w.r.t. the kernel variable
45-
const MaterialProperty<T> & _dDdc;
46-
47-
/// diffusion coefficient derivatives w.r.t. coupled variables
48-
std::vector<const MaterialProperty<T> *> _dDdarg;
49-
50-
/// is the kernel used in a coupled form?
51-
const bool _is_coupled;
52-
53-
/// int label for the Concentration
54-
unsigned int _v_var;
38+
const GenericMaterialProperty<T, is_ad> & _diffusivity;
5539

5640
/// Gradient of the concentration
57-
const VariableGradient & _grad_v;
58-
};
59-
60-
template <typename T>
61-
InputParameters
62-
MatDiffusionBase<T>::validParams()
63-
{
64-
InputParameters params = Kernel::validParams();
65-
params.addDeprecatedParam<MaterialPropertyName>(
66-
"D_name",
67-
"The name of the diffusivity",
68-
"This parameter has been renamed to 'diffusivity', which is more mnemonic and more conducive "
69-
"to passing a number literal");
70-
params.addParam<MaterialPropertyName>(
71-
"diffusivity", "D", "The diffusivity value or material property");
72-
params.addCoupledVar("args",
73-
"Optional vector of arguments for the diffusivity. If provided and "
74-
"diffusivity is a derivative parsed material, Jacobian contributions from "
75-
"the diffusivity will be automatically computed");
76-
params.addCoupledVar("conc", "Deprecated! Use 'v' instead");
77-
params.addCoupledVar("v",
78-
"Coupled concentration variable for kernel to operate on; if this "
79-
"is not specified, the kernel's nonlinear variable will be used as "
80-
"usual");
81-
return params;
82-
}
83-
84-
template <typename T>
85-
MatDiffusionBase<T>::MatDiffusionBase(const InputParameters & parameters)
86-
: DerivativeMaterialInterface<JvarMapKernelInterface<Kernel>>(parameters),
87-
_D(isParamValid("D_name") ? getMaterialProperty<T>("D_name")
88-
: getMaterialProperty<T>("diffusivity")),
89-
_dDdc(getMaterialPropertyDerivative<T>(isParamValid("D_name") ? "D_name" : "diffusivity",
90-
_var.name())),
91-
_dDdarg(_coupled_moose_vars.size()),
92-
_is_coupled(isCoupled("v")),
93-
_v_var(_is_coupled ? coupled("v") : (isCoupled("conc") ? coupled("conc") : _var.number())),
94-
_grad_v(_is_coupled ? coupledGradient("v")
95-
: (isCoupled("conc") ? coupledGradient("conc") : _grad_u))
96-
{
97-
// deprecated variable parameter conc
98-
if (isCoupled("conc"))
99-
mooseDeprecated("In '", name(), "' the parameter 'conc' is deprecated, please use 'v' instead");
41+
const GenericVariableGradient<is_ad> & _grad_v;
10042

101-
// fetch derivatives
102-
for (unsigned int i = 0; i < _dDdarg.size(); ++i)
103-
_dDdarg[i] = &getMaterialPropertyDerivative<T>(
104-
isParamValid("D_name") ? "D_name" : "diffusivity", _coupled_moose_vars[i]->name());
105-
}
43+
usingGenericKernelGradMembers;
44+
};
10645

10746
template <typename T>
108-
void
109-
MatDiffusionBase<T>::initialSetup()
47+
class MatDiffusionBase : public MatDiffusionBaseTempl<T, false>
11048
{
111-
validateNonlinearCoupling<Real>(parameters().isParamSetByUser("D_name") ? "D_name"
112-
: "diffusivity");
113-
}
49+
public:
50+
static InputParameters validParams();
11451

115-
template <typename T>
116-
Real
117-
MatDiffusionBase<T>::computeQpResidual()
118-
{
119-
return _D[_qp] * _grad_v[_qp] * _grad_test[_i][_qp];
120-
}
52+
MatDiffusionBase(const InputParameters & parameters);
12153

122-
template <typename T>
123-
Real
124-
MatDiffusionBase<T>::computeQpJacobian()
125-
{
126-
Real sum = _phi[_j][_qp] * _dDdc[_qp] * _grad_v[_qp] * _grad_test[_i][_qp];
127-
if (!_is_coupled)
128-
sum += computeQpCJacobian();
54+
using MatDiffusionBaseTempl<T, false>::MatDiffusionBaseTempl;
12955

130-
return sum;
131-
}
56+
protected:
57+
virtual void initialSetup() override;
58+
virtual RealGradient precomputeQpJacobian() override;
59+
virtual Real computeQpOffDiagJacobian(unsigned int jvar) override;
60+
virtual RealGradient precomputeQpCJacobian();
13261

133-
template <typename T>
134-
Real
135-
MatDiffusionBase<T>::computeQpOffDiagJacobian(unsigned int jvar)
136-
{
137-
// get the coupled variable jvar is referring to
138-
const unsigned int cvar = mapJvarToCvar(jvar);
62+
/// diffusion coefficient derivative w.r.t. the kernel variable
63+
const MaterialProperty<T> & _ddiffusivity_dc;
13964

140-
Real sum = (*_dDdarg[cvar])[_qp] * _phi[_j][_qp] * _grad_v[_qp] * _grad_test[_i][_qp];
65+
/// diffusion coefficient derivatives w.r.t. coupled variables
66+
std::vector<const MaterialProperty<T> *> _ddiffusivity_darg;
14167

142-
if (_v_var == jvar)
143-
sum += computeQpCJacobian();
68+
/// is the kernel used in a coupled form?
69+
const bool _is_coupled;
14470

145-
return sum;
146-
}
71+
/// int label for the Concentration
72+
unsigned int _v_var;
14773

148-
template <typename T>
149-
Real
150-
MatDiffusionBase<T>::computeQpCJacobian()
151-
{
152-
return _D[_qp] * _grad_phi[_j][_qp] * _grad_test[_i][_qp];
153-
}
74+
using MatDiffusionBaseTempl<T, false>::_diffusivity;
75+
using MatDiffusionBaseTempl<T, false>::_qp;
76+
using MatDiffusionBaseTempl<T, false>::_grad_phi;
77+
using MatDiffusionBaseTempl<T, false>::_j;
78+
using MatDiffusionBaseTempl<T, false>::_i;
79+
using MatDiffusionBaseTempl<T, false>::_phi;
80+
using MatDiffusionBaseTempl<T, false>::_grad_v;
81+
using MatDiffusionBaseTempl<T, false>::_grad_test;
82+
using MatDiffusionBaseTempl<T, false>::_var;
83+
using MatDiffusionBaseTempl<T, false>::_coupled_moose_vars;
84+
};

0 commit comments

Comments
 (0)