General concepts » Meshes » Mesh Input/Output

Saving and loading meshes.

Rodin supports reading and writing meshes in various file formats.

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

Rodin supports several mesh file formats:

FormatExtensionDescription
MFEM.meshMFEM native format (default)
MEDIT.meshMEDIT format from INRIA
GMSH.msh, .gmshGmsh mesh format

Loading Meshes

Basic Loading

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

using namespace Rodin;
using namespace Rodin::Geometry;

int main() {
  Mesh mesh;

  // Load with default format (MFEM)
  mesh.load("domain.mesh");

  // Explicitly specify format
  mesh.load("domain.mesh", IO::FileFormat::MFEM);
  mesh.load("domain.msh", IO::FileFormat::GMSH);

  return 0;
}

Constructor Loading

// Load during construction
Mesh mesh("domain.mesh", IO::FileFormat::MFEM);

// Mesh is ready to use
std::cout << "Loaded mesh with " << mesh.getCellCount() 
          << " cells" << std::endl;

Saving Meshes

Basic Saving

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

// Save with default format (MFEM)
mesh.save("output.mesh");

// Explicitly specify format
mesh.save("output.mesh", IO::FileFormat::MFEM);
mesh.save("output.msh", IO::FileFormat::GMSH);

MEDIT Format

The MEDIT format is commonly used with MMG and ISCD tools:

#include <Rodin/IO.h>

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

Complete Example

#include <Rodin/Geometry.h>
#include <Rodin/IO.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

Gmsh

Gmsh is a popular open-source mesh generator:

  1. Create geometry in Gmsh
  2. Generate mesh
  3. Export as .msh file
  4. Load in Rodin:
Mesh mesh;
mesh.load("gmsh_output.msh", IO::FileFormat::GMSH);

MMG

MMG is used for mesh adaptation:

// Rodin can work with MMG through RodinExternal::MMG
// See MMG examples for details

File Paths

Absolute Paths

mesh.load("/home/user/meshes/domain.mesh");

Relative Paths

mesh.load("../data/domain.mesh");
mesh.save("./output/result.mesh");

Typical Workflow

Generation and Storage

// Generate mesh
Mesh mesh;
mesh = mesh.UniformGrid(Polytope::Type::Triangle, {64, 64});

// Save for later use
mesh.save("fine_mesh.mesh");

Simulation Workflow

// Load pre-generated mesh
Mesh mesh("fine_mesh.mesh");

// Setup and solve problem
// ...

// Save solution (grid functions saved separately)
// solution.save("solution.gf");

Format Considerations

Compatibility

  • MFEM: Best for MFEM-based workflows
  • MEDIT: Required for MMG tools
  • GMSH: For Gmsh-generated meshes

Attributes

All formats preserve:

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

See Also