Skip to content

Commit 6b43a29

Browse files
authored
Add n_elements to the MeshBase protocol and deprecate num_mesh_cells (#3745)
1 parent c5df2bf commit 6b43a29

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

openmc/mesh.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,17 @@ class MeshBase(IDManagerMixin, ABC):
153153
Unique identifier for the mesh
154154
name : str
155155
Name of the mesh
156+
lower_left : Iterable of float
157+
The lower-left coordinates
158+
upper_right : Iterable of float
159+
The upper-right coordinates
156160
bounding_box : openmc.BoundingBox
157161
Axis-aligned bounding box of the mesh as defined by the upper-right and
158162
lower-left coordinates.
159163
indices : Iterable of tuple
160164
An iterable of mesh indices for each mesh element, e.g. [(1, 1, 1), (2, 1, 1), ...]
165+
n_elements : int
166+
Number of elements in the mesh
161167
"""
162168

163169
next_id = 1
@@ -179,6 +185,16 @@ def name(self, name: str):
179185
self._name = name
180186
else:
181187
self._name = ''
188+
189+
@property
190+
@abstractmethod
191+
def lower_left(self):
192+
pass
193+
194+
@property
195+
@abstractmethod
196+
def upper_right(self):
197+
pass
182198

183199
@property
184200
def bounding_box(self) -> openmc.BoundingBox:
@@ -188,6 +204,11 @@ def bounding_box(self) -> openmc.BoundingBox:
188204
@abstractmethod
189205
def indices(self):
190206
pass
207+
208+
@property
209+
@abstractmethod
210+
def n_elements(self):
211+
pass
191212

192213
def __repr__(self):
193214
string = type(self).__name__ + '\n'
@@ -557,10 +578,19 @@ def centroids(self):
557578
s0 = (slice(0, -1),)*ndim + (slice(None),)
558579
s1 = (slice(1, None),)*ndim + (slice(None),)
559580
return (vertices[s0] + vertices[s1]) / 2
581+
582+
@property
583+
def n_elements(self):
584+
return np.prod(self.dimension)
560585

561586
@property
562587
def num_mesh_cells(self):
563-
return np.prod(self.dimension)
588+
warnings.warn(
589+
"The 'num_mesh_cells' attribute is deprecated and will be removed in a future version. "
590+
"Use 'n_elements' instead.",
591+
FutureWarning, stacklevel=2
592+
)
593+
return self.n_elements
564594

565595
def write_data_to_vtk(self,
566596
filename: PathLike,
@@ -822,10 +852,10 @@ def _check_vtk_dataset(self, label: str, dataset: np.ndarray):
822852
"""
823853
cv.check_type('data label', label, str)
824854

825-
if dataset.size != self.num_mesh_cells:
855+
if dataset.size != self.n_elements:
826856
raise ValueError(
827857
f"The size of the dataset '{label}' ({dataset.size}) should be"
828-
f" equal to the number of mesh cells ({self.num_mesh_cells})"
858+
f" equal to the number of mesh cells ({self.n_elements})"
829859
)
830860

831861
# accept a flat array as-is, assuming it is in the correct order

openmc/source.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,10 +572,10 @@ def sources(self, s):
572572
s = np.asarray(s)
573573

574574
if isinstance(self.mesh, StructuredMesh):
575-
if s.size != self.mesh.num_mesh_cells:
575+
if s.size != self.mesh.n_elements:
576576
raise ValueError(
577577
f'The length of the source array ({s.size}) does not match '
578-
f'the number of mesh elements ({self.mesh.num_mesh_cells}).')
578+
f'the number of mesh elements ({self.mesh.n_elements}).')
579579

580580
# If user gave a multidimensional array, flatten in the order
581581
# of the mesh indices

tests/unit_tests/mesh_to_vtk/test_vtk_dims.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ def test_write_data_to_vtk_size_mismatch(mesh):
131131
mesh : openmc.StructuredMesh
132132
The mesh to test
133133
"""
134-
right_size = mesh.num_mesh_cells
134+
right_size = mesh.n_elements
135135
data = np.random.random(right_size + 1)
136136

137137
# Error message has \ in to escape characters that are otherwise recognized
138138
# by regex. These are needed to make the test string match the error message
139139
# string when using the match argument as that uses regular expression
140140
expected_error_msg = (
141141
fr"The size of the dataset 'label' \({len(data)}\) should be equal to "
142-
fr"the number of mesh cells \({mesh.num_mesh_cells}\)"
142+
fr"the number of mesh cells \({mesh.n_elements}\)"
143143
)
144144
with pytest.raises(ValueError, match=expected_error_msg):
145145
mesh.write_data_to_vtk(filename="out.vtk", datasets={"label": data})

0 commit comments

Comments
 (0)