LCOV - code coverage report
Current view: top level - src/meshgenerators - SubdomainExtraElementIDGenerator.C (source / functions) Hit Total Coverage
Test: idaholab/moose reactor: #31730 (e8b711) with base e0c998 Lines: 48 52 92.3 %
Date: 2025-10-29 16:54:09 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          88 : SubdomainExtraElementIDGenerator::validParams()
      20             : {
      21          88 :   InputParameters params = MeshGenerator::validParams();
      22         176 :   params.addRequiredParam<MeshGeneratorName>(
      23             :       "input", "Name of an existing mesh generator to which we assign element IDs");
      24         176 :   params.addRequiredParam<std::vector<SubdomainName>>("subdomains",
      25             :                                                       "Subdomain names present in the input mesh");
      26         176 :   params.addRequiredParam<std::vector<std::string>>("extra_element_id_names",
      27             :                                                     "List of user-defined extra element ID names");
      28         176 :   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         176 :   params.addParam<std::vector<dof_id_type>>(
      33             :       "default_extra_element_ids", "Default extra element IDs for elements not in 'subdomains'");
      34             : 
      35          88 :   params.addClassDescription(
      36             :       "Assign extra element IDs for elements on a mesh based on mesh subdomains.");
      37          88 :   return params;
      38           0 : }
      39             : 
      40          48 : SubdomainExtraElementIDGenerator::SubdomainExtraElementIDGenerator(const InputParameters & params)
      41             :   : MeshGenerator(params),
      42          48 :     _input(getMesh("input")),
      43          96 :     _subdomain_names(getParam<std::vector<SubdomainName>>("subdomains")),
      44          96 :     _id_names(getParam<std::vector<std::string>>("extra_element_id_names")),
      45          96 :     _ids(getParam<std::vector<std::vector<dof_id_type>>>("extra_element_ids")),
      46          96 :     _defaults(queryParam<std::vector<dof_id_type>>("default_extra_element_ids"))
      47             : {
      48          48 :   if (_subdomain_names.size() == 0)
      49           2 :     paramError("subdomains", "Empty subdomain vector provided!");
      50          46 :   if (_id_names.size() != _ids.size())
      51           4 :     paramError("extra_element_ids",
      52             :                "Inconsistent vector size for element IDs (must have same size as "
      53             :                "'extra_element_id_names')");
      54         114 :   for (const auto i : index_range(_ids))
      55          72 :     if (_subdomain_names.size() != _ids[i].size())
      56           0 :       paramError("extra_element_ids",
      57           0 :                  "Inconsistent vector size for element IDs at index " + std::to_string(i) +
      58             :                      " (must have same size as 'subdomains')");
      59          42 :   if (_defaults && _defaults->size() != _id_names.size())
      60           2 :     paramError("default_extra_element_ids",
      61             :                "Inconsistent vector size for default element IDs (must have same size as "
      62             :                "'extra_element_id_names')");
      63          40 : }
      64             : 
      65             : std::unique_ptr<MeshBase>
      66          40 : SubdomainExtraElementIDGenerator::generate()
      67             : {
      68          40 :   std::unique_ptr<MeshBase> mesh = std::move(_input);
      69             : 
      70             :   // construct a map from the subdomain ID to the index in 'subdomains'
      71          40 :   const auto subdomain_ids = MooseMeshUtils::getSubdomainIDs(*mesh, _subdomain_names);
      72             : 
      73             :   // check that all subdomains are present
      74         255 :   for (const auto & name : _subdomain_names)
      75         217 :     if (!MooseMeshUtils::hasSubdomainName(*mesh, name))
      76           4 :       paramError("subdomains", "Subdomain " + name + " does not exist in the mesh");
      77             : 
      78             :   // check to make sure no duplicated subdomain ids
      79             :   std::set<SubdomainID> unique_subdomain_ids;
      80         247 :   for (const auto & id : subdomain_ids)
      81             :     if (unique_subdomain_ids.count(id) > 0)
      82           2 :       paramError("subdomains", "Cannot have subdomain with ID ", id, " listed more than once!");
      83             :     else
      84         209 :       unique_subdomain_ids.insert(id);
      85             : 
      86             :   std::map<SubdomainID, unsigned int> subdomains;
      87         243 :   for (const auto i : index_range(_subdomain_names))
      88         207 :     subdomains[subdomain_ids[i]] = i;
      89             : 
      90             :   // get indices for all extra element integers
      91             :   std::vector<unsigned int> extra_id_indices;
      92          90 :   for (const auto & id_name : _id_names)
      93             :   {
      94          54 :     if (!mesh->has_elem_integer(id_name))
      95         108 :       extra_id_indices.push_back(mesh->add_elem_integer(id_name));
      96             :     else
      97           0 :       extra_id_indices.push_back(mesh->get_elem_integer_index(id_name));
      98             :   }
      99             : 
     100       19890 :   for (auto & elem : mesh->element_ptr_range())
     101             :   {
     102        9909 :     if (const auto it = subdomains.find(elem->subdomain_id()); it != subdomains.end())
     103             :     {
     104       20898 :       for (const auto i : index_range(_ids))
     105       11169 :         elem->set_extra_integer(extra_id_indices[i], _ids[i][it->second]);
     106             :     }
     107         180 :     else if (_defaults)
     108             :     {
     109         720 :       for (const auto i : index_range(_ids))
     110         540 :         elem->set_extra_integer(extra_id_indices[i], (*_defaults)[i]);
     111             :     }
     112          36 :   }
     113             : 
     114          36 :   return mesh;
     115          72 : }

Generated by: LCOV version 1.14