#undef SEEK_SET
C++ include files that we need
#include <iostream>
Functions to initialize the library.
#include "libmesh.h"
Basic include files needed for the mesh functionality.
#include "mesh.h"
int main (int argc, char** argv)
{
Initialize the library. This is necessary because the library
may depend on a number of other libraries (i.e. MPI and Petsc)
that require initialization before use.
libMesh::init (argc, argv);
Force all our objects to have local scope. By declaring
libMesh objects in the next pair of braces we can assert
that they will go out of scope (and should have been deleted)
before we return from main. This allows the library to do
internal reference counting and assure memory is not leaked.
{
Check for proper usage. The program is designed to be run
as follows:
./ex1 -d DIM input_mesh_name [output_mesh_name]
where [output_mesh_name] is an optional parameter giving
a filename to write the mesh into.
if (argc < 4)
{
std::cerr << "Usage: " << argv[0] << " -d 2 in.mesh [out.mesh]"
<< std::endl;
This handy function will print the file name, line number,
and then abort. Currently the library does not use C++
exception handling.
error();
}
Get the dimensionality of the mesh from argv[2]
const unsigned int dim = std::atoi(argv[2]);
Create a mesh with the requested dimension.
Mesh mesh(dim);
Read the input mesh.
mesh.read (argv[3]);
mesh.find_neighbors();
Print information about the mesh to the screen.
mesh.print_info();
Write the output mesh if the user specified an
output file name.
if (argc == 5)
mesh.write (argv[4]);
At this closing brace all of our objects will be forced
out of scope and will get deconstructed.
}
All done. Call the libMesh::close() function to close any
external libraries and check for leaked memory. To be absolutey
certain this is called last we will return its value. This
also allows main to return nonzero if memory is leaked, which
can be useful for testing purposes.
return libMesh::close();
}
The program without comments:
#undef SEEK_SET
#include <iostream>
#include "libmesh.h"
#include "mesh.h"
int main (int argc, char** argv)
{
libMesh::init (argc, argv);
{
if (argc < 4)
{
std::cerr << "Usage: " << argv[0] << " -d 2 in.mesh [out.mesh]"
<< std::endl;
error();
}
const unsigned int dim = std::atoi(argv[2]);
Mesh mesh(dim);
mesh.read (argv[3]);
mesh.find_neighbors();
mesh.print_info();
if (argc == 5)
mesh.write (argv[4]);
}
return libMesh::close();
}
The console output of the program:
*************************************************************** * Running Example ./ex1-devel -d 3 ../../reference_elements/3D/one_hex27.xda *************************************************************** Mesh Information: mesh_dimension()=3 spatial_dimension()=3 n_nodes()=27 n_elem()=1 n_local_elem()=1 n_active_elem()=1 n_subdomains()=1 n_processors()=1 processor_id()=0 *************************************************************** * Done Running Example ./ex1-devel -d 3 ../../reference_elements/3D/one_hex27.xda ***************************************************************
Example 1 - Creation of a Mesh Object
This is the first example program. It simply demonstrates how to create a mesh object. A mesh is read from file, information is printed to the screen, and the mesh is then written.