Adaptive remeshing with MMG and XMDF -> MSH conversion

Hi,
I am attempting to perform mesh-adaptation based on a given metric using the MMG platform (https://www.mmgtools.org). There is no support for XDMF file in MMG currently but only .msh or .medit files.

I am therefore attempting to add to a .msh file a NodeData field via a dolfin Function. I naively attempted to convert back my xdmf solution file to msh using meshio-convert but it fails with

  raise ReadError("Unknown section '{}'.".format(c.tag))
meshio._exceptions.ReadError: Unknown section 'Grid'.

Any take on that ? Here is my MWE
plate.geo file:

Point(1) = {-0.5,  0.5, 0, 0.1};
Point(2) = {-0.5, -0.5, 0, 0.1};
Point(3) = { 0.5, -0.5, 0, 0.1};
Point(4) = { 0.5,  0.5, 0, 0.1};

Line(1) = {1, 2};
Line(2) = {2, 3};
Line(3) = {3, 4};
Line(4) = {4, 1};

Curve Loop(1) = {1, 2, 3, 4};

Plane Surface(1) = {1};

Physical Surface(1) = {1};

Python script

from dolfin import *
import meshio
import subprocess

fname = "plate"
subprocess.call(["gmsh", fname+".geo", "-2", "-format", "msh2"])
subprocess.call(["meshio-convert", fname+".msh", fname+".xdmf", "-p", "-z"])

mesh = Mesh()
with XDMFFile(fname+".xdmf") as infile:
    infile.read(mesh)

V = FunctionSpace(mesh, "CG", 1)
metric = Expression("0.01*(sin(10*x[0])*cos(5*x[1])+2)", degree=2)
v = Function(V)
v.interpolate(metric)

with XDMFFile("metric.xdmf") as infile:
    infile.write(v, 0)

subprocess.call(["meshio-convert", "metric.xdmf", "metric.msh"])

Hi Jeremy,

Quickly checking, if I explicitly write the NodeData to xdmf using meshio, I don’t get an error on conversion:

meshio.xdmf.write("metricNew.xdmf",meshio.Mesh(mesh.coordinates(),
                                               cells={"triangle":mesh.cells()},
                                               point_data={"v": v.compute_vertex_values(mesh)}))
nodedata = meshio.xdmf.read("metricNew.xdmf")  # should work with `meshio-convert ...` too
meshio.write("GmshWithMetric.msh", nodedata)

Of course with this simple case the dofs are easier to map using dof_to_vertex_map(V) (and vertex_to_dof_map(V) for the reverse) from the msh file back into the dolfin function, but I don’t know of a more robust solution.

1 Like

Hi Bhavesh,
thanks, this should do the trick for now.