diff --git a/mesh_handle/mesh.hxx b/mesh_handle/mesh.hxx index b08fba5e78..6dacfc2419 100644 --- a/mesh_handle/mesh.hxx +++ b/mesh_handle/mesh.hxx @@ -141,6 +141,16 @@ class mesh: public TMeshCompetencePack::template apply /** General t8code header, always include this. */ +#include /** General Mesh Header, always needed for mesh_handle code. */ +#include /** cmesh definition and basic interface. */ +#include /** Wrapper for basic Cmesh to mesh_handle conversions. */ +#include /** Used to export mesh to vtk files. */ +#include /** default refinement scheme. */ +#include + +/** Builds cmesh of 2 prisms that build up a unit cube. + * See step1 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 (" [tutorial] Constructed coarse mesh with 2 prisms.\n"); + + return cmesh; +} + +/** Build a uniform mesh on a cmesh using the default refinement scheme. + * \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. + */ +static std::unique_ptr> +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. */ + auto mesh = t8_mesh_handle::handle_new_uniform> ( + cmesh, scheme, level, comm, false); + + t8_global_productionf (" [tutorial] Constructed uniform mesh with %d elements per tree.\n", 1 << (3 * level)); + + return mesh; +} + +int +main (int argc, char **argv) +{ + int mpiret; + sc_MPI_Comm comm; + + /** File prefix for our vtk files. */ + const char *prefix = "t8_step2_uniform_mesh"; + /** Uniform refinement level of the mesh. */ + const int level = 3; + t8_locidx_t local_num_elements; + t8_gloidx_t global_num_elements; + + /** Initialize MPI. This has to happen before we initialize sc or t8code. */ + 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_DEBUG); + + /** Print a message on the root process. */ + t8_global_productionf (" [tutorial] \n"); + t8_global_productionf (" [tutorial] Hello, this is the step2 example of t8code using the mesh handle.\n"); + t8_global_productionf (" [tutorial] In this example we build our first uniform mesh and output it to vtu files.\n"); + t8_global_productionf (" [tutorial] \n"); + + /** We will use MPI_COMM_WORLD as a communicator. */ + comm = sc_MPI_COMM_WORLD; + /** Create the cmesh. */ + t8_cmesh_t cmesh = t8_step2_build_prismcube_coarse_mesh (comm); + + /** Build the uniform mesh. */ + auto mesh = t8_step2_build_uniform_mesh (comm, cmesh, level); + /** Get the number of local elements. */ + local_num_elements = mesh->get_num_local_elements (); + /** Get the number of global elements. */ + global_num_elements = mesh->get_num_global_elements (); + + /** Print information on the mesh. */ + t8_global_productionf (" [tutorial] Created uniform mesh.\n"); + t8_global_productionf (" [tutorial] Refinement level:\t\t\t%i\n", level); + t8_global_productionf (" [tutorial] Local number of elements:\t\t%i\n", local_num_elements); + t8_global_productionf (" [tutorial] 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 (" [tutorial] Wrote mesh to vtu files:\t%s*\n", prefix); + + /** Destroy the mesh. */ + mesh.reset (); + t8_global_productionf (" [tutorial] Destroyed mesh.\n"); + + sc_finalize (); + + mpiret = sc_MPI_Finalize (); + SC_CHECK_MPI (mpiret); + + return 0; +}