LCOV - code coverage report
Current view: top level - src/materials - ParsedMaterialBase.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 58 75 77.3 %
Date: 2026-05-29 20:35:17 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       17753 : ParsedMaterialBase::validParams()
      16             : {
      17       17753 :   InputParameters params = emptyInputParameters();
      18       71012 :   params.addCoupledVar("coupled_variables", "Vector of variables used in the parsed function");
      19             : 
      20             :   // Constants and their values
      21       53259 :   params.addParam<std::vector<std::string>>(
      22             :       "constant_names",
      23       35506 :       std::vector<std::string>(),
      24             :       "Vector of constants used in the parsed function (use this for kB etc.)");
      25       53259 :   params.addParam<std::vector<std::string>>(
      26             :       "constant_expressions",
      27       35506 :       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       53259 :   params.addParam<std::vector<std::string>>("tol_names",
      32       35506 :                                             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       53259 :   params.addParam<std::vector<Real>>("tol_values",
      37       35506 :                                      std::vector<Real>(),
      38             :                                      "Vector of tolerance values for the variables in tol_names");
      39             : 
      40             :   // Functors and their symbols
      41       71012 :   params.addParam<std::vector<MooseFunctorName>>(
      42             :       "functor_names", {}, "Functors to use in the parsed expression");
      43       71012 :   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       53259 :   params.addParam<std::vector<std::string>>(
      51             :       "material_property_names",
      52       35506 :       std::vector<std::string>(),
      53             :       "Vector of material properties used in the parsed function");
      54             : 
      55             :   // Postprocessors
      56       53259 :   params.addParam<std::vector<PostprocessorName>>(
      57             :       "postprocessor_names",
      58       35506 :       std::vector<PostprocessorName>(),
      59             :       "Vector of postprocessor names used in the parsed function");
      60             : 
      61             :   // Function expression
      62      142024 :   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       88765 :   params.addCustomTypeParam<std::string>(
      69             :       "expression",
      70             :       "FunctionExpression",
      71             :       "Parsed function (see FParser) expression for the parsed material");
      72             : 
      73       17753 :   return params;
      74           0 : }
      75             : 
      76        4227 : ParsedMaterialBase::ParsedMaterialBase(const InputParameters & parameters)
      77        8454 :   : _function_param(parameters.isParamValid("function") ? "function" : "expression"),
      78        8454 :     _function(parameters.get<std::string>(_function_param))
      79             : {
      80             :   // get constant vectors
      81        4227 :   _constant_names = parameters.get<std::vector<std::string>>("constant_names");
      82        4227 :   _constant_expressions = parameters.get<std::vector<std::string>>("constant_expressions");
      83             : 
      84             :   // get tolerance vectors
      85        4227 :   _tol_names = parameters.get<std::vector<std::string>>("tol_names");
      86        4227 :   _tol_values = parameters.get<std::vector<Real>>("tol_values");
      87             : 
      88             :   // get functor vectors
      89        4227 :   _functor_names = parameters.get<std::vector<MooseFunctorName>>("functor_names");
      90        4227 :   _functor_symbols = parameters.get<std::vector<std::string>>("functor_symbols");
      91             : 
      92             :   // validate all vector names (constants, tolerances, and functors)
      93        4227 :   validateVectorNames();
      94        4227 : }
      95             : 
      96             : void
      97        5865 : ParsedMaterialBase::validateVectorNames(const std::set<std::string> & reserved_names)
      98             : {
      99             :   // helper method to raise an paramError
     100           0 :   auto raiseErr = [this](std::string param_name, std::string msg)
     101             :   {
     102           0 :     if (auto derived_object = dynamic_cast<MooseObject *>(this))
     103           0 :       derived_object->paramError(param_name, msg);
     104             :     else
     105           0 :       mooseException(msg);
     106        5865 :   };
     107             : 
     108       11808 :   auto hasDuplicates = [](const std::vector<std::string> & values)
     109             :   {
     110       11808 :     std::set<std::string> s(values.begin(), values.end());
     111       23616 :     return values.size() != s.size();
     112       11808 :   };
     113             : 
     114             :   // helper function to check if the name given is one of the constants
     115          78 :   auto isKnownConstantName = [this](const std::string & name)
     116             :   {
     117             :     return (
     118          78 :         _constant_names.size() &&
     119          78 :         (std::find(_constant_names.begin(), _constant_names.end(), name) != _constant_names.end()));
     120        5865 :   };
     121             : 
     122             :   // helper function to check if the name given is one of the reserved_names
     123          78 :   auto isReservedName = [reserved_names](const std::string & name)
     124        5943 :   { return reserved_names.find(name) != reserved_names.end(); };
     125             : 
     126             :   // check constants
     127        5865 :   if (hasDuplicates(_constant_names))
     128           0 :     raiseErr("constant_names", "In the constants duplicate names are not permitted.");
     129        5865 :   for (const auto & name : _constant_names)
     130             :   {
     131           0 :     if (isReservedName(name))
     132           0 :       raiseErr("constant_names", "In constants, the name '" + name + "' is not permitted.");
     133             :   }
     134             : 
     135             :   // check tolerance vectors
     136        5865 :   if (hasDuplicates(_tol_names))
     137           0 :     raiseErr("tol_names", "In the tolerances duplicate names are not permitted.");
     138             : 
     139             :   // check functor vectors
     140        5865 :   if (_functor_symbols.empty())
     141             :   {
     142        5787 :     if (!_functor_names.empty())
     143           0 :       raiseErr("functor_names", "functor_symbols must be the same length as functor_names.");
     144             :   }
     145             :   else
     146             :   {
     147          78 :     if (_functor_symbols.size() != _functor_names.size())
     148           0 :       raiseErr("functor_names", "functor_symbols must be the same length as functor_names.");
     149          78 :     std::vector<std::string> names;
     150          78 :     if (_functor_symbols.empty())
     151           0 :       std::copy(_functor_names.begin(), _functor_names.end(), std::back_inserter(names));
     152             :     else
     153          78 :       std::copy(_functor_symbols.begin(), _functor_symbols.end(), std::back_inserter(names));
     154          78 :     if (hasDuplicates(names))
     155           0 :       raiseErr(_functor_symbols.empty() ? "functor_names" : "functor_symbols",
     156             :                "In functors, duplicate names are not permitted.");
     157         156 :     for (const auto & name : names)
     158             :     {
     159          78 :       if (isKnownConstantName(name))
     160           0 :         raiseErr(_functor_symbols.empty() ? "functor_names" : "functor_symbols",
     161           0 :                  "In functors, the name '" + name + "' is already in use as a constant.");
     162          78 :       if (isReservedName(name))
     163           0 :         raiseErr(_functor_symbols.empty() ? "functor_names" : "functor_symbols",
     164           0 :                  "In functors, the name '" + name + "' is not permitted.");
     165             :     }
     166          78 :   }
     167        5865 : }

Generated by: LCOV version 1.14