NiHu  2.0
Mesh building

Introduction

The C++ NiHu core does not contain versatile mesh building methods for creating parametric meshes or to import different mesh formats. NiHu provides a single mesh building interface, the NiHu::create_mesh function that reads two mesh description matrices: one for the vertex locations and one for the element nodal connectivities.

Note
For versatile mesh generation and mesh importing functionalities, refer to the Matlab frontend.

The create_mesh function

The first example

The NiHu::create_mesh function takes two matrices as its first arguments:

  • nodes is the nodal location matrix
  • elements is the nodal connectivity matrix.

As NiHu uses the linear algebra library Eigen for matrix manipulations, we use two typedef-s for Eigen matrices.

typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> dMatrix;
typedef Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic> uMatrix;
  • dMatrix denotes a dynamically resizeable matrix with real (double) coefficients,
  • uMatrix denotes the same with unsigned coefficients.

We instantiate the two matrices by allocating their coefficients, and initialising them using Eigen's comma initialiser syntax:

dMatrix nodes(6,3);
nodes << // 3 4 5
0.0, 0.0, 0.0, // +---+---+
1.0, 0.0, 0.0, // | |2 /|
2.0, 0.0, 0.0, // | 0 | / |
0.0, 1.0, 0.0, // | |/ 1|
1.0, 1.0, 0.0, // +---+---+
2.0, 1.0, 0.0; // 0 1 2
uMatrix elements(3,5);
elements <<
NiHu::quad_1_elem::id, 0, 1, 4, 3,
NiHu::tria_1_elem::id, 1, 2, 5, 0,
NiHu::tria_1_elem::id, 1, 5, 4, 0;
  • The rows of matrix nodes contain 3D locations.
  • The rows or matrix elements contain an element id followed by as many element nodal indices as needed by the element type. Unused coefficients are left zero.

Apparently, we are building a heterogeneous mesh consisting of linear quadrangles (NiHu::quad_1_elem) and triangles (NiHu::tria_1_elem). The mesh is finally built by calling the NiHu::create_mesh function

auto my_mesh = NiHu::create_mesh(nodes, elements, NiHu::quad_1_tag(), NiHu::tria_1_tag());

The additional arguments NiHu::quad_1_tag () and NiHu::tria_1_tag () are additional type information telling the compiler which element types are used in the mesh. From this information, the compiler builds a specific mesh type that stores the specified element types separately, in an optimised way.

Note
The function's return type is hidden from the library user by the auto keyword. In fact, it is
mesh<tmp::vector<quad_1_elem, tria_1_elem> >

but this information is not needed to use the library.

Building a 2D mesh

The next example builds a circle from line elements in 2D:

double R = 1.0; // radius
unsigned N = 180; // number of elements
dMatrix nodes(N, 2); // nodal locations
for (unsigned i = 0; i < N; ++i)
{
double phi = i * (two_pi / N);
nodes(i,0) = R * cos(phi);
nodes(i,1) = R * sin(phi);
}
uMatrix elements(N,3); // element connections
for (unsigned i = 0; i < N; ++i)
{
elements(i,0) = NiHu::line_1_elem::id;
elements(i,1) = i;
elements(i,2) = (i+1)%N;
}
auto mesh = NiHu::create_mesh(nodes, elements, NiHu::line_1_tag()); // homogeneous mesh

Working with other matrix formats

The NiHu::create_mesh function is a template that can be called with any indexable matrix types. The only requirement against the matrix types is that they should be indexable with integers, using a (row,col)-type syntax.

This feature, together with the NiHu::mex::real_matrix class, makes easy Matlab MEX integration possible.

// the MATLAB entry point
void mexFunction(int nlhs, mxArray *lhs[], int nrhs, mxArray const *rhs[])
{
// matrix import
NiHu::mex::real_matrix<double> nodes(rhs[0]), elements(rhs[1]);
// create the mesh from Matlab
auto surf_mesh = NiHu::create_mesh(nodes, elements, NiHu::tria_1_tag(), NiHu::quad_1_tag());
}
Note
For more information on the Matlab MEX interface and on the arguments of the mexFunction, refer to the Matlab MEX site.

Reading a mesh from an OFF file

NiHu can import meshes stored in OFF. This can easily be done using the NiHu::read_off_mesh function. The usage of this function is demonstrated below.

auto msh = read_off_mesh("a_mesh_file.off", tria_1_tag(), quad_1_tag())

The element types to be read from the file are listed by passing the NiHu::read_off_mesh function instances of the corresponding tags.

Note
Only NiHu::tria_1_tag and quad_1_tag are currently supported by the NiHu::read_off_mesh function.
NiHu::mex::real_matrix
Definition: mex_matrix_separate.hpp:133
NiHu::type2tag
Netafunction assigning a tag to a type.
Definition: type2tag.hpp:17
NiHu::create_mesh
mesh< tmp::vector< typename tag2type< Args >::type... > > create_mesh(nodes_t const &nodes, elements_t const &elements, Args...)
factory function to create a mesh from nodes and elements matrices
Definition: mesh.hpp:287
mex_matrix.hpp
A Matlab mex matrix interface.