Permalink
Browse files

Merge pull request #12842 from aeslaughter/ad-coupled-vector-value

Adds adCoupledVectorValue and adCoupledVectorGradient
  • Loading branch information...
lindsayad committed Feb 7, 2019
2 parents 3b999a1 + 436b894 commit a6b86606d719a90a9bb31b6fc4bc52f7e3f48290
@@ -45,6 +45,9 @@ class DenseVector;
#define adCoupledGradient this->template adCoupledGradientTemplate<compute_stage>
#define adCoupledSecond this->template adCoupledSecondTemplate<compute_stage>
#define adCoupledDot this->template adCoupledDotTemplate<compute_stage>
#define adCoupledVectorValue this->template adCoupledVectorValueTemplate<compute_stage>
#define adCoupledVectorGradient this->template adCoupledVectorGradientTemplate<compute_stage>
#define adCoupledVectorSecond this->template adCoupledVectorSecondTemplate<compute_stage>
#define adZeroValue this->template adZeroValueTemplate<compute_stage>
#define adZeroGradient this->template adZeroGradientTemplate<compute_stage>
#define adZeroSecond this->template adZeroSecondTemplate<compute_stage>
@@ -162,6 +165,17 @@ class Coupleable
const ADVariableValue & adCoupledValueTemplate(const std::string & var_name,
unsigned int comp = 0);

/**
* Returns value of a coupled vector variable for use in Automatic Differentiation
* @param var_name Name of coupled vector variable
* @param comp Component number for vector of coupled variables
* @return Reference to a VariableValue for the coupled variable
* @see Kernel::value
*/
template <ComputeStage compute_stage>
const ADVectorVariableValue & adCoupledVectorValueTemplate(const std::string & var_name,
unsigned int comp = 0);

/**
* Returns value of a coupled variable for a given tag
* @param var_name Name of coupled variable
@@ -276,6 +290,17 @@ class Coupleable
const ADVariableGradient & adCoupledGradientTemplate(const std::string & var_name,
unsigned int comp = 0);

/**
* Returns gradient of a coupled vector variable for use in Automatic Differentation
* @param var_name Name of coupled vector variable
* @param comp Component number for vector of coupled vector variables
* @return Reference to a VectorVariableGradient containing the gradient of the coupled variable
* @see Kernel::gradient
*/
template <ComputeStage compute_stage>
const ADVectorVariableGradient & adCoupledVectorGradientTemplate(const std::string & var_name,
unsigned int comp = 0);

/**
* Returns second derivatives of a coupled variable for use in Automatic Differentation
* @param var_name Name of coupled variable
@@ -286,6 +311,17 @@ class Coupleable
const ADVariableSecond & adCoupledSecondTemplate(const std::string & var_name,
unsigned int comp = 0);

/**
* Returns second derivatives of a coupled vector variable for use in Automatic Differentation
* @param var_name Name of coupled vector variable
* @param comp Component number for vector of coupled vector variables
* @return Reference to a VectorVariableSecond containing the second derivatives of the coupled
* variable
*/
template <ComputeStage compute_stage>
const ADVectorVariableSecond & adCoupledVectorSecondTemplate(const std::string & var_name,
unsigned int comp = 0);

/**
* Returns an old gradient from previous time step of a coupled variable
* @param var_name Name of coupled variable
@@ -715,6 +751,9 @@ class Coupleable
/// Will hold the default value for optional vector coupled variables.
std::map<std::string, VectorVariableValue *> _default_vector_value;

/// Will hold the default value for optional vector coupled variables for automatic differentiation.
std::map<std::string, MooseArray<DualRealVectorValue> *> _ad_default_vector_value;

/**
* This will always be zero because the default values for optionally coupled variables is always
* constant and this is used for time derivative info
@@ -727,6 +766,9 @@ class Coupleable
/// This will always be zero because the default values for optionally coupled variables is always constant
MooseArray<DualRealVectorValue> _ad_default_gradient;

/// This will always be zero because the default values for optionally coupled vector variables is always constant
MooseArray<DualRealTensorValue> _ad_default_vector_gradient;

/// This will always be zero because the default values for optionally coupled variables is always constant
VariableSecond _default_second;

@@ -830,6 +872,15 @@ class Coupleable
template <ComputeStage compute_stage>
ADVariableValue * getADDefaultValue(const std::string & var_name);

/**
* Helper method to return (and insert if necessary) the default vector value for Automatic
* Differentiation for an uncoupled variable.
* @param var_name the name of the vector variable for which to retrieve a default value
* @return VariableVectorValue * a pointer to the associated VarirableVectorValue.
*/
template <ComputeStage compute_stage>
ADVectorVariableValue * getADDefaultVectorValue(const std::string & var_name);

