diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 30014e719f..f9b1767371 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -197,6 +197,8 @@ target_sources( T8 PRIVATE t8_cmesh/t8_cmesh_vertex_connectivity/t8_cmesh_vertex_conn_tree_to_vertex.cxx t8_cmesh/t8_cmesh_vertex_connectivity/t8_cmesh_vertex_conn_vertex_to_tree.cxx t8_cmesh/t8_cmesh_vertex_connectivity/t8_cmesh_vertex_connectivity.cxx + t8_cmesh/t8_cmesh_healpix/t8_cmesh_healpix.cxx + t8_cmesh/t8_cmesh_healpix/t8_geometry_healpix.cxx t8_cmesh/t8_cmesh_boundary_conditions/t8_cmesh_boundary_conditions.cxx t8_cmesh/t8_cmesh_boundary_conditions/internal/t8_cmesh_boundary_condition_handler.cxx t8_cmesh/t8_cmesh_boundary_conditions/internal/t8_cmesh_boundary_condition_handler_c_helper.cxx diff --git a/src/t8_cmesh/t8_cmesh_healpix/t8_cmesh_healpix.cxx b/src/t8_cmesh/t8_cmesh_healpix/t8_cmesh_healpix.cxx new file mode 100644 index 0000000000..22d69025ea --- /dev/null +++ b/src/t8_cmesh/t8_cmesh_healpix/t8_cmesh_healpix.cxx @@ -0,0 +1,143 @@ +/* + 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) 2026 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 + +/** + * @brief Helper struct for joining healpix elements for t8_cmesh_set_join. + */ +struct t8_healpix_join +{ + /** ID of tree 1 */ + t8_gloidx_t tree1; + + /** Index of face 1 */ + int face1; + + /** ID of tree 2 */ + t8_gloidx_t tree2; + + /** Index of face 2 */ + int face2; + + /** Orientation for face 1 and face 2 */ + int orient; +}; + +struct t8_healpix_join healpix_joins[24] + = { { 1, 3, 0, 1, 0 }, { 2, 3, 1, 1, 0 }, { 3, 1, 0, 3, 0 }, { 3, 3, 2, 1, 0 }, + + { 4, 1, 0, 0, 0 }, { 4, 3, 3, 2, 0 }, { 5, 1, 1, 0, 0 }, { 5, 3, 0, 2, 0 }, + + { 6, 1, 2, 0, 0 }, { 6, 3, 1, 2, 0 }, { 7, 1, 3, 0, 0 }, { 7, 3, 2, 2, 0 }, + + { 8, 1, 5, 0, 0 }, { 8, 3, 4, 2, 0 }, { 9, 0, 8, 2, 0 }, { 9, 1, 6, 0, 0 }, + + { 9, 3, 5, 2, 0 }, { 10, 0, 9, 2, 0 }, { 10, 1, 7, 0, 0 }, { 10, 3, 6, 2, 0 }, + + { 11, 0, 10, 2, 0 }, { 11, 1, 4, 0, 0 }, { 11, 2, 8, 0, 0 }, { 11, 3, 7, 2, 0 } + + }; + +/** + * The main logic for the HEALPix topology. + * Refer to: https://healpix.sourceforge.io/credits.php + * + * Registers the HEALPix geometry and sets up a 12-tree base coarse mesh consisting + * of quadrilateral faces. It explicitly establishes $O(1)$ manual + * face connectivity between neighboring trees according to the HEALPix adjacency table + * before committing the mesh. + * + * @param[in] comm The MPI communicator used to manage parallel partitioning/commit. + * @param[in] eclass The eclass that should be assigned + * @param[in] sphere_outer_radius The outer radius of the sphere. + * @param[in] sphere_inner_radius The inner radius of the sphere + * @return t8_cmesh_t A committed coarse mesh object containing the HEALPix base structure as a surface. + */ +t8_cmesh_t +t8_healpix_init (sc_MPI_Comm comm, t8_eclass eclass, double sphere_outer_radius, double sphere_inner_radius) +{ + + t8_cmesh_t cmesh; + t8_cmesh_init (&cmesh); + + const int ntrees = 12; + + /* Register geometry and retain the pointer */ + t8_cmesh_register_geometry (cmesh, sphere_outer_radius, sphere_inner_radius); + // If the mesh has no depth, set the class to quads to remove unnecessary elements + for (int itree = 0; itree < ntrees; itree++) { + t8_cmesh_set_tree_class (cmesh, itree, eclass); + } + + /* Explicit O(1) manual face joining */ + for (size_t i = 0; i < 24; ++i) { + t8_cmesh_set_join (cmesh, healpix_joins[i].tree1, healpix_joins[i].tree2, healpix_joins[i].face1, + healpix_joins[i].face2, healpix_joins[i].orient); + } + + t8_cmesh_commit (cmesh, comm); + return cmesh; +} + +/** + * Creates and initializes a new coarse mesh (cmesh) with HEALPix topology. + * + * Registers the HEALPix geometry and sets up a 12-tree base coarse mesh consisting + * of quadrilateral faces (`T8_ECLASS_QUAD`). It explicitly establishes $O(1)$ manual + * face connectivity between neighboring trees according to the HEALPix adjacency table + * before committing the mesh. + * + * @param[in] comm The MPI communicator used to manage parallel partitioning/commit. + * @param[in] surface_outer_radius The outer radius of the sphere. + * @param[in] surface_inner_radius The inner radius of the sphere + * @return t8_cmesh_t A committed coarse mesh object containing the HEALPix base structure as a surface. + */ +t8_cmesh_t +t8_cmesh_new_healpix_surface (sc_MPI_Comm comm, double surface_outer_radius, double surface_inner_radius) +{ + t8_cmesh_t cmesh = t8_healpix_init (comm, T8_ECLASS_QUAD, surface_outer_radius, surface_inner_radius); + return cmesh; +} + +/** + * Creates and initializes a new coarse mesh (cmesh) with HEALPix topology. + * + * Registers the HEALPix geometry and sets up a 12-tree base coarse mesh consisting + * of quadrilateral faces (`T8_ECLASS_HEX`). It explicitly establishes $O(1)$ manual + * face connectivity between neighboring trees according to the HEALPix adjacency table + * before committing the mesh. + * + * @param[in] comm The MPI communicator used to manage parallel partitioning/commit. + * @param[in] shell_radius The radius of the sphere. + * @return t8_cmesh_t A committed coarse mesh object containing the HEALPix base structure as a sphere. + */ +t8_cmesh_t +t8_cmesh_new_healpix_shell (sc_MPI_Comm comm, double shell_radius) +{ + t8_cmesh_t cmesh = t8_healpix_init (comm, T8_ECLASS_HEX, shell_radius, shell_radius); + return cmesh; +} diff --git a/src/t8_cmesh/t8_cmesh_healpix/t8_cmesh_healpix.h b/src/t8_cmesh/t8_cmesh_healpix/t8_cmesh_healpix.h new file mode 100644 index 0000000000..2e9ce4cf23 --- /dev/null +++ b/src/t8_cmesh/t8_cmesh_healpix/t8_cmesh_healpix.h @@ -0,0 +1,36 @@ +/* + 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) 2026 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 +/** Creates a t8code cmesh consisting of 12 quadrilateral trees arranged + * according to the HEALPix base decomposition as a surface + * \return A coarse mesh representing the HEALPix base grid. + */ +t8_cmesh_t +t8_cmesh_new_healpix_surface (sc_MPI_Comm comm, double surface_outer_radius, double surface_inner_radius); + +/** Creates a t8code cmesh consisting of 12 quadrilateral trees arranged + * according to the HEALPix base decomposition as a sphere + * \return A coarse mesh representing the HEALPix base grid. + */ +t8_cmesh_t +t8_cmesh_new_healpix_shell (sc_MPI_Comm comm, double shell_radius); diff --git a/src/t8_cmesh/t8_cmesh_healpix/t8_geometry_healpix.cxx b/src/t8_cmesh/t8_cmesh_healpix/t8_geometry_healpix.cxx new file mode 100644 index 0000000000..d27578260e --- /dev/null +++ b/src/t8_cmesh/t8_cmesh_healpix/t8_geometry_healpix.cxx @@ -0,0 +1,126 @@ +/* + 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) 2026 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 + +/** + * Evaluates a point on a sphere surface geometry from mesh layer and face parameters. + * + * Maps 3D parametric reference coordinates (\p xi, \p eta) of a specific mesh element, + * identified by its \p layer and \p face indices, to 3D Cartesian coordinates on a unit sphere. + * + * @param[in] layer The layer index of the mesh element. + * @param[in] face The face index of the mesh element within the layer. + * @param[in] xi The reference coordinate in the first parametric direction (typically [0, 1]). + * @param[in] eta The reference coordinate in the second parametric direction (typically [0, 1]). + * @param[in] radial_pos Normalized radial position in [0.0, 1.0] (0.0 -> inner_radius, 1.0 -> outer_radius). + * @param[out] out_coord Pointer to an array of size 3 where the resulting (x, y, z) + * Cartesian coordinates will be stored. + * @param[in] inner_radius Inner radius of the shell (clamped to [0.0, outer_radius]). + * @param[in] outer_radius Outer radius of the shell. + */ +void +t8_eval_geom_point (t8_gloidx_t layer, t8_gloidx_t face, double xi, double eta, double radial_pos, double *out_coord, + double inner_radius, double outer_radius) +{ + const double safe_outer = std::max (0.0, outer_radius); + const double safe_inner = std::clamp (inner_radius, 0.0, safe_outer); + const double clamped_pos = std::clamp (radial_pos, 0.0, 1.0); + + // Interpolate between inner_radius and outer_radius; as safe_inner -> safe_outer, shell becomes thin + const double r = safe_inner + clamped_pos * (safe_outer - safe_inner); + + const double offset_y = 1.0 - static_cast (layer); + const double offset_x = 2.0 * face + ((layer + 1) % 2); + + const double dX = offset_x + (xi - eta); + const double dY = offset_y + (xi + eta - 1.0); + + double z = 0.0; + double phi = 0.0; + + if (std::abs (dY) <= 1.0) { + z = (2.0 / 3.0) * dY; + phi = (std::numbers::pi / 4.0) * dX; + } + else if (dY > 1.0) { + const double denominator = 2.0 - dY; + z = 1.0 - (1.0 / 3.0) * std::pow (denominator, 2.0); + + if (denominator > 0.0) { + phi = (std::numbers::pi / 4.0) * (offset_x + (dX - offset_x) / denominator); + } + else { + // At the north pole, phi is arbitrary. + phi = (std::numbers::pi / 4.0) * offset_x; + } + } + else { + const double denominator = 2.0 + dY; + z = -1.0 + (1.0 / 3.0) * std::pow (denominator, 2.0); + + if (denominator > 0.0) { + phi = (std::numbers::pi / 4.0) * (offset_x + (dX - offset_x) / denominator); + } + else { + // At the south pole, phi is arbitrary. + phi = (std::numbers::pi / 4.0) * offset_x; + } + } + + z = std::clamp (z, -1.0, 1.0); + + // Convert unit-sphere spherical coordinates (z, phi) to Cartesian coordinates at radius r + const double theta = std::acos (z); + const double sin_theta = std::sin (theta); + + out_coord[0] = (sin_theta * std::cos (phi)) * r; + out_coord[1] = (sin_theta * std::sin (phi)) * r; + out_coord[2] = z * r; +} + +void +t8_geometry_healpix::t8_geom_evaluate ([[maybe_unused]] t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords, + const size_t num_coords, double *out_coords) const +{ + // This geometry is inspired by HEALPix + const t8_gloidx_t layer = gtreeid / 4; + const t8_gloidx_t face = gtreeid % 4; + + for (size_t i = 0; i < num_coords; ++i) { + const size_t base_idx_ref = i * 3; // 3D reference coordinates (xi, eta, zeta) + const size_t base_idx_3d = i * 3; // 3D Cartesian coordinates (x, y, z) + + const double xi = ref_coords[base_idx_ref + 0]; + const double eta = ref_coords[base_idx_ref + 1]; + const double zeta = ref_coords[base_idx_ref + 2]; // Radial parameter in [0, 1] + + t8_eval_geom_point (layer, face, xi, eta, zeta, out_coords + base_idx_3d, sphere_inner_radius, sphere_outer_radius); + } +} +t8_geometry_type_t +t8_geometry_healpix::t8_geom_get_type () const +{ + return T8_GEOMETRY_TYPE_ANALYTIC; // Or the specific t8_geometry_type_t enum value for HEALPix +} diff --git a/src/t8_cmesh/t8_cmesh_healpix/t8_geometry_healpix.hxx b/src/t8_cmesh/t8_cmesh_healpix/t8_geometry_healpix.hxx new file mode 100644 index 0000000000..501588cf0e --- /dev/null +++ b/src/t8_cmesh/t8_cmesh_healpix/t8_geometry_healpix.hxx @@ -0,0 +1,144 @@ +/* + 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) 2026 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. +*/ +#pragma once +#include +#include +/** +* The t8_geometry_healpix struct implements a spherical geometry mapping +* based on the HEALPix (Hierarchical Equal Area isoLatitude Pixelization) +* base-mesh configuration. It inherits from t8_geometry and +* is designed to map planar quadrilateral faces onto a spherical surface. +*/ +struct t8_geometry_healpix: public t8_geometry +{ + private: + double sphere_outer_radius; + double sphere_inner_radius = 0; + + public: + /** + * @brief Constructs a HEALPix geometry with the specified sphere inner and outer radius. + * + * @param outer_radius The outer radius of the sphere. + * @param inner_radius The inner radius of the sphere. + */ + t8_geometry_healpix (double outer_radius, double inner_radius): t8_geometry ("t8_geometry_healpix") + { + if (inner_radius > outer_radius) { + t8_errorf ("\n[t8_healpix] Warning inner radius is greater than outer radius \n[t8_healpix] defaulting to a " + "radius of 1\n"); + } + + sphere_outer_radius = outer_radius; + sphere_inner_radius = inner_radius; + } + /** + * @brief Default constructor. + * + * Constructs a HEALPix geometry with a given sphere radius. + */ + t8_geometry_healpix (double radius): t8_geometry ("t8_geometry_healpix") + { + sphere_outer_radius = radius; + } + /** + * @brief Default constructor. + * + * Constructs a HEALPix geometry with a sphere radius of 1. + */ + t8_geometry_healpix (): t8_geometry ("t8_geometry_healpix") + { + sphere_outer_radius = 1; + } + + /** + * @brief Destructor. + */ + virtual ~t8_geometry_healpix () + { + } + + /** + * Map the faces of a cube to a spherical surface. + * \param [in] cmesh The cmesh in which the point lies. + * \param [in] gtreeid The global tree (of the cmesh) in which the reference point is. + * \param [in] ref_coords Array of tree dimension x \a num_coords many entries, specifying a point in \f$ [0,1]^\mathrm{dim} \f$. + * \param [in] num_coords The number of points to map. + * \param [out] out_coords The mapped coordinates in physical space of \a ref_coords. The length is \a num_coords * 3. + * + * This routine expects an input mesh of twelve quadrilaterals in a configuration of the healpix base-mesh + * + */ + void + t8_geom_evaluate (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords, const size_t num_coords, + double *out_coords) const override; + + /** + * Jacobian, not implemented. + * \param[in] cmesh The cmesh in which the point lies. + * \param[in] gtreeid The global tree (of the cmesh) in which the reference point is. + * \param[in] ref_coords Array of tree dimension x \a num_coords many entries, specifying a point in \f$ [0,1]^\mathrm{dim} \f$. + * \param[in] num_coords The number of points to map. + * \param[in] jacobian The Jacobian matrix to be filled. + */ + void + t8_geom_evaluate_jacobian ([[maybe_unused]] t8_cmesh_t cmesh, [[maybe_unused]] t8_gloidx_t gtreeid, + [[maybe_unused]] const double *ref_coords, [[maybe_unused]] const size_t num_coords, + [[maybe_unused]] double *jacobian) const override + { + SC_ABORT_NOT_REACHED (); + } + + /** + * Check for compatibility of the currently loaded tree with the geometry. + * Only quad elements are supported by this geometry. + * \return True if the geometry is compatible with the tree. + */ + bool + t8_geom_check_tree_compatibility () const override + { + if (active_tree_class != T8_ECLASS_QUAD) { + t8_productionf ("t8_geometry_healpix is not compatible with tree type %s\n" + "It is only compatible with quad elements.\n", + t8_eclass_to_string[active_tree_class]); + return false; + } + return true; + } + + /** + * Get the type of this geometry. + * \return The type. + */ + t8_geometry_type_t + t8_geom_get_type () const override; + + /** + * Override to suppress warning. + * \return True if the currently loaded tree has a negative volume. + */ + virtual bool + t8_geom_tree_negative_volume () const override + { + return 0; + }; +}; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 540ba9cbba..b58acb397f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -140,6 +140,8 @@ add_t8_cpp_test( NAME t8_gtest_cmesh_vertex_conn_serial SOUR add_t8_cpp_test( NAME t8_gtest_multiple_attributes_parallel SOURCES t8_cmesh/t8_gtest_multiple_attributes.cxx ) add_t8_cpp_test( NAME t8_gtest_attribute_gloidx_array_serial SOURCES t8_cmesh/t8_gtest_attribute_gloidx_array.cxx ) add_t8_cpp_test( NAME t8_gtest_cmesh_bounding_box_serial SOURCES t8_cmesh/t8_gtest_cmesh_bounding_box.cxx ) +add_t8_cpp_test( NAME t8_gtest_cmesh_healpix_unit_sphere SOURCES t8_cmesh/t8_gtest_cmesh_healpix_unit_sphere.cxx ) +add_t8_cpp_test( NAME t8_gtest_cmesh_healpix_check_correctness SOURCES t8_cmesh/t8_gtest_cmesh_healpix_check_correctness.cxx ) add_t8_cpp_test( NAME t8_gtest_cmesh_boundary_conditions_parallel SOURCES t8_cmesh/t8_gtest_cmesh_boundary_conditions.cxx ) add_t8_cpp_test( NAME t8_gtest_shmem_parallel SOURCES t8_data/t8_gtest_shmem.cxx ) diff --git a/test/t8_cmesh/t8_gtest_cmesh_healpix_check_correctness.cxx b/test/t8_cmesh/t8_gtest_cmesh_healpix_check_correctness.cxx new file mode 100644 index 0000000000..98732aa96b --- /dev/null +++ b/test/t8_cmesh/t8_gtest_cmesh_healpix_check_correctness.cxx @@ -0,0 +1,181 @@ +/* + 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) 2026 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 + +/** \file We cross reference our implementation with this of +- xyf2loc from the HEALPix Julia source code. See here: + (https://github.com/JuliaAstro/Healpix.jl/blob/master/src/xyf.jl). +- We evaluate both implementation and compare them to eachother. +- if the evaluation is close enough then the test passes. + */ + +struct t8_location_result +{ + double z; + double phi; + double sintheta; + bool have_sintheta; +}; + +constexpr t8_location_result +xyf2loc (const double x, const double y, const int face, const int JRLL[], const int JPLL[], const double radius = 1.0) +{ + /* + A helper function translated from the HEALPix julia code base. + */ + double z = 0.0; + double nr = 0.0; + double sintheta = 0.0; + bool have_sintheta = false; + + const double jr = static_cast (JRLL[face]) - x - y; + + if (jr < 1.0) { + nr = jr; + const double tmp = (nr * nr) / 3.0; + z = 1.0 - tmp; + if (z > 0.99) { + sintheta = std::sqrt (tmp * (2.0 - tmp)); + have_sintheta = true; + } + } + else if (jr > 3.0) { + nr = 4.0 - jr; + const double tmp = (nr * nr) / 3.0; + z = tmp - 1.0; + if (z < -0.99) { + sintheta = std::sqrt (tmp * (2.0 - tmp)); + have_sintheta = true; + } + } + else { + nr = 1.0; + z = (2.0 - jr) * 2.0 / 3.0; + } + + double tmp = static_cast (JPLL[face]) * nr + x - y; + if (tmp < 0.0) + tmp += 8.0; + if (tmp >= 8.0) + tmp -= 8.0; + + const double phi = (std::abs (nr) < 1e-15) ? 0.0 : ((std::numbers::pi / 4.0 * tmp) / nr); + + return { z * radius, phi, sintheta * radius, have_sintheta }; +} + +TEST (t8_healpix_geometry_test, test_batch_against_xyf2loc_reference) +{ + const int JRLL[12] = { 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 }; + const int JPLL[12] = { 1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7 }; + +#if T8_TEST_LEVEL_INT >= 2 + const double outer_radius = 0.5; + const double inner_radius = 0.25; +#else + const double outer_radius = 10.0; + const double inner_radius = 4.0; +#endif + constexpr std::array test_samples = { 0.0, 0.001, 0.25, 0.5, 0.75, 0.999, 1.0 }; + + // Construct t8_geometry_healpix with (outer_radius, inner_radius) + t8_geometry_healpix geom (outer_radius, inner_radius); + + // Build 3D reference coordinates (xi, eta, zeta) with zeta = 1.0 (outer boundary) + constexpr size_t num_samples = test_samples.size (); + constexpr int num_coords = num_samples * num_samples; + std::array ref_coords; + size_t coord_idx = 0; + for (double xi : test_samples) { + for (double eta : test_samples) { + ref_coords[coord_idx++] = xi; + ref_coords[coord_idx++] = eta; + ref_coords[coord_idx++] = 1.0; // Outer surface of the shell matches xyf2loc at outer_radius + } + } + + // Loop through all t8code layers and faces + for (int layer = 0; layer < 3; ++layer) { + for (int face = 0; face < 4; ++face) { + // t8code Tree ID (0..11) + const t8_gloidx_t gtreeid = layer * 4 + face; + + // HEALPix base face index corresponding to t8code's layer + // layer 0 (South) -> HEALPix faces 8..11 + // layer 1 (Equator) -> HEALPix faces 4..7 + // layer 2 (North) -> HEALPix faces 0..3 + const int healpix_face = layer * 4 + face; + + // Output buffers + std::array t8_out; + + // Call the batch evaluation method provided + geom.t8_geom_evaluate (nullptr, gtreeid, ref_coords.data (), num_coords, t8_out.data ()); + + // Validate each point in the batch against xyf2loc reference + for (int coord_index = 0; coord_index < num_coords; coord_index++) { + const double xi = ref_coords[coord_index * 3 + 0]; + const double eta = ref_coords[coord_index * 3 + 1]; + + // Clamped values used inside t8_geom_evaluate + const double xi_clamped = std::clamp (xi, 1e-10, 1.0 - 1e-10); + const double eta_clamped = std::clamp (eta, 1e-10, 1.0 - 1e-10); + + const t8_location_result ref_loc = xyf2loc (xi_clamped, eta_clamped, healpix_face, JRLL, JPLL, outer_radius); + + const double ref_sin_theta + = ref_loc.have_sintheta ? ref_loc.sintheta + : std::sqrt (std::max (0.0, (outer_radius * outer_radius) - ref_loc.z * ref_loc.z)); + const double ref_x = ref_sin_theta * std::cos (ref_loc.phi); + const double ref_y = ref_sin_theta * std::sin (ref_loc.phi); + const double ref_z = ref_loc.z; + + const size_t idx3d = coord_index * 3; + + // Allow a slightly relaxed tolerance near the poles where sin(theta) ~ 0 + // Scale the polar check and tolerance by the active radius + const double base_tol + = (std::abs (ref_z) > 0.99 * outer_radius) ? (16.0 * T8_PRECISION_SQRT_EPS) : (128.0 * T8_PRECISION_EPS); + const double adaptive_tol = std::max (base_tol * outer_radius, 1e-8); + + // Add a minimum absolute floor (e.g., 1e-8) to gracefully handle near-zero values + // where microscopic rounding noise in reference data shouldn't trigger a failure. + EXPECT_NEAR (t8_out[idx3d + 0], ref_x, adaptive_tol) + << "Mismatch in X | Tree ID: " << gtreeid << " (Layer: " << layer << ", Face: " << face << ")" + << ", (xi, eta): (" << xi << ", " << eta << ")"; + EXPECT_NEAR (t8_out[idx3d + 1], ref_y, adaptive_tol) + << "Mismatch in Y | Tree ID: " << gtreeid << " (Layer: " << layer << ", Face: " << face << ")" + << ", (xi, eta): (" << xi << ", " << eta << ")"; + EXPECT_NEAR (t8_out[idx3d + 2], ref_z, adaptive_tol) + << "Mismatch in Z | Tree ID: " << gtreeid << " (Layer: " << layer << ", Face: " << face << ")" + << ", (xi, eta): (" << xi << ", " << eta << ")"; + } + } + } +} diff --git a/test/t8_cmesh/t8_gtest_cmesh_healpix_unit_sphere.cxx b/test/t8_cmesh/t8_gtest_cmesh_healpix_unit_sphere.cxx new file mode 100644 index 0000000000..4c89a55262 --- /dev/null +++ b/test/t8_cmesh/t8_gtest_cmesh_healpix_unit_sphere.cxx @@ -0,0 +1,88 @@ +/* + 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) 2026 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. +*/ + +/* This gtest checks whether a cmesh of a healpix is committed and whether the points have the same radius*/ + +#include +#include +#include +#include +#include +#include +#include +/* Test whether the healpix geometry has an equal radius all across the geometry. */ +TEST (t8_geometry_healpix_test, test_all_trees_are_on_unit_sphere) +{ + const double outer_radius = 1.0; + const double inner_radius = 0.5; + + t8_geometry_healpix geom (outer_radius, inner_radius); + + std::random_device rd; + std::mt19937 gen (rd ()); + std::uniform_real_distribution dist (0.0, 1.0); + +// A set of 2D reference coordinates within the element +// in addition to the basic test cases generate random coordinates to test out implementation +#if T8_TEST_LEVEL_INT >= 2 + const int num_gen_coords = 10; // Test level basic +#else + const int num_gen_coords = 20; +#endif + + std::array ref_coords = { + 0.0, 0.0, 1.0, // Corner, outer surface + 1.0, 0.0, 1.0, // Corner, outer surface + 0.0, 1.0, 1.0, // Corner, outer surface + 1.0, 1.0, 1.0, // Corner, outer surface + 0.5, 0.5, 1.0, // Center, outer surface + 0.5, 0.5, 0.0 // Center, inner surface + }; + + for (int coord_index = 0; coord_index < num_gen_coords; coord_index++) { + const size_t base = 18 + 3 * static_cast (coord_index); + ref_coords[base + 0] = dist (gen); + ref_coords[base + 1] = dist (gen); + ref_coords[base + 2] = dist (gen); + } + + const size_t num_coords = ref_coords.size () / 3; + std::vector out_coords (num_coords * 3, 0.0); + // Loop through all 12 base trees of the HEALPix projection + for (t8_gloidx_t gtreeid = 0; gtreeid < 12; ++gtreeid) { + geom.t8_geom_evaluate (nullptr, gtreeid, ref_coords.data (), num_coords, out_coords.data ()); + + for (size_t i = 0; i < num_coords; ++i) { + const double xi = ref_coords[i * 3 + 0]; + const double eta = ref_coords[i * 3 + 1]; + const double zeta = ref_coords[i * 3 + 2]; + const double expected_radius = inner_radius + zeta * (outer_radius - inner_radius); + + const std::span point (out_coords.data () + 3 * i, 3); + const double distance = t8_norm (point); + + EXPECT_NEAR (distance, expected_radius, T8_PRECISION_EPS * outer_radius) + << "Radius check failed at Tree ID: " << gtreeid << " for point index: " << i << " (xi=" << xi + << ", eta=" << eta << ", zeta=" << zeta << ")"; + } + } +}