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 : #pragma once 11 : 12 : #include "KokkosFunctionParser.h" 13 : 14 : #include "MooseObject.h" 15 : 16 : namespace Moose::Kokkos 17 : { 18 : 19 : class ParsedObjectBase 20 : { 21 : public: 22 : static InputParameters validParams(); 23 : 24 : ParsedObjectBase(const MooseObject * object); 25 : 26 : protected: 27 : /** 28 : * Check if duplicate symbols were added 29 : * @param symbols The list of symbols 30 : * @param param The parameter name containing symbols 31 : */ 32 : template <typename T> 33 : void checkDuplicateSymbols(const std::vector<T> & symbols, const std::string & param); 34 : 35 : /** 36 : * Add a constant 37 : * @param name The variable name 38 : * @param constant The constant value 39 : */ 40 : void addConstant(const std::string & name, const Real constant); 41 : /** 42 : * Add a scalar variable 43 : * @param name The variable name 44 : * @param scalar The pointer to the scalar variable 45 : */ 46 : void addScalar(const std::string & name, const Real & scalar); 47 : /** 48 : * Add a field variable 49 : * @param name The variable name 50 : * @param field The coupled field variable 51 : */ 52 : void addField(const std::string & name, const VariableValue & field); 53 : /** 54 : * Add a material property 55 : * @param name The variable name 56 : * @param property The material property 57 : */ 58 : void addProperty(const std::string & name, const MaterialProperty<Real> & property); 59 : /** 60 : * Add a function 61 : * @param name The variable name 62 : * @param function The function 63 : */ 64 : void addFunction(const std::string & name, const Function & function); 65 : 66 : /** 67 : * Initialize symbols from parsed parameters. \p variable_names must be supplied by 68 : * the caller because Coupleable::coupledNames() is protected. 69 : */ 70 : template <typename T> 71 : void initParsed(T * obj, const std::vector<VariableName> & variable_names); 72 : 73 : /** 74 : * Parsed expression 75 : */ 76 : const std::string & _expression; 77 : /** 78 : * Parsed function builder 79 : */ 80 : std::shared_ptr<RPNBuilder> _builder; 81 : /** 82 : * Parsed function evaluator 83 : */ 84 : RPNEvaluator _evaluator; 85 : /** 86 : * Constants used in the parsed expression 87 : */ 88 : std::unordered_map<std::string, Real> _constants; 89 : /** 90 : * Scalar variables used in the parsed expression 91 : */ 92 : std::unordered_map<std::string, std::reference_wrapper<const Real>> _scalars; 93 : /** 94 : * Field variables used in the parsed expression 95 : */ 96 : std::unordered_map<std::string, VariableValue> _fields; 97 : /** 98 : * Material properties used in the parsed expression 99 : */ 100 : std::unordered_map<std::string, MaterialProperty<Real>> _properties; 101 : /** 102 : * Functions used in the parsed expression 103 : */ 104 : std::unordered_map<std::string, Function> _functions; 105 : 106 : private: 107 : /** 108 : * Finalize parsed function 109 : */ 110 : void finalize(); 111 : 112 : /** 113 : * Parsed object 114 : */ 115 : const MooseObject * _parsed_object; 116 : /** 117 : * All symbols added to the parsed function 118 : */ 119 : std::unordered_set<std::string> _all_symbols; 120 : }; 121 : 122 : template <typename T> 123 : void 124 1155 : ParsedObjectBase::checkDuplicateSymbols(const std::vector<T> & symbols, const std::string & param) 125 : { 126 1379 : for (const auto & symbol : symbols) 127 : { 128 224 : if (_all_symbols.count(symbol)) 129 0 : _parsed_object->paramError(param, "Symbol '", symbol, "' was added multiple times."); 130 : 131 224 : _all_symbols.insert(symbol); 132 : } 133 1155 : } 134 : 135 : template <typename T> 136 : void 137 231 : ParsedObjectBase::initParsed(T * obj, const std::vector<VariableName> & variable_names) 138 : { 139 462 : const auto & constant_names = obj->template getParam<std::vector<std::string>>("constant_names"); 140 192 : const auto & postprocessor_names = 141 270 : obj->template getParam<std::vector<PostprocessorName>>("postprocessor_names"); 142 192 : const auto & property_names = 143 270 : obj->template getParam<std::vector<MaterialPropertyName>>("material_property_names"); 144 462 : const auto & function_names = obj->template getParam<std::vector<FunctionName>>("function_names"); 145 192 : const auto & constant_expressions = 146 270 : obj->template getParam<std::vector<Real>>("constant_expressions"); 147 : 148 231 : for (const auto i : make_range(constant_names.size())) 149 0 : addConstant(constant_names[i], constant_expressions[i]); 150 : 151 231 : for (const auto & pp : postprocessor_names) 152 0 : addScalar(pp, obj->getPostprocessorValueByName(pp)); 153 : 154 394 : for (const auto i : make_range(variable_names.size())) 155 489 : addField(variable_names[i], obj->kokkosCoupledValue("coupled_variables", i)); 156 : 157 265 : for (const auto & prop : property_names) 158 34 : addProperty(prop, obj->template getKokkosMaterialPropertyByName<Real>(prop)); 159 : 160 258 : for (const auto & func : function_names) 161 27 : addFunction(func, obj->getKokkosFunctionByName(func)); 162 : 163 231 : finalize(); 164 231 : } 165 : 166 : } // namespace Moose::Kokkos