Skip to content

Commit

Permalink
Improve coverage (idaholab#22563)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed Feb 28, 2023
1 parent 1826209 commit be90223
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 102 deletions.
30 changes: 10 additions & 20 deletions framework/src/interfaces/Coupleable.C
Original file line number Diff line number Diff line change
Expand Up @@ -842,33 +842,23 @@ Coupleable::writableVariable(const std::string & var_name, unsigned int comp)
const auto * nuo = dynamic_cast<const NodalUserObject *>(this);

if (!aux && !euo && !nuo)
mooseError("writableCoupledvalue() can only be called from AuxKernels, ElementUserObjects, or "
mooseError("writableVariable() can only be called from AuxKernels, ElementUserObjects, or "
"NodalUserObjects. '",
_obj->name(),
"' is neither of those.");

if (aux && aux->isNodal() != var->isNodal())
mooseError("The AuxKernel '",
if (aux && !aux->isNodal() && var->isNodal())
mooseError("The elemental AuxKernel '",
_obj->name(),
"' and the variable '",
"' cannot obtain a writable reference to the nodal variable '",
var->name(),
"' must both either be nodal or elemental.");
"'.");
if (euo && var->isNodal())
mooseError("The ElementUserObject '",
_obj->name(),
"' cannot obtain a writable reference to the nodal variable '",
var->name(),
"'.");
// if (nuo && !var->isNodal()) is handled by checkVar already

// check block restrictions for compatibility
const auto * br = dynamic_cast<const BlockRestrictable *>(this);
if (!var->hasBlocks(br->blockIDs()))
mooseError("The variable '",
var->name(),
"' must be defined on all blocks '",
_obj->name(),
"' is defined on");

// make sure only one object can access a variable
checkWritableVar(var);
Expand Down Expand Up @@ -897,16 +887,16 @@ Coupleable::writableCoupledValue(const std::string & var_name, unsigned int comp
const auto * aux = dynamic_cast<const AuxKernel *>(this);

if (!aux)
mooseError("writableCoupledvalue() can only be called from AuxKernels, but '",
mooseError("writableCoupledValue() can only be called from AuxKernels, but '",
_obj->name(),
"' is not an AuxKernel.");

if (aux->isNodal() != var->isNodal())
mooseError("The AuxKernel '",
if (!aux->isNodal() && var->isNodal())
mooseError("The elemental AuxKernel '",
_obj->name(),
"' and the variable '",
"' cannot obtain a writable reference to the nodal variable '",
var->name(),
"' must both either be nodal or elemental.");
"'.");

// make sure only one object can access a variable
checkWritableVar(var);
Expand Down
13 changes: 11 additions & 2 deletions test/include/auxkernels/MultipleUpdateAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ class MultipleUpdateAux : public AuxKernel
virtual Real computeValue();

const VariableValue & _nl_u;
MooseVariable & _var1;
MooseVariable & _var2;

/// use deprecated API
const bool _deprecated;

/// current API
MooseVariable * _var1;
MooseVariable * _var2;

/// deprectated API
VariableValue * _dvar1;
VariableValue * _dvar2;
};
31 changes: 0 additions & 31 deletions test/include/auxkernels/MultipleUpdateAuxDeprecated.h

This file was deleted.

39 changes: 39 additions & 0 deletions test/include/kernels/MultipleUpdateErrorKernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//* 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"

/**
* Kernel that tries to obtain a writable reference to a variable, but fails immediately because
* only AuxKernels, ElementUserObjects, and NodalUserObjects are permitted to get a writable
* reference.
*/
class MultipleUpdateErrorKernel : public Kernel
{
public:
static InputParameters validParams();

MultipleUpdateErrorKernel(const InputParameters & parameters);

protected:
Real computeQpResidual() override;

/// use deprecated API
const bool _deprecated;

/// current API
MooseVariable * _var1;
MooseVariable * _var2;

/// deprectated API
VariableValue * _dvar1;
VariableValue * _dvar2;
};
27 changes: 22 additions & 5 deletions test/src/auxkernels/MultipleUpdateAux.C
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,41 @@ MultipleUpdateAux::validParams()
params.addRequiredCoupledVar("u", "unknown (nl-variable)");
params.addRequiredCoupledVar("var1", "an aux variable to update");
params.addRequiredCoupledVar("var2", "another aux variable to update");

params.addParam<bool>("use_deprecated_api", false, "Test the deprecated API");
return params;
}

MultipleUpdateAux::MultipleUpdateAux(const InputParameters & parameters)
: AuxKernel(parameters),
_nl_u(coupledValue("u")),
_var1(writableVariable("var1")),
_var2(writableVariable("var2"))
_deprecated(getParam<bool>("use_deprecated_api"))
{
if (_deprecated)
{
_dvar1 = &writableCoupledValue("var1");
_dvar2 = &writableCoupledValue("var2");
}
else
{
_var1 = &writableVariable("var1");
_var2 = &writableVariable("var2");
}
}

MultipleUpdateAux::~MultipleUpdateAux() {}

Real
MultipleUpdateAux::computeValue()
{
_var1.setNodalValue(_nl_u[_qp] + 10.0, _qp);
_var2.setNodalValue(_nl_u[_qp] + 200.0, _qp);
if (_deprecated)
{
(*_dvar1)[_qp] = _nl_u[_qp] + 10.0;
(*_dvar2)[_qp] = _nl_u[_qp] + 200.0;
}
else
{
_var1->setNodalValue(_nl_u[_qp] + 10.0, _qp);
_var2->setNodalValue(_nl_u[_qp] + 200.0, _qp);
}
return -3.33;
}
42 changes: 0 additions & 42 deletions test/src/auxkernels/MultipleUpdateAuxDeprecated.C

This file was deleted.

43 changes: 43 additions & 0 deletions test/src/kernels/MultipleUpdateErrorKernel.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//* 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 "MultipleUpdateErrorKernel.h"

registerMooseObject("MooseTestApp", MultipleUpdateErrorKernel);

InputParameters
MultipleUpdateErrorKernel::validParams()
{
InputParameters params = Kernel::validParams();
params.addRequiredCoupledVar("var1", "an aux variable to update");
params.addRequiredCoupledVar("var2", "another aux variable to update");
params.addParam<bool>("use_deprecated_api", false, "Test the deprecated API");
return params;
}

MultipleUpdateErrorKernel::MultipleUpdateErrorKernel(const InputParameters & parameters)
: Kernel(parameters), _deprecated(getParam<bool>("use_deprecated_api"))
{
if (_deprecated)
{
_dvar1 = &writableCoupledValue("var1");
_dvar2 = &writableCoupledValue("var2");
}
else
{
_var1 = &writableVariable("var1");
_var2 = &writableVariable("var2");
}
}

Real
MultipleUpdateErrorKernel::computeQpResidual()
{
mooseError("This should never be reached");
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@

[./all]
variable = tt
type = MultipleUpdateAuxDeprecated
type = MultipleUpdateAux
use_deprecated_api = true
u = u
var1 = ten
var2 = 2k
Expand Down
58 changes: 58 additions & 0 deletions test/tests/auxkernels/nodal_aux_var/multi_update_var_error.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[Mesh]
type = GeneratedMesh
dim = 2
xmin = 0
xmax = 1
ymin = 0
ymax = 1
nx = 2
ny = 2
[]

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

[AuxVariables]
[ten]
order = FIRST
family = LAGRANGE
initial_condition = 1
[]
[2k]
order = FIRST
family = LAGRANGE
initial_condition = 2
[]
[]

[Kernels]
[all]
type = MultipleUpdateErrorKernel
variable = u
var1 = ten
var2 = 2k
[]
[]

[BCs]
[left]
type = DirichletBC
variable = u
boundary = 1
value = 0
[]
[right]
type = DirichletBC
variable = u
boundary = 3
value = 1
[]
[]

[Executioner]
type = Steady
[]

0 comments on commit be90223

Please sign in to comment.