LCOV - code coverage report
Current view: top level - src/materials - ParsedMaterialBase.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #31730 (e8b711) with base e0c998 Lines: 59 76 77.6 %
Date: 2025-10-29 16:49:47 Functions: 6 7 85.7 %
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 "ParsedMaterialBase.h"
      11             : #include "ParsedAux.h"
      12             : #include "MooseObject.h"
      13             : 
      14             : InputParameters
      15       64993 : ParsedMaterialBase::validParams()
      16             : {
      17       64993 :   InputParameters params = emptyInputParameters();
      18      259972 :   params.addCoupledVar("coupled_variables", "Vector of variables used in the parsed function");
      19             : 
      20             :   // Constants and their values
      21      194979 :   params.addParam<std::vector<std::string>>(
      22             :       "constant_names",
      23      129986 :       std::vector<std::string>(),
      24             :       "Vector of constants used in the parsed function (use this for kB etc.)");
      25      194979 :   params.addParam<std::vector<std::string>>(
      26             :       "constant_expressions",
      27      129986 :       std::vector<std::string>(),
      28             :       "Vector of values for the constants in constant_names (can be an FParser expression)");
      29             : 
      30             :   // Variables with applied tolerances and their tolerance values
      31      194979 :   params.addParam<std::vector<std::string>>("tol_names",
      32      129986 :                                             std::vector<std::string>(),
      33             :                                             "Vector of variable names to be protected from "
      34             :                                             "being 0 or 1 within a tolerance (needed for log(c) "
      35             :                                             "and log(1-c) terms)");
      36      194979 :   params.addParam<std::vector<Real>>("tol_values",
      37      129986 :                                      std::vector<Real>(),
      38             :                                      "Vector of tolerance values for the variables in tol_names");
      39             : 
      40             :   // Functors and their symbols
      41      259972 :   params.addParam<std::vector<MooseFunctorName>>(
      42             :       "functor_names", {}, "Functors to use in the parsed expression");
      43      259972 :   params.addParam<std::vector<std::string>>(
      44             :       "functor_symbols",
      45             :       {},
      46             :       "Symbolic name to use for each functor in 'functor_names' in the parsed expression. If not "
      47             :       "provided, then the actual functor names will be used in the parsed expression.");
      48             : 
      49             :   // Material properties
      50      194979 :   params.addParam<std::vector<std::string>>(
      51             :       "material_property_names",
      52      129986 :       std::vector<std::string>(),
      53             :       "Vector of material properties used in the parsed function");
      54             : 
      55             :   // Postprocessors
      56      194979 :   params.addParam<std::vector<PostprocessorName>>(
      57             :       "postprocessor_names",
      58      129986 :       std::vector<PostprocessorName>(),
      59             :       "Vector of postprocessor names used in the parsed function");
      60             : 
      61             :   // Function expression
      62      519944 :   params.addDeprecatedCustomTypeParam<std::string>(
      63             :       "function",
      64             :       "FunctionExpression",
      65             :       "Parsed function (see FParser) expression for the parsed material",
      66             :       "'function' is deprecated, use 'expression' instead");
      67             :   // TODO Make required once deprecation is handled, see #19119
      68      324965 :   params.addCustomTypeParam<std::string>(
      69             :       "expression",
      70             :       "FunctionExpression",
      71             :       "Parsed function (see FParser) expression for the parsed material");
      72             : 
      73       64993 :   return params;
      74           0 : }
      75             : 
      76        4233 : ParsedMaterialBase::ParsedMaterialBase(const InputParameters & parameters, const MooseObject * obj)
      77        4233 :   : _derived_object(obj),
      78       12699 :     _function_param(parameters.isParamValid("function") ? "function" : "expression"),
      79        4233 :     _function(parameters.get<std::string>(_function_param))
      80             : {
      81             :   // get constant vectors
      82        4233 :   _constant_names = parameters.get<std::vector<std::string>>("constant_names");
      83        4233 :   _constant_expressions = parameters.get<std::vector<std::string>>("constant_expressions");
      84             : 
      85             :   // get tolerance vectors
      86        4233 :   _tol_names = parameters.get<std::vector<std::string>>("tol_names");
      87        4233 :   _tol_values = parameters.get<std::vector<Real>>("tol_values");
      88             : 
      89             :   // get functor vectors
      90        4233 :   _functor_names = parameters.get<std::vector<MooseFunctorName>>("functor_names");
      91        4233 :   _functor_symbols = parameters.get<std::vector<std::string>>("functor_symbols");
      92             : 
      93             :   // validate all vector names (constants, tolerances, and functors)
      94        4233 :   validateVectorNames();
      95        4233 : }
      96             : 
      97             : void
      98        5847 : ParsedMaterialBase::validateVectorNames(const std::set<std::string> & reserved_names)
      99             : {
     100             :   // helper method to raise an paramError
     101           0 :   auto raiseErr = [this](std::string param_name, std::string msg)
     102             :   {
     103           0 :     if (_derived_object != nullptr)
     104           0 :       _derived_object->paramError(param_name, msg);
     105             :     else
     106           0 :       mooseException(msg);
     107        5847 :   };
     108             : 
     109       11778 :   auto hasDuplicates = [](const std::vector<std::string> & values)
     110             :   {
     111       11778 :     std::set<std::string> s(values.begin(), values.end());
     112       23556 :     return values.size() != s.size();
     113       11778 :   };
     114             : 
     115             :   // helper function to check if the name given is one of the constants
     116          84 :   auto isKnownConstantName = [this](const std::string & name)
     117             :   {
     118             :     return (
     119          84 :         _constant_names.size() &&
     120          84 :         (std::find(_constant_names.begin(), _constant_names.end(), name) != _constant_names.end()));
     121        5847 :   };
     122             : 
     123             :   // helper function to check if the name given is one of the reserved_names
     124          84 :   auto isReservedName = [reserved_names](const std::string & name)
     125        5931 :   { return reserved_names.find(name) != reserved_names.end(); };
     126             : 
     127             :   // check constants
     128        5847 :   if (hasDuplicates(_constant_names))
     129           0 :     raiseErr("constant_names", "In the constants duplicate names are not permitted.");
     130        5847 :   for (const auto & name : _constant_names)
     131             :   {
     132           0 :     if (isReservedName(name))
     133           0 :       raiseErr("constant_names", "In constants, the name '" + name + "' is not permitted.");
     134             :   }
     135             : 
     136             :   // check tolerance vectors
     137        5847 :   if (hasDuplicates(_tol_names))
     138           0 :     raiseErr("tol_names", "In the tolerances duplicate names are not permitted.");
     139             : 
     140             :   // check functor vectors
     141        5847 :   if (_functor_symbols.empty())
     142             :   {
     143        5763 :     if (!_functor_names.empty())
     144           0 :       raiseErr("functor_names", "functor_symbols must be the same length as functor_names.");
     145             :   }
     146             :   else
     147             :   {
     148          84 :     if (_functor_symbols.size() != _functor_names.size())
     149           0 :       raiseErr("functor_names", "functor_symbols must be the same length as functor_names.");
     150          84 :     std::vector<std::string> names;
     151          84 :     if (_functor_symbols.empty())
     152           0 :       std::copy(_functor_names.begin(), _functor_names.end(), std::back_inserter(names));
     153             :     else
     154          84 :       std::copy(_functor_symbols.begin(), _functor_symbols.end(), std::back_inserter(names));
     155          84 :     if (hasDuplicates(names))
     156           0 :       raiseErr(_functor_symbols.empty() ? "functor_names" : "functor_symbols",
     157             :                "In functors, duplicate names are not permitted.");
     158         168 :     for (const auto & name : names)
     159             :     {
     160          84 :       if (isKnownConstantName(name))
     161           0 :         raiseErr(_functor_symbols.empty() ? "functor_names" : "functor_symbols",
     162           0 :                  "In functors, the name '" + name + "' is already in use as a constant.");
     163          84 :       if (isReservedName(name))
     164           0 :         raiseErr(_functor_symbols.empty() ? "functor_names" : "functor_symbols",
     165           0 :                  "In functors, the name '" + name + "' is not permitted.");
     166             :     }
     167          84 :   }
     168        5847 : }

Generated by: LCOV version 1.14