Check that Surface IDs are at least 1 (#3772)

Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
This commit is contained in:
GuySten 2026-02-06 20:44:16 +02:00 committed by GitHub
parent a632429267
commit 04bee9c49f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 29 additions and 1 deletions

View file

@ -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)

View file

@ -151,6 +151,7 @@ class Surface(IDManagerMixin, ABC):
"""
min_id = 1
next_id = 1
used_ids = set()
_atol = 1.e-12

View file

@ -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

View file

@ -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()

View file

@ -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))

View file

@ -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