This is way easier in DOLFINx, as you can work directly with numpy arrays
from mpi4py import MPI
import dolfinx
import basix.ufl
import ufl
import numpy
geometrical_dim = 2
c_el = basix.ufl.element("Lagrange", "triangle", 1, shape=(geometrical_dim,))
ufl_domain = ufl.Mesh(c_el)
if MPI.COMM_WORLD.rank == 0:
nodes = numpy.array(
[
[0.0, 0.0],
[1.0, 0.0],
[0.0, 1.0],
[1.0, 1.0],
[0.0, 2.0],
[1.0, 2.0],
[3.0, 0.0],
[3.0, 1.0],
],
dtype=numpy.float64,
)
cells = numpy.array(
[[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [1, 6, 3], [6, 7, 3]],
dtype=numpy.int64,
)
else:
nodes = numpy.empty((0, geometrical_dim), dtype=numpy.float64)
cells = numpy.empty((0, 3), dtype=numpy.int64)
mesh = dolfinx.mesh.create_mesh(MPI.COMM_WORLD, cells, nodes, ufl_domain)
with dolfinx.io.XDMFFile(mesh.comm, "L_grid.xdmf", "w") as xdmf:
xdmf.write_mesh(mesh)
This is also documented in Mesh generation — FEniCS Workshop