Skip to content

Commit b8ac89d

Browse files
author
Kylee Swanson
committed
Eliminated code duplication in sanity checks
Issue #21422
1 parent ba44786 commit b8ac89d

File tree

3 files changed

+71
-44
lines changed

3 files changed

+71
-44
lines changed

framework/include/auxkernels/ParsedAux.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ class ParsedAux : public AuxKernel, public FunctionParserUtils<false>
2525
protected:
2626
virtual Real computeValue() override;
2727

28+
/// Function to validate the symbols in _functor_symbols
29+
void validateFunctorSymbols();
30+
31+
/// Function to validate the names in _functor_names
32+
void validateFunctorNames();
33+
34+
/**
35+
* Function to ensure vector entries (names) do not overlap with xyzt or coupled variable names.
36+
* @param names_vec Vector containing names to compare to xyzt and coupled variables names.
37+
* @param param_name Name of the parameter corresponding to names_vec. This will be the paremeter
38+
* errored on, if applicable.
39+
*/
40+
template <typename T>
41+
void validateGenericVectorNames(const std::vector<T> & names_vec, const std::string & param_name);
42+
2843
/// function expression
2944
std::string _function;
3045

@@ -35,6 +50,9 @@ class ParsedAux : public AuxKernel, public FunctionParserUtils<false>
3550
/// import coordinates and time
3651
const bool _use_xyzt;
3752

53+
/// coordinate and time variable names
54+
const std::vector<std::string> _xyzt;
55+
3856
/// function parser object for the resudual and on-diagonal Jacobian
3957
SymFunctionPtr _func_F;
4058

@@ -51,4 +69,31 @@ class ParsedAux : public AuxKernel, public FunctionParserUtils<false>
5169

5270
/// Vector of pointers to functors
5371
std::vector<const Moose::Functor<Real> *> _functors;
72+
73+
/// Vector of coupled variable names
74+
std::vector<std::string> _coupled_variable_names;
5475
};
76+
77+
template <typename T>
78+
void
79+
ParsedAux::validateGenericVectorNames(const std::vector<T> & names_vec,
80+
const std::string & param_name)
81+
{
82+
for (const auto & name : names_vec)
83+
{
84+
// Make sure symbol is not x, y, z, or t
85+
if (_use_xyzt && (std::find(_xyzt.begin(), _xyzt.end(), name) != _xyzt.end()))
86+
paramError(
87+
param_name,
88+
"x, y, z, and t cannot be used in '" + param_name + "' when use_xyzt=true." +
89+
(param_name == "functor_names" ? " Use 'functor_symbols' to disambiguate." : ""));
90+
// Make sure symbol is not a coupled variable name
91+
if (_coupled_variable_names.size() &&
92+
(std::find(_coupled_variable_names.begin(), _coupled_variable_names.end(), name) !=
93+
_coupled_variable_names.end()))
94+
paramError(
95+
param_name,
96+
"Values in '" + param_name + "' cannot overlap with coupled variable names." +
97+
(param_name == "functor_names" ? " Use 'functor_symbols' to disambiguate." : ""));
98+
}
99+
}

