LCOV - code coverage report
Current view: top level - src/meshgenerators - ParsedSubdomainGeneratorBase.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 45 46 97.8 %
Date: 2025-07-17 01:28:37 Functions: 4 4 100.0 %
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 "ParsedSubdomainGeneratorBase.h"
      11             : #include "Conversion.h"
      12             : #include "MooseMeshUtils.h"
      13             : #include "CastUniquePointer.h"
      14             : 
      15             : #include "libmesh/fparser_ad.hh"
      16             : #include "libmesh/elem.h"
      17             : 
      18             : InputParameters
      19       32654 : ParsedSubdomainGeneratorBase::validParams()
      20             : {
      21       32654 :   InputParameters params = MeshGenerator::validParams();
      22       32654 :   params += FunctionParserUtils<false>::validParams();
      23             : 
      24       32654 :   params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
      25       32654 :   params.addRequiredParam<std::string>(
      26             :       "expression", "Parsed expression to determine the subdomain id of each involved element");
      27       32654 :   params.addParam<std::vector<SubdomainName>>(
      28             :       "excluded_subdomains",
      29             :       "A set of subdomain names that will not changed even if "
      30             :       "they are inside/outside the combinatorial geometry");
      31       32654 :   params.addDeprecatedParam<std::vector<subdomain_id_type>>(
      32             :       "excluded_subdomain_ids",
      33             :       "A set of subdomain ids that will not changed even if "
      34             :       "they are inside/outside the combinatorial geometry",
      35             :       "excluded_subdomain_ids is deprecated, use excluded_subdomains (ids or names accepted)");
      36       32654 :   params.addParam<std::vector<std::string>>(
      37             :       "constant_names", {}, "Vector of constants used in the parsed function");
      38       32654 :   params.addParam<std::vector<std::string>>(
      39             :       "constant_expressions",
      40             :       {},
      41             :       "Vector of values for the constants in constant_names (can be an FParser expression)");
      42       32654 :   params.addParam<std::vector<ExtraElementIDName>>(
      43             :       "extra_element_id_names", {}, "Extra element integers used in the parsed expression");
      44       32654 :   params.addClassDescription("A base class for mesh generators that Use a parsed expression to "
      45             :                              "assign new subdomain id(s).");
      46             : 
      47       32654 :   return params;
      48           0 : }
      49             : 
      50        2062 : ParsedSubdomainGeneratorBase::ParsedSubdomainGeneratorBase(const InputParameters & parameters)
      51             :   : MeshGenerator(parameters),
      52             :     FunctionParserUtils<false>(parameters),
      53        2062 :     _input(getMesh("input")),
      54        2062 :     _function(parameters.get<std::string>("expression")),
      55        4124 :     _excluded_ids(isParamValid("excluded_subdomain_ids")
      56        2062 :                       ? parameters.get<std::vector<subdomain_id_type>>("excluded_subdomain_ids")
      57             :                       : std::vector<subdomain_id_type>()),
      58        4124 :     _eeid_names(getParam<std::vector<ExtraElementIDName>>("extra_element_id_names"))
      59             : {
      60        2062 :   functionInitialize(_function);
      61        2062 : }
      62             : 
      63             : std::unique_ptr<MeshBase>
      64        2018 : ParsedSubdomainGeneratorBase::generate()
      65             : {
      66        2018 :   std::unique_ptr<MeshBase> mesh = std::move(_input);
      67             : 
      68             :   // the extra element ids would not have existed at construction so we only do this now
      69        2038 :   for (const auto & eeid_name : _eeid_names)
      70          20 :     _eeid_indices.push_back(mesh->get_elem_integer_index(eeid_name));
      71             : 
      72        2018 :   if (isParamValid("excluded_subdomains"))
      73             :   {
      74          14 :     auto excluded_subdomains = parameters().get<std::vector<SubdomainName>>("excluded_subdomains");
      75             : 
      76             :     // check that the subdomains exist in the mesh
      77          24 :     for (const auto & name : excluded_subdomains)
      78          14 :       if (!MooseMeshUtils::hasSubdomainName(*mesh, name))
      79           4 :         paramError("excluded_subdomains", "The block '", name, "' was not found in the mesh");
      80             : 
      81          10 :     _excluded_ids = MooseMeshUtils::getSubdomainIDs(*mesh, excluded_subdomains);
      82          10 :   }
      83             : 
      84             :   // Loop over the elements
      85     1443046 :   for (const auto & elem : mesh->active_element_ptr_range())
      86      722530 :     assignElemSubdomainID(elem);
      87             : 
      88             :   // Assign block name, if applicable
      89        2010 :   setBlockName(mesh);
      90             : 
      91        2010 :   mesh->set_isnt_prepared();
      92        4020 :   return dynamic_pointer_cast<MeshBase>(mesh);
      93        2010 : }
      94             : 
      95             : void
      96        2062 : ParsedSubdomainGeneratorBase::functionInitialize(const std::string & function_expression)
      97             : {
      98             :   // add the extra element integers
      99        2062 :   std::string symbol_str = "x,y,z";
     100        2082 :   for (const auto & eeid_name : _eeid_names)
     101          20 :     symbol_str += "," + eeid_name;
     102             : 
     103             :   // Create parsed function
     104        2062 :   _func_F = std::make_shared<SymFunction>();
     105        2062 :   parsedFunctionSetup(_func_F,
     106             :                       function_expression,
     107             :                       symbol_str,
     108             :                       getParam<std::vector<std::string>>("constant_names"),
     109             :                       getParam<std::vector<std::string>>("constant_expressions"),
     110             :                       comm());
     111             : 
     112        2062 :   _func_params.resize(3 + _eeid_names.size());
     113        2062 : }

Generated by: LCOV version 1.14