mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-28 14:15:42 -04:00
Add type checks on Intersection, Union, Complement (#2685)
This commit is contained in:
parent
1c82dfcc1c
commit
0cf29f1459
2 changed files with 26 additions and 2 deletions
|
|
@ -4,7 +4,6 @@ from copy import deepcopy
|
|||
|
||||
import numpy as np
|
||||
|
||||
from .checkvalue import check_type
|
||||
from .bounding_box import BoundingBox
|
||||
|
||||
|
||||
|
|
@ -362,6 +361,9 @@ class Intersection(Region, MutableSequence):
|
|||
|
||||
def __init__(self, nodes):
|
||||
self._nodes = list(nodes)
|
||||
for node in nodes:
|
||||
if not isinstance(node, Region):
|
||||
raise ValueError('Intersection operands must be of type Region')
|
||||
|
||||
def __and__(self, other):
|
||||
new = Intersection(self)
|
||||
|
|
@ -450,6 +452,9 @@ class Union(Region, MutableSequence):
|
|||
|
||||
def __init__(self, nodes):
|
||||
self._nodes = list(nodes)
|
||||
for node in nodes:
|
||||
if not isinstance(node, Region):
|
||||
raise ValueError('Union operands must be of type Region')
|
||||
|
||||
def __or__(self, other):
|
||||
new = Union(self)
|
||||
|
|
@ -566,7 +571,8 @@ class Complement(Region):
|
|||
|
||||
@node.setter
|
||||
def node(self, node):
|
||||
check_type('node', node, Region)
|
||||
if not isinstance(node, Region):
|
||||
raise ValueError('Complement operand must be of type Region')
|
||||
self._node = node
|
||||
|
||||
@property
|
||||
|
|
|
|||
|
|
@ -222,3 +222,21 @@ def test_translate_inplace():
|
|||
# Translating a region in-place should *not* produce new surfaces
|
||||
region3 = region.translate((0.5, -6.7, 3.9), inplace=True)
|
||||
assert str(region) == str(region3)
|
||||
|
||||
|
||||
def test_invalid_operands():
|
||||
s = openmc.Sphere()
|
||||
z = 3
|
||||
|
||||
# Intersection with invalid operand
|
||||
with pytest.raises(ValueError, match='must be of type Region'):
|
||||
-s & +z
|
||||
|
||||
# Union with invalid operand
|
||||
with pytest.raises(ValueError, match='must be of type Region'):
|
||||
-s | +z
|
||||
|
||||
# Complement with invalid operand
|
||||
with pytest.raises(ValueError, match='must be of type Region'):
|
||||
openmc.Complement(z)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue