Mesh.create_mesh runtime error: A facet is connected to more than two cells

I tried to use dolfinx.mesh.create_mesh to copy a mesh (from mesh0 to mesh1). Consider the following in v0.8.0:

from mpi4py import MPI
from dolfinx import mesh

mesh0 = mesh.create_unit_square(comm, 4, 4)
mesh1 = mesh.create_mesh(comm, mesh0.geometry.dofmap, mesh0.geometry.x, mesh0.ufl_domain())

Run with multiple processes and runtime error occured: A facet is connected to more than two cells. I guess it is due to ghosts included?

To re-create a mesh in parallel, you would need to map the geometry to its global indices (and probably send in a custom partitioner keeping the mesh partitioning the same).

Looks complicated, and any advice on the implementation?

In my case, I have a original mesh mesh0. In every time step, I need to copy mesh0 to mesh_current where the nodes are moved and functionspaces are built. I usually use save & load method, but it is expensive when mesh is large:

# Pseudo code

# Save, with `mesh0` is ready
io.XDMFFile(MPI.COMM_WORLD, filename, "w").write_mesh(mesh0)

while t < t_end:
        # Load
        mesh_current = io.XDMFFile(MPI.COMM_WORLD, filename, "r").read_mesh()

        # Move mesh
        mesh_current.geometry.x[:, :tdim] += disp.x.array.reshape((-1, tdim))

        V = fem.functionspace(mesh_current, ("Lagrange", 1, (tdim,)))

Why is it that you need to copy the mesh at all?

If all you need to have is the ability to go back to the original mesh after deformation, the only thing you need to save is the coordinates of its vertices with mesh.geometry.x.copy() before you deform it.