How to refine a mesh in dolfinx?

I want to refine a mesh in a specific area, however I am having some difficulty. My doubts are the following doubts:

  • Is it not possible use meshtags to refine?
  • Is it better to refine in gmsh format or convert first to XDMF and then refine?

Thanks for the help!

See: dolfinx/test_refinement.py at 621d1479adf1e1a8beb9eed86cc8ad3de963f223 · FEniCS/dolfinx · GitHub
which shows how to refine MeshTags of dim mesh.topology.dim and mesh.topology.dim-1

This works for meshes with simplicies (triangle/tetra). For refinement of quad/hexahedral elements I would recommend using Gmsh.

1 Like

Thanks @dokken!
I’ve been analyzing the code and it’s only possible to refine the predefined MeshTag. Is it not possible to refine some mesh cells (selected through a given condition) with the help of MeshTag?

You can mark edges of the cells you want to refine, and pass that into the refine code, see:
https://docs.fenicsproject.org/dolfinx/v0.5.1/python/generated/dolfinx.mesh.html?highlight=refine#dolfinx.mesh.refine

You can compute these edges by using


mesh.topology.create_connectivity(mesh.topology.dim,1)

c_to_e  = mesh.topology.connectivity(mesh.topology.dim,1)

edges=[]
for cell in marked_cells:
    for e in c_to_e.links(cell):
        edges.append(e)
1 Like

Thanks, @dokken! I thought it was possible to refine directly through the MeshTag.

Thanks for the sample code!
I found this function, which probably encodes your code:

edges = locate_entities(mesh, marked_cells, 2 , 1)

One last question: it is not possible to define the degree of refinement, correct? By default in the refinement, the edge is divided into 2 parts. For example, to divide it into 4 parts, I have done through 2 cycles the refinement. Is there a more efficient way?

I think you mean compute_incident_entities
https://docs.fenicsproject.org/dolfinx/v0.5.1/python/generated/dolfinx.mesh.html?highlight=compute_incident_entities#dolfinx.mesh.compute_incident_entities

You would need to refine twice

Yes, I used the compute_incident_entities!
Thanks, @dokken for all your help!