General concepts » Input, output, and visualization » Grid function I/O

Saving and loading grid functions in Rodin.

Introduction

Grid functions represent the discrete solution of a finite element problem. Their coefficients are interpreted through a finite element space, its basis and its mesh. Saving and loading grid functions is essential for:

  • Storing computed solutions
  • Post-processing and visualization
  • Saving field data needed by application-defined restart logic
  • Transferring data between different analyses

Saving grid functions

MFEM format

The MFEM format stores grid functions as text files.

  P1 vh(mesh);
  GridFunction u(vh);
  u = RealFunction([](const Point& point) { return point.x() + point.y(); });
  mesh.save("Square.mfem.mesh", IO::FileFormat::MFEM);
  u.save("Square.mfem.gf", IO::FileFormat::MFEM);

XDMF format (recommended)

For visualization, the XDMF format is recommended. It stores both mesh and field data together and can be opened directly in ParaView.

#include <Rodin/IO/XDMF.h>

// After computing the solution...
IO::XDMF xdmf("Output");
xdmf.grid().setMesh(mesh).add("solution", u);
xdmf.write().flush();

Loading grid functions

From MFEM format

Loading a grid function from MFEM requires the mesh and finite element space already set up.

  Mesh loaded;
  loaded.load("Square.mfem.mesh", IO::FileFormat::MFEM);
  P1 loadedSpace(loaded);
  GridFunction restored(loadedSpace);
  restored.load("Square.mfem.gf", IO::FileFormat::MFEM);

The complete round-trip example evaluates the loaded field at cell centroids and compares it with the prescribed affine field.

Format coverage

Grid function I/O coverage depends on the role of the format:

FormatTested grid function role
MFEMP0, P1, and H1 finite element grid functions
MEDITVertex-based solution files (SolAtVertices)
HDF5Raw degree-of-freedom persistence
XDMFEvaluated visualization attributes

MFEM H1 grid functions are tested against fixture files generated by MFEM for degrees K = 1, 2, 3, 4, 5, 6, for scalar and vector data, and for both MFEM vector orderings. P0 scalar grid functions are tested on Point, Segment, Triangle, Quadrilateral, Tetrahedron, Hexahedron, and Wedge meshes. P1 and H1 vector grid functions use the vector finite element spaces provided by Rodin; The listed P0 round-trip tests use scalar fields. Other range and format combinations follow their loader/printer specialization contracts.

Preserve mesh coordinates in high-order round trips

A field is interpreted on its loaded geometry. Text mesh streams need enough digits to retain non-binary coordinates such as $1/7$ . The checked degree-five scalar/vector example writes the mesh through a stream with max_digits10 precision, then rebuilds the required incidence relations before constructing the restored high-order spaces.

  {
    std::ofstream output("H1MFEMGridFunction.mesh");
    output.precision(std::numeric_limits<Real>::max_digits10);
    IO::MeshPrinter<IO::FileFormat::MFEM, Context::Local> printer(mesh);
    printer.print(output);
    if (!output)
      return EXIT_FAILURE;
  }

The example supplies all three vector components, reloads both fields and compares their cell-center values with the prescribed polynomials. Build RodinIOH1MFEMGridFunction; its source is IO/H1MFEMGridFunction.cpp.

Multiple fields

Several fields can be registered in one XDMF file.

IO::XDMF xdmf("MultiField");
auto grid = xdmf.grid();
grid.setMesh(mesh);
grid.add("temperature", temperature);
grid.add("velocity", velocity);
grid.add("pressure", pressure);
xdmf.write().flush();

All fields will be available for selection in ParaView.

Vector-valued fields

Vector-valued grid functions are defined on finite element spaces with multiple components.

// Create a vector P1 space (2 components)
P1 vh(mesh, 2);
GridFunction velocity(vh);
velocity = [](const Geometry::Point& p)
{
  return Math::Vector<Real>{{ -p.y(), p.x() }};
};

IO::XDMF xdmf("VectorField");
xdmf.grid().setMesh(mesh).add("velocity", velocity);
xdmf.write().flush();

See also