Wiki

Clone wiki

lifev-release / tutorial / Read_a_mesh

Go back


Read a mesh

In this tutorial we show how to read a mesh in LifeV. The LifeV classes to be used are called MeshData and RegionMesh which are located in lifev/core/mesh.

In the datafile of our example we need to introduce a section which has to contain the following fields

  • mesh_dir: it is the directory where our mesh is located
  • mesh_file: it is the name of the file (with its extension)
  • mesh_type: the extension (i.e. the mesh type) of the mesh used

The input datafile can be written in either the GetPot or the ParameterList formats. In this tutorial we make use of the GetPot datafile format. The datafile used (called readMesh.in) is reported below and can be downloaded here

[mesh]
    mesh_dir  = ./ 
    mesh_file = cube_geometry.mesh
    mesh_type = .mesh
[../]

The main steps involved in the reading mesh phase are:

  1. Read the input datafile
  2. Instantiate an object of type MeshData and RegionMesh
  3. Read the mesh

In LifeV the mesh is an object of type RegionMesh and it is templated over the element type. Examples of element type are LinearTetra (tetrahedral elements with straight edges), LinearTriangle (triangular elements with straight edges), etc. In our case the mesh that we want to load is comprised of tetrahedra, therefore the template parameter of the RegionMesh object is LinearTetra. In the code example reported below we show step by step how a mesh can be loaded by LifeV:

#include <Epetra_ConfigDefs.h>
#ifdef EPETRA_MPI
#include <mpi.h>
#include <Epetra_MpiComm.h>
#else
#include <Epetra_SerialComm.h>
#endif

#include <lifev/core/LifeV.hpp>
#include <lifev/core/filter/GetPot.hpp>
#include <lifev/core/mesh/MeshData.hpp>

using namespace LifeV;

int main ( Int argc, char** argv )
{
    typedef RegionMesh<LinearTetra> mesh_Type;
    typedef std::shared_ptr<mesh_Type> meshPtr_Type;

#ifdef HAVE_MPI
    MPI_Init (&argc, &argv);
    std::shared_ptr<Epetra_Comm> Comm ( new Epetra_MpiComm (MPI_COMM_WORLD) );
#else
    std::shared_ptr<Epetra_Comm> Comm( new Epetra_SerialComm () );
    verbose = true;
#endif

    // Read datafile
    GetPot dataFile ( "readMesh.in" );

    // Instantiate RegionMesh and MeshData objects
    mesh_Type mesh ( Comm );
    MeshData meshData;

    // Reading the mesh
    meshData.setup (dataFile, "mesh");
    readMesh (mesh, meshData);

    // Print out some mesh information 
    if ( Comm->MyPID() == 0 )
    {
        std::cout << "Number of tetrahedra = " << mesh.numVolumes() << ", number of vertices " << mesh.numVertices() << std::endl;
    }

#ifdef HAVE_MPI
    std::cout << std::endl << "MPI Finalization" << std::endl;
    MPI_Finalize();
#endif

    return 0;
}

The MeshData object takes as input the instance of GetPot object created and the name of the section in the datafile where the mesh information are written. Then, to actually load the mesh we use the function readMesh whose inputs are the RegionMesh and MeshData objects. Here you can download the readMesh.in and mainReadMesh.cpp files.

Updated