Mesh refinement with meshtags

Refining a mesh with dolfinx.mesh.refine_plaza and passing edges crashes. I traced it to mesh.py not passing option to _cpp.refinement.refine_plaza. However, even if I add option to the call, I cannot get it working correctly.

dim = mesh.topology.dim
edges = dolfinx.mesh.compute_incident_entities(mesh.topology, cell_markers, dim, 1)
fine_mesh, parent_cell, parent_facet = dolfinx.mesh.refine_plaza(mesh, edges, False, dolfinx.mesh.RefinementOption.parent_cell_and_facet)
fine_mesh.topology.create_connectivity(dim - 1, dim)
fine_cell_tags = dolfinx.mesh.transfer_meshtag(cell_tags, fine_mesh, parent_cell)
fine_facet_tags = dolfinx.mesh.transfer_meshtag(facet_tags, fine_mesh, parent_cell, parent_facet)

The result is a mesh that is finer, but when saving with VTXWriter and inspecting with ParaView (culling the front faces to get a view of the interior), I get a garbled up mesh:

Am I missing something? Is the implementation of refine_plaza really missing the option in its call to _cpp.refinement.refine_plaza?

Update: the issue does not seem to be related to refine_plaza or the meshtags, but to refinement at all. Here’s a minimal example:

from mpi4py import MPI
import dolfinx

mesh = dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD, 5, 7, 9, ghost_mode=GhostMode.none)
mesh.topology.create_entities(1)
fine_mesh = dolfinx.mesh.refine(mesh, redistribute=False)

with dolfinx.io.VTXWriter(mesh.comm, "mesh.bp", mesh) as vtx:
    vtx.write(0)

with dolfinx.io.VTXWriter(fine_mesh.comm, "fine_mesh.bp", fine_mesh) as vtx:
    vtx.write(0)

Culling the front faces on mesh looks like this:

Culling the front faces on fine_mesh looks like this:

Could you specify how you are culling the data in Paraview?
It is not clear to me how this is done, as there are may ways of culling data in Paraview: 4. Visualizing Large Models — ParaView Documentation 5.11.0 documentation

Right, sorry for not mentioning it! With “advanced properties” toggled, there is an option “Backface Styling” > “Backface representation”, where I select “Cull Frontface”

Im assuming this relates to the fact that Paraview assumes a certain ordering of a tetrahedra (they want the first three points to be ordered counter clockwise), that DOLFINx doesn’t take into account.
This means that the CellSize filter for volume in Paraview becomes negative, probably affecting the culling function.

@mscroggs do you a nice way of going from the DOLFINx ordering, that does care about counter clockwise or clockwise orientation of the bottom triangle, to a consistent counter clockwise ordering?

Hello, dokken. Are there some examples about the usage of meshtags?For example, what does the function transfer_meshtag do, and when should we use it, when we need a meshtag. Thanks.

A MeshTag is used to associate a (sub)set of entities (vertex, edge, facet or cell) of a mesh with some values.
It is for instance used when you define integration measures such as ufl.dx or ufl.ds, if you supply the meshtag as a subdomain_data keyword argument. You can then restrict integration to all cells or facets marked with a given value (say 2 as an example) by calling

dx = ufl.Measure("dx", domain=mesh, subdomain_data=mesh_tag)
sub_integral = 1*dx(2)

This would only integrate over cells marked with the value 2.

Transfer meshtags is used to transfer information about values associated with a (coarse) grid, to its refined version. This means that if a cell marked with 1 is subdivided into two new cells, these will be marked in the new meshtag with the same value.