Skip to content

Commit 4f3452a

Browse files
committed
Address comments idaholab#30655
1 parent 8b15f16 commit 4f3452a

8 files changed

Lines changed: 60 additions & 53 deletions

File tree

framework/include/kokkos/base/KokkosParsedObjectBase.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ParsedObjectBase
3030
* @param param The parameter name containing symbols
3131
*/
3232
template <typename T>
33-
void checkDuplicateSymbols(const std::vector<T> symbols, const std::string param);
33+
void checkDuplicateSymbols(const std::vector<T> & symbols, const std::string & param);
3434

3535
/**
3636
* Add a constant
@@ -43,7 +43,7 @@ class ParsedObjectBase
4343
* @param name The variable name
4444
* @param scalar The pointer to the scalar variable
4545
*/
46-
void addScalar(const std::string & name, const Real * scalar);
46+
void addScalar(const std::string & name, const Real & scalar);
4747
/**
4848
* Add a field variable
4949
* @param name The variable name
@@ -57,18 +57,14 @@ class ParsedObjectBase
5757
*/
5858
void addProperty(const std::string & name, const MaterialProperty<Real> & property);
5959
/**
60-
* Add a material property
60+
* Add a function
6161
* @param name The variable name
6262
* @param function The function
6363
*/
6464
void addFunction(const std::string & name, const Function & function);
65-
/**
66-
* Finalize parsed function
67-
*/
68-
void finalize();
6965

7066
/**
71-
* Initialize symbols from parsed parameters. variable_names must be supplied by
67+
* Initialize symbols from parsed parameters. \p variable_names must be supplied by
7268
* the caller because Coupleable::coupledNames() is protected.
7369
*/
7470
template <typename T>
@@ -89,25 +85,30 @@ class ParsedObjectBase
8985
/**
9086
* Constants used in the parsed expression
9187
*/
92-
std::map<std::string, Real> _constants;
88+
std::unordered_map<std::string, Real> _constants;
9389
/**
9490
* Scalar variables used in the parsed expression
9591
*/
96-
std::map<std::string, const Real *> _scalars;
92+
std::unordered_map<std::string, std::reference_wrapper<const Real>> _scalars;
9793
/**
9894
* Field variables used in the parsed expression
9995
*/
100-
std::map<std::string, VariableValue> _fields;
96+
std::unordered_map<std::string, VariableValue> _fields;
10197
/**
10298
* Material properties used in the parsed expression
10399
*/
104-
std::map<std::string, MaterialProperty<Real>> _properties;
100+
std::unordered_map<std::string, MaterialProperty<Real>> _properties;
105101
/**
106102
* Functions used in the parsed expression
107103
*/
108-
std::map<std::string, Function> _functions;
104+
std::unordered_map<std::string, Function> _functions;
109105

110106
private:
107+
/**
108+
* Finalize parsed function
109+
*/
110+
void finalize();
111+
111112
/**
112113
* Parsed object
113114
*/
@@ -120,7 +121,7 @@ class ParsedObjectBase
120121

