Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish support for AD time derivatives of dofs #28857

Open
wants to merge 7 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions framework/include/interfaces/Coupleable.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,28 @@ class Coupleable
const GenericVariableValue<is_ad> & coupledGenericDofValue(const std::string & var_name,
unsigned int comp = 0) const;

/**
* Returns time derivative of a coupled variable for use in templated automatic differentiation
* classes
* @param var_name Name of coupled variable
* @param comp Component number for vector of coupled variables
* @return Reference to a GenericVariableValue for the coupled variable time derivative
*/
template <bool is_ad>
const GenericVariableValue<is_ad> & coupledGenericDot(const std::string & var_name,
unsigned int comp = 0) const;

/**
* Returns the second time derivative of a coupled variable for use in templated automatic
* differentiation classes
* @param var_name Name of coupled variable
* @param comp Component number for vector of coupled variables
* @return Reference to a GenericVariableValue for the coupled variable second time derivative
*/
template <bool is_ad>
const GenericVariableValue<is_ad> & coupledGenericDotDot(const std::string & var_name,
unsigned int comp = 0) const;

/**
* Returns value of a coupled lower-dimensional variable
* @param var_name Name of coupled variable
Expand Down
1 change: 1 addition & 0 deletions framework/include/variables/MooseLinearVariableFV.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ class MooseLinearVariableFV : public MooseVariableField<OutputType>

[[noreturn]] virtual const MooseArray<ADReal> & adDofValues() const override;
[[noreturn]] virtual const MooseArray<ADReal> & adDofValuesNeighbor() const override;
[[noreturn]] virtual const MooseArray<ADReal> & adDofValuesDot() const override;
[[noreturn]] virtual const dof_id_type & nodalDofIndex() const override final;
[[noreturn]] virtual const dof_id_type & nodalDofIndexNeighbor() const override final;

Expand Down
16 changes: 16 additions & 0 deletions framework/include/variables/MooseVariableData.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ class MooseVariableData : public MooseVariableDataBase<OutputType>
*/
const MooseArray<ADReal> & adDofValues() const;

/**
* Return the AD time derivative values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Return the AD time derivative values
* Return the AD time derivative values of degrees of freedom

*/
const MooseArray<ADReal> & adDofValuesDot() const;

/////////////////////////////// Increment stuff ///////////////////////////////////////

/**
Expand Down Expand Up @@ -726,6 +731,17 @@ MooseVariableData<OutputType>::adDofValues() const
return _ad_dof_values;
}

template <typename OutputType>
const MooseArray<ADReal> &
MooseVariableData<OutputType>::adDofValuesDot() const
{
_need_ad = _need_ad_u_dot = true;
lindsayad marked this conversation as resolved.
Show resolved Hide resolved
if (!_time_integrator)
// See explanation in adUDot() body
_need_u_dot = true;
return _ad_dofs_dot;
}

template <typename OutputType>
const typename Moose::ADType<OutputType>::type &
MooseVariableData<OutputType>::adNodalValue() const
Expand Down
13 changes: 13 additions & 0 deletions framework/include/variables/MooseVariableDataFV.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ class MooseVariableDataFV : public MooseVariableDataBase<OutputType>, public Mes
*/
const MooseArray<ADReal> & adDofValues() const;

/**
* Return the AD dof time derivatives
*/
const MooseArray<ADReal> & adDofValuesDot() const;

/////////////////////////////// Increment stuff ///////////////////////////////////////

/**
Expand Down Expand Up @@ -473,6 +478,14 @@ MooseVariableDataFV<OutputType>::adDofValues() const
return _ad_dof_values;
}

template <typename OutputType>
const MooseArray<ADReal> &
MooseVariableDataFV<OutputType>::adDofValuesDot() const
{
_need_ad = _need_ad_u_dot = true;
return _ad_dofs_dot;
}

template <typename OutputType>
inline bool
MooseVariableDataFV<OutputType>::safeToComputeADUDot() const
Expand Down
15 changes: 8 additions & 7 deletions framework/include/variables/MooseVariableFE.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,15 +594,9 @@ class MooseVariableFE : public MooseVariableField<OutputType>
const MooseArray<Number> & dofValuesDuDotDotDu() const override;
const MooseArray<Number> & dofValuesDuDotDotDuNeighbor() const override;

/**
* Return the AD dof values
*/
const MooseArray<ADReal> & adDofValues() const override;

/**
* Return the AD neignbor dof values
*/
const MooseArray<ADReal> & adDofValuesNeighbor() const override;
const MooseArray<ADReal> & adDofValuesDot() const override;