framework/src/auxkernels/ParsedAux.C

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -55,58 +55,28 @@ ParsedAux::ParsedAux(const InputParameters & parameters)
5555
_nargs(coupledComponents("coupled_variables")),
5656
_args(coupledValues("coupled_variables")),
5757
_use_xyzt(getParam<bool>("use_xyzt")),
58+
_xyzt({"x", "y", "z", "t"}),
5859
_functor_names(getParam<std::vector<MooseFunctorName>>("functor_names")),
5960
_n_functors(_functor_names.size()),
6061
_functor_symbols(getParam<std::vector<std::string>>("functor_symbols"))
6162
{
63+
64+
for (const auto i : make_range(_nargs))
65+
_coupled_variable_names.push_back(getFieldVar("coupled_variables", i)->name());
66+
6267
// sanity checks
6368
if (!_functor_symbols.empty() && (_functor_symbols.size() != _n_functors))
6469
paramError("functor_symbols", "functor_symbols must be the same length as functor_names.");
6570

66-
const std::vector<std::string> xyzt = {"x", "y", "z", "t"};
67-
std::vector<std::string> variable_names;
68-
for (const auto i : make_range(_nargs))
69-
variable_names.push_back(getFieldVar("coupled_variables", i)->name());
70-
71-
if (_functor_symbols.size())
72-
{
73-
for (const auto & symbol : _functor_symbols)
74-
{
75-
// Make sure symbol is not x, y, z, or t
76-
if (_use_xyzt && (std::find(xyzt.begin(), xyzt.end(), symbol) != xyzt.end()))
77-
paramError("functor_symbols",
78-
"x, y, z, and t cannot be used as a functor symbol when use_xyzt=true.");
79-
// Make sure symbol is not a variable name
80-
if (variable_names.size() &&
81-
(std::find(variable_names.begin(), variable_names.end(), symbol) != variable_names.end()))
82-
paramError("functor_symbols",
83-
"Functor symbols cannot overlap with coupled variable names.");
84-
}
85-
}
86-
else
87-
for (const auto & name : _functor_names)
88-
{
89-
// Make sure symbol is not x, y, z, or t
90-
if (_use_xyzt && (std::find(xyzt.begin(), xyzt.end(), std::string(name)) != xyzt.end()))
91-
paramError("functor_names",
92-
"x, y, z, and t cannot be used as a functor name when use_xyzt=true. Use "
93-
"'functor_symbols' to disambiguate.");
94-
// Make sure symbol is not a variable name
95-
if (variable_names.size() &&
96-
(std::find(variable_names.begin(), variable_names.end(), std::string(name)) !=
97-
variable_names.end()))
98-
paramError(
99-
"functor_names",
100-
"Functor names cannot overlap with coupled variable names. Use 'functor_symbols' to "
101-
"disambiguate.");
102-
}
71+
validateFunctorSymbols();
72+
validateFunctorNames();
10373

10474
// build variables argument
10575
std::string variables;
10676

10777
// coupled field variables
108-
for (const auto i : make_range(_nargs))
109-
variables += (i == 0 ? "" : ",") + getFieldVar("coupled_variables", i)->name();
78+
for (const auto i : index_range(_coupled_variable_names))
79+
variables += (i == 0 ? "" : ",") + _coupled_variable_names[i];
11080

11181
// adding functors to the expression
11282
if (_functor_symbols.size())
@@ -118,7 +88,7 @@ ParsedAux::ParsedAux(const InputParameters & parameters)
11888

11989
// "system" variables
12090
if (_use_xyzt)
121-
for (auto & v : xyzt)
91+
for (auto & v : _xyzt)
12292
variables += (variables.empty() ? "" : ",") + v;
12393

12494
// base function object
@@ -191,3 +161,15 @@ ParsedAux::computeValue()
191161

192162
return evaluate(_func_F);
193163
}
164+
165+
void
166+
ParsedAux::validateFunctorSymbols()
167+
{
168+
validateGenericVectorNames(_functor_symbols, "functor_symbols");
169+
}
170+
171+
void
172+
ParsedAux::validateFunctorNames()
173+
{
174+
validateGenericVectorNames(_functor_names, "functor_names");
175+
}

test/tests/auxkernels/parsed_aux/tests

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,28 @@
3838
type = RunException
3939
input = 'parsed_aux_functors_test.i'
4040
cli_args = "AuxKernels/set_parsed/functor_names='u v' AuxKernels/set_parsed/functor_symbols='x v' AuxKernels/set_parsed/use_xyzt=true"
41-
expect_err = "x, y, z, and t cannot be used as a functor symbol when use_xyzt=true."
41+
expect_err = "x, y, z, and t cannot be used in 'functor_symbols' when use_xyzt=true."
4242
detail = "functor symbols parameter contains 'x', 'y', 'z', or 't' when coordinates and time parameters are already in use"
4343
[../]
4444
[functor_symbol_variable_name_overlap]
4545
type = RunException
4646
input = 'parsed_aux_functors_test.i'
4747
cli_args = "AuxKernels/set_parsed/functor_symbols='u v' AuxKernels/set_parsed/coupled_variables='u v'"
48-
expect_err = "Functor symbols cannot overlap with coupled variable names."
48+
expect_err = "Values in 'functor_symbols' cannot overlap with coupled variable names."
4949
detail = "functor symbols cannot overlap with coupled variable names"
5050
[../]
5151
[invalid_functor_name]
5252
type = RunException
5353
input = 'parsed_aux_functors_test.i'
5454
cli_args = "AuxKernels/set_parsed/functor_names='x v' AuxKernels/set_parsed/functor_symbols='' AuxKernels/set_parsed/use_xyzt=true"
55-
expect_err = "x, y, z, and t cannot be used as a functor name when use_xyzt=true. Use 'functor_symbols' to disambiguate."
55+
expect_err = "x, y, z, and t cannot be used in 'functor_names' when use_xyzt=true. Use 'functor_symbols' to disambiguate."
5656
detail = "functor names parameter contains 'x', 'y', 'z', or 't' when coordinates and time parameters are already in use"
5757
[../]
5858
[functor_name_variable_name_overlap]
5959
type = RunException
6060
input = 'parsed_aux_functors_test.i'
6161
cli_args = "AuxKernels/set_parsed/functor_names='u v' AuxKernels/set_parsed/coupled_variables='u v' AuxKernels/set_parsed/functor_symbols=''"
62-
expect_err = "Functor names cannot overlap with coupled variable names. Use 'functor_symbols' to disambiguate."
62+
expect_err = "Values in 'functor_names' cannot overlap with coupled variable names. Use 'functor_symbols' to disambiguate."
6363
detail = "functor names cannot overlap with coupled variable names"
6464
[../]
6565
[expression]

0 commit comments

Comments
 (0)