XDMF time-evolution output
Exporting time-dependent data with XDMF for visualization in ParaView.
Introduction
This example demonstrates how to use the XDMF class to export time-dependent simulation data. XDMF (eXtensible Data Model and Format) is a widely-used format for scientific visualization that combines lightweight XML metadata with efficient HDF5 binary storage.
The output files can be opened directly in ParaView to animate the time evolution of the field.
Setup
We create a triangular mesh on the unit square and define a vector-valued P1 finite element space with 2 components:
const Real dt = 0.05; const size_t Nt = 40; const size_t Nc = 32; Mesh mesh; mesh = mesh.UniformGrid(Polytope::Type::Triangle, { Nc, Nc }); mesh.scale(1.0 / (Nc - 1)); P1 fes(mesh, 2); // vector-valued, 2 components GridFunction u(fes); u.setName("u"); // name appears in ParaView
Note the call to setName("u") — this name is used as the attribute label in the XDMF file, and will appear as the field name in ParaView.
Setting Up the XDMF Writer
The XDMF object is created with a stem name that determines the output file names. A grid is configured with the mesh and the grid function to export:
IO::XDMF xdmf("TimeEvolution"); auto grid = xdmf.grid(); grid.setMesh(mesh); grid.add(u);
The grid() method returns a handle to the default grid. You can register multiple fields with add() — they will all be exported at each time step.
Time Loop
At each time step, we update the grid function and call write(t) with the current simulation time:
for (size_t k = 0; k < Nt; ++k) { const Real t = k * dt; // Update the vector field at current time u = [t](const Geometry::Point& p) { const Real x = p.x(); const Real y = p.y(); return Math::Vector<Real>{{ std::sin(2.0 * Math::Constants::pi() * (x + t)), std::cos(2.0 * Math::Constants::pi() * (y - t)) }}; }; // Write snapshot at time t xdmf.write(t); }
Each call to write(t) records the current state of all registered grid functions at time . The XDMF writer automatically manages the underlying HDF5 files.
Output Files
Running this example produces:
TimeEvolution.xdmf— XML metadata file describing the temporal collection of gridsTimeEvolution.*.h5— HDF5 files containing mesh geometry, topology, and field data for each snapshot
Viewing in ParaView
To visualize the time evolution:
- Open ParaView
- File → Open and select
TimeEvolution.xdmf - Click Apply in the Properties panel
- Select "u" from the field dropdown (it appears as a vector field)
- Apply Glyph filter to display arrows, or use the magnitude coloring mode
- Use the Play button or timeline slider to animate through time steps
Full Source Code
The complete source code is: