Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions openmc/lib/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,22 +265,40 @@ def property_map(plot):


# Python wrappings for overlap functions
def slice_data_overlap_count():
def slice_data_overlap_count() -> int:
"""Return the number of unique overlaps from the last slice plot.

Returns
-------
int
Number of unique overlapping cell pairs detected by the most recent
:func:`slice_data` call with overlap checking enabled.
"""
count = c_size_t()
_dll.openmc_slice_data_overlap_count(count)
return count.value


def slice_data_overlap_info():
def slice_data_overlap_info() -> np.ndarray:
"""Return identifying information for overlaps from the last slice plot.

Returns
-------
numpy.ndarray
Array of shape ``(n, 3)`` with int32 dtype, where ``n`` is the number
of unique overlaps detected by the most recent :func:`slice_data` call
with overlap checking enabled. Each row contains ``[universe_id,
cell1_id, cell2_id]``.
"""
n = slice_data_overlap_count()
overlap_info = np.empty(n * 3, dtype=np.int32)
overlap_info = np.empty((n, 3), dtype=np.int32)

if n > 0:
_dll.openmc_slice_data_overlap_info(
n,
overlap_info.ctypes.data_as(POINTER(c_int32)),
)
return overlap_info, n
return overlap_info


_dll.openmc_get_plot_index.argtypes = [c_int32, POINTER(c_int32)]
Expand Down
10 changes: 5 additions & 5 deletions src/plot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ void RasterData::set_value(size_t y, size_t x, const Particle& p, int level,

void RasterData::set_overlap(size_t y, size_t x, int overlap_idx)
{
// Set cell, instance, and material to OVERLAP, but preserve filter bin
id_data_(y, x, 0) = OVERLAP;
// Set cell, instance, and material to OVERLAP, but preserve filter bin for
// tally plotting. Cell encodes the overlap index as a negative number so that
// it can be used to look up overlap information in the plotter.
id_data_(y, x, 0) = OVERLAP - overlap_idx - 1;
id_data_(y, x, 1) = OVERLAP;
id_data_(y, x, 2) = OVERLAP - overlap_idx - 1;
// Note: id_data_(y, x, 3) is NOT overwritten - preserves filter bin for tally
// plotting
id_data_(y, x, 2) = OVERLAP;

property_data_(y, x, 0) = OVERLAP;
property_data_(y, x, 1) = OVERLAP;
Expand Down
18 changes: 10 additions & 8 deletions tests/unit_tests/test_slice_data_overlap_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,23 @@ def test_overlaps_enabled(overlap_model):
# Run a single slice with overlap detection enabled and check all
# expected properties in one pass.
geom_data = run_slice()
overlap_info, n = openmc.lib.slice_data_overlap_info()
mat_ids = geom_data[:, :, 2]
overlap_info = openmc.lib.slice_data_overlap_info()
n = overlap_info.shape[0]
cell_ids = geom_data[:, :, 0]

# mat_ids should contain values more negative than _OVERLAP; RasterData
# cell_ids should contain values more negative than _OVERLAP; RasterData
# encodes each unique overlap as OVERLAP - overlap_idx - 1 into slot 2.
assert np.any(mat_ids < _OVERLAP)
assert np.any(cell_ids < _OVERLAP)

# overlap_keys should have 2 entries for the two distinct overlapping
# cylinder pairs in this model.
assert n == 2, f"Expected exactly 2 overlap entries, got {n}"

# Each entry is a (universe_id, cell1_id, cell2_id) triple; verify values.
for i in range(n):
universe_id = int(overlap_info[i * 3])
cell1_id = int(overlap_info[i * 3 + 1])
cell2_id = int(overlap_info[i * 3 + 2])
universe_id = int(overlap_info[i, 0])
cell1_id = int(overlap_info[i, 1])
cell2_id = int(overlap_info[i, 2])
assert universe_id == 1
assert cell1_id in {1, 2, 3}
assert cell2_id in {1, 2, 3}
Expand All @@ -81,7 +82,8 @@ def test_overlaps_disabled(overlap_model):
# With show_overlaps=False, set_overlap is never called and overlap_keys
# is never written to, so the image and map should both be clean.
geom_data = run_slice(show_overlaps=False)
_, n = openmc.lib.slice_data_overlap_info()
overlap_info = openmc.lib.slice_data_overlap_info()
n = overlap_info.shape[0]

assert not np.any(geom_data[:, :, 2] < _OVERLAP)
assert n == 0
Loading