121122
template <typename T>
122123
void
123-
ParsedObjectBase::checkDuplicateSymbols(const std::vector<T> symbols, const std::string param)
124+
ParsedObjectBase::checkDuplicateSymbols(const std::vector<T> & symbols, const std::string & param)
124125
{
125126
for (const auto & symbol : symbols)
126127
{
@@ -148,7 +149,7 @@ ParsedObjectBase::initParsed(T * obj, const std::vector<VariableName> & variable
148149
addConstant(constant_names[i], constant_expressions[i]);
149150

150151
for (const auto & pp : postprocessor_names)
151-
addScalar(pp, &obj->getPostprocessorValueByName(pp));
152+
addScalar(pp, obj->getPostprocessorValueByName(pp));
152153

153154
for (const auto i : make_range(variable_names.size()))
154155
addField(variable_names[i], obj->kokkosCoupledValue("coupled_variables", i));

framework/src/functions/MooseParsedFunctionBase.C

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ MooseParsedFunctionBase::validParams()
3030
"Use 'symbol_values' instead.");
3131
params.addParam<std::vector<std::string>>(
3232
"symbol_names",
33-
std::vector<std::string>(),
3433
"Symbols (excluding t,x,y,z) that are bound to the values provided by the corresponding "
3534
"items in the vals vector.");
3635
params.addParam<std::vector<std::string>>("symbol_values",
37-
std::vector<std::string>(),
3836
"Constant numeric values, postprocessor names, "
3937
"function names, and scalar variables corresponding to"
4038
" the symbols in symbol_names.");

framework/src/kokkos/base/KokkosParsedObjectBase.K

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include "KokkosParsedObjectBase.h"
1111

12+
#include "FEProblemBase.h"
13+
1214
namespace Moose::Kokkos
1315
{
1416

@@ -21,6 +23,8 @@ ParsedObjectBase::validParams()
2123
"use_xyzt",
2224
false,
2325
"Make coordinate (x,y,z) and time (t) variables available in the function expression.");
26+
params.addParam<bool>(
27+
"use_dt", false, "Make time step (dt) variable available in the function expression.");
2428

2529
// Constants and their values
2630
params.addParam<std::vector<std::string>>("constant_names",
@@ -34,24 +38,24 @@ ParsedObjectBase::validParams()
3438
params.addParam<std::vector<PostprocessorName>>(
3539
"postprocessor_names",
3640
std::vector<PostprocessorName>(),
37-
"Vector of postprocessor names used in the parsed function");
41+
"Vector of postprocessor names used in the parsed function.");
3842

3943
// Field variables
40-
params.addCoupledVar("coupled_variables", "Vector of variables used in the parsed function");
44+
params.addCoupledVar("coupled_variables", "Vector of variables used in the parsed function.");
4145

4246
// Material properties
4347
params.addParam<std::vector<MaterialPropertyName>>(
4448
"material_property_names",
4549
std::vector<MaterialPropertyName>(),
46-
"Vector of material properties used in the parsed function");
50+
"Vector of material properties used in the parsed function.");
4751

4852
// Functions
4953
params.addParam<std::vector<FunctionName>>("function_names",
5054
std::vector<FunctionName>(),
51-
"Vector of functions used in the parsed function");
55+
"Vector of functions used in the parsed function.");
5256

5357
// Function expression
54-
params.addParam<std::string>("expression", "Parsed function expression for the parsed material");
58+
params.addParam<std::string>("expression", "Parsed function expression.");
5559

5660
return params;
5761
}
@@ -73,6 +77,13 @@ ParsedObjectBase::ParsedObjectBase(const MooseObject * object)
7377
_all_symbols.insert("t");
7478
}
7579

80+
if (parameters.get<bool>("use_dt"))
81+
{
82+
addScalar("dt", object->getMooseApp().feProblem().dt());
83+
84+
_all_symbols.insert("dt");
85+
}
86+
7687
const auto & constant_names = parameters.get<std::vector<std::string>>("constant_names");
7788
const auto & postprocessor_names =
7889
parameters.get<std::vector<PostprocessorName>>("postprocessor_names");
@@ -96,16 +107,14 @@ ParsedObjectBase::addConstant(const std::string & name, const Real constant)
96107
_builder->addVariable(name);
97108

98109
_constants[name] = constant;
99-
_builder->associateScalar(name, &_constants.at(name));
100110
}
101111

102112
void
103-
ParsedObjectBase::addScalar(const std::string & name, const Real * scalar)
113+
ParsedObjectBase::addScalar(const std::string & name, const Real & scalar)
104114
{
105115
_builder->addVariable(name);
106116

107-
_scalars[name] = scalar;
108-
_builder->associateScalar(name, _scalars.at(name));
117+
_scalars.insert({name, scalar});
109118
}
110119

111120
void
@@ -114,7 +123,6 @@ ParsedObjectBase::addField(const std::string & name, const VariableValue & field
114123
_builder->addVariable(name);
115124

116125
_fields[name] = field;
117-
_builder->associateField(name, &_fields.at(name));
118126
}
119127

120128
void
@@ -123,7 +131,6 @@ ParsedObjectBase::addProperty(const std::string & name, const MaterialProperty<R
123131
_builder->addVariable(name);
124132

125133
_properties[name] = property;
126-
_builder->associateProperty(name, &_properties.at(name));
127134
}
128135

129136
void
@@ -132,12 +139,26 @@ ParsedObjectBase::addFunction(const std::string & name, const Function & functio
132139
_builder->addVariable(name);
133140

134141
_functions.insert({name, function});
135-
_builder->associateFunction(name, &_functions.at(name));
136142
}
137143

