From f68206d687ff7160930ed32c794711cdd1613d59 Mon Sep 17 00:00:00 2001 From: Sandro Elsweijer Date: Mon, 16 Sep 2024 17:03:19 +0200 Subject: [PATCH 01/24] move binary soearch to forest_private --- src/CMakeLists.txt | 2 +- src/t8_forest/t8_forest.cxx | 46 ------------------- ...forest_private.c => t8_forest_private.cxx} | 46 +++++++++++++++++++ src/t8_forest/t8_forest_private.h | 11 +++++ 4 files changed, 58 insertions(+), 47 deletions(-) rename src/t8_forest/{t8_forest_private.c => t8_forest_private.cxx} (60%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b562792f1d..35ab90de74 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -105,7 +105,7 @@ target_sources( T8 PRIVATE t8_forest/t8_forest_adapt.cxx t8_forest/t8_forest_partition.cxx t8_forest/t8_forest.cxx - t8_forest/t8_forest_private.c + t8_forest/t8_forest_private.cxx t8_forest/t8_forest_vtk.cxx t8_forest/t8_forest_ghost.cxx t8_forest/t8_forest_iterate.cxx diff --git a/src/t8_forest/t8_forest.cxx b/src/t8_forest/t8_forest.cxx index 23a718fee4..1a3a2aa910 100644 --- a/src/t8_forest/t8_forest.cxx +++ b/src/t8_forest/t8_forest.cxx @@ -1441,52 +1441,6 @@ t8_forest_copy_trees (t8_forest_t forest, t8_forest_t from, int copy_elements) } } -/* Search for a linear element id (at forest->maxlevel) in a sorted array of - * elements. If the element does not exist, return the largest index i - * such that the element at position i has a smaller id than the given one. - * If no such i exists, return -1. - */ -/* TODO: should return t8_locidx_t */ -static t8_locidx_t -t8_forest_bin_search_lower (const t8_element_array_t *elements, const t8_linearidx_t element_id, const int maxlevel) -{ - t8_linearidx_t query_id; - t8_locidx_t low, high, guess; - - const t8_eclass_scheme_c *ts = t8_element_array_get_scheme (elements); - /* At first, we check whether any element has smaller id than the - * given one. */ - const t8_element_t *query = t8_element_array_index_int (elements, 0); - query_id = ts->t8_element_get_linear_id (query, maxlevel); - if (query_id > element_id) { - /* No element has id smaller than the given one */ - return -1; - } - - /* We now perform the binary search */ - low = 0; - high = t8_element_array_get_count (elements) - 1; - while (low < high) { - guess = (low + high + 1) / 2; - query = t8_element_array_index_int (elements, guess); - query_id = ts->t8_element_get_linear_id (query, maxlevel); - if (query_id == element_id) { - /* we are done */ - return guess; - } - else if (query_id > element_id) { - /* look further left */ - high = guess - 1; - } - else { - /* look further right, but keep guess in the search range */ - low = guess; - } - } - T8_ASSERT (low == high); - return low; -} - t8_eclass_t t8_forest_element_neighbor_eclass (t8_forest_t forest, t8_locidx_t ltreeid, const t8_element_t *elem, int face) { diff --git a/src/t8_forest/t8_forest_private.c b/src/t8_forest/t8_forest_private.cxx similarity index 60% rename from src/t8_forest/t8_forest_private.c rename to src/t8_forest/t8_forest_private.cxx index 6febf22cc1..85a536b918 100644 --- a/src/t8_forest/t8_forest_private.c +++ b/src/t8_forest/t8_forest_private.cxx @@ -23,6 +23,9 @@ #include #include #include +#include + +T8_EXTERN_C_BEGIN (); const t8_element_t* t8_forest_get_tree_element (t8_tree_t tree, t8_locidx_t elem_in_tree) @@ -52,3 +55,46 @@ t8_forest_get_tree_element_array_mutable (const t8_forest_t forest, t8_locidx_t { return (t8_element_array_t*) t8_forest_get_tree_element_array (forest, ltreeid); } + +/* TODO: should return t8_locidx_t */ +t8_locidx_t +t8_forest_bin_search_lower (const t8_element_array_t* elements, const t8_linearidx_t element_id, const int maxlevel) +{ + t8_linearidx_t query_id; + t8_locidx_t low, high, guess; + + const t8_eclass_scheme_c* ts = t8_element_array_get_scheme (elements); + /* At first, we check whether any element has smaller id than the + * given one. */ + const t8_element_t* query = t8_element_array_index_int (elements, 0); + query_id = ts->t8_element_get_linear_id (query, maxlevel); + if (query_id > element_id) { + /* No element has id smaller than the given one */ + return -1; + } + + /* We now perform the binary search */ + low = 0; + high = t8_element_array_get_count (elements) - 1; + while (low < high) { + guess = (low + high + 1) / 2; + query = t8_element_array_index_int (elements, guess); + query_id = ts->t8_element_get_linear_id (query, maxlevel); + if (query_id == element_id) { + /* we are done */ + return guess; + } + else if (query_id > element_id) { + /* look further left */ + high = guess - 1; + } + else { + /* look further right, but keep guess in the search range */ + low = guess; + } + } + T8_ASSERT (low == high); + return low; +} + +T8_EXTERN_C_END (); diff --git a/src/t8_forest/t8_forest_private.h b/src/t8_forest/t8_forest_private.h index f43e244631..f4197c3409 100644 --- a/src/t8_forest/t8_forest_private.h +++ b/src/t8_forest/t8_forest_private.h @@ -195,6 +195,17 @@ t8_forest_get_tree_element_array (t8_forest_t forest, t8_locidx_t ltreeid); t8_element_array_t * t8_forest_get_tree_element_array_mutable (const t8_forest_t forest, t8_locidx_t ltreeid); +/** Search for a linear element id (at forest->maxlevel) in a sorted array of + * elements. If the element does not exist, return the largest index i + * such that the element at position i has a smaller id than the given one. + * If no such i exists, return -1. + * \param [in] elements The array of elements. + * \param [in] element_id The linear id of the element to search for. + * \param [in] maxlevel The maximum level of the elements. + */ +t8_locidx_t +t8_forest_bin_search_lower (const t8_element_array_t *elements, const t8_linearidx_t element_id, const int maxlevel); + /** Find the owner process of a given element, deprecated version. * Use t8_forest_element_find_owner instead. * \param [in] forest The forest. From 933ac8810ba457a96d2c70b4f6ec76aa1d662d5a Mon Sep 17 00:00:00 2001 From: Sandro Elsweijer Date: Mon, 16 Sep 2024 17:03:47 +0200 Subject: [PATCH 02/24] consting --- src/t8_forest/t8_forest.cxx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/t8_forest/t8_forest.cxx b/src/t8_forest/t8_forest.cxx index 1a3a2aa910..4b4eaf16a0 100644 --- a/src/t8_forest/t8_forest.cxx +++ b/src/t8_forest/t8_forest.cxx @@ -3643,11 +3643,10 @@ t8_forest_get_element (t8_forest_t forest, t8_locidx_t lelement_id, t8_locidx_t const t8_element_t * t8_forest_get_element_in_tree (t8_forest_t forest, t8_locidx_t ltreeid, t8_locidx_t leid_in_tree) { - t8_tree_t tree; T8_ASSERT (t8_forest_is_committed (forest)); T8_ASSERT (0 <= ltreeid && ltreeid < t8_forest_get_num_local_trees (forest)); - tree = t8_forest_get_tree (forest, ltreeid); + const t8_tree_t tree = t8_forest_get_tree (forest, ltreeid); const t8_element_t *element = t8_forest_get_tree_element (tree, leid_in_tree); T8_ASSERT (t8_forest_element_is_leaf (forest, element, ltreeid)); return element; From 1e0ef3711fbc67e499da70d544b4d72b1e65512b Mon Sep 17 00:00:00 2001 From: Sandro Elsweijer Date: Mon, 16 Sep 2024 17:05:44 +0200 Subject: [PATCH 03/24] add ghost retrieval functions --- src/t8_forest/t8_forest_ghost.cxx | 49 +++++++++++++++++++++++++++++++ src/t8_forest/t8_forest_ghost.h | 24 +++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/src/t8_forest/t8_forest_ghost.cxx b/src/t8_forest/t8_forest_ghost.cxx index b537cb0ed3..be01f6c74e 100644 --- a/src/t8_forest/t8_forest_ghost.cxx +++ b/src/t8_forest/t8_forest_ghost.cxx @@ -303,6 +303,55 @@ t8_forest_ghost_tree_num_elements (t8_forest_t forest, t8_locidx_t lghost_tree) return t8_element_array_get_count (&ghost_tree->elements); } +const t8_element_t * +t8_ghost_get_ghost_in_tree (t8_forest_t forest, t8_locidx_t lghost_tree, t8_linearidx_t linear_id, int element_level, + t8_locidx_t *loc_ghost_id) +{ + T8_ASSERT (t8_forest_is_committed (forest)); + const t8_element_array_t *ghost_elements = t8_forest_ghost_get_tree_elements (forest, lghost_tree); + T8_ASSERT (ghost_elements != NULL); + /* Search for the element. + * The search returns the largest index i, + * such that the element at position i has a smaller id than the given one. + * If no such i exists, it returns -1. */ + *loc_ghost_id = t8_forest_bin_search_lower (ghost_elements, linear_id, element_level); + if (*loc_ghost_id >= 0) { + /* The element was found */ + return (const t8_element_t *) t8_sc_array_index_locidx (&(ghost_elements->array), *loc_ghost_id); + } + return nullptr; +} + +t8_locidx_t +t8_ghost_get_ghost_id_in_tree (t8_forest_t forest, t8_locidx_t lghost_tree, t8_element_t *ghost_element) +{ + T8_ASSERT (t8_forest_is_committed (forest)); + const t8_element_array_t *ghost_elements = t8_forest_ghost_get_tree_elements (forest, lghost_tree); + T8_ASSERT (ghost_elements != NULL); + /* In order to find the element, we need to compute its linear id. + * To do so, we need the scheme and the level of the element. */ + const t8_eclass_scheme_c *scheme = t8_element_array_get_scheme (ghost_elements); + const int ghost_level = scheme->t8_element_level (ghost_element); + /* Compute the linear id. */ + const t8_linearidx_t element_id = scheme->t8_element_get_linear_id (ghost_element, ghost_level); + /* Search for the element */ + int loc_ghost_id; + const t8_element_t *found_ghost + = t8_ghost_get_ghost_in_tree (forest, lghost_tree, element_id, ghost_level, &loc_ghost_id); + if (loc_ghost_id < 0) { + /* The element was not found */ + return -1; + } + /* An element was found but it may not be the candidate element. + * To identify whether the element was found, we compare these two. */ + if (scheme->t8_element_equal (ghost_element, found_ghost)) { + return loc_ghost_id; + } + else { + return -1; + } +} + t8_element_array_t * t8_forest_ghost_get_tree_elements (const t8_forest_t forest, const t8_locidx_t lghost_tree) { diff --git a/src/t8_forest/t8_forest_ghost.h b/src/t8_forest/t8_forest_ghost.h index 9236f54312..140f363b78 100644 --- a/src/t8_forest/t8_forest_ghost.h +++ b/src/t8_forest/t8_forest_ghost.h @@ -69,6 +69,30 @@ t8_forest_ghost_get_tree_element_offset (t8_forest_t forest, t8_locidx_t lghost_ t8_locidx_t t8_forest_ghost_tree_num_elements (t8_forest_t forest, t8_locidx_t lghost_tree); +/** Retrieves the ghost index of an element in a specific ghost tree. + * + * \param [in] forest The forest object. + * \param [in] lghost_tree The local index of the ghost tree. + * \param [in] linear_id The local index of the element. + * \param [in] element_level The level of the element. + * \param [out] loc_ghost_id The local id of the ghost. -1 if no ghost was found. + * \return The ghost element. nullptr if no ghost was found. + */ +const t8_element_t * +t8_ghost_get_ghost_in_tree (t8_forest_t forest, t8_locidx_t lghost_tree, t8_linearidx_t linear_id, int element_level, + t8_locidx_t *loc_ghost_id); + +/** Retrieves the ghost index of an element in a specific ghost tree. + * + * \param [in] forest The forest object. + * \param [in] lghost_tree The local index of the ghost tree. + * \param [in] ghost_element The ghost element. + * \return The local index of the element in the ghost element array of the ghost tree. + * -1 if no ghost element was found. + */ +t8_locidx_t +t8_ghost_get_ghost_id_in_tree (t8_forest_t forest, t8_locidx_t lghost_tree, t8_element_t *ghost_element); + /** Get a pointer to the ghost element array of a ghost tree. * \param [in] forest The forest. Ghost layer must exist. * \param [in] lghost_tree The ghost tree id of a ghost tree. From 3fee901469f7bccccdacf69a79c0cff440531d7b Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Wed, 18 Sep 2024 12:17:00 +0200 Subject: [PATCH 04/24] Typos --- src/t8_forest/t8_forest_ghost.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/t8_forest/t8_forest_ghost.cxx b/src/t8_forest/t8_forest_ghost.cxx index be01f6c74e..394a33c45c 100644 --- a/src/t8_forest/t8_forest_ghost.cxx +++ b/src/t8_forest/t8_forest_ghost.cxx @@ -1938,9 +1938,9 @@ t8_forest_ghost_destroy (t8_forest_ghost_t *pghost) /** * Implementation of the ghost-interface - * Wrapper for the abstracte base classe + * Wrapper for the abstracte base class * Implementation of the comunication steps - * and implementation of the dreived classe search + * and implementation of the dreived class search */ t8_ghost_type_t From 8c171a8b65a0381cf69dfd63b72736dceb76585f Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 24 Sep 2024 16:27:56 +0200 Subject: [PATCH 05/24] Update src/t8_forest/t8_forest_ghost.cxx --- src/t8_forest/t8_forest_ghost.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/t8_forest/t8_forest_ghost.cxx b/src/t8_forest/t8_forest_ghost.cxx index 394a33c45c..af47985b47 100644 --- a/src/t8_forest/t8_forest_ghost.cxx +++ b/src/t8_forest/t8_forest_ghost.cxx @@ -317,7 +317,7 @@ t8_ghost_get_ghost_in_tree (t8_forest_t forest, t8_locidx_t lghost_tree, t8_line *loc_ghost_id = t8_forest_bin_search_lower (ghost_elements, linear_id, element_level); if (*loc_ghost_id >= 0) { /* The element was found */ - return (const t8_element_t *) t8_sc_array_index_locidx (&(ghost_elements->array), *loc_ghost_id); + return t8_element_array_index_locidx (ghost_elements, *loc_ghost_id); } return nullptr; } From 5616096041297d6c21b418ba020e5b198c2959fb Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 24 Sep 2024 16:28:05 +0200 Subject: [PATCH 06/24] Update src/t8_forest/t8_forest_ghost.h --- src/t8_forest/t8_forest_ghost.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/t8_forest/t8_forest_ghost.h b/src/t8_forest/t8_forest_ghost.h index 140f363b78..e67048be6a 100644 --- a/src/t8_forest/t8_forest_ghost.h +++ b/src/t8_forest/t8_forest_ghost.h @@ -73,7 +73,7 @@ t8_forest_ghost_tree_num_elements (t8_forest_t forest, t8_locidx_t lghost_tree); * * \param [in] forest The forest object. * \param [in] lghost_tree The local index of the ghost tree. - * \param [in] linear_id The local index of the element. + * \param [in] linear_id The linear id of the element. * \param [in] element_level The level of the element. * \param [out] loc_ghost_id The local id of the ghost. -1 if no ghost was found. * \return The ghost element. nullptr if no ghost was found. From 574b4152e7992ce1797e9f1bfaa9dc317dfedf5b Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 24 Sep 2024 16:28:14 +0200 Subject: [PATCH 07/24] Update src/t8_forest/t8_forest_private.cxx --- src/t8_forest/t8_forest_private.cxx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/t8_forest/t8_forest_private.cxx b/src/t8_forest/t8_forest_private.cxx index 85a536b918..6e09489e26 100644 --- a/src/t8_forest/t8_forest_private.cxx +++ b/src/t8_forest/t8_forest_private.cxx @@ -56,7 +56,6 @@ t8_forest_get_tree_element_array_mutable (const t8_forest_t forest, t8_locidx_t return (t8_element_array_t*) t8_forest_get_tree_element_array (forest, ltreeid); } -/* TODO: should return t8_locidx_t */ t8_locidx_t t8_forest_bin_search_lower (const t8_element_array_t* elements, const t8_linearidx_t element_id, const int maxlevel) { From 5909a16c87d7c0086bb206ce2ba598f5631cbee4 Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 24 Sep 2024 16:28:26 +0200 Subject: [PATCH 08/24] Update src/t8_forest/t8_forest_private.h --- src/t8_forest/t8_forest_private.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/t8_forest/t8_forest_private.h b/src/t8_forest/t8_forest_private.h index f4197c3409..4d03aeffaa 100644 --- a/src/t8_forest/t8_forest_private.h +++ b/src/t8_forest/t8_forest_private.h @@ -199,6 +199,7 @@ t8_forest_get_tree_element_array_mutable (const t8_forest_t forest, t8_locidx_t * elements. If the element does not exist, return the largest index i * such that the element at position i has a smaller id than the given one. * If no such i exists, return -1. + * \note This search does not work for subelements. * \param [in] elements The array of elements. * \param [in] element_id The linear id of the element to search for. * \param [in] maxlevel The maximum level of the elements. From 31e9d928b3442969e123b01efdeacb03f2cd8193 Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Fri, 18 Oct 2024 13:11:38 +0200 Subject: [PATCH 09/24] indent example --- benchmarks/time_forest_partition.cxx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/benchmarks/time_forest_partition.cxx b/benchmarks/time_forest_partition.cxx index fc280e2e62..674ee56734 100644 --- a/benchmarks/time_forest_partition.cxx +++ b/benchmarks/time_forest_partition.cxx @@ -421,11 +421,12 @@ main (int argc, char *argv[]) /* check for wrong usage of arguments */ if (first_argc < 0 || first_argc != argc || dim < 2 || dim > 3 || (cmeshfileprefix == NULL && mshfileprefix == NULL && test_tet == 0 && test_cad_cylinder == 0 - && test_linear_cylinder == 0 && test_hybrid_cube == 0 && test_hex_cube == 0 ) + && test_linear_cylinder == 0 && test_hybrid_cube == 0 && test_hex_cube == 0) || stride <= 0 || (num_files - 1) * stride >= mpisize || cfl < 0 || T <= 0 || test_tet + test_linear_cylinder + test_cad_cylinder + test_hybrid_cube + test_hex_cube > 1 || (cmesh_level >= 0 && (!test_linear_cylinder && !test_cad_cylinder && !test_hybrid_cube && !test_hex_cube)) - || ((mshfileprefix != NULL || cmeshfileprefix != NULL) && (test_linear_cylinder || test_cad_cylinder || test_tet || test_hybrid_cube || test_hex_cube)) + || ((mshfileprefix != NULL || cmeshfileprefix != NULL) + && (test_linear_cylinder || test_cad_cylinder || test_tet || test_hybrid_cube || test_hex_cube)) || (mshfileprefix == NULL && use_cad)) { sc_options_print_usage (t8_get_package_id (), SC_LP_ERROR, opt, NULL); return 1; @@ -460,12 +461,12 @@ main (int argc, char *argv[]) sc_intpow (2, cmesh_level), sc_intpow (2, cmesh_level), test_cad_cylinder); test_linear_cylinder ? vtu_prefix = "test_linear_cylinder" : vtu_prefix = "test_cad_cylinder"; } - else if (test_hybrid_cube){ + else if (test_hybrid_cube) { cmesh = t8_cmesh_new_hypercube_hybrid (sc_MPI_COMM_WORLD, 0, 0); vtu_prefix = "test_hypercube_hybrid"; } - else if (test_hex_cube){ - cmesh = t8_cmesh_new_hypercube(T8_ECLASS_HEX, sc_MPI_COMM_WORLD, 0, 0, 0); + else if (test_hex_cube) { + cmesh = t8_cmesh_new_hypercube (T8_ECLASS_HEX, sc_MPI_COMM_WORLD, 0, 0, 0); vtu_prefix = "test_hypercube_hex"; } else { From e718252fa5177275d0f09bc5506da34f7081b563 Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 29 Oct 2024 11:34:14 +0100 Subject: [PATCH 10/24] Fix Makefile line wrap --- src/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Makefile.am b/src/Makefile.am index e2bc6ba6e7..ac4b843944 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -59,7 +59,7 @@ libt8_installed_headers_forest = \ src/t8_forest/t8_forest_ghost_interface_wrapper.h \ src/t8_forest/t8_forest_ghost_interface.h \ src/t8_forest/t8_forest_ghost_interface.hxx \ - src/t8_forest/t8_forest_ghost_search.hxx + src/t8_forest/t8_forest_ghost_search.hxx \ src/t8_forest/t8_forest_ghost_stencil.hxx libt8_installed_headers_geometry = \ src/t8_geometry/t8_geometry.h \ From 8d929a8d7b4a1d64a116f6f1161f9083a02b61ac Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 29 Oct 2024 11:48:08 +0100 Subject: [PATCH 11/24] Add new bin search to private and remove from static --- src/t8_forest/t8_forest.cxx | 33 ------------------ src/t8_forest/t8_forest_private.cxx | 53 +++++++++++++---------------- 2 files changed, 23 insertions(+), 63 deletions(-) diff --git a/src/t8_forest/t8_forest.cxx b/src/t8_forest/t8_forest.cxx index a434708ef1..d0893292b6 100644 --- a/src/t8_forest/t8_forest.cxx +++ b/src/t8_forest/t8_forest.cxx @@ -1444,39 +1444,6 @@ t8_forest_copy_trees (t8_forest_t forest, t8_forest_t from, int copy_elements) } } -/** \brief Search for a linear element id (at forest->maxlevel) in a sorted array of - * elements. If the element does not exist, return the largest index i - * such that the element at position i has a smaller id than the given one. - * If no such i exists, return -1. - */ -static t8_locidx_t -t8_forest_bin_search_lower (const t8_element_array_t *elements, const t8_linearidx_t element_id, const int maxlevel) -{ - const t8_eclass_scheme_c *ts = t8_element_array_get_scheme (elements); - /* At first, we check whether any element has smaller id than the - * given one. */ - const t8_element_t *query = t8_element_array_index_int (elements, 0); - const t8_linearidx_t query_id = ts->t8_element_get_linear_id (query, maxlevel); - if (query_id > element_id) { - /* No element has id smaller than the given one. */ - return -1; - } - - /* We search for the first element in the array that is greater than the given element id. */ - auto elem_iter = std::upper_bound ( - t8_element_array_begin (elements), t8_element_array_end (elements), element_id, - [&maxlevel, &ts] (const t8_linearidx_t element_id_, const t8_element_array_iterator::value_type &elem_ptr) { - return (element_id_ < ts->t8_element_get_linear_id (elem_ptr, maxlevel)); - }); - - /* After we found the element with an id greater than the given one, we are able to jump one index back. - * This guarantees us that the element at (index - 1) is smaller or equal to the given element id. - * In case we do not find an element that is greater than the given element_id, the binary search returns - * the end-iterator of the element array. In that case, we want to return the last index from the element - * array. */ - return elem_iter.GetCurrentIndex () - 1; -} - t8_eclass_t t8_forest_element_neighbor_eclass (t8_forest_t forest, t8_locidx_t ltreeid, const t8_element_t *elem, int face) { diff --git a/src/t8_forest/t8_forest_private.cxx b/src/t8_forest/t8_forest_private.cxx index 6e09489e26..426585fb89 100644 --- a/src/t8_forest/t8_forest_private.cxx +++ b/src/t8_forest/t8_forest_private.cxx @@ -56,44 +56,37 @@ t8_forest_get_tree_element_array_mutable (const t8_forest_t forest, t8_locidx_t return (t8_element_array_t*) t8_forest_get_tree_element_array (forest, ltreeid); } +/** \brief Search for a linear element id (at forest->maxlevel) in a sorted array of + * elements. If the element does not exist, return the largest index i + * such that the element at position i has a smaller id than the given one. + * If no such i exists, return -1. + */ t8_locidx_t -t8_forest_bin_search_lower (const t8_element_array_t* elements, const t8_linearidx_t element_id, const int maxlevel) +t8_forest_bin_search_lower (const t8_element_array_t *elements, const t8_linearidx_t element_id, const int maxlevel) { - t8_linearidx_t query_id; - t8_locidx_t low, high, guess; - - const t8_eclass_scheme_c* ts = t8_element_array_get_scheme (elements); + const t8_eclass_scheme_c *ts = t8_element_array_get_scheme (elements); /* At first, we check whether any element has smaller id than the * given one. */ - const t8_element_t* query = t8_element_array_index_int (elements, 0); - query_id = ts->t8_element_get_linear_id (query, maxlevel); + const t8_element_t *query = t8_element_array_index_int (elements, 0); + const t8_linearidx_t query_id = ts->t8_element_get_linear_id (query, maxlevel); if (query_id > element_id) { - /* No element has id smaller than the given one */ + /* No element has id smaller than the given one. */ return -1; } - /* We now perform the binary search */ - low = 0; - high = t8_element_array_get_count (elements) - 1; - while (low < high) { - guess = (low + high + 1) / 2; - query = t8_element_array_index_int (elements, guess); - query_id = ts->t8_element_get_linear_id (query, maxlevel); - if (query_id == element_id) { - /* we are done */ - return guess; - } - else if (query_id > element_id) { - /* look further left */ - high = guess - 1; - } - else { - /* look further right, but keep guess in the search range */ - low = guess; - } - } - T8_ASSERT (low == high); - return low; + /* We search for the first element in the array that is greater than the given element id. */ + auto elem_iter = std::upper_bound ( + t8_element_array_begin (elements), t8_element_array_end (elements), element_id, + [&maxlevel, &ts] (const t8_linearidx_t element_id_, const t8_element_array_iterator::value_type &elem_ptr) { + return (element_id_ < ts->t8_element_get_linear_id (elem_ptr, maxlevel)); + }); + + /* After we found the element with an id greater than the given one, we are able to jump one index back. + * This guarantees us that the element at (index - 1) is smaller or equal to the given element id. + * In case we do not find an element that is greater than the given element_id, the binary search returns + * the end-iterator of the element array. In that case, we want to return the last index from the element + * array. */ + return elem_iter.GetCurrentIndex () - 1; } T8_EXTERN_C_END (); From 2d977da9d228620f78872a7eebec7fd2e68448c3 Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 29 Oct 2024 11:48:50 +0100 Subject: [PATCH 12/24] rename get ghost function to better match parameters --- src/t8_forest/t8_forest_ghost.cxx | 9 +++++---- src/t8_forest/t8_forest_ghost.h | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/t8_forest/t8_forest_ghost.cxx b/src/t8_forest/t8_forest_ghost.cxx index fd34e260bb..07c9543df5 100644 --- a/src/t8_forest/t8_forest_ghost.cxx +++ b/src/t8_forest/t8_forest_ghost.cxx @@ -304,12 +304,13 @@ t8_forest_ghost_tree_num_elements (t8_forest_t forest, t8_locidx_t lghost_tree) } const t8_element_t * -t8_ghost_get_ghost_in_tree (t8_forest_t forest, t8_locidx_t lghost_tree, t8_linearidx_t linear_id, int element_level, - t8_locidx_t *loc_ghost_id) +t8_ghost_get_ghost_in_tree_from_linear_id (t8_forest_t forest, t8_locidx_t lghost_tree, t8_linearidx_t linear_id, + int element_level, t8_locidx_t *loc_ghost_id) { T8_ASSERT (t8_forest_is_committed (forest)); const t8_element_array_t *ghost_elements = t8_forest_ghost_get_tree_elements (forest, lghost_tree); T8_ASSERT (ghost_elements != NULL); + T8_ASSERT (forest->incomplete_trees); // This function will not word properlt with incomplete trees. /* Search for the element. * The search returns the largest index i, * such that the element at position i has a smaller id than the given one. @@ -335,9 +336,9 @@ t8_ghost_get_ghost_id_in_tree (t8_forest_t forest, t8_locidx_t lghost_tree, t8_e /* Compute the linear id. */ const t8_linearidx_t element_id = scheme->t8_element_get_linear_id (ghost_element, ghost_level); /* Search for the element */ - int loc_ghost_id; + t8_locidx_t loc_ghost_id; const t8_element_t *found_ghost - = t8_ghost_get_ghost_in_tree (forest, lghost_tree, element_id, ghost_level, &loc_ghost_id); + = t8_ghost_get_ghost_in_tree_from_linear_id (forest, lghost_tree, element_id, ghost_level, &loc_ghost_id); if (loc_ghost_id < 0) { /* The element was not found */ return -1; diff --git a/src/t8_forest/t8_forest_ghost.h b/src/t8_forest/t8_forest_ghost.h index e7703da12f..a6c4eba608 100644 --- a/src/t8_forest/t8_forest_ghost.h +++ b/src/t8_forest/t8_forest_ghost.h @@ -79,8 +79,8 @@ t8_forest_ghost_tree_num_elements (t8_forest_t forest, t8_locidx_t lghost_tree); * \return The ghost element. nullptr if no ghost was found. */ const t8_element_t * -t8_ghost_get_ghost_in_tree (t8_forest_t forest, t8_locidx_t lghost_tree, t8_linearidx_t linear_id, int element_level, - t8_locidx_t *loc_ghost_id); +t8_ghost_get_ghost_in_tree_from_linear_id (t8_forest_t forest, t8_locidx_t lghost_tree, t8_linearidx_t linear_id, + int element_level, t8_locidx_t *loc_ghost_id); /** Retrieves the ghost index of an element in a specific ghost tree. * From 4581fe0d29b7a187dfc4ace38ed5b5b9dc89e08b Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 29 Oct 2024 11:49:00 +0100 Subject: [PATCH 13/24] indent --- src/t8_forest/t8_forest_private.cxx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/t8_forest/t8_forest_private.cxx b/src/t8_forest/t8_forest_private.cxx index 426585fb89..71770198ea 100644 --- a/src/t8_forest/t8_forest_private.cxx +++ b/src/t8_forest/t8_forest_private.cxx @@ -27,7 +27,7 @@ T8_EXTERN_C_BEGIN (); -const t8_element_t* +const t8_element_t * t8_forest_get_tree_element (t8_tree_t tree, t8_locidx_t elem_in_tree) { T8_ASSERT (tree != NULL); @@ -35,13 +35,13 @@ t8_forest_get_tree_element (t8_tree_t tree, t8_locidx_t elem_in_tree) return t8_element_array_index_locidx (&tree->elements, elem_in_tree); } -t8_element_t* +t8_element_t * t8_forest_get_tree_element_mutable (t8_tree_t tree, t8_locidx_t elem_in_tree) { - return (t8_element_t*) t8_forest_get_tree_element (tree, elem_in_tree); + return (t8_element_t *) t8_forest_get_tree_element (tree, elem_in_tree); } -const t8_element_array_t* +const t8_element_array_t * t8_forest_get_tree_element_array (const t8_forest_t forest, t8_locidx_t ltreeid) { T8_ASSERT (t8_forest_is_committed (forest)); @@ -50,10 +50,10 @@ t8_forest_get_tree_element_array (const t8_forest_t forest, t8_locidx_t ltreeid) return &t8_forest_get_tree (forest, ltreeid)->elements; } -t8_element_array_t* +t8_element_array_t * t8_forest_get_tree_element_array_mutable (const t8_forest_t forest, t8_locidx_t ltreeid) { - return (t8_element_array_t*) t8_forest_get_tree_element_array (forest, ltreeid); + return (t8_element_array_t *) t8_forest_get_tree_element_array (forest, ltreeid); } /** \brief Search for a linear element id (at forest->maxlevel) in a sorted array of From 49b2dc37ef09c15cda3c766b8664caa6da6d73f2 Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 29 Oct 2024 11:58:53 +0100 Subject: [PATCH 14/24] remove vtk file from build list --- src/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c5a4c823b5..4b94e9a294 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -109,7 +109,6 @@ target_sources( T8 PRIVATE t8_forest/t8_forest_partition.cxx t8_forest/t8_forest.cxx t8_forest/t8_forest_private.cxx - t8_forest/t8_forest_vtk.cxx t8_forest/t8_forest_ghost.cxx t8_forest/t8_forest_iterate.cxx t8_forest/t8_forest_balance.cxx From 38deaa6ed6fca2be1cbed5699e778213ad81fea9 Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 29 Oct 2024 12:07:44 +0100 Subject: [PATCH 15/24] finish moving bin search - move includes --- src/t8_forest/t8_forest.cxx | 3 --- src/t8_forest/t8_forest_private.cxx | 2 ++ 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/t8_forest/t8_forest.cxx b/src/t8_forest/t8_forest.cxx index d0893292b6..0fd3f5a12f 100644 --- a/src/t8_forest/t8_forest.cxx +++ b/src/t8_forest/t8_forest.cxx @@ -46,9 +46,6 @@ #include #include #endif -#include - -#include /* We want to export the whole implementation to be callable from "C" */ T8_EXTERN_C_BEGIN (); diff --git a/src/t8_forest/t8_forest_private.cxx b/src/t8_forest/t8_forest_private.cxx index 71770198ea..ce9c31bacf 100644 --- a/src/t8_forest/t8_forest_private.cxx +++ b/src/t8_forest/t8_forest_private.cxx @@ -24,6 +24,8 @@ #include #include #include +#include +#include T8_EXTERN_C_BEGIN (); From 975e66bbd590a221d923eed3bdcb2e923c10ebef Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 29 Oct 2024 12:16:08 +0100 Subject: [PATCH 16/24] improve documentation --- src/t8_forest/t8_forest_ghost.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/t8_forest/t8_forest_ghost.h b/src/t8_forest/t8_forest_ghost.h index a6c4eba608..f68c66f7ac 100644 --- a/src/t8_forest/t8_forest_ghost.h +++ b/src/t8_forest/t8_forest_ghost.h @@ -69,7 +69,7 @@ t8_forest_ghost_get_tree_element_offset (t8_forest_t forest, t8_locidx_t lghost_ t8_locidx_t t8_forest_ghost_tree_num_elements (t8_forest_t forest, t8_locidx_t lghost_tree); -/** Retrieves the ghost index of an element in a specific ghost tree. +/** Retrieves a ghost element from its ghost tree given the element's linear id. * * \param [in] forest The forest object. * \param [in] lghost_tree The local index of the ghost tree. @@ -82,7 +82,7 @@ const t8_element_t * t8_ghost_get_ghost_in_tree_from_linear_id (t8_forest_t forest, t8_locidx_t lghost_tree, t8_linearidx_t linear_id, int element_level, t8_locidx_t *loc_ghost_id); -/** Retrieves the ghost index of an element in a specific ghost tree. +/** Retrieves the local index of a ghost element in its specific ghost tree. * * \param [in] forest The forest object. * \param [in] lghost_tree The local index of the ghost tree. From c79445b8a421ce3f6a57db99ddc6e7a09eb2cad8 Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 29 Oct 2024 12:42:30 +0100 Subject: [PATCH 17/24] Add test for ghost in tree getters --- test/CMakeLists.txt | 1 + test/t8_forest/t8_gtest_ghost_in_tree.cxx | 149 ++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 test/t8_forest/t8_gtest_ghost_in_tree.cxx diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ef9f9ba71a..cc738e695a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -96,6 +96,7 @@ add_t8_test( NAME t8_gtest_find_owner_parallel SOURCES t8_gtest_main.cx add_t8_test( NAME t8_gtest_user_data_parallel SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_user_data.cxx ) add_t8_test( NAME t8_gtest_transform_serial SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_transform.cxx ) add_t8_test( NAME t8_gtest_ghost_exchange_parallel SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_ghost_exchange.cxx ) +add_t8_test( NAME t8_gtest_ghost_in_tree SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_ghost_in_tree.cxx ) add_t8_test( NAME t8_gtest_ghost_delete_parallel SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_ghost_delete.cxx ) add_t8_test( NAME t8_gtest_ghost_and_owner_parallel SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_ghost_and_owner.cxx ) add_t8_test( NAME t8_gtest_balance_parallel SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_balance.cxx ) diff --git a/test/t8_forest/t8_gtest_ghost_in_tree.cxx b/test/t8_forest/t8_gtest_ghost_in_tree.cxx new file mode 100644 index 0000000000..ab152e8f3b --- /dev/null +++ b/test/t8_forest/t8_gtest_ghost_in_tree.cxx @@ -0,0 +1,149 @@ +/* + This file is part of t8code. + t8code is a C library to manage a collection (a forest) of multiple + connected adaptive space-trees of general element classes in parallel. + + Copyright (C) 2015 the developers + + t8code is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + t8code is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with t8code; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include "test/t8_cmesh_generator/t8_cmesh_example_sets.hxx" +#include + +/* This test program tests the t8_ghost_get_ghost_in_tree_from_linear_id + * and t8_ghost_get_ghost_id_in_tree function of the Ghost interface. + * We adapt a forest and create its ghost layer. Afterwards, we + * parse through all ghost elements and perform checks. + */ + +static int +t8_test_adapt_second_element (t8_forest_t forest, t8_forest_t forest_from, t8_locidx_t which_tree, + t8_locidx_t lelement_id, t8_eclass_scheme_c *ts, const int is_family, + const int num_elements, t8_element_t *elements[]) +{ + /* refine every second element up to the provided maximum level */ + int level = ts->t8_element_level (elements[0]); + t8_linearidx_t eid = ts->t8_element_get_linear_id (elements[0], level); + int maxlevel = *(int *) t8_forest_get_user_data (forest); + + if (eid % 2 && level < maxlevel) { + return 1; + } + return 0; +} + +class forest_ghost_in_tree: public testing::TestWithParam { + protected: + void + SetUp () override + { + + scheme = t8_scheme_new_default_cxx (); + /* Construct a cmesh */ + cmesh = GetParam ()->cmesh_create (); + if (t8_cmesh_is_empty (cmesh)) { + /* empty cmeshes do not have ghosts and are skipped */ + GTEST_SKIP (); + } + + /* Create a uniformly refined forest */ + t8_forest_t forest_uniform = t8_forest_new_uniform (cmesh, scheme, level, 1, sc_MPI_COMM_WORLD); + /* Adapt the forest */ + int maxlevel = level + 2; + forest_adapt = t8_forest_new_adapt (forest, t8_test_gao_adapt, 1, 1, &maxlevel); + } + void + TearDown () override + { + t8_cmesh_destroy (&cmesh); + t8_scheme_cxx_unref (&scheme); + } + t8_cmesh_t cmesh; + t8_scheme_cxx_t *scheme; + t8_forest_t forest_adapt; + int level = // initial uniform refinement level +#if T8_ENABLE_LESS_TESTS + 5; // less-tests, shorter runtime +#else + 7; // more tests, longer runtime +#endif +}; + +// For reference: +// t8_ghost_get_ghost_id_in_tree +// Retrieves the local index of a ghost element in its specific ghost tree. +// +// t8_ghost_get_ghost_in_tree_from_linear_id +// Retrieves a ghost element from its ghost tree given the element's linear id. + +TEST_P (forest_ghost_in_tree, test_get_ghost_id_in_tree) +{ + const t8_locidx_t num_ghost_trees = t8_forest_get_num_ghost_trees (forest_adapt); + const t8_locidx_t num_local_trees = t8_forest_get_num_local_trees (forest_adapt); + + for (t8_locidx_t ighost_tree = 0; ighost_tree < num_ghost_trees; ++ighost_tree) { + // iterate over all Ghost trees + // Get the ghost elements of that tree + const t8_element_array_t *ghosts = t8_forest_ghost_get_tree_elements (forest_adapt, ighost_tree); + t8_locidx_t ghost_index_in_tree = 0; + for (const t8_element_t &ighost : ghosts) { // loop over all ghost elements + // Compute the index in the tree of the current ghost + const t8_locidx_t check_ghost_index_in_tree = t8_ghost_get_ghost_id_in_tree (forest_adapt, ighost_tree, ighost); + ASSERT_GE (check_ghost_index_in_tree, 0) << "Ghost element was not found."; // The ghost must have been found. + // Check that the index is correct + EXPECT_EQ (check_ghost_index_in_tree, ghost_index_in_tree) << "Wrong index returned for ghost element."; + ++ghost_index_in_tree; + } + } +} + +TEST_P (forest_ghost_in_tree, test_get_ghost_in_tree_from_linear_id) +{ + const t8_locidx_t num_ghost_trees = t8_forest_get_num_ghost_trees (forest_adapt); + const t8_locidx_t num_local_trees = t8_forest_get_num_local_trees (forest_adapt); + + for (t8_locidx_t ighost_tree = 0; ighost_tree < num_ghost_trees; ++ighost_tree) { + // iterate over all Ghost trees + // Get the ghost elements of that tree + const t8_element_array_t *ghosts = t8_forest_ghost_get_tree_elements (forest_adapt, ighost_tree); + t8_locidx_t ghost_index_in_tree = 0; + for (const t8_element_t &ighost : ghosts) { // loop over all ghost elements + // Now checking t8_ghost_get_ghost_in_tree_from_linear_id + const t8_eclass_t eclass = t8_forest_get_tree_class (forest_adapt, num_local_trees + ighost_tree); + const t8_eclass_scheme_c *scheme = t8_forest_get_eclass_scheme (forest_adapt, eclass); + const int ghost_level = scheme->t8_element_level (ghost); + const t8_linearidx_t ghost_id = scheme->t8_element_get_linear_id (ghost, ghost_level); + t8_locidx_t check_ghost_index; + const t8_element_t *check_ghost = t8_ghost_get_ghost_in_tree_from_linear_id (forest_adapt, ighost_tree, ghost_id, + ghost_level, &check_ghost_index); + + EXPECT_TRUE (scheme->t8_element_equal (ighost, check_ghost)) << "Returned wrong ghost element."; + EXPECT_EQ (check_ghost_index, ghost_index_in_tree) << "Returned wrong ghost index."; + + ++ghost_index_in_tree; + } + } +} + +INSTANTIATE_TEST_SUITE_P (t8_gtest_forest_ghost_in_tree, forest_ghost_in_tree, AllCmeshsParam, + pretty_print_base_example); From a05f39ad5b35ca9342e9647f9ccf9e437727321f Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 29 Oct 2024 13:04:47 +0100 Subject: [PATCH 18/24] fix assertion logic --- src/t8_forest/t8_forest_ghost.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/t8_forest/t8_forest_ghost.cxx b/src/t8_forest/t8_forest_ghost.cxx index 07c9543df5..a9ad8c7a91 100644 --- a/src/t8_forest/t8_forest_ghost.cxx +++ b/src/t8_forest/t8_forest_ghost.cxx @@ -310,7 +310,7 @@ t8_ghost_get_ghost_in_tree_from_linear_id (t8_forest_t forest, t8_locidx_t lghos T8_ASSERT (t8_forest_is_committed (forest)); const t8_element_array_t *ghost_elements = t8_forest_ghost_get_tree_elements (forest, lghost_tree); T8_ASSERT (ghost_elements != NULL); - T8_ASSERT (forest->incomplete_trees); // This function will not word properlt with incomplete trees. + T8_ASSERT (!forest->incomplete_trees); // This function will not word properlt with incomplete trees. /* Search for the element. * The search returns the largest index i, * such that the element at position i has a smaller id than the given one. From 039efd7079d738ad774c786b3b94f2d63d4a4a10 Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 29 Oct 2024 13:05:13 +0100 Subject: [PATCH 19/24] more const --- src/t8_forest/t8_forest_ghost.cxx | 2 +- src/t8_forest/t8_forest_ghost.h | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/t8_forest/t8_forest_ghost.cxx b/src/t8_forest/t8_forest_ghost.cxx index a9ad8c7a91..4cc515052d 100644 --- a/src/t8_forest/t8_forest_ghost.cxx +++ b/src/t8_forest/t8_forest_ghost.cxx @@ -324,7 +324,7 @@ t8_ghost_get_ghost_in_tree_from_linear_id (t8_forest_t forest, t8_locidx_t lghos } t8_locidx_t -t8_ghost_get_ghost_id_in_tree (t8_forest_t forest, t8_locidx_t lghost_tree, t8_element_t *ghost_element) +t8_ghost_get_ghost_id_in_tree (t8_forest_t forest, t8_locidx_t lghost_tree, const t8_element_t *ghost_element) { T8_ASSERT (t8_forest_is_committed (forest)); const t8_element_array_t *ghost_elements = t8_forest_ghost_get_tree_elements (forest, lghost_tree); diff --git a/src/t8_forest/t8_forest_ghost.h b/src/t8_forest/t8_forest_ghost.h index f68c66f7ac..1edee32ddf 100644 --- a/src/t8_forest/t8_forest_ghost.h +++ b/src/t8_forest/t8_forest_ghost.h @@ -79,8 +79,9 @@ t8_forest_ghost_tree_num_elements (t8_forest_t forest, t8_locidx_t lghost_tree); * \return The ghost element. nullptr if no ghost was found. */ const t8_element_t * -t8_ghost_get_ghost_in_tree_from_linear_id (t8_forest_t forest, t8_locidx_t lghost_tree, t8_linearidx_t linear_id, - int element_level, t8_locidx_t *loc_ghost_id); +t8_ghost_get_ghost_in_tree_from_linear_id (t8_forest_t forest, const t8_locidx_t lghost_tree, + const t8_linearidx_t linear_id, const int element_level, + t8_locidx_t *loc_ghost_id); /** Retrieves the local index of a ghost element in its specific ghost tree. * @@ -91,7 +92,7 @@ t8_ghost_get_ghost_in_tree_from_linear_id (t8_forest_t forest, t8_locidx_t lghos * -1 if no ghost element was found. */ t8_locidx_t -t8_ghost_get_ghost_id_in_tree (t8_forest_t forest, t8_locidx_t lghost_tree, t8_element_t *ghost_element); +t8_ghost_get_ghost_id_in_tree (t8_forest_t forest, const t8_locidx_t lghost_tree, const t8_element_t *ghost_element); /** Get a pointer to the ghost element array of a ghost tree. * \param [in] forest The forest. Ghost layer must exist. From 74322410e7a08533d1572dfcc879b2dcfa0c2a08 Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 29 Oct 2024 13:05:48 +0100 Subject: [PATCH 20/24] minor test changes --- test/t8_forest/t8_gtest_ghost_in_tree.cxx | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/test/t8_forest/t8_gtest_ghost_in_tree.cxx b/test/t8_forest/t8_gtest_ghost_in_tree.cxx index ab152e8f3b..0cbc5dc268 100644 --- a/test/t8_forest/t8_gtest_ghost_in_tree.cxx +++ b/test/t8_forest/t8_gtest_ghost_in_tree.cxx @@ -29,6 +29,7 @@ #include #include "test/t8_cmesh_generator/t8_cmesh_example_sets.hxx" #include +#include /* This test program tests the t8_ghost_get_ghost_in_tree_from_linear_id * and t8_ghost_get_ghost_id_in_tree function of the Ghost interface. @@ -58,7 +59,6 @@ class forest_ghost_in_tree: public testing::TestWithParam SetUp () override { - scheme = t8_scheme_new_default_cxx (); /* Construct a cmesh */ cmesh = GetParam ()->cmesh_create (); if (t8_cmesh_is_empty (cmesh)) { @@ -66,26 +66,33 @@ class forest_ghost_in_tree: public testing::TestWithParam GTEST_SKIP (); } + scheme = t8_scheme_new_default_cxx (); /* Create a uniformly refined forest */ t8_forest_t forest_uniform = t8_forest_new_uniform (cmesh, scheme, level, 1, sc_MPI_COMM_WORLD); /* Adapt the forest */ int maxlevel = level + 2; - forest_adapt = t8_forest_new_adapt (forest, t8_test_gao_adapt, 1, 1, &maxlevel); + forest_adapt = t8_forest_new_adapt (forest_uniform, t8_test_adapt_second_element, 1, 1, &maxlevel); + // We need to update the cmesh since it changes with adapt. + cmesh = t8_forest_get_cmesh (forest_adapt); } void TearDown () override { - t8_cmesh_destroy (&cmesh); - t8_scheme_cxx_unref (&scheme); + if (t8_cmesh_is_empty (cmesh)) { + t8_cmesh_destroy (&cmesh); + } + else { + t8_forest_unref (&forest_adapt); + } } t8_cmesh_t cmesh; t8_scheme_cxx_t *scheme; t8_forest_t forest_adapt; int level = // initial uniform refinement level #if T8_ENABLE_LESS_TESTS - 5; // less-tests, shorter runtime + 3; // less-tests, shorter runtime #else - 7; // more tests, longer runtime + 5; // more tests, longer runtime #endif }; From 183b117b8e48761ad1c49959346ca860963a82a1 Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 29 Oct 2024 13:06:04 +0100 Subject: [PATCH 21/24] temporarily remove foreach loop over ghosts --- test/t8_forest/t8_gtest_ghost_in_tree.cxx | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/test/t8_forest/t8_gtest_ghost_in_tree.cxx b/test/t8_forest/t8_gtest_ghost_in_tree.cxx index 0cbc5dc268..97d02ce5fb 100644 --- a/test/t8_forest/t8_gtest_ghost_in_tree.cxx +++ b/test/t8_forest/t8_gtest_ghost_in_tree.cxx @@ -113,13 +113,16 @@ TEST_P (forest_ghost_in_tree, test_get_ghost_id_in_tree) // Get the ghost elements of that tree const t8_element_array_t *ghosts = t8_forest_ghost_get_tree_elements (forest_adapt, ighost_tree); t8_locidx_t ghost_index_in_tree = 0; - for (const t8_element_t &ighost : ghosts) { // loop over all ghost elements + //for (const t8_element_t &ighost : ghosts) { // loop over all ghost elements + + for (; ghost_index_in_tree < t8_element_array_get_count (ghosts); ++ghost_index_in_tree) { + const t8_element_t *ighost = t8_element_array_index_locidx (ghosts, ghost_index_in_tree); // Compute the index in the tree of the current ghost const t8_locidx_t check_ghost_index_in_tree = t8_ghost_get_ghost_id_in_tree (forest_adapt, ighost_tree, ighost); ASSERT_GE (check_ghost_index_in_tree, 0) << "Ghost element was not found."; // The ghost must have been found. // Check that the index is correct EXPECT_EQ (check_ghost_index_in_tree, ghost_index_in_tree) << "Wrong index returned for ghost element."; - ++ghost_index_in_tree; + // ++ghost_index_in_tree; } } } @@ -134,12 +137,15 @@ TEST_P (forest_ghost_in_tree, test_get_ghost_in_tree_from_linear_id) // Get the ghost elements of that tree const t8_element_array_t *ghosts = t8_forest_ghost_get_tree_elements (forest_adapt, ighost_tree); t8_locidx_t ghost_index_in_tree = 0; - for (const t8_element_t &ighost : ghosts) { // loop over all ghost elements + //for (const t8_element_t &ighost : ghosts) { // loop over all ghost elements + for (; ghost_index_in_tree < t8_element_array_get_count (ghosts); ++ghost_index_in_tree) { + + const t8_element_t *ighost = t8_element_array_index_locidx (ghosts, ghost_index_in_tree); // Now checking t8_ghost_get_ghost_in_tree_from_linear_id const t8_eclass_t eclass = t8_forest_get_tree_class (forest_adapt, num_local_trees + ighost_tree); const t8_eclass_scheme_c *scheme = t8_forest_get_eclass_scheme (forest_adapt, eclass); - const int ghost_level = scheme->t8_element_level (ghost); - const t8_linearidx_t ghost_id = scheme->t8_element_get_linear_id (ghost, ghost_level); + const int ghost_level = scheme->t8_element_level (ighost); + const t8_linearidx_t ghost_id = scheme->t8_element_get_linear_id (ighost, ghost_level); t8_locidx_t check_ghost_index; const t8_element_t *check_ghost = t8_ghost_get_ghost_in_tree_from_linear_id (forest_adapt, ighost_tree, ghost_id, ghost_level, &check_ghost_index); @@ -147,7 +153,7 @@ TEST_P (forest_ghost_in_tree, test_get_ghost_in_tree_from_linear_id) EXPECT_TRUE (scheme->t8_element_equal (ighost, check_ghost)) << "Returned wrong ghost element."; EXPECT_EQ (check_ghost_index, ghost_index_in_tree) << "Returned wrong ghost index."; - ++ghost_index_in_tree; + // ++ghost_index_in_tree; } } } From e1ee81f463eac4d1f983f75b9294ede4125a0c0b Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Mon, 4 Nov 2024 12:10:07 +0100 Subject: [PATCH 22/24] Add negative result tests --- src/t8_forest/t8_forest_ghost.cxx | 1 + test/t8_forest/t8_gtest_ghost_in_tree.cxx | 63 ++++++++++++++++++++--- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/t8_forest/t8_forest_ghost.cxx b/src/t8_forest/t8_forest_ghost.cxx index 4cc515052d..9abaad318c 100644 --- a/src/t8_forest/t8_forest_ghost.cxx +++ b/src/t8_forest/t8_forest_ghost.cxx @@ -320,6 +320,7 @@ t8_ghost_get_ghost_in_tree_from_linear_id (t8_forest_t forest, t8_locidx_t lghos /* The element was found */ return t8_element_array_index_locidx (ghost_elements, *loc_ghost_id); } + *loc_ghost_id = -1; return nullptr; } diff --git a/test/t8_forest/t8_gtest_ghost_in_tree.cxx b/test/t8_forest/t8_gtest_ghost_in_tree.cxx index 97d02ce5fb..16487fb849 100644 --- a/test/t8_forest/t8_gtest_ghost_in_tree.cxx +++ b/test/t8_forest/t8_gtest_ghost_in_tree.cxx @@ -112,10 +112,9 @@ TEST_P (forest_ghost_in_tree, test_get_ghost_id_in_tree) // iterate over all Ghost trees // Get the ghost elements of that tree const t8_element_array_t *ghosts = t8_forest_ghost_get_tree_elements (forest_adapt, ighost_tree); - t8_locidx_t ghost_index_in_tree = 0; - //for (const t8_element_t &ighost : ghosts) { // loop over all ghost elements - - for (; ghost_index_in_tree < t8_element_array_get_count (ghosts); ++ghost_index_in_tree) { + // loop over all ghosts in that tree + const size_t num_ghosts_of_tree = t8_element_array_get_count (ghosts); + for (t8_locidx_t ghost_index_in_tree = 0; ghost_index_in_tree < num_ghosts_of_tree; ++ghost_index_in_tree) { const t8_element_t *ighost = t8_element_array_index_locidx (ghosts, ghost_index_in_tree); // Compute the index in the tree of the current ghost const t8_locidx_t check_ghost_index_in_tree = t8_ghost_get_ghost_id_in_tree (forest_adapt, ighost_tree, ighost); @@ -127,6 +126,24 @@ TEST_P (forest_ghost_in_tree, test_get_ghost_id_in_tree) } } +TEST_P (forest_ghost_in_tree, test_get_ghost_id_in_tree_negative) +{ + // Get a not ghost element and try to query it. + // We check for the correct return value. + if (t8_forest_get_local_num_elements (forest_adapt) > 0) { + // Get the first element of the forest. + // This will not be a ghost. + const t8_element_t *element = t8_forest_get_element_in_tree (forest_adapt, 0, 0); + // Loop over all ghost trees + const t8_locidx_t num_ghost_trees = t8_forest_get_num_ghost_trees (forest_adapt); + for (t8_locidx_t ighost_tree = 0; ighost_tree < num_ghost_trees; ++ighost_tree) { + t8_locidx_t check_ghost_id = t8_ghost_get_ghost_id_in_tree (forest_adapt, ighost_tree, element); + /* Since the element is not a ghost, we expect -1 as return value. */ + EXPECT_EQ (check_ghost_id, -1) << "Element was wrongly recognized as ghost."; + } + } +} + TEST_P (forest_ghost_in_tree, test_get_ghost_in_tree_from_linear_id) { const t8_locidx_t num_ghost_trees = t8_forest_get_num_ghost_trees (forest_adapt); @@ -136,10 +153,9 @@ TEST_P (forest_ghost_in_tree, test_get_ghost_in_tree_from_linear_id) // iterate over all Ghost trees // Get the ghost elements of that tree const t8_element_array_t *ghosts = t8_forest_ghost_get_tree_elements (forest_adapt, ighost_tree); - t8_locidx_t ghost_index_in_tree = 0; - //for (const t8_element_t &ighost : ghosts) { // loop over all ghost elements - for (; ghost_index_in_tree < t8_element_array_get_count (ghosts); ++ghost_index_in_tree) { - + // Loop over all ghost elements of that tree + const size_t num_ghosts_of_tree = t8_element_array_get_count (ghosts); + for (t8_locidx_t ghost_index_in_tree = 0; ghost_index_in_tree < num_ghosts_of_tree; ++ghost_index_in_tree) { const t8_element_t *ighost = t8_element_array_index_locidx (ghosts, ghost_index_in_tree); // Now checking t8_ghost_get_ghost_in_tree_from_linear_id const t8_eclass_t eclass = t8_forest_get_tree_class (forest_adapt, num_local_trees + ighost_tree); @@ -158,5 +174,36 @@ TEST_P (forest_ghost_in_tree, test_get_ghost_in_tree_from_linear_id) } } +TEST_P (forest_ghost_in_tree, test_get_ghost_in_tree_from_linear_id_negative) +{ + // Get a not ghost element and try to query it. + // We check for the correct return value. + const t8_locidx_t tree_id = 0; // We check the first element of the first tree + const t8_locidx_t element_id = 0; + if (t8_forest_get_local_num_elements (forest_adapt) > 0) { + // Get the first element of the forest. + // This will not be a ghost. + const t8_element_t *element = t8_forest_get_element_in_tree (forest_adapt, tree_id, element_id); + // Loop over all ghost trees + const t8_locidx_t num_ghost_trees = t8_forest_get_num_ghost_trees (forest_adapt); + for (t8_locidx_t ighost_tree = 0; ighost_tree < num_ghost_trees; ++ighost_tree) { + // Now checking t8_ghost_get_ghost_in_tree_from_linear_id + // Get the scheme + const t8_eclass_t eclass = t8_forest_get_tree_class (forest_adapt, tree_id); + const t8_eclass_scheme_c *scheme = t8_forest_get_eclass_scheme (forest_adapt, eclass); + // Get the element's level and linear id + const int element_level = scheme->t8_element_level (element); + const t8_linearidx_t element_id = scheme->t8_element_get_linear_id (element, element_level); + // Query t8_ghost_get_ghost_in_tree_from_linear_id + t8_locidx_t check_ghost_index; + const t8_element_t *check_ghost = t8_ghost_get_ghost_in_tree_from_linear_id ( + forest_adapt, ighost_tree, element_id, element_level, &check_ghost_index); + // The element should not have been found - we check for the error return values. + EXPECT_EQ (check_ghost_index, -1) << "Wrongly found ghost index for non-existing ghost."; + EXPECT_EQ (check_ghost, nullptr) << "Wrongly found ghost element for non-existing ghost."; + } + } +} + INSTANTIATE_TEST_SUITE_P (t8_gtest_forest_ghost_in_tree, forest_ghost_in_tree, AllCmeshsParam, pretty_print_base_example); From 3dd4d2cd6b2fd5f93bc7782f37f720d99bccef22 Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Fri, 8 Nov 2024 09:56:22 +0100 Subject: [PATCH 23/24] fix private.c rename in Makefile --- src/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Makefile.am b/src/Makefile.am index ac4b843944..95a7ee8995 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -144,7 +144,7 @@ libt8_compiled_sources = \ src/t8_geometry/t8_geometry_implementations/t8_geometry_zero.cxx \ src/t8_geometry/t8_geometry_implementations/t8_geometry_examples.cxx \ src/t8_forest/t8_forest_partition.cxx src/t8_forest/t8_forest.cxx \ - src/t8_forest/t8_forest_private.c \ + src/t8_forest/t8_forest_private.cxx \ src/t8_forest/t8_forest_ghost.cxx src/t8_forest/t8_forest_iterate.cxx \ src/t8_version.c \ src/t8_vtk.c src/t8_forest/t8_forest_balance.cxx \ From 6c699d2821e05af5eaa1b4c34afbd1ed4bd18adf Mon Sep 17 00:00:00 2001 From: Johannes Holke Date: Tue, 12 Nov 2024 10:27:09 +0100 Subject: [PATCH 24/24] remove old files from Makefile --- src/Makefile.am | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 95a7ee8995..3bc8c1058e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -53,8 +53,6 @@ libt8_installed_headers_forest = \ src/t8_forest/t8_forest_profiling.h \ src/t8_forest/t8_forest_io.h \ src/t8_forest/t8_forest_adapt.h \ - src/t8_forest/t8_forest_vtk.h \ - src/t8_forest/t8_forest_to_vtkUnstructured.hxx \ src/t8_forest/t8_forest_iterate.h src/t8_forest/t8_forest_partition.h \ src/t8_forest/t8_forest_ghost_interface_wrapper.h \ src/t8_forest/t8_forest_ghost_interface.h \