/**
* Helper method to return (and insert if necessary) the default gradient for Automatic
* Differentiation for an uncoupled variable.
@@ -839,6 +890,15 @@ class Coupleable
template <ComputeStage compute_stage>
ADVariableGradient & getADDefaultGradient();

/**
* Helper method to return (and insert if necessary) the default gradient for Automatic
* Differentiation for an uncoupled vector variable.
* @param var_name the name of the vector variable for which to retrieve a default gradient
* @return VariableGradient * a pointer to the associated VectorVariableGradient.
*/
template <ComputeStage compute_stage>
ADVectorVariableGradient & getADDefaultVectorGradient();

/**
* Helper method to return (and insert if necessary) the default second derivatives for Automatic
* Differentiation for an uncoupled variable.
@@ -855,7 +915,7 @@ class Coupleable
* @param var_name the name of the vector variable for which to retrieve a default value
* @return a pointer to the associated VectorVariableValue.
*/
VectorVariableValue * getVectorDefaultValue(const std::string & var_name);
VectorVariableValue * getDefaultVectorValue(const std::string & var_name);

/// Maximum qps for any element in this system
unsigned int _coupleable_max_qps;
@@ -965,6 +1025,14 @@ Coupleable::adCoupledSecondTemplate(const std::string & var_name, unsigned int c
}
}

template <ComputeStage compute_stage>
const ADVectorVariableSecond &
adCoupledVectorSecondTemplate(const std::string & /*var_name*/, unsigned int /*comp = 0*/)
{
mooseError(
"Automatic differentiation using second derivatives of vector variables is not implemented.");
}

template <ComputeStage compute_stage>
const ADVariableValue &
Coupleable::adCoupledDotTemplate(const std::string & var_name, unsigned int comp)
@@ -993,6 +1061,72 @@ Coupleable::adCoupledDotTemplate(const std::string & var_name, unsigned int comp
}
}

template <ComputeStage compute_stage>
const ADVectorVariableValue &
Coupleable::adCoupledVectorValueTemplate(const std::string & var_name, unsigned int comp)
{
if (!isCoupled(var_name))
return *getADDefaultVectorValue<compute_stage>(var_name);

coupledCallback(var_name, false);
VectorMooseVariable * var = getVectorVar(var_name, comp);

if (!_coupleable_neighbor)
{
if (_c_nodal)
mooseError("Not implemented");
else
{
if (_c_is_implicit)
return var->adSln<compute_stage>();
else
mooseError("Not implemented");
}
}
else
{
if (_c_nodal)
mooseError("Not implemented");
else
{
if (_c_is_implicit)
return var->adSlnNeighbor<compute_stage>();
else
mooseError("Not implemented");
}
}
}

template <ComputeStage compute_stage>
const ADVectorVariableGradient &
Coupleable::adCoupledVectorGradientTemplate(const std::string & var_name, unsigned int comp)
{

if (!isCoupled(var_name)) // Return default 0
return getADDefaultVectorGradient<compute_stage>();

coupledCallback(var_name, false);
if (_c_nodal)
mooseError("Nodal variables do not have gradients");

VectorMooseVariable * var = getVectorVar(var_name, comp);

if (!_coupleable_neighbor)
{
if (_c_is_implicit)
return var->adGradSln<compute_stage>();
else
mooseError("Not implemented");
}
else
{
if (_c_is_implicit)
return var->adGradSlnNeighbor<compute_stage>();
else
mooseError("Not implemented");
}
}

template <ComputeStage compute_stage>
ADVariableValue *
Coupleable::getADDefaultValue(const std::string & var_name)
@@ -1012,6 +1146,27 @@ Coupleable::getADDefaultValue(const std::string & var_name)
template <>
VariableValue * Coupleable::getADDefaultValue<RESIDUAL>(const std::string & var_name);

template <ComputeStage compute_stage>
ADVectorVariableValue *
Coupleable::getADDefaultVectorValue(const std::string & var_name)
{
std::map<std::string, MooseArray<DualRealVectorValue> *>::iterator default_value_it =
_ad_default_vector_value.find(var_name);
if (default_value_it == _ad_default_vector_value.end())
{
RealVectorValue default_vec;
for (unsigned int i = 0; i < _c_parameters.numberDefaultCoupledValues(var_name); ++i)
default_vec(i) = _c_parameters.defaultCoupledValue(var_name, i);
ADVectorVariableValue * value = new ADVectorVariableValue(_coupleable_max_qps, default_vec);
default_value_it = _ad_default_vector_value.insert(std::make_pair(var_name, value)).first;
}

return default_value_it->second;
}

template <>
VectorVariableValue * Coupleable::getADDefaultVectorValue<RESIDUAL>(const std::string & var_name);

