-
Notifications
You must be signed in to change notification settings - Fork 74
Improvement: Mesh handle tutorials #2348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vyp3er
wants to merge
26
commits into
DLR-AMR:main
Choose a base branch
from
Vyp3er:mesh_handle_tutorials
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+455
−62
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
05c741b
First Changes and File Creation
53199cb
Spellchecking correct
665d24c
Mesh_handle_tutorial Step3
9523fc5
Mesh_handle_tutorial Step3
604e003
Mesh_handle_tutorial Step3 fixed and Step4 added.
282f592
added bsd license
9f7696b
Merge remote-tracking branch 'origin/main' into mesh_handle_tutorials
bd84430
spell checking corrected
ff4aa99
Merge branch 'main' into mesh_handle_tutorials
Vyp3er d0dc470
Apply suggestion from @lenaploetzke
Vyp3er ca472bd
Documentation Changes and Renaming
9896011
added documentation and comments
a05095c
Added Scope into main to prevent deletion Error
bfe57f8
Integrated Review and cleaned up the comments.
b79b1d6
Adapted to comments.
7b316f3
renamed element data tutorial to step 5
8ba48ac
Fixed Pull Requested changes
0d60c0e
Apply suggestions from code review
Vyp3er c411a76
Fixed errors marked in pull request
4ab1785
change print labels
d47f0f5
Merge main and resolve conflicts
a06cc2d
Fixing comments
8500924
Fixed Doxygen errorrs
c800745
Fixed Doxygen errors
695c841
Doxygen errors
a0c918c
Doxygen errors
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| 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 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 <t8.h> /** General t8code header. Always include this. */ | ||
| #include <mesh_handle/mesh.hxx> /** General mesh header. Always needed for mesh_handle code. */ | ||
| #include <mesh_handle/competence_pack.hxx> /** Competence pack for basic mesh_handle features. | ||
| * Look into tutorials/mesh_handle/t8_mesh_competences for more information. */ | ||
| #include <mesh_handle/constructor_wrappers.hxx> /** Wrapper for basic cmesh to mesh_handle conversions. */ | ||
| #include <mesh_handle/mesh_io.hxx> /** Used to export mesh to vtk files. */ | ||
| #include <mesh_handle/concepts.hxx> /** Include this to use c++ concepts related to the mesh handle. | ||
| * This can be used to constraint the template parameters to only allow mesh handle classes. */ | ||
| #include "t8_mesh_tutorials_common.hxx" /** Adaption function definition used for this tutorial. */ | ||
| #include <memory> | ||
|
|
||
| /** Build our adapted mesh by transferring the adaption parameters and adapting once with our adapt_callback_sphere function. | ||
| * \tparam TMeshClass The mesh handle class. | ||
| * \param comm The MPI communicator. | ||
| * \param level The initial uniform refinement level. | ||
| * \returns Unique pointer to the adapted mesh. | ||
| */ | ||
| template <t8_mesh_handle::T8MeshType TMeshClass> | ||
| std::unique_ptr<TMeshClass> | ||
| 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<TMeshClass> (level, comm); | ||
| /* Saving the initial mesh to vtu files to compare them later. */ | ||
| t8_mesh_handle::write_mesh_to_vtk (*mesh, "step3_initial_uniform_mesh.vtu"); | ||
| /* Defining the adaption parameters. */ | ||
| adapt_data adapt_params = { { 0.5, 0.5, 1.0 }, 0.2, 0.4 }; | ||
| /** Adapting once using our adapt callback. | ||
| * set_adapt() only records how the mesh should be changed, it does not modify anything yet. | ||
| * commit() is the function that actually builds the new, adapted mesh from these settings. | ||
| * This "configure, then commit" split let's t8code carry out several mesh operations together in one efficient pass, rather than one at a time. | ||
| */ | ||
| mesh->set_adapt ( | ||
| TMeshClass::template mesh_adapt_callback_wrapper<adapt_data> (adapt_callback_sphere<TMeshClass>, adapt_params)); | ||
| mesh->commit (); | ||
|
Vyp3er marked this conversation as resolved.
|
||
| 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 (" [mesh_step3] \n"); | ||
| t8_global_productionf ( | ||
| " [mesh_step3] Hello, this is the mesh adaptation tutorial of t8code using the mesh handle.\n"); | ||
| t8_global_productionf ( | ||
| " [mesh_step3] In this tutorial 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 (" [mesh_step3] \n"); | ||
|
|
||
| using mesh_type = t8_mesh_handle::mesh<>; | ||
|
|
||
| t8_global_productionf (" [mesh_step3] \n"); | ||
| t8_global_productionf (" [mesh_step3] Creating an adapted mesh.\n"); | ||
| t8_global_productionf (" [mesh_step3] \n"); | ||
| /* The initial uniform refinement level. */ | ||
| const int uniform_level = 3; | ||
| /* Building the mesh. */ | ||
| { /** Scope to ensure mesh is deleted properly. */ | ||
| auto mesh = build_mesh<mesh_type> (comm, uniform_level); | ||
|
Vyp3er marked this conversation as resolved.
|
||
| /* Write the mesh to a vtu file. */ | ||
| t8_global_productionf (" [mesh_step3] \n"); | ||
| t8_global_productionf (" [mesh_step3] Writing adapted mesh to vtu file: step3_adapted_mesh.vtu\n"); | ||
| t8_global_productionf (" [mesh_step3] \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; | ||
| } | ||
|
Vyp3er marked this conversation as resolved.
|
||
216 changes: 216 additions & 0 deletions
216
tutorials/mesh_handle/t8_mesh_step4_partition_balance_ghost.cxx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| /* | ||
| 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_step4_partition_balance_ghost.cxx | ||
| * This is step4 of the t8code mesh handle tutorials. | ||
| * Therefore, 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 partition and balance a mesh and how to generate a layer of ghost elements. | ||
| */ | ||
|
|
||
| #include <t8.h> /** General t8code header. Always include this. */ | ||
| #include <mesh_handle/mesh.hxx> /** General mesh header. Always needed for mesh_handle code. */ | ||
| #include <mesh_handle/mesh_io.hxx> /** Used to export mesh to vtk files. */ | ||
| #include <mesh_handle/constructor_wrappers.hxx> /** Wrapper for basic cmesh to mesh_handle conversions. */ | ||
| #include <mesh_handle/concepts.hxx> /** Include this to use c++ concepts related to the mesh handle. This can be used to constraint the template parameters to only allow mesh handle classes. */ | ||
| #include <t8_types/t8_vec.hxx> /** t8 vector dataclass. */ | ||
| #include "t8_mesh_tutorials_common.hxx" /** Adaption function definition used for this tutorial. */ | ||
| #include <memory> | ||
|
|
||
| using mesh_type = t8_mesh_handle:: | ||
| mesh<>; /**< Mesh class used in this tutorial. We define it globally to get rid of the function templates to simplify the code. */ | ||
|
|
||
| /** 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. | ||
| */ | ||
| void | ||
| print_mesh_stats (const std::unique_ptr<mesh_type>& mesh, const char* stage) | ||
| { | ||
| int local_elements = mesh->get_num_local_elements (); | ||
| int global_elements = mesh->get_num_global_elements (); | ||
|
|
||
| t8_global_productionf (" [mesh_step4] === %s === \n", stage); | ||
| t8_global_productionf (" [mesh_step4] Local elements on this process: %i \n", local_elements); | ||
| t8_global_productionf (" [mesh_step4] Total elements: %i \n", global_elements); | ||
| } | ||
|
|
||
| /** Helper function to adapt a given mesh using the predefined adaption callback function. | ||
| * \param mesh The initial mesh to adapt. | ||
| * \param adapt_params The adaptation parameters to use for the adaptation. | ||
| */ | ||
| void | ||
| create_adapted_mesh (std::unique_ptr<mesh_type>& mesh, const adapt_data& adapt_params) | ||
| { | ||
| /* Setting the adapt-flag with our adapt_callback_sphere function from step 3 and the adapt_params. Both can be found in the file \ref t8_mesh_tutorials_common.hxx. */ | ||
| mesh->set_adapt ( | ||
| mesh_type::template mesh_adapt_callback_wrapper<adapt_data> (&adapt_callback_sphere<mesh_type>, adapt_params)); | ||
| /* Committing the mesh. */ | ||
| mesh->commit (); | ||
| } | ||
|
|
||
| /** Helper function to partition and balance a given mesh. | ||
| * \param mesh The initial mesh to adapt. | ||
| */ | ||
| void | ||
| create_partitioned_balanced_mesh (const std::unique_ptr<mesh_type>& mesh) | ||
| { | ||
| /* Setting partition flag.*/ | ||
| mesh->set_partition (); | ||
|
|
||
| /* Setting balancing flag. */ | ||
| mesh->set_balance (); | ||
|
|
||
| /* Committing the mesh. */ | ||
| mesh->commit (); | ||
| } | ||
|
|
||
| /** Helper function to create a mesh with ghosts from an initial mesh. | ||
| * \param mesh The initial mesh to adapt. | ||
| */ | ||
| void | ||
| create_ghost_mesh (const std::unique_ptr<mesh_type>& mesh) | ||
| { | ||
| /* Creating the ghost layers. */ | ||
| mesh->set_ghost (); | ||
|
|
||
| /* Committing the ghost mesh. */ | ||
| mesh->commit (); | ||
| } | ||
|
|
||
| /** 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 (" [mesh_step4] \n"); | ||
| t8_global_productionf (" [mesh_step4] Hello, this is the mesh adaptation example of t8code using the mesh handle.\n"); | ||
| t8_global_productionf (" [mesh_step4] In this example we will create a mesh, adapt, partition, balance " | ||
| "and create a ghost layer on it. \n"); | ||
| t8_global_productionf (" [mesh_step4] \n"); | ||
|
|
||
| /* The initial uniform refinement level. */ | ||
| const int uniform_level = 3; | ||
|
|
||
| /* Parameters for the adaption step. */ | ||
| adapt_data adapt_params = { { 0.5, 0.5, 1.0 }, 0.2, 0.4 }; | ||
|
|
||
| /** | ||
| * INITIAL MESH | ||
| */ | ||
|
|
||
| t8_global_productionf (" [mesh_step4] \n"); | ||
| t8_global_productionf (" [mesh_step4] Creating initial mesh.\n"); | ||
| t8_global_productionf (" [mesh_step4] \n"); | ||
| { /** Mesh scope begin. */ | ||
| /* Creating the initial mesh with uniform refinement. */ | ||
| auto mesh = t8_mesh_handle::handle_hypercube_hybrid_uniform_default<mesh_type> (uniform_level, comm); | ||
|
|
||
| /* Printing the mesh information. */ | ||
| print_mesh_stats (mesh, "Initial mesh"); | ||
|
|
||
| /* 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 (" [mesh_step4] \n"); | ||
| t8_global_productionf (" [mesh_step4] Creating adapted mesh.\n"); | ||
| t8_global_productionf (" [mesh_step4] \n"); | ||
|
|
||
| /** Call adaption helper function. */ | ||
| create_adapted_mesh (mesh, adapt_params); | ||
|
|
||
| /* Printing the mesh information. */ | ||
| print_mesh_stats (mesh, "Adapted mesh"); | ||
|
|
||
| /* Writing the mesh to vtu and pvtu files using the extended version of the function. */ | ||
| t8_mesh_handle::write_mesh_to_vtk_ext (*mesh, "adapted_mesh.vtu", 0, nullptr, true, true, true, true, true, false, | ||
| false); | ||
|
|
||
| /** | ||
| * PARTITIONED, BALANCED MESH | ||
| */ | ||
|
|
||
| t8_global_productionf (" [mesh_step4] \n"); | ||
| t8_global_productionf (" [mesh_step4] Creating partitioned and balanced mesh.\n"); | ||
| t8_global_productionf (" [mesh_step4] \n"); | ||
|
|
||
| /** Adapting the mesh from above a second time to see a difference when balancing. */ | ||
| create_adapted_mesh (mesh, adapt_params); | ||
|
|
||
| /** Call partitioning and balancing helper function. */ | ||
| create_partitioned_balanced_mesh (mesh); | ||
|
|
||
| /* Printing the mesh information. */ | ||
| print_mesh_stats (mesh, "Partitioned and Balanced mesh"); | ||
|
|
||
| /* 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_mesh.vtu", 0, nullptr, true, true, true, true, | ||
| true, false, false); | ||
|
|
||
| /** | ||
| * GHOST MESH | ||
| */ | ||
|
|
||
| t8_global_productionf (" [mesh_step4] \n"); | ||
| t8_global_productionf (" [mesh_step4] Creating ghost layer for mesh.\n"); | ||
| t8_global_productionf (" [mesh_step4] \n"); | ||
|
|
||
| /** Call ghost helper function. */ | ||
| create_ghost_mesh (mesh); | ||
|
|
||
| /* Printing the mesh information. */ | ||
| print_mesh_stats (mesh, "Ghost mesh"); | ||
| int ghost_elements = mesh->get_num_ghosts (); | ||
| t8_global_productionf (" [mesh_step4] Number of ghost elements: %i \n", ghost_elements); | ||
|
|
||
| /* 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_mesh.vtu", 0, nullptr, true, true, true, true, true, false, | ||
| false); | ||
|
|
||
|
Vyp3er marked this conversation as resolved.
|
||
| t8_global_productionf (" [mesh_step4] \n"); | ||
| t8_global_productionf (" [mesh_step4] Finished all steps successfully.\n"); | ||
| t8_global_productionf (" [mesh_step4] \n"); | ||
| } /** Mesh scope end. */ | ||
| sc_finalize (); | ||
| mpiret = sc_MPI_Finalize (); | ||
| SC_CHECK_MPI (mpiret); | ||
| return 0; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.