General concepts » Meshes » Mesh creation

Construct a grid, build a tagged mesh or load an existing mesh.

A mesh must specify its embedding coordinates and cell connectivity. Region and boundary attributes attach integer labels to selected entities. This page introduces the three construction paths used in the examples.

Generate a uniform grid

UniformGrid takes a cell type and a vertex count for each coordinate axis. Its initial spacing is one. A triangular grid with 16 vertices per axis has 256 vertices, 450 triangles and domain $[0,15]^2$ . Scaling by $1/15$ produces the unit square.

Mesh mesh = Mesh().UniformGrid(Polytope::Type::Triangle, {16, 16});
mesh.scale(1.0 / 15.0);

Two-dimensional grids

Triangular and quadrilateral grids share this coordinate convention. Each rectangular grid block gives two triangles or one quadrilateral.

Three-dimensional grids

The shape argument has three entries for tetrahedra, pyramids, hexahedra and wedges. See Generating uniform grids for supported types and the resulting counts.

Domain scaling

An axis with $n_i$ vertices spans $[0,n_i-1]$ . Uniform scaling changes all axes by the same factor. For anisotropic domains, supply coordinates through the builder or apply an appropriate coordinate map.

Load a mesh

File format

MFEM and MEDIT both use the .mesh extension. Pass the format explicitly. The conversion example writes its own input file and checks the loaded mesh counts.

Loading

  Mesh loaded;
  loaded.load("Square.medit.mesh", IO::FileFormat::MEDIT);
  loaded.save("Square.mfem.mesh", IO::FileFormat::MFEM);

  Mesh converted;
  converted.load("Square.mfem.mesh", IO::FileFormat::MFEM);

Build vertices and cells

The builder records vertex coordinates first, then cells using zero-based vertex indices. initialize(2) selects a two-dimensional embedding space. The following rectangle combines two triangles with one quadrilateral. Attributes one and two label its left and right regions.

Connectivity

  Mesh mesh = Mesh<Context::Local>::Builder()
                .initialize(2)
                .nodes(6)
                .vertex({0, 0})
                .vertex({1, 0})
                .vertex({2, 0})
                .vertex({0, 1})
                .vertex({1, 1})
                .vertex({2, 1})
                .polytope(Polytope::Type::Triangle, {0, 1, 4})
                .attribute({2, 0}, 1)
                .polytope(Polytope::Type::Triangle, {0, 4, 3})
                .attribute({2, 1}, 1)
                .polytope(Polytope::Type::Quadrilateral, {1, 2, 5, 4})
                .attribute({2, 2}, 2)
                .finalize();
  mesh.getConnectivity().compute(1, 2);
  mesh.getConnectivity().compute(2, 2);

Region attributes

.attribute({dimension, index}, value) labels an entity already inserted into the builder. Cell index and vertex index belong to different dimensions. The constructed rectangle has area two; each tagged region has area one.

Mixed cell types

Mixed meshes share vertex indices across adjacent cells. Their local vertex order must follow the reference-element convention. The polytope guide describes the supported shapes.

Construct a box boundary

Box takes a face type and an interval count for each embedding-space axis. With triangular faces, three interval counts describe a two-dimensional surface embedded in three dimensions. The following surface encloses the unit cube after scaling by one third and has area six. In this factory, three intervals span coordinates zero to three before scaling.

  Mesh surface = Mesh().Box(Polytope::Type::Triangle, {3, 3, 3});
  surface.scale(1.0 / 3.0);
  std::cout << "Box boundary: dimension " << surface.getDimension()
            << ", embedding dimension " << surface.getSpaceDimension() << ", area "
            << surface.getArea() << std::endl;

Empty meshes

Default construction creates an empty mesh. load() replaces it with file data; a builder's finalize() produces a constructed mesh. mesh.isEmpty() can be checked before operations requiring cells.

Mesh and field lifetimes

Meshes can be copied and moved. Finite element spaces and grid functions borrow their mesh; retain that mesh and its topology for their lifetime. Rebuild dependent spaces and problems after changing topology. See Numerical contracts for the corresponding algebraic lifetimes.

Checked program

The MeshBasics target validates counts, region measures, boundary extraction and the box surface. Its source is Geometry/MeshBasics.cpp.

Continue