Wiki

Clone wiki

lifev-release / tutorial / Importing_data

Go back


Importing data

In this tutorial we show how to load a Finite Element vector from an external file. In particular, we will import a vector defined on a given mesh and compute its 2-norm (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 "importer" section (see attached file importer.in):

    [importer]
        type                   = hdf5
        input_name             = interpolatedField
        vector_name            = value
    [../]

where type can be hdf5, vtk, or ensight. We 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 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 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, similarly to what was done in Exporting data, we define the FE space and the vector that we want to fill with the imported values:

        ...

        // Creating the P1 FE space and the vector to be filled
        fespacePtr_Type feSpace( new fespace_Type ( local_mesh, "P1", 1, Comm ) );
        vectorPtr_Type loadedVector;
        loadedVector.reset( new vector_Type( feSpace->map(), Unique ) );

        ...

Then, we create and set up the importer

        ...

        // Creating importer
        std::string importerType = dataFile ( "importer/type", "hdf5" );
        exporterPtr_Type importer;

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


        // Setting importer
        importer->setMeshProcId( local_mesh, Comm->MyPID() );
        importer->setPrefix ( dataFile ( "importer/input_name", "filename" ) );

        LifeV::ExporterData< RegionMesh< LinearTetra > > reader( LifeV::ExporterData< RegionMesh< LinearTetra > >::ScalarField,
                                                                 std::string( dataFile( "importer/vector_name", "vectorname") + ".00000" ),
                                                                 feSpace, loadedVector, UInt( 0 ),
                                                                 LifeV::ExporterData< RegionMesh< LinearTetra > >::UnsteadyRegime );

        ...

Finally, we load the given vector and print its 2-norm on the screen:

        ...

        // Reading vector and computing 2-norm
        importer->readVariable( reader );
        std::cout << "The 2-norm of the imported vector is " << loadedVector->norm2() << '\n';

        ...

Download the mainImportingData.cpp.

Updated