Examples and tutorials » Working with MMG » Casting between MMG and Rodin

How to convert from MMG to Rodin objects and vice-versa.

Introduction

The MMG::Mesh class extends Mesh<Context::Local> and can be used wherever a regular mesh is expected. This section explains how to work with both types interchangeably.

Creating an MMG Mesh

An MMG::Mesh is created the same way as a standard mesh:

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

// MMG mesh (supports remeshing operations)
MMG::Mesh mmgMesh;
mmgMesh = mmgMesh.UniformGrid(Polytope::Type::Triangle, { 16, 16 });

Using MMG Meshes with FE Spaces

Since MMG::Mesh inherits from Mesh, finite element spaces and all variational tools work transparently:

MMG::Mesh mesh;
mesh = mesh.UniformGrid(Polytope::Type::Triangle, { 16, 16 });
mesh.getConnectivity().compute(1, 2);

// Create P1 space directly on MMG mesh
P1 vh(mesh);
TrialFunction u(vh);
TestFunction  v(vh);

Problem poisson(u, v);
poisson = Integral(Grad(u), Grad(v))
        - Integral(RealFunction(1.0), v)
        + DirichletBC(u, Zero());
Solver::CG(poisson).solve();

// Now remesh for the next iteration
MMG::Optimizer().setHMax(0.1).optimize(mesh);

Loading and Saving

Both mesh types support the same I/O formats. The format must be specified explicitly via IO::FileFormat:

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

// Save in MEDIT format (native for MMG)
mmgMesh.save("output.medit.mesh", IO::FileFormat::MEDIT);

// Load back
MMG::Mesh loaded;
loaded.load("output.medit.mesh", IO::FileFormat::MEDIT);

See Also