General concepts » Meshes » Mesh Input/Output

Saving and loading meshes.

Rodin supports reading and writing meshes in various file formats through the IO module.

Introduction

Mesh I/O operations allow you to:

  • Load meshes from external mesh generators
  • Save computed meshes for later use
  • Exchange meshes with other software
  • Archive simulation data

Supported Formats

The file format is specified explicitly via the IO::FileFormat enumeration:

FormatExtensionDescription
MFEM.meshMFEM native format
MEDIT.meshMEDIT format from INRIA (used by MMG)
HDF5.h5High-performance binary format

Loading Meshes

Basic Loading

Use Mesh::load() with an explicit IO::FileFormat:

#include <Rodin/Geometry.h>

using namespace Rodin;
using namespace Rodin::Geometry;

int main() {
  Mesh mesh;

  // Load MFEM format
  mesh.load("domain.mesh", IO::FileFormat::MFEM);

  // Load MEDIT format
  mesh.load("domain.mesh", IO::FileFormat::MEDIT);

  return 0;
}

Saving Meshes

Basic Saving

Use Mesh::save() with an explicit format:

Mesh mesh;
mesh = mesh.UniformGrid(Polytope::Type::Triangle, {16, 16});

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

// Save in MEDIT format
mesh.save("output.mesh", IO::FileFormat::MEDIT);

MEDIT Format

The MEDIT format is commonly used with the MMG mesh adaptation tools:

mesh.save("domain.mesh", IO::FileFormat::MEDIT);

Complete Example

#include <Rodin/Geometry.h>

using namespace Rodin;
using namespace Rodin::Geometry;

int main() {
  // Generate a mesh
  Mesh mesh;
  mesh = mesh.UniformGrid(Polytope::Type::Triangle, {32, 32});

  std::cout << "Generated mesh with:" << std::endl;
  std::cout << "  Vertices: " << mesh.getVertexCount() << std::endl;
  std::cout << "  Cells: " << mesh.getCellCount() << std::endl;

  // Save in different formats
  mesh.save("output_mfem.mesh", IO::FileFormat::MFEM);
  mesh.save("output_medit.mesh", IO::FileFormat::MEDIT);
  std::cout << "Saved mesh in MFEM and MEDIT formats" << std::endl;

  // Load the mesh back
  Mesh loadedMesh;
  loadedMesh.load("output_mfem.mesh", IO::FileFormat::MFEM);

  std::cout << "Loaded mesh with:" << std::endl;
  std::cout << "  Vertices: " << loadedMesh.getVertexCount() << std::endl;
  std::cout << "  Cells: " << loadedMesh.getCellCount() << std::endl;

  return 0;
}

External Mesh Generators

MMG

MMG is used for mesh adaptation and optimization. Rodin integrates directly with MMG through the MMG module. See the MMG examples for details.

File Paths

Absolute Paths

mesh.load("/home/user/meshes/domain.mesh", IO::FileFormat::MFEM);

Relative Paths

mesh.load("../data/domain.mesh", IO::FileFormat::MFEM);
mesh.save("./output/result.mesh", IO::FileFormat::MFEM);

Format Considerations

Compatibility

  • MFEM: Best for MFEM-based workflows
  • MEDIT: Required for MMG tools

Attributes

All formats preserve:

  • Vertex coordinates
  • Element connectivity
  • Element attributes (region markers)
  • Boundary attributes

See Also