Skip to content

Commit

Permalink
add tests for tagged coupling idaholab#17586
Browse files Browse the repository at this point in the history
  • Loading branch information
YaqiWang committed Apr 17, 2021
1 parent 5f517f3 commit b4cb9e2
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 24 deletions.
4 changes: 3 additions & 1 deletion test/include/kernels/CoupledForceLagged.h
Expand Up @@ -28,6 +28,8 @@ class CoupledForceLagged : public Kernel

virtual Real computeQpOffDiagJacobian(unsigned int jvar) override;

unsigned int _v_var;
const unsigned int _v_var;
const bool _tagged;
const VariableValue & _v;
const Real _coef;
};
25 changes: 25 additions & 0 deletions test/include/kernels/MatKernel.h
@@ -0,0 +1,25 @@
//* 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

#pragma once

#include "Kernel.h"

class MatKernel : public Kernel
{
public:
static InputParameters validParams();

MatKernel(const InputParameters & parameters);

protected:
virtual Real computeQpResidual();
const MaterialProperty<Real> & _mat_prop;
const Real _coef;
};
11 changes: 8 additions & 3 deletions test/include/problems/FixedPointProblem.h
Expand Up @@ -26,8 +26,13 @@ class FixedPointProblem : public FEProblem
virtual void computeFullResidual(const NumericVector<Number> & soln,
NumericVector<Number> & residual);

bool taggedVectorForResidual() const { return _tagged_vector_for_partial_residual; }

void copySolution();

protected:
TagName _tag_previous;
TagID _tag_id;
NumericVector<Number> & _residual_previous;
const bool _tagged_vector_for_partial_residual;
const TagName _tag_previous;
const TagID _tag_id;
NumericVector<Number> & _tagged_vector;
};
2 changes: 2 additions & 0 deletions test/src/executioners/FixedPoint.C
Expand Up @@ -52,6 +52,7 @@ FixedPoint::solve()
_console << "Fixed point iteration " << it << std::endl;

Real residual_norm_previous = residual_norm;

if (!_inner_solve->solve())
{
_console << COLOR_RED << " Fixed point iteration did NOT converge!" << COLOR_DEFAULT
Expand All @@ -61,6 +62,7 @@ FixedPoint::solve()
return false;
}

_fp_problem.copySolution();
_fp_problem.computeFullResidual(*_nl.currentSolution(), _nl.RHS());
residual_norm = _nl.RHS().l2_norm();
_console << "Fixed point residual norm " << residual_norm << std::endl;
Expand Down
10 changes: 8 additions & 2 deletions test/src/kernels/CoupledForceLagged.C
Expand Up @@ -16,18 +16,24 @@ CoupledForceLagged::validParams()
{
InputParameters params = Kernel::validParams();
params.addRequiredCoupledVar("v", "The coupled variable which provides the force");
params.addParam<Real>("coefficient", 1.0, "Coefficient of the term");
params.addParam<TagName>("tag", "The solution vector to be coupled in");
return params;
}

CoupledForceLagged::CoupledForceLagged(const InputParameters & parameters)
: Kernel(parameters), _v_var(coupled("v")), _v(coupledValuePreviousNL("v"))
: Kernel(parameters),
_v_var(coupled("v")),
_tagged(isTagNameValid("tag")),
_v(_tagged ? coupledVectorTagValue("v", "tag") : coupledValuePreviousNL("v")),
_coef(getParam<Real>("coefficient"))
{
}

Real
CoupledForceLagged::computeQpResidual()
{
return -_v[_qp] * _test[_i][_qp];
return -_coef * _v[_qp] * _test[_i][_qp];
}

Real
Expand Down
37 changes: 37 additions & 0 deletions test/src/kernels/MatKernel.C
@@ -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

#include "MatKernel.h"

registerMooseObject("MooseTestApp", MatKernel);

InputParameters
MatKernel::validParams()
{
InputParameters params = Kernel::validParams();
params.addParam<Real>("coefficient", 1.0, "Coefficient of the term");
params.addParam<MaterialPropertyName>("mat_prop",
"the name of the material property for coupling");
return params;
}

MatKernel::MatKernel(const InputParameters & parameters)
: Kernel(parameters),
_mat_prop(getMaterialProperty<Real>("mat_prop")),
_coef(getParam<Real>("coefficient"))
{
}

Real
MatKernel::computeQpResidual()
{
return _coef * _test[_i][_qp] * _mat_prop[_qp];
}

// we currently have no way to assemble Jacobian properly for a general material property
3 changes: 2 additions & 1 deletion test/src/materials/VarCouplingMaterial.C
Expand Up @@ -20,12 +20,13 @@ VarCouplingMaterial::validParams()
params.addParam<Real>("coef", 1.0, "The linear coefficient of the coupled var");
params.addParam<bool>(
"declare_old", false, "When True the old value for the material property is declared.");
params.addParam<TagName>("tag", "The solution vector to be coupled in");
return params;
}

