14 #include "libmesh/elem.h" 15 #include "libmesh/boundary_info.h" 16 #include "libmesh/int_range.h" 17 #include "libmesh/enum_to_string.h" 18 #include "libmesh/mesh_modification.h" 19 #include "libmesh/mesh_serializer.h" 20 #include "libmesh/mesh_triangle_holes.h" 21 #include "libmesh/parsed_function.h" 22 #include "libmesh/poly2tri_triangulator.h" 23 #include "libmesh/unstructured_mesh.h" 30 std::unique_ptr<MeshBase>
32 std::unique_ptr<MeshBase> boundary_mesh,
33 std::vector<std::unique_ptr<MeshBase>> hole_meshes,
37 std::unique_ptr<UnstructuredMesh>
mesh =
46 std::set<std::size_t> bdy_ids;
52 "input_subdomain_names",
53 "input_boundary_names and input_subdomain_names cannot both specify an outer boundary.");
59 mg.
paramError(
"input_boundary_names",
name,
" is not a boundary name in the input mesh");
71 const auto subdomain_ids =
75 std::set<SubdomainID> subdomains;
83 " was not found in the boundary mesh");
85 bdy_ids.insert(subdomain_ids[i]);
90 poly2tri.set_outer_boundary_ids(bdy_ids);
93 poly2tri.set_refine_boundary_allowed(xyd_opts.
refine_bdy);
94 poly2tri.set_verify_hole_boundaries(xyd_opts.
verify_holes);
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();
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 =
122 : ((holes_with_midpoints[hole_i] && xyd_opts.
stitch_holes[hole_i]) ||
123 stitch_second_order_holes);
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 &&
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);
143 poly2tri.set_desired_area_function(&area_func);
147 poly2tri.set_auto_area_function(
157 poly2tri.elem_type() = libMesh::ElemType::TRI6;
159 poly2tri.elem_type() = libMesh::ElemType::TRI7;
165 poly2tri.triangulate();
182 for (
auto & hole_ptr : hole_meshes)
184 auto possible_sbdid =
189 output_subdomain_id = possible_sbdid;
192 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;
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" 221 <<
" found in triangulation");
223 elem->subdomain_id() = output_subdomain_id;
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);
270 hole_meshes[hole_i]->comm().max(free_boundary_id);
276 hole_boundary_rec[h] = h + 1 + free_boundary_id;
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];
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);
331 doing_stitching =
true;
348 if (!holes_with_midpoints[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())
443 hole_boundary_rec[hole_i],
450 mesh->stitch_meshes(hole_mesh,
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.");
std::string name(const ElemQuality q)
void remove_id(boundary_id_type id, bool global=false)
std::string desired_area_func
Bundle of inputs for triangulateWithDelaunay.
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 ...
Real auto_area_func_default_size
std::string tri_elem_type
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
bool has_output_subdomain_id
static constexpr Real TOLERANCE
bool has_output_subdomain_name
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.
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
const BoundaryInfo & get_boundary_info() const
SubdomainID output_subdomain_id
bool has_cached_elem_data
std::vector< BoundaryName > hole_boundaries
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
std::vector< BoundaryName > input_boundary_names
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.
Real auto_area_func_default_size_dist
std::vector< SubdomainName > input_subdomain_names
BoundaryName output_boundary
std::vector< bool > refine_holes
static const boundary_id_type invalid_id
const std::map< subdomain_id_type, std::string > & get_subdomain_name_map() const
boundary_id_type BoundaryID
std::vector< std::set< std::size_t > > hole_boundary_id_filters
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::unique_ptr< MeshBase > triangulateWithDelaunay(MeshGenerator &mg, std::unique_ptr< MeshBase > boundary_mesh, std::vector< std::unique_ptr< MeshBase >> hole_meshes, const XYDelaunayOptions &xyd_opts)
unsigned int auto_area_function_num_points
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
unsigned int add_nodes_per_boundary_segment
void max(const T &r, T &o, Request &req) const
SubdomainName output_subdomain_name
std::vector< bool > stitch_holes
virtual void all_complete_order()
std::vector< Point > interior_points
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
std::vector< std::set< BoundaryID > > hole_boundary_inner_id_defaults
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...
static constexpr subdomain_id_type invalid_subdomain_id
const DofMap &dof_map LIBMESH_COMMA unsigned int std::string & set_subdomain_name_map()
MeshGenerators are objects that can modify or add to an existing mesh.
auto index_range(const T &sizable)
Real auto_area_function_power