166#ifdef LIBMESH_HAVE_NETGEN
168 std::unique_ptr<UnstructuredMesh>
mesh =
169 dynamic_pointer_cast<UnstructuredMesh>(std::move(
_bdy_ptr));
174 mesh->get_boundary_info().clear_boundary_node_ids();
183 const bool second_order_output = (tet_type !=
libMesh::TET4);
198 for (
const auto hole_i : index_range(
_hole_ptrs))
200 UnstructuredMesh & hole_mesh =
dynamic_cast<UnstructuredMesh &
>(**
_hole_ptrs[hole_i]);
204 std::set<ElemType> hole_elem_types;
205 std::set<unsigned short> hole_elem_dims;
206 std::vector<std::pair<dof_id_type, unsigned int>> hole_elem_external_sides;
210 bool hole_has_midpoints =
false;
211 for (
auto elem : hole_mesh.element_ptr_range())
213 hole_elem_dims.emplace(elem->dim());
215 hole_has_midpoints =
true;
219 if (elem->dim() == 3)
220 for (
auto s : make_range(elem->n_sides()))
223 if (!elem->neighbor_ptr(s))
225 hole_elem_types.emplace(elem->side_ptr(s)->type());
226 hole_elem_external_sides.emplace_back(elem->id(), s);
231 hole_elem_types.emplace(elem->type());
233 if (hole_elem_dims.size() != 1 || *hole_elem_dims.begin() < 2)
236 "All elements in a hole mesh must have the same dimension that is either 2D or 3D.");
237 else if (*hole_elem_dims.begin() == 3)
242 if (!second_order_output && stitching_this_hole && hole_has_midpoints)
245 "Cannot use first order elements with stitched quadratic element holes. Please "
246 "specify a higher-order tet_element_type or reduce the order of the hole inputs.");
254 if (*hole_elem_types.begin() != ElemType::TRI3 || hole_elem_types.size() > 1)
258 "3D hole meshes with non-TRI3 surface elements cannot be stitched without "
259 "converting them to TET4. Consider setting convert_holes_for_stitching=true.");
263 std::set<subdomain_id_type> hole_mesh_original_subdomain_ids;
264 for (
auto elem_it = hole_mesh.active_elements_begin();
265 elem_it != hole_mesh.active_elements_end();
267 hole_mesh_original_subdomain_ids.emplace((*elem_it)->subdomain_id());
271 for (
const auto & hees : hole_elem_external_sides)
272 hole_mesh.get_boundary_info().add_side(hees.first, hees.second, temp_ext_bid);
274 hole_mesh, std::vector<BoundaryName>({std::to_string(temp_ext_bid)}), 1,
false);
275 hole_mesh.get_boundary_info().remove_id(temp_ext_bid);
282 hole_mesh_original_subdomain_ids,
283 *hole_mesh_original_subdomain_ids.rbegin() + 1,
284 getParam<SubdomainName>(
"converted_tet_element_subdomain_name_suffix"),
285 getParam<SubdomainName>(
"converted_pyramid_element_subdomain_name_suffix"));
287 catch (
const std::exception & e)
289 if (((std::string)e.what()).compare(26, 4,
"TET4") == 0)
290 paramError(
"converted_tet_element_subdomain_name_suffix", e.what());
292 paramError(
"converted_pyramid_element_subdomain_name_suffix", e.what());
299 hole_mesh.prepare_for_use();
306 if (!hole_mesh.is_prepared())
307 hole_mesh.cache_elem_data();
308 MeshTools::Modification::all_tri(**
_hole_ptrs[hole_i]);
316 if (second_order_output && stitching_this_hole)
318 if (!hole_mesh.is_prepared())
319 hole_mesh.prepare_for_use();
322 for (
const auto elem : hole_mesh.element_ptr_range())
324 cur_type = elem->type();
328 hole_mesh.all_second_order();
330 hole_mesh.all_complete_order();
338 "the hole mesh with index " + std::to_string(hole_i) +
339 " is a 2D mesh, for which stitching onto a 3D mesh does not make sense.");
343 std::unique_ptr<std::vector<std::unique_ptr<UnstructuredMesh>>> ngholes =
344 std::make_unique<std::vector<std::unique_ptr<UnstructuredMesh>>>();
348 for (std::unique_ptr<MeshBase> * hole_ptr :
_hole_ptrs)
352 const UnstructuredMesh & hole =
dynamic_cast<UnstructuredMesh &
>(**hole_ptr);
353 ngholes->push_back(std::make_unique<ReplicatedMesh>(hole));
355#ifdef LIBMESH_ENABLE_UNIQUE_ID
362 UnstructuredMesh & hole_copy = *ngholes->back();
363 hole_copy.set_next_unique_id(hole_copy.parallel_max_unique_id());
374 auto output_subdomain_name = getParam<SubdomainName>(
"output_subdomain_name");
387 if (possible_sbdid != Elem::invalid_subdomain_id)
401 for (
auto elem :
mesh->element_ptr_range())
403 mooseAssert(elem->type() == tet_type,
"Unexpected element type found in triangulation");
412 if (elem->is_flipped())
415 mooseError(
"Inverted element found in triangulation.\n"
416 "Laplacian smoothing can create these at reentrant corners; disable it?");
418 mooseError(
"Unexplained inverted element found in triangulation.\n");
423 mesh->unset_has_cached_elem_data();
425 const bool use_binary_search = (
_algorithm ==
"BINARY");
442 const boundary_id_type end_bcid =
_hole_ptrs.size() + 1;
466 boundary_id_type new_hole_bcid = end_bcid + free_boundary_id;
469 ? getParam<std::vector<BoundaryName>>(
"hole_boundaries")
470 : std::vector<BoundaryName>();
471 const BoundaryName output_boundary =
472 isParamValid(
"output_boundary") ? getParam<BoundaryName>(
"output_boundary") : BoundaryName();
476 const std::vector<BoundaryID> output_boundary_id =
478 ? (MooseUtils::isDigits(output_boundary)
479 ? std::vector<BoundaryID>(
480 1, MooseMeshUtils::getIDFromName<BoundaryName, BoundaryID>(output_boundary))
481 : std::vector<BoundaryID>(1, free_boundary_id))
482 : std::vector<BoundaryID>();
484 std::vector<BoundaryID> hole_boundary_ids;
486 for (
auto h : index_range(hole_boundaries))
487 hole_boundary_ids.push_back(
488 MooseUtils::isDigits(hole_boundaries[h])
489 ? MooseMeshUtils::getIDFromName<BoundaryName, BoundaryID>(hole_boundaries[h])
490 : h + 1 + free_boundary_id);
494 if (hole_boundary_ids.size())
496 new_hole_bcid = std::max(new_hole_bcid, boundary_id_type(hole_boundary_ids[h] + 1));
497 if (output_boundary_id.size())
498 new_hole_bcid = std::max(new_hole_bcid, boundary_id_type(output_boundary_id[0] + 1));
500 bool doing_stitching =
false;
504 const MeshBase & hole_mesh = **
_hole_ptrs[hole_i];
505 auto & hole_boundary_info = hole_mesh.get_boundary_info();
506 const std::set<boundary_id_type> & local_hole_bcids = hole_boundary_info.get_boundary_ids();
508 if (!local_hole_bcids.empty())
509 new_hole_bcid = std::max(new_hole_bcid, boundary_id_type(*local_hole_bcids.rbegin() + 1));
510 hole_mesh.comm().max(new_hole_bcid);
513 doing_stitching =
true;
516 const boundary_id_type inner_bcid = new_hole_bcid + 1;
531 std::unordered_map<std::tuple<Point, Point, Point>,
532 std::pair<std::pair<Elem *, unsigned int>,
bool>>
535 auto sorted_point_tuple = [](Elem & elem,
unsigned int side)
540 const std::vector<unsigned int> nodes_on_side = elem.nodes_on_side(side);
541 std::vector<Point> p;
543 for (
const auto n : nodes_on_side)
544 if (elem.is_vertex(n))
545 p.push_back(elem.point(n));
546 libmesh_assert_equal_to(p.size(), 3);
550 return std::make_tuple(p[0], p[1], p[2]);
551 else if (p[0] < p[2])
552 return std::make_tuple(p[0], p[2], p[1]);
554 return std::make_tuple(p[2], p[0], p[1]);
559 return std::make_tuple(p[1], p[0], p[2]);
560 else if (p[1] < p[2])
561 return std::make_tuple(p[1], p[2], p[0]);
563 return std::make_tuple(p[2], p[1], p[0]);
568 for (
auto elem :
mesh->element_ptr_range())
569 for (
auto s : make_range(elem->n_sides()))
570 if (!elem->neighbor_ptr(s))
572 auto points = sorted_point_tuple(*elem, s);
573 libmesh_assert(!mesh_faces.count(points));
574 mesh_faces.emplace(points, std::make_pair(std::make_pair(elem, s),
false));
577 auto & mesh_boundary_info =
mesh->get_boundary_info();
580 auto & main_subdomain_map =
mesh->set_subdomain_name_map();
583 UnstructuredMesh & hole_mesh =
dynamic_cast<UnstructuredMesh &
>(**
_hole_ptrs[hole_i]);
584 auto & hole_boundary_info = hole_mesh.get_boundary_info();
594 for (
auto elem : hole_mesh.element_ptr_range())
595 for (
auto s : make_range(elem->n_sides()))
596 if (!elem->neighbor_ptr(s))
598 auto points = sorted_point_tuple(*elem, s);
599 auto it = mesh_faces.find(points);
603 if (it != mesh_faces.end())
605 auto [main_elem, main_side] = it->second.first;
608 hole_boundary_info.add_side(elem, s, new_hole_bcid);
609 mesh_boundary_info.add_side(main_elem, main_side, inner_bcid);
613 mesh_boundary_info.add_side(main_elem, main_side, hole_i + 1);
614 it->second.second =
true;
621 for (
auto & [points, elem_side] : mesh_faces)
622 if (!elem_side.second)
624 auto [main_elem, main_side] = elem_side.first;
625 mesh_boundary_info.add_side(main_elem, main_side, 0);
641 *
mesh, 0, temp_bcid_shift + free_boundary_id);
645 *
mesh, hole_i + 1, hole_i + 1 + temp_bcid_shift + free_boundary_id);
650 if (output_boundary_id.size())
652 *
mesh, temp_bcid_shift + free_boundary_id, output_boundary_id[0]);
655 *
mesh, temp_bcid_shift + free_boundary_id, free_boundary_id);
657 if (hole_boundary_ids.size())
660 *
mesh, hole_i + 1 + temp_bcid_shift + free_boundary_id, hole_boundary_ids[hole_i]);
664 *
mesh, hole_i + 1 + temp_bcid_shift + free_boundary_id, hole_i + 1 + free_boundary_id);
668 UnstructuredMesh & hole_mesh =
dynamic_cast<UnstructuredMesh &
>(**
_hole_ptrs[hole_i]);
677 *
mesh, hole_mesh,
false);
680 *
mesh, hole_mesh, {new_hole_bcid, inner_bcid});
684 const auto & increment_subdomain_map = hole_mesh.get_subdomain_name_map();
685 main_subdomain_map.insert(increment_subdomain_map.begin(), increment_subdomain_map.end());
697 std::size_t n_nodes_stitched =
mesh->stitch_meshes(hole_mesh,
705 if (!n_nodes_stitched)
706 mooseError(
"Failed to stitch hole mesh ", hole_i,
" to new tetrahedralization.");
712 std::size_t n_nodes_stitched =
mesh->stitch_surfaces(inner_bcid,
718 if (!n_nodes_stitched)
719 mooseError(
"Failed to stitch combined hole meshes to new tetrahedralization.");
723 if (hole_boundary_ids.size())
725 mesh->get_boundary_info().sideset_name(hole_boundary_ids[h]) = hole_boundaries[h];
726 if (output_boundary_id.size())
727 mesh->get_boundary_info().sideset_name(output_boundary_id[0]) = output_boundary;
730 std::set<SubdomainName> main_subdomain_map_name_list;
731 for (
auto const & id_name_pair : main_subdomain_map)
732 main_subdomain_map_name_list.emplace(id_name_pair.second);
733 if (main_subdomain_map.size() != main_subdomain_map_name_list.size())
734 paramError(
"holes",
"The hole meshes contain subdomain name maps with conflicts.");
741 mesh->unset_is_prepared();
744 mooseError(
"Cannot use XYZDelaunayGenerator without NetGen-enabled libMesh.");
745 return std::unique_ptr<MeshBase>();