LCOV - code coverage report
Current view: top level - src/meshgenerators - UniqueExtraIDMeshGenerator.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 49 54 90.7 %
Date: 2025-07-17 01:28:37 Functions: 3 3 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 "UniqueExtraIDMeshGenerator.h"
      11             : #include "MooseMeshUtils.h"
      12             : #include "libmesh/elem.h"
      13             : 
      14             : registerMooseObject("MooseApp", UniqueExtraIDMeshGenerator);
      15             : 
      16             : InputParameters
      17       14345 : UniqueExtraIDMeshGenerator::validParams()
      18             : {
      19       14345 :   InputParameters params = MeshGenerator::validParams();
      20       14345 :   params.addRequiredParam<MeshGeneratorName>(
      21             :       "input", "Name of an existing mesh generator to which we assign extra element IDs");
      22       14345 :   params.addRequiredParam<std::vector<ExtraElementIDName>>(
      23             :       "id_name",
      24             :       "Existing extra integer ID names that is used to generate a new extra integer ID by finding "
      25             :       "unique combinations of their values");
      26       14345 :   params.addRequiredParam<ExtraElementIDName>("new_id_name", "New extra integer ID name");
      27       14345 :   params.addParam<std::vector<unsigned int>>(
      28             :       "new_id_rule",
      29             :       "Vector of unsigned integers to determine new integer ID values by multiplying the provided "
      30             :       "integers to the corresponding existing ID values and then summing the resulting values");
      31       14345 :   params.addParam<std::vector<SubdomainName>>(
      32             :       "restricted_subdomains", "Only set new extra element id for elements in given subdomains");
      33       14345 :   params.addClassDescription("Add a new extra element integer ID by finding unique combinations of "
      34             :                              "the existing extra element integer ID values");
      35       14345 :   return params;
      36           0 : }
      37             : 
      38          40 : UniqueExtraIDMeshGenerator::UniqueExtraIDMeshGenerator(const InputParameters & params)
      39             :   : MeshGenerator(params),
      40          40 :     _input(getMesh("input")),
      41          40 :     _extra_ids(getParam<std::vector<ExtraElementIDName>>("id_name")),
      42          40 :     _use_new_id_rule(isParamValid("new_id_rule")),
      43          80 :     _has_restriction(isParamValid("restricted_subdomains"))
      44             : {
      45          50 :   if (_use_new_id_rule &&
      46          50 :       getParam<std::vector<unsigned int>>("new_id_rule").size() != _extra_ids.size())
      47           0 :     paramError("new_id_rule",
      48             :                "This parameter, if provided, must have a length equal to length of id_name.");
      49          40 : }
      50             : 
      51             : std::unique_ptr<MeshBase>
      52          40 : UniqueExtraIDMeshGenerator::generate()
      53             : {
      54          40 :   std::unique_ptr<MeshBase> mesh = std::move(_input);
      55             : 
      56          40 :   std::set<SubdomainID> restricted_ids;
      57          40 :   if (_has_restriction)
      58             :   {
      59          10 :     auto names = getParam<std::vector<SubdomainName>>("restricted_subdomains");
      60          20 :     for (auto & name : names)
      61             :     {
      62             :       // check that the subdomain exists in the mesh
      63          10 :       if (!MooseMeshUtils::hasSubdomainName(*mesh, name))
      64           0 :         paramError("restricted_subdomains", "The block '", name, "' was not found in the mesh");
      65             : 
      66          10 :       restricted_ids.insert(MooseMeshUtils::getSubdomainID(name, *mesh));
      67             :     }
      68          10 :   }
      69             : 
      70             :   auto parsed_ids =
      71          40 :       MooseMeshUtils::getExtraIDUniqueCombinationMap(*mesh, restricted_ids, _extra_ids);
      72             : 
      73             :   // override the extra ID values from MooseMeshUtils::getExtraIDUniqueCombinationMap by using
      74             :   // new_id_rule
      75          40 :   if (_use_new_id_rule)
      76             :   {
      77          10 :     std::vector<unsigned int> new_id_rule = getParam<std::vector<unsigned int>>("new_id_rule");
      78          10 :     std::vector<unsigned int> existing_extra_id_index;
      79          30 :     for (const auto & id_name : _extra_ids)
      80          20 :       existing_extra_id_index.push_back(mesh->get_elem_integer_index(id_name));
      81         458 :     for (auto & elem : mesh->active_local_element_ptr_range())
      82             :     {
      83         448 :       dof_id_type new_id_value = 0;
      84        1344 :       for (unsigned int i = 0; i < _extra_ids.size(); ++i)
      85         896 :         new_id_value += new_id_rule[i] * elem->get_extra_integer(existing_extra_id_index[i]);
      86         448 :       parsed_ids[elem->id()] = new_id_value;
      87          10 :     }
      88          10 :   }
      89          40 :   auto new_id_name = getParam<ExtraElementIDName>("new_id_name");
      90             :   unsigned int extra_id_index;
      91          40 :   if (!mesh->has_elem_integer(new_id_name))
      92          40 :     extra_id_index = mesh->add_elem_integer(new_id_name);
      93             :   else
      94             :   {
      95           0 :     extra_id_index = mesh->get_elem_integer_index(new_id_name);
      96           0 :     paramWarning(
      97             :         "new_id_name", "An element integer with the name '", new_id_name, "' already exists");
      98             :   }
      99             : 
     100       39720 :   for (auto & elem : mesh->active_element_ptr_range())
     101             :   {
     102       19840 :     if (_has_restriction && restricted_ids.count(elem->subdomain_id()) == 0)
     103         480 :       continue;
     104       19360 :     elem->set_extra_integer(extra_id_index, parsed_ids.at(elem->id()));
     105          40 :   }
     106          80 :   return mesh;
     107          40 : }

Generated by: LCOV version 1.14