Skip to content

Commit f007c85

Browse files
authored
Check for positive radii (#3813)
1 parent 6d6b051 commit f007c85

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

openmc/surface.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ class SurfaceCoefficient:
3838
value : float or str
3939
Value of the coefficient (float) or the name of the coefficient that
4040
it is equivalent to (str).
41+
positive : bool
42+
Does the surface coefficient must be positive. Defaults to False.
4143
4244
"""
43-
def __init__(self, value):
45+
def __init__(self, value, positive=False):
4446
self.value = value
47+
self.positive = positive
4548

4649
def __get__(self, instance, owner=None):
4750
if instance is None:
@@ -56,6 +59,8 @@ def __set__(self, instance, value):
5659
if isinstance(self.value, Real):
5760
raise AttributeError('This coefficient is read-only')
5861
check_type(f'{self.value} coefficient', value, Real)
62+
if self.positive:
63+
check_greater_than(f'{self.value} coefficient', value, 0.0)
5964
instance._coefficients[self.value] = value
6065

6166

@@ -1261,7 +1266,7 @@ def __subclasshook__(cls, c):
12611266
x0 = SurfaceCoefficient('x0')
12621267
y0 = SurfaceCoefficient('y0')
12631268
z0 = SurfaceCoefficient('z0')
1264-
r = SurfaceCoefficient('r')
1269+
r = SurfaceCoefficient('r', positive=True)
12651270
dx = SurfaceCoefficient('dx')
12661271
dy = SurfaceCoefficient('dy')
12671272
dz = SurfaceCoefficient('dz')
@@ -1427,7 +1432,7 @@ def __init__(self, y0=0., z0=0., r=1., *args, **kwargs):
14271432
x0 = SurfaceCoefficient(0.)
14281433
y0 = SurfaceCoefficient('y0')
14291434
z0 = SurfaceCoefficient('z0')
1430-
r = SurfaceCoefficient('r')
1435+
r = SurfaceCoefficient('r', positive=True)
14311436
dx = SurfaceCoefficient(1.)
14321437
dy = SurfaceCoefficient(0.)
14331438
dz = SurfaceCoefficient(0.)
@@ -1525,7 +1530,7 @@ def __init__(self, x0=0., z0=0., r=1., *args, **kwargs):
15251530
x0 = SurfaceCoefficient('x0')
15261531
y0 = SurfaceCoefficient(0.)
15271532
z0 = SurfaceCoefficient('z0')
1528-
r = SurfaceCoefficient('r')
1533+
r = SurfaceCoefficient('r', positive=True)
15291534
dx = SurfaceCoefficient(0.)
15301535
dy = SurfaceCoefficient(1.)
15311536
dz = SurfaceCoefficient(0.)
@@ -1623,7 +1628,7 @@ def __init__(self, x0=0., y0=0., r=1., *args, **kwargs):
16231628
x0 = SurfaceCoefficient('x0')
16241629
y0 = SurfaceCoefficient('y0')
16251630
z0 = SurfaceCoefficient(0.)
1626-
r = SurfaceCoefficient('r')
1631+
r = SurfaceCoefficient('r', positive=True)
16271632
dx = SurfaceCoefficient(0.)
16281633
dy = SurfaceCoefficient(0.)
16291634
dz = SurfaceCoefficient(1.)
@@ -1723,7 +1728,7 @@ def __init__(self, x0=0., y0=0., z0=0., r=1., *args, **kwargs):
17231728
x0 = SurfaceCoefficient('x0')
17241729
y0 = SurfaceCoefficient('y0')
17251730
z0 = SurfaceCoefficient('z0')
1726-
r = SurfaceCoefficient('r')
1731+
r = SurfaceCoefficient('r', positive=True)
17271732

17281733
def _get_base_coeffs(self):
17291734
x0, y0, z0, r = self.x0, self.y0, self.z0, self.r
@@ -1849,7 +1854,7 @@ def __subclasshook__(cls, c):
18491854
x0 = SurfaceCoefficient('x0')
18501855
y0 = SurfaceCoefficient('y0')
18511856
z0 = SurfaceCoefficient('z0')
1852-
r2 = SurfaceCoefficient('r2')
1857+
r2 = SurfaceCoefficient('r2', positive=True)
18531858
dx = SurfaceCoefficient('dx')
18541859
dy = SurfaceCoefficient('dy')
18551860
dz = SurfaceCoefficient('dz')
@@ -1985,7 +1990,7 @@ def __init__(self, x0=0., y0=0., z0=0., r2=1., *args, **kwargs):
19851990
x0 = SurfaceCoefficient('x0')
19861991
y0 = SurfaceCoefficient('y0')
19871992
z0 = SurfaceCoefficient('z0')
1988-
r2 = SurfaceCoefficient('r2')
1993+
r2 = SurfaceCoefficient('r2', positive=True)
19891994
dx = SurfaceCoefficient(1.)
19901995
dy = SurfaceCoefficient(0.)
19911996
dz = SurfaceCoefficient(0.)
@@ -2087,7 +2092,7 @@ def __init__(self, x0=0., y0=0., z0=0., r2=1., *args, **kwargs):
20872092
x0 = SurfaceCoefficient('x0')
20882093
y0 = SurfaceCoefficient('y0')
20892094
z0 = SurfaceCoefficient('z0')
2090-
r2 = SurfaceCoefficient('r2')
2095+
r2 = SurfaceCoefficient('r2', positive=True)
20912096
dx = SurfaceCoefficient(0.)
20922097
dy = SurfaceCoefficient(1.)
20932098
dz = SurfaceCoefficient(0.)
@@ -2189,7 +2194,7 @@ def __init__(self, x0=0., y0=0., z0=0., r2=1., *args, **kwargs):
21892194
x0 = SurfaceCoefficient('x0')
21902195
y0 = SurfaceCoefficient('y0')
21912196
z0 = SurfaceCoefficient('z0')
2192-
r2 = SurfaceCoefficient('r2')
2197+
r2 = SurfaceCoefficient('r2', positive=True)
21932198
dx = SurfaceCoefficient(0.)
21942199
dy = SurfaceCoefficient(0.)
21952200
dz = SurfaceCoefficient(1.)
@@ -2292,9 +2297,9 @@ def __init__(self, x0=0., y0=0., z0=0., a=0., b=0., c=0., **kwargs):
22922297
x0 = SurfaceCoefficient('x0')
22932298
y0 = SurfaceCoefficient('y0')
22942299
z0 = SurfaceCoefficient('z0')
2295-
a = SurfaceCoefficient('a')
2296-
b = SurfaceCoefficient('b')
2297-
c = SurfaceCoefficient('c')
2300+
a = SurfaceCoefficient('a', positive=True)
2301+
b = SurfaceCoefficient('b', positive=True)
2302+
c = SurfaceCoefficient('c', positive=True)
22982303

22992304
def translate(self, vector, inplace=False):
23002305
surf = self if inplace else self.clone()

tests/unit_tests/test_pin.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ def test_failure(pin_mats, good_radii):
4646
with pytest.raises(ValueError, match="length"):
4747
pin(good_surfaces[:len(pin_mats) - 2], pin_mats)
4848

49-
# Non-positive radii
50-
rad = [openmc.ZCylinder(r=-0.1)] + good_surfaces[1:]
51-
with pytest.raises(ValueError, match="index 0"):
52-
pin(rad, pin_mats)
53-
5449
# Non-increasing radii
5550
surfs = tuple(reversed(good_surfaces))
5651
with pytest.raises(ValueError, match="index 1"):

tests/unit_tests/test_surface.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ def test_cylinder():
195195
assert s.dy == -1
196196
assert s.dz == 1
197197
assert s.r == 2
198+
199+
# Check radius must be positive
200+
with pytest.raises(ValueError):
201+
openmc.Cylinder(x0=x0, y0=y0, z0=z0, dx=dx, dy=dy, dz=dz, r=0.0)
202+
with pytest.raises(ValueError):
203+
openmc.Cylinder(x0=x0, y0=y0, z0=z0, dx=dx, dy=dy, dz=dz, r=-1.0)
198204

199205
# Check bounding box
200206
assert_infinite_bb(s)
@@ -244,6 +250,12 @@ def test_xcylinder():
244250
assert s.y0 == y
245251
assert s.z0 == z
246252
assert s.r == r
253+
254+
# Check radius must be positive
255+
with pytest.raises(ValueError):
256+
openmc.XCylinder(y0=y, z0=z, r=0.0)
257+
with pytest.raises(ValueError):
258+
openmc.XCylinder(y0=y, z0=z, r=-1.0)
247259

248260
# Check bounding box
249261
ll, ur = (+s).bounding_box
@@ -290,6 +302,12 @@ def test_ycylinder():
290302
assert s.x0 == x
291303
assert s.z0 == z
292304
assert s.r == r
305+
306+
# Check radius must be positive
307+
with pytest.raises(ValueError):
308+
openmc.YCylinder(x0=x, z0=z, r=0.0)
309+
with pytest.raises(ValueError):
310+
openmc.YCylinder(x0=x, z0=z, r=-1.0)
293311

294312
# Check bounding box
295313
ll, ur = (+s).bounding_box
@@ -327,6 +345,12 @@ def test_zcylinder():
327345
assert s.x0 == x
328346
assert s.y0 == y
329347
assert s.r == r
348+
349+
# Check radius must be positive
350+
with pytest.raises(ValueError):
351+
openmc.ZCylinder(x0=x, y0=y, r=0.0)
352+
with pytest.raises(ValueError):
353+
openmc.ZCylinder(x0=x, y0=y, r=-1.0)
330354

331355
# Check bounding box
332356
ll, ur = (+s).bounding_box
@@ -365,6 +389,12 @@ def test_sphere():
365389
assert s.y0 == y
366390
assert s.z0 == z
367391
assert s.r == r
392+
393+
# Check radius must be positive
394+
with pytest.raises(ValueError):
395+
openmc.Sphere(x0=x, y0=y, z0=z, r=0.0)
396+
with pytest.raises(ValueError):
397+
openmc.Sphere(x0=x, y0=y, z0=z, r=-1.0)
368398

369399
# Check bounding box
370400
ll, ur = (+s).bounding_box
@@ -404,6 +434,12 @@ def cone_common(apex, r2, cls):
404434
assert s.y0 == y
405435
assert s.z0 == z
406436
assert s.r2 == r2
437+
438+
# Check radius must be positive
439+
with pytest.raises(ValueError):
440+
cls(x0=x, y0=y, z0=z, r2=0.0)
441+
with pytest.raises(ValueError):
442+
cls(x0=x, y0=y, z0=z, r2=-1.0)
407443

408444
# Check bounding box
409445
assert_infinite_bb(s)
@@ -442,6 +478,12 @@ def test_cone():
442478
assert s.dy == -1
443479
assert s.dz == 1
444480
assert s.r2 == 4
481+
482+
# Check radius must be positive
483+
with pytest.raises(ValueError):
484+
openmc.Cone(x0=x0, y0=y0, z0=z0, dx=dx, dy=dy, dz=dz, r2=0.0)
485+
with pytest.raises(ValueError):
486+
openmc.Cone(x0=x0, y0=y0, z0=z0, dx=dx, dy=dy, dz=dz, r2=-1.0)
445487

446488
# Check bounding box
447489
assert_infinite_bb(s)
@@ -622,6 +664,13 @@ def torus_common(center, R, r1, r2, cls):
622664
assert s.a == R
623665
assert s.b == r1
624666
assert s.c == r2
667+
668+
# Check radius must be positive
669+
params = [(0.0, r1, r2), (R, 0.0, r2), (R, r1, 0.0),
670+
(-1.0, r1, r2), (R, -1.0, r2), (R, r1, -1.0)]
671+
for a,b,c in params:
672+
with pytest.raises(ValueError):
673+
cls(x0=x, y0=y, z0=z, a=a, b=b, c=c)
625674

626675
# evaluate method
627676
assert s.evaluate((x, y, z)) > 0.0

0 commit comments

Comments
 (0)