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 : #pragma once 11 : 12 : #include "MooseEnum.h" 13 : #include "FunctionMaterialBase.h" 14 : #include "FunctionParserUtils.h" 15 : #include "FunctionMaterialPropertyDescriptor.h" 16 : #include "DerivativeMaterialPropertyNameInterface.h" 17 : 18 : #include "libmesh/fparser_ad.hh" 19 : 20 : #include <optional> 21 : 22 : #define usingParsedMaterialHelperMembers(T) \ 23 : usingFunctionMaterialBaseMembers(T); \ 24 : usingFunctionParserUtilsMembers(T); \ 25 : using typename ParsedMaterialHelper<T>::VariableNameMappingMode; \ 26 : using typename ParsedMaterialHelper<T>::MatPropDescriptorList; \ 27 : using ParsedMaterialHelper<T>::functionParse; \ 28 : using ParsedMaterialHelper<T>::functionsPostParse; \ 29 : using ParsedMaterialHelper<T>::_func_F; \ 30 : using ParsedMaterialHelper<T>::_symbol_names; \ 31 : using ParsedMaterialHelper<T>::_mat_prop_descriptors; \ 32 : using ParsedMaterialHelper<T>::_tol; \ 33 : using ParsedMaterialHelper<T>::_postprocessor_values; \ 34 : using ParsedMaterialHelper<T>::_extra_symbols; \ 35 : using ParsedMaterialHelper<T>::_functors; \ 36 : using ParsedMaterialHelper<T>::_map_mode 37 : 38 : /** 39 : * Helper class to perform the parsing and optimization of the 40 : * function expression. 41 : */ 42 : template <bool is_ad> 43 : class ParsedMaterialHelper : public FunctionMaterialBase<is_ad>, public FunctionParserUtils<is_ad> 44 : { 45 : public: 46 : typedef DerivativeMaterialPropertyNameInterface::SymbolName SymbolName; 47 : 48 0 : CreateMooseEnumClass(VariableNameMappingMode, USE_MOOSE_NAMES, USE_PARAM_NAMES); 49 230789 : CreateMooseEnumClass(ExtraSymbols, x, y, z, t, dt); 50 : 51 : ParsedMaterialHelper(const InputParameters & parameters, 52 : const VariableNameMappingMode map_mode, 53 : const std::optional<std::string> & function_param_name = {}); 54 : 55 : static InputParameters validParams(); 56 : 57 : /** 58 : * This method sets up and parses the function string given by the user. 59 : * @param function_expression Functional expression to parse. 60 : * Arguments not exposed by this method overload (e.g. post-processors, functors, etc.) are 61 : * assigned as an empty vector. 62 : */ 63 : void functionParse(const std::string & function_expression); 64 : 65 : /** 66 : * This method sets up all variables (e.g. constants) 67 : * to be used in the function and parses the function string given by the user. 68 : * Arguments not exposed by this method overload (e.g. post-processors, functors, tolerances) are 69 : * assigned as an empty vector. 70 : * @param function_expression Functional expression to parse. 71 : * @param constant_names Vector of constant names to use. 72 : * @param constant_expressions Vector of values for the constants in \p constant_names (can be an 73 : * FParser expression). 74 : */ 75 : void functionParse(const std::string & function_expression, 76 : const std::vector<std::string> & constant_names, 77 : const std::vector<std::string> & constant_expressions); 78 : 79 : /** 80 : * This method sets up all variables (e.g. constants, material properties) 81 : * to be used in the function and parses the function string given by the user. 82 : * Arguments not exposed by this method overload (e.g. post-processors, functors) are assigned as 83 : * an empty vector. 84 : * @param function_expression Functional expression to parse. 85 : * @param constant_names Vector of constant names to use. 86 : * @param constant_expressions Vector of values for the constants in \p constant_names (can be an 87 : * FParser expression). 88 : * @param mat_prop_names Vector of material properties used in the parsed function. 89 : * @param tol_names Vector of variable names to be protected from being 0 or 1 within a tolerance 90 : * (needed for log(c) and log(1-c) terms). 91 : * @param tol_values Vector of tolerance values for the variables in \p tol_names . 92 : */ 93 : void functionParse(const std::string & function_expression, 94 : const std::vector<std::string> & constant_names, 95 : const std::vector<std::string> & constant_expressions, 96 : const std::vector<std::string> & mat_prop_names, 97 : const std::vector<std::string> & tol_names, 98 : const std::vector<Real> & tol_values); 99 : 100 : /** 101 : * This method sets up all variables (e.g. constants, material properties, post-processors) 102 : * to be used in the function and parses the function string given by the user. 103 : * Arguments not exposed by this method overload (e.g. functors) are assigned as an empty vector. 104 : * @param function_expression Functional expression to parse. 105 : * @param constant_names Vector of constant names to use. 106 : * @param constant_expressions Vector of values for the constants in \p constant_names (can be an 107 : * FParser expression). 108 : * @param mat_prop_names Vector of material properties used in the parsed function. 109 : * @param postprocessor_names Vector of postprocessor names used in the parsed function. 110 : * @param tol_names Vector of variable names to be protected from being 0 or 1 within a tolerance 111 : * (needed for log(c) and log(1-c) terms). 112 : * @param tol_values Vector of tolerance values for the variables in \p tol_names . 113 : */ 114 : void functionParse(const std::string & function_expression, 115 : const std::vector<std::string> & constant_names, 116 : const std::vector<std::string> & constant_expressions, 117 : const std::vector<std::string> & mat_prop_names, 118 : const std::vector<PostprocessorName> & postprocessor_names, 119 : const std::vector<std::string> & tol_names, 120 : const std::vector<Real> & tol_values); 121 : 122 : /** 123 : * This method sets up all variables (e.g. constants, material properties, post-processors, 124 : * functors) to be used in the function and parses the function string given by the user. 125 : * @param function_expression Functional expression to parse. 126 : * @param constant_names Vector of constant names to use. 127 : * @param constant_expressions Vector of values for the constants in \p constant_names (can be an 128 : * FParser expression). 129 : * @param mat_prop_names Vector of material properties used in the parsed function. 130 : * @param postprocessor_names Vector of postprocessor names used in the parsed function. 131 : * @param tol_names Vector of variable names to be protected from being 0 or 1 within a tolerance 132 : * (needed for log(c) and log(1-c) terms). 133 : * @param tol_values Vector of tolerance values for the variables in \p tol_names . 134 : * @param functor_names vector of constant names to use. 135 : * @param functor_symbols vector of constant names to use. If this vector is empty, \p 136 : * functor_names are used as symbol names. 137 : */ 138 : void functionParse(const std::string & function_expression, 139 : const std::vector<std::string> & constant_names, 140 : const std::vector<std::string> & constant_expressions, 141 : const std::vector<std::string> & mat_prop_names, 142 : const std::vector<PostprocessorName> & postprocessor_names, 143 : const std::vector<std::string> & tol_names, 144 : const std::vector<Real> & tol_values, 145 : const std::vector<MooseFunctorName> & functor_names, 146 : const std::vector<std::string> & functor_symbols); 147 : 148 : protected: 149 : usingFunctionMaterialBaseMembers(is_ad); 150 : usingFunctionParserUtilsMembers(is_ad); 151 : 152 : void initQpStatefulProperties() override; 153 : void computeQpProperties() override; 154 : virtual void initialSetup() override final; 155 : 156 : /** 157 : * Populates the given set with names not to be used as user-defined symbol (e.g. for a functor) 158 : */ 159 : void insertReservedNames(std::set<std::string> & reserved_names); 160 : 161 : // tasks to perform after parsing the primary function 162 : virtual void functionsPostParse(); 163 : 164 : /// The undiffed free energy function parser object. 165 : SymFunctionPtr _func_F; 166 : 167 : /** 168 : * Symbol names used in the expression (depends on the map_mode). 169 : * We distinguish "symbols" i.e. FParser placeholder names from "variables", which 170 : * are MOOSE solution objects 171 : */ 172 : std::vector<SymbolName> _symbol_names; 173 : 174 : /// Extra symbols 175 : const std::vector<ExtraSymbols> _extra_symbols; 176 : 177 : /// Vector of pointers to functors 178 : std::vector<const Moose::Functor<Real> *> _functors; 179 : 180 : /// convenience typedef for the material property descriptors 181 : typedef std::vector<FunctionMaterialPropertyDescriptor<is_ad>> MatPropDescriptorList; 182 : 183 : /// Material property descriptors (obtained by parsing _mat_prop_expressions) 184 : MatPropDescriptorList _mat_prop_descriptors; 185 : 186 : /// Tolerance values for all arguments (to protect from log(0)). 187 : std::vector<Real> _tol; 188 : 189 : /// List of coupled Postprocessors 190 : std::vector<const PostprocessorValue *> _postprocessor_values; 191 : 192 : /** 193 : * Flag to indicate if MOOSE nonlinear variable names should be used as FParser variable names. 194 : * This should be USE_MOOSE_NAMES only for DerivativeParsedMaterial. If set to USE_PARAM_NAMES, 195 : * this class looks up the input parameter name for each coupled variable and uses it as the 196 : * FParser parameter name when parsing the FParser expression. 197 : */ 198 : const VariableNameMappingMode _map_mode; 199 : 200 : /// Optional parameter name that represents the function to associate errors with 201 : const std::optional<std::string> _function_param_name; 202 : 203 : /** 204 : * Vector to hold list of material names that must be updated prior to evaluating current material 205 : * (for compute = false materials) 206 : */ 207 : std::vector<MaterialName> _upstream_mat_names; 208 : 209 : /// This is true by default, but can be disabled to make non-existing properties default to zero 210 : const bool _error_on_missing_material_properties; 211 : 212 : /** 213 : * Vector to hold list of materials that must be updated prior to evaluating current material 214 : * (for compute = false materials) 215 : */ 216 : std::vector<MaterialBase *> _upstream_mat; 217 : 218 : private: 219 : /// Helper for reporting a parse error with a much as context as possible 220 : void parseError(const std::string & message) const; 221 : 222 : /// The underlying parameters 223 : const InputParameters & _params; 224 : };