16#include "libmesh/unstructured_mesh.h"
21 StitchedMeshGenerator,
30 params.
addRequiredParam<std::vector<MeshGeneratorName>>(
"inputs",
"The input MeshGenerators.");
31 params.
addParam<
bool>(
"prevent_boundary_ids_overlap",
33 "Whether to re-number boundaries in stitched meshes to prevent merging of "
34 "unrelated boundaries");
36 "merge_boundaries_with_same_name",
38 "If the input meshes have boundaries with the same name (but different IDs), merge them");
40 "subdomain_remapping",
42 "Treat input subdomain names as primary, preserving them and remapping IDs as needed");
44 "verbose_remapping",
false,
"Whether mesh subdomain remapping should have verbose output.");
46 "verbose_stitching",
false,
"Whether mesh stitching should have verbose output.");
48 "enforce_all_nodes_match_on_boundaries",
50 "Whether to have the stitcher be very picky about the nodes being stitched.");
51 params.
addParam<
bool>(
"merge_boundary_nodes_all_or_nothing",
53 "Whether the stitcher is set to merge all nodes or none.");
55 "Allows multiple mesh files to be stitched together to form a single mesh.");
64 _mesh_ptrs(getMeshes(
"inputs")),
65 _input_names(getParam<
std::vector<MeshGeneratorName>>(
"inputs")),
66 _prevent_boundary_ids_overlap(getParam<bool>(
"prevent_boundary_ids_overlap")),
67 _merge_boundaries_with_same_name(getParam<bool>(
"merge_boundaries_with_same_name"))
72 "Can only stitch one pair of boundary per pair of mesh. We have '" +
74 "' meshes specified (=" + std::to_string(
_input_names.size() - 1) +
76 " pairs of boundaries specified");
81 "' is not of length 2, but of length: " + std::to_string(pair.size()));
84std::unique_ptr<MeshBase>
88 const auto verbose_remapping = getParam<bool>(
"verbose_remapping");
91 std::unique_ptr<UnstructuredMesh>
mesh = dynamic_pointer_cast<UnstructuredMesh>(*
_mesh_ptrs[0]);
93 mooseError(
"StitchMeshGenerator is only implemented for unstructured meshes");
96 std::vector<std::unique_ptr<UnstructuredMesh>> meshes(
_mesh_ptrs.size() - 1);
100 meshes[i - 1] = dynamic_pointer_cast<UnstructuredMesh>(*
_mesh_ptrs[i]);
103 for (MooseIndex(meshes) i = 0; i < meshes.size(); i++)
111 ((i == 0) ?
"" : (
" (stitched with " + std::to_string(i) +
" previous meshes)")),
115 const bool use_binary_search = (
_algorithm ==
"BINARY");
118 if (!
mesh->is_prepared())
119 mesh->prepare_for_use();
120 if (!meshes[i]->is_prepared())
121 meshes[i]->prepare_for_use();
128 const auto & base_mesh_bids =
mesh->get_boundary_info().get_global_boundary_ids();
129 const auto stitched_mesh_bids = meshes[i]->get_boundary_info().get_global_boundary_ids();
132 bool overlap_found =
false;
133 for (
const auto & bid : stitched_mesh_bids)
134 if (base_mesh_bids.count(bid))
135 overlap_found =
true;
139 const auto max_boundary_id =
140 std::max(*base_mesh_bids.rbegin(), *stitched_mesh_bids.rbegin());
142 for (
const auto bid : stitched_mesh_bids)
144 const auto new_bid = max_boundary_id + (new_index++);
145 meshes[i]->get_boundary_info().renumber_id(bid, new_bid);
157 const auto & base_mesh_bids =
mesh->get_boundary_info().get_global_boundary_ids();
158 const auto & other_mesh_bids = meshes[i]->get_boundary_info().get_global_boundary_ids();
161 std::set<boundary_id_type> bd_id_intersection;
162 std::set_intersection(base_mesh_bids.begin(),
163 base_mesh_bids.end(),
164 other_mesh_bids.begin(),
165 other_mesh_bids.end(),
166 std::inserter(bd_id_intersection, bd_id_intersection.begin()));
168 for (
const auto bid : bd_id_intersection)
170 const auto & sideset_name_on_first_mesh =
mesh->get_boundary_info().get_sideset_name(bid);
171 const auto & sideset_name_on_second_mesh =
172 meshes[i]->get_boundary_info().get_sideset_name(bid);
174 if (sideset_name_on_first_mesh != sideset_name_on_second_mesh)
178 " corresponds to different boundary names on the input meshes! On the first "
179 "mesh it corresponds to `",
180 sideset_name_on_first_mesh,
181 "` while on the second mesh it corresponds to `",
182 sideset_name_on_second_mesh,
183 "`. The final mesh will replace boundary `",
184 sideset_name_on_second_mesh,
186 sideset_name_on_first_mesh,
187 "`. To avoid this situation, use the `prevent_boundary_ids_overlap` parameter!");
194 if (getParam<bool>(
"subdomain_remapping"))
203 std::unordered_map<subdomain_id_type, subdomain_id_type> id_remapping;
204 for (
const auto & [bl_name, bl_id] : secondary_subdomains)
206 subdomain_id_type new_id = bl_id;
212 std::as_const(*mesh).subdomain_name(bl_id) != bl_name)
214 new_id = next_free_id++;
216 mesh->set_subdomain_name(new_id, bl_name,
true);
221 if (verbose_remapping)
223 << bl_name <<
" (" << bl_id <<
") to ID " << new_id << std::endl;
224 id_remapping[bl_id] = new_id;
228 if (!id_remapping.empty())
230 for (
auto *
const elem : meshes[i]->element_ptr_range())
231 if (
const auto it = id_remapping.find(elem->subdomain_id()); it != id_remapping.end())
232 elem->subdomain_id() = it->second;
234 auto & secondary_name_map = meshes[i]->set_subdomain_name_map();
235 for (
const auto & [old_id, _] : id_remapping)
236 secondary_name_map.erase(old_id);
237 meshes[i]->unset_has_synched_subdomain_name_map();
241 mesh->stitch_meshes(*meshes[i],
244 getParam<Real>(
"stitching_hmin_tolerance_factor"),
246 getParam<bool>(
"verbose_stitching"),
249 getParam<bool>(
"enforce_all_nodes_match_on_boundaries"),
251 getParam<bool>(
"merge_boundary_nodes_all_or_nothing"),
256 if (getParam<bool>(
"subdomain_remapping"))
257 for (
const auto & [block_id, block_name] : meshes[i]->get_subdomain_name_map())
258 mesh->set_subdomain_name_map().insert(std::make_pair(block_id, block_name));
264 mesh->unset_is_prepared();
265 return dynamic_pointer_cast<MeshBase>(
mesh);
boundary_id_type BoundaryID
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
registerMooseObject("MooseApp", StitchMeshGenerator)
registerMooseObjectRenamed("MooseApp", StitchedMeshGenerator, "06/30/2026 24:00", StitchMeshGenerator)
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
const std::string & name() const
Get the name of the class.
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 ...
A base class for mesh generators that stitch boundaries together.
static InputParameters validParams()
boundary_id_type getBoundaryIdToStitch(const MeshBase &mesh, const std::string &input_mg_name, const BoundaryName &bname) const
Get the boundary id from the name of a boundary to stitch.
const bool _clear_stitched_boundary_ids
Whether or not to clear (remove) the stitched boundary IDs.
const std::vector< std::vector< std::string > > _stitch_boundaries_pairs
A transformed version of _stitch_boundaries into a more logical "pairwise" structure.
const MooseEnum _algorithm
Type of algorithm used to find matching nodes (binary or exhaustive)
Allows multiple mesh files to be "stitched" together to form a single mesh.
std::vector< std::unique_ptr< MeshBase > * > _mesh_ptrs
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
StitchMeshGenerator(const InputParameters ¶meters)
static InputParameters validParams()
const bool _merge_boundaries_with_same_name
Whether to merge boundaries if they have the same name but different boundary IDs.
const bool _prevent_boundary_ids_overlap
Whether to renumber all boundaries in stitched meshes to prevent accidental merging of sidesets with ...
const std::vector< MeshGeneratorName > & _input_names
The mesh generator inputs to read.
bool hasSubdomainName(const MeshBase &input_mesh, const SubdomainName &name)
Whether a particular subdomain name exists in the mesh.
std::set< std::pair< SubdomainName, SubdomainID > > getAllSubdomainNamesAndIDs(const libMesh::MeshBase &mesh)
Get all subdomain names and ids from a mesh.
bool hasSubdomainID(const MeshBase &input_mesh, const SubdomainID &id)
Whether a particular subdomain ID exists in the mesh.
void mergeBoundaryIDsWithSameName(MeshBase &mesh)
Merges the boundary IDs of boundaries that have the same names but different IDs.
SubdomainID getNextFreeSubdomainID(MeshBase &input_mesh)
Checks input mesh and returns max(block ID) + 1, which represents a block ID that is not currently in...
SubdomainID getSubdomainID(const SubdomainName &subdomain_name, const MeshBase &mesh)
Gets the subdomain ID associated with the given SubdomainName.
std::string stringify(const T &t)
conversion to string