From 6ae2001400c19a62c2a9e33a26dc6757818d61ff Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Fri, 21 Feb 2025 14:58:37 -0600 Subject: [PATCH] Enable overlap plotting from Python API (#3310) Co-authored-by: Paul Romano --- openmc/model/model.py | 5 +++++ openmc/plots.py | 13 +++++++++---- tests/unit_tests/test_model.py | 11 +++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/openmc/model/model.py b/openmc/model/model.py index c7f358ce8e..e8558f4ad0 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -863,6 +863,8 @@ class Model: legend: bool = False, axis_units: str = 'cm', outline: bool | str = False, + show_overlaps: bool = False, + overlap_color: Sequence[int] | str | None = None, n_samples: int | None = None, plane_tolerance: float = 1., legend_kwargs: dict | None = None, @@ -941,6 +943,9 @@ class Model: plot.pixels = pixels plot.basis = basis plot.color_by = color_by + plot.show_overlaps = show_overlaps + if overlap_color is not None: + plot.overlap_color = overlap_color if colors is not None: plot.colors = colors self.plots.append(plot) diff --git a/openmc/plots.py b/openmc/plots.py index 2460765984..9c1e31575e 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -209,16 +209,21 @@ _PLOT_PARAMS = """ legend : bool Whether a legend showing material or cell names should be drawn + .. versionadded:: 0.14.0 + axis_units : {'km', 'm', 'cm', 'mm'} + Units used on the plot axis + .. versionadded:: 0.14.0 outline : bool or str Whether outlines between color boundaries should be drawn. If set to 'only', only outlines will be drawn. .. versionadded:: 0.14.0 - axis_units : {'km', 'm', 'cm', 'mm'} - Units used on the plot axis - - .. versionadded:: 0.14.0 + show_overlaps: bool + Indicate whether or not overlapping regions are shown. + Default is False. + overlap_color: Iterable of int or str + Color to apply to overlapping regions. Default is red. n_samples : int, optional The number of source particles to sample and add to plot. Defaults to None which doesn't plot any particles on the plot. diff --git a/tests/unit_tests/test_model.py b/tests/unit_tests/test_model.py index 4d7d5be2bc..ee5d8895ca 100644 --- a/tests/unit_tests/test_model.py +++ b/tests/unit_tests/test_model.py @@ -638,3 +638,14 @@ def test_model_plot(): plot = model.plot(n_samples=1, plane_tolerance=0.1, basis="xy") coords = plot.axes.collections[0].get_offsets().data.flatten() assert (coords == np.array([])).all() + + # modify model to include another cell that overlaps the original cell entirely + model.geometry.root_universe.add_cell(openmc.Cell(region=-surface)) + axes = model.plot(show_overlaps=True) + white = np.array((1.0, 1.0, 1.0)) + red = np.array((1.0, 0.0, 0.0)) + axes_image = axes.get_images()[0] + image_data = axes_image.get_array() + # ensure that all of the data in the image data is either white or red + test_mask = (image_data == white) | (image_data == red) + assert np.all(test_mask), "Colors other than white or red found in overlap plot image"