/**
* Compute and store incremental change in solution at QPs based on increment_vec
Expand Down Expand Up @@ -844,6 +838,13 @@ MooseVariableFE<OutputType>::adDofValuesNeighbor() const
return _neighbor_data->adDofValues();
}

template <typename OutputType>
inline const MooseArray<ADReal> &
MooseVariableFE<OutputType>::adDofValuesDot() const
{
return _element_data->adDofValuesDot();
}

template <typename OutputType>
inline const typename Moose::ADType<OutputType>::type &
MooseVariableFE<OutputType>::adNodalValue() const
Expand Down
11 changes: 8 additions & 3 deletions framework/include/variables/MooseVariableFV.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,9 @@ class MooseVariableFV : public MooseVariableField<OutputType>
const MooseArray<Number> & dofValuesDuDotDotDu() const override;
const MooseArray<Number> & dofValuesDuDotDotDuNeighbor() const override;

/// Returns the AD dof values.
const MooseArray<ADReal> & adDofValues() const override;

/// Returns the AD neighbor dof values
const MooseArray<ADReal> & adDofValuesNeighbor() const override;
const MooseArray<ADReal> & adDofValuesDot() const override;

/// Note: const monomial is always the case - higher order solns are
/// reconstructed - so this is simpler func than FE equivalent.
Expand Down Expand Up @@ -732,6 +730,13 @@ MooseVariableFV<OutputType>::adDofValuesNeighbor() const
return _neighbor_data->adDofValues();
}

template <typename OutputType>
inline const MooseArray<ADReal> &
MooseVariableFV<OutputType>::adDofValuesDot() const
{
return _element_data->adDofValuesDot();
}

template <typename OutputType>
typename MooseVariableFV<OutputType>::ValueType
MooseVariableFV<OutputType>::evaluate(const ElemArg & elem_arg, const StateArg & state) const
Expand Down
5 changes: 5 additions & 0 deletions framework/include/variables/MooseVariableField.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ class MooseVariableField : public MooseVariableFieldBase,
*/
virtual const MooseArray<ADReal> & adDofValuesNeighbor() const = 0;

/**
* Return the AD time derivatives at dofs
*/
virtual const MooseArray<ADReal> & adDofValuesDot() const = 0;

