From 04bee9c49fc95836cd0324992d69deed72aa1e09 Mon Sep 17 00:00:00 2001 From: GuySten <62616591+GuySten@users.noreply.github.com> Date: Fri, 6 Feb 2026 20:44:16 +0200 Subject: [PATCH] Check that Surface IDs are at least 1 (#3772) Co-authored-by: Paul Romano --- openmc/mixin.py | 4 +++- openmc/surface.py | 1 + tests/unit_tests/test_cell.py | 6 ++++++ tests/unit_tests/test_material.py | 7 +++++++ tests/unit_tests/test_surface.py | 6 ++++++ tests/unit_tests/test_universe.py | 6 ++++++ 6 files changed, 29 insertions(+), 1 deletion(-) diff --git a/openmc/mixin.py b/openmc/mixin.py index 0bc4128b0..28c97bfdd 100644 --- a/openmc/mixin.py +++ b/openmc/mixin.py @@ -39,6 +39,8 @@ class IDManagerMixin: """ + min_id = 0 + @property def id(self): return self._id @@ -64,7 +66,7 @@ class IDManagerMixin: else: name = cls.__name__ cv.check_type(f'{name} ID', uid, Integral) - cv.check_greater_than(f'{name} ID', uid, 0, equality=True) + cv.check_greater_than(f'{name} ID', uid, cls.min_id, equality=True) if uid in cls.used_ids: msg = f'Another {name} instance already exists with id={uid}.' warn(msg, IDWarning) diff --git a/openmc/surface.py b/openmc/surface.py index 1fe5fabdf..97aca43b3 100644 --- a/openmc/surface.py +++ b/openmc/surface.py @@ -151,6 +151,7 @@ class Surface(IDManagerMixin, ABC): """ + min_id = 1 next_id = 1 used_ids = set() _atol = 1.e-12 diff --git a/tests/unit_tests/test_cell.py b/tests/unit_tests/test_cell.py index 60b205818..2baa59f1b 100644 --- a/tests/unit_tests/test_cell.py +++ b/tests/unit_tests/test_cell.py @@ -20,6 +20,12 @@ def test_contains(): assert (10.0, -4., 2.0) in c +def test_id(): + openmc.Cell(cell_id=0) + with pytest.raises(ValueError): + openmc.Cell(cell_id=-1) + + def test_repr(cell_with_lattice): cells, mats, univ, lattice = cell_with_lattice repr(cells[0]) # cell with distributed materials diff --git a/tests/unit_tests/test_material.py b/tests/unit_tests/test_material.py index 764c98d41..0b1b5fce2 100644 --- a/tests/unit_tests/test_material.py +++ b/tests/unit_tests/test_material.py @@ -77,6 +77,13 @@ def test_add_components(): with pytest.raises(ValueError): m.add_components({'H1': 1.0}, percent_type = 'oa') + +def test_id(): + openmc.Material(material_id=0) + with pytest.raises(ValueError): + openmc.Material(material_id=-1) + + def test_nuclides_to_ignore(run_in_tmpdir): """Test nuclides_to_ignore when exporting a material to XML""" m = openmc.Material() diff --git a/tests/unit_tests/test_surface.py b/tests/unit_tests/test_surface.py index e9560223d..aaf591e05 100644 --- a/tests/unit_tests/test_surface.py +++ b/tests/unit_tests/test_surface.py @@ -7,6 +7,12 @@ import openmc import pytest +def test_id(): + for i in range(-10, 1): + with pytest.raises(ValueError): + openmc.Plane(a=1, b=2, c=-1, d=3, surface_id=i) + + def assert_infinite_bb(s): ll, ur = (-s).bounding_box assert np.all(np.isinf(ll)) diff --git a/tests/unit_tests/test_universe.py b/tests/unit_tests/test_universe.py index 6fbf1cc38..0955347f7 100644 --- a/tests/unit_tests/test_universe.py +++ b/tests/unit_tests/test_universe.py @@ -49,6 +49,12 @@ def test_bounding_box(): assert_unbounded(u) +def test_id(): + openmc.Universe(universe_id=0) + with pytest.raises(ValueError): + openmc.Universe(universe_id=-1) + + def test_plot(run_in_tmpdir, sphere_model): # model with -inf and inf in the bounding box