Examples and tutorials » Working with MMG » Optimizing the mesh

How to optimize a mesh using the MMG remesher.

Introduction

Mesh quality is critical for accurate finite element solutions. The MMG::Optimizer provides automatic mesh improvement: it refines, coarsens, and improves element shapes according to user-specified size parameters.

Basic Usage

The simplest workflow is to create a mesh, mark its geometric features (corners and ridges), and optimize:

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

using namespace Rodin;
using namespace Rodin::Geometry;

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

  // Mark corner vertices (preserved exactly during remeshing)
  mesh.setCorner(0);
  mesh.setCorner(15);
  mesh.setCorner(240);
  mesh.setCorner(255);

  // Mark all boundary edges as ridges (sharp features)
  for (auto it = mesh.getBoundary(); !it.end(); ++it)
    mesh.setRidge(it->getIndex());

  // Optimize with target edge length ≤ 0.5
  MMG::Optimizer().setHMax(0.5).optimize(mesh);

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

  return 0;
}

Optimizer Parameters

The MMG::Optimizer supports several parameters:

MethodDescription
setHMax(h)Maximum edge length
setHMin(h)Minimum edge length
setHausdorff(h)Hausdorff distance for curved boundaries
setAngleDetection(b)Enable/disable automatic ridge detection

Typical settings for shape optimization:

MMG::Optimizer()
  .setHMax(0.05)
  .setHMin(0.005)
  .setHausdorff(0.0025)
  .setAngleDetection(false)
  .optimize(mesh);

Full Source Code

See Also