Skip to content

Commit

Permalink
Allowing to define an array varible with component size one, refs ida…
Browse files Browse the repository at this point in the history
  • Loading branch information
yjung-anl committed Dec 7, 2021
1 parent 8b86c66 commit 7e46260
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 3 deletions.
9 changes: 9 additions & 0 deletions framework/include/actions/AddVariableAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ class AddVariableAction : public MooseObjectAction
static std::string
determineType(const FEType & fe_type, unsigned int components, bool is_fv = false);

/**
* Determines a variable type
* @param fe_type The FE type
* @param is_fv Whether or not the variable is use for finite volume
* @param is_array Whether or not the variable is an array variable
*/
static std::string
variableType(const FEType & fe_type, const bool is_fv = false, const bool is_array = false);

protected:
/**
* Initialize the action's member variables
Expand Down
13 changes: 11 additions & 2 deletions framework/src/actions/AddVariableAction.C
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ AddVariableAction::init()

// Determine the MooseVariable type
bool is_fv = _moose_object_pars.get<bool>("fv");
bool is_array = (_components > 1 || _moose_object_pars.get<bool>("array")) ? true : false;
if (_type == "MooseVariableBase")
_type = determineType(_fe_type, _components, is_fv);
_type = variableType(_fe_type, is_fv, is_array);
if (is_fv)
_problem->needFV();

Expand Down Expand Up @@ -224,11 +225,19 @@ AddVariableAction::createInitialConditionAction()

std::string
AddVariableAction::determineType(const FEType & fe_type, unsigned int components, bool is_fv)
{
mooseDeprecated("AddVariableAction::determineType() is deprecated. Use "
"AddVariableAction::variableType() instead.");
return variableType(fe_type, is_fv, components > 1);
}

std::string
AddVariableAction::variableType(const FEType & fe_type, const bool is_fv, const bool is_array)
{
if (is_fv)
return "MooseVariableFVReal";

if (components > 1)
if (is_array)
{
if (fe_type.family == LAGRANGE_VEC || fe_type.family == NEDELEC_ONE ||
fe_type.family == MONOMIAL_VEC)
Expand Down
7 changes: 7 additions & 0 deletions framework/src/systems/SystemBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,13 @@ SystemBase::addVariable(const std::string & var_type,
// The number returned by libMesh is the _last_ variable number... we want to hold onto the
// _first_
var_num = system().add_variables(var_names, fe_type, &blocks) - (components - 1);

// Set as array variable
if (parameters.isParamSetByUser("array") && !parameters.get<bool>("array"))
mooseError("Variable '",
name,
"' is an array variable ('components' > 1) but 'array' is set to false.");
parameters.set<bool>("array") = true;
}
else
var_num = system().add_variable(name, fe_type, &blocks);
Expand Down
11 changes: 10 additions & 1 deletion framework/src/variables/MooseVariableBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ MooseVariableBase::validParams()
"Specifies a scaling factor to apply to this variable");
params.addParam<bool>("eigen", false, "True to make this variable an eigen variable");
params.addParam<bool>("fv", false, "True to make this variable a finite volume variable");
params.addParam<bool>(
"array",
false,
"True to make this variable a array variable regardless of number of components");
params.addParamNamesToGroup("scaling eigen", "Advanced");

params.addParam<bool>("use_dual", false, "True to use dual basis for Lagrange multipliers");
Expand Down Expand Up @@ -107,8 +111,10 @@ MooseVariableBase::MooseVariableBase(const InputParameters & parameters)
paramError("family", "finite volume (fv=true) variables must be have MONOMIAL family");
if (getParam<bool>("fv") && _fe_type.order != 0)
paramError("order", "finite volume (fv=true) variables currently support CONST order only");

bool is_array = getParam<bool>("array");
if (_count > 1)
mooseAssert(is_array, "Must be true with component > 1");
if (is_array)
{
auto name0 = _sys.system().variable(_var_num).name();
std::size_t found = name0.find_last_of("_");
Expand All @@ -117,7 +123,10 @@ MooseVariableBase::MooseVariableBase(const InputParameters & parameters)
_var_name = name0.substr(0, found);
}
else
{
mooseAssert(_count == 1, "component size of normal variable (_count) must be one");
_var_name = _sys.system().variable(_var_num).name();
}
}

const std::vector<dof_id_type> &
Expand Down
67 changes: 67 additions & 0 deletions test/tests/variables/array_variable/array_variable_size_one_test.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[Mesh]
[square]
type = GeneratedMeshGenerator
nx = 3
ny = 3
dim = 2
[]
[]

[Variables]
[u]
order = CONSTANT
family = MONOMIAL
components = 1
array = true
[]
[]

[Kernels]
[diff]
type = ArrayDiffusion
variable = u
diffusion_coefficient = dc
[]
[reaction]
type = ArrayReaction
variable = u
reaction_coefficient = rc
[]
[source]
type = ArrayBodyForce
variable = u
function = '1'
[]
[]

[BCs]
[all]
type = ArrayVacuumBC
boundary = 'left right top bottom'
variable = u
[]
[]

[Materials]
[dc]
type = GenericConstantArray
prop_name = dc
prop_value = '1'
[]
[rc]
type = GenericConstantArray
prop_name = rc
prop_value = '2'
[]
[]

[Executioner]
type = Steady
solve_type = 'NEWTON'
[]

[Outputs]
[out]
type = Exodus
[]
[]
Binary file not shown.
17 changes: 17 additions & 0 deletions test/tests/variables/array_variable/tests
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,21 @@
issues = '#6881'
design = 'ArrayMooseVariable.md'
[../]
[array_variable_size_one]
type = 'Exodiff'
input = 'array_variable_size_one_test.i'
exodiff = 'array_variable_size_one_test_out.e'
requirement = 'MOOSE shall be able to define an array variable with component size one.'
issues = '#19564'
design = 'ArrayMooseVariable.md'
[]
[array_variable_array_false]
type = 'RunException'
input = 'array_variable_size_one_test.i'
cli_args = 'Variables/u/components=2 Variables/u/array=false'
requirement = 'MOOSE shall report a reasonable error when defining a variable with multiple components not as an array variable.'
issues = '#19564'
expect_err = "'array' is set to false."
design = 'ArrayMooseVariable.md'
[]
[]

0 comments on commit 7e46260

Please sign in to comment.