From 05c741be09d1575b637ca88f580986871b291071 Mon Sep 17 00:00:00 2001 From: Schmitt Date: Fri, 19 Jun 2026 09:37:28 +0200 Subject: [PATCH 01/14] First Changes and File Creation --- tutorials/CMakeLists.txt | 5 + .../t8_mesh_step3_adapt_forest.cxx | 95 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx diff --git a/tutorials/CMakeLists.txt b/tutorials/CMakeLists.txt index 4c21e232db..32dd1976c9 100644 --- a/tutorials/CMakeLists.txt +++ b/tutorials/CMakeLists.txt @@ -84,3 +84,8 @@ copy_tutorial_file (features/t8_features_curved_meshes_generate_cmesh_tri.geo) if( T8CODE_BUILD_MESH_HANDLE ) add_mesh_handle_tutorial( NAME t8_mesh_element_data SOURCES mesh_handle/t8_mesh_element_data.cxx ) endif() + +if( T8CODE_BUILD_MESH_HANDLE ) + add_mesh_handle_tutorial( NAME t8_mesh_step3_adapt_forest SOURCES mesh_handle/t8_mesh_step3_adapt_forest.cxx ) +endif() + diff --git a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx new file mode 100644 index 0000000000..0fba3470c6 --- /dev/null +++ b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx @@ -0,0 +1,95 @@ +/* + 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 types 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. +*/ + +/** \file t8_mesh_step3_adapt_forest.cxx + * This is the same as general/t8_step3_adapt_forest.cxx but using the mesh handle interface instead of the forest + * interface. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct adapt_data { + std::array midpoint; + double refine_radius; + double coarsen_radius; +}; + +template +int adapt_callback(const TMeshClass &mesh, std::span elements, const adapt_data &adapt_data) { + auto element_centroid = elements[0].get_centroid(); + double dist = t8_dist(element_centroid, adapt_data.midpoint); + if (dist < adapt_data.refine_radius) { + return 1; // refine + } else if ((elements.size() > 1) && (dist > adapt_data.coarsen_radius)) { + return -1; // coarsen + } + return 0; // do nothing +} + +template +std::unique_ptr build_mesh(sc_MPI_Comm comm, int level) { + auto mesh = t8_mesh_handle::handle_hypercube_hybrid_uniform_default(level, comm); + struct adapt_data adapt_params = { + {0.5, 0.5, 1.0}, + 0.2, + 0.4 + }; + mesh->set_balance(); + mesh->set_partition(); + mesh->set_adapt( + TMeshClass::template mesh_adapt_callback_wrapper( + adapt_callback, + adapt_params + ) + ); + mesh->set_ghost(); + return mesh; +} + +int main(int argc, char** argv) { + int mpiret = sc_MPI_Init(&argc, &argv); + SC_CHECK_MPI(mpiret); + + sc_init (sc_MPI_COMM_WORLD, 1, 1, NULL, SC_LP_ESSENTIAL); + + t8_init (SC_LP_PRODUCTION); + + sc_MPI_Comm comm = sc_MPI_COMM_WORLD; + + using mesh_type = t8_mesh_handle::mesh<>; + + int uniform_level = 2; + auto mesh = build_mesh(comm, uniform_level); + + t8_mesh_handle::write_mesh_to_vtk(mesh, "adapted_mesh.vtu"); + + sc_finalize(); + return 0; +} From 53199cbc23ea577eb2db723f38d6eca40c1305a6 Mon Sep 17 00:00:00 2001 From: Schmitt Date: Fri, 19 Jun 2026 09:44:26 +0200 Subject: [PATCH 02/14] Spellchecking correct --- tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx index 0fba3470c6..fd8552980a 100644 --- a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx +++ b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx @@ -66,7 +66,7 @@ std::unique_ptr build_mesh(sc_MPI_Comm comm, int level) { mesh->set_adapt( TMeshClass::template mesh_adapt_callback_wrapper( adapt_callback, - adapt_params +adapt_params ) ); mesh->set_ghost(); From 665d24cd0936267c2879ef9c306f078f816e44fd Mon Sep 17 00:00:00 2001 From: Schmitt Date: Mon, 22 Jun 2026 10:18:12 +0200 Subject: [PATCH 03/14] Mesh_handle_tutorial Step3 --- tutorials/CMakeLists.txt | 4 + .../t8_mesh_step3_adapt_forest.cxx | 141 +++++++++++------- 2 files changed, 95 insertions(+), 50 deletions(-) diff --git a/tutorials/CMakeLists.txt b/tutorials/CMakeLists.txt index 32dd1976c9..daa0bc8146 100644 --- a/tutorials/CMakeLists.txt +++ b/tutorials/CMakeLists.txt @@ -89,3 +89,7 @@ if( T8CODE_BUILD_MESH_HANDLE ) add_mesh_handle_tutorial( NAME t8_mesh_step3_adapt_forest SOURCES mesh_handle/t8_mesh_step3_adapt_forest.cxx ) endif() +if( T8CODE_BUILD_MESH_HANDLE ) + add_mesh_handle_tutorial( NAME t8_mesh_step4_partition_balance_ghost SOURCES mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx ) +endif() + diff --git a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx index fd8552980a..6dcf176481 100644 --- a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx +++ b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx @@ -31,65 +31,106 @@ #include #include #include -#include +#include #include #include -struct adapt_data { - std::array midpoint; - double refine_radius; - double coarsen_radius; +/* The data that determines the adaptation characteristics of our algorithm. + * In this example we want to adapt in a spherical shape around a given point. */ +struct adapt_data +{ + std::array midpoint; /**< midpoint of our sphere. */ + double refine_radius; /**< We refine inside this radius of our sphere.*/ + double coarsen_radius; /**< We coarsen outside this radius of our sphere. */ }; +/** The adaption callback function. This will refine elements inside of a given sphere and coarsen the elements + * outside of a given sphere. + * \tparam TMeshClass The mesh handle class. + * \param [in] mesh The mesh that should be adapted. + * \param [in] elements One element or a family of elements to consider for adaptation. + * \param [in] adapt_data The user data to be used during the adaptation process. + * \returns 1 if the first entry in \a elements should be refined, + * -1 if the family of elements should be coarsened, + * 0 else. +*/ template -int adapt_callback(const TMeshClass &mesh, std::span elements, const adapt_data &adapt_data) { - auto element_centroid = elements[0].get_centroid(); - double dist = t8_dist(element_centroid, adapt_data.midpoint); - if (dist < adapt_data.refine_radius) { - return 1; // refine - } else if ((elements.size() > 1) && (dist > adapt_data.coarsen_radius)) { - return -1; // coarsen - } - return 0; // do nothing +int +adapt_callback ([[maybe_unused]]const TMeshClass &mesh, std::span elements, + const adapt_data &adapt_data) +{ + auto element_centroid = elements[0].get_centroid (); + double dist = t8_dist (element_centroid, adapt_data.midpoint); + if (dist < adapt_data.refine_radius) { + return 1; // refine + } //first check if there is a family, and only if yes check if we should coarsen. + else if ((elements.size () > 1) && (dist > adapt_data.coarsen_radius)) { + return -1; // coarsen + } + return 0; // do nothing } +/** Build our adapted mesh by transfering the adaption parameters and adapting once with our adapt_callback function. + * \tparam TMeshClass The mesh handle class. + * \param sc_MPI_Comm The MPI Communicator. + * \param level The initial uniform refinement level. + * \returns Unique pointer to the adapted mesh. + */ template -std::unique_ptr build_mesh(sc_MPI_Comm comm, int level) { - auto mesh = t8_mesh_handle::handle_hypercube_hybrid_uniform_default(level, comm); - struct adapt_data adapt_params = { - {0.5, 0.5, 1.0}, - 0.2, - 0.4 - }; - mesh->set_balance(); - mesh->set_partition(); - mesh->set_adapt( - TMeshClass::template mesh_adapt_callback_wrapper( - adapt_callback, -adapt_params - ) - ); - mesh->set_ghost(); - return mesh; +std::unique_ptr +build_mesh (sc_MPI_Comm comm, int level) +{ + /*Generate a hybrid hypercube, made out of cubes, prisms etc. */ + auto mesh = t8_mesh_handle::handle_hypercube_hybrid_uniform_default (level, comm); + /*Defining the adaption parameters. */ + struct adapt_data adapt_params = { { 0.5, 0.5, 1.0 }, 0.2, 0.4 }; + mesh->set_balance (); + mesh->set_partition (); + /*Adapting once with our adapt_callback function. */ + mesh->set_adapt ( + TMeshClass::template mesh_adapt_callback_wrapper (adapt_callback, adapt_params)); + mesh->set_ghost (); + mesh->commit (); + return mesh; } -int main(int argc, char** argv) { - int mpiret = sc_MPI_Init(&argc, &argv); - SC_CHECK_MPI(mpiret); - - sc_init (sc_MPI_COMM_WORLD, 1, 1, NULL, SC_LP_ESSENTIAL); - - t8_init (SC_LP_PRODUCTION); - - sc_MPI_Comm comm = sc_MPI_COMM_WORLD; - - using mesh_type = t8_mesh_handle::mesh<>; - - int uniform_level = 2; - auto mesh = build_mesh(comm, uniform_level); - - t8_mesh_handle::write_mesh_to_vtk(mesh, "adapted_mesh.vtu"); - - sc_finalize(); - return 0; +/** Entry point of the program. */ +int +main (int argc, char **argv) +{ + /*Initialize MPI. This has to happen before we initialize sc or t8code. */ + int mpiret = sc_MPI_Init (&argc, &argv); + /*Error check the MPI return value. */ + SC_CHECK_MPI (mpiret); + /* Initialize the sc library, has to happen before we initialize t8code. */ + sc_init (sc_MPI_COMM_WORLD, 1, 1, NULL, SC_LP_ESSENTIAL); + /* Initialize t8code with log level SC_LP_PRODUCTION. See sc.h for more info on the log levels. */ + t8_init (SC_LP_PRODUCTION); + /* We will use MPI_COMM_WORLD as a communicator. */ + sc_MPI_Comm comm = sc_MPI_COMM_WORLD; + + /* Print a starting message. */ + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Hello, this is the mesh adaptation example of t8code using the mesh handle.\n"); + t8_global_productionf ( + " [tutorial] In this example we will adapt a mesh in a spherical shape around a given point and write the adapted mesh to a vtu file.\n"); + t8_global_productionf (" [tutorial] \n"); + + using mesh_type = t8_mesh_handle::mesh<>; + + t8_global productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Creating an adapted mesh.\n"); + t8_global_productionf (" [tutorial] \n"); + /* The initial uniform refinement level. */ + int uniform_level = 3; + /* Building the Mesh*/ + auto mesh = build_mesh (comm, uniform_level); + /* Write the mesh to a vtu file. */ + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Writing adapted mesh to vtu file: adapted_mesh.vtu\n"); + t8_global_productionf (" [tutorial] \n"); + t8_mesh_handle::write_mesh_to_vtk (*mesh, "adapted_mesh.vtu"); + + sc_finalize (); + return 0; } From 9523fc53313dcd23257a017c0c6bba59f0032be3 Mon Sep 17 00:00:00 2001 From: Schmitt Date: Mon, 22 Jun 2026 10:18:51 +0200 Subject: [PATCH 04/14] Mesh_handle_tutorial Step3 --- tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx index 6dcf176481..9876a8c06e 100644 --- a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx +++ b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx @@ -56,21 +56,21 @@ struct adapt_data */ template int -adapt_callback ([[maybe_unused]]const TMeshClass &mesh, std::span elements, +adapt_callback ([[maybe_unused]] const TMeshClass &mesh, std::span elements, const adapt_data &adapt_data) { auto element_centroid = elements[0].get_centroid (); double dist = t8_dist (element_centroid, adapt_data.midpoint); if (dist < adapt_data.refine_radius) { return 1; // refine - } //first check if there is a family, and only if yes check if we should coarsen. + } //first check if there is a family, and only if yes check if we should coarsen. else if ((elements.size () > 1) && (dist > adapt_data.coarsen_radius)) { return -1; // coarsen } return 0; // do nothing } -/** Build our adapted mesh by transfering the adaption parameters and adapting once with our adapt_callback function. +/** Build our adapted mesh by transferring the adaption parameters and adapting once with our adapt_callback function. * \tparam TMeshClass The mesh handle class. * \param sc_MPI_Comm The MPI Communicator. * \param level The initial uniform refinement level. @@ -83,7 +83,7 @@ build_mesh (sc_MPI_Comm comm, int level) /*Generate a hybrid hypercube, made out of cubes, prisms etc. */ auto mesh = t8_mesh_handle::handle_hypercube_hybrid_uniform_default (level, comm); /*Defining the adaption parameters. */ - struct adapt_data adapt_params = { { 0.5, 0.5, 1.0 }, 0.2, 0.4 }; + struct adapt_data adapt_params = { { 0.5, 0.5, 1.0 }, 0.2, 0.4 }; mesh->set_balance (); mesh->set_partition (); /*Adapting once with our adapt_callback function. */ @@ -112,8 +112,8 @@ main (int argc, char **argv) /* Print a starting message. */ t8_global_productionf (" [tutorial] \n"); t8_global_productionf (" [tutorial] Hello, this is the mesh adaptation example of t8code using the mesh handle.\n"); - t8_global_productionf ( - " [tutorial] In this example we will adapt a mesh in a spherical shape around a given point and write the adapted mesh to a vtu file.\n"); + t8_global_productionf (" [tutorial] In this example we will adapt a mesh in a spherical shape around a given point " + "and write the adapted mesh to a vtu file.\n"); t8_global_productionf (" [tutorial] \n"); using mesh_type = t8_mesh_handle::mesh<>; From 604e003fcc6134389b08c3c0c6081c84b489798a Mon Sep 17 00:00:00 2001 From: Schmitt Date: Mon, 22 Jun 2026 11:32:35 +0200 Subject: [PATCH 05/14] Mesh_handle_tutorial Step3 fixed and Step4 added. --- .../t8_mesh_step3_adapt_forest.cxx | 4 +- .../t8_mesh_step4_partition_balance_ghost.cxx | 228 ++++++++++++++++++ 2 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx diff --git a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx index 9876a8c06e..8e1bf1bffe 100644 --- a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx +++ b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx @@ -118,7 +118,7 @@ main (int argc, char **argv) using mesh_type = t8_mesh_handle::mesh<>; - t8_global productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] \n"); t8_global_productionf (" [tutorial] Creating an adapted mesh.\n"); t8_global_productionf (" [tutorial] \n"); /* The initial uniform refinement level. */ @@ -132,5 +132,7 @@ main (int argc, char **argv) t8_mesh_handle::write_mesh_to_vtk (*mesh, "adapted_mesh.vtu"); sc_finalize (); + mpiret = sc_MPI_Finalize (); + SC_CHECK_MPI (mpiret); return 0; } diff --git a/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx b/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx new file mode 100644 index 0000000000..db5582c418 --- /dev/null +++ b/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx @@ -0,0 +1,228 @@ +/* + 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 types 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. +*/ + +/** \file t8_mesh_element_data.cxx + * This is the same as general/t8_step4_partition_balance_ghost.cxx but using the mesh handle interface instead of the forest + * interface. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** (This is the same as in tutorial step 3) + * The data that determines the adaptation characteristics of our algorithm. + * In this example we want to adapt in a spherical shape around a given point. +*/ +struct adapt_data +{ + std::array midpoint; /**< midpoint of our sphere. */ + double refine_radius; /**< We refine inside this radius of our sphere.*/ + double coarsen_radius; /**< We coarsen outside this radius of our sphere. */ +}; + +/** (This is the same as in tutorial step 3) + * The adaption callback function. This will refine elements inside of a given sphere and coarsen the elements + * outside of a given sphere. + * \tparam TMeshClass The mesh handle class. + * \param [in] mesh The mesh that should be adapted. + * \param [in] elements One element or a family of elements to consider for adaptation. + * \param [in] adapt_data The user data to be used during the adaptation process. + * \returns 1 if the first entry in \a elements should be refined, + * -1 if the family of elements should be coarsened, + * 0 else. +*/ +template +int +adapt_callback ([[maybe_unused]]const mesh_type &mesh, std::span elements, + const adapt_data &adapt_data) +{ + auto element_centroid = elements[0].get_centroid (); + double dist = t8_dist (element_centroid, adapt_data.midpoint); + if (dist < adapt_data.refine_radius) { + return 1; // refine + } //first check if there is a family, and only if yes check if we should coarsen. + else if ((elements.size () > 1) && (dist > adapt_data.coarsen_radius)) { + return -1; // coarsen + } + return 0; // do nothing +} + +/** Helper function to print the total number of elements in the mesh after each step. + * \param mesh The mesh handle to get the number of elements from. + * \param stage The stage of the mesh (e.g. "Initial mesh", "Adapted mesh", etc.) to print in the output. + * \param comm The MPI communicator to use for the reduction and printing. +*/ +void print_mesh_stats(const std::unique_ptr> &mesh, const char *stage, sc_MPI_Comm comm) { + int local_elements = mesh->get_num_local_elements(); + int global_elements = 0; + MPI_Allreduce(&local_elements, &global_elements, 1, MPI_INT, MPI_SUM, comm); + + int rank = 0; + MPI_Comm_rank(comm, &rank); + if (rank == 0) { + std::cout << "=== " << stage << " ===" << std::endl; + std::cout << "Total elements: " << global_elements << std::endl; + } +} + +/** Entry point of the program. */ +int main(int argc, char **argv) { + + /* Initialize MPI. This has to happen before we initialize sc or t8code. */ + int mpiret = sc_MPI_Init(&argc, &argv); + /* Error check the MPI return value. */ + SC_CHECK_MPI(mpiret); + /* Initialize the sc library, has to happen before we initialize t8code. */ + sc_init (sc_MPI_COMM_WORLD, 1, 1, NULL, SC_LP_ESSENTIAL); + /* Initialize t8code with log level SC_LP_PRODUCTION. See sc.h for more info on the log levels. */ + t8_init (SC_LP_PRODUCTION); + /* We will use MPI_COMM_WORLD as a communicator. */ + sc_MPI_Comm comm = sc_MPI_COMM_WORLD; + + /* Print a starting message. */ + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Hello, this is the mesh adaptation example of t8code using the mesh handle.\n"); + t8_global_productionf ( + " [tutorial] In this example we will Us\n"); + t8_global_productionf (" [tutorial] \n"); + + /* The initial uniform refinement level. */ + int uniform_level = 3; + + /* Parameters for the adaption step. */ + struct adapt_data adapt_params = { { 0.5, 0.5, 1.0 }, 0.2, 0.4 }; + + using mesh_type = t8_mesh_handle::mesh<>; + + /** + * INITIAL MESH + */ + + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Creating initial mesh.\n"); + t8_global_productionf (" [tutorial] \n"); + + /* Creating the initial mesh with uniform refinement. */ + auto mesh = t8_mesh_handle::handle_hypercube_hybrid_uniform_default (uniform_level, comm); + + /* Commiting the initial mesh. */ + mesh->commit(); + + /* Printing the mesh information. */ + print_mesh_stats(mesh, "Initial mesh", comm); + + /* Writing the Mesh to vtu and pvtu files, using the extended version of the function to ensure additional data like ghost elements, treeid etc. to be written into the files. */ + t8_mesh_handle::write_mesh_to_vtk_ext(*mesh, "initial_mesh.vtu", 0, nullptr, true, true, true, true, true, false, false); + + /** + * ADAPTED MESH + */ + + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Creating adapted mesh.\n"); + t8_global_productionf (" [tutorial] \n"); + + /* Creating the adapted mesh as a copy of the initial mesh (Initial mesh can't be refined because it is already commited.) */ + auto mesh_adapt = std::make_unique(*mesh); + + /* Adapting the mesh once with our adapt_callback function from step 3 and the parameters defined above. */ + mesh_adapt->set_adapt( + mesh_type::template mesh_adapt_callback_wrapper ( + &adapt_callback, + adapt_params) + ); + /* Commiting the adapted mesh. */ + mesh_adapt->commit(); + + /* Printing the mesh information. */ + print_mesh_stats(mesh_adapt, "Adapted mesh", comm); + + /* Writing the mesh to vtu and pvtu files using the extended version of the function. */ + t8_mesh_handle::write_mesh_to_vtk_ext(*mesh_adapt, "adapted_mesh.vtu", 0, nullptr, true, true, true, true, true, false, false); + + /** + * PARTITIONED, BALANCED MESH + */ + + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Creating partitioned and balanced mesh.\n"); + t8_global_productionf (" [tutorial] \n"); + + /* Creating the partition and balance mesh as a copy of the adapted mesh. */ + auto mesh_partition_balance = std::make_unique(*mesh_adapt); + + /* Partitioning the mesh.*/ + mesh_partition_balance->set_partition(); + + /* Balancing the mesh. */ + mesh_partition_balance->set_balance(); + + /* Commiting the partitioned and balanced mesh. */ + mesh_partition_balance->commit(); + + /* Printing the mesh information. */ + print_mesh_stats(mesh_partition_balance, "Partitioned and Balanced mesh", comm); + + /* Writing the mesh to vtu and pvtu files using the extended version of the function. */ + t8_mesh_handle::write_mesh_to_vtk_ext(*mesh_partition_balance, "partition_balance_mesh.vtu", 0, nullptr, true, true, true, true, true, false, false); + + /** + * GHOST MESH + */ + + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Creating ghost mesh.\n"); + t8_global_productionf (" [tutorial] \n"); + + /* Creating the ghost mesh as a copy of the partitioned and balanced mesh. */ + auto mesh_ghost = std::make_unique(*mesh_partition_balance); + + /* Creating the ghost layers. */ + mesh_ghost->set_ghost(); + + /* Commiting the ghost mesh. */ + mesh_ghost->commit(); + + /* Printing the mesh information*/ + print_mesh_stats(mesh_ghost, "Ghost mesh", comm); + + /* Writing the mesh to vtu and pvtu files using the extended version of the function. */ + t8_mesh_handle::write_mesh_to_vtk_ext(*mesh_ghost, "ghost_mesh.vtu", 0, nullptr, true, true, true, true, true, false, false); + + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Finished all steps successfully.\n"); + t8_global_productionf (" [tutorial] \n"); + + + sc_finalize(); + mpiret = sc_MPI_Finalize(); + SC_CHECK_MPI(mpiret); + return 0; +} \ No newline at end of file From 282f592a7b0c8a577ac7d497996ae0fd13748c50 Mon Sep 17 00:00:00 2001 From: Schmitt Date: Wed, 1 Jul 2026 10:44:48 +0200 Subject: [PATCH 06/14] added bsd license --- doc/author_schmitt.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/author_schmitt.txt diff --git a/doc/author_schmitt.txt b/doc/author_schmitt.txt new file mode 100644 index 0000000000..42045f2bc9 --- /dev/null +++ b/doc/author_schmitt.txt @@ -0,0 +1 @@ +I place my contributions to t8code under the FreeBSD license. Vincent Schmitt (vinz@vincent-schmitt.de) \ No newline at end of file From bd844302fba902311cecb5369045ba0efcaa364b Mon Sep 17 00:00:00 2001 From: Schmitt Date: Fri, 3 Jul 2026 10:27:02 +0200 Subject: [PATCH 07/14] spell checking corrected --- .../t8_mesh_step4_partition_balance_ghost.cxx | 239 +++++++++--------- 1 file changed, 121 insertions(+), 118 deletions(-) diff --git a/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx b/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx index db5582c418..586b66bff3 100644 --- a/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx +++ b/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx @@ -60,14 +60,14 @@ struct adapt_data */ template int -adapt_callback ([[maybe_unused]]const mesh_type &mesh, std::span elements, +adapt_callback ([[maybe_unused]] const mesh_type &mesh, std::span elements, const adapt_data &adapt_data) { auto element_centroid = elements[0].get_centroid (); double dist = t8_dist (element_centroid, adapt_data.midpoint); if (dist < adapt_data.refine_radius) { return 1; // refine - } //first check if there is a family, and only if yes check if we should coarsen. + } //first check if there is a family, and only if yes check if we should coarsen. else if ((elements.size () > 1) && (dist > adapt_data.coarsen_radius)) { return -1; // coarsen } @@ -79,150 +79,153 @@ adapt_callback ([[maybe_unused]]const mesh_type &mesh, std::span> &mesh, const char *stage, sc_MPI_Comm comm) { - int local_elements = mesh->get_num_local_elements(); - int global_elements = 0; - MPI_Allreduce(&local_elements, &global_elements, 1, MPI_INT, MPI_SUM, comm); - - int rank = 0; - MPI_Comm_rank(comm, &rank); - if (rank == 0) { - std::cout << "=== " << stage << " ===" << std::endl; - std::cout << "Total elements: " << global_elements << std::endl; - } +void +print_mesh_stats (const std::unique_ptr> &mesh, const char *stage, sc_MPI_Comm comm) +{ + int local_elements = mesh->get_num_local_elements (); + int global_elements = 0; + MPI_Allreduce (&local_elements, &global_elements, 1, MPI_INT, MPI_SUM, comm); + + int rank = 0; + MPI_Comm_rank (comm, &rank); + if (rank == 0) { + std::cout << "=== " << stage << " ===" << std::endl; + std::cout << "Total elements: " << global_elements << std::endl; + } } /** Entry point of the program. */ -int main(int argc, char **argv) { - - /* Initialize MPI. This has to happen before we initialize sc or t8code. */ - int mpiret = sc_MPI_Init(&argc, &argv); - /* Error check the MPI return value. */ - SC_CHECK_MPI(mpiret); - /* Initialize the sc library, has to happen before we initialize t8code. */ - sc_init (sc_MPI_COMM_WORLD, 1, 1, NULL, SC_LP_ESSENTIAL); - /* Initialize t8code with log level SC_LP_PRODUCTION. See sc.h for more info on the log levels. */ - t8_init (SC_LP_PRODUCTION); - /* We will use MPI_COMM_WORLD as a communicator. */ - sc_MPI_Comm comm = sc_MPI_COMM_WORLD; - - /* Print a starting message. */ - t8_global_productionf (" [tutorial] \n"); - t8_global_productionf (" [tutorial] Hello, this is the mesh adaptation example of t8code using the mesh handle.\n"); - t8_global_productionf ( - " [tutorial] In this example we will Us\n"); - t8_global_productionf (" [tutorial] \n"); - - /* The initial uniform refinement level. */ - int uniform_level = 3; - - /* Parameters for the adaption step. */ - struct adapt_data adapt_params = { { 0.5, 0.5, 1.0 }, 0.2, 0.4 }; - - using mesh_type = t8_mesh_handle::mesh<>; - - /** +int +main (int argc, char **argv) +{ + + /* Initialize MPI. This has to happen before we initialize sc or t8code. */ + int mpiret = sc_MPI_Init (&argc, &argv); + /* Error check the MPI return value. */ + SC_CHECK_MPI (mpiret); + /* Initialize the sc library, has to happen before we initialize t8code. */ + sc_init (sc_MPI_COMM_WORLD, 1, 1, NULL, SC_LP_ESSENTIAL); + /* Initialize t8code with log level SC_LP_PRODUCTION. See sc.h for more info on the log levels. */ + t8_init (SC_LP_PRODUCTION); + /* We will use MPI_COMM_WORLD as a communicator. */ + sc_MPI_Comm comm = sc_MPI_COMM_WORLD; + + /* Print a starting message. */ + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Hello, this is the mesh adaptation example of t8code using the mesh handle.\n"); + t8_global_productionf (" [tutorial] In this example we will Us\n"); + t8_global_productionf (" [tutorial] \n"); + + /* The initial uniform refinement level. */ + int uniform_level = 3; + + /* Parameters for the adaption step. */ + struct adapt_data adapt_params = { { 0.5, 0.5, 1.0 }, 0.2, 0.4 }; + + using mesh_type = t8_mesh_handle::mesh<>; + + /** * INITIAL MESH */ - t8_global_productionf (" [tutorial] \n"); - t8_global_productionf (" [tutorial] Creating initial mesh.\n"); - t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Creating initial mesh.\n"); + t8_global_productionf (" [tutorial] \n"); - /* Creating the initial mesh with uniform refinement. */ - auto mesh = t8_mesh_handle::handle_hypercube_hybrid_uniform_default (uniform_level, comm); + /* Creating the initial mesh with uniform refinement. */ + auto mesh = t8_mesh_handle::handle_hypercube_hybrid_uniform_default (uniform_level, comm); - /* Commiting the initial mesh. */ - mesh->commit(); + /* Committing the initial mesh. */ + mesh->commit (); - /* Printing the mesh information. */ - print_mesh_stats(mesh, "Initial mesh", comm); - - /* Writing the Mesh to vtu and pvtu files, using the extended version of the function to ensure additional data like ghost elements, treeid etc. to be written into the files. */ - t8_mesh_handle::write_mesh_to_vtk_ext(*mesh, "initial_mesh.vtu", 0, nullptr, true, true, true, true, true, false, false); + /* Printing the mesh information. */ + print_mesh_stats (mesh, "Initial mesh", comm); - /** + /* Writing the Mesh to vtu and pvtu files, using the extended version of the function to ensure additional data like ghost elements, treeid etc. to be written into the files. */ + t8_mesh_handle::write_mesh_to_vtk_ext (*mesh, "initial_mesh.vtu", 0, nullptr, true, true, true, true, true, false, + false); + + /** * ADAPTED MESH */ - t8_global_productionf (" [tutorial] \n"); - t8_global_productionf (" [tutorial] Creating adapted mesh.\n"); - t8_global_productionf (" [tutorial] \n"); - - /* Creating the adapted mesh as a copy of the initial mesh (Initial mesh can't be refined because it is already commited.) */ - auto mesh_adapt = std::make_unique(*mesh); - - /* Adapting the mesh once with our adapt_callback function from step 3 and the parameters defined above. */ - mesh_adapt->set_adapt( - mesh_type::template mesh_adapt_callback_wrapper ( - &adapt_callback, - adapt_params) - ); - /* Commiting the adapted mesh. */ - mesh_adapt->commit(); - - /* Printing the mesh information. */ - print_mesh_stats(mesh_adapt, "Adapted mesh", comm); - - /* Writing the mesh to vtu and pvtu files using the extended version of the function. */ - t8_mesh_handle::write_mesh_to_vtk_ext(*mesh_adapt, "adapted_mesh.vtu", 0, nullptr, true, true, true, true, true, false, false); - - /** + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Creating adapted mesh.\n"); + t8_global_productionf (" [tutorial] \n"); + + /* Creating the adapted mesh as a copy of the initial mesh (Initial mesh can't be refined because it is already committed.) */ + auto mesh_adapt = std::make_unique (*mesh); + + /* Adapting the mesh once with our adapt_callback function from step 3 and the parameters defined above. */ + mesh_adapt->set_adapt ( + mesh_type::template mesh_adapt_callback_wrapper (&adapt_callback, adapt_params)); + /* Committing the adapted mesh. */ + mesh_adapt->commit (); + + /* Printing the mesh information. */ + print_mesh_stats (mesh_adapt, "Adapted mesh", comm); + + /* Writing the mesh to vtu and pvtu files using the extended version of the function. */ + t8_mesh_handle::write_mesh_to_vtk_ext (*mesh_adapt, "adapted_mesh.vtu", 0, nullptr, true, true, true, true, true, + false, false); + + /** * PARTITIONED, BALANCED MESH */ - t8_global_productionf (" [tutorial] \n"); - t8_global_productionf (" [tutorial] Creating partitioned and balanced mesh.\n"); - t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Creating partitioned and balanced mesh.\n"); + t8_global_productionf (" [tutorial] \n"); + + /* Creating the partition and balance mesh as a copy of the adapted mesh. */ + auto mesh_partition_balance = std::make_unique (*mesh_adapt); - /* Creating the partition and balance mesh as a copy of the adapted mesh. */ - auto mesh_partition_balance = std::make_unique(*mesh_adapt); + /* Partitioning the mesh.*/ + mesh_partition_balance->set_partition (); - /* Partitioning the mesh.*/ - mesh_partition_balance->set_partition(); + /* Balancing the mesh. */ + mesh_partition_balance->set_balance (); - /* Balancing the mesh. */ - mesh_partition_balance->set_balance(); + /* Committing the partitioned and balanced mesh. */ + mesh_partition_balance->commit (); - /* Commiting the partitioned and balanced mesh. */ - mesh_partition_balance->commit(); + /* Printing the mesh information. */ + print_mesh_stats (mesh_partition_balance, "Partitioned and Balanced mesh", comm); - /* Printing the mesh information. */ - print_mesh_stats(mesh_partition_balance, "Partitioned and Balanced mesh", comm); - - /* Writing the mesh to vtu and pvtu files using the extended version of the function. */ - t8_mesh_handle::write_mesh_to_vtk_ext(*mesh_partition_balance, "partition_balance_mesh.vtu", 0, nullptr, true, true, true, true, true, false, false); + /* Writing the mesh to vtu and pvtu files using the extended version of the function. */ + t8_mesh_handle::write_mesh_to_vtk_ext (*mesh_partition_balance, "partition_balance_mesh.vtu", 0, nullptr, true, true, + true, true, true, false, false); - /** + /** * GHOST MESH */ - t8_global_productionf (" [tutorial] \n"); - t8_global_productionf (" [tutorial] Creating ghost mesh.\n"); - t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Creating ghost mesh.\n"); + t8_global_productionf (" [tutorial] \n"); - /* Creating the ghost mesh as a copy of the partitioned and balanced mesh. */ - auto mesh_ghost = std::make_unique(*mesh_partition_balance); + /* Creating the ghost mesh as a copy of the partitioned and balanced mesh. */ + auto mesh_ghost = std::make_unique (*mesh_partition_balance); - /* Creating the ghost layers. */ - mesh_ghost->set_ghost(); - - /* Commiting the ghost mesh. */ - mesh_ghost->commit(); + /* Creating the ghost layers. */ + mesh_ghost->set_ghost (); - /* Printing the mesh information*/ - print_mesh_stats(mesh_ghost, "Ghost mesh", comm); - - /* Writing the mesh to vtu and pvtu files using the extended version of the function. */ - t8_mesh_handle::write_mesh_to_vtk_ext(*mesh_ghost, "ghost_mesh.vtu", 0, nullptr, true, true, true, true, true, false, false); + /* Committing the ghost mesh. */ + mesh_ghost->commit (); - t8_global_productionf (" [tutorial] \n"); - t8_global_productionf (" [tutorial] Finished all steps successfully.\n"); - t8_global_productionf (" [tutorial] \n"); + /* Printing the mesh information*/ + print_mesh_stats (mesh_ghost, "Ghost mesh", comm); + /* Writing the mesh to vtu and pvtu files using the extended version of the function. */ + t8_mesh_handle::write_mesh_to_vtk_ext (*mesh_ghost, "ghost_mesh.vtu", 0, nullptr, true, true, true, true, true, false, + false); - sc_finalize(); - mpiret = sc_MPI_Finalize(); - SC_CHECK_MPI(mpiret); - return 0; -} \ No newline at end of file + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Finished all steps successfully.\n"); + t8_global_productionf (" [tutorial] \n"); + + sc_finalize (); + mpiret = sc_MPI_Finalize (); + SC_CHECK_MPI (mpiret); + return 0; +} From d0dc4704cdde35265f7f9451de0daed79b5218bb Mon Sep 17 00:00:00 2001 From: Vincent Schmitt <136984538+Vyp3er@users.noreply.github.com> Date: Fri, 17 Jul 2026 10:14:54 +0200 Subject: [PATCH 08/14] Apply suggestion from @lenaploetzke Co-authored-by: lenaploetzke <70579874+lenaploetzke@users.noreply.github.com> --- tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx index 8e1bf1bffe..2c8f8e731b 100644 --- a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx +++ b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx @@ -3,7 +3,7 @@ t8code is a C library to manage a collection (a forest) of multiple connected adaptive space-trees of general element types in parallel. - Copyright (C) 2015 the developers + 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 From ca472bddeaf8278c2db9b7413315bb2b03a52e9f Mon Sep 17 00:00:00 2001 From: Schmitt Date: Fri, 17 Jul 2026 10:29:03 +0200 Subject: [PATCH 09/14] Documentation Changes and Renaming --- .../mesh_handle/t8_mesh_step3_adapt_mesh.cxx | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx diff --git a/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx b/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx new file mode 100644 index 0000000000..6f94aac0cf --- /dev/null +++ b/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx @@ -0,0 +1,138 @@ +/* + 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 types 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. +*/ + +/** \file t8_mesh_step3_adapt_mesh.cxx + * This is the same as general/t8_step3_adapt_forest.cxx but using the mesh handle interface instead of the forest + * interface. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* The data that determines the adaptation characteristics of our algorithm. + * In this example we want to adapt in a spherical shape around a given point. */ +struct adapt_data +{ + t8_3D_vec midpoint; /**< midpoint of our sphere. */ + double refine_radius; /**< We refine inside this radius of our sphere.*/ + double coarsen_radius; /**< We coarsen outside this radius of our sphere. */ +}; + +/** The adaption callback function. This will refine elements inside of a given sphere and coarsen the elements + * outside of a given sphere. + * \tparam TMeshClass The mesh handle class. + * \param [in] mesh The mesh that should be adapted. + * \param [in] elements One element or a family of elements to consider for adaptation. + * \param [in] adapt_data The user data to be used during the adaptation process. + * \returns 1 if the first entry in \a elements should be refined, + * -1 if the family of elements should be coarsened, + * 0 else. +*/ +template +int +adapt_callback ([[maybe_unused]] const TMeshClass &mesh, std::span elements, + const adapt_data &adapt_data) +{ + auto element_centroid = elements[0].get_centroid (); + double dist = t8_dist (element_centroid, adapt_data.midpoint); + if (dist < adapt_data.refine_radius) { + return 1; /**< Refine. */ + } /** First check if there is a family, and only if yes coarsen. */ + else if ((elements.size () > 1) && (dist > adapt_data.coarsen_radius)) { + return -1; /**< Coarsen. */ + } + return 0; /**< Do Nothing. */ +} + +/** Build our adapted mesh by transferring the adaption parameters and adapting once with our \ref adapt_callback function. + * \tparam TMeshClass The mesh handle class. + * \param sc_MPI_Comm The MPI Communicator. + * \param level The initial uniform refinement level. + * \returns Unique pointer to the adapted mesh. + */ +template +std::unique_ptr +build_mesh (sc_MPI_Comm comm, int level) +{ + /*Generate a hybrid hypercube, made out of cubes, prisms etc. */ + auto mesh = t8_mesh_handle::handle_hypercube_hybrid_uniform_default (level, comm); + /*Defining the adaption parameters. */ + struct adapt_data adapt_params = { { 0.5, 0.5, 1.0 }, 0.2, 0.4 }; + mesh->set_balance (); + mesh->set_partition (); + /*Adapting once with our adapt_callback function. */ + mesh->set_adapt ( + TMeshClass::template mesh_adapt_callback_wrapper (adapt_callback, adapt_params)); + mesh->set_ghost (); + mesh->commit (); + return mesh; +} + +/** Entry point of the program. */ +int +main (int argc, char **argv) +{ + /*Initialize MPI. This has to happen before we initialize sc or t8code. */ + int mpiret = sc_MPI_Init (&argc, &argv); + /*Error check the MPI return value. */ + SC_CHECK_MPI (mpiret); + /* Initialize the sc library, has to happen before we initialize t8code. */ + sc_init (sc_MPI_COMM_WORLD, 1, 1, NULL, SC_LP_ESSENTIAL); + /* Initialize t8code with log level SC_LP_PRODUCTION. See sc.h for more info on the log levels. */ + t8_init (SC_LP_PRODUCTION); + /* We will use MPI_COMM_WORLD as a communicator. */ + sc_MPI_Comm comm = sc_MPI_COMM_WORLD; + + /* Print a starting message. */ + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Hello, this is the mesh adaptation example of t8code using the mesh handle.\n"); + t8_global_productionf (" [tutorial] In this example we will adapt a mesh in a spherical shape around a given point " + "and write the adapted mesh to a vtu file.\n"); + t8_global_productionf (" [tutorial] \n"); + + using mesh_type = t8_mesh_handle::mesh<>; + + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Creating an adapted mesh.\n"); + t8_global_productionf (" [tutorial] \n"); + /* The initial uniform refinement level. */ + int uniform_level = 3; + /* Building the mesh. */ + auto mesh = build_mesh (comm, uniform_level); + /* Write the mesh to a vtu file. */ + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Writing adapted mesh to vtu file: step3_adapted_mesh.vtu\n"); + t8_global_productionf (" [tutorial] \n"); + t8_mesh_handle::write_mesh_to_vtk (*mesh, "step3_adapted_mesh.vtu"); + + sc_finalize (); + mpiret = sc_MPI_Finalize (); + SC_CHECK_MPI (mpiret); + return 0; +} From 989601173a4e9ebee251f5a0ae430aa5c43268f7 Mon Sep 17 00:00:00 2001 From: Schmitt Date: Mon, 20 Jul 2026 10:24:13 +0200 Subject: [PATCH 10/14] added documentation and comments --- tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx index 2c8f8e731b..8e1bf1bffe 100644 --- a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx +++ b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx @@ -3,7 +3,7 @@ t8code is a C library to manage a collection (a forest) of multiple connected adaptive space-trees of general element types in parallel. - Copyright (C) 2026 the developers + 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 From a05095c63e7c93d86f64455ecdd1c742164ed2af Mon Sep 17 00:00:00 2001 From: Schmitt Date: Mon, 20 Jul 2026 10:50:05 +0200 Subject: [PATCH 11/14] Added Scope into main to prevent deletion Error --- tutorials/CMakeLists.txt | 2 +- .../t8_mesh_step3_adapt_forest.cxx | 138 ------------------ .../mesh_handle/t8_mesh_step3_adapt_mesh.cxx | 15 +- 3 files changed, 9 insertions(+), 146 deletions(-) delete mode 100644 tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx diff --git a/tutorials/CMakeLists.txt b/tutorials/CMakeLists.txt index daa0bc8146..667ce7f946 100644 --- a/tutorials/CMakeLists.txt +++ b/tutorials/CMakeLists.txt @@ -86,7 +86,7 @@ if( T8CODE_BUILD_MESH_HANDLE ) endif() if( T8CODE_BUILD_MESH_HANDLE ) - add_mesh_handle_tutorial( NAME t8_mesh_step3_adapt_forest SOURCES mesh_handle/t8_mesh_step3_adapt_forest.cxx ) + add_mesh_handle_tutorial( NAME t8_mesh_step3_adapt_mesh SOURCES mesh_handle/t8_mesh_step3_adapt_mesh.cxx ) endif() if( T8CODE_BUILD_MESH_HANDLE ) diff --git a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx b/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx deleted file mode 100644 index 8e1bf1bffe..0000000000 --- a/tutorials/mesh_handle/t8_mesh_step3_adapt_forest.cxx +++ /dev/null @@ -1,138 +0,0 @@ -/* - 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 types 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. -*/ - -/** \file t8_mesh_step3_adapt_forest.cxx - * This is the same as general/t8_step3_adapt_forest.cxx but using the mesh handle interface instead of the forest - * interface. -*/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/* The data that determines the adaptation characteristics of our algorithm. - * In this example we want to adapt in a spherical shape around a given point. */ -struct adapt_data -{ - std::array midpoint; /**< midpoint of our sphere. */ - double refine_radius; /**< We refine inside this radius of our sphere.*/ - double coarsen_radius; /**< We coarsen outside this radius of our sphere. */ -}; - -/** The adaption callback function. This will refine elements inside of a given sphere and coarsen the elements - * outside of a given sphere. - * \tparam TMeshClass The mesh handle class. - * \param [in] mesh The mesh that should be adapted. - * \param [in] elements One element or a family of elements to consider for adaptation. - * \param [in] adapt_data The user data to be used during the adaptation process. - * \returns 1 if the first entry in \a elements should be refined, - * -1 if the family of elements should be coarsened, - * 0 else. -*/ -template -int -adapt_callback ([[maybe_unused]] const TMeshClass &mesh, std::span elements, - const adapt_data &adapt_data) -{ - auto element_centroid = elements[0].get_centroid (); - double dist = t8_dist (element_centroid, adapt_data.midpoint); - if (dist < adapt_data.refine_radius) { - return 1; // refine - } //first check if there is a family, and only if yes check if we should coarsen. - else if ((elements.size () > 1) && (dist > adapt_data.coarsen_radius)) { - return -1; // coarsen - } - return 0; // do nothing -} - -/** Build our adapted mesh by transferring the adaption parameters and adapting once with our adapt_callback function. - * \tparam TMeshClass The mesh handle class. - * \param sc_MPI_Comm The MPI Communicator. - * \param level The initial uniform refinement level. - * \returns Unique pointer to the adapted mesh. - */ -template -std::unique_ptr -build_mesh (sc_MPI_Comm comm, int level) -{ - /*Generate a hybrid hypercube, made out of cubes, prisms etc. */ - auto mesh = t8_mesh_handle::handle_hypercube_hybrid_uniform_default (level, comm); - /*Defining the adaption parameters. */ - struct adapt_data adapt_params = { { 0.5, 0.5, 1.0 }, 0.2, 0.4 }; - mesh->set_balance (); - mesh->set_partition (); - /*Adapting once with our adapt_callback function. */ - mesh->set_adapt ( - TMeshClass::template mesh_adapt_callback_wrapper (adapt_callback, adapt_params)); - mesh->set_ghost (); - mesh->commit (); - return mesh; -} - -/** Entry point of the program. */ -int -main (int argc, char **argv) -{ - /*Initialize MPI. This has to happen before we initialize sc or t8code. */ - int mpiret = sc_MPI_Init (&argc, &argv); - /*Error check the MPI return value. */ - SC_CHECK_MPI (mpiret); - /* Initialize the sc library, has to happen before we initialize t8code. */ - sc_init (sc_MPI_COMM_WORLD, 1, 1, NULL, SC_LP_ESSENTIAL); - /* Initialize t8code with log level SC_LP_PRODUCTION. See sc.h for more info on the log levels. */ - t8_init (SC_LP_PRODUCTION); - /* We will use MPI_COMM_WORLD as a communicator. */ - sc_MPI_Comm comm = sc_MPI_COMM_WORLD; - - /* Print a starting message. */ - t8_global_productionf (" [tutorial] \n"); - t8_global_productionf (" [tutorial] Hello, this is the mesh adaptation example of t8code using the mesh handle.\n"); - t8_global_productionf (" [tutorial] In this example we will adapt a mesh in a spherical shape around a given point " - "and write the adapted mesh to a vtu file.\n"); - t8_global_productionf (" [tutorial] \n"); - - using mesh_type = t8_mesh_handle::mesh<>; - - t8_global_productionf (" [tutorial] \n"); - t8_global_productionf (" [tutorial] Creating an adapted mesh.\n"); - t8_global_productionf (" [tutorial] \n"); - /* The initial uniform refinement level. */ - int uniform_level = 3; - /* Building the Mesh*/ - auto mesh = build_mesh (comm, uniform_level); - /* Write the mesh to a vtu file. */ - t8_global_productionf (" [tutorial] \n"); - t8_global_productionf (" [tutorial] Writing adapted mesh to vtu file: adapted_mesh.vtu\n"); - t8_global_productionf (" [tutorial] \n"); - t8_mesh_handle::write_mesh_to_vtk (*mesh, "adapted_mesh.vtu"); - - sc_finalize (); - mpiret = sc_MPI_Finalize (); - SC_CHECK_MPI (mpiret); - return 0; -} diff --git a/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx b/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx index 6f94aac0cf..1dd3fba82a 100644 --- a/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx +++ b/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx @@ -124,13 +124,14 @@ main (int argc, char **argv) /* The initial uniform refinement level. */ int uniform_level = 3; /* Building the mesh. */ - auto mesh = build_mesh (comm, uniform_level); - /* Write the mesh to a vtu file. */ - t8_global_productionf (" [tutorial] \n"); - t8_global_productionf (" [tutorial] Writing adapted mesh to vtu file: step3_adapted_mesh.vtu\n"); - t8_global_productionf (" [tutorial] \n"); - t8_mesh_handle::write_mesh_to_vtk (*mesh, "step3_adapted_mesh.vtu"); - + { /** Scope to ensure mesh is deleted properly. */ + auto mesh = build_mesh (comm, uniform_level); + /* Write the mesh to a vtu file. */ + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Writing adapted mesh to vtu file: step3_adapted_mesh.vtu\n"); + t8_global_productionf (" [tutorial] \n"); + t8_mesh_handle::write_mesh_to_vtk (*mesh, "step3_adapted_mesh.vtu"); + } sc_finalize (); mpiret = sc_MPI_Finalize (); SC_CHECK_MPI (mpiret); From bfe57f823f4ebb12a350f187e853dc636e176119 Mon Sep 17 00:00:00 2001 From: Schmitt Date: Mon, 20 Jul 2026 14:52:36 +0200 Subject: [PATCH 12/14] Integrated Review and cleaned up the comments. --- tutorials/CMakeLists.txt | 6 -- .../mesh_handle/default_adapt_callback.hxx | 87 +++++++++++++++++++ .../mesh_handle/t8_mesh_element_data.cxx | 61 ++++--------- .../mesh_handle/t8_mesh_step3_adapt_mesh.cxx | 69 +++++---------- .../t8_mesh_step4_partition_balance_ghost.cxx | 60 +++---------- 5 files changed, 140 insertions(+), 143 deletions(-) create mode 100644 tutorials/mesh_handle/default_adapt_callback.hxx diff --git a/tutorials/CMakeLists.txt b/tutorials/CMakeLists.txt index 667ce7f946..79d631bbb5 100644 --- a/tutorials/CMakeLists.txt +++ b/tutorials/CMakeLists.txt @@ -83,13 +83,7 @@ copy_tutorial_file (features/t8_features_curved_meshes_generate_cmesh_tri.geo) if( T8CODE_BUILD_MESH_HANDLE ) add_mesh_handle_tutorial( NAME t8_mesh_element_data SOURCES mesh_handle/t8_mesh_element_data.cxx ) -endif() - -if( T8CODE_BUILD_MESH_HANDLE ) add_mesh_handle_tutorial( NAME t8_mesh_step3_adapt_mesh SOURCES mesh_handle/t8_mesh_step3_adapt_mesh.cxx ) -endif() - -if( T8CODE_BUILD_MESH_HANDLE ) add_mesh_handle_tutorial( NAME t8_mesh_step4_partition_balance_ghost SOURCES mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx ) endif() diff --git a/tutorials/mesh_handle/default_adapt_callback.hxx b/tutorials/mesh_handle/default_adapt_callback.hxx new file mode 100644 index 0000000000..090a34f822 --- /dev/null +++ b/tutorials/mesh_handle/default_adapt_callback.hxx @@ -0,0 +1,87 @@ +/* + 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 types 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. +*/ + +/** \file default_adapt_callback.hxx + * This is the default adaptation callback function that can be used to refine and coarsen a mesh in a spherical shape around a given point. + * It is used in step 3, 4 and 5 in the mesh_handle tutorials. + */ + +#ifndef DEFAULT_ADAPT_CALLBACK_HXX +#define DEFAULT_ADAPT_CALLBACK_HXX + +#include /** General t8code header. Always include this. */ + +#include /** General Mesh header. Always needed for mesh_handle code. */ +#include /** t8 vector dataclass. */ +#include +#include + +/* The data that determines the adaptation characteristics of our algorithm. + * In this example we want to adapt in a spherical shape around a given point. */ +struct adapt_data +{ + t8_3D_vec midpoint; /**< midpoint of our sphere. */ + double refine_radius; /**< We refine inside this radius of our sphere.*/ + double coarsen_radius; /**< We coarsen outside this radius of our sphere. */ +}; + +/** + * The default adaptation callback function. + * + * This will refine elements inside of a given sphere and coarsen elements + * outside of a given sphere. + * + * \tparam mesh_type The mesh handle class. + * \param[in] mesh The mesh that should be adapted. + * \param[in] elements One element or a family of elements to consider. + * \param[in] adapt_data The user data used during adaptation. + * + * \return + * 1 if the first element should be refined, + * -1 if the family of elements should be coarsened, + * 0 otherwise. + */ +template +int +default_adapt_callback ([[maybe_unused]] const mesh_type& mesh, + std::span elements, const adapt_data& adapt_data) +{ + auto element_centroid = elements[0].get_centroid (); + + double dist = t8_dist (element_centroid, adapt_data.midpoint); + + if ( + dist + < adapt_data + .refine_radius) { /**< When this if Statement returns true, we are inside the set radius of our "refinement Sphere" of our point and therefor need to refine. */ + return 1; /**< Refine. */ + } + + /** Only coarsen if we actually have a complete family. */ + if ((elements.size () > 1) && (dist > adapt_data.coarsen_radius)) { + return -1; /**< Coarsen. */ + } + + return 0; /**< Do nothing. */ +} + +#endif // DEFAULT_ADAPT_CALLBACK_HXX diff --git a/tutorials/mesh_handle/t8_mesh_element_data.cxx b/tutorials/mesh_handle/t8_mesh_element_data.cxx index 03249f0002..a4dcbdf60f 100644 --- a/tutorials/mesh_handle/t8_mesh_element_data.cxx +++ b/tutorials/mesh_handle/t8_mesh_element_data.cxx @@ -21,18 +21,26 @@ */ /** \file t8_mesh_element_data.cxx - * This is the same as general/t8_step5_element_data.cxx but using the mesh handle interface instead of the forest + * This is step5 of the t8code tutorials. + * Therefor, this is the same as general/t8_step5_element_data.cxx but using the mesh handle interface instead of the forest * interface. + * In the following we will store data in the individual elements of our mesh. + * To do this, we will again create a uniform mesh, which will get adapted as in step4, + * with the difference that we partition, balance and create ghost elements all in the same step. + * After adapting the mesh we will learn how to build a data array and gather data for + * the local elements. Furthermore, we exchange the data values of the ghost elements and + * output the volume data to vtu. */ -#include +#include /** General t8code header. Always include this. */ -#include -#include -#include -#include +#include /** General Mesh header. Always needed for mesh_handle code. */ +#include /** Competence Pack for basic mesh_handle features. Look into tutorials/mesh_handle/t8_mesh_competences for more information. */ +#include /** Wrapper for basic Cmesh to mesh_handle conversions. */ +#include /** Used to export mesh to vtk files. */ #include -#include +#include /** t8 vector dataclass. */ +#include "default_adapt_callback.hxx" /** Default adaption function. */ #include #include @@ -44,41 +52,6 @@ struct data_per_element_type double volume; /**< Volume of the element. */ }; -/** User data type we will pass to the adapt callback. */ -struct user_data -{ - t8_3D_vec midpoint; /**< The midpoint of our sphere. */ - double refine_if_inside_radius; /**< If an element's center is smaller than this value, we refine the element. */ - double coarsen_if_outside_radius; /**< If an element's center is larger this value, we coarsen its family. */ -}; - -/** The adaptation callback function. This will refine elements inside of a given sphere and coarsen the elements - * outside of a given sphere. - * \tparam TMeshClass The mesh handle class. - * \param [in] mesh The mesh that should be adapted. - * \param [in] elements One element or a family of elements to consider for adaptation. - * \param [in] user_data The user data to be used during the adaptation process. - * \return 1 if the first entry in \a elements should be refined, - * -1 if the family \a elements shall be coarsened, - * 0 else. - */ -template -int -adapt_callback ([[maybe_unused]] const TMeshClass &mesh, std::span elements, - const user_data &user_data) -{ - auto element_centroid = elements[0].get_centroid (); - double dist = t8_dist (element_centroid, user_data.midpoint); - if (dist < user_data.refine_if_inside_radius) { - return 1; - } - // Check if we got a family and if yes, if we should coarsen. - if ((elements.size () > 1) && (dist > user_data.coarsen_if_outside_radius)) { - return -1; - } - return 0; -} - /** Build a mesh with initial uniform refinement level \a level which is adapted according to \ref adapt_callback, * partitioned and balanced afterwards, and ghost elements are set. * \tparam TMeshClass The mesh handle class. @@ -91,7 +64,7 @@ std::unique_ptr build_mesh (sc_MPI_Comm comm, int level) { auto mesh_handle = t8_mesh_handle::handle_hypercube_hybrid_uniform_default (level, comm); - struct user_data adapt_data = { + struct adapt_data adapt_params = { { 0.5, 0.5, 1 }, /* Midpoint of the sphere. */ 0.2, /* Refine if inside this radius. */ 0.4 /* Coarsen if outside this radius. */ @@ -100,7 +73,7 @@ build_mesh (sc_MPI_Comm comm, int level) mesh_handle->set_balance (); mesh_handle->set_partition (); mesh_handle->set_adapt ( - TMeshClass::template mesh_adapt_callback_wrapper (adapt_callback, adapt_data)); + TMeshClass::template mesh_adapt_callback_wrapper (&default_adapt_callback, adapt_params)); mesh_handle->set_ghost (); mesh_handle->commit (); return mesh_handle; diff --git a/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx b/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx index 1dd3fba82a..c8a014cc4d 100644 --- a/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx +++ b/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx @@ -21,55 +21,32 @@ */ /** \file t8_mesh_step3_adapt_mesh.cxx - * This is the same as general/t8_step3_adapt_forest.cxx but using the mesh handle interface instead of the forest + * This is step3 of the t8code mesh handle tutorials. + * Therefore, this is the same as general/t8_step3_adapt_forest.cxx but using the mesh handle interface instead of the forest * interface. + * After generating a coarse mesh (step1) and building a uniform mesh + * on it (step2), we will now adapt (= refine and coarsen) the mesh + * according to our own criterion. + * + * The geometry (coarse mesh) is again a cube, this time modelled with + * 6 tetrahedra, 6 prisms and 4 cubes. + * We refine an element if its midpoint is within a sphere of given radius + * around the point (0.5, 0.5, 1) and we coarsen outside of a given radius. + * We will use non-recursive refinement, that means that the refinement level + * of any element will change by at most +-1. */ -#include -#include -#include -#include -#include +#include /** General t8code header. Always include this. */ +#include /** General Mesh header. Always needed for mesh_handle code. */ +#include /** Competence Pack for basic mesh_handle features. Look into tutorials/mesh_handle/t8_mesh_competences for more information. */ +#include /** Wrapper for basic Cmesh to mesh_handle conversions. */ +#include /** Used to export mesh to vtk files. */ #include -#include +#include /** t8 vector dataclass. */ +#include "default_adapt_callback.hxx" /** Default adaption function. */ #include #include -/* The data that determines the adaptation characteristics of our algorithm. - * In this example we want to adapt in a spherical shape around a given point. */ -struct adapt_data -{ - t8_3D_vec midpoint; /**< midpoint of our sphere. */ - double refine_radius; /**< We refine inside this radius of our sphere.*/ - double coarsen_radius; /**< We coarsen outside this radius of our sphere. */ -}; - -/** The adaption callback function. This will refine elements inside of a given sphere and coarsen the elements - * outside of a given sphere. - * \tparam TMeshClass The mesh handle class. - * \param [in] mesh The mesh that should be adapted. - * \param [in] elements One element or a family of elements to consider for adaptation. - * \param [in] adapt_data The user data to be used during the adaptation process. - * \returns 1 if the first entry in \a elements should be refined, - * -1 if the family of elements should be coarsened, - * 0 else. -*/ -template -int -adapt_callback ([[maybe_unused]] const TMeshClass &mesh, std::span elements, - const adapt_data &adapt_data) -{ - auto element_centroid = elements[0].get_centroid (); - double dist = t8_dist (element_centroid, adapt_data.midpoint); - if (dist < adapt_data.refine_radius) { - return 1; /**< Refine. */ - } /** First check if there is a family, and only if yes coarsen. */ - else if ((elements.size () > 1) && (dist > adapt_data.coarsen_radius)) { - return -1; /**< Coarsen. */ - } - return 0; /**< Do Nothing. */ -} - /** Build our adapted mesh by transferring the adaption parameters and adapting once with our \ref adapt_callback function. * \tparam TMeshClass The mesh handle class. * \param sc_MPI_Comm The MPI Communicator. @@ -80,15 +57,15 @@ template std::unique_ptr build_mesh (sc_MPI_Comm comm, int level) { - /*Generate a hybrid hypercube, made out of cubes, prisms etc. */ + /* Generate a hybrid hypercube, made out of cubes, prisms etc. */ auto mesh = t8_mesh_handle::handle_hypercube_hybrid_uniform_default (level, comm); - /*Defining the adaption parameters. */ + /* Defining the adaption parameters. */ struct adapt_data adapt_params = { { 0.5, 0.5, 1.0 }, 0.2, 0.4 }; mesh->set_balance (); mesh->set_partition (); - /*Adapting once with our adapt_callback function. */ + /* Adapting once with our adapt_callback function. */ mesh->set_adapt ( - TMeshClass::template mesh_adapt_callback_wrapper (adapt_callback, adapt_params)); + TMeshClass::template mesh_adapt_callback_wrapper (default_adapt_callback, adapt_params)); mesh->set_ghost (); mesh->commit (); return mesh; diff --git a/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx b/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx index 586b66bff3..9259ef6b7c 100644 --- a/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx +++ b/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx @@ -21,59 +21,25 @@ */ /** \file t8_mesh_element_data.cxx - * This is the same as general/t8_step4_partition_balance_ghost.cxx but using the mesh handle interface instead of the forest + * This is step4 of the t8code mesh handle tutorials. + * Therefor, this is the same as general/t8_step4_partition_balance_ghost.cxx but using the mesh handle interface instead of the forest * interface. + * After generating a coarse mesh (step1), building a uniform mesh + * on it (step2) and adapting this mesh (step3) + * we will now learn how to control the mesh creation in more detail, + * how to partition and balance a mesh and how to generate a layer of ghost elements. */ -#include -#include -#include -#include +#include /** General t8code header. Always include this. */ +#include /** General Mesh header. Always needed for mesh_handle code. */ +#include /** Used to export mesh to vtk files. */ +#include /** Wrapper for basic Cmesh to mesh_handle conversions. */ #include -#include -#include -#include +#include /** t8 vector dataclass. */ +#include "default_adapt_callback.hxx" /** Default adaption function. */ #include #include -/** (This is the same as in tutorial step 3) - * The data that determines the adaptation characteristics of our algorithm. - * In this example we want to adapt in a spherical shape around a given point. -*/ -struct adapt_data -{ - std::array midpoint; /**< midpoint of our sphere. */ - double refine_radius; /**< We refine inside this radius of our sphere.*/ - double coarsen_radius; /**< We coarsen outside this radius of our sphere. */ -}; - -/** (This is the same as in tutorial step 3) - * The adaption callback function. This will refine elements inside of a given sphere and coarsen the elements - * outside of a given sphere. - * \tparam TMeshClass The mesh handle class. - * \param [in] mesh The mesh that should be adapted. - * \param [in] elements One element or a family of elements to consider for adaptation. - * \param [in] adapt_data The user data to be used during the adaptation process. - * \returns 1 if the first entry in \a elements should be refined, - * -1 if the family of elements should be coarsened, - * 0 else. -*/ -template -int -adapt_callback ([[maybe_unused]] const mesh_type &mesh, std::span elements, - const adapt_data &adapt_data) -{ - auto element_centroid = elements[0].get_centroid (); - double dist = t8_dist (element_centroid, adapt_data.midpoint); - if (dist < adapt_data.refine_radius) { - return 1; // refine - } //first check if there is a family, and only if yes check if we should coarsen. - else if ((elements.size () > 1) && (dist > adapt_data.coarsen_radius)) { - return -1; // coarsen - } - return 0; // do nothing -} - /** Helper function to print the total number of elements in the mesh after each step. * \param mesh The mesh handle to get the number of elements from. * \param stage The stage of the mesh (e.g. "Initial mesh", "Adapted mesh", etc.) to print in the output. @@ -158,7 +124,7 @@ main (int argc, char **argv) /* Adapting the mesh once with our adapt_callback function from step 3 and the parameters defined above. */ mesh_adapt->set_adapt ( - mesh_type::template mesh_adapt_callback_wrapper (&adapt_callback, adapt_params)); + mesh_type::template mesh_adapt_callback_wrapper (&default_adapt_callback, adapt_params)); /* Committing the adapted mesh. */ mesh_adapt->commit (); From b79b1d6227586b8605362aeba36aa16facd90a5f Mon Sep 17 00:00:00 2001 From: Schmitt Date: Mon, 20 Jul 2026 16:40:42 +0200 Subject: [PATCH 13/14] Adapted to comments. --- .../mesh_handle/t8_mesh_element_data.cxx | 4 ++-- .../mesh_handle/t8_mesh_step3_adapt_mesh.cxx | 4 ++-- .../t8_mesh_step4_partition_balance_ghost.cxx | 4 ++-- ...lback.hxx => t8_mesh_tutorials_common.hxx} | 23 +++++++------------ 4 files changed, 14 insertions(+), 21 deletions(-) rename tutorials/mesh_handle/{default_adapt_callback.hxx => t8_mesh_tutorials_common.hxx} (80%) diff --git a/tutorials/mesh_handle/t8_mesh_element_data.cxx b/tutorials/mesh_handle/t8_mesh_element_data.cxx index a4dcbdf60f..1bc8161d85 100644 --- a/tutorials/mesh_handle/t8_mesh_element_data.cxx +++ b/tutorials/mesh_handle/t8_mesh_element_data.cxx @@ -39,8 +39,8 @@ #include /** Wrapper for basic Cmesh to mesh_handle conversions. */ #include /** Used to export mesh to vtk files. */ #include -#include /** t8 vector dataclass. */ -#include "default_adapt_callback.hxx" /** Default adaption function. */ +#include /** t8 vector dataclass. */ +#include "t8_mesh_tutorials_common.hxx" /** Default adaption function. */ #include #include diff --git a/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx b/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx index c8a014cc4d..026988d85c 100644 --- a/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx +++ b/tutorials/mesh_handle/t8_mesh_step3_adapt_mesh.cxx @@ -42,8 +42,8 @@ #include /** Wrapper for basic Cmesh to mesh_handle conversions. */ #include /** Used to export mesh to vtk files. */ #include -#include /** t8 vector dataclass. */ -#include "default_adapt_callback.hxx" /** Default adaption function. */ +#include /** t8 vector dataclass. */ +#include "t8_mesh_tutorials_common.hxx" /** Default adaption function. */ #include #include diff --git a/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx b/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx index 9259ef6b7c..4569d3b514 100644 --- a/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx +++ b/tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx @@ -35,8 +35,8 @@ #include /** Used to export mesh to vtk files. */ #include /** Wrapper for basic Cmesh to mesh_handle conversions. */ #include -#include /** t8 vector dataclass. */ -#include "default_adapt_callback.hxx" /** Default adaption function. */ +#include /** t8 vector dataclass. */ +#include "t8_mesh_tutorials_common.hxx" /** Default adaption function. */ #include #include diff --git a/tutorials/mesh_handle/default_adapt_callback.hxx b/tutorials/mesh_handle/t8_mesh_tutorials_common.hxx similarity index 80% rename from tutorials/mesh_handle/default_adapt_callback.hxx rename to tutorials/mesh_handle/t8_mesh_tutorials_common.hxx index 090a34f822..0ce7605756 100644 --- a/tutorials/mesh_handle/default_adapt_callback.hxx +++ b/tutorials/mesh_handle/t8_mesh_tutorials_common.hxx @@ -25,17 +25,15 @@ * It is used in step 3, 4 and 5 in the mesh_handle tutorials. */ -#ifndef DEFAULT_ADAPT_CALLBACK_HXX -#define DEFAULT_ADAPT_CALLBACK_HXX +#pragma once #include /** General t8code header. Always include this. */ -#include /** General Mesh header. Always needed for mesh_handle code. */ -#include /** t8 vector dataclass. */ +#include /** Mesh concepts header. */ +#include /** t8 vector dataclass. */ #include -#include -/* The data that determines the adaptation characteristics of our algorithm. +/** The data that determines the adaptation characteristics of our algorithm. * In this example we want to adapt in a spherical shape around a given point. */ struct adapt_data { @@ -45,7 +43,7 @@ struct adapt_data }; /** - * The default adaptation callback function. + * Exemplary adaption callback function for the mesh handle. * * This will refine elements inside of a given sphere and coarsen elements * outside of a given sphere. @@ -68,12 +66,9 @@ default_adapt_callback ([[maybe_unused]] const mesh_type& mesh, auto element_centroid = elements[0].get_centroid (); double dist = t8_dist (element_centroid, adapt_data.midpoint); - - if ( - dist - < adapt_data - .refine_radius) { /**< When this if Statement returns true, we are inside the set radius of our "refinement Sphere" of our point and therefor need to refine. */ - return 1; /**< Refine. */ + /**< When this if Statement returns true, we are inside the set radius of our "refinement Sphere" of our point and therefor need to refine. */ + if (dist < adapt_data.refine_radius) { + return 1; /**< Refine. */ } /** Only coarsen if we actually have a complete family. */ @@ -83,5 +78,3 @@ default_adapt_callback ([[maybe_unused]] const mesh_type& mesh, return 0; /**< Do nothing. */ } - -#endif // DEFAULT_ADAPT_CALLBACK_HXX From 7b316f318e1cda860faf5b0587e7911e8d671500 Mon Sep 17 00:00:00 2001 From: Schmitt Date: Mon, 20 Jul 2026 16:42:25 +0200 Subject: [PATCH 14/14] renamed element data tutorial to step 5 --- .../{t8_mesh_element_data.cxx => t8_mesh_step5_element_data.cxx} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tutorials/mesh_handle/{t8_mesh_element_data.cxx => t8_mesh_step5_element_data.cxx} (100%) diff --git a/tutorials/mesh_handle/t8_mesh_element_data.cxx b/tutorials/mesh_handle/t8_mesh_step5_element_data.cxx similarity index 100% rename from tutorials/mesh_handle/t8_mesh_element_data.cxx rename to tutorials/mesh_handle/t8_mesh_step5_element_data.cxx