Minimal distance between nodes for each element

Hi!

I would like to be able to use the minimum distance between nodes for each element in a variational form in dolfinx 0.9. To do this I have created a function he on a DG-0 space and made the following code:

num_cells_local = mesh.topology.index_map(mesh.topology.dim).size_local
cell_to_vertices = mesh.topology.connectivity(dim, 0)

for cell_idx in range(num_cells_local):
    vertices = cell_to_vertices.links(cell_idx)
    points = mesh.geometry.x[vertices]
    dists = pdist(points) #scipy.spatial.distance from scipy
    min_dist = np.min(dists) if len(dists) > 0 else 0.0
    he.x.array[cell_idx] = min_dist

he.x.scatter_forward()

The code works fine in sequential mode but in parallel I get incorrect values around the mpi boundaries (see image).

I tried using num_cells = mesh.topology.index_map(mesh.topology.dim).size_local + mesh.topology.index_map(mesh.topology.dim).num_ghosts but without success (it changes nothing).

Many thanks in advance!

You are assuming a 1-1 relationship between the mesh topology and mesh-geometry.
You should use dolfinx.mesh.entities_to_geometry(mesh, mesh.topology.dim, np.arange(num_cells_local,dtype=np.int32))
to get the correct indices to look up in mesh.geometry.x. See the documentation:

See for instance: