Skip to content

Commit

Permalink
add tag gradient idaholab#17586
Browse files Browse the repository at this point in the history
  • Loading branch information
YaqiWang committed Apr 12, 2021
1 parent 9d937e2 commit 1b99d11
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
20 changes: 20 additions & 0 deletions framework/include/interfaces/Coupleable.h
Expand Up @@ -246,6 +246,26 @@ class Coupleable
std::vector<const VariableValue *> coupledVectorTagValues(const std::string & var_name,
TagID tag) const;

/**
* Returns gradient of a coupled variable for a given tag
* @param var_name Name of coupled variable
* @param tag vector tag ID
* @param comp Component number for vector of coupled variables
* @return Reference to a VariableGradient containing the gradient of the coupled variable
* @see Kernel::gradient
*/
virtual const VariableGradient &
coupledVectorTagGradient(const std::string & var_name, TagID tag, unsigned int comp = 0) const;

/**
* Returns gradients for all of a coupled variable's components for a given tag
* @param var_name Name of coupled variable
* @param tag vector tag ID
* @return Vector of VariableGradient pointers for each component of \p var_name
*/
std::vector<const VariableGradient *> coupledVectorTagGradients(const std::string & var_name,
TagID tag) const;

/**
* Returns dof value of a coupled variable for a given tag
* @param var_name Name of coupled variable
Expand Down
7 changes: 7 additions & 0 deletions framework/include/variables/MooseVariableData.h
Expand Up @@ -472,6 +472,11 @@ class MooseVariableData
_need_vector_tag_u[tag] = true;
return _vector_tag_u[tag];
}
const FieldVariableGradient & vectorTagGradient(TagID tag) const
{
_need_vector_tag_grad[tag] = true;
return _vector_tag_grad[tag];
}
const FieldVariableValue & matrixTagValue(TagID tag) const
{
_need_matrix_tag_u[tag] = true;
Expand Down Expand Up @@ -541,6 +546,8 @@ class MooseVariableData

std::vector<FieldVariableValue> _vector_tag_u;
mutable std::vector<bool> _need_vector_tag_u;
std::vector<FieldVariableGradient> _vector_tag_grad;
mutable std::vector<bool> _need_vector_tag_grad;
std::vector<FieldVariableValue> _matrix_tag_u;
mutable std::vector<bool> _need_matrix_tag_u;

Expand Down
4 changes: 4 additions & 0 deletions framework/include/variables/MooseVariableFE.h
Expand Up @@ -266,6 +266,10 @@ class MooseVariableFE : public MooseVariableField<OutputType>
{
return _element_data->vectorTagValue(tag);
}
const FieldVariableGradient & vectorTagGradient(TagID tag) const
{
return _element_data->vectorTagGradient(tag);
}
const DoFValue & vectorTagDofValue(TagID tag) const
{
return _element_data->vectorTagDofValue(tag);
Expand Down
31 changes: 31 additions & 0 deletions framework/src/interfaces/Coupleable.C
Expand Up @@ -492,6 +492,28 @@ Coupleable::coupledVectorTagValue(const std::string & var_name, TagID tag, unsig
return var->vectorTagValue(tag);
}

const VariableGradient &
Coupleable::coupledVectorTagGradient(const std::string & var_name,
TagID tag,
unsigned int comp) const
{
const auto * var = getVar(var_name, comp);
if (!var)
mooseError(var_name, ": invalid variable name for coupledVectorTagGradient");
checkFuncType(var_name, VarType::Ignore, FuncAge::Curr);

if (!_c_fe_problem.vectorTagExists(tag))
mooseError("Attempting to couple to vector tag with ID ",
tag,
"in ",
_c_name,
", but a vector tag with that ID does not exist");

const_cast<Coupleable *>(this)->addFEVariableCoupleableVectorTag(tag);

return var->vectorTagGradient(tag);
}

const VariableValue &
Coupleable::coupledVectorTagDofValue(const std::string & var_name,
TagID tag,
Expand Down Expand Up @@ -1929,6 +1951,15 @@ Coupleable::coupledVectorTagValues(const std::string & var_name, TagID tag) cons
return coupledVectorHelper<const VariableValue *>(var_name, func);
}

std::vector<const VariableGradient *>
Coupleable::coupledVectorTagGradients(const std::string & var_name, TagID tag) const
{
auto func = [this, &var_name, &tag](unsigned int comp) {
return &coupledVectorTagGradient(var_name, tag, comp);
};
return coupledVectorHelper<const VariableGradient *>(var_name, func);
}

std::vector<const VariableValue *>
Coupleable::coupledVectorTagDofValues(const std::string & var_name, TagID tag) const
{
Expand Down
30 changes: 29 additions & 1 deletion framework/src/variables/MooseVariableData.C
Expand Up @@ -114,6 +114,9 @@ MooseVariableData<OutputType>::MooseVariableData(const MooseVariableFE<OutputTyp
_need_vector_tag_u.resize(num_vector_tags);
_vector_tag_u.resize(num_vector_tags);

_need_vector_tag_grad.resize(num_vector_tags);
_vector_tag_grad.resize(num_vector_tags);

auto num_matrix_tags = _sys.subproblem().numMatrixTags();

_matrix_tags_dof_u.resize(num_matrix_tags);
Expand Down Expand Up @@ -510,8 +513,12 @@ MooseVariableData<OutputType>::computeValues()
_grad_u.resize(nqp);

for (auto tag : active_coupleable_vector_tags)
{
if (_need_vector_tag_u[tag])
_vector_tag_u[tag].resize(nqp);
if (_need_vector_tag_grad[tag])
_vector_tag_grad[tag].resize(nqp);
}

for (auto tag : active_coupleable_matrix_tags)
if (_need_matrix_tag_u[tag])
Expand Down Expand Up @@ -586,8 +593,12 @@ MooseVariableData<OutputType>::computeValues()
_grad_u[i] = 0;

for (auto tag : active_coupleable_vector_tags)
{
if (_need_vector_tag_u[tag])
_vector_tag_u[tag][i] = 0;
if (_need_vector_tag_grad[tag])
_vector_tag_grad[tag][i] = 0;
}

for (auto tag : active_coupleable_matrix_tags)
if (_need_matrix_tag_u[tag])
Expand Down Expand Up @@ -749,8 +760,12 @@ MooseVariableData<OutputType>::computeValues()
}

for (auto tag : active_coupleable_vector_tags)
{
if (_need_vector_tag_u[tag])
_vector_tag_u[tag][qp] += phi_local * _vector_tags_dof_u[tag][i];
if (_need_vector_tag_grad[tag])
_vector_tag_grad[tag][qp].add_scaled(dphi_qp, _vector_tags_dof_u[tag][i]);
}

for (auto tag : active_coupleable_matrix_tags)
if (_need_matrix_tag_u[tag])
Expand Down Expand Up @@ -815,8 +830,12 @@ MooseVariableData<RealEigenVector>::computeValues()
_grad_u.resize(nqp);

for (auto tag : active_coupleable_vector_tags)
{
if (_need_vector_tag_u[tag])
_vector_tag_u[tag].resize(nqp);
if (_need_vector_tag_grad[tag])
_vector_tag_grad[tag].resize(nqp);
}

for (auto tag : active_coupleable_matrix_tags)
if (_need_matrix_tag_u[tag])
Expand Down Expand Up @@ -891,8 +910,12 @@ MooseVariableData<RealEigenVector>::computeValues()
_grad_u[i].setZero(_count, LIBMESH_DIM);

for (auto tag : active_coupleable_vector_tags)
{
if (_need_vector_tag_u[tag])
_vector_tag_u[tag][i].setZero(_count);
if (_need_vector_tag_grad[tag])
_vector_tag_grad[tag][i].setZero(_count, LIBMESH_DIM);
}

for (auto tag : active_coupleable_matrix_tags)
if (_need_matrix_tag_u[tag])
Expand Down Expand Up @@ -1067,8 +1090,13 @@ MooseVariableData<RealEigenVector>::computeValues()
}

for (auto tag : active_coupleable_vector_tags)
{
if (_need_vector_tag_u[tag])
_vector_tag_u[tag][qp] += phi_local * _vector_tags_dof_u[tag][i];
if (_need_vector_tag_grad[tag])
for (unsigned int d = 0; d < LIBMESH_DIM; ++d)
_vector_tag_grad[tag][qp].col(d) += dphi_qp(d) * _vector_tags_dof_u[tag][i];
}

for (auto tag : active_coupleable_matrix_tags)
if (_need_matrix_tag_u[tag])
Expand Down Expand Up @@ -2212,7 +2240,7 @@ MooseVariableData<OutputType>::fetchDoFValues()
auto & active_coupleable_vector_tags =
_sys.subproblem().getActiveFEVariableCoupleableVectorTags(_tid);
for (auto tag : active_coupleable_vector_tags)
if (_need_vector_tag_u[tag] || _need_vector_tag_dof_u[tag])
if (_need_vector_tag_u[tag] || _need_vector_tag_grad[tag] || _need_vector_tag_dof_u[tag])
if ((_sys.subproblem().vectorTagType(tag) == Moose::VECTOR_TAG_RESIDUAL &&
_sys.subproblem().safeAccessTaggedVectors()) ||
_sys.subproblem().vectorTagType(tag) == Moose::VECTOR_TAG_SOLUTION)
Expand Down

0 comments on commit 1b99d11

Please sign in to comment.