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" 35 params.
addRequiredParam<MeshGeneratorName>(
"input",
"The mesh we want to diagnose");
36 params.
addClassDescription(
"Runs a series of diagnostics on the mesh to detect potential issues " 37 "such as unsupported features");
40 MooseEnum chk_option(
"NO_CHECK INFO WARNING ERROR",
"NO_CHECK");
43 "examine_sidesets_orientation",
45 "whether to check that sidesets are consistently oriented using neighbor subdomains. If a " 46 "sideset is inconsistently oriented within a subdomain, this will not be detected");
48 "check_for_watertight_sidesets",
50 "whether to check for external sides that are not assigned to any sidesets");
52 "check_for_watertight_nodesets",
54 "whether to check for external nodes that are not assigned to any nodeset");
55 params.
addParam<std::vector<BoundaryName>>(
56 "boundaries_to_check",
58 "Names boundaries that should form a watertight envelope around the mesh. Defaults to all " 59 "the boundaries combined.");
61 "examine_element_volumes", chk_option,
"whether to examine volume of the elements");
62 params.
addParam<
Real>(
"minimum_element_volumes", 1e-16,
"minimum size for element volume");
63 params.
addParam<
Real>(
"maximum_element_volumes", 1e16,
"Maximum size for element volume");
67 "whether to look for multiple element types in the same sub-domain");
69 "examine_element_overlap", chk_option,
"whether to find overlapping elements");
71 "examine_nonplanar_sides", chk_option,
"whether to check element sides are planar");
74 "whether to examine the conformality of elements in the mesh");
77 "Whether to check if there are any intersecting edges");
78 params.
addParam<
Real>(
"intersection_tol", TOLERANCE,
"tolerence for intersecting edges");
79 params.
addParam<
Real>(
"nonconformal_tol", TOLERANCE,
"tolerance for element non-conformality");
81 "search_for_adaptivity_nonconformality",
83 "whether to check for non-conformality arising from adaptive mesh refinement");
86 "whether to check the local Jacobian for negative values");
90 "How many problematic element/nodes/sides/etc are explicitly reported on by each check");
96 _input(getMesh(
"input")),
97 _check_sidesets_orientation(getParam<
MooseEnum>(
"examine_sidesets_orientation")),
98 _check_watertight_sidesets(getParam<
MooseEnum>(
"check_for_watertight_sidesets")),
99 _check_watertight_nodesets(getParam<
MooseEnum>(
"check_for_watertight_nodesets")),
100 _watertight_boundary_names(getParam<
std::vector<BoundaryName>>(
"boundaries_to_check")),
101 _check_element_volumes(getParam<
MooseEnum>(
"examine_element_volumes")),
102 _min_volume(getParam<
Real>(
"minimum_element_volumes")),
103 _max_volume(getParam<
Real>(
"maximum_element_volumes")),
104 _check_element_types(getParam<
MooseEnum>(
"examine_element_types")),
105 _check_element_overlap(getParam<
MooseEnum>(
"examine_element_overlap")),
106 _check_non_planar_sides(getParam<
MooseEnum>(
"examine_nonplanar_sides")),
107 _check_non_conformal_mesh(getParam<
MooseEnum>(
"examine_non_conformality")),
108 _non_conformality_tol(getParam<
Real>(
"nonconformal_tol")),
109 _check_non_matching_edges(getParam<
MooseEnum>(
"examine_non_matching_edges")),
110 _non_matching_edge_tol(getParam<
Real>(
"intersection_tol")),
111 _check_adaptivity_non_conformality(
112 getParam<
MooseEnum>(
"search_for_adaptivity_nonconformality")),
113 _check_local_jacobian(getParam<
MooseEnum>(
"check_local_jacobian")),
114 _num_outputs(getParam<unsigned
int>(
"log_length_limit"))
121 "You must set this parameter to true to trigger element size checks");
124 "You must set this parameter to true to trigger mesh conformality check");
131 mooseError(
"You need to turn on at least one diagnostic. Did you misspell a parameter?");
134 std::unique_ptr<MeshBase>
137 std::unique_ptr<MeshBase>
mesh = std::move(
_input);
140 if (!
mesh->is_serial())
141 mooseError(
"Only serialized meshes are supported");
145 mesh->prepare_for_use();
151 mooseError(
"User specified boundary_to_check \'", boundary_name,
"\' does not exist");
195 auto & boundary_info =
mesh->get_boundary_info();
196 auto side_tuples = boundary_info.build_side_list();
198 for (
const auto bid : boundary_info.get_boundary_ids())
202 std::set<std::pair<subdomain_id_type, subdomain_id_type>> block_neighbors;
205 if (std::get<2>(side_tuples[index]) != bid)
207 const auto elem_ptr =
mesh->elem_ptr(std::get<0>(side_tuples[index]));
208 if (elem_ptr->neighbor_ptr(std::get<1>(side_tuples[index])))
209 block_neighbors.insert(std::make_pair(
210 elem_ptr->subdomain_id(),
211 elem_ptr->neighbor_ptr(std::get<1>(side_tuples[index]))->subdomain_id()));
215 std::set<std::pair<subdomain_id_type, subdomain_id_type>> flipped_pairs;
216 for (
const auto & block_pair_1 : block_neighbors)
217 for (
const auto & block_pair_2 : block_neighbors)
218 if (block_pair_1 != block_pair_2)
219 if (block_pair_1.first == block_pair_2.second &&
220 block_pair_1.second == block_pair_2.first)
221 flipped_pairs.insert(block_pair_1);
224 const std::string sideset_full_name =
225 boundary_info.sideset_name(bid) +
" (" + std::to_string(bid) +
")";
226 if (!flipped_pairs.empty())
228 std::string block_pairs_string =
"";
229 for (
const auto & pair : flipped_pairs)
230 block_pairs_string +=
231 " [" +
mesh->subdomain_name(pair.first) +
" (" + std::to_string(pair.first) +
"), " +
232 mesh->subdomain_name(pair.second) +
" (" + std::to_string(pair.second) +
")]";
233 message =
"Inconsistent orientation of sideset " + sideset_full_name +
234 " with regards to subdomain pairs" + block_pairs_string;
237 message =
"Sideset " + sideset_full_name +
238 " is consistently oriented with regards to the blocks it neighbors";
245 unsigned int num_normals_flipping = 0;
246 Real steepest_side_angles = 0;
247 for (
const auto & [elem_id,
side_id, side_bid] : side_tuples)
251 const auto & elem_ptr =
mesh->elem_ptr(elem_id);
254 const std::unique_ptr<const Elem> face = elem_ptr->build_side_ptr(
side_id);
255 std::unique_ptr<libMesh::FEBase> fe(
258 fe->attach_quadrature_rule(&qface);
259 const auto & normals = fe->get_normals();
261 mooseAssert(normals.size() == 1,
"We expected only one normal here");
262 const auto & side_normal = normals[0];
265 for (
const auto neighbor : elem_ptr->neighbor_ptr_range())
267 for (
const auto neigh_side_index : neighbor->side_index_range())
270 if (!boundary_info.has_boundary_id(neighbor, neigh_side_index, bid))
277 fe_neighbor->attach_quadrature_rule(&qface);
278 const auto & neigh_normals = fe_neighbor->get_normals();
279 fe_neighbor->reinit(neighbor, neigh_side_index);
280 mooseAssert(neigh_normals.size() == 1,
"We expected only one normal here");
281 const auto & neigh_side_normal = neigh_normals[0];
284 if (neigh_side_normal * side_normal <= 0)
286 num_normals_flipping++;
287 steepest_side_angles =
288 std::max(std::acos(neigh_side_normal * side_normal), steepest_side_angles);
290 _console <<
"Side normals changed by more than pi/2 for sideset " 291 << sideset_full_name <<
" between side " <<
side_id <<
" of element " 292 << elem_ptr->id() <<
" and side " << neigh_side_index
293 <<
" of neighbor element " << neighbor->id() << std::endl;
295 _console <<
"Maximum output reached for sideset normal flipping check. Silencing " 302 if (num_normals_flipping)
303 message =
"Sideset " + sideset_full_name +
304 " has two neighboring sides with a very large angle. Largest angle detected: " +
305 std::to_string(steepest_side_angles) +
" rad (" +
306 std::to_string(steepest_side_angles * 180 /
libMesh::pi) +
" degrees).";
308 message =
"Sideset " + sideset_full_name +
309 " does not appear to have side-to-neighbor-side orientation flips. All neighbor " 310 "sides normal differ by less than pi/2";
326 if (
mesh->mesh_dimension() < 2)
327 mooseError(
"The sideset check only works for 2D and 3D meshes");
328 auto & boundary_info =
mesh->get_boundary_info();
329 boundary_info.build_side_list();
330 const auto sideset_map = boundary_info.get_sideset_map();
331 unsigned int num_faces_without_sideset = 0;
333 for (
const auto elem :
mesh->active_element_ptr_range())
335 for (
auto i : elem->side_index_range())
338 if (elem->neighbor_ptr(i) ==
nullptr)
341 std::vector<boundary_id_type> boundary_ids;
342 auto side_range = sideset_map.equal_range(elem);
343 for (
const auto & itr :
as_range(side_range))
344 if (itr.second.first == i)
345 boundary_ids.push_back(i);
347 std::vector<boundary_id_type> intersections =
350 bool no_specified_ids = boundary_ids.empty();
353 if (
mesh->mesh_dimension() == 3)
354 message =
"Element " + std::to_string(elem->id()) +
355 " contains an external face which has not been assigned to ";
357 message =
"Element " + std::to_string(elem->id()) +
358 " contains an external edge which has not been assigned to ";
359 if (no_specified_ids)
360 message = message +
"a sideset";
361 else if (specified_ids)
362 message = message +
"one of the specified sidesets";
363 if ((no_specified_ids || specified_ids) && num_faces_without_sideset <
_num_outputs)
366 num_faces_without_sideset++;
372 if (
mesh->mesh_dimension() == 3)
373 message =
"Number of external element faces that have not been assigned to a sideset: " +
374 std::to_string(num_faces_without_sideset);
376 message =
"Number of external element edges that have not been assigned to a sideset: " +
377 std::to_string(num_faces_without_sideset);
393 if (
mesh->mesh_dimension() < 2)
394 mooseError(
"The nodeset check only works for 2D and 3D meshes");
395 auto & boundary_info =
mesh->get_boundary_info();
396 unsigned int num_nodes_without_nodeset = 0;
397 std::set<dof_id_type> checked_nodes_id;
399 for (
const auto elem :
mesh->active_element_ptr_range())
401 for (
const auto i : elem->side_index_range())
404 if (elem->neighbor_ptr(i) ==
nullptr)
407 auto side = elem->side_ptr(i);
408 const auto & node_list = side->get_nodes();
409 for (
unsigned int j = 0; j < side->n_nodes(); j++)
411 const auto node = node_list[j];
412 if (checked_nodes_id.count(node->id()))
415 std::vector<boundary_id_type> boundary_ids;
416 boundary_info.boundary_ids(node, boundary_ids);
417 std::vector<boundary_id_type> intersection =
420 bool no_specified_ids = boundary_info.n_boundary_ids(node) == 0;
422 std::string message =
423 "Node " + std::to_string(node->id()) +
424 " is on an external boundary of the mesh, but has not been assigned to ";
425 if (no_specified_ids)
426 message = message +
"a nodeset";
427 else if (specified_ids)
428 message = message +
"one of the specified nodesets";
429 if ((no_specified_ids || specified_ids) && num_nodes_without_nodeset <
_num_outputs)
431 checked_nodes_id.insert(node->id());
432 num_nodes_without_nodeset++;
440 message =
"Number of external nodes that have not been assigned to a nodeset: " +
441 std::to_string(num_nodes_without_nodeset);
445 std::vector<boundary_id_type>
447 const std::vector<boundary_id_type> & watertight_boundaries,
448 std::vector<boundary_id_type> & boundary_ids)
const 452 std::sort(boundary_ids.begin(), boundary_ids.end());
453 std::vector<boundary_id_type> boundary_intersection;
454 std::set_intersection(watertight_boundaries.begin(),
455 watertight_boundaries.end(),
456 boundary_ids.begin(),
458 std::back_inserter(boundary_intersection));
459 return boundary_intersection;
465 unsigned int num_tiny_elems = 0;
466 unsigned int num_negative_elems = 0;
467 unsigned int num_big_elems = 0;
469 for (
auto & elem :
mesh->active_element_ptr_range())
474 _console <<
"Element with volume below threshold detected : \n" 475 <<
"id " << elem->id() <<
" near point " << elem->vertex_average() << std::endl;
477 _console <<
"Maximum output reached, log is silenced" << std::endl;
483 _console <<
"Element with volume above threshold detected : \n" 484 << elem->get_info() << std::endl;
486 _console <<
"Maximum output reached, log is silenced" << std::endl;
490 diagnosticsLog(
"Number of elements below prescribed minimum volume : " +
491 std::to_string(num_tiny_elems),
494 diagnosticsLog(
"Number of elements with negative volume : " + std::to_string(num_negative_elems),
497 diagnosticsLog(
"Number of elements above prescribed maximum volume : " +
498 std::to_string(num_big_elems),
506 std::set<subdomain_id_type> ids;
507 mesh->subdomain_ids(ids);
509 for (
auto &
id : ids)
512 std::set<ElemType> types;
514 for (
auto & elem :
mesh->active_subdomain_elements_ptr_range(
id))
515 types.insert(elem->type());
517 std::string elem_type_names =
"";
518 for (
auto & elem_type : types)
521 _console <<
"Element type in subdomain " +
mesh->subdomain_name(
id) +
" (" +
522 std::to_string(
id) +
") :" + elem_type_names
524 if (types.size() > 1)
525 diagnosticsLog(
"Two different element types in subdomain " + std::to_string(
id),
535 unsigned int num_elem_overlaps = 0;
536 auto pl =
mesh->sub_point_locator();
538 for (
auto & node :
mesh->node_ptr_range())
541 std::set<const Elem *> elements;
542 (*pl)(*node, elements);
544 for (
auto & elem : elements)
546 if (!elem->contains_point(*node))
551 for (
auto & elem_node : elem->node_ref_range())
552 if (*node == elem_node)
558 bool on_a_side =
false;
559 for (
const auto & elem_side_index : elem->side_index_range())
562 if (!found && !on_a_side)
566 _console <<
"Element overlap detected at : " << *node << std::endl;
568 _console <<
"Maximum output reached, log is silenced" << std::endl;
573 diagnosticsLog(
"Number of elements overlapping (node-based heuristics): " +
577 num_elem_overlaps = 0;
580 for (
auto & elem :
mesh->active_element_ptr_range())
583 std::set<const Elem *> overlaps;
584 (*pl)(elem->vertex_average(), overlaps);
586 if (overlaps.size() > 1)
590 _console <<
"Element overlap detected with element : " << elem->id() <<
" near point " 591 << elem->vertex_average() << std::endl;
593 _console <<
"Maximum output reached, log is silenced" << std::endl;
596 diagnosticsLog(
"Number of elements overlapping (centroid-based heuristics): " +
606 unsigned int sides_non_planar = 0;
608 for (
auto & elem :
mesh->active_element_ptr_range())
612 auto side = elem->side_ptr(i);
613 std::vector<const Point *> nodes;
614 for (
auto & node : side->node_ref_range())
615 nodes.emplace_back(&node);
617 if (nodes.size() <= 3)
625 unsigned int third_node_index = 2;
627 while (aligned && third_node_index < nodes.size())
629 v2 = *nodes[0] - *nodes[third_node_index++];
637 bool found_non_planar =
false;
639 for (
auto node_offset :
make_range(nodes.size() - 3))
644 found_non_planar =
true;
647 if (found_non_planar)
651 _console <<
"Nonplanar side detected for side " << i
652 <<
" of element :" << elem->get_info() << std::endl;
654 _console <<
"Maximum output reached, log is silenced" << std::endl;
667 unsigned int num_nonconformal_nodes = 0;
672 num_nonconformal_nodes);
677 const std::unique_ptr<MeshBase> & mesh)
const 679 unsigned int num_likely_AMR_created_nonconformality = 0;
680 auto pl =
mesh->sub_point_locator();
686 auto mesh_copy =
mesh->clone();
690 for (
auto & node :
mesh->node_ptr_range())
693 std::set<const Elem *> elements_around;
694 (*pl)(*node, elements_around);
697 std::set<const Elem *> fine_elements;
698 std::set<const Elem *> coarse_elements;
701 for (
auto elem : elements_around)
705 bool node_on_elem =
false;
711 if (!elem->is_vertex(elem->get_node_index(node)))
718 fine_elements.insert(elem);
722 coarse_elements.insert(elem);
729 if (fine_elements.size() == elements_around.size())
732 if (fine_elements.empty())
739 const auto elem_type = (*fine_elements.begin())->
type();
740 if ((elem_type ==
QUAD4 || elem_type ==
QUAD8 || elem_type ==
QUAD9) &&
741 fine_elements.size() != 2)
743 else if ((elem_type ==
HEX8 || elem_type ==
HEX20 || elem_type ==
HEX27) &&
744 fine_elements.size() != 4)
746 else if ((elem_type ==
TRI3 || elem_type ==
TRI6 || elem_type ==
TRI7) &&
747 fine_elements.size() != 3)
749 else if ((elem_type ==
TET4 || elem_type ==
TET10 || elem_type ==
TET14) &&
750 (fine_elements.size() % 2 != 0))
757 if (elem_type !=
TET4 && elem_type !=
TET10 && elem_type !=
TET14 && coarse_elements.size() > 1)
764 std::vector<const Node *> tentative_coarse_nodes;
771 const auto elem = *fine_elements.begin();
774 std::vector<Elem *> node_on_sides;
778 const auto side = elem->side_ptr(i);
779 std::vector<const Node *> other_nodes_on_side;
780 bool node_on_side =
false;
781 for (
const auto & elem_node : side->node_ref_range())
783 if (*node == elem_node)
786 other_nodes_on_side.emplace_back(&elem_node);
794 bool all_side_nodes_are_shared =
true;
795 for (
const auto & other_node : other_nodes_on_side)
797 bool shared_with_a_fine_elem =
false;
798 for (
const auto & other_elem : fine_elements)
799 if (other_elem != elem &&
801 shared_with_a_fine_elem =
true;
803 if (!shared_with_a_fine_elem)
804 all_side_nodes_are_shared =
false;
806 if (all_side_nodes_are_shared)
808 side_inside_parent = i;
821 const auto interior_side = elem->side_ptr(side_inside_parent);
822 const Node * interior_node =
nullptr;
823 for (
const auto & other_node : interior_side->node_ref_range())
825 if (other_node == *node)
827 bool in_all_node_neighbor_elements =
true;
828 for (
auto other_elem : fine_elements)
831 in_all_node_neighbor_elements =
false;
833 if (in_all_node_neighbor_elements)
835 interior_node = &other_node;
844 std::set<const Elem *> all_elements;
845 elem->find_point_neighbors(*interior_node, all_elements);
850 *interior_node, *node, *elem, tentative_coarse_nodes, fine_elements);
860 const auto & coarse_elem = *coarse_elements.begin();
861 unsigned short coarse_side_i = 0;
862 for (
const auto & coarse_side_index : coarse_elem->side_index_range())
864 const auto coarse_side_ptr = coarse_elem->side_ptr(coarse_side_index);
870 coarse_side_i = coarse_side_index;
874 const auto coarse_side = coarse_elem->side_ptr(coarse_side_i);
887 tentative_coarse_nodes.resize(4);
888 for (
const auto & elem_1 : fine_elements)
889 for (
const auto & coarse_node : elem_1->node_ref_range())
891 bool node_shared =
false;
892 for (
const auto & elem_2 : fine_elements)
894 if (elem_2 != elem_1)
902 elem_1->is_vertex(elem_1->get_node_index(&coarse_node)))
903 tentative_coarse_nodes[i++] = &coarse_node;
904 mooseAssert(i <= 5,
"We went too far in this index");
914 Point axis = *interior_node - *node;
915 const auto start_circle = elem->vertex_average();
917 tentative_coarse_nodes, *interior_node, start_circle, axis);
918 tentative_coarse_nodes.resize(8);
922 for (
const auto & elem : fine_elements)
925 unsigned int node_index = 0;
926 for (
const auto & coarse_node : tentative_coarse_nodes)
934 for (
const auto & neighbor : elem->neighbor_ptr_range())
935 if (all_elements.count(neighbor) && !fine_elements.count(neighbor))
938 const Node * coarse_elem_node =
nullptr;
939 for (
const auto & fine_node : neighbor->node_ref_range())
941 if (!neighbor->is_vertex(neighbor->get_node_index(&fine_node)))
943 bool node_shared =
false;
944 for (
const auto & elem_2 : all_elements)
945 if (elem_2 != neighbor &&
950 coarse_elem_node = &fine_node;
955 tentative_coarse_nodes[node_index + 4] = coarse_elem_node;
956 mooseAssert(node_index + 4 < tentative_coarse_nodes.size(),
"Indexed too far");
957 mooseAssert(coarse_elem_node,
"Did not find last coarse element node");
963 fine_elements = all_elements;
967 else if (elem_type ==
TRI3 || elem_type ==
TRI6 || elem_type ==
TRI7)
972 const Elem * center_elem =
nullptr;
973 for (
const auto refined_elem_1 : fine_elements)
975 unsigned int num_neighbors = 0;
976 for (
const auto refined_elem_2 : fine_elements)
978 if (refined_elem_1 == refined_elem_2)
980 if (refined_elem_1->has_neighbor(refined_elem_2))
983 if (num_neighbors >= 2)
984 center_elem = refined_elem_1;
990 for (
const auto refined_elem : fine_elements)
992 if (refined_elem == center_elem)
994 for (
const auto & other_node : refined_elem->node_ref_range())
996 refined_elem->is_vertex(refined_elem->get_node_index(&other_node)))
997 tentative_coarse_nodes.push_back(&other_node);
1003 for (
auto side_index : center_elem->side_index_range())
1005 center_side_opposite_node = side_index;
1006 const auto neighbor_on_other_side_of_opposite_center_side =
1007 center_elem->neighbor_ptr(center_side_opposite_node);
1010 if (!neighbor_on_other_side_of_opposite_center_side)
1013 fine_elements.insert(neighbor_on_other_side_of_opposite_center_side);
1014 for (
const auto & tri_node : neighbor_on_other_side_of_opposite_center_side->node_ref_range())
1015 if (neighbor_on_other_side_of_opposite_center_side->is_vertex(
1016 neighbor_on_other_side_of_opposite_center_side->get_node_index(&tri_node)) &&
1017 center_elem->side_ptr(center_side_opposite_node)->get_node_index(&tri_node) ==
1019 tentative_coarse_nodes.push_back(&tri_node);
1022 "Did not find the side opposite the non-conformality");
1023 mooseAssert(tentative_coarse_nodes.size() == 3,
1024 "We are forming a coarsened triangle element");
1028 else if (elem_type ==
TET4 || elem_type ==
TET10 || elem_type ==
TET14)
1032 std::set<const Elem *> tips_tets;
1033 std::set<const Elem *> inside_tets;
1036 const Elem * coarse_elem =
nullptr;
1037 std::set<const Elem *> fine_tets;
1038 for (
auto & coarse_one : coarse_elements)
1040 for (
const auto & elem : fine_elements)
1043 if (elem->has_neighbor(coarse_one))
1044 fine_tets.insert(elem);
1046 if (fine_tets.size())
1048 coarse_elem = coarse_one;
1057 for (
const auto & elem : fine_elements)
1059 int num_face_neighbors = 0;
1060 for (
const auto & tet : fine_tets)
1061 if (tet->has_neighbor(elem))
1062 num_face_neighbors++;
1063 if (num_face_neighbors == 2)
1065 fine_tets.insert(elem);
1073 std::set<const Node *> other_nodes;
1074 for (
const auto & tet_1 : fine_tets)
1076 for (
const auto & node_1 : tet_1->node_ref_range())
1078 if (&node_1 == node)
1080 if (!tet_1->is_vertex(tet_1->get_node_index(&node_1)))
1082 for (
const auto & tet_2 : fine_tets)
1089 other_nodes.insert(&node_1);
1093 mooseAssert(other_nodes.size() == 2,
1094 "Should find only two extra non-conformal nodes near the coarse element");
1097 for (
const auto & tet_1 : fine_tets)
1099 for (
const auto & neighbor : tet_1->neighbor_ptr_range())
1101 neighbor->is_vertex(neighbor->get_node_index(*other_nodes.begin())) &&
1103 neighbor->is_vertex(neighbor->get_node_index(*other_nodes.rbegin())))
1104 fine_tets.insert(neighbor);
1107 for (
const auto & tet_1 : fine_tets)
1109 for (
const auto & neighbor : tet_1->neighbor_ptr_range())
1111 neighbor->is_vertex(neighbor->get_node_index(*other_nodes.begin())) &&
1113 neighbor->is_vertex(neighbor->get_node_index(*other_nodes.rbegin())))
1114 fine_tets.insert(neighbor);
1118 for (
const auto & tet_1 : fine_tets)
1119 for (
const auto & neighbor : tet_1->neighbor_ptr_range())
1120 for (
const auto & tet_2 : fine_tets)
1121 if (tet_1 != tet_2 && tet_2->has_neighbor(neighbor) && neighbor != coarse_elem)
1122 fine_tets.insert(neighbor);
1125 for (
const auto & tet_1 : fine_tets)
1127 unsigned int unshared_nodes = 0;
1128 for (
const auto & other_node : tet_1->node_ref_range())
1130 if (!tet_1->is_vertex(tet_1->get_node_index(&other_node)))
1132 bool node_shared =
false;
1133 for (
const auto & tet_2 : fine_tets)
1139 if (unshared_nodes == 1)
1140 tips_tets.insert(tet_1);
1141 else if (unshared_nodes == 0)
1142 inside_tets.insert(tet_1);
1144 mooseError(
"Did not expect a tet to have two unshared vertex nodes here");
1151 for (
const auto & tet : inside_tets)
1153 for (
const auto & neighbor : tet->neighbor_ptr_range())
1156 bool shared_with_another_tet =
false;
1157 for (
const auto & tet_2 : fine_tets)
1161 if (tet_2->has_neighbor(neighbor))
1162 shared_with_another_tet =
true;
1164 if (shared_with_another_tet)
1168 std::vector<const Node *> tip_nodes_shared;
1169 unsigned int unshared_nodes = 0;
1170 for (
const auto & other_node : neighbor->node_ref_range())
1172 if (!neighbor->is_vertex(neighbor->get_node_index(&other_node)))
1176 for (
const auto & tip_tet : tips_tets)
1178 if (neighbor == tip_tet)
1183 tip_nodes_shared.push_back(&other_node);
1186 bool node_shared =
false;
1187 for (
const auto & tet_2 : fine_tets)
1193 if (tip_nodes_shared.size() == 3 && unshared_nodes == 1)
1194 tips_tets.insert(neighbor);
1201 fine_elements.clear();
1202 for (
const auto & elem : tips_tets)
1203 fine_elements.insert(elem);
1204 for (
const auto & elem : inside_tets)
1205 fine_elements.insert(elem);
1208 for (
const auto & tip : tips_tets)
1210 for (
const auto & node : tip->node_ref_range())
1212 bool outside =
true;
1214 const auto id = tip->get_node_index(&node);
1215 if (!tip->is_vertex(
id))
1217 for (
const auto & tet : inside_tets)
1222 tentative_coarse_nodes.push_back(&node);
1229 std::sort(tentative_coarse_nodes.begin(), tentative_coarse_nodes.end());
1230 tentative_coarse_nodes.erase(
1231 std::unique(tentative_coarse_nodes.begin(), tentative_coarse_nodes.end()),
1232 tentative_coarse_nodes.end());
1236 if (tentative_coarse_nodes.size() != 4)
1243 ". Skipping detection for this node and all future nodes near only this " 1249 for (
auto elem : fine_elements)
1250 if (elem->type() != elem_type)
1254 for (
const auto & check_node : tentative_coarse_nodes)
1255 if (check_node ==
nullptr)
1259 std::unique_ptr<Elem> parent = Elem::build(Elem::first_order_equivalent_type(elem_type));
1260 auto parent_ptr = mesh_copy->add_elem(parent.release());
1263 for (
auto i :
index_range(tentative_coarse_nodes))
1264 parent_ptr->set_node(i, mesh_copy->node_ptr(tentative_coarse_nodes[i]->id()));
1267 parent_ptr->set_refinement_flag(Elem::REFINE);
1268 parent_ptr->refine(mesh_refiner);
1269 const auto num_children = parent_ptr->n_children();
1277 unsigned int num_children_match = 0;
1278 for (
const auto & child : parent_ptr->child_ref_range())
1280 for (
const auto & potential_children : fine_elements)
1282 potential_children->vertex_average()(0),
1285 potential_children->vertex_average()(1),
1288 potential_children->vertex_average()(2),
1291 num_children_match++;
1296 if (num_children_match == num_children ||
1297 ((elem_type ==
TET4 || elem_type ==
TET10 || elem_type ==
TET14) &&
1298 num_children_match == 4))
1300 num_likely_AMR_created_nonconformality++;
1301 if (num_likely_AMR_created_nonconformality <
_num_outputs)
1303 _console <<
"Detected non-conformality likely created by AMR near" << *node
1305 <<
" elements that could be merged into a coarse element:" << std::endl;
1306 for (
const auto & elem : fine_elements)
1310 else if (num_likely_AMR_created_nonconformality ==
_num_outputs)
1311 _console <<
"Maximum log output reached, silencing output" << std::endl;
1316 "Number of non-conformal nodes likely due to mesh refinement detected by heuristic: " +
1319 num_likely_AMR_created_nonconformality);
1320 pl->unset_close_to_point_tol();
1326 unsigned int num_negative_elem_qp_jacobians = 0;
1328 auto qrule_dimension =
mesh->mesh_dimension();
1335 std::unique_ptr<libMesh::FEBase> fe_elem;
1336 if (
mesh->mesh_dimension() == 1)
1338 if (
mesh->mesh_dimension() == 2)
1341 fe_elem = std::make_unique<libMesh::FEMonomial<3>>(fe_type);
1344 fe_elem->attach_quadrature_rule(&qrule);
1347 for (
const auto & elem :
mesh->element_ptr_range())
1350 if (qrule_dimension != elem->dim())
1353 qrule_dimension = elem->dim();
1357 if (elem->dim() == 1)
1359 if (elem->dim() == 2)
1362 fe_elem = std::make_unique<libMesh::FEMonomial<3>>(fe_type);
1365 fe_elem->attach_quadrature_rule(&qrule);
1370 fe_elem->reinit(elem);
1374 num_negative_elem_qp_jacobians++;
1375 const auto msg = std::string(e.what());
1376 if (msg.find(
"negative Jacobian") != std::string::npos)
1379 _console <<
"Negative Jacobian found in element " << elem->id() <<
" near point " 1380 << elem->vertex_average() << std::endl;
1381 else if (num_negative_elem_qp_jacobians ==
_num_outputs)
1382 _console <<
"Maximum log output reached, silencing output" << std::endl;
1391 num_negative_elem_qp_jacobians);
1393 unsigned int num_negative_side_qp_jacobians = 0;
1395 auto qrule_side_dimension =
mesh->mesh_dimension() - 1;
1399 fe_elem->attach_quadrature_rule(&qrule_side);
1402 for (
const auto & elem :
mesh->element_ptr_range())
1405 if (
int(qrule_side_dimension) != elem->dim() - 1)
1407 qrule_side_dimension = elem->dim() - 1;
1411 if (elem->dim() == 1)
1413 if (elem->dim() == 2)
1416 fe_elem = std::make_unique<libMesh::FEMonomial<3>>(fe_type);
1419 fe_elem->attach_quadrature_rule(&qrule_side);
1422 for (
const auto & side : elem->side_index_range())
1426 fe_elem->reinit(elem, side);
1430 const auto msg = std::string(e.what());
1431 if (msg.find(
"negative Jacobian") != std::string::npos)
1433 num_negative_side_qp_jacobians++;
1435 _console <<
"Negative Jacobian found in side " << side <<
" of element" << elem->id()
1436 <<
" near point " << elem->vertex_average() << std::endl;
1437 else if (num_negative_side_qp_jacobians ==
_num_outputs)
1438 _console <<
"Maximum log output reached, silencing output" << std::endl;
1445 diagnosticsLog(
"Number of element sides with negative Jacobians: " +
1448 num_negative_side_qp_jacobians);
1467 if (
mesh->mesh_dimension() != 3)
1469 mooseWarning(
"The edge intersection algorithm only works with 3D meshes. " 1470 "'examine_non_matching_edges' is skipped");
1473 if (!
mesh->is_serial())
1474 mooseError(
"Only serialized/replicated meshes are supported");
1475 unsigned int num_intersecting_edges = 0;
1479 std::unordered_map<Elem *, BoundingBox> bounding_box_map;
1480 for (
const auto elem :
mesh->active_element_ptr_range())
1482 const auto boundingBox = elem->loose_bounding_box();
1483 bounding_box_map.insert({elem, boundingBox});
1486 std::unique_ptr<PointLocatorBase> point_locator =
mesh->sub_point_locator();
1487 std::set<std::array<dof_id_type, 4>> overlapping_edges_nodes;
1488 for (
const auto elem :
mesh->active_element_ptr_range())
1491 std::set<const Elem *> candidate_elems;
1492 std::set<const Elem *> nearby_elems;
1493 for (
unsigned int i = 0; i < elem->n_nodes(); i++)
1495 (*point_locator)(elem->point(i), candidate_elems);
1496 nearby_elems.insert(candidate_elems.begin(), candidate_elems.end());
1498 std::vector<std::unique_ptr<const Elem>> elem_edges(elem->n_edges());
1499 for (
auto i : elem->edge_index_range())
1500 elem_edges[i] = elem->build_edge_ptr(i);
1501 for (
const auto other_elem : nearby_elems)
1504 if (elem->id() >= other_elem->id())
1507 std::vector<std::unique_ptr<const Elem>> other_edges(other_elem->n_edges());
1508 for (
auto j : other_elem->edge_index_range())
1509 other_edges[j] = other_elem->build_edge_ptr(j);
1510 for (
auto & edge : elem_edges)
1512 for (
auto & other_edge : other_edges)
1515 const Node * n1 = edge->get_nodes()[0];
1516 const Node * n2 = edge->get_nodes()[1];
1517 const Node * n3 = other_edge->get_nodes()[0];
1518 const Node * n4 = other_edge->get_nodes()[1];
1521 std::array<dof_id_type, 4> node_id_array = {n1->id(), n2->id(), n3->id(), n4->id()};
1522 std::sort(node_id_array.begin(), node_id_array.end());
1525 if (overlapping_edges_nodes.count(node_id_array))
1531 if (edge->type() !=
EDGE2)
1533 std::string element_message =
"Edge of type " + Utility::enum_to_string(edge->type()) +
1534 " was found in cell " + std::to_string(elem->id()) +
1535 " which is of type " +
1536 Utility::enum_to_string(elem->type()) +
'\n' +
1537 "The edge intersection check only works for EDGE2 " 1538 "elements.\nThis message will not be output again";
1539 mooseDoOnce(
_console << element_message << std::endl);
1542 if (other_edge->type() !=
EDGE2)
1546 Point intersection_coords;
1552 overlapping_edges_nodes.insert(node_id_array);
1553 num_intersecting_edges += 2;
1557 std::string elem_id = std::to_string(elem->id());
1558 std::string other_elem_id = std::to_string(other_elem->id());
1559 std::string x_coord = std::to_string(intersection_coords(0));
1560 std::string y_coord = std::to_string(intersection_coords(1));
1561 std::string z_coord = std::to_string(intersection_coords(2));
1562 std::string message =
"Intersecting edges found between elements " + elem_id +
1563 " and " + other_elem_id +
" near point (" + x_coord +
", " +
1564 y_coord +
", " + z_coord +
")";
1575 num_intersecting_edges);
1581 bool problem_detected)
const 1583 mooseAssert(log_level !=
"NO_CHECK",
1584 "We should not be outputting logs if the check had been disabled");
1585 if (log_level ==
"INFO" || !problem_detected)
1587 else if (log_level ==
"WARNING")
1589 else if (log_level ==
"ERROR")
const Real _non_matching_edge_tol
void checkNonMatchingEdges(const std::unique_ptr< MeshBase > &mesh) const
std::unique_ptr< FEGenericBase< Real > > build(const unsigned int dim, const FEType &fet)
void checkWatertightNodesets(const std::unique_ptr< MeshBase > &mesh) const
auto norm() const -> decltype(std::norm(Real()))
bool absoluteFuzzyEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
const unsigned int invalid_uint
bool hasBoundaryName(const MeshBase &input_mesh, const BoundaryName &name)
Whether a particular boundary name exists in the mesh.
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 ...
void mooseInfo(Args &&... args) const
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.
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...
void mooseWarning(Args &&... args) const
Emits a warning prefixed with object name and type.
auto max(const L &left, const R &right)
const Real _non_conformality_tol
tolerance for detecting when meshes are not conformal
registerMooseObject("MooseApp", MeshDiagnosticsGenerator)
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.
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 ...
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)
bool isParamSetByUser(const std::string &nm) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
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.
void checkSidesetsOrientation(const std::unique_ptr< MeshBase > &mesh) const
Routine to check sideset orientation near subdomains.
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)
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)