Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce interface function t8_element_equal #852

Merged
merged 10 commits into from
Nov 16, 2023
17 changes: 17 additions & 0 deletions src/t8_element_c_interface.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ t8_element_compare (const t8_eclass_scheme_c *ts, const t8_element_t *elem1, con
return ts->t8_element_compare (elem1, elem2);
}

int
t8_element_equal (const t8_eclass_scheme_c *ts, const t8_element_t *elem1, const t8_element_t *elem2)
{
T8_ASSERT (ts != NULL);

return ts->t8_element_equal (elem1, elem2);
}

void
t8_element_parent (const t8_eclass_scheme_c *ts, const t8_element_t *elem, t8_element_t *parent)
{
Expand Down Expand Up @@ -422,6 +430,15 @@ t8_element_debug_print (const t8_eclass_scheme_c *ts, const t8_element_t *elem)

return ts->t8_element_debug_print (elem);
}

void
t8_element_to_string (const t8_eclass_scheme_c *ts, const t8_element_t *elem, char *debug_string, const int string_size)
{
T8_ASSERT (ts != NULL);
T8_ASSERT (debug_string != NULL);

ts->t8_element_to_string (elem, debug_string, string_size);
}
#endif

void
Expand Down
21 changes: 20 additions & 1 deletion src/t8_element_c_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ t8_element_level (const t8_eclass_scheme_c *ts, const t8_element_t *elem);
void
t8_element_copy (const t8_eclass_scheme_c *ts, const t8_element_t *source, t8_element_t *dest);

/** Compare two elements.
/** Compare two elements with respect to the scheme.
* \param [in] ts Implementation of a class scheme.
* \param [in] elem1 The first element.
* \param [in] elem2 The second element.
Expand All @@ -94,6 +94,15 @@ t8_element_copy (const t8_eclass_scheme_c *ts, const t8_element_t *source, t8_el
int
t8_element_compare (const t8_eclass_scheme_c *ts, const t8_element_t *elem1, const t8_element_t *elem2);

/** Check if two elements are equal.
* \param [in] ts Implementation of a class scheme.
* \param [in] elem1 The first element.
* \param [in] elem2 The second element.
* \return 1 if the elements are equal, 0 if they are not equal
*/
int
t8_element_equal (const t8_eclass_scheme_c *ts, const t8_element_t *elem1, const t8_element_t *elem2);

/** Compute the parent of a given element \b elem and store it in \b parent.
* \b parent needs to be an existing element. No memory is allocated by this function.
* \b elem and \b parent can point to the same element, then the entries of
Expand Down Expand Up @@ -644,6 +653,16 @@ t8_element_is_valid (const t8_eclass_scheme_c *ts, const t8_element_t *elem);
*/
void
t8_element_debug_print (const t8_eclass_scheme_c *ts, const t8_element_t *elem);

/**
* \brief Fill a string with readable information about the element
*
* \param[in] elem The element to translate into human-readable information
* \param[in, out] debug_string The string to fill.
*/
void
t8_element_to_string (const t8_eclass_scheme_c *ts, const t8_element_t *elem, char *debug_string,
const int string_size);
#endif

/** Allocate memory for an array of elements of a given class and initialize them.
Expand Down
20 changes: 20 additions & 0 deletions src/t8_element_cxx.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ struct t8_eclass_scheme
t8_element_compare (const t8_element_t *elem1, const t8_element_t *elem2) const
= 0;

/** Check if two elements are equal.
* \param [in] ts Implementation of a class scheme.
* \param [in] elem1 The first element.
* \param [in] elem2 The second element.
* \return 1 if the elements are equal, 0 if they are not equal
*/
virtual int
t8_element_equal (const t8_element_t *elem1, const t8_element_t *elem2) const
= 0;

/** Compute the parent of a given element \b elem and store it in \b parent.
* \b parent needs to be an existing element. No memory is allocated by this function.
* \b elem and \b parent can point to the same element, then the entries of
Expand Down Expand Up @@ -702,6 +712,16 @@ struct t8_eclass_scheme
virtual void
t8_element_debug_print (const t8_element_t *elem) const
= 0;

/**
* \brief Fill a string with readable information about the element
*
* \param[in] elem The element to translate into human-readable information
* \param[in, out] debug_string The string to fill.
*/
virtual void
t8_element_to_string (const t8_element_t *elem, char *debug_string, const int string_size) const
= 0;
#endif

