Examples and tutorials » Input/Output examples » MFEM format

How to read and write meshes and grid functions in MFEM format.

Introduction

The MFEM format is a simple text-based format for meshes (.mesh) and grid functions (.gf). It is useful for debugging, archiving, and interoperability with MFEM-based tools.

Use IO::FileFormat::MFEM when saving or loading.

Building a Mesh Programmatically

The Mesh::Builder API lets you construct a mesh from scratch by specifying vertices and polytopes:

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

using namespace Rodin;
using namespace Rodin::Geometry;
using namespace Rodin::Variational;

int main()
{
  // Build a simple two-triangle mesh on the unit square
  Mesh mesh =
    Mesh<Rodin::Context::Local>::Builder()
      .initialize(2)                                         // 2D
      .nodes(4)                                              // 4 vertices
      .vertex({0, 0})
      .vertex({1, 0})
      .vertex({0, 1})
      .vertex({1, 1})
      .polytope(Polytope::Type::Triangle, {0, 1, 2})         // lower-left
      .polytope(Polytope::Type::Triangle, {1, 3, 2})         // upper-right
      .finalize();

  mesh.save("square.mesh", IO::FileFormat::MFEM);

  return 0;
}

Saving a Grid Function

A GridFunction stores DOF values in a finite element space. It can be saved alongside the mesh:

P1 fes(mesh);
GridFunction gf(fes);

// Interpolate a function onto the FE space
gf = RealFunction([](const Geometry::Point& p) {
  return p.x() + p.y();
});

// Save mesh and grid function in MFEM format
mesh.save("output.mesh", IO::FileFormat::MFEM);
gf.save("output.gf", IO::FileFormat::MFEM);

Full Source Code

See Also