LCOV - code coverage report
Current view: top level - src/meshgenerators - SubdomainExtraElementIDGenerator.C (source / functions) Hit Total Coverage
Test: idaholab/moose reactor: #31405 (292dce) with base fef103 Lines: 51 54 94.4 %
Date: 2025-09-04 07:56:24 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 "SubdomainExtraElementIDGenerator.h"
      11             : 
      12             : registerMooseObject("ReactorApp", SubdomainExtraElementIDGenerator);
      13             : 
      14             : #include "MooseMeshUtils.h"
      15             : 
      16             : #include "libmesh/elem.h"
      17             : 
      18             : InputParameters
      19          94 : SubdomainExtraElementIDGenerator::validParams()
      20             : {
      21          94 :   InputParameters params = MeshGenerator::validParams();
      22         188 :   params.addRequiredParam<MeshGeneratorName>(
      23             :       "input", "Name of an existing mesh generator to which we assign element IDs");
      24         188 :   params.addRequiredParam<std::vector<SubdomainName>>("subdomains",
      25             :                                                       "Subdomain names present in the input mesh");
      26         188 :   params.addRequiredParam<std::vector<std::string>>("extra_element_id_names",
      27             :                                                     "List of user-defined extra element ID names");
      28         188 :   params.addRequiredParam<std::vector<std::vector<dof_id_type>>>(
      29             :       "extra_element_ids",
      30             :       "User-defined extra element IDs corresponding to 'subdomains' in the same order");
      31             : 
      32         188 :   params.addParam<std::vector<dof_id_type>>(
      33             :       "default_extra_element_ids", "Default extra element IDs for elements not in 'subdomains'");
      34             : 
      35          94 :   params.addClassDescription(
      36             :       "Assign extra element IDs for elements on a mesh based on mesh subdomains.");
      37          94 :   return params;
      38           0 : }
      39             : 
      40          48 : SubdomainExtraElementIDGenerator::SubdomainExtraElementIDGenerator(const InputParameters & params)
      41             :   : MeshGenerator(params),
      42          48 :     _input(getMesh("input")),
      43         144 :     _subdomain_names(getParam<std::vector<SubdomainName>>("subdomains"))
      44             : {
      45          48 :   if (_subdomain_names.size() == 0)
      46           2 :     paramError("subdomains", "Empty subdomain vector provided!");
      47          46 : }
      48             : 
      49             : std::unique_ptr<MeshBase>
      50          46 : SubdomainExtraElementIDGenerator::generate()
      51             : {
      52          46 :   std::unique_ptr<MeshBase> mesh = std::move(_input);
      53             : 
      54             :   // construct a map from the subdomain ID to the index in 'subdomains'
      55          46 :   auto subdomain_ids = MooseMeshUtils::getSubdomainIDs(*mesh, _subdomain_names);
      56             : 
      57             :   // check that all subdomains are present
      58         281 :   for (const auto & name : _subdomain_names)
      59         237 :     if (!MooseMeshUtils::hasSubdomainName(*mesh, name))
      60           4 :       paramError("subdomains", "Subdomain " + name + " does not exist in the mesh");
      61             : 
      62             :   // check to make sure no duplicated subdomain ids
      63             :   std::set<SubdomainID> unique_subdomain_ids;
      64         277 :   for (const auto & id : subdomain_ids)
      65             :     if (unique_subdomain_ids.count(id) > 0)
      66           2 :       paramError("subdomains", "Cannot have subdomain with ID ", id, " listed more than once!");
      67             :     else
      68         233 :       unique_subdomain_ids.insert(id);
      69             : 
      70             :   std::map<SubdomainID, unsigned int> subdomains;
      71         273 :   for (unsigned int i = 0; i < _subdomain_names.size(); ++i)
      72         231 :     subdomains[subdomain_ids[i]] = i;
      73             : 
      74          42 :   auto & element_id_names = getParam<std::vector<std::string>>("extra_element_id_names");
      75          84 :   auto & element_ids = getParam<std::vector<std::vector<dof_id_type>>>("extra_element_ids");
      76             : 
      77          42 :   if (element_id_names.size() != element_ids.size())
      78           4 :     paramError("extra_element_ids", "Inconsistent vector size for element IDs");
      79          98 :   for (auto & element_id : element_ids)
      80             :   {
      81          60 :     if (_subdomain_names.size() != element_id.size())
      82           0 :       paramError("extra_element_ids", "Inconsistent vector size for element IDs");
      83             :   }
      84             : 
      85             :   // get indices for all extra element integers
      86             :   std::vector<unsigned int> extra_id_indices;
      87          98 :   for (auto & element_id_name : element_id_names)
      88             :   {
      89          60 :     if (!mesh->has_elem_integer(element_id_name))
      90         120 :       extra_id_indices.push_back(mesh->add_elem_integer(element_id_name));
      91             :     else
      92           0 :       extra_id_indices.push_back(mesh->get_elem_integer_index(element_id_name));
      93             :   }
      94             : 
      95          76 :   if (isParamValid("default_extra_element_ids"))
      96             :   {
      97          22 :     auto & default_ids = getParam<std::vector<dof_id_type>>("default_extra_element_ids");
      98          11 :     if (default_ids.size() != element_id_names.size())
      99           2 :       paramError("default_extra_element_ids", "Inconsistent vector size for default element IDs");
     100             : 
     101        1818 :     for (auto & elem : mesh->element_ptr_range())
     102        3600 :       for (unsigned int i = 0; i < element_ids.size(); ++i)
     103        2709 :         elem->set_extra_integer(extra_id_indices[i], default_ids[i]);
     104             :   }
     105             : 
     106       19890 :   for (auto & elem : mesh->element_ptr_range())
     107             :   {
     108        9909 :     SubdomainID id = elem->subdomain_id();
     109             :     auto it = subdomains.find(id);
     110        9909 :     if (it == subdomains.end())
     111         180 :       continue;
     112             : 
     113       20898 :     for (unsigned int i = 0; i < element_ids.size(); ++i)
     114       11169 :       elem->set_extra_integer(extra_id_indices[i], element_ids[i][it->second]);
     115          36 :   }
     116             : 
     117          36 :   return mesh;
     118          72 : }

Generated by: LCOV version 1.14