From 8fd146bffe2741feea08f149860f19b485b8c393 Mon Sep 17 00:00:00 2001 From: David Wells Date: Sat, 21 Oct 2023 21:12:03 -0400 Subject: [PATCH] Use std::optional in orthogonal_equality(). This is cleaner than returning a boolean and modifying an input argument. --- include/deal.II/grid/grid_tools.h | 16 +++++----- source/grid/grid_tools_dof_handlers.cc | 33 +++++++++----------- source/grid/grid_tools_dof_handlers.inst.in | 6 ++-- tests/dofs/dof_tools_21_b.cc | 34 +++++++-------------- tests/dofs/dof_tools_21_c.cc | 34 +++++++-------------- 5 files changed, 48 insertions(+), 75 deletions(-) diff --git a/include/deal.II/grid/grid_tools.h b/include/deal.II/grid/grid_tools.h index 75c988f7a5..8f8eb1c09c 100644 --- a/include/deal.II/grid/grid_tools.h +++ b/include/deal.II/grid/grid_tools.h @@ -2361,6 +2361,8 @@ namespace GridTools * * @p face1 and @p face2 are considered equal, if a one to one matching * between its vertices can be achieved via an orthogonal equality relation. + * If no such relation exists then the returned std::optional object is empty + * (i.e., has_value() will return `false`). * * Here, two vertices v_1 and v_2 are considered equal, if * $M\cdot v_1 + offset - v_2$ is parallel to the unit vector in unit @@ -2368,12 +2370,13 @@ namespace GridTools * spacedim x spacedim matrix, $M$ is set to @p matrix, otherwise $M$ is the * identity matrix. * - * If the matching was successful, the _relative_ orientation of @p face1 - * with respect to @p face2 is returned in the bitset @p orientation, where + * If the matching was successful, the _relative_ orientation of @p face1 with + * respect to @p face2 is returned a std::optional> object + * orientation in which * @code - * orientation[0] -> face_orientation - * orientation[1] -> face_flip - * orientation[2] -> face_rotation + * orientation.value()[0] = face_orientation + * orientation.value()[1] = face_flip + * orientation.value()[2] = face_rotation * @endcode * * In 2d face_orientation is always true, @@ -2420,9 +2423,8 @@ namespace GridTools * article. */ template - bool + std::optional> orthogonal_equality( - std::bitset<3> &orientation, const FaceIterator &face1, const FaceIterator &face2, const unsigned int direction, diff --git a/source/grid/grid_tools_dof_handlers.cc b/source/grid/grid_tools_dof_handlers.cc index 0617720f77..493d088e29 100644 --- a/source/grid/grid_tools_dof_handlers.cc +++ b/source/grid/grid_tools_dof_handlers.cc @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -2142,7 +2143,6 @@ namespace GridTools unsigned int n_matches = 0; // Match with a complexity of O(n^2). This could be improved... - std::bitset<3> orientation; using PairIterator = typename std::set>::const_iterator; for (PairIterator it1 = pairs1.begin(); it1 != pairs1.end(); ++it1) @@ -2153,18 +2153,21 @@ namespace GridTools const CellIterator cell2 = it2->first; const unsigned int face_idx1 = it1->second; const unsigned int face_idx2 = it2->second; - if (GridTools::orthogonal_equality(orientation, - cell1->face(face_idx1), - cell2->face(face_idx2), - direction, - offset, - matrix)) + if (const std::optional> orientation = + GridTools::orthogonal_equality(cell1->face(face_idx1), + cell2->face(face_idx2), + direction, + offset, + matrix)) { // We have a match, so insert the matching pairs and // remove the matched cell in pairs2 to speed up the // matching: const PeriodicFacePair matched_face = { - {cell1, cell2}, {face_idx1, face_idx2}, orientation, matrix}; + {cell1, cell2}, + {face_idx1, face_idx2}, + orientation.value(), + matrix}; matched_pairs.push_back(matched_face); pairs2.erase(it2); ++n_matches; @@ -2504,9 +2507,8 @@ namespace GridTools template - inline bool + std::optional> orthogonal_equality( - std::bitset<3> &orientation, const FaceIterator &face1, const FaceIterator &face2, const unsigned int direction, @@ -2548,15 +2550,10 @@ namespace GridTools } // And finally, a lookup to determine the ordering bitmask: - if (face2_vertices.empty()) - orientation = OrientationLookupTable::lookup(matching); - - return face2_vertices.empty(); - + return face2_vertices.empty() ? + std::make_optional(OrientationLookupTable::lookup(matching)) : + std::nullopt; } - - - } // namespace GridTools diff --git a/source/grid/grid_tools_dof_handlers.inst.in b/source/grid/grid_tools_dof_handlers.inst.in index d08016bad8..c5fb404c69 100644 --- a/source/grid/grid_tools_dof_handlers.inst.in +++ b/source/grid/grid_tools_dof_handlers.inst.in @@ -250,18 +250,16 @@ for (X : SEQUENTIAL_TRIANGULATION_AND_DOFHANDLER; namespace GridTools \{ - template bool + template std::optional> orthogonal_equality( - std::bitset<3> &, const X::active_face_iterator &, const X::active_face_iterator &, const unsigned int, const Tensor<1, deal_II_space_dimension> &, const FullMatrix &); - template bool + template std::optional> orthogonal_equality( - std::bitset<3> &, const X::face_iterator &, const X::face_iterator &, const unsigned int, diff --git a/tests/dofs/dof_tools_21_b.cc b/tests/dofs/dof_tools_21_b.cc index 0e42ae238c..adc85f5502 100644 --- a/tests/dofs/dof_tools_21_b.cc +++ b/tests/dofs/dof_tools_21_b.cc @@ -317,24 +317,14 @@ print_matching(DoFHandler &dof_handler, deallog << std::endl; - std::bitset<3> orientation; - if (not GridTools::orthogonal_equality(orientation, - face_1, - face_2, - dim == 2 ? 1 : 2, - dealii::Tensor<1, spacedim>())) - std::cerr << " not match! oh noze!! " << std::endl; - deallog << "Orientation: " << orientation[0] << orientation[1] - << orientation[2] << std::endl; - - - DoFTools::make_periodicity_constraints(face_1, - face_2, - constraint_matrix, - velocity_mask, - orientation[0], - orientation[1], - orientation[2]); + const auto orientation = GridTools::orthogonal_equality( + face_1, face_2, dim == 2 ? 1 : 2, dealii::Tensor<1, spacedim>()); + AssertThrow(orientation, ExcMessage(" not match! oh noze!! ")); + const auto o = *orientation; + deallog << "Orientation: " << o[0] << o[1] << o[2] << std::endl; + + DoFTools::make_periodicity_constraints( + face_1, face_2, constraint_matrix, velocity_mask, o[0], o[1], o[2]); deallog << "Matching:" << std::endl; constraint_matrix.print(deallog.get_file_stream()); constraint_matrix.close(); @@ -343,11 +333,9 @@ print_matching(DoFHandler &dof_handler, face_1, constraint_matrix_reverse, velocity_mask, - orientation[0], - orientation[0] ? - orientation[1] ^ orientation[2] : - orientation[1], - orientation[2]); + o[0], + o[0] ? o[1] ^ o[2] : o[1], + o[2]); deallog << "Reverse Matching:" << std::endl; constraint_matrix_reverse.print(deallog.get_file_stream()); constraint_matrix_reverse.close(); diff --git a/tests/dofs/dof_tools_21_c.cc b/tests/dofs/dof_tools_21_c.cc index 6b26841247..116796598b 100644 --- a/tests/dofs/dof_tools_21_c.cc +++ b/tests/dofs/dof_tools_21_c.cc @@ -323,24 +323,14 @@ print_matching(DoFHandler &dof_handler, deallog << std::endl; - std::bitset<3> orientation; - if (not GridTools::orthogonal_equality(orientation, - face_1, - face_2, - dim == 2 ? 1 : 2, - dealii::Tensor<1, spacedim>())) - std::cerr << " not match! oh noze!! " << std::endl; - deallog << "Orientation: " << orientation[0] << orientation[1] - << orientation[2] << std::endl; - - - DoFTools::make_periodicity_constraints(face_1, - face_2, - constraint_matrix, - velocity_mask, - orientation[0], - orientation[1], - orientation[2]); + const auto orientation = GridTools::orthogonal_equality( + face_1, face_2, dim == 2 ? 1 : 2, dealii::Tensor<1, spacedim>()); + AssertThrow(orientation, ExcMessage(" not match! oh noze!! ")); + const auto o = *orientation; + deallog << "Orientation: " << o[0] << o[1] << o[2] << std::endl; + + DoFTools::make_periodicity_constraints( + face_1, face_2, constraint_matrix, velocity_mask, o[0], o[1], o[2]); deallog << "Matching:" << std::endl; constraint_matrix.print(deallog.get_file_stream()); constraint_matrix.close(); @@ -349,11 +339,9 @@ print_matching(DoFHandler &dof_handler, face_1, constraint_matrix_reverse, velocity_mask, - orientation[0], - orientation[0] ? - orientation[1] ^ orientation[2] : - orientation[1], - orientation[2]); + o[0], + o[0] ? o[1] ^ o[2] : o[1], + o[2]); deallog << "Reverse Matching:" << std::endl; constraint_matrix_reverse.print(deallog.get_file_stream()); constraint_matrix_reverse.close(); -- 2.39.5