Wiki

Clone wiki

lifev-release / tutorial / Exporting_data

Go back


Exporting data

In this tutorial we show how to save a Finite Element vector to an external file for postprocessing. In particular, we will interpolate an analytical function on a given mesh (we suggest reading Writing and reading a datafile, Mesh handling and Assembling before proceeding to this tutorial).

We expand the datafile used in Mesh partitioning by adding an "exporter" section (see attached file exporter.in):

[exporter]
    type                   = hdf5
    output_name            = interpolatedField
    output_dir             = ./
[../]

where type can be hdf5, vtk, or ensight.

For what regards the C++ code, we first need to include the following headers

    #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/fem/FESpace.hpp>
    #include <lifev/core/array/VectorEpetra.hpp>

    #include <lifev/core/filter/ExporterEnsight.hpp>
    #include <lifev/core/filter/ExporterHDF5.hpp>
    #include <lifev/core/filter/ExporterVTK.hpp>

    #include <lifev/core/mesh/MeshData.hpp>
    #include <lifev/core/mesh/MeshPartitionTool.hpp>

and to define the following types:

    using namespace LifeV;

    typedef RegionMesh<LinearTetra> mesh_Type;
    typedef std::shared_ptr<mesh_Type> meshPtr_Type;
    typedef MeshPartitionTool<mesh_Type> meshPartitioner_Type;

    typedef FESpace<mesh_Type, MapEpetra> fespace_Type;
    typedef std::shared_ptr<fespace_Type> fespacePtr_Type;

    typedef std::function< Real( Real, Real, Real, Real, UInt ) > function_Type;

    typedef VectorEpetra vector_Type;
    typedef std::shared_ptr<vector_Type> vectorPtr_Type;

    typedef Exporter<mesh_Type> exporter_Type;
    typedef std::shared_ptr<exporter_Type> exporterPtr_Type;

Then, starting from the code from Read a mesh after the mesh has been successfully partitioned in the variable local_mesh, we define a scalar field and interpolate its values on the mesh:

        ...

        // Creating the P1 FE space
        fespacePtr_Type feSpace( new fespace_Type ( local_mesh, "P1", 1, Comm ) );

        // Defining the function to be interpolated
        function_Type func = []( Real t, Real x, Real y, Real z, UInt ID )
        {
          return x + y + z;
        };

        // Creating the FE vector to be filled with the interpolated values
        vectorPtr_Type interpolatedVector( new vector_Type( feSpace->map() ) );
        feSpace->interpolate ( func, *interpolatedVector );
        interpolatedVector->globalAssemble();

        ...

We can now create and set up the exporter. Depending on the chosen export format, we create a different exporter:

        ...

        // Creating exporter
        std::string exporterType = dataFile ( "exporter/type", "hdf5" );
        exporterPtr_Type exporter;

        if( exporterType.compare( "hdf5" ) == 0 ) exporter.reset( new ExporterHDF5<mesh_Type>() );
        else if( exporterType.compare( "vtk" ) == 0 ) exporter.reset( new ExporterVTK<mesh_Type>() );
        else if( exporterType.compare( "ensight" ) == 0 ) exporter.reset( new ExporterEnsight<mesh_Type>() );

        ...

The most common options for the exporter are the file directory and name; the setMultimesh method tells the exporter wether the mesh should be saved at each timestep (useful in case when the geometry moves during the simulation) or not:

        ...

        exporter->setPostDir ( dataFile ( "exporter/output_dir", "./" ) );
        exporter->setPrefix ( dataFile ( "exporter/output_name", "output" ) );
        exporter->setMeshProcId ( local_mesh, Comm->MyPID() );
        exporter->setMultimesh( false );

        ...

The addVariable command adds the given vector pointer to the list of variables which are saved each time the method postProcess is called

        ...

        // Add the solution to the exporter
        exporter->addVariable ( ExporterData<mesh_Type>::ScalarField, "value", feSpace, interpolatedVector, static_cast<UInt> ( 0 ) );

        ...

Finally, the postProcess method writes to file the vectors previously added to the exporter. Its only argument is the time at which the vector is to be saved:

        ...

        // Save the initial solution into the exporter
        exporter->postProcess ( 0 );

        ...
Below we show the exported vector opened in Paraview:

Download the mainExportingData.cpp.

Updated