From 853ef5ea49862f9c545948b4c8ecf18c3c228c41 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Thu, 9 Feb 2023 19:51:46 +0000 Subject: [PATCH] 2nd round of review improvments from paulromano Co-authored-by: Paul Romano --- openmc/filter.py | 7 +++++-- tests/unit_tests/test_filters.py | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/openmc/filter.py b/openmc/filter.py index d646cc019e..cb1b57ed03 100644 --- a/openmc/filter.py +++ b/openmc/filter.py @@ -1352,12 +1352,15 @@ class EnergyFilter(RealFilter): Tabular distribution with histogram interpolation """ - probabilities = np.array(values) / sum(values) + probabilities = np.array(values, dtype=float) + probabilities /= probabilities.sum() + # Determine probability per eV, adding extra 0 at the end since it is a histogram probability_per_ev = probabilities / np.diff(self.values) + probability_per_ev = np.append(probability_per_ev, 0.0) kwargs.setdefault('interpolation', 'histogram') - return openmc.stats.Tabular(self.bins, probability_per_ev, **kwargs) + return openmc.stats.Tabular(self.values, probability_per_ev, **kwargs) @property def lethargy_bin_width(self): diff --git a/tests/unit_tests/test_filters.py b/tests/unit_tests/test_filters.py index afe85cdde4..d4f0edbf98 100644 --- a/tests/unit_tests/test_filters.py +++ b/tests/unit_tests/test_filters.py @@ -275,12 +275,15 @@ def test_tabular_from_energyfilter(): efilter = openmc.EnergyFilter([0.0, 10.0, 20.0, 25.0]) tab = efilter.get_tabular(values=[5, 10, 10]) - assert tab.x.tolist() == [[0.0, 10.0], [10.0, 20.0], [20.0, 25.0]] + assert tab.x.tolist() == [0.0, 10.0, 20.0, 25.0] # combination of different values passed into get_tabular and different # width energy bins results in a doubling value for each p value assert tab.p.tolist() == [0.02, 0.04, 0.08] + # distribution should integrate to unity + assert tab.integral() == approx(1.0) + # 'histogram' is the default assert tab.interpolation == 'histogram'