template <ComputeStage compute_stage>
ADVariableGradient &
Coupleable::getADDefaultGradient()
@@ -1022,6 +1177,16 @@ Coupleable::getADDefaultGradient()
template <>
VariableGradient & Coupleable::getADDefaultGradient<RESIDUAL>();

template <ComputeStage compute_stage>
ADVectorVariableGradient &
Coupleable::getADDefaultVectorGradient()
{
return _ad_default_vector_gradient;
}

template <>
VectorVariableGradient & Coupleable::getADDefaultVectorGradient<RESIDUAL>();

template <ComputeStage compute_stage>
ADVariableSecond &
Coupleable::getADDefaultSecond()
@@ -112,6 +112,11 @@ Coupleable::~Coupleable()
it.second->release();
delete it.second;
}
for (auto & it : _ad_default_vector_value)
{
it.second->release();
delete it.second;
}

_default_value_zero.release();
_default_gradient.release();
@@ -121,6 +126,7 @@ Coupleable::~Coupleable()
_default_vector_curl.release();
_ad_default_gradient.release();
_ad_default_second.release();
_ad_default_vector_gradient.release();
}

void
@@ -303,7 +309,7 @@ Coupleable::getDefaultValue(const std::string & var_name, unsigned int comp)
}

VectorVariableValue *
Coupleable::getVectorDefaultValue(const std::string & var_name)
Coupleable::getDefaultVectorValue(const std::string & var_name)
{
std::map<std::string, VectorVariableValue *>::iterator default_value_it =
_default_vector_value.find(var_name);
@@ -328,7 +334,7 @@ template <>
const RealVectorValue &
Coupleable::getNodalDefaultValue<RealVectorValue>(const std::string & var_name, unsigned int)
{
auto && default_variable_value = getVectorDefaultValue(var_name);
auto && default_variable_value = getDefaultVectorValue(var_name);
return *default_variable_value->data();
}

@@ -404,7 +410,7 @@ const VectorVariableValue &
Coupleable::coupledVectorValue(const std::string & var_name, unsigned int comp)
{
if (!isCoupled(var_name))
return *getVectorDefaultValue(var_name);
return *getDefaultVectorValue(var_name);

coupledCallback(var_name, false);
VectorMooseVariable * var = getVectorVar(var_name, comp);
@@ -546,7 +552,7 @@ const VectorVariableValue &
Coupleable::coupledVectorValueOld(const std::string & var_name, unsigned int comp)
{
if (!isCoupled(var_name))
return *getVectorDefaultValue(var_name);
return *getDefaultVectorValue(var_name);

validateExecutionerType(var_name, "coupledVectorValueOld");
coupledCallback(var_name, true);
@@ -576,7 +582,7 @@ const VectorVariableValue &
Coupleable::coupledVectorValueOlder(const std::string & var_name, unsigned int comp)
{
if (!isCoupled(var_name))
return *getVectorDefaultValue(var_name);
return *getDefaultVectorValue(var_name);

validateExecutionerType(var_name, "coupledVectorValueOlder");
coupledCallback(var_name, true);
@@ -1513,13 +1519,27 @@ Coupleable::getADDefaultValue<RESIDUAL>(const std::string & var_name)
return getDefaultValue(var_name, 0);
}

template <>
VectorVariableValue *
Coupleable::getADDefaultVectorValue<RESIDUAL>(const std::string & var_name)
{
return getDefaultVectorValue(var_name);
}

template <>
VariableGradient &
Coupleable::getADDefaultGradient<RESIDUAL>()
{
return _default_gradient;
}

template <>
VectorVariableGradient &
Coupleable::getADDefaultVectorGradient<RESIDUAL>()
{
return _default_vector_gradient;
}

template <>
VariableSecond &
Coupleable::getADDefaultSecond<RESIDUAL>()
@@ -0,0 +1,37 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#ifndef ADCOUPLEDVECTORCONVECTION_H
#define ADCOUPLEDVECTORCONVECTION_H

#include "ADKernel.h"

template <ComputeStage compute_stage>
class ADCoupledVectorConvection;

declareADValidParams(ADCoupledVectorConvection);

template <ComputeStage compute_stage>
class ADCoupledVectorConvection : public ADKernel<compute_stage>
{
public:
ADCoupledVectorConvection(const InputParameters & parameters);

protected:
virtual ADResidual computeQpResidual() override;

usingKernelMembers;

private:
const bool & _use_grad;
const ADVectorVariableValue & _velocity_vector;
const ADVectorVariableGradient & _grad_velocity_vector;
};

#endif
Oops, something went wrong.

0 comments on commit a6b8660

Please sign in to comment.