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/ 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:

You can compute these edges by using


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

for cell in marked_cells:
    for e in c_to_e.links(cell):
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

You would need to refine twice

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