413 {
414 LOG_UNIT_TEST;
415
416 if (multiple_subdomains && tangle_mesh)
417 libmesh_not_implemented_msg(
418 "Arbitrary mesh tangling with multiple subdomains is not supported.");
419
420
422 const auto dim = ref_elem->dim();
427
428
429
431 const auto side_length = 1.0 / n_elems_per_side;
432
434 {
435 case 1:
437 break;
438 case 2:
440 mesh, n_elems_per_side, n_elems_per_side, 0., 1., 0., 1., type);
441 break;
442
443 case 3:
445 n_elems_per_side,
446 n_elems_per_side,
447 n_elems_per_side,
448 0.,
449 1.,
450 0.,
451 1.,
452 0.,
453 1.,
454 type);
455 break;
456
457 default:
458 libmesh_error_msg(
"Unsupported dimension " <<
dim);
459 }
460
461
462 DistortHyperCube dh(
dim);
464
467
468 if (tangle_mesh)
469 {
470
471
472
473
474
475
476
480
481
482
483
484
485 std::map<dof_id_type, Real> dist1_map;
486
487 std::map<dof_id_type, Real> dist2_map;
488
489 for (
const auto * node :
mesh.local_node_ptr_range())
490 {
491 dist1_map[node->id()] = (*node - p1).
norm();
492 dist2_map[node->id()] = (*node - p2).
norm();
493 }
494
495
496
497
498 auto get_closet_point_accross_all_procs =
499 [&
mesh, &proc_id](
const std::map<dof_id_type, Real> &dist_map) {
500
502 Real d_min_local = std::numeric_limits<Real>::max();
504
505
506
507 if (dist_map.size())
508 {
509
510 auto min_it =
511 std::min_element(dist_map.begin(), dist_map.end(),
512 [](
const auto &a,
const auto &
b) {
513 return a.second < b.second;
514 });
515
516 node_id = min_it->first;
517 d_min_local = min_it->second;
518 }
519
520
521 auto d_min_global = d_min_local;
523
524
525
527 if (d_min_local == d_min_global) {
528 broadcasting_proc = proc_id;
530 }
531
533
536
537 return std::pair(node_id, node);
538 };
539
540 const auto [node_id1, node1] =
541 get_closet_point_accross_all_procs(dist1_map);
542 const auto [node_id2, node2] =
543 get_closet_point_accross_all_procs(dist2_map);
544
545
546 const auto displacement = tangle_damping_factor * (node1 - node2);
551
554 mesh.nodes_end(), sync_object);
555
556
557
560 CPPUNIT_ASSERT(unsmoothed_info.mesh_is_tangled);
561 }
562
563
564 std::unordered_map<subdomain_id_type, Real> distorted_subdomain_volumes;
566 if (multiple_subdomains)
567 {
568
569 for (
auto * elem :
mesh.active_element_ptr_range())
570 {
571 unsigned int subdomain_id = 0;
573 if (elem->vertex_average()(d) > 0.5)
574 ++subdomain_id;
575 elem->subdomain_id() += subdomain_id;
576 }
577
578
579
580
581 for (
auto *elem :
mesh.active_element_ptr_range()) {
582 const auto sub_id = elem->subdomain_id();
583
584 if (elem->processor_id() != proc_id)
585 continue;
586 distorted_subdomain_volumes[sub_id] += elem->volume();
587 highest_subdomain_id = std::max(sub_id, highest_subdomain_id);
588 }
589
590
592 for (
const auto sub_id :
make_range(highest_subdomain_id + 1)) {
593
594 if (distorted_subdomain_volumes.find(sub_id) ==
595 distorted_subdomain_volumes.end())
596 distorted_subdomain_volumes[sub_id] = 0.;
597
598 mesh.
comm().
sum(distorted_subdomain_volumes[sub_id]);
599 }
600 }
601
602
603
604
605
606
608
609
611 libmesh_error_msg_if(elem_orders.size() != 1,
612 "The variational smoother cannot be used for mixed-order meshes!");
613
614
615
616
617 const auto scale_factor = *elem_orders.begin() * ((type_is_pyramid || type_is_tet) ? 2 * 4 : 1);
618
619
620 auto node_distortion_is = [&n_elems_per_side, &
dim, &boundary_info, &scale_factor, &type_is_prism](
622
623 std::vector<boundary_id_type> boundary_ids;
624 boundary_info.boundary_ids(&node, boundary_ids);
625
626
627 const auto num_dofs =
dim - boundary_ids.size();
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647 std::size_t num_zero_or_one = 0;
648
649 bool distorted = false;
651 {
652 const Real r = node(d);
653 const Real R = r * n_elems_per_side * scale_factor;
654 CPPUNIT_ASSERT_GREATER(-distortion_tol * distortion_tol, r);
655 CPPUNIT_ASSERT_GREATER(-distortion_tol * distortion_tol, 1 - r);
656
657 bool d_distorted = std::abs(R - std::round(R)) > distortion_tol;
658 if (type_is_prism && (scale_factor == 3))
659 {
660
661
662
663 const Real R_prism = R / scale_factor * 2;
664 const bool d_distorted_prism =
665 std::abs(R_prism - std::round(R_prism)) > distortion_tol;
666 d_distorted &= d_distorted_prism;
667 }
668 distorted |= d_distorted;
670 }
671
672 CPPUNIT_ASSERT_GREATEREQUAL(
dim - num_dofs, num_zero_or_one);
673
674
675 if (num_dofs == 0)
676
677 return true;
678 return distorted == distortion;
679 };
680
681
682 for (
auto node :
mesh.node_ptr_range())
683 CPPUNIT_ASSERT(node_distortion_is(*node, true));
684
685
686
687
688 if (type_is_tri)
689 {
690
691
692
693 SquareToParallelogram stp;
695 }
696 else if (type_is_prism)
697 {
698
699
700
701 CubeToParallelepiped ctp;
703 }
704
706
707 if (type_is_tri)
708 {
709
710
711
712 ParallelogramToSquare pts;
714 }
715 else if (type_is_prism)
716 {
717
718
719
720 ParallelepipedToCube ptc;
722 }
723
724 if (multiple_subdomains)
725 {
726
727
728
729 std::unordered_map<subdomain_id_type, Real> smoothed_subdomain_volumes;
730 for (
auto *elem :
mesh.active_element_ptr_range()) {
731
732 if (elem->processor_id() != proc_id)
733 continue;
734 smoothed_subdomain_volumes[elem->subdomain_id()] += elem->volume();
735 }
736
737
738 for (
const auto sub_id :
make_range(highest_subdomain_id + 1)) {
739
740 if (smoothed_subdomain_volumes.find(sub_id) ==
741 smoothed_subdomain_volumes.end())
742 smoothed_subdomain_volumes[sub_id] = 0.;
743
744 mesh.
comm().
sum(smoothed_subdomain_volumes[sub_id]);
745 }
746
747 for (
const auto sub_id :
make_range(highest_subdomain_id + 1))
749 libmesh_map_find(distorted_subdomain_volumes, sub_id),
750 libmesh_map_find(smoothed_subdomain_volumes, sub_id),
TOLERANCE));
751 }
752 else
753 {
754 std::unordered_map<dof_id_type, std::vector<const Elem *>> nodes_to_elem_map;
756
757
758
759 std::set<dof_id_type> nodes_checked;
761
762 for (
const auto * elem :
mesh.active_element_ptr_range())
763 {
765 {
766 const auto & node = elem->node_ref(local_node_id);
767 if (nodes_checked.find(node.id()) != nodes_checked.end())
768 continue;
769
770 nodes_checked.insert(node.id());
771
772
773
774 if (type_is_pyramid)
775 {
776 if (local_node_id > 8 && local_node_id < 13)
777 {
778
779
780
781
782
783
784
785
786
787
788 const auto & base = elem->node_ref(local_node_id - 9);
789 const auto & apex = elem->node_ref(4);
790 const Real x = (type ==
PYRAMID18) ? 0.56646084 : 0.54985875;
791
792 CPPUNIT_ASSERT(node.absolute_fuzzy_equals(base + x * (apex - base), tol));
793 continue;
794 }
795 else if (local_node_id > 13)
796 {
797
798
799
800
801
802
803
804
805 const auto & base1 = elem->node_ref(local_node_id - 14);
806 const auto & base2 = elem->node_ref((local_node_id - 13) % 4);
807 const auto & apex = elem->node_ref(4);
808
809 const auto node_approx =
810 (0.31401599 * base1 + 0.31401599 * base2 + 0.37196802 * apex);
811 CPPUNIT_ASSERT(node.absolute_fuzzy_equals(node_approx, tol));
812 continue;
813 }
814 }
815
816
817
818 else if (type_is_tet && !elem->is_vertex(local_node_id))
819 {
820
821
822
823 std::vector<const Node *> neighbors;
825 mesh, node, nodes_to_elem_map, neighbors);
826
827 switch (neighbors.size())
828 {
829 case 2: {
830
831
832
833
834 const auto is_0_cube_center =
836 const auto is_1_cube_center =
839
840 if (is_0_cube_center || is_1_cube_center)
841 {
842 const auto & cube_center =
843 is_0_cube_center ? *neighbors[0] : *neighbors[1];
844 const auto & other =
845 is_0_cube_center ? *neighbors[1] : *neighbors[0];
846
847
848
849
851 {
852 const Real x = (type ==
TET10) ? 0.42895041 : 0.41486385;
853 CPPUNIT_ASSERT(node.absolute_fuzzy_equals(
854 other + x * (cube_center - other), tol));
855 }
856
858 {
859 const Real x = (type ==
TET10) ? 0.55388920 : 0.58093516;
860 CPPUNIT_ASSERT(node.absolute_fuzzy_equals(
861 other + x * (cube_center - other), tol));
862 }
863 }
864
865 else
866 {
867
868
869
870 const auto is_0_cube_vertex =
872 const auto is_1_cube_vertex =
874 const auto is_0_cube_face_center =
876 const auto is_1_cube_face_center =
878
879 if (is_0_cube_vertex && is_1_cube_vertex)
880
881
882
883 CPPUNIT_ASSERT(node_distortion_is(node, false));
884
885 else
886 {
887 libmesh_error_msg_if(
888 (is_0_cube_center || is_0_cube_face_center) &&
889 (is_1_cube_center || is_1_cube_face_center),
890 "We should never get here!");
891 const auto & cube_vertex =
892 is_0_cube_vertex ? *neighbors[0] : *neighbors[1];
893 const auto & cube_face_center =
894 is_0_cube_face_center ? *neighbors[0] : *neighbors[1];
895 const Real x = (type ==
TET10) ? 0.61299101 : 0.65125580;
896 CPPUNIT_ASSERT(node.absolute_fuzzy_equals(
897 cube_vertex + x * (cube_face_center - cube_vertex), tol));
898 }
899 }
900
901 continue;
902 break;
903 }
904
905 case 6: {
906
907
908 neighbors.erase(std::remove_if(neighbors.begin(),
909 neighbors.end(),
910 [&elem](
const Node * n) {
911 return !elem->is_vertex(
912 elem->local_node(n->id()));
913 }),
914 neighbors.end());
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929 unsigned int num_vertices_at_cube_center = 0;
930 unsigned int num_vertices_at_cube_vertices = 0;
931 unsigned int num_vertices_at_cube_face_centers = 0;
932 for (const auto * neighbor : neighbors)
933 {
935 num_vertices_at_cube_center += 1;
937 num_vertices_at_cube_vertices += 1;
939 num_vertices_at_cube_face_centers += 1;
940 }
941
942
943
945
946
947 if (num_vertices_at_cube_vertices == 2)
948 {
949
950 if (num_vertices_at_cube_center)
951 for (const auto * neighbor : neighbors)
952 {
958 else
959 libmesh_error_msg("We should never get here!");
960
961 node_approx +=
weight * (*neighbor);
962 }
963
964
965 else if (num_vertices_at_cube_face_centers)
966 for (const auto * neighbor : neighbors)
967 {
973 else
974 libmesh_error_msg("We should never get here!");
975
976 node_approx +=
weight * (*neighbor);
977 }
978
979 else
980 libmesh_error_msg("We should never get here!");
981 }
982
983
984 else if (num_vertices_at_cube_center && num_vertices_at_cube_vertices &&
985 num_vertices_at_cube_face_centers)
986 for (const auto * neighbor : neighbors)
987 {
995 else
996 libmesh_error_msg("We should never get here!");
997
998 node_approx +=
weight * (*neighbor);
999 }
1000
1001 else
1002 libmesh_error_msg("We should never get here!");
1003
1004 CPPUNIT_ASSERT(node.absolute_fuzzy_equals(node_approx, tol));
1005
1006 continue;
1007 break;
1008 }
1009 default: {
1010 libmesh_error_msg(node << " has unexpected number of neighbors ("
1011 << neighbors.size() << ")");
1012 break;
1013 }
1014 }
1015 }
1016
1017 CPPUNIT_ASSERT(node_distortion_is(node, false));
1018
1019 }
1020 }
1021 }
1022 }
bool pointIsCubeFaceCenter(const Point &point, const Real &side_length, const Real &tol=TOLERANCE)
bool pointIsCubeCenter(const Point &point, const Real &side_length, const Real &tol=TOLERANCE)
bool pointIsCubeVertex(const Point &point, const Real &side_length, const Real &tol=TOLERANCE)
void max(const T &r, T &o, Request &req) const
void min(const T &r, T &o, Request &req) const
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
static constexpr dof_id_type invalid_id
An invalid id to distinguish an uninitialized DofObject.
static const Order type_to_default_order_map[INVALID_ELEM]
This array maps the integer representation of the ElemType enum to the default approximation order of...
virtual const Node & node_ref(const dof_id_type i) const
const BoundaryInfo & get_boundary_info() const
The information about boundary ids on the mesh.
virtual const Node * query_node_ptr(const dof_id_type i) const =0
const std::set< Order > & elem_default_orders() const
processor_id_type processor_id() const
const Parallel::Communicator & comm() const
virtual void smooth() override
Redefinition of the smooth function from the base class.
const MeshQualityInfo & get_mesh_info() const
Getter for the _system's _mesh_info attribute.
virtual void setup()
Setup method that creates equation systems, system, and constraints, to be called just prior to smoot...
void sync_dofobject_data_by_id(const Communicator &comm, const Iterator &range_begin, const Iterator &range_end, SyncFunctor &sync)
Request data about a range of ghost dofobjects uniquely identified by their id.
const Elem & get(const ElemType type_in)
std::string enum_to_string(const T e)
bool relative_fuzzy_equals(const T &var1, const T2 &var2, const Real tol=TOLERANCE *TOLERANCE)
Function to check whether two variables are equal within a relative tolerance.
uint8_t processor_id_type
const dof_id_type n_nodes