From bd95b52f4d711fd3e62ec80fed9a4f1d1778198f Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Fri, 11 Apr 2025 23:35:30 +0200 Subject: [PATCH] Add check for equal value bins in an EnergyFilter (#3372) --- openmc/filter.py | 4 ++-- tests/unit_tests/test_filters.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/openmc/filter.py b/openmc/filter.py index 35a7e7c7e9..755777c5e7 100644 --- a/openmc/filter.py +++ b/openmc/filter.py @@ -1238,7 +1238,7 @@ class RealFilter(Filter): cv.check_type('filter value', v1, Real) # Make sure that each tuple has values that are increasing - if v1 < v0: + if v1 <= v0: raise ValueError(f'Values {v0} and {v1} appear to be out of ' 'order') @@ -1384,7 +1384,7 @@ class EnergyFilter(RealFilter): ---------- values : Iterable of Real A list of values for which each successive pair constitutes a range of - energies in [eV] for a single bin + energies in [eV] for a single bin. Entries must be positive and ascending. filter_id : int Unique identifier for the filter diff --git a/tests/unit_tests/test_filters.py b/tests/unit_tests/test_filters.py index 55bb62075b..c3b7371923 100644 --- a/tests/unit_tests/test_filters.py +++ b/tests/unit_tests/test_filters.py @@ -294,3 +294,21 @@ def test_tabular_from_energyfilter(): tab = efilter.get_tabular(values=np.array([10, 10, 5]), interpolation='linear-linear') assert tab.interpolation == 'linear-linear' + + +def test_energy_filter(): + + # testing that bins descending value raises error + msg = "Values 1.0 and 0.5 appear to be out of order" + with raises(ValueError, match=msg): + openmc.EnergyFilter([0.0, 1.0, 0.5]) + + # testing that bins with same value raises error + msg = "Values 0.25 and 0.25 appear to be out of order" + with raises(ValueError, match=msg): + openmc.EnergyFilter([0.0, 0.25, 0.25]) + + # testing that negative bins values raises error + msg = 'Unable to set "filter value" to "-1.2" since it is less than "0.0"' + with raises(ValueError, match=msg): + openmc.EnergyFilter([-1.2, 0.25, 0.5])