///@{
/**
* Methods for retrieving values of variables at the nodes in a MooseArray for AuxKernelBase
Expand Down
42 changes: 38 additions & 4 deletions framework/src/interfaces/Coupleable.C
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,20 @@ Coupleable::coupledDotDot(const std::string & var_name, unsigned int comp) const
}
}

template <>
const GenericVariableValue<false> &
Coupleable::coupledGenericDotDot<false>(const std::string & var_name, unsigned int comp) const
{
return coupledDotDot(var_name, comp);
}

template <>
const GenericVariableValue<true> &
Coupleable::coupledGenericDotDot<true>(const std::string & var_name, unsigned int comp) const
{
return adCoupledDotDot(var_name, comp);
}

const VariableValue &
Coupleable::coupledDotOld(const std::string & var_name, unsigned int comp) const
{
Expand Down Expand Up @@ -2207,12 +2221,18 @@ Coupleable::adCoupledDot(const std::string & var_name, unsigned int comp) const
return *getADDefaultValue(var_name);
checkFuncType(var_name, VarType::Dot, FuncAge::Curr);

if (_c_nodal)
mooseError("Not implemented");

if (!_coupleable_neighbor)
{
if (_c_nodal)
return var->adDofValuesDot();
return var->adUDot();
return var->adUDotNeighbor();
}
else
{
if (_c_nodal)
mooseError("AD neighbor nodal dof dot implemented");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mooseError("AD neighbor nodal dof dot implemented");
mooseError("AD neighbor nodal dof dot not implemented");

return var->adUDotNeighbor();
}
}

const ADVariableValue &
Expand Down Expand Up @@ -2687,6 +2707,20 @@ Coupleable::adCoupledDots(const std::string & var_name) const
return coupledVectorHelper<const ADVariableValue *>(var_name, func);
}

template <>
const GenericVariableValue<false> &
Coupleable::coupledGenericDot<false>(const std::string & var_name, unsigned int comp) const
{
return coupledDot(var_name, comp);
}

template <>
const GenericVariableValue<true> &
Coupleable::coupledGenericDot<true>(const std::string & var_name, unsigned int comp) const
{
return adCoupledDot(var_name, comp);
}

// Explicit instantiations

template const Real & Coupleable::getDefaultNodalValue<Real>(const std::string & var_name,
Expand Down
4 changes: 4 additions & 0 deletions framework/src/timeintegrators/CentralDifference.C
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ CentralDifference::computeADTimeDerivatives(ADReal & ad_u_dot,
const dof_id_type & dof,
ADReal & ad_u_dotdot) const
{
// Seeds ad_u_dotdot with _ad_dof_values and associated derivatives provided via ad_u_dot from
// MooseVariableData
ad_u_dotdot = ad_u_dot;

computeTimeDerivativeHelper(ad_u_dot, ad_u_dotdot, _solution_old(dof), _solution_older(dof));
}

Expand Down
7 changes: 7 additions & 0 deletions framework/src/variables/MooseLinearVariableFV.C
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,13 @@ MooseLinearVariableFV<OutputType>::adDofValuesNeighbor() const
adError();
}

template <typename OutputType>
const MooseArray<ADReal> &
MooseLinearVariableFV<OutputType>::adDofValuesDot() const
{
adError();
}

template <typename OutputType>
const dof_id_type &
MooseLinearVariableFV<OutputType>::nodalDofIndex() const
Expand Down
29 changes: 28 additions & 1 deletion framework/src/variables/MooseVariableData.C
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ template <>
void
MooseVariableData<RealEigenVector>::computeMonomialValues()
{
// Fixeme: will think of optimization later
// FIXME: will think of optimization later
computeValues();
}

Expand Down Expand Up @@ -1271,12 +1271,16 @@ MooseVariableData<OutputType>::computeAD(const unsigned int num_dofs, const unsi
}

if (_need_ad_u_dot && !_time_integrator)
{
for (MooseIndex(nqp) qp = 0; qp < nqp; ++qp)
{
_ad_u_dot[qp] = _u_dot[qp];
if (_need_ad_u_dotdot)
_ad_u_dotdot[qp] = _u_dotdot[qp];
}
for (unsigned int i = 0; i < num_dofs; i++)
_ad_dofs_dot[i] = _dof_values_dot[i];
}

if (_need_ad_grad_u_dot && !_time_integrator)
for (MooseIndex(nqp) qp = 0; qp < nqp; ++qp)
Expand Down Expand Up @@ -1785,6 +1789,11 @@ MooseVariableData<OutputType>::fetchADDoFValues()
libmesh_assert(n);
_ad_dof_values.resize(n);

if (_need_ad_u_dot)
_ad_dofs_dot.resize(n);
if (_need_ad_u_dotdot)
_ad_dofs_dotdot.resize(n);

const bool do_derivatives =
ADReal::do_derivatives && _sys.number() == _subproblem.currentNlSysNum();

Expand All @@ -1794,6 +1803,24 @@ MooseVariableData<OutputType>::fetchADDoFValues()
if (do_derivatives)
Moose::derivInsert(_ad_dof_values[i].derivatives(), _dof_indices[i], 1.);
assignADNodalValue(_ad_dof_values[i], i);

if (_need_ad_u_dot)
{
if (_time_integrator && _time_integrator->dt())
{
_ad_dofs_dot[i] = _ad_dof_values[i];
_time_integrator->computeADTimeDerivatives(_ad_dofs_dot[i],
_dof_indices[i],
_need_ad_u_dotdot ? _ad_dofs_dotdot[i]
: _ad_real_dummy);
}
// Executing something with a time derivative at initial should not put a NaN
else if (_time_integrator && !_time_integrator->dt())
_ad_dofs_dot[i] = 0;
else
mooseError("AD nodal time derivatives not implemented for variables without a time "
"integrator (auxiliary variables)");
}
Comment on lines +1807 to +1823
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

computeNodalValues is the only caller of fetchADDoFValues. I've always thought we should sync the names better. As it is, I think it's a bit confusing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I ll rename!

}
}

Expand Down
4 changes: 2 additions & 2 deletions framework/src/variables/MooseVariableDataFV.C
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ MooseVariableDataFV<OutputType>::MooseVariableDataFV(const MooseVariableFV<Outpu
_need_curl_old(false),
_need_curl_older(false),
// for FV variables, they use each other's ad_u values to compute ghost cell
// values - we don't have any way to propogate these inter-variable-data
// values - we don't have any way to propagate these inter-variable-data
// dependencies. So if something needs an ad_u value, that need isn't
// propogated through to both the element and the neighbor
// propagated through to both the element and the neighbor
// data structures. So instead just set _need_ad+_need_ad_u values to true always.
_need_ad(true),
_need_ad_u(true),
Expand Down
14 changes: 8 additions & 6 deletions test/include/auxkernels/CoupledAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@
/**
* Coupled auxiliary value
*/
class CoupledAux : public AuxKernel
template <bool is_ad>
class CoupledAuxTempl : public AuxKernel
{
public:
static InputParameters validParams();

CoupledAux(const InputParameters & parameters);

virtual ~CoupledAux() {}
CoupledAuxTempl(const InputParameters & parameters);

protected:
virtual Real computeValue();

Real _value; ///< The value being set for this kernel
MooseEnum _operator; ///< Operator being applied on this variable and coupled variable

int _coupled; ///< The number of the coupled variable
const VariableValue & _coupled_val; ///< Coupled variable
int _coupled; ///< The number of the coupled variable
const GenericVariableValue<is_ad> & _coupled_val; ///< Coupled variable
};

typedef CoupledAuxTempl<false> CoupledAux;
typedef CoupledAuxTempl<true> ADCoupledAux;
11 changes: 7 additions & 4 deletions test/include/auxkernels/DotCouplingAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
/**
* Couples in the time derivatives of a NL variable
*/
class DotCouplingAux : public AuxKernel
template <bool is_ad>
class DotCouplingAuxTempl : public AuxKernel
{
public:
static InputParameters validParams();

DotCouplingAux(const InputParameters & parameters);
virtual ~DotCouplingAux();
DotCouplingAuxTempl(const InputParameters & parameters);

protected:
virtual Real computeValue();

const VariableValue & _v_dot;
const GenericVariableValue<is_ad> & _v_dot;
};

typedef DotCouplingAuxTempl<false> DotCouplingAux;
typedef DotCouplingAuxTempl<true> ADDotCouplingAux;
Loading