VarCouplingMaterial::VarCouplingMaterial(const InputParameters & parameters)
: Material(parameters),
_var(coupledValue("var")),
_var(coupledVectorTagValue("var", "tag")),
_base(getParam<Real>("base")),
_coef(getParam<Real>("coef")),
_diffusion(declareProperty<Real>("diffusion")),
Expand Down
5 changes: 4 additions & 1 deletion test/src/postprocessors/ElementL2Diff.C
Expand Up @@ -15,11 +15,14 @@ InputParameters
ElementL2Diff::validParams()
{
InputParameters params = ElementIntegralVariablePostprocessor::validParams();
params.addParam<TagName>("tag", "Tag name of the solution vector");
return params;
}

ElementL2Diff::ElementL2Diff(const InputParameters & parameters)
: ElementIntegralVariablePostprocessor(parameters), _u_old(coupledValueOld("variable"))
: ElementIntegralVariablePostprocessor(parameters),
_u_old(isParamValid("tag") ? coupledVectorTagValue("variable", "tag")
: (_fe_problem.isTransient() ? valueOld() : _u))
{
}

Expand Down
48 changes: 35 additions & 13 deletions test/src/problems/FixedPointProblem.C
Expand Up @@ -18,43 +18,65 @@ FixedPointProblem::validParams()
{
InputParameters params = FEProblem::validParams();
params.addParam<TagName>("fp_tag_name", "fp_previous", "Tag name for the fixed point iteration");
params.addParam<bool>(
"tagged_vector_for_partial_residual",
true,
"Toggle between parital residual and previous solution for the tagged vector");
return params;
}

FixedPointProblem::FixedPointProblem(const InputParameters & params)
: FEProblem(params),
_tagged_vector_for_partial_residual(getParam<bool>("tagged_vector_for_partial_residual")),
_tag_previous(getParam<TagName>("fp_tag_name")),
_tag_id(addVectorTag(_tag_previous)),
_residual_previous(_nl->addVector(_tag_id, false, GHOSTED))
_tag_id(addVectorTag(_tag_previous,
_tagged_vector_for_partial_residual ? Moose::VECTOR_TAG_RESIDUAL
: Moose::VECTOR_TAG_SOLUTION)),
_tagged_vector(_nl->addVector(_tag_id, false, GHOSTED))
{
}

void
FixedPointProblem::computeResidual(const NumericVector<Number> & soln,
NumericVector<Number> & residual)
{
// excluding the previous tag evaluation
_nl->disassociateVectorFromTag(_residual_previous, _tag_id);
if (_tagged_vector_for_partial_residual)
{
// excluding the previous tag evaluation
_nl->disassociateVectorFromTag(_tagged_vector, _tag_id);

const auto & residual_vector_tags = getVectorTags(Moose::VECTOR_TAG_RESIDUAL);
const auto & residual_vector_tags = getVectorTags(Moose::VECTOR_TAG_RESIDUAL);

_fe_vector_tags.clear();
_fe_vector_tags.clear();

for (const auto & residual_vector_tag : residual_vector_tags)
if (residual_vector_tag._id != _tag_id)
_fe_vector_tags.insert(residual_vector_tag._id);
for (const auto & residual_vector_tag : residual_vector_tags)
if (residual_vector_tag._id != _tag_id)
_fe_vector_tags.insert(residual_vector_tag._id);

computeResidualInternal(soln, residual, _fe_vector_tags);
computeResidualInternal(soln, residual, _fe_vector_tags);

residual += _residual_previous;
residual += _tagged_vector;

_nl->associateVectorToTag(_residual_previous, _tag_id);
_nl->associateVectorToTag(_tagged_vector, _tag_id);
}
else
FEProblem::computeResidual(soln, residual);
}

void
FixedPointProblem::computeFullResidual(const NumericVector<Number> & soln,
NumericVector<Number> & residual)
{
FEProblem::computeResidual(soln, residual);
residual += _residual_previous;
if (_tagged_vector_for_partial_residual)
residual += _tagged_vector;
}

void
FixedPointProblem::copySolution()
{
// copy current solution to the tagged vector only when the tagged vector is for
// storing the solution of previous fixed point iteration
if (!_tagged_vector_for_partial_residual)
_tagged_vector = _nl->solution();
}
@@ -0,0 +1,79 @@
[Mesh]
type = GeneratedMesh
dim = 2
nx = 4
ny = 4
xmin = -1
xmax = 1
ymin = -1
ymax = 1
[]

[Variables]
[u]
order = FIRST
family = LAGRANGE
[]
[]

[Kernels]
[diff]
type = Diffusion
variable = u
[]
[pseudo_time]
type = CoupledForceLagged
variable = u
coefficient = 0.1
v = u
tag = 'previous'
[]
[pseudo_time_compensation]
type = CoefReaction
variable = u
coefficient = 0.1
[]
[]

[BCs]
[left]
type = VacuumBC
variable = u
boundary = left
[]

[right]
type = NeumannBC
variable = u
boundary = right
value = 1
[]
[]

[Postprocessors]
[unorm]
type = ElementL2Norm
variable = u
[]
[udiff]
type = ElementL2Diff
variable = u
tag = 'previous'
[]
[]

[Problem]
type = FixedPointProblem
fp_tag_name = 'previous'
tagged_vector_for_partial_residual = false
[]

[Executioner]
type = FixedPointSteady
nl_rel_tol = 1e-2
nl_abs_tol = 1e-12
[]

[Outputs]
exodus = true
[]

0 comments on commit b4cb9e2

Please sign in to comment.