Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 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 "KokkosParsedFunction.h" 11 : 12 : #include "FEProblemBase.h" 13 : 14 : registerKokkosFunction("MooseApp", KokkosParsedFunction); 15 : 16 : InputParameters 17 4214 : KokkosParsedFunction::validParams() 18 : { 19 4214 : InputParameters params = FunctionBase::validParams(); 20 4214 : params += MooseParsedFunctionBase::validParams(); 21 25284 : params.addCustomTypeParam<std::string>( 22 : "expression", "FunctionExpression", "The user defined function."); 23 13654 : params.addParam<bool>( 24 6404 : "print_rpn", false, "Print parsed Reverse Polish Notation (RPN) for debugging."); 25 : 26 4214 : params.addClassDescription("Function created by parsing a string"); 27 : 28 4214 : return params; 29 0 : } 30 : 31 3562 : KokkosParsedFunction::KokkosParsedFunction(const InputParameters & parameters) 32 : : FunctionBase(parameters), 33 : MooseParsedFunctionBase(parameters), 34 538 : _expression(verifyFunction(getParam<std::string>("expression"))), 35 538 : _builder(std::make_unique<Moose::Kokkos::RPNBuilder>(_expression, &_console)), 36 2690 : _problem(*parameters.getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")) 37 : { 38 1042 : _builder->addDefaultVariables(); 39 : 40 3126 : for (const auto & [symbol, _] : _math_symbols) 41 2084 : _builder->addVariable(symbol); 42 : 43 1042 : std::vector<std::string> symbol_names; 44 : 45 3126 : if (isParamValid("symbol_names")) 46 51 : symbol_names = getParam<std::vector<std::string>>("symbol_names"); 47 : 48 1076 : for (const auto & name : symbol_names) 49 34 : _builder->addVariable(name); 50 : 51 1042 : _builder->build(); 52 : 53 3126 : if (getParam<bool>("print_rpn")) 54 : { 55 17 : _console << std::endl 56 9 : << "Reverse Polish Notation (RPN) of KokkosParsedFunction " << name() << ":" 57 9 : << std::endl; 58 17 : _builder->printRPN(_console); 59 : } 60 1042 : } 61 : 62 29012 : KokkosParsedFunction::KokkosParsedFunction(const KokkosParsedFunction & function) 63 : : FunctionBase(function), 64 : MooseParsedFunctionBase(function.parameters()), 65 19520 : _expression(function._expression), 66 19520 : _evaluator(function._evaluator), 67 39040 : _problem(function._problem) 68 : { 69 29012 : } 70 : 71 : void 72 1042 : KokkosParsedFunction::initialSetup() 73 : { 74 3126 : for (const auto & [symbol, value] : _math_symbols) 75 2084 : _builder->associateScalar(symbol, &value); 76 : 77 1042 : std::vector<std::string> symbol_names; 78 1042 : std::vector<std::string> symbol_values; 79 : 80 3126 : if (isParamValid("symbol_names")) 81 51 : symbol_names = getParam<std::vector<std::string>>("symbol_names"); 82 3126 : if (isParamValid("symbol_values")) 83 51 : symbol_values = getParam<std::vector<std::string>>("symbol_values"); 84 : 85 1042 : _symbol_values.reserve(symbol_values.size()); 86 1042 : _symbol_functions.reserve(symbol_values.size()); 87 : 88 1076 : for (const auto i : make_range(symbol_names.size())) 89 : { 90 34 : if (hasPostprocessorByName(symbol_values[i])) 91 17 : _builder->associateScalar(symbol_names[i], &getPostprocessorValueByName(symbol_values[i])); 92 17 : else if (_problem.hasKokkosFunction(symbol_values[i])) 93 : { 94 0 : _symbol_functions.push_back(_problem.getKokkosFunction(symbol_values[i])); 95 0 : _builder->associateFunction(symbol_names[i], &_symbol_functions.back()); 96 : } 97 17 : else if (MooseUtils::parsesToReal(symbol_values[i])) 98 : { 99 17 : _symbol_values.push_back(MooseUtils::convert<Real>(symbol_values[i])); 100 17 : _builder->associateScalar(symbol_names[i], &_symbol_values.back()); 101 : } 102 : else 103 0 : paramError("symbol_values", "Symbols cannot be scalar variables for now."); 104 : } 105 : 106 1042 : _builder->finalize(); 107 : 108 : // Should be called after finalization 109 1042 : _evaluator.init(*_builder); 110 1042 : }