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 "ParsedFunctorMaterial.h" 11 : 12 : registerMooseObject("MooseApp", ParsedFunctorMaterial); 13 : registerMooseObject("MooseApp", ADParsedFunctorMaterial); 14 : 15 : template <bool is_ad> 16 : InputParameters 17 29286 : ParsedFunctorMaterialTempl<is_ad>::validParams() 18 : { 19 29286 : InputParameters params = FunctorMaterial::validParams(); 20 29286 : params += FunctionParserUtils<is_ad>::validParams(); 21 29286 : params.addClassDescription( 22 : "Computes a functor material from a parsed expression of other functors."); 23 29286 : params.addRequiredCustomTypeParam<std::string>( 24 : "expression", "FunctionExpression", "Expression to parse for the new functor material"); 25 29286 : params.addParam<std::vector<std::string>>( 26 : "functor_names", {}, "Functors to use in the parsed expression"); 27 29286 : params.addParam<std::vector<std::string>>( 28 : "functor_symbols", 29 : {}, 30 : "Symbolic name to use for each functor in 'functor_names' in the parsed expression. If not " 31 : "provided, then the actual functor names must be used in the parsed expression."); 32 29286 : params.addRequiredParam<std::string>("property_name", 33 : "Name to give the new functor material property"); 34 : 35 29286 : return params; 36 0 : } 37 : 38 : template <bool is_ad> 39 395 : ParsedFunctorMaterialTempl<is_ad>::ParsedFunctorMaterialTempl(const InputParameters & parameters) 40 : : FunctorMaterial(parameters), 41 : FunctionParserUtils<is_ad>(parameters), 42 395 : _expression(getParam<std::string>("expression")), 43 395 : _functor_names(getParam<std::vector<std::string>>("functor_names")), 44 395 : _n_functors(_functor_names.size()), 45 395 : _functor_symbols(getParam<std::vector<std::string>>("functor_symbols")), 46 790 : _property_name(getParam<std::string>("property_name")) 47 : { 48 : // Check/modify 'functor_symbols' 49 395 : if (_functor_symbols.size() != _n_functors) 50 : { 51 28 : if (_functor_symbols.size() == 0) 52 28 : _functor_symbols = _functor_names; 53 : else 54 0 : paramError("functor_symbols", 55 : "The number of entries must be equal to either zero or the number of entries in " 56 : "'functor_names'."); 57 : } 58 : 59 : // Get the functors 60 395 : _functors.resize(_n_functors); 61 1613 : for (const auto i : index_range(_functor_names)) 62 1218 : _functors[i] = &getFunctor<GenericReal<is_ad>>(_functor_names[i]); 63 : 64 : // Build the parsed function 65 395 : buildParsedFunction(); 66 : 67 : // Add the functor material property 68 395 : const std::set<ExecFlagType> clearance_schedule(_execute_enum.begin(), _execute_enum.end()); 69 395 : addFunctorProperty<GenericReal<is_ad>>( 70 : _property_name, 71 2150124 : [this](const auto & r, const auto & t) -> GenericReal<is_ad> 72 : { 73 : // Store the functor values 74 726607 : for (const auto i : index_range(_functors)) 75 587225 : _func_params[i] = (*_functors[i])(r, t); 76 : 77 : // Store the space and time values 78 139382 : const auto r_point = r.getPoint(); 79 557528 : for (const auto i : make_range(Moose::dim)) 80 418146 : _func_params[_n_functors + i] = r_point(i); 81 139382 : _func_params[_n_functors + Moose::dim] = _fe_problem.getTimeFromStateArg(t); 82 : 83 : // Evaluate the parsed function 84 278764 : return evaluate(_parsed_function, _name); 85 : }, 86 : clearance_schedule); 87 395 : } 88 : 89 : template <bool is_ad> 90 : void 91 395 : ParsedFunctorMaterialTempl<is_ad>::buildParsedFunction() 92 : { 93 395 : _parsed_function = std::make_shared<SymFunction>(); 94 : 95 : // Collect the symbols corresponding to the _func_params values 96 395 : std::vector<std::string> symbols(_functor_symbols); 97 395 : std::string symbols_str = Moose::stringify(symbols); 98 : if (Moose::dim == 3) 99 395 : symbols_str += symbols_str.empty() ? "x,y,z" : ",x,y,z"; 100 : else 101 : mooseError("ParsedFunctorMaterial assumes the dimension is always equal to 3."); 102 395 : symbols_str += symbols_str.empty() ? "t" : ",t"; 103 : 104 : // Create parsed function 105 3160 : this->parsedFunctionSetup(_parsed_function, 106 : _expression, 107 : symbols_str, 108 : {"pi", "e"}, 109 : {std::to_string(libMesh::pi), std::to_string(std::exp(Real(1)))}, 110 : comm()); 111 : 112 : // Resize the values vector 113 395 : _func_params.resize(_n_functors + Moose::dim + 1); 114 1580 : } 115 : 116 : template class ParsedFunctorMaterialTempl<false>; 117 : template class ParsedFunctorMaterialTempl<true>;