-
Notifications
You must be signed in to change notification settings - Fork 74
Improvement: Step 2 tutorial and get_num_global_elements to mesh.hxx #2371
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
Merged
lenaploetzke
merged 8 commits into
DLR-AMR:main
from
Vyp3er:step_2_creating_uniform_mesh_tutorial
Aug 10, 2026
+178
−4
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
54f48b6
Added tutorial Step 2 and getter function get_num_global_elements to …
81f193d
Fixed several Spelling issues and Documentation.
0a4455e
Changed print messages
34b8c2a
Fixed syntax error.
c5c04e3
Apply suggestions from code review
Vyp3er b9cc8d2
Changed templating in mesh building function.
e25b206
Spellchecking
Vyp3er f160bba
Fixed capitalization and line breaks.
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
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
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
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
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,161 @@ | ||
| /* | ||
| 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_step2_uniform_mesh.cxx | ||
| * This is step 2 of the t8code mesh handle tutorials. | ||
| * Therefore, this is the same as general/t8_step2_uniform_forest.cxx but using the mesh handle interface instead of the forest | ||
| * interface. | ||
| * After we learned how to create a cmesh in step1, we will | ||
| * now build our first partitioned mesh, get its local and global | ||
| * element count, and output it into .vtu files. | ||
| * | ||
| * When we create a mesh from a coarse mesh using the mesh handle interface, the mesh will always be | ||
| * uniform (every element has the same refinement level) and can then be adapted | ||
| * later (see the following steps). | ||
| * Together with the cmesh, we also need a refinement scheme. This scheme tells the | ||
| * mesh how elements of each shape (t8_eclass_t) are refined, what their neighbors | ||
| * are etc. | ||
| * The default scheme in t8_schemes/t8_default/t8_default.hxx provides an implementation for | ||
| * all element shapes that t8code supports. | ||
| */ | ||
| #include <t8.h> /** General t8code header, always include this. */ | ||
| #include <mesh_handle/mesh.hxx> /** General mesh header, always needed for mesh_handle code. */ | ||
| #include <t8_cmesh/t8_cmesh.h> /** Cmesh definition and basic interface. */ | ||
| #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 <t8_schemes/t8_default/t8_default.hxx> /** Default refinement scheme. */ | ||
| #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 <memory> | ||
|
|
||
| /** Builds cmesh of 2 prisms that build up a unit cube. | ||
| * See \ref tutorials/general/t8_step1_coarsemesh.cxx for a detailed description. | ||
| * \param [in] comm MPI Communicator to use. | ||
| * \return The coarse mesh. | ||
| */ | ||
| static t8_cmesh_t | ||
| t8_step2_build_prismcube_coarse_mesh (sc_MPI_Comm comm) | ||
| { | ||
| t8_cmesh_t cmesh; | ||
|
|
||
| /* Build a coarse mesh of 2 prisms that form a cube. */ | ||
| t8_cmesh_init (&cmesh); | ||
| t8_cmesh_new_hypercube (&cmesh, T8_ECLASS_PRISM, comm, 0, 0, 0); | ||
| t8_global_productionf (" [mesh_step2] Constructed coarse mesh with 2 prisms.\n"); | ||
|
|
||
| return cmesh; | ||
| } | ||
|
|
||
| /** Build a uniform mesh on a cmesh using the default refinement scheme. | ||
| * | ||
| * The mesh type is constrained by the \ref t8_mesh_handle::T8MeshType concept, | ||
| * which ensures that TMeshClass provides the required mesh handle interface. | ||
| * | ||
| * \tparam TMeshClass the mesh handle type to construct. It must satisfy the \ref t8_mesh_handle::T8MeshType concept. | ||
| * \param [in] comm MPI Communicator to use. | ||
| * \param [in] cmesh The coarse mesh to build the uniform mesh on. | ||
| * \param [in] level The initial uniform refinement level. | ||
| * \return A uniform mesh with the given refinement level that is | ||
| * partitioned across the processes in \a comm. | ||
| */ | ||
|
Vyp3er marked this conversation as resolved.
|
||
| template <t8_mesh_handle::T8MeshType TMeshClass> | ||
| static std::unique_ptr<TMeshClass> | ||
| t8_step2_build_uniform_mesh (sc_MPI_Comm comm, t8_cmesh_t cmesh, int level) | ||
| { | ||
| const t8_scheme *scheme = t8_scheme_new_default (); /** Default refinement scheme. */ | ||
|
|
||
| /* Build the uniform mesh, it is automatically partitioned among the processes. */ | ||
| std::unique_ptr mesh = t8_mesh_handle::handle_new_uniform<TMeshClass> (cmesh, scheme, level, comm); | ||
|
|
||
| t8_global_productionf (" [mesh_step2] Constructed uniform mesh with refinement level %d.\n", level); | ||
|
|
||
| return mesh; | ||
| } | ||
|
|
||
| int | ||
| main (int argc, char **argv) | ||
| { | ||
| /** File prefix for our vtk files. */ | ||
| const char *prefix = "t8_step2_uniform_mesh"; | ||
| /** Uniform refinement level of the mesh. */ | ||
| const int level = 3; | ||
|
|
||
| /** 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); | ||
|
|
||
| /** Print a message on the root process. */ | ||
| t8_global_productionf (" [mesh_step2] \n"); | ||
| t8_global_productionf (" [mesh_step2] Hello, this is step 2 of t8code's mesh handle tutorials.\n"); | ||
| t8_global_productionf ( | ||
| " [mesh_step2] In this tutorial we build our first uniform mesh and output it to vtu files.\n"); | ||
| t8_global_productionf (" [mesh_step2] \n"); | ||
|
|
||
| /** We will use MPI_COMM_WORLD as a communicator. */ | ||
| sc_MPI_Comm comm = sc_MPI_COMM_WORLD; | ||
|
|
||
| /** Create the cmesh. */ | ||
| t8_cmesh_t cmesh = t8_step2_build_prismcube_coarse_mesh (comm); | ||
| /** | ||
| * We will put the mesh in a separate scope here, | ||
| * because it will be destroyed automatically at the end of this scope. | ||
| * This is only needed because SC_CHECK_MPI checks for leftover references. | ||
| * Otherwise, it would be destroyed at the end of the main function. | ||
| */ | ||
| { | ||
| /** Build the uniform mesh using a mesh type that satisfies the T8MeshType concept. | ||
| * The mesh class is templated so that further capabilities and features that not needed by default can be added. | ||
| * In this case, the default mesh class is sufficient. | ||
| */ | ||
| using mesh_class = t8_mesh_handle::mesh<>; | ||
|
|
||
| auto mesh = t8_step2_build_uniform_mesh<mesh_class> (comm, cmesh, level); | ||
| /** Get the number of local elements. */ | ||
| const t8_locidx_t local_num_elements = mesh->get_num_local_elements (); | ||
| /** Get the number of global elements. */ | ||
| const t8_gloidx_t global_num_elements = mesh->get_num_global_elements (); | ||
|
|
||
| /** Print information on the mesh. */ | ||
| t8_global_productionf (" [mesh_step2] Created uniform mesh.\n"); | ||
| t8_global_productionf (" [mesh_step2] Refinement level:\t\t\t%i\n", level); | ||
| t8_global_productionf (" [mesh_step2] Local number of elements:\t\t%i\n", local_num_elements); | ||
| t8_global_productionf (" [mesh_step2] Global number of elements:\t%" T8_GLOIDX_FORMAT "\n", global_num_elements); | ||
|
|
||
| /** Write mesh to vtu files. */ | ||
| t8_mesh_handle::write_mesh_to_vtk (*mesh, prefix); | ||
| t8_global_productionf (" [mesh_step2] Wrote mesh to vtu files:\t%s*\n", prefix); | ||
|
|
||
| } /** End of Mesh scope. */ | ||
| t8_global_productionf (" [mesh_step2] Mesh scope ended.\n"); | ||
|
|
||
| sc_finalize (); | ||
|
|
||
| mpiret = sc_MPI_Finalize (); | ||
| SC_CHECK_MPI (mpiret); | ||
|
|
||
| return 0; | ||
| } | ||
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.