16 #include "libmesh/mesh_tools.h" 17 #include "libmesh/mesh_refinement.h" 18 #include "libmesh/fe.h" 19 #include "libmesh/quadrature_gauss.h" 20 #include "libmesh/face_tri3.h" 21 #include "libmesh/cell_tet4.h" 22 #include "libmesh/face_quad4.h" 23 #include "libmesh/cell_hex8.h" 24 #include "libmesh/string_to_enum.h" 25 #include "libmesh/enum_point_locator_type.h" 38 params.
addRequiredParam<MeshGeneratorName>(
"input",
"The mesh we want to diagnose");
39 params.
addClassDescription(
"Runs a series of diagnostics on the mesh to detect potential issues " 40 "such as unsupported features");
43 MooseEnum chk_option(
"NO_CHECK INFO WARNING ERROR",
"NO_CHECK");
46 "examine_sidesets_orientation",
48 "whether to check that sidesets are consistently oriented using neighbor subdomains. If a " 49 "sideset is inconsistently oriented within a subdomain, this will not be detected");
51 "check_for_watertight_sidesets",
53 "whether to check for external sides that are not assigned to any sidesets");
55 "check_for_watertight_nodesets",
57 "whether to check for external nodes that are not assigned to any nodeset");
58 params.
addParam<std::vector<BoundaryName>>(
59 "boundaries_to_check",
61 "Names boundaries that should form a watertight envelope around the mesh. Defaults to all " 62 "the boundaries combined.");
64 "examine_element_volumes", chk_option,
"whether to examine volume of the elements");
65 params.
addParam<
Real>(
"minimum_element_volumes", 1e-16,
"minimum size for element volume");
66 params.
addParam<
Real>(
"maximum_element_volumes", 1e16,
"Maximum size for element volume");
70 "whether to look for multiple element types in the same sub-domain");
72 "examine_element_overlap", chk_option,
"whether to find overlapping elements");
74 "examine_nonplanar_sides", chk_option,
"whether to check element sides are planar");
77 "whether to examine the conformality of elements in the mesh");
80 "Whether to check if there are any intersecting edges");
81 params.
addParam<
Real>(
"intersection_tol", TOLERANCE,
"tolerence for intersecting edges");
82 params.
addParam<
Real>(
"nonconformal_tol", TOLERANCE,
"tolerance for element non-conformality");
84 "search_for_adaptivity_nonconformality",
86 "whether to check for non-conformality arising from adaptive mesh refinement");
89 "whether to check the local Jacobian for bad (non-positive) values");
91 "check_polygons", chk_option,
"Whether to check that all C0 polygons are convex");
95 "How many problematic element/nodes/sides/etc are explicitly reported on by each check");
101 _input(getMesh(
"input")),
102 _check_sidesets_orientation(getParam<
MooseEnum>(
"examine_sidesets_orientation")),
103 _check_watertight_sidesets(getParam<
MooseEnum>(
"check_for_watertight_sidesets")),
104 _check_watertight_nodesets(getParam<
MooseEnum>(
"check_for_watertight_nodesets")),
105 _watertight_boundary_names(getParam<
std::vector<BoundaryName>>(
"boundaries_to_check")),
106 _check_element_volumes(getParam<
MooseEnum>(
"examine_element_volumes")),
107 _min_volume(getParam<
Real>(
"minimum_element_volumes")),
108 _max_volume(getParam<
Real>(
"maximum_element_volumes")),
109 _check_element_types(getParam<
MooseEnum>(
"examine_element_types")),
110 _check_element_overlap(getParam<
MooseEnum>(
"examine_element_overlap")),
111 _check_non_planar_sides(getParam<
MooseEnum>(
"examine_nonplanar_sides")),
112 _check_non_conformal_mesh(getParam<
MooseEnum>(
"examine_non_conformality")),
113 _non_conformality_tol(getParam<
Real>(
"nonconformal_tol")),
114 _check_non_matching_edges(getParam<
MooseEnum>(
"examine_non_matching_edges")),
115 _non_matching_edge_tol(getParam<
Real>(
"intersection_tol")),
116 _check_adaptivity_non_conformality(
117 getParam<
MooseEnum>(
"search_for_adaptivity_nonconformality")),
118 _check_local_jacobian(getParam<
MooseEnum>(
"check_local_jacobian")),
119 _check_polygons(getParam<
MooseEnum>(
"check_polygons")),
120 _num_outputs(getParam<unsigned
int>(
"log_length_limit"))
127 "You must set this parameter to true to trigger element size checks");
130 "You must set this parameter to true to trigger mesh conformality check");
137 mooseError(
"You need to turn on at least one diagnostic. Did you misspell a parameter?");
140 std::unique_ptr<MeshBase>
143 std::unique_ptr<MeshBase>
mesh = std::move(
_input);
146 if (!
mesh->is_serial())
147 mooseError(
"Only serialized meshes are supported");
151 mesh->prepare_for_use();
157 mooseError(
"User specified boundary_to_check \'", boundary_name,
"\' does not exist");
204 auto & boundary_info =
mesh->get_boundary_info();
205 auto side_tuples = boundary_info.build_side_list();
207 for (
const auto bid : boundary_info.get_boundary_ids())
211 std::set<std::pair<subdomain_id_type, subdomain_id_type>> block_neighbors;
214 if (std::get<2>(side_tuples[index]) != bid)
216 const auto elem_ptr =
mesh->elem_ptr(std::get<0>(side_tuples[index]));
217 if (elem_ptr->neighbor_ptr(std::get<1>(side_tuples[index])))
218 block_neighbors.insert(std::make_pair(
219 elem_ptr->subdomain_id(),
220 elem_ptr->neighbor_ptr(std::get<1>(side_tuples[index]))->subdomain_id()));
224 std::set<std::pair<subdomain_id_type, subdomain_id_type>> flipped_pairs;
225 for (
const auto & block_pair_1 : block_neighbors)
226 for (
const auto & block_pair_2 : block_neighbors)
227 if (block_pair_1 != block_pair_2)
228 if (block_pair_1.first == block_pair_2.second &&
229 block_pair_1.second == block_pair_2.first)
230 flipped_pairs.insert(block_pair_1);
233 const std::string sideset_full_name =
234 boundary_info.sideset_name(bid) +
" (" + std::to_string(bid) +
")";
235 if (!flipped_pairs.empty())
237 std::string block_pairs_string =
"";
238 for (
const auto & pair : flipped_pairs)
239 block_pairs_string +=
240 " [" +
mesh->subdomain_name(pair.first) +
" (" + std::to_string(pair.first) +
"), " +
241 mesh->subdomain_name(pair.second) +
" (" + std::to_string(pair.second) +
")]";
242 message =
"Inconsistent orientation of sideset " + sideset_full_name +
243 " with regards to subdomain pairs" + block_pairs_string;
246 message =
"Sideset " + sideset_full_name +
247 " is consistently oriented with regards to the blocks it neighbors";
254 unsigned int num_normals_flipping = 0;
255 Real steepest_side_angles = 0;
256 for (
const auto & [elem_id,
side_id, side_bid] : side_tuples)
260 const auto & elem_ptr =
mesh->elem_ptr(elem_id);
263 const std::unique_ptr<const Elem> face = elem_ptr->build_side_ptr(
side_id);
264 std::unique_ptr<libMesh::FEBase> fe(
267 fe->attach_quadrature_rule(&qface);
268 const auto & normals = fe->get_normals();
270 mooseAssert(normals.size() == 1,
"We expected only one normal here");
271 const auto & side_normal = normals[0];
274 for (
const auto neighbor : elem_ptr->neighbor_ptr_range())
276 for (
const auto neigh_side_index : neighbor->side_index_range())
279 if (!boundary_info.has_boundary_id(neighbor, neigh_side_index, bid))
286 fe_neighbor->attach_quadrature_rule(&qface);
287 const auto & neigh_normals = fe_neighbor->get_normals();
288 fe_neighbor->reinit(neighbor, neigh_side_index);
289 mooseAssert(neigh_normals.size() == 1,
"We expected only one normal here");
290 const auto & neigh_side_normal = neigh_normals[0];
293 if (neigh_side_normal * side_normal <= 0)
295 num_normals_flipping++;
296 steepest_side_angles =
297 std::max(std::acos(neigh_side_normal * side_normal), steepest_side_angles);
299 _console <<
"Side normals changed by more than pi/2 for sideset " 300 << sideset_full_name <<
" between side " <<
side_id <<
" of element " 301 << elem_ptr->id() <<
" and side " << neigh_side_index
302 <<
" of neighbor element " << neighbor->id() << std::endl;
304 _console <<
"Maximum output reached for sideset normal flipping check. Silencing " 311 if (num_normals_flipping)
312 message =
"Sideset " + sideset_full_name +
313 " has two neighboring sides with a very large angle. Largest angle detected: " +
314 std::to_string(steepest_side_angles) +
" rad (" +
315 std::to_string(steepest_side_angles * 180 /
libMesh::pi) +
" degrees).";
317 message =
"Sideset " + sideset_full_name +
318 " does not appear to have side-to-neighbor-side orientation flips. All neighbor " 319 "sides normal differ by less than pi/2";
335 if (
mesh->mesh_dimension() < 2)
336 mooseError(
"The sideset check only works for 2D and 3D meshes");
337 auto & boundary_info =
mesh->get_boundary_info();
338 boundary_info.build_side_list();
339 const auto sideset_map = boundary_info.get_sideset_map();
340 unsigned int num_faces_without_sideset = 0;
342 for (
const auto elem :
mesh->active_element_ptr_range())
344 for (
auto i : elem->side_index_range())
347 if (elem->neighbor_ptr(i) ==
nullptr)
350 std::vector<boundary_id_type> boundary_ids;
351 auto side_range = sideset_map.equal_range(elem);
352 for (
const auto & itr :
as_range(side_range))
353 if (itr.second.first == i)
354 boundary_ids.push_back(i);
356 std::vector<boundary_id_type> intersections =
359 bool no_specified_ids = boundary_ids.empty();
362 if (
mesh->mesh_dimension() == 3)
363 message =
"Element " + std::to_string(elem->id()) +
364 " contains an external face which has not been assigned to ";
366 message =
"Element " + std::to_string(elem->id()) +
367 " contains an external edge which has not been assigned to ";
368 if (no_specified_ids)
369 message = message +
"a sideset";
370 else if (specified_ids)
371 message = message +
"one of the specified sidesets";
372 if ((no_specified_ids || specified_ids) && num_faces_without_sideset <
_num_outputs)
375 num_faces_without_sideset++;
381 if (
mesh->mesh_dimension() == 3)
382 message =
"Number of external element faces that have not been assigned to a sideset: " +
383 std::to_string(num_faces_without_sideset);
385 message =
"Number of external element edges that have not been assigned to a sideset: " +
386 std::to_string(num_faces_without_sideset);
402 if (
mesh->mesh_dimension() < 2)
403 mooseError(
"The nodeset check only works for 2D and 3D meshes");
404 auto & boundary_info =
mesh->get_boundary_info();
405 unsigned int num_nodes_without_nodeset = 0;
406 std::set<dof_id_type> checked_nodes_id;
408 for (
const auto elem :
mesh->active_element_ptr_range())
410 for (
const auto i : elem->side_index_range())
413 if (elem->neighbor_ptr(i) ==
nullptr)
416 auto side = elem->side_ptr(i);
417 const auto & node_list = side->get_nodes();
418 for (
unsigned int j = 0; j < side->n_nodes(); j++)
420 const auto node = node_list[j];
421 if (checked_nodes_id.count(node->id()))
424 std::vector<boundary_id_type> boundary_ids;
425 boundary_info.boundary_ids(node, boundary_ids);
426 std::vector<boundary_id_type> intersection =
429 bool no_specified_ids = boundary_info.n_boundary_ids(node) == 0;
431 std::string message =
432 "Node " + std::to_string(node->id()) +
433 " is on an external boundary of the mesh, but has not been assigned to ";
434 if (no_specified_ids)
435 message = message +
"a nodeset";
436 else if (specified_ids)
437 message = message +
"one of the specified nodesets";
438 if ((no_specified_ids || specified_ids) && num_nodes_without_nodeset <
_num_outputs)
440 checked_nodes_id.insert(node->id());
441 num_nodes_without_nodeset++;
449 message =
"Number of external nodes that have not been assigned to a nodeset: " +
450 std::to_string(num_nodes_without_nodeset);
454 std::vector<boundary_id_type>
456 const std::vector<boundary_id_type> & watertight_boundaries,
457 std::vector<boundary_id_type> & boundary_ids)
const 461 std::sort(boundary_ids.begin(), boundary_ids.end());
462 std::vector<boundary_id_type> boundary_intersection;
463 std::set_intersection(watertight_boundaries.begin(),
464 watertight_boundaries.end(),
465 boundary_ids.begin(),
467 std::back_inserter(boundary_intersection));
468 return boundary_intersection;
474 unsigned int num_tiny_elems = 0;
475 unsigned int num_negative_elems = 0;
476 unsigned int num_big_elems = 0;
478 for (
auto & elem :
mesh->active_element_ptr_range())
480 Real vol = elem->volume();
485 _console <<
"Element with volume below threshold detected : \n" 486 <<
"id " << elem->id() <<
" near point " << elem->vertex_average() << std::endl;
488 _console <<
"Maximum output reached, log is silenced" << std::endl;
494 _console <<
"Element with negative volume detected : \n" 495 <<
"id " << elem->id() <<
" near point " << elem->vertex_average() << std::endl;
497 _console <<
"Maximum output reached, log is silenced" << std::endl;
498 num_negative_elems++;
503 _console <<
"Element with volume above threshold detected : \n" 504 << elem->get_info() << std::endl;
506 _console <<
"Maximum output reached, log is silenced" << std::endl;
510 diagnosticsLog(
"Number of elements below prescribed minimum volume : " +
511 std::to_string(num_tiny_elems),
514 diagnosticsLog(
"Number of elements with negative volume : " + std::to_string(num_negative_elems),
517 diagnosticsLog(
"Number of elements above prescribed maximum volume : " +
518 std::to_string(num_big_elems),
526 std::set<subdomain_id_type> ids;
527 mesh->subdomain_ids(ids);
529 for (
auto &
id : ids)
532 std::set<ElemType> types;
534 for (
auto & elem :
mesh->active_subdomain_elements_ptr_range(
id))
535 types.insert(elem->type());
537 std::string elem_type_names =
"";
538 for (
auto & elem_type : types)
541 _console <<
"Element type in subdomain " +
mesh->subdomain_name(
id) +
" (" +
542 std::to_string(
id) +
") :" + elem_type_names
544 if (types.size() > 1)
545 diagnosticsLog(
"Two different element types in subdomain " + std::to_string(
id),
555 unsigned int num_elem_overlaps = 0;
556 auto pl =
mesh->sub_point_locator();
558 for (
auto & node :
mesh->node_ptr_range())
561 std::set<const Elem *> elements;
562 (*pl)(*node, elements);
564 for (
auto & elem : elements)
566 if (!elem->contains_point(*node))
571 for (
auto & elem_node : elem->node_ref_range())
572 if (*node == elem_node)
578 bool on_a_side =
false;
579 for (
const auto & elem_side_index : elem->side_index_range())
582 if (!found && !on_a_side)
586 _console <<
"Element overlap detected at : " << *node << std::endl;
588 _console <<
"Maximum output reached, log is silenced" << std::endl;
593 diagnosticsLog(
"Number of elements overlapping (node-based heuristics): " +
597 num_elem_overlaps = 0;
600 for (
auto & elem :
mesh->active_element_ptr_range())
603 std::set<const Elem *> overlaps;
604 (*pl)(elem->vertex_average(), overlaps);
606 if (overlaps.size() > 1)
610 _console <<
"Element overlap detected with element : " << elem->id() <<
" near point " 611 << elem->vertex_average() << std::endl;
613 _console <<
"Maximum output reached, log is silenced" << std::endl;
616 diagnosticsLog(
"Number of elements overlapping (centroid-based heuristics): " +
626 unsigned int sides_non_planar = 0;
628 for (
auto & elem :
mesh->active_element_ptr_range())
632 auto side = elem->side_ptr(i);
633 std::vector<const Point *> nodes;
634 for (
auto & node : side->node_ref_range())
635 nodes.emplace_back(&node);
637 if (nodes.size() <= 3)
645 unsigned int third_node_index = 2;
647 while (aligned && third_node_index < nodes.size())
649 v2 = *nodes[0] - *nodes[third_node_index++];
650 aligned = MooseUtils::absoluteFuzzyEqual(v1 * v2 - v1.
norm() * v2.
norm(), 0);
657 bool found_non_planar =
false;
659 for (
auto node_offset :
make_range(nodes.size() - 3))
662 bool planar = MooseUtils::absoluteFuzzyEqual(v2.
cross(v1) * v3, 0);
664 found_non_planar =
true;
667 if (found_non_planar)
671 _console <<
"Nonplanar side detected for side " << i
672 <<
" of element :" << elem->get_info() << std::endl;
674 _console <<
"Maximum output reached, log is silenced" << std::endl;
687 unsigned int num_nonconformal_nodes = 0;
692 num_nonconformal_nodes);
697 const std::unique_ptr<MeshBase> & mesh)
const 699 unsigned int num_likely_AMR_created_nonconformality = 0;
700 auto pl =
mesh->sub_point_locator();
706 auto mesh_copy =
mesh->clone();
710 for (
auto & node :
mesh->node_ptr_range())
713 std::set<const Elem *> elements_around;
714 (*pl)(*node, elements_around);
717 std::set<const Elem *> fine_elements;
718 std::set<const Elem *> coarse_elements;
721 for (
auto elem : elements_around)
725 bool node_on_elem =
false;
731 if (!elem->is_vertex(elem->get_node_index(node)))
738 fine_elements.insert(elem);
742 coarse_elements.insert(elem);
749 if (fine_elements.size() == elements_around.size())
752 if (fine_elements.empty())
759 const auto elem_type = (*fine_elements.begin())->
type();
760 if ((elem_type ==
QUAD4 || elem_type ==
QUAD8 || elem_type ==
QUAD9) &&
761 fine_elements.size() != 2)
763 else if ((elem_type ==
HEX8 || elem_type ==
HEX20 || elem_type ==
HEX27) &&
764 fine_elements.size() != 4)
766 else if ((elem_type ==
TRI3 || elem_type ==
TRI6 || elem_type ==
TRI7) &&
767 fine_elements.size() != 3)
769 else if ((elem_type ==
TET4 || elem_type ==
TET10 || elem_type ==
TET14) &&
770 (fine_elements.size() % 2 != 0))
777 if (elem_type !=
TET4 && elem_type !=
TET10 && elem_type !=
TET14 && coarse_elements.size() > 1)
784 std::vector<const Node *> tentative_coarse_nodes;
791 const auto elem = *fine_elements.begin();
794 std::vector<Elem *> node_on_sides;
798 const auto side = elem->side_ptr(i);
799 std::vector<const Node *> other_nodes_on_side;
800 bool node_on_side =
false;
801 for (
const auto & elem_node : side->node_ref_range())
803 if (*node == elem_node)
806 other_nodes_on_side.emplace_back(&elem_node);
814 bool all_side_nodes_are_shared =
true;
815 for (
const auto & other_node : other_nodes_on_side)
817 bool shared_with_a_fine_elem =
false;
818 for (
const auto & other_elem : fine_elements)
819 if (other_elem != elem &&
821 shared_with_a_fine_elem =
true;
823 if (!shared_with_a_fine_elem)
824 all_side_nodes_are_shared =
false;
826 if (all_side_nodes_are_shared)
828 side_inside_parent = i;
841 const auto interior_side = elem->side_ptr(side_inside_parent);
842 const Node * interior_node =
nullptr;
843 for (
const auto & other_node : interior_side->node_ref_range())
845 if (other_node == *node)
847 bool in_all_node_neighbor_elements =
true;
848 for (
auto other_elem : fine_elements)
851 in_all_node_neighbor_elements =
false;
853 if (in_all_node_neighbor_elements)
855 interior_node = &other_node;
864 std::set<const Elem *> all_elements;
865 elem->find_point_neighbors(*interior_node, all_elements);
870 *interior_node, *node, *elem, tentative_coarse_nodes, fine_elements);
880 const auto & coarse_elem = *coarse_elements.begin();
881 unsigned short coarse_side_i = 0;
882 for (
const auto & coarse_side_index : coarse_elem->side_index_range())
884 const auto coarse_side_ptr = coarse_elem->side_ptr(coarse_side_index);
890 coarse_side_i = coarse_side_index;
894 const auto coarse_side = coarse_elem->side_ptr(coarse_side_i);
907 tentative_coarse_nodes.resize(4);
908 for (
const auto & elem_1 : fine_elements)
909 for (
const auto & coarse_node : elem_1->node_ref_range())
911 bool node_shared =
false;
912 for (
const auto & elem_2 : fine_elements)
914 if (elem_2 != elem_1)
922 elem_1->is_vertex(elem_1->get_node_index(&coarse_node)))
923 tentative_coarse_nodes[i++] = &coarse_node;
924 mooseAssert(i <= 5,
"We went too far in this index");
934 Point axis = *interior_node - *node;
935 const auto start_circle = elem->vertex_average();
937 tentative_coarse_nodes, *interior_node, start_circle, axis);
938 tentative_coarse_nodes.resize(8);
942 for (
const auto & elem : fine_elements)
945 unsigned int node_index = 0;
946 for (
const auto & coarse_node : tentative_coarse_nodes)
954 for (
const auto & neighbor : elem->neighbor_ptr_range())
955 if (all_elements.count(neighbor) && !fine_elements.count(neighbor))
958 const Node * coarse_elem_node =
nullptr;
959 for (
const auto & fine_node : neighbor->node_ref_range())
961 if (!neighbor->is_vertex(neighbor->get_node_index(&fine_node)))
963 bool node_shared =
false;
964 for (
const auto & elem_2 : all_elements)
965 if (elem_2 != neighbor &&
970 coarse_elem_node = &fine_node;
975 tentative_coarse_nodes[node_index + 4] = coarse_elem_node;
976 mooseAssert(node_index + 4 < tentative_coarse_nodes.size(),
"Indexed too far");
977 mooseAssert(coarse_elem_node,
"Did not find last coarse element node");
983 fine_elements = all_elements;
987 else if (elem_type ==
TRI3 || elem_type ==
TRI6 || elem_type ==
TRI7)
992 const Elem * center_elem =
nullptr;
993 for (
const auto refined_elem_1 : fine_elements)
995 unsigned int num_neighbors = 0;
996 for (
const auto refined_elem_2 : fine_elements)
998 if (refined_elem_1 == refined_elem_2)
1000 if (refined_elem_1->has_neighbor(refined_elem_2))
1003 if (num_neighbors >= 2)
1004 center_elem = refined_elem_1;
1010 for (
const auto refined_elem : fine_elements)
1012 if (refined_elem == center_elem)
1014 for (
const auto & other_node : refined_elem->node_ref_range())
1016 refined_elem->is_vertex(refined_elem->get_node_index(&other_node)))
1017 tentative_coarse_nodes.push_back(&other_node);
1023 for (
auto side_index : center_elem->side_index_range())
1025 center_side_opposite_node = side_index;
1026 const auto neighbor_on_other_side_of_opposite_center_side =
1027 center_elem->neighbor_ptr(center_side_opposite_node);
1030 if (!neighbor_on_other_side_of_opposite_center_side)
1033 fine_elements.insert(neighbor_on_other_side_of_opposite_center_side);
1034 for (
const auto & tri_node : neighbor_on_other_side_of_opposite_center_side->node_ref_range())
1035 if (neighbor_on_other_side_of_opposite_center_side->is_vertex(
1036 neighbor_on_other_side_of_opposite_center_side->get_node_index(&tri_node)) &&
1037 center_elem->side_ptr(center_side_opposite_node)->get_node_index(&tri_node) ==
1039 tentative_coarse_nodes.push_back(&tri_node);
1042 "Did not find the side opposite the non-conformality");
1043 mooseAssert(tentative_coarse_nodes.size() == 3,
1044 "We are forming a coarsened triangle element");
1048 else if (elem_type ==
TET4 || elem_type ==
TET10 || elem_type ==
TET14)
1052 std::set<const Elem *> tips_tets;
1053 std::set<const Elem *> inside_tets;
1056 const Elem * coarse_elem =
nullptr;
1057 std::set<const Elem *> fine_tets;
1058 for (
auto & coarse_one : coarse_elements)
1060 for (
const auto & elem : fine_elements)
1063 if (elem->has_neighbor(coarse_one))
1064 fine_tets.insert(elem);
1066 if (fine_tets.size())
1068 coarse_elem = coarse_one;
1077 for (
const auto & elem : fine_elements)
1079 int num_face_neighbors = 0;
1080 for (
const auto & tet : fine_tets)
1081 if (tet->has_neighbor(elem))
1082 num_face_neighbors++;
1083 if (num_face_neighbors == 2)
1085 fine_tets.insert(elem);
1093 std::set<const Node *> other_nodes;
1094 for (
const auto & tet_1 : fine_tets)
1096 for (
const auto & node_1 : tet_1->node_ref_range())
1098 if (&node_1 == node)
1100 if (!tet_1->is_vertex(tet_1->get_node_index(&node_1)))
1102 for (
const auto & tet_2 : fine_tets)
1109 other_nodes.insert(&node_1);
1113 mooseAssert(other_nodes.size() == 2,
1114 "Should find only two extra non-conformal nodes near the coarse element");
1117 for (
const auto & tet_1 : fine_tets)
1119 for (
const auto & neighbor : tet_1->neighbor_ptr_range())
1121 neighbor->is_vertex(neighbor->get_node_index(*other_nodes.begin())) &&
1123 neighbor->is_vertex(neighbor->get_node_index(*other_nodes.rbegin())))
1124 fine_tets.insert(neighbor);
1127 for (
const auto & tet_1 : fine_tets)
1129 for (
const auto & neighbor : tet_1->neighbor_ptr_range())
1131 neighbor->is_vertex(neighbor->get_node_index(*other_nodes.begin())) &&
1133 neighbor->is_vertex(neighbor->get_node_index(*other_nodes.rbegin())))
1134 fine_tets.insert(neighbor);
1138 for (
const auto & tet_1 : fine_tets)
1139 for (
const auto & neighbor : tet_1->neighbor_ptr_range())
1140 for (
const auto & tet_2 : fine_tets)
1141 if (tet_1 != tet_2 && tet_2->has_neighbor(neighbor) && neighbor != coarse_elem)
1142 fine_tets.insert(neighbor);
1145 for (
const auto & tet_1 : fine_tets)
1147 unsigned int unshared_nodes = 0;
1148 for (
const auto & other_node : tet_1->node_ref_range())
1150 if (!tet_1->is_vertex(tet_1->get_node_index(&other_node)))
1152 bool node_shared =
false;
1153 for (
const auto & tet_2 : fine_tets)
1159 if (unshared_nodes == 1)
1160 tips_tets.insert(tet_1);
1161 else if (unshared_nodes == 0)
1162 inside_tets.insert(tet_1);
1164 mooseError(
"Did not expect a tet to have two unshared vertex nodes here");
1171 for (
const auto & tet : inside_tets)
1173 for (
const auto & neighbor : tet->neighbor_ptr_range())
1176 bool shared_with_another_tet =
false;
1177 for (
const auto & tet_2 : fine_tets)
1181 if (tet_2->has_neighbor(neighbor))
1182 shared_with_another_tet =
true;
1184 if (shared_with_another_tet)
1188 std::vector<const Node *> tip_nodes_shared;
1189 unsigned int unshared_nodes = 0;
1190 for (
const auto & other_node : neighbor->node_ref_range())
1192 if (!neighbor->is_vertex(neighbor->get_node_index(&other_node)))
1196 for (
const auto & tip_tet : tips_tets)
1198 if (neighbor == tip_tet)
1203 tip_nodes_shared.push_back(&other_node);
1206 bool node_shared =
false;
1207 for (
const auto & tet_2 : fine_tets)
1213 if (tip_nodes_shared.size() == 3 && unshared_nodes == 1)
1214 tips_tets.insert(neighbor);
1221 fine_elements.clear();
1222 for (
const auto & elem : tips_tets)
1223 fine_elements.insert(elem);
1224 for (
const auto & elem : inside_tets)
1225 fine_elements.insert(elem);
1228 for (
const auto & tip : tips_tets)
1230 for (
const auto & node : tip->node_ref_range())
1232 bool outside =
true;
1234 const auto id = tip->get_node_index(&node);
1235 if (!tip->is_vertex(
id))
1237 for (
const auto & tet : inside_tets)
1242 tentative_coarse_nodes.push_back(&node);
1249 std::sort(tentative_coarse_nodes.begin(), tentative_coarse_nodes.end());
1250 tentative_coarse_nodes.erase(
1251 std::unique(tentative_coarse_nodes.begin(), tentative_coarse_nodes.end()),
1252 tentative_coarse_nodes.end());
1256 if (tentative_coarse_nodes.size() != 4)
1263 ". Skipping detection for this node and all future nodes near only this " 1269 for (
auto elem : fine_elements)
1270 if (elem->type() != elem_type)
1274 for (
const auto & check_node : tentative_coarse_nodes)
1275 if (check_node ==
nullptr)
1279 std::unique_ptr<Elem> parent = Elem::build(Elem::first_order_equivalent_type(elem_type));
1280 auto parent_ptr = mesh_copy->add_elem(parent.release());
1283 for (
auto i :
index_range(tentative_coarse_nodes))
1284 parent_ptr->set_node(i, mesh_copy->node_ptr(tentative_coarse_nodes[i]->id()));
1287 parent_ptr->set_refinement_flag(Elem::REFINE);
1288 parent_ptr->refine(mesh_refiner);
1289 const auto num_children = parent_ptr->n_children();
1297 unsigned int num_children_match = 0;
1298 for (
const auto & child : parent_ptr->child_ref_range())
1300 for (
const auto & potential_children : fine_elements)
1301 if (MooseUtils::absoluteFuzzyEqual(child.vertex_average()(0),
1302 potential_children->vertex_average()(0),
1304 MooseUtils::absoluteFuzzyEqual(child.vertex_average()(1),
1305 potential_children->vertex_average()(1),
1307 MooseUtils::absoluteFuzzyEqual(child.vertex_average()(2),
1308 potential_children->vertex_average()(2),
1311 num_children_match++;
1316 if (num_children_match == num_children ||
1317 ((elem_type ==
TET4 || elem_type ==
TET10 || elem_type ==
TET14) &&
1318 num_children_match == 4))
1320 num_likely_AMR_created_nonconformality++;
1321 if (num_likely_AMR_created_nonconformality <
_num_outputs)
1323 _console <<
"Detected non-conformality likely created by AMR near" << *node
1325 <<
" elements that could be merged into a coarse element:" << std::endl;
1326 for (
const auto & elem : fine_elements)
1330 else if (num_likely_AMR_created_nonconformality ==
_num_outputs)
1331 _console <<
"Maximum log output reached, silencing output" << std::endl;
1336 "Number of non-conformal nodes likely due to mesh refinement detected by heuristic: " +
1339 num_likely_AMR_created_nonconformality);
1340 pl->unset_close_to_point_tol();
1346 unsigned int num_bad_elem_qp_jacobians = 0;
1348 auto qrule_dimension =
mesh->mesh_dimension();
1355 std::unique_ptr<libMesh::FEBase> fe_elem;
1356 if (
mesh->mesh_dimension() == 1)
1358 if (
mesh->mesh_dimension() == 2)
1361 fe_elem = std::make_unique<libMesh::FEMonomial<3>>(fe_type);
1364 fe_elem->attach_quadrature_rule(&qrule);
1367 for (
const auto & elem :
mesh->element_ptr_range())
1370 if (qrule_dimension != elem->dim())
1373 qrule_dimension = elem->dim();
1377 if (elem->dim() == 1)
1379 if (elem->dim() == 2)
1382 fe_elem = std::make_unique<libMesh::FEMonomial<3>>(fe_type);
1385 fe_elem->attach_quadrature_rule(&qrule);
1390 fe_elem->reinit(elem);
1392 catch (std::exception & e)
1394 if (!strstr(e.what(),
"Jacobian"))
1397 num_bad_elem_qp_jacobians++;
1399 _console <<
"Bad Jacobian found in element " << elem->id() <<
" near point " 1400 << elem->vertex_average() << std::endl;
1402 _console <<
"Maximum log output reached, silencing output" << std::endl;
1408 num_bad_elem_qp_jacobians);
1410 unsigned int num_bad_side_qp_jacobians = 0;
1412 auto qrule_side_dimension =
mesh->mesh_dimension() - 1;
1416 fe_elem->attach_quadrature_rule(&qrule_side);
1419 for (
const auto & elem :
mesh->element_ptr_range())
1422 if (
int(qrule_side_dimension) != elem->dim() - 1)
1424 qrule_side_dimension = elem->dim() - 1;
1428 if (elem->dim() == 1)
1430 if (elem->dim() == 2)
1433 fe_elem = std::make_unique<libMesh::FEMonomial<3>>(fe_type);
1436 fe_elem->attach_quadrature_rule(&qrule_side);
1439 for (
const auto & side : elem->side_index_range())
1443 fe_elem->reinit(elem, side);
1445 catch (std::exception & e)
1449 if (!strstr(e.what(),
"Jacobian") && !strstr(e.what(),
"det != 0"))
1452 num_bad_side_qp_jacobians++;
1454 _console <<
"Bad Jacobian found in side " << side <<
" of element" << elem->id()
1455 <<
" near point " << elem->vertex_average() << std::endl;
1457 _console <<
"Maximum log output reached, silencing output" << std::endl;
1464 num_bad_side_qp_jacobians);
1483 if (
mesh->mesh_dimension() != 3)
1485 mooseWarning(
"The edge intersection algorithm only works with 3D meshes. " 1486 "'examine_non_matching_edges' is skipped");
1489 if (!
mesh->is_serial())
1490 mooseError(
"Only serialized/replicated meshes are supported");
1491 unsigned int num_intersecting_edges = 0;
1495 std::unordered_map<Elem *, BoundingBox> bounding_box_map;
1496 for (
const auto elem :
mesh->active_element_ptr_range())
1498 const auto boundingBox = elem->loose_bounding_box();
1499 bounding_box_map.insert({elem, boundingBox});
1502 std::unique_ptr<PointLocatorBase> point_locator =
mesh->sub_point_locator();
1503 std::set<std::array<dof_id_type, 4>> overlapping_edges_nodes;
1504 for (
const auto elem :
mesh->active_element_ptr_range())
1507 std::set<const Elem *> candidate_elems;
1508 std::set<const Elem *> nearby_elems;
1509 for (
unsigned int i = 0; i < elem->n_nodes(); i++)
1511 (*point_locator)(elem->point(i), candidate_elems);
1512 nearby_elems.insert(candidate_elems.begin(), candidate_elems.end());
1514 std::vector<std::unique_ptr<const Elem>> elem_edges(elem->n_edges());
1515 for (
auto i : elem->edge_index_range())
1516 elem_edges[i] = elem->build_edge_ptr(i);
1517 for (
const auto other_elem : nearby_elems)
1520 if (elem->id() >= other_elem->id())
1523 std::vector<std::unique_ptr<const Elem>> other_edges(other_elem->n_edges());
1524 for (
auto j : other_elem->edge_index_range())
1525 other_edges[j] = other_elem->build_edge_ptr(j);
1526 for (
auto & edge : elem_edges)
1528 for (
auto & other_edge : other_edges)
1531 const Node * n1 = edge->get_nodes()[0];
1532 const Node * n2 = edge->get_nodes()[1];
1533 const Node * n3 = other_edge->get_nodes()[0];
1534 const Node * n4 = other_edge->get_nodes()[1];
1537 std::array<dof_id_type, 4> node_id_array = {n1->id(), n2->id(), n3->id(), n4->id()};
1538 std::sort(node_id_array.begin(), node_id_array.end());
1541 if (overlapping_edges_nodes.count(node_id_array))
1547 if (edge->type() !=
EDGE2)
1549 std::string element_message =
"Edge of type " + Utility::enum_to_string(edge->type()) +
1550 " was found in cell " + std::to_string(elem->id()) +
1551 " which is of type " +
1552 Utility::enum_to_string(elem->type()) +
'\n' +
1553 "The edge intersection check only works for EDGE2 " 1554 "elements.\nThis message will not be output again";
1555 mooseDoOnce(
_console << element_message << std::endl);
1558 if (other_edge->type() !=
EDGE2)
1562 Point intersection_coords;
1568 overlapping_edges_nodes.insert(node_id_array);
1569 num_intersecting_edges += 2;
1573 std::string elem_id = std::to_string(elem->id());
1574 std::string other_elem_id = std::to_string(other_elem->id());
1575 std::string x_coord = std::to_string(intersection_coords(0));
1576 std::string y_coord = std::to_string(intersection_coords(1));
1577 std::string z_coord = std::to_string(intersection_coords(2));
1578 std::string message =
"Intersecting edges found between elements " + elem_id +
1579 " and " + other_elem_id +
" near point (" + x_coord +
", " +
1580 y_coord +
", " + z_coord +
")";
1591 num_intersecting_edges);
1597 unsigned int num_polygons = 0;
1598 unsigned int num_nonconvex = 0;
1599 unsigned int num_nonplanar = 0;
1600 unsigned int num_flat_consecutive_sides = 0;
1602 for (
const auto & elem :
mesh->element_ptr_range())
1606 const auto n_nodes = elem->n_nodes();
1607 Point base_top_dir(0, 0, 0);
1608 bool nonconvex =
false;
1609 bool nonplanar =
false;
1612 const auto n1 = elem->point(i);
1613 const auto n2 = elem->point((i + 1) %
n_nodes);
1614 const auto n3 = elem->point((i + 2) %
n_nodes);
1616 Point top_dir = (n2 - n1).cross(n3 - n2);
1618 if (top_dir.norm_sq() > 0 && base_top_dir.norm() == 0)
1620 base_top_dir = top_dir.unit();
1623 if (base_top_dir * top_dir < 0)
1625 if (top_dir.norm_sq() > 0)
1626 top_dir = top_dir.unit();
1628 num_flat_consecutive_sides++;
1629 if (!MooseUtils::absoluteFuzzyEqual((top_dir - base_top_dir).
norm_sq(), 0, TOLERANCE) &&
1630 !MooseUtils::absoluteFuzzyEqual((top_dir + base_top_dir).
norm_sq(), 0, TOLERANCE))
1638 _console <<
"Non convex C0 polygon detected:" << elem->get_info() << std::endl;
1640 _console <<
"Ouptut limit reached for non-convex polygons" << std::endl;
1646 _console <<
"Non planar C0 polygon detected:" << elem->get_info() << std::endl;
1648 _console <<
"Ouptut limit reached for non-planar polygons" << std::endl;
1653 mooseWarning(
"No C0 polygons in geometry: polyon check did nothing");
1662 diagnosticsLog(
"Number of colinear consecutive sides of polygons: " +
1665 num_flat_consecutive_sides);
1672 bool problem_detected)
const 1674 mooseAssert(log_level !=
"NO_CHECK",
1675 "We should not be outputting logs if the check had been disabled");
1676 if (log_level ==
"INFO" || !problem_detected)
1678 else if (log_level ==
"WARNING")
1680 else if (log_level ==
"ERROR")
const Real _non_matching_edge_tol
void mooseInfo(Args &&... args) const
void checkNonMatchingEdges(const std::unique_ptr< MeshBase > &mesh) const
Routine to check for non matching edges.
std::unique_ptr< FEGenericBase< Real > > build(const unsigned int dim, const FEType &fet)
void checkWatertightNodesets(const std::unique_ptr< MeshBase > &mesh) const
const unsigned int invalid_uint
bool hasBoundaryName(const MeshBase &input_mesh, const BoundaryName &name)
Whether a particular boundary name exists in the mesh.
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< MeshBase > & _input
the input mesh to be diagnosed
const MooseEnum _check_sidesets_orientation
whether to check that sidesets are consistently oriented using neighbor subdomains ...
const MooseEnum _check_element_types
whether to check different element types in the same sub-domain
bool checkFirstOrderEdgeOverlap(const Elem &edge1, const Elem &edge2, Point &intersection_point, const Real intersection_tol)
const boundary_id_type side_id
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
const unsigned int _num_outputs
number of logs to output at most for each check
void mooseInfoRepeated(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
void checkPolygons(const std::unique_ptr< MeshBase > &mesh) const
Routine to check for non-convex polygons.
const MooseEnum _check_non_conformal_mesh
whether to check for non-conformal meshes
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< BoundaryID > _watertight_boundaries
IDs of boundaries to be checked in watertight checks.
void checkNonConformalMeshFromAdaptivity(const std::unique_ptr< MeshBase > &mesh) const
Routine to check whether a mesh presents non-conformality born from adaptivity.
MeshDiagnosticsGenerator(const InputParameters ¶meters)
const MooseEnum _check_local_jacobian
whether to check for negative jacobians in the domain
static InputParameters validParams()
bool getFineElementsFromInteriorNode(const libMesh::Node &interior_node, const libMesh::Node &reference_node, const libMesh::Elem &elem, std::vector< const libMesh::Node *> &tentative_coarse_nodes, std::set< const libMesh::Elem *> &fine_elements)
Utility routine to gather vertex nodes for, and elements contained in, for a coarse QUAD or HEX eleme...
auto max(const L &left, const R &right)
const Real _non_conformality_tol
tolerance for detecting when meshes are not conformal
void mooseWarning(Args &&... args) const
registerMooseObject("MooseApp", MeshDiagnosticsGenerator)
const dof_id_type n_nodes
std::vector< BoundaryName > _watertight_boundary_names
Names of boundaries to be checked in watertight checks.
void checkLocalJacobians(const std::unique_ptr< MeshBase > &mesh) const
Routine to check whether the Jacobians (elem and side) are not negative.
void checkNonConformalMesh(const std::unique_ptr< MeshBase > &mesh) const
Routine to check whether a mesh presents non-conformality.
void checkElementTypes(const std::unique_ptr< MeshBase > &mesh) const
Routine to check the element types in each subdomain.
const MooseEnum _check_adaptivity_non_conformality
whether to check for the adaptivity of non-conformal meshes
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
const Real _min_volume
minimum size for element volume to be counted as a tiny element
const std::string & type() const
Get the type of this class.
virtual_for_inffe const std::vector< Real > & get_JxW() const
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
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.
TypeVector< typename CompareTypes< Real, T2 >::supertype > cross(const TypeVector< T2 > &v) const
const Real _max_volume
maximum size for element volume to be counted as a big element
void checkElementOverlap(const std::unique_ptr< MeshBase > &mesh) const
Routine to check whether elements overlap in the mesh.
static InputParameters validParams()
std::string stringify(const T &t)
conversion to string
const MooseEnum _check_element_volumes
whether to check element volumes
void checkElementVolumes(const std::unique_ptr< MeshBase > &mesh) const
Routine to check the element volumes.
const MooseEnum _check_watertight_nodesets
whether to check that each external node is assigned to a nodeset
const MooseEnum _check_non_planar_sides
whether to check for elements in different planes (non_planar)
const MooseEnum _check_non_matching_edges
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void diagnosticsLog(std::string msg, const MooseEnum &log_level, bool problem_detected) const
Utility routine to output the final diagnostics level in the desired mode.
std::vector< boundary_id_type > findBoundaryOverlap(const std::vector< boundary_id_type > &watertight_boundaries, std::vector< boundary_id_type > &boundary_ids) const
Helper function that finds the intersection between the given vectors.
IntRange< T > make_range(T beg, T end)
const MooseEnum _check_watertight_sidesets
whether to check that each external side is assigned to a sideset
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...
void checkSidesetsOrientation(const std::unique_ptr< MeshBase > &mesh) const
Routine to check sideset orientation near subdomains.
const MooseEnum _check_polygons
whether to check for non-convex polygons in the mesh
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
const MooseEnum _check_element_overlap
whether to check for intersecting elements
void reorderNodes(std::vector< const libMesh::Node *> &nodes, const libMesh::Point &origin, const libMesh::Point &clock_start, libMesh::Point &axis)
Utility routine to re-order a vector of nodes so that they can form a valid quad element.
void checkNonConformalMesh(const std::unique_ptr< libMesh::MeshBase > &mesh, const ConsoleStream &console, const unsigned int num_outputs, const Real conformality_tol, unsigned int &num_nonconformal_nodes)
bool isParamSetByUser(const std::string &name) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
void checkNonPlanarSides(const std::unique_ptr< MeshBase > &mesh) const
Routine to check whether there are non-planar sides in the mesh.
MeshGenerators are objects that can modify or add to an existing mesh.
void checkWatertightSidesets(const std::unique_ptr< MeshBase > &mesh) const
void ErrorVector unsigned int
auto index_range(const T &sizable)