How to refine a mesh (through the script) imported from GMSH?

Hi there!

Suppose I create a mesh and import it to FEniCS through my script. Is there any way I can further refine the imported mesh through my Python script? For example, suppose I import the mesh example.msh (don’t worry about the geometry or the FEM problem that follows) through the following script:

import meshio
msh = meshio.read("example.msh")
triangle_mesh = create_mesh(msh, "triangle", True)
line_mesh = create_mesh(msh, "line", True)
meshio.write("mesh.xdmf", triangle_mesh)
meshio.write("mf.xdmf", line_mesh) 
from dolfin import *
mesh = Mesh()
mvc = MeshValueCollection("size_t", mesh, mesh.topology().dim())
with XDMFFile("mesh.xdmf") as infile:
   infile.read(mesh)
   infile.read(mvc, "name_to_read")
cf = cpp.mesh.MeshFunctionSizet(mesh, mvc)

mvc = MeshValueCollection("size_t", mesh, mesh.topology().dim()-1)
with XDMFFile("mf.xdmf") as infile:
    infile.read(mvc, "name_to_read")
mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)
ds_custom = Measure("ds", domain=mesh, subdomain_data=mf)

Now I want to further refine the imported mesh through my script. Any idea on how to do that?

You can use the refine and adapt commands, as used in: