Skip to content

dune-grid: first contact

This page is your first practical step with dune-grid. You will create a simple Cartesian grid, obtain a GridView, and iterate over elements.

Learning goals

After this page, you should be able to:

  • create a structured 2D grid with Dune::YaspGrid,
  • explain the difference between Grid and GridView,
  • count entities of different codimensions,
  • loop over elements and read geometric data (center and area).

Create a structured grid

The smallest useful example is a unit square split into 4 x 4 cells.

#include <iostream>

#include <dune/grid/yaspgrid.hh>

int main()
{
    constexpr int dim = 2;

    Dune::FieldVector<double, dim> L(1.0);  // domain size: [0,1] x [0,1]
    std::array<int, dim> N{4,4};            // cells per coordinate direction

    Dune::YaspGrid<dim> grid(L, N);
    auto gv = grid.leafGridView();

    std::cout << "Grid dimension: " << gv.dimension << "\n";
    std::cout << "Number of elements: " << gv.size(0) << "\n";
}

File: examples/01_grid_creation/grid_creation.cc

What this means

  • YaspGrid<2> is a structured Cartesian grid in 2D.
  • L stores physical lengths in each coordinate direction.
  • N stores how many cells you want in each direction.
  • grid is the hierarchical data structure.
  • gv = grid.leafGridView() is the view on the current leaf entities.

Common grid types in dune-grid

dune-grid offers multiple grid implementations with different properties:

  • Dune::OneDGrid: 1D line grids (segments).
  • Dune::YaspGrid<dim>: structured Cartesian grids (axis-aligned cubes/hexahedra).
  • Dune::UGGrid<dim>: unstructured grids (simplices and cubes, depending on input/refinement).
  • Dune::AlbertaGrid<dim,dimworld>: simplex-based grids (triangles/tetrahedra), when Alberta is available.

Which grid type you choose depends on:

  • geometry and element types you need,
  • whether your grid is structured or unstructured,
  • which external grid managers/modules are available in your setup.

Grid vs GridView

This distinction appears everywhere in DUNE:

  • Grid: owns topology and hierarchy (levels, refinement, etc.).
  • GridView: iterable "slice" of the grid (for example leaf level).

Most algorithms use a GridView, not the full Grid.

Codimension in practice

For a dim-dimensional grid:

  • codim 0 = elements (cells),
  • codim 1 = facets (faces in 3D, edges in 2D),
  • codim dim-1 = edges,
  • codim dim = vertices.

Typical queries:

gv.size(0);      // number of elements
gv.size(dim);    // number of vertices

Traverse elements and access geometry

Now iterate over all elements and print center and area.

#include <array>
#include <iostream>

#include <dune/common/fvector.hh>
#include <dune/grid/common/rangegenerators.hh>
#include <dune/grid/yaspgrid.hh>

int main()
{
    constexpr int dim = 2;

    Dune::FieldVector<double, dim> L(1.0);
    std::array<int, dim> N{4, 4};
    Dune::YaspGrid<dim> grid(L, N);

    const auto gv = grid.leafGridView();

    for (const auto& element : elements(gv))
    {
        const auto geometry = element.geometry();
        const auto center = geometry.center();
        const double area = geometry.volume();

        std::cout << "center = " << center << ", area = " << area << "\n";
    }
}

File: examples/02_grid_traversal/traversal.cc

Hierarchy preview

DUNE grids are hierarchical. After refinement, you can work on:

  • levels (grid.levelGridView(level)),
  • leaves (grid.leafGridView()),
  • parent/child relations of entities (for interpolation and transfer operations).

This is covered next in Grid Hierarchy and Refinement.

Summary and next steps

You now have the core mental model for dune-grid:

  • build a grid,
  • select a grid view,
  • iterate over entities,
  • read geometry.

Next, continue with Grid Traversal, then Grid Hierarchy and Refinement.