Skip to content

Commit 391fb53

Browse files
committed
Some reverting and small fix in filter.py
1 parent 75dcb51 commit 391fb53

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

openmc/filter.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
)
3838

3939

40-
4140
class FilterMeta(ABCMeta):
4241
"""Metaclass for filters that ensures class names are appropriate."""
4342

@@ -1113,12 +1112,14 @@ def from_volumes(cls, mesh: openmc.MeshBase, volumes: openmc.MeshMaterialVolumes
11131112
A new MeshMaterialFilter instance
11141113
11151114
"""
1116-
# Get flat arrays of material IDs and element indices
1117-
mat_ids = volumes._materials[volumes._materials > -1]
1118-
elems, _ = np.where(volumes._materials > -1)
1119-
1120-
# Stack them into a 2D array of (element, material) pairs
1121-
bins = np.column_stack((elems, mat_ids))
1115+
# Build bins using by_element so ordering is deterministic (volume
1116+
# ascending, material ID as tiebreaker) regardless of the ray-traversal
1117+
# order returned by the C library.
1118+
bins = []
1119+
for i in range(volumes.num_elements):
1120+
for mat_id, _ in volumes.by_element(i):
1121+
if mat_id is not None:
1122+
bins.append((i, mat_id))
11221123
return cls(mesh, bins)
11231124

11241125
def __hash__(self):
@@ -1861,7 +1862,7 @@ def __init__(self, particles, energies=None, filter_id=None):
18611862
def __repr__(self):
18621863
string = type(self).__name__ + '\n'
18631864
string += '{: <16}=\t{}\n'.format('\tParticles',
1864-
[str(p) for p in self.particles])
1865+
[str(p) for p in self.particles])
18651866
if self.energies is not None:
18661867
string += '{: <16}=\t{}\n'.format('\tEnergies', self.energies)
18671868
string += '{: <16}=\t{}\n'.format('\tID', self.id)
@@ -2171,7 +2172,7 @@ def paths(self):
21712172
if self._paths is None:
21722173
if not hasattr(self, '_geometry'):
21732174
raise ValueError(
2174-
"Model must be exported before the 'paths' attribute is" \
2175+
"Model must be exported before the 'paths' attribute is"
21752176
"available for a DistribcellFilter.")
21762177

21772178
# Determine paths for cell instances

openmc/mesh.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ def by_element(
160160
else:
161161
results.append((mat_id, vol))
162162

163+
# Sort by volume (ascending) so output is deterministic regardless of
164+
# the ray-traversal order returned by the C library. Ties are broken by
165+
# material ID (ascending). None (void) sorts after all real material IDs.
166+
results.sort(key=lambda t: (
167+
t[0] is None, t[1], t[0] if t[0] is not None else float('inf')))
163168
return results
164169

165170
def save(self, filename: PathLike):

tests/unit_tests/test_mesh.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,10 @@ def test_write_ascii_vtk_unchanged(run_in_tmpdir):
12171217

12181218
# This should work without requiring vtk module changes
12191219
vtkIOLegacy = pytest.importorskip("vtkmodules.vtkIOLegacy")
1220-
mesh.write_data_to_vtk(datasets={"data": ref_data}, filename=filename)
1220+
mesh.write_data_to_vtk(datasets={"data": ref_data},
1221+
filename=filename,
1222+
volume_normalization=False
1223+
)
12211224

12221225
assert Path(filename).exists()
12231226

0 commit comments

Comments
 (0)