37 std::unique_ptr<UnstructuredMesh>
mesh =
46 std::set<std::size_t> bdy_ids;
48 if (!xyd_opts.input_boundary_names.empty())
50 if (!xyd_opts.input_subdomain_names.empty())
52 "input_subdomain_names",
53 "input_boundary_names and input_subdomain_names cannot both specify an outer boundary.");
55 for (
const auto & name : xyd_opts.input_boundary_names)
58 if (bcid == BoundaryInfo::invalid_id)
59 mg.
paramError(
"input_boundary_names", name,
" is not a boundary name in the input mesh");
65 if (!xyd_opts.input_subdomain_names.empty())
71 const auto subdomain_ids =
75 std::set<SubdomainID> subdomains;
82 xyd_opts.input_subdomain_names[i],
83 " was not found in the boundary mesh");
85 bdy_ids.insert(subdomain_ids[i]);
90 poly2tri.set_outer_boundary_ids(bdy_ids);
92 poly2tri.set_interpolate_boundary_points(xyd_opts.add_nodes_per_boundary_segment);
93 poly2tri.set_refine_boundary_allowed(xyd_opts.refine_bdy);
94 poly2tri.set_verify_hole_boundaries(xyd_opts.verify_holes);
96 poly2tri.desired_area() = xyd_opts.desired_area;
97 poly2tri.minimum_angle() = 0;
98 poly2tri.smooth_after_generating() = xyd_opts.smooth_tri;
100 std::vector<libMesh::TriangulatorInterface::MeshedHole> meshed_holes;
101 std::vector<libMesh::TriangulatorInterface::Hole *> triangulator_hole_ptrs(hole_meshes.size());
104 std::vector<bool> holes_with_midpoints(hole_meshes.size());
105 bool stitch_second_order_holes(
false);
108 meshed_holes.reserve(hole_meshes.size());
111 if (!hole_meshes[hole_i]->is_prepared())
112 hole_meshes[hole_i]->prepare_for_use();
113 if (hole_i < xyd_opts.hole_boundary_id_filters.size() &&
114 !xyd_opts.hole_boundary_id_filters[hole_i].empty())
115 meshed_holes.emplace_back(*hole_meshes[hole_i], xyd_opts.hole_boundary_id_filters[hole_i]);
117 meshed_holes.emplace_back(*hole_meshes[hole_i]);
118 holes_with_midpoints[hole_i] = meshed_holes.back().n_midpoints();
119 stitch_second_order_holes =
120 xyd_opts.stitch_holes.empty()
122 : ((holes_with_midpoints[hole_i] && xyd_opts.stitch_holes[hole_i]) ||
123 stitch_second_order_holes);
124 if (hole_i < xyd_opts.refine_holes.size())
125 meshed_holes.back().set_refine_boundary_allowed(xyd_opts.refine_holes[hole_i]);
127 triangulator_hole_ptrs[hole_i] = &meshed_holes.back();
129 if (stitch_second_order_holes &&
130 (xyd_opts.tri_elem_type ==
"TRI3" || xyd_opts.tri_elem_type ==
"DEFAULT"))
133 "Cannot use first order elements with stitched quadratic element holes. Please try " 134 "to specify a higher-order tri_element_type or reduce the order of the hole inputs.");
136 if (!triangulator_hole_ptrs.empty())
137 poly2tri.attach_hole_list(&triangulator_hole_ptrs);
139 if (xyd_opts.desired_area_func !=
"")
143 poly2tri.set_desired_area_function(&area_func);
145 else if (xyd_opts.use_auto_area_func)
147 poly2tri.set_auto_area_function(
149 xyd_opts.auto_area_function_num_points,
150 xyd_opts.auto_area_function_power,
151 xyd_opts.auto_area_func_default_size > 0.0 ? xyd_opts.auto_area_func_default_size : 0.0,
152 xyd_opts.auto_area_func_default_size_dist > 0.0 ? xyd_opts.auto_area_func_default_size_dist
156 if (xyd_opts.tri_elem_type ==
"TRI6")
157 poly2tri.elem_type() = libMesh::ElemType::TRI6;
158 else if (xyd_opts.tri_elem_type ==
"TRI7")
159 poly2tri.elem_type() = libMesh::ElemType::TRI7;
162 for (
const auto &
point : xyd_opts.interior_points)
165 poly2tri.triangulate();
168 xyd_opts.has_output_subdomain_id ? xyd_opts.output_subdomain_id : 0;
170 if (xyd_opts.has_output_subdomain_name)
174 if (
id == Elem::invalid_subdomain_id)
176 if (!xyd_opts.has_output_subdomain_id)
182 for (
auto & hole_ptr : hole_meshes)
184 auto possible_sbdid =
187 if (possible_sbdid != Elem::invalid_subdomain_id)
189 output_subdomain_id = possible_sbdid;
192 output_subdomain_id =
199 if (xyd_opts.has_output_subdomain_id)
201 if (
id != output_subdomain_id)
203 "name has been used by the input meshes and the corresponding id is not " 204 "equal to 'output_subdomain_id'");
207 output_subdomain_id = id;
210 if (xyd_opts.output_subdomain_name.size())
214 if (xyd_opts.smooth_tri || output_subdomain_id)
215 for (
auto elem :
mesh->element_ptr_range())
217 mooseAssert(elem->type() == (xyd_opts.tri_elem_type ==
"TRI6" 219 : (xyd_opts.tri_elem_type ==
"TRI7" ?
TRI7 :
TRI3)),
221 <<
" found in triangulation");
223 elem->subdomain_id() = output_subdomain_id;
230 if (xyd_opts.smooth_tri)
232 auto cross_prod = (elem->point(1) - elem->point(0)).cross(elem->point(2) - elem->point(0));
234 if (cross_prod(2) <= 0)
235 mooseError(
"Inverted element found in triangulation.\n" 236 "Laplacian smoothing can create these at reentrant corners; disable it?");
255 std::vector<BoundaryID> hole_boundary_rec(hole_meshes.size());
256 std::iota(hole_boundary_rec.begin(), hole_boundary_rec.end(), 1);
262 if (xyd_opts.stitch_holes.size())
266 if (xyd_opts.stitch_holes[hole_i])
270 hole_meshes[hole_i]->comm().max(free_boundary_id);
276 hole_boundary_rec[h] = h + 1 + free_boundary_id;
286 *mesh, 0, (xyd_opts.has_output_boundary ? end_bcid : 0) + free_boundary_id);
288 if (!xyd_opts.hole_boundaries.empty())
294 *mesh, h + 1 + free_boundary_id, h + 1 + free_boundary_id + end_bcid);
299 *mesh, h + 1 + free_boundary_id + end_bcid, hole_boundary_ids[h]);
300 hole_boundary_rec[h] = hole_boundary_ids[h];
306 if (xyd_opts.has_output_boundary)
308 const std::vector<BoundaryID> output_boundary_id =
312 *mesh, end_bcid + free_boundary_id, output_boundary_id[0]);
318 bool doing_stitching =
false;
322 const MeshBase & hole_mesh = *hole_meshes[hole_i];
324 const std::set<boundary_id_type> & local_hole_bcids = hole_boundary_info.
get_boundary_ids();
326 if (!local_hole_bcids.empty())
328 hole_mesh.
comm().
max(new_hole_bcid);
330 if (hole_i < xyd_opts.stitch_holes.size() && xyd_opts.stitch_holes[hole_i])
331 doing_stitching =
true;
344 if (hole_i < xyd_opts.stitch_holes.size() && xyd_opts.stitch_holes[hole_i])
348 if (!holes_with_midpoints[hole_i])
350 if (xyd_opts.tri_elem_type ==
"TRI6")
352 else if (xyd_opts.tri_elem_type ==
"TRI7")
368 const auto & hole_bdy_id_filter = (hole_i < xyd_opts.hole_boundary_id_filters.size())
369 ? xyd_opts.hole_boundary_id_filters[hole_i]
370 : std::set<std::size_t>();
375 std::unordered_map<Point, Point> next_hole_boundary_point;
376 const int np = mh.n_points();
378 next_hole_boundary_point[mh.point(pi - 1)] = mh.
point(pi);
379 next_hole_boundary_point[mh.point(np - 1)] = mh.point(0);
382 int found_hole_sides = 0;
384 for (
auto elem : hole_mesh.element_ptr_range())
386 if (elem->dim() != 2)
387 mooseError(
"Non 2-D element found in hole; stitching is not supported.");
389 auto ns = elem->n_sides();
392 auto it_s = next_hole_boundary_point.find(elem->point(s));
393 if (it_s != next_hole_boundary_point.end())
394 if (it_s->second == elem->point((s + 1) % ns))
396 hole_boundary_info.add_side(elem, s, new_hole_bcid);
403 mooseAssert(found_hole_sides == np,
"Failed to find full outer boundary of meshed hole");
407 int found_inner_sides = 0;
409 for (
auto elem :
mesh->element_ptr_range())
411 auto ns = elem->n_sides();
414 auto it_s = next_hole_boundary_point.find(elem->point((s + 1) % ns));
415 if (it_s != next_hole_boundary_point.end())
416 if (it_s->second == elem->point(s))
418 mesh_boundary_info.
add_side(elem, s, inner_bcid);
425 mooseAssert(found_inner_sides == np,
"Failed to find full boundary around meshed hole");
430 main_subdomain_map.insert(increment_subdomain_map.begin(), increment_subdomain_map.end());
433 for (
const auto & bcid : hole_bdy_id_filter)
434 hole_boundary_info.remove_id(bcid);
436 if (hole_bdy_id_filter.size())
440 xyd_opts.hole_boundary_inner_id_defaults[hole_i].empty()
442 : *xyd_opts.hole_boundary_inner_id_defaults[hole_i].begin(),
443 hole_boundary_rec[hole_i],
450 mesh->stitch_meshes(hole_mesh,
455 xyd_opts.verbose_stitching,
456 xyd_opts.use_binary_search);
460 std::set<SubdomainName> main_subdomain_map_name_list;
461 for (
auto const & id_name_pair : main_subdomain_map)
462 main_subdomain_map_name_list.emplace(id_name_pair.second);
463 if (main_subdomain_map.size() != main_subdomain_map_name_list.size())
464 mg.
paramError(
"holes",
"The hole meshes contain subdomain name maps with conflicts.");
void remove_id(boundary_id_type id, bool global=false)
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 ...
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
const Parallel::Communicator & comm() const
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.
std::vector< subdomain_id_type > getSubdomainIDs(const libMesh::MeshBase &mesh, const std::vector< SubdomainName > &subdomain_name)
Get the associated subdomainIDs for the subdomain names that are passed in.
const BoundaryInfo & get_boundary_info() const
bool has_cached_elem_data
Preparation preparation() const
virtual Node * add_point(const Point &p, const dof_id_type id=DofObject::invalid_id, const processor_id_type proc_id=DofObject::invalid_processor_id)=0
SubdomainID getSubdomainID(const SubdomainName &subdomain_name, const MeshBase &mesh)
Gets the subdomain ID associated with the given SubdomainName.
auto max(const L &left, const R &right)
const SubdomainID INVALID_BLOCK_ID
BoundaryID getBoundaryID(const BoundaryName &boundary_name, const MeshBase &mesh)
Gets the boundary ID associated with the given BoundaryName.
const std::map< subdomain_id_type, std::string > & get_subdomain_name_map() const
boundary_id_type BoundaryID
void subdomain_ids(std::set< subdomain_id_type > &ids, const bool global=true) const
std::vector< BoundaryID > getBoundaryIDs(const libMesh::MeshBase &mesh, const std::vector< BoundaryName > &boundary_name, bool generate_unknown, const std::set< BoundaryID > &mesh_boundary_ids)
Gets the boundary IDs with their names.
std::string & subdomain_name(subdomain_id_type id)
std::string & sideset_name(boundary_id_type id)
std::string enum_to_string(const T e)
const std::set< boundary_id_type > & get_boundary_ids() const
void max(const T &r, T &o, Request &req) const
virtual void all_complete_order()
void add_side(const dof_id_type elem, const unsigned short int side, const boundary_id_type id)
IntRange< T > make_range(T beg, T end)
void all_second_order(const bool full_ordered=true)
virtual const Point & point(const dof_id_type i) const=0
void changeBoundaryId(MeshBase &mesh, const libMesh::boundary_id_type old_id, const libMesh::boundary_id_type new_id, bool delete_prev)
Changes the old boundary ID to a new ID in the mesh.
SubdomainID getNextFreeSubdomainID(MeshBase &input_mesh)
Checks input mesh and returns max(block ID) + 1, which represents a block ID that is not currently in...
BoundaryID getNextFreeBoundaryID(MeshBase &input_mesh)
Checks input mesh and returns the largest boundary ID in the mesh plus one, which is a boundary ID in...
const DofMap &dof_map LIBMESH_COMMA unsigned int std::string & set_subdomain_name_map()
auto index_range(const T &sizable)