Examples and tutorials » Mesh geometry examples » Generating uniform grids

Control vertex counts, cell types, and physical dimensions.

Mesh::UniformGrid creates a structured mesh without an input file. The grid parameters count vertices along each coordinate direction. Coordinates initially have unit spacing; physical dimensions are set by scaling the mesh.

Create a two-dimensional grid

Sixteen vertices in each direction give fifteen intervals. Splitting each rectangular grid cell into two triangles produces 450 triangles and 256 vertices.

#include <iostream>
#include <Rodin/Geometry.h>

using namespace Rodin;
using namespace Rodin::Geometry;

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

  Real area = 0;
  for (auto cell = mesh.getCell(); !cell.end(); ++cell)
    area += cell->getMeasure();

  std::cout << "Vertices: " << mesh.getVertexCount() << '\n';
  std::cout << "Triangles: " << mesh.getCellCount() << '\n';
  std::cout << "Area: " << area << '\n';
  return 0;
}

Before scaling, the grid occupies $[0,15]^2$ . After scaling, it occupies $[0,1]^2$ and has area one. Scaling preserves the mesh connectivity and entity counts.

The cell type determines how each grid rectangle is represented:

Type and vertex countsVerticesCells
Triangle, {16, 16}256450 triangles
Quadrilateral, {32, 32}1024961 quadrilaterals

For unequal vertex counts $n_x,n_y$ , the initial domain is $[0,n_x-1]\times[0,n_y-1]$ . A scalar scaling factor preserves that aspect ratio. The parameters must provide at least two vertices in each direction.

Extend to three dimensions

Supply three vertex counts and choose a volume cell type.

Mesh tetrahedra = Mesh().UniformGrid(Polytope::Type::Tetrahedron, {8, 8, 8});
Mesh hexahedra = Mesh().UniformGrid(Polytope::Type::Hexahedron, {8, 8, 8});
Mesh wedges = Mesh().UniformGrid(Polytope::Type::Wedge, {8, 8, 8});

These grids initially occupy $[0,7]^3$ . Calling scale(1.0 / 7.0) on each mesh maps it to the unit cube. The selected type changes the cells inside each grid brick; the three parameters remain vertex counts.

Prepare boundary operations

Boundary algorithms require the relevant incidence relation. Exterior-edge detection in a triangular two-dimensional mesh uses edge-to-cell connectivity.

mesh.getConnectivity().compute(1, 2);

For a three-dimensional volume mesh, face-to-cell connectivity is (2, 3). See Computing mesh connectivity for the interpretation of these queries.

Check the generated geometry

getVertexCount() and getCellCount() check topology. Summing cell getMeasure() values checks the area of a two-dimensional mesh, as shown above. Mesh::getVolume() sums the measures of three-dimensional entities. getDimension() and getSpaceDimension() distinguish topological from ambient dimension. These checks help detect an unintended domain size before a field or PDE is constructed.

Source example

UniformGrid.cpp generates a three-dimensional wedge mesh and saves it in MEDIT format. The Poisson tutorial uses a scaled triangular grid in a complete solve.

Further reading