Line data Source code
1 : //* This file is part of the MOOSE framework
2 : //* https://www.mooseframework.org
3 : //*
4 : //* All rights reserved, see COPYRIGHT for full restrictions
5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 : //*
7 : //* Licensed under LGPL 2.1, please see LICENSE for details
8 : //* https://www.gnu.org/licenses/lgpl-2.1.html
9 :
10 : #include "KokkosParsedObjectBase.h"
11 :
12 : #include "FEProblemBase.h"
13 :
14 : namespace Moose::Kokkos
15 : {
16 :
17 : InputParameters
18 4572 : ParsedObjectBase::validParams()
19 : {
20 4572 : InputParameters params = emptyInputParameters();
21 :
22 13880 : params.addParam<bool>(
23 : "use_xyzt",
24 8816 : false,
25 : "Make coordinate (x,y,z) and time (t) variables available in the function expression.");
26 13880 : params.addParam<bool>(
27 8816 : "use_dt", false, "Make time step (dt) variable available in the function expression.");
28 :
29 : // Constants and their values
30 13880 : params.addParam<std::vector<std::string>>("constant_names",
31 8816 : std::vector<std::string>(),
32 : "Vector of constants used in the parsed function.");
33 13880 : params.addParam<std::vector<Real>>("constant_expressions",
34 8816 : std::vector<Real>(),
35 : "Vector of values for the constants in constant_names.");
36 :
37 : // Postprocessors
38 13880 : params.addParam<std::vector<PostprocessorName>>(
39 : "postprocessor_names",
40 8816 : std::vector<PostprocessorName>(),
41 : "Vector of postprocessor names used in the parsed function.");
42 :
43 : // Field variables
44 18288 : params.addCoupledVar("coupled_variables", "Vector of variables used in the parsed function.");
45 :
46 : // Material properties
47 13880 : params.addParam<std::vector<MaterialPropertyName>>(
48 : "material_property_names",
49 8816 : std::vector<MaterialPropertyName>(),
50 : "Vector of material properties used in the parsed function.");
51 :
52 : // Functions
53 13880 : params.addParam<std::vector<FunctionName>>("function_names",
54 8816 : std::vector<FunctionName>(),
55 : "Vector of functions used in the parsed function.");
56 :
57 : // Function expression
58 13716 : params.addParam<std::string>("expression", "Parsed function expression.");
59 :
60 4572 : return params;
61 0 : }
62 :
63 231 : ParsedObjectBase::ParsedObjectBase(const MooseObject * object)
64 135 : : _expression(object->parameters().get<std::string>("expression")),
65 135 : _builder(std::make_shared<Moose::Kokkos::RPNBuilder>(_expression, &object->_console)),
66 135 : _parsed_object(object)
67 : {
68 231 : const auto & parameters = object->parameters();
69 :
70 231 : if (parameters.get<bool>("use_xyzt"))
71 : {
72 68 : _builder->addDefaultVariables();
73 :
74 136 : _all_symbols.insert("x");
75 136 : _all_symbols.insert("y");
76 136 : _all_symbols.insert("z");
77 204 : _all_symbols.insert("t");
78 : }
79 :
80 231 : if (parameters.get<bool>("use_dt"))
81 : {
82 0 : addScalar("dt", object->getMooseApp().feProblem().dt());
83 :
84 0 : _all_symbols.insert("dt");
85 : }
86 :
87 231 : const auto & constant_names = parameters.get<std::vector<std::string>>("constant_names");
88 96 : const auto & postprocessor_names =
89 135 : parameters.get<std::vector<PostprocessorName>>("postprocessor_names");
90 231 : const auto & variable_names = parameters.isParamValid("coupled_variables")
91 135 : ? parameters.get<std::vector<VariableName>>("coupled_variables")
92 135 : : std::vector<VariableName>();
93 96 : const auto & property_names =
94 135 : parameters.get<std::vector<MaterialPropertyName>>("material_property_names");
95 231 : const auto & function_names = parameters.get<std::vector<FunctionName>>("function_names");
96 :
97 462 : checkDuplicateSymbols(constant_names, "constant_names");
98 462 : checkDuplicateSymbols(postprocessor_names, "postprocessor_names");
99 462 : checkDuplicateSymbols(variable_names, "coupled_variables");
100 462 : checkDuplicateSymbols(property_names, "material_property_names");
101 231 : checkDuplicateSymbols(function_names, "function_names");
102 231 : }
103 :
104 : void
105 0 : ParsedObjectBase::addConstant(const std::string & name, const Real constant)
106 : {
107 0 : _builder->addVariable(name);
108 :
109 0 : _constants[name] = constant;
110 0 : }
111 :
112 : void
113 0 : ParsedObjectBase::addScalar(const std::string & name, const Real & scalar)
114 : {
115 0 : _builder->addVariable(name);
116 :
117 0 : _scalars.insert({name, scalar});
118 0 : }
119 :
120 : void
121 163 : ParsedObjectBase::addField(const std::string & name, const VariableValue & field)
122 : {
123 163 : _builder->addVariable(name);
124 :
125 163 : _fields[name] = field;
126 163 : }
127 :
128 : void
129 34 : ParsedObjectBase::addProperty(const std::string & name, const MaterialProperty<Real> & property)
130 : {
131 34 : _builder->addVariable(name);
132 :
133 34 : _properties[name] = property;
134 34 : }
135 :
136 : void
137 27 : ParsedObjectBase::addFunction(const std::string & name, const Function & function)
138 : {
139 27 : _builder->addVariable(name);
140 :
141 27 : _functions.insert({name, function});
142 27 : }
143 :
144 : void
145 231 : ParsedObjectBase::finalize()
146 : {
147 231 : for (const auto & [name, constant] : _constants)
148 0 : _builder->associateScalar(name, &constant);
149 :
150 231 : for (const auto & [name, scalar] : _scalars)
151 0 : _builder->associateScalar(name, &scalar.get());
152 :
153 394 : for (const auto & [name, field] : _fields)
154 163 : _builder->associateField(name, &field);
155 :
156 265 : for (const auto & [name, property] : _properties)
157 34 : _builder->associateProperty(name, &property);
158 :
159 258 : for (const auto & [name, function] : _functions)
160 27 : _builder->associateFunction(name, &function);
161 :
162 231 : _builder->build();
163 231 : _builder->finalize();
164 :
165 : // Should be called after finalization
166 231 : _evaluator.init(*_builder);
167 231 : }
168 :
169 : } // namespace Moose::Kokkos
|