Skip to content

Grid traversal essentials

This page sharpens traversal concepts introduced in Grid Basics.

Learning goals

After this page, you should be able to:

  • iterate entities with range generators and explicit codimensions,
  • access geometry and local-to-global mappings,
  • traverse element subentities,
  • distinguish leaf traversal from level traversal.

Entities and codimension

DUNE uses a uniform entity concept:

  • elements (codim = 0)
  • facets (codim = 1)
  • edges (codim = dim - 1)
  • vertices (codim = dim)

For a 2D grid this becomes:

  • elements: cells,
  • facets: edges,
  • vertices: points.

Iteration patterns

Given a grid view:

auto gv = grid.leafGridView();

you typically iterate with range generators:

for (const auto& e : elements(gv)) { /* ... */ }
for (const auto& v : vertices(gv)) { /* ... */ }

For explicit codimension:

for (const auto& e : entities(gv, Dune::Codim<0>{})) { /* elements */ }

You can also use predefined entity ranges:

for (const auto& e : elements(gv)) { /* codim 0 */ }
for (const auto& f : facets(gv))   { /* codim 1 */ }
for (const auto& ed : edges(gv))   { /* codim dim-1 */ }
for (const auto& v : vertices(gv)) { /* codim dim */ }

Geometry access

Every element has a geometry object:

const auto geometry = element.geometry();
const auto center = geometry.center();
const double volume = geometry.volume();

In 2D, volume() is area. In 3D, it is volume.

Reference-to-physical mapping

geometry also maps local reference coordinates to global coordinates:

const Dune::FieldVector<double, 2> xi(0.5);   // reference center for a square
const auto x = geometry.global(xi);           // physical coordinate

This mapping is central for quadrature, local operators, and finite element assembly.

Accessing subentities

From an element (codim 0), you can access subentities of any codimension:

constexpr int codim = 2;
for (const auto& e : elements(gv))
  for (unsigned int i = 0; i < e.subEntities(codim); ++i)
    [[maybe_unused]] auto sub = e.template subEntity<codim>(i);

This pattern is useful for local indexing and element-local topology traversal.

Leaf vs level traversal

Two common views:

auto leaf = grid.leafGridView();      // active leaf entities
auto lvl0 = grid.levelGridView(0);    // entities on level 0

Leaf traversal is what you use in most assembled discretizations. Level traversal is useful for multilevel algorithms and refinement diagnostics.

What we intentionally skip here

We do not explain intersections deeply on this page. Intersections are covered in Grid intersections.

Summary and next steps

You now know the standard traversal patterns used in most DUNE algorithms. Next, read Entity and geometry details, then continue with Grid intersections.