21 params.
addRequiredParam<MeshGeneratorName>(
"input",
"The mesh we want to modify");
22 params.
addParam<std::vector<SubdomainName>>(
24 "Elements and nodes within these blocks will be renumbered. If none are specified, all " 25 "subdomains are affected by the renumbering");
27 params.
addClassDescription(
"Changes the element and node IDs so that elements and nodes are " 28 "contiguous within a subdomain. Note that DoF ordering may be " 29 "affected as well, and that the mesh renumbering will be turned off.");
39 std::unique_ptr<MeshBase>
42 std::unique_ptr<MeshBase>
mesh = std::move(
_input);
45 if (!
mesh->is_serial())
46 mooseError(
"Not implemented for non-serialized distributed meshes");
50 mesh->remove_orphaned_nodes();
51 mesh->renumber_nodes_and_elements();
54 std::optional<std::vector<SubdomainName>>
blocks =
56 ? getParam<std::vector<SubdomainName>>(
"blocks_to_renumber")
57 : std::vector<SubdomainName>();
58 std::vector<SubdomainID> block_ids(
blocks->size());
59 std::stringstream missing_block;
63 const SubdomainName &
name =
blocks.value()[i];
71 missing_block <<
name <<
" ";
73 if (missing_block.str().size())
75 "The following blocks were requested to be renumbered, but do not exist: ",
81 std::set<subdomain_id_type> block_ids_set;
82 mesh->subdomain_ids(block_ids_set);
83 block_ids.reserve(block_ids_set.size());
84 block_ids.assign(block_ids_set.begin(), block_ids_set.end());
91 const auto max_elem_id =
mesh->max_elem_id();
92 const auto max_node_id =
mesh->max_node_id();
93 std::unordered_map<dof_id_type, dof_id_type> new_elem_ids;
94 new_elem_ids.reserve(max_elem_id);
95 std::unordered_set<dof_id_type> renumbered_nodes;
96 renumbered_nodes.reserve(max_node_id);
104 for (
auto elem :
mesh->active_subdomain_elements_ptr_range(block_ids[i]))
108 while (
mesh->query_elem_ptr(elem_count))
113 new_elem_ids[elem->id()] = elem_count++;
115 for (
auto & node : elem->node_ref_range())
117 if (!renumbered_nodes.count(node.id()))
121 while (
mesh->query_node_ptr(node_count))
125 renumbered_nodes.insert(node.id());
126 mesh->renumber_node(node.id(), node_count++);
132 for (
const auto [key,
value] : new_elem_ids)
139 mesh->allow_renumbering(
false);
141 if (
mesh->n_nodes() !=
mesh->max_node_id() ||
mesh->n_elem() !=
mesh->max_elem_id())
142 mooseWarning(
"Mesh is not contiguously numbered after renumbering. The numbering may be erased " 143 "by outputs that require contiguous numbering such as Exodus.\nNumber of nodes: " +
144 std::to_string(
mesh->n_nodes()) +
145 "\nMax node ID: " + std::to_string(
mesh->max_node_id() - 1) +
146 "\nNumber of elements: " + std::to_string(
mesh->n_elem()) +
147 "\nMax elem ID: " + std::to_string(
mesh->max_elem_id() - 1));
149 mesh->unset_is_prepared();
void paramError(const std::string ¶m, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
std::unique_ptr< T_DEST, T_DELETER > dynamic_pointer_cast(std::unique_ptr< T_SRC, T_DELETER > &src)
These are reworked from https://stackoverflow.com/a/11003103.
SubdomainID getSubdomainID(const SubdomainName &subdomain_name, const MeshBase &mesh)
Gets the subdomain ID associated with the given SubdomainName.
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
const SubdomainID INVALID_BLOCK_ID
void mooseWarning(Args &&... args) const
const std::string & name() const
Get the name of the class.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
registerMooseObject("MooseApp", RenumberBySubdomainGenerator)
MeshGenerator for re-numbering elements and nodes contiguously within subdomains. ...
static InputParameters validParams()
std::unique_ptr< MeshBase > & _input
static InputParameters validParams()
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
RenumberBySubdomainGenerator(const InputParameters ¶meters)
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
MeshGenerators are objects that can modify or add to an existing mesh.
auto index_range(const T &sizable)