138144
void
139145
ParsedObjectBase::finalize()
140146
{
147+
for (const auto & [name, constant] : _constants)
148+
_builder->associateScalar(name, &constant);
149+
150+
for (const auto & [name, scalar] : _scalars)
151+
_builder->associateScalar(name, &scalar.get());
152+
153+
for (const auto & [name, field] : _fields)
154+
_builder->associateField(name, &field);
155+
156+
for (const auto & [name, property] : _properties)
157+
_builder->associateProperty(name, &property);
158+
159+
for (const auto & [name, function] : _functions)
160+
_builder->associateFunction(name, &function);
161+
141162
_builder->build();
142163
_builder->finalize();
143164

framework/src/kokkos/functions/KokkosParsedFunction.K

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ KokkosParsedFunction::KokkosParsedFunction(const InputParameters & parameters)
3737
{
3838
_builder->addDefaultVariables();
3939

40-
const auto & symbol_names = getParam<std::vector<std::string>>("symbol_names");
40+
std::vector<std::string> symbol_names;
41+
42+
if (isParamValid("symbol_names"))
43+
symbol_names = getParam<std::vector<std::string>>("symbol_names");
4144

4245
for (const auto & name : symbol_names)
4346
_builder->addVariable(name);
@@ -65,8 +68,13 @@ KokkosParsedFunction::KokkosParsedFunction(const KokkosParsedFunction & function
6568
void
6669
KokkosParsedFunction::initialSetup()
6770
{
68-
const auto & symbol_names = getParam<std::vector<std::string>>("symbol_names");
69-
const auto & symbol_values = getParam<std::vector<std::string>>("symbol_values");
71+
std::vector<std::string> symbol_names;
72+
std::vector<std::string> symbol_values;
73+
74+
if (isParamValid("symbol_names"))
75+
symbol_names = getParam<std::vector<std::string>>("symbol_names");
76+
if (isParamValid("symbol_values"))
77+
symbol_values = getParam<std::vector<std::string>>("symbol_values");
7078

7179
_symbol_values.reserve(symbol_values.size());
7280
_symbol_functions.reserve(symbol_values.size());

framework/src/kokkos/materials/KokkosParsedMaterial.K

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ KokkosParsedMaterial::validParams()
1919

2020
params.addRequiredParam<MaterialPropertyName>("property_name",
2121
"Name of the parsed material property");
22-
params.addParam<bool>(
23-
"use_dt", false, "Make time step (dt) variable available in the function expression.");
2422
params.addClassDescription("Evaluates a property with a parsed expression.");
2523

2624
return params;
@@ -31,13 +29,5 @@ KokkosParsedMaterial::KokkosParsedMaterial(const InputParameters & parameters)
3129
ParsedObjectBase(this),
3230
_prop(declareKokkosProperty<Real>("property_name"))
3331
{
34-
if (getParam<bool>("use_dt"))
35-
{
36-
checkDuplicateSymbols(std::vector<std::string>{"dt"}, "use_dt");
37-
38-
auto & dt = *_dt;
39-
addScalar("dt", &dt);
40-
}
41-
4232
initParsed(this, coupledNames("coupled_variables"));
4333
}

test/tests/kokkos/auxkernels/parsed/kokkos_parsed_aux_mat_test.i

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
[Mesh]
22
type = GeneratedMesh
3-
43
dim = 2
5-
64
xmin = 0
75
xmax = 1
8-
96
ymin = 0
107
ymax = 1
11-
128
nx = 10
139
ny = 10
1410
[]
@@ -100,7 +96,6 @@
10096

10197
[Executioner]
10298
type = Steady
103-
10499
solve_type = 'PJFNK'
105100
[]
106101

test/tests/kokkos/auxkernels/parsed/kokkos_parsed_aux_test.i

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
[Mesh]
22
type = GeneratedMesh
3-
43
dim = 2
5-
64
xmin = 0
75
xmax = 1
8-
96
ymin = 0
107
ymax = 1
11-
128
nx = 10
139
ny = 10
1410
[]
@@ -85,7 +81,6 @@
8581

8682
[Executioner]
8783
type = Steady
88-
8984
solve_type = 'PJFNK'
9085
[]
9186

test/tests/kokkos/materials/parsed/kokkos_parsed_material.i

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282

8383
[Executioner]
8484
type = Transient
85-
8685
solve_type = 'PJFNK'
8786
end_time = 10
8887
[]

0 commit comments

Comments
 (0)