Minimum length of element

Hello all,

Here is MWE for calculating the minimum element length, which works smoothly on 0.8.0, but on the latest docker nightly installation of dolfinx 0.9.0.0, it results an error.

# Scaled variable
import dolfinx
from mpi4py import MPI
from dolfinx import mesh, fem, plot, io, default_scalar_type
from petsc4py import PETSc
import numpy as np

def mpi_print(s):
    print(f"Rank {comm.rank}: {s}")

dtype = PETSc.ScalarType  # type: ignore
comm = MPI.COMM_WORLD
rank = comm.rank

# prints dolfinx version
mpi_print(f"DOLFINx version: {dolfinx.__version__}")

domain = mesh.create_box(MPI.COMM_WORLD, [[0.0, 0.0, 0.0], [1, 1, 1]], [3, 2, 1], mesh.CellType.hexahedron)
# Assuming `mesh` is the mesh created in the code
num_elements = domain.topology.index_map(3).size_local
num_nodes = domain.topology.index_map(0).size_local
# Summing up the counts from all processes in MPI_COMM_WORLD
num_elements_global = MPI.COMM_WORLD.allreduce(num_elements, op=MPI.SUM)
num_nodes_global = MPI.COMM_WORLD.allreduce(num_nodes, op=MPI.SUM)

mpi_print(f"Number of elements: {num_elements_global}")
mpi_print(f"Number of nodes: {num_nodes_global}")

# filter and projection parameters
tdim = domain.topology.dim
num_cells = domain.topology.index_map(tdim).size_local + domain.topology.index_map(tdim).num_ghosts
h = domain.h(tdim-2, np.arange(num_cells))
hmax = max(h)
hmin = min(h)
mpi_print(f"Max Element Size: {hmax}")
mpi_print(f"Min Element Size: {hmin}")

Output on Dolfinx 0.8.0

Rank 0: DOLFINx version: 0.8.0
Rank 0: Number of elements: 6
Rank 0: Number of nodes: 24
Rank 0: Max Element Size: 1.0
Rank 0: Min Element Size: 0.3333333333333333

Output on docker nightly Dolfinx 0.9.0.0

Rank 0: DOLFINx version: 0.9.0.0
Rank 0: Number of elements: 6
Rank 0: Number of nodes: 24
Traceback (most recent call last):
  File "/home/versiontest/vertest2.py", line 32, in <module>
    h = domain.h(tdim-2, np.arange(num_cells))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/dolfinx-real/lib/python3.12/dist-packages/dolfinx/mesh.py", line 346, in h
    return _cpp.mesh.h(self._cpp_object, dim, entities)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Entity-to-cell connectivity has not been computed. Missing dims 1->3

Can anyone please let me know the changes I require on the latest version?

Thanks.

1 Like

I’m not sure what happened with the interface between these versions. Try replacing

h = domain.h(tdim-2, np.arange(num_cells))

with a direct use of the C++ function

import dolfinx
domain.topology.create_connectivity(tdim - 2, tdim)
h = dolfinx.cpp.mesh.h(domain._cpp_object, tdim-2, np.arange(num_cells))

It works with the C++ function.
Thank you @nate for quick response.

Simply add domain.topology.create_connectivity(tdim-2, tdim)
Prior to calling domain.h.
What has happened between versions is that dolfinx.mesh.entities_to_geometry was rewritten to support higher order mesh geometries, meaning that if you have a higher order geometry, these nodes are taken into account when computing h dolfinx/cpp/dolfinx/mesh/utils.h at main · FEniCS/dolfinx · GitHub

2 Likes