fixed a bug in MeshMaterialFilter.from_volumes (#3520)

Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
This commit is contained in:
GuySten 2025-08-08 03:53:33 +03:00 committed by GitHub
parent a64cc96ed5
commit 4fabed542d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 2 deletions

View file

@ -1082,7 +1082,12 @@ class MeshMaterialFilter(MeshFilter):
A new MeshMaterialFilter instance
"""
bins = list(zip(*np.where(volumes._materials > -1)))
# Get flat arrays of material IDs and element indices
mat_ids = volumes._materials[volumes._materials > -1]
elems, _ = np.where(volumes._materials > -1)
# Stack them into a 2D array of (element, material) pairs
bins = np.column_stack((elems, mat_ids))
return cls(mesh, bins)
def __hash__(self):

View file

@ -9,6 +9,7 @@ def test_filter_mesh_material(run_in_tmpdir):
materials = []
for i in range(4):
mat = openmc.Material()
mat.id = 10*(i+1)
mat.add_nuclide('Fe56', 1.0)
materials.append(mat)
@ -35,7 +36,7 @@ def test_filter_mesh_material(run_in_tmpdir):
# MeshMaterialFilter with corresponding bins
vols = mesh.material_volumes(model)
mmf = openmc.MeshMaterialFilter.from_volumes(mesh, vols)
expected_bins = [(0, 1), (1, 1), (1, 2), (2, 2), (2, 3), (3, 3), (3, 4), (4, 4)]
expected_bins = [(0, 10), (1, 10), (1, 20), (2, 20), (2, 30), (3, 40), (3, 30), (4, 40)]
np.testing.assert_equal(mmf.bins, expected_bins)
# Create two tallies, one with a mesh filter and one with mesh-material