LCOV - code coverage report
Current view: top level - src/functormaterials - ParsedFunctorMaterial.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 42 44 95.5 %
Date: 2025-07-17 01:28:37 Functions: 13 24 54.2 %
Legend: Lines: hit not hit

          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       28936 : ParsedFunctorMaterialTempl<is_ad>::validParams()
      18             : {
      19       28936 :   InputParameters params = FunctorMaterial::validParams();
      20       28936 :   params += FunctionParserUtils<is_ad>::validParams();
      21       28936 :   params.addClassDescription(
      22             :       "Computes a functor material from a parsed expression of other functors.");
      23       28936 :   params.addRequiredCustomTypeParam<std::string>(
      24             :       "expression", "FunctionExpression", "Expression to parse for the new functor material");
      25       28936 :   params.addParam<std::vector<std::string>>(
      26             :       "functor_names", {}, "Functors to use in the parsed expression");
      27       28936 :   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       28936 :   params.addRequiredParam<std::string>("property_name",
      33             :                                        "Name to give the new functor material property");
      34             : 
      35       28936 :   return params;
      36           0 : }
      37             : 
      38             : template <bool is_ad>
      39         212 : ParsedFunctorMaterialTempl<is_ad>::ParsedFunctorMaterialTempl(const InputParameters & parameters)
      40             :   : FunctorMaterial(parameters),
      41             :     FunctionParserUtils<is_ad>(parameters),
      42         212 :     _expression(getParam<std::string>("expression")),
      43         212 :     _functor_names(getParam<std::vector<std::string>>("functor_names")),
      44         212 :     _n_functors(_functor_names.size()),
      45         212 :     _functor_symbols(getParam<std::vector<std::string>>("functor_symbols")),
      46         424 :     _property_name(getParam<std::string>("property_name"))
      47             : {
      48             :   // Check/modify 'functor_symbols'
      49         212 :   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         212 :   _functors.resize(_n_functors);
      61         813 :   for (const auto i : index_range(_functor_names))
      62         601 :     _functors[i] = &getFunctor<GenericReal<is_ad>>(_functor_names[i]);
      63             : 
      64             :   // Build the parsed function
      65         212 :   buildParsedFunction();
      66             : 
      67             :   // Add the functor material property
      68         212 :   const std::set<ExecFlagType> clearance_schedule(_execute_enum.begin(), _execute_enum.end());
      69         212 :   addFunctorProperty<GenericReal<is_ad>>(
      70             :       _property_name,
      71     1638102 :       [this](const auto & r, const auto & t) -> GenericReal<is_ad>
      72             :       {
      73             :         // Store the functor values
      74      552396 :         for (const auto i : index_range(_functors))
      75      445734 :           _func_params[i] = (*_functors[i])(r, t);
      76             : 
      77             :         // Store the space and time values
      78      106662 :         const auto r_point = r.getPoint();
      79      426648 :         for (const auto i : make_range(Moose::dim))
      80      319986 :           _func_params[_n_functors + i] = r_point(i);
      81      106662 :         _func_params[_n_functors + Moose::dim] = _fe_problem.getTimeFromStateArg(t);
      82             : 
      83             :         // Evaluate the parsed function
      84      213324 :         return evaluate(_parsed_function, _name);
      85             :       },
      86             :       clearance_schedule);
      87         212 : }
      88             : 
      89             : template <bool is_ad>
      90             : void
      91         212 : ParsedFunctorMaterialTempl<is_ad>::buildParsedFunction()
      92             : {
      93         212 :   _parsed_function = std::make_shared<SymFunction>();
      94             : 
      95             :   // Collect the symbols corresponding to the _func_params values
      96         212 :   std::vector<std::string> symbols(_functor_symbols);
      97         212 :   std::string symbols_str = Moose::stringify(symbols);
      98             :   if (Moose::dim == 3)
      99         212 :     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         212 :   symbols_str += symbols_str.empty() ? "t" : ",t";
     103             : 
     104             :   // Create parsed function
     105        1696 :   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         212 :   _func_params.resize(_n_functors + Moose::dim + 1);
     114         848 : }
     115             : 
     116             : template class ParsedFunctorMaterialTempl<false>;
     117             : template class ParsedFunctorMaterialTempl<true>;

Generated by: LCOV version 1.14