Skip to content

Step 2 Creating a uniform mesh

Vincent Schmitt edited this page Oct 14, 2026 · 4 revisions

Step 2 – Creating a uniform mesh

Please have a look at step 0 and step 1 of the general t8code tutorials. Come back here, once you are finished. After creating a cmesh in step 1, we will now build our first mesh with the mesh handle. You find the code to this tutorial in tutorials/mesh_handle/t8_mesh_step2_uniform_mesh.cxx and it creates the executable tutorials/mesh_handle/t8_mesh_step2_uniform_mesh. Unlike what was described in step 1, we do not perform the AMR operations on a forest, but rather via the mesh_handle mesh. In this example we create a basic mesh as a uniformly refined cmesh. That means that every element of a given coarse mesh is on the same refinement level. This is usually the first step in creating a mesh and the mesh can then later be adapted to any given criterion, which we will do in step 3.

The first picture shows a cmesh of two prisms, while the second one shows the resulting uniform mesh with a refinement level of 3.

The ingredients

To build a uniform mesh, we need 3 ingredients:

  1. A t8_cmesh_t coarse mesh that provides the description of our domain. In this example we will choose a cube consisting of just two prisms:
t8_cmesh_t cmesh;
t8_cmesh_init(&cmesh); 
t8_cmesh_new_hypercube (&cmesh, T8_ECLASS_PRISM, comm, 0, 0, 0);
  1. A t8_scheme refinement scheme. The scheme holds all information on how to modify our elements. For example, how to refine elements, how to find the neighbor elements and how to compute the space filling curve index of an element. We provide a default implementation in t8_schemes/t8_default.hxx which implements the Morton space filling curves for all elements. To obtain the default scheme, we call
const t8_scheme *scheme = t8_scheme_new_default(); 
  1. The initial refinement level. An integer that specifies how many levels each element in the cmesh should get refined.
const int level = 3;

Building the mesh

We can now construct our mesh with a call to handle_new_uniform:

std::unique_ptr mesh = t8_mesh_handle::handle_new_uniform<TMeshClass> (cmesh, scheme, level, comm);

This will create a level 3 uniform mesh with the default refinement scheme on the cube geometry with 2 prism elements. This forest will be partitioned amongst the processes in comm such that each process has the same number of elements (+- 1). When creating the mesh, you can also specify if you want to create a layer of ghost elements by adding a 0 or a 1 as a fifth parameter into the method call. By default, this will not be done. You can also look into step4 for more details on ghost elements. Since the default scheme refines a prism into 8 subprisms, the resulting level 3 mesh has

2 * 8^3 = 1024 

elements.

Getting the number of elements

In mesh.hxx you find various functions to gather information about our mesh. We now want to get the global number of elements (1024) and the number of elements local to each process (depends on the number of processes, approx. 1024/#ranks) and print them. To do this we use the two function calls:

const t8_locidx_t local_num_elements = mesh->get_num_local_elements ();
const t8_gloidx_t global_num_elements = mesh->get_num_global_elements ();

which we then print with:

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); 

Note that due to using t8_global_productionf opposed to t8_productionfonly rank 0 will print its local number of elements. If you use t8_productionf instead, you can compare the local element numbers for different processes. You are invited to play with this 😊

Writing a mesh to .vtu files

To output our mesh to .vtu files we simply call

t8_mesh_handle::write_mesh_to_vtk (*mesh, prefix); 

which will create the file prefix.pvtu and for each MPI rank a file prefix_MPIRANK.vtu. In our example prefix = t8_step2_uniform_meshand when you open the t8_step2_uniform_mesh.pvtu file in ParaView you should be able to see this:

The uniform level 3 mesh on 5 MPI ranks. The colors correspond to different MPI ranks.

Clone this wiki locally