openmc icon indicating copy to clipboard operation
openmc copied to clipboard

Add len() support to Meshes

Open MicahGale opened this issue 4 months ago • 1 comments

Description

Sometimes it is helpful to know how many elements are in a Mesh. Right now there is no easy way to do this.

I proposed implementing __len__ for all Mesh classes (e.g., SphericalMesh). This is the function called by len().

Alternatives

The current workaround that I have seen is:

mesh = openmc.SphericalMesh(...)
mesh_size = len(list(mesh.indices))

Compatibility

This would not change any existing behavior. There is the question of if this should return the number of elements, or the number of "edges".

MicahGale avatar Aug 26 '25 19:08 MicahGale

UnstructuredMesh has an n_elements property and the StructuredMesh class has num_mesh_cells, which is kind of silly. I think it would be good to provide this via an attribute of the same name for consistency.

I'll advise against an implementation of len for these objects as they aren't really containers (not able to be indexed or iterated).

Thanks for bringing this up!

pshriwise avatar Aug 26 '25 19:08 pshriwise