Add tests for regions

This commit is contained in:
Paul Romano 2018-01-25 16:26:40 -06:00
parent b0d35268f3
commit d26165f524
3 changed files with 138 additions and 5 deletions

View file

@ -39,10 +39,8 @@ class Region(object):
def __eq__(self, other):
if not isinstance(other, type(self)):
return False
elif str(self) != str(other):
return False
else:
return True
return str(self) == str(other)
def __ne__(self, other):
return not self == other
@ -463,7 +461,7 @@ class Union(Region, MutableSequence):
if memo is None:
memo = {}
clone = copy.deepcopy(self)
clone = deepcopy(self)
clone[:] = [n.clone(memo) for n in self]
return clone
@ -584,6 +582,6 @@ class Complement(Region):
if memo is None:
memo = {}
clone = copy.deepcopy(self)
clone = deepcopy(self)
clone.node = self.node.clone(memo)
return clone

View file

@ -0,0 +1,134 @@
import numpy as np
import pytest
import openmc
@pytest.fixture
def reset():
openmc.reset_auto_ids()
def assert_unbounded(region):
ll, ur = region.bounding_box
assert ll == pytest.approx((-np.inf, -np.inf, -np.inf))
assert ur == pytest.approx((np.inf, np.inf, np.inf))
def test_union(reset):
s1 = openmc.XPlane(surface_id=1, x0=5)
s2 = openmc.XPlane(surface_id=2, x0=-5)
region = +s1 | -s2
assert isinstance(region, openmc.Union)
# Check bounding box
assert_unbounded(region)
# __contains__
assert (6, 0, 0) in region
assert (-6, 0, 0) in region
assert (0, 0, 0) not in region
# string representation
assert str(region) == '(1 | -2)'
# Combining region with intersection
s3 = openmc.YPlane(surface_id=3)
reg2 = region & +s3
assert (6, 1, 0) in reg2
assert (6, -1, 0) not in reg2
assert str(reg2) == '((1 | -2) 3)'
def test_intersection(reset):
s1 = openmc.XPlane(surface_id=1, x0=5)
s2 = openmc.XPlane(surface_id=2, x0=-5)
region = -s1 & +s2
assert isinstance(region, openmc.Intersection)
# Check bounding box
ll, ur = region.bounding_box
assert ll == pytest.approx((-5, -np.inf, -np.inf))
assert ur == pytest.approx((5, np.inf, np.inf))
# __contains__
assert (6, 0, 0) not in region
assert (-6, 0, 0) not in region
assert (0, 0, 0) in region
# string representation
assert str(region) == '(-1 2)'
# Combining region with union
s3 = openmc.YPlane(surface_id=3)
reg2 = region | +s3
assert (-6, 2, 0) in reg2
assert (-6, -2, 0) not in reg2
assert str(reg2) == '((-1 2) | 3)'
def test_complement(reset):
zcyl = openmc.ZCylinder(surface_id=1, R=1.)
z0 = openmc.ZPlane(surface_id=2, z0=-5.)
z1 = openmc.ZPlane(surface_id=3, z0=5.)
outside = +zcyl | -z0 | +z1
inside = ~outside
outside_equiv = ~(-zcyl & +z0 & -z1)
inside_equiv = ~outside_equiv
# Check bounding box
for region in (inside, inside_equiv):
ll, ur = region.bounding_box
assert ll == pytest.approx((-1., -1., -5.))
assert ur == pytest.approx((1., 1., 5.))
assert_unbounded(outside)
assert_unbounded(outside_equiv)
# string represention
assert str(inside) == '~(1 | -2 | 3)'
# evaluate method
assert (0, 0, 0) in inside
assert (0, 0, 0) not in outside
assert (0, 0, 6) not in inside
assert (0, 0, 6) in outside
def test_get_surfaces():
s1 = openmc.XPlane()
s2 = openmc.YPlane()
s3 = openmc.ZPlane()
region = (+s1 & -s2) | +s3
# Make sure get_surfaces() returns all surfaces
surfs = set(region.get_surfaces().values())
assert not (surfs ^ {s1, s2, s3})
inverse = ~region
surfs = set(inverse.get_surfaces().values())
assert not (surfs ^ {s1, s2, s3})
def test_extend_clone():
s1 = openmc.XPlane()
s2 = openmc.YPlane()
s3 = openmc.ZPlane()
s4 = openmc.ZCylinder()
# extend intersection
r1 = +s1 & -s2
r1 &= +s3 & -s4
assert r1[:] == [+s1, -s2, +s3, -s4]
# extend union
r2 = +s1 | -s2
r2 |= +s3 | -s4
assert r2[:] == [+s1, -s2, +s3, -s4]
# clone methods
r3 = r1.clone()
assert len(r3) == len(r1)
r4 = r2.clone()
assert len(r4) == len(r2)
r5 = ~r1
r6 = r5.clone()

View file

@ -80,6 +80,7 @@ def test_yplane():
# evaluate method
assert s.evaluate((0., 0., 0.)) == pytest.approx(-3.)
def test_zplane():
s = openmc.ZPlane(z0=3.)
assert s.z0 == 3.