Examples and tutorials » Working with MMG » Adapting the mesh

How to adapt a mesh based on a solution field.

Introduction

Mesh adaptation (or adaptive refinement) modifies the mesh density based on a computed solution. Regions where the solution has sharp gradients or features receive finer elements, while smooth regions are coarsened. This produces more accurate results with fewer degrees of freedom.

The MMG module supports metric-based mesh adaptation where a solution field or metric tensor guides the remesher.

Typical Workflow

A typical solve–adapt cycle looks like:

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

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

int main()
{
  // 1. Start with a coarse mesh
  MMG::Mesh mesh;
  mesh = mesh.UniformGrid(Polytope::Type::Triangle, { 8, 8 });

  for (int adapt = 0; adapt < 3; ++adapt)
  {
    mesh.getConnectivity().compute(1, 2);

    // 2. Solve the PDE on the current 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();

    // 3. Adapt the mesh using MMG with solution-driven sizing
    MMG::Optimizer()
      .setHMax(0.2)
      .setHMin(0.01)
      .optimize(mesh);
  }

  mesh.save("Adapted.mesh", IO::FileFormat::MFEM);
  return 0;
}

See Also