Example 1: Processing input files and exporting vtu-files

Actually, the very first example given herein is not about the solution of some boundary value problems nor is it on boundary elements. It simply deals with the presentation of a possible tool-chain which might be used with BETL applications. In particular this example is about

Although the input- an export-methods do not belong to the BETL's core functionality they are, nevertheless, a part of the BETL distribution. And, while the use of those structures might be recommended, they are not mandatory since, e.g, the construction of the mesh container does not rely on a particular input-type. The complete source code for this example can be found at $BETL_ROOT/tutorial/example_1/main.cpp.

Starting with the coding itself, everything begins with the inclusion of some header files as depicted below.

// system includes -------------------------------------------------------------
#include <iostream>
#include <string>
#include <cstdlib>
// reader includes -------------------------------------------------------------
#include "gmsh_input/input.hpp"
// command line parser ---------------------------------------------------------
#include "clp/clp.hpp"
// betl core includes ----------------------------------------------------------
#include "element/element.hpp"
#include "geometry/mesh.hpp"
// exporter includes -----------------------------------------------------------
#include "vtu_exporter/exporter.hpp"

Beside the most common C++ headers there are a couple of BETL specific header files. Theses headers are self-explanatory and if not they will become so in the following.

In the main section the command line arguments are passed to a tiny library called CLP. This library implements a CommandLineParser whose intention is the extraction of the input's base-name from the command line. Note that all BETL structures and commands are embedded into the namespace betl. The base-name, i.e., the name provided on the command line without the final extension is returned by the method GetBasename().

int main( int argc, char* argv[] ) {
// parse the command line
betl::clp::CommandLineParser clp;
clp.Parse( argc, argv );

// define the basename 
const std::string basename = clp.GetBasename( );

The provided base-name serves as an argument for creating an object input of type betl::inp::gmsh::Input. The gmsh-namespace identifies the input as an input-parser for the gmsh mesh format. Refer to the gmsh-homepage for details on that file format. Note that the input type takes the number of nodes per element as a non-type template parameter. Hence, with this type only homogeneous meshes, i.e., meshes which contain only one element type, can be processed. Up to now, support for non-homogeneous meshes is not implemented. Possible choices of nodes_per_element are

// create the input
const std::size_t nodes_per_elem = 3; 
typedef betl::inp::gmsh::Input< nodes_per_elem > input_t;
input_t input( basename );

When the input has been parsed we can proceed with the construction of the mesh. In principle, the mesh itself is nothing but a data container for storing all the elements the model consists of. Hence, the Mesh type features the Element type as a template argument. As already mentioned at the beginning of this example the mesh can be constructed via several input data types as long as the input provides a certain set of functionality. Please take a look at the doxygen documentation for more information about all the mandatory interfaces the input needs to provide.

// with the input, create the mesh
typedef betl::Element< nodes_per_elem > element_t;
typedef betl::Mesh< element_t > mesh_t;
mesh_t mesh( input );

Once the mesh is created we can pass it through an Exporter object in order to prepare an output to a vtu-file. Naturally, when dealing with numerical methods one would like to pass more data to the exporter than just the topological information but this is left to the following examples. Here, the initialisation of an Exporter is rather simple. The instance exporter is created with a reference to the mesh object. Afterwards the Write-method is called in order to perform the writing of the vtu-file. The resulting file is written to the working directory as base-name.vtu.

// instantiate the exporter object
typedef betl::output::vtu::Exporter< mesh_t > exporter_t;
exporter_t exporter( mesh );
exporter.Write( basename );

Note that the export can be also done via stream objects. For instance, the following code snippet writes the contents of the vtu-file directly to the standard output.

// write vtu-content directly to stream
std::cout << exporter;

With the export of a vtu-file this very first example is done. You can build the example via make betl_ex1. Once created, simply enter betl_ex1 l_shape.msh (the extension can be omitted) at the prompt. If everything works, the output on screen should look similar to:

   Version              = 2.2
   Format               = ASCII
   No. of phys. names   = 0
   No. of nodes         = 20
   No. of element types = 1
      No. of elements of type 'TRIA_3' = 36

   Parsing finished after: 0.000497 sec

             Mesh<E>::creating a Element<3>-template mesh             
                         Some mesh statistics                         
  20 vertices created
  36 elements created

In addition, in your working directory you should find a file being named as l_shape.vtu. The contents of this file can be visualised with tools like, e.g, ParaView or MayaVi. For more information on the file format itself please visit the vtk homepage. The file format's specifications are also available as pdf.