/** Allocate memory for an array of elements of a given class and initialize them.
Expand Down
6 changes: 3 additions & 3 deletions src/t8_forest/t8_forest_adapt.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ t8_forest_is_family_callback (t8_eclass_scheme_c *ts, const int num_elements, t8

for (int iter = 0; iter < num_elements; iter++) {
ts->t8_element_parent (elements[iter], element_parent_compare);
if (ts->t8_element_compare (element_parent, element_parent_compare)) {
if (!ts->t8_element_equal (element_parent, element_parent_compare)) {
ts->t8_element_destroy (1, &element_parent);
ts->t8_element_destroy (1, &element_parent_compare);
return 0;
Expand All @@ -73,7 +73,7 @@ t8_forest_is_family_callback (t8_eclass_scheme_c *ts, const int num_elements, t8
if (num_elements < ts->t8_element_num_siblings (elements[0])) {
for (int iter = num_elements; iter < num_elements; iter++) {
ts->t8_element_parent (elements[iter], element_parent_compare);
if (!ts->t8_element_compare (element_parent, element_parent_compare)) {
if (ts->t8_element_equal (element_parent, element_parent_compare)) {
ts->t8_element_destroy (1, &element_parent);
ts->t8_element_destroy (1, &element_parent_compare);
return 0;
Expand Down Expand Up @@ -155,7 +155,7 @@ t8_forest_pos (t8_forest_t forest, t8_eclass_scheme_c *ts, t8_element_array_t *t
break;
}
ts->t8_element_parent (element_compare, element_parent_compare);
if (ts->t8_element_compare (element_parent, element_parent_compare)) {
if (!ts->t8_element_equal (element_parent, element_parent_compare)) {
break;
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/t8_forest/t8_forest_cxx.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ t8_forest_is_incomplete_family (const t8_forest_t forest, const t8_locidx_t ltre
}
tscheme->t8_element_parent (elements[family_iter], element_compare);
/* If the levels are equal, check if the parents are too. */
if (0 != tscheme->t8_element_compare (element_parent_current, element_compare)) {
if (!tscheme->t8_element_equal (element_parent_current, element_compare)) {
family_size = family_iter;
break;
}
Expand Down Expand Up @@ -352,7 +352,7 @@ t8_forest_is_equal (t8_forest_t forest_a, t8_forest_t forest_b)
elem_a = t8_forest_get_element_in_tree (forest_a, itree, ielem);
elem_b = t8_forest_get_element_in_tree (forest_b, itree, ielem);
/* check for equality */
if (ts_a->t8_element_compare (elem_a, elem_b)) {
if (!ts_a->t8_element_equal (elem_a, elem_b)) {
/* The elements are not equal */
return 0;
}
Expand Down Expand Up @@ -1628,7 +1628,7 @@ t8_forest_tree_shared (t8_forest_t forest, int first_or_last)
/* We can now check whether the first/last possible descendant matches the
* first/last local descendant */
tree_desc = first_or_last == 0 ? tree->first_desc : tree->last_desc;
ret = ts->t8_element_compare (desc, tree_desc);
ret = !ts->t8_element_equal (desc, tree_desc);
/* clean-up */
ts->t8_element_destroy (1, &element);
ts->t8_element_destroy (1, &desc);
Expand Down Expand Up @@ -2177,7 +2177,7 @@ t8_forest_leaf_face_neighbors (t8_forest_t forest, t8_locidx_t ltreeid, const t8
t8_element_t *check_element;
check_element = t8_forest_get_element (forest, element_indices[ineigh], &check_ltreeid);
T8_ASSERT (check_ltreeid == lneigh_treeid);
T8_ASSERT (!neigh_scheme->t8_element_compare (check_element, neighbor_leafs[ineigh]));
T8_ASSERT (neigh_scheme->t8_element_equal (check_element, neighbor_leafs[ineigh]));
}
#endif
}
Expand All @@ -2192,7 +2192,7 @@ t8_forest_leaf_face_neighbors (t8_forest_t forest, t8_locidx_t ltreeid, const t8
{
t8_element_t *check_element;
check_element = t8_forest_ghost_get_element (forest, lghost_treeid, element_indices[ineigh]);
T8_ASSERT (!neigh_scheme->t8_element_compare (check_element, neighbor_leafs[ineigh]));
T8_ASSERT (neigh_scheme->t8_element_equal (check_element, neighbor_leafs[ineigh]));
}
#endif
/* Add the element offset of previous ghosts to this index */
Expand Down Expand Up @@ -2686,9 +2686,9 @@ t8_forest_element_owners_at_face_recursion (t8_forest_t forest, t8_gloidx_t gtre

ts->t8_element_new (1, &test_desc);
ts->t8_element_last_descendant_face (element, face, test_desc, forest->maxlevel);
T8_ASSERT (!ts->t8_element_compare (test_desc, last_face_desc));
T8_ASSERT (ts->t8_element_equal (test_desc, last_face_desc));
ts->t8_element_first_descendant_face (element, face, test_desc, forest->maxlevel);
T8_ASSERT (!ts->t8_element_compare (test_desc, first_face_desc));
T8_ASSERT (ts->t8_element_equal (test_desc, first_face_desc));
ts->t8_element_destroy (1, &test_desc);
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/t8_forest/t8_forest_general.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ t8_forest_no_overlap (t8_forest_t forest);
* \param [in] forest_b The second forest.
* \return True if \a forest_a and \a forest_b do have the same
* number of local trees and each local tree has the same
* elements, that is \ref t8_element_compare returns false
* elements, that is \ref t8_element_equal returns true
* for each pair of elements of \a forest_a and \a forest_b.
* \note This function is not collective. It only returns the state on the current
* rank.
Expand Down
2 changes: 1 addition & 1 deletion src/t8_forest/t8_forest_ghost.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ t8_ghost_add_remote (t8_forest_t forest, t8_forest_ghost_t ghost, int remote_ran
int elem_count = t8_element_array_get_count (&remote_tree->elements);
for (ielem = 0; ielem < elem_count - 1; ielem++) {
test_el = t8_element_array_index_int (&remote_tree->elements, ielem);
SC_CHECK_ABORTF (ts->t8_element_compare (test_el, elem), "Local element %i already in remote ghosts at pos %i\n",
SC_CHECK_ABORTF (!ts->t8_element_equal (test_el, elem), "Local element %i already in remote ghosts at pos %i\n",
element_index, ielem);
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/t8_forest/t8_forest_iterate.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ t8_forest_iterate_faces (t8_forest_t forest, t8_locidx_t ltreeid, const t8_eleme
/* There is only one leaf left, we check whether it is the same as element
* and if so call the callback function */
leaf = t8_element_array_index_locidx (leaf_elements, 0);
if (!ts->t8_element_compare (element, leaf)) {
if (ts->t8_element_equal (element, leaf)) {
/* The element is the leaf, we are at the last stage of the recursion
* and can call the callback. */
(void) callback (forest, ltreeid, leaf, face, user_data, tree_lindex_of_first_leaf);
Expand Down Expand Up @@ -213,7 +213,7 @@ t8_forest_search_recursion (t8_forest_t forest, t8_locidx_t ltreeid, t8_eclass_t
SC_CHECK_ABORT (ts->t8_element_level (element) <= ts->t8_element_level (leaf),
"Search: element level greater than leaf level\n");
if (ts->t8_element_level (element) == ts->t8_element_level (leaf)) {
T8_ASSERT (!ts->t8_element_compare (element, leaf));
T8_ASSERT (ts->t8_element_equal (element, leaf));
/* The element is the leaf */
is_leaf = 1;
}
Expand Down Expand Up @@ -395,7 +395,7 @@ t8_forest_iterate_replace (t8_forest_t forest_new, t8_forest_t forest_old, t8_fo
t8_element_t *elem_parent;
ts->t8_element_new (1, &elem_parent);
ts->t8_element_parent (elem_new, elem_parent);
if (!ts->t8_element_compare (elem_old, elem_parent)) {
if (ts->t8_element_equal (elem_old, elem_parent)) {
/* elem_old got refined */
T8_ASSERT (level_new == level_old + 1);
const t8_locidx_t family_size = ts->t8_element_num_children (elem_old);
Expand All @@ -406,7 +406,7 @@ t8_forest_iterate_replace (t8_forest_t forest_new, t8_forest_t forest_old, t8_fo
for (t8_locidx_t ielem = 1; ielem < family_size; ielem++) {
elem_new_debug = t8_forest_get_element_in_tree (forest_new, itree, ielem_new + ielem);
ts->t8_element_parent (elem_new_debug, elem_parent);
SC_CHECK_ABORT (!ts->t8_element_compare (elem_old, elem_parent), "Family is not complete.");
SC_CHECK_ABORT (ts->t8_element_equal (elem_old, elem_parent), "Family is not complete.");
}
#endif
ts->t8_element_destroy (1, &elem_parent);
Expand All @@ -427,7 +427,7 @@ t8_forest_iterate_replace (t8_forest_t forest_new, t8_forest_t forest_old, t8_fo
t8_element_t *elem_parent;
ts->t8_element_new (1, &elem_parent);
ts->t8_element_parent (elem_old, elem_parent);
if (!ts->t8_element_compare (elem_new, elem_parent)) {
if (ts->t8_element_equal (elem_new, elem_parent)) {
/* elem_old got coarsened */
T8_ASSERT (level_new == level_old - 1);
/* Get size of family of old forest */
Expand All @@ -436,7 +436,7 @@ t8_forest_iterate_replace (t8_forest_t forest_new, t8_forest_t forest_old, t8_fo
ielem < ts->t8_element_num_children (elem_new) && ielem + ielem_old < elems_per_tree_old; ielem++) {
elem_old = t8_forest_get_element_in_tree (forest_old, itree, ielem_old + ielem);
ts->t8_element_parent (elem_old, elem_parent);
if (!ts->t8_element_compare (elem_new, elem_parent)) {
if (ts->t8_element_equal (elem_new, elem_parent)) {
family_size++;
}
}
Expand All @@ -448,7 +448,7 @@ t8_forest_iterate_replace (t8_forest_t forest_new, t8_forest_t forest_old, t8_fo
ielem++) {
elem_old_debug = t8_forest_get_element_in_tree (forest_old, itree, ielem_old - ielem);
ts->t8_element_parent (elem_old_debug, elem_parent);
SC_CHECK_ABORT (0 != ts->t8_element_compare (elem_new, elem_parent),
SC_CHECK_ABORT (!ts->t8_element_equal (elem_new, elem_parent),
"elem_old is not the first of the family.");
}
#endif
Expand All @@ -467,7 +467,7 @@ t8_forest_iterate_replace (t8_forest_t forest_new, t8_forest_t forest_old, t8_fo
}
else {
/* elem_old was untouched or got removed */
if (!ts->t8_element_compare (elem_new, elem_old)) {
if (ts->t8_element_equal (elem_new, elem_old)) {
/* elem_new = elem_old */
const int refine = 0;
replace_fn (forest_old, forest_new, itree, ts, refine, 1, ielem_old, 1, ielem_new);
Expand Down Expand Up @@ -518,7 +518,7 @@ t8_forest_iterate_replace (t8_forest_t forest_new, t8_forest_t forest_old, t8_fo
}
else {
/* elem_new = elem_old */
T8_ASSERT (!ts->t8_element_compare (elem_new, elem_old));
T8_ASSERT (ts->t8_element_equal (elem_new, elem_old));
const int refine = 0;
replace_fn (forest_old, forest_new, itree, ts, refine, 1, ielem_old, 1, ielem_new);
/* Advance to the next element */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,14 @@ t8_default_scheme_common_c::t8_element_general_function (const t8_element_t *ele
/* This function is intentionally left blank. */
}

#if T8_ENABLE_DEBUG
void
t8_default_scheme_common_c::t8_element_debug_print (const t8_element_t *elem) const
{
char debug_string[BUFSIZ];
t8_element_to_string (elem, debug_string, BUFSIZ);
t8_debugf ("%s\n", debug_string);
}
#endif

T8_EXTERN_C_END ();
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ class t8_default_scheme_common_c: public t8_eclass_scheme_c {
virtual void
t8_element_anchor (const t8_element_t *elem, int anchor[3]) const
= 0;
#if T8_ENABLE_DEBUG
virtual void
t8_element_debug_print (const t8_element_t *elem) const;
#endif
};

#endif /* !T8_DEFAULT_COMMON_CXX_HXX */
12 changes: 10 additions & 2 deletions src/t8_schemes/t8_default/t8_default_hex/t8_default_hex_cxx.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ t8_default_scheme_hex_c::t8_element_compare (const t8_element_t *elem1, const t8
return p8est_quadrant_compare ((const p8est_quadrant_t *) elem1, (const p8est_quadrant_t *) elem2);
}

int
t8_default_scheme_hex_c::t8_element_equal (const t8_element_t *elem1, const t8_element_t *elem2) const
{
return p8est_quadrant_is_equal ((const p8est_quadrant_t *) elem1, (const p8est_quadrant_t *) elem2);
}

void
t8_default_scheme_hex_c::t8_element_parent (const t8_element_t *elem, t8_element_t *parent) const
{
Expand Down Expand Up @@ -636,11 +642,13 @@ t8_default_scheme_hex_c::t8_element_is_valid (const t8_element_t *elem) const
}

void
t8_default_scheme_hex_c::t8_element_debug_print (const t8_element_t *elem) const
t8_default_scheme_hex_c::t8_element_to_string (const t8_element_t *elem, char *debug_string,
const int string_size) const
{
T8_ASSERT (t8_element_is_valid (elem));
T8_ASSERT (debug_string != NULL);
p8est_quadrant_t *hex = (p8est_quadrant_t *) elem;
p8est_quadrant_print (SC_LP_DEBUG, hex);
snprintf (debug_string, string_size, "x: %i, y: %i, z: %i, level: %i", hex->x, hex->y, hex->z, hex->level);
}
#endif

Expand Down
11 changes: 10 additions & 1 deletion src/t8_schemes/t8_default/t8_default_hex/t8_default_hex_cxx.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ struct t8_default_scheme_hex_c: public t8_default_scheme_common_c
virtual int
t8_element_compare (const t8_element_t *elem1, const t8_element_t *elem2) const;

/** Check if two elements are equal.
* \param [in] ts Implementation of a class scheme.
* \param [in] elem1 The first element.
* \param [in] elem2 The second element.
* \return 1 if the elements are equal, 0 if they are not equal
*/
virtual int
t8_element_equal (const t8_element_t *elem1, const t8_element_t *elem2) const;

/** Compute the parent of a given element \b elem and store it in \b parent.
* \b parent needs to be an existing element. No memory is allocated by this function.
* \b elem and \b parent can point to the same element, then the entries of
Expand Down Expand Up @@ -590,7 +599,7 @@ struct t8_default_scheme_hex_c: public t8_default_scheme_common_c
* \param [in] elem The element to print
*/
virtual void
t8_element_debug_print (const t8_element_t *elem) const;
t8_element_to_string (const t8_element_t *elem, char *debug_string, const int string_size) const;
#endif
};

Expand Down
13 changes: 11 additions & 2 deletions src/t8_schemes/t8_default/t8_default_line/t8_default_line_cxx.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ t8_default_scheme_line_c::t8_element_compare (const t8_element_t *elem1, const t
return t8_dline_compare ((const t8_dline_t *) elem1, (const t8_dline_t *) elem2);
}

int
t8_default_scheme_line_c::t8_element_equal (const t8_element_t *elem1, const t8_element_t *elem2) const
{
return t8_dline_equal ((const t8_dline_t *) elem1, (const t8_dline_t *) elem2);
}

void
t8_default_scheme_line_c::t8_element_parent (const t8_element_t *elem, t8_element_t *parent) const
{
Expand Down Expand Up @@ -400,10 +406,13 @@ t8_default_scheme_line_c::t8_element_is_valid (const t8_element_t *elem) const
}

void
t8_default_scheme_line_c::t8_element_debug_print (const t8_element_t *elem) const
t8_default_scheme_line_c::t8_element_to_string (const t8_element_t *elem, char *debug_string,
const int string_size) const
{
T8_ASSERT (t8_element_is_valid (elem));
t8_dline_debug_print ((const t8_dline_t *) elem);
T8_ASSERT (debug_string != NULL);
t8_dline_t *line = (t8_dline_t *) elem;
snprintf (debug_string, string_size, "x: %i, level: %i", line->x, line->level);
}
#endif

Expand Down
Loading