Skip to content

Commit

Permalink
Immediately resolve complement operators for regions (#3145)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulromano authored Oct 10, 2024
1 parent 579777a commit 91fd60b
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 31 deletions.
8 changes: 0 additions & 8 deletions openmc/model/surface_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,12 +778,6 @@ def __init__(self, x0=0., y0=0., z0=0., r2=1., up=True, **kwargs):
def __neg__(self):
return -self.cone & (+self.plane if self.up else -self.plane)

def __pos__(self):
if self.up:
return (+self.cone & +self.plane) | -self.plane
else:
return (+self.cone & -self.plane) | +self.plane


class YConeOneSided(CompositeSurface):
"""One-sided cone parallel the y-axis
Expand Down Expand Up @@ -836,7 +830,6 @@ def __init__(self, x0=0., y0=0., z0=0., r2=1., up=True, **kwargs):
self.up = up

__neg__ = XConeOneSided.__neg__
__pos__ = XConeOneSided.__pos__


class ZConeOneSided(CompositeSurface):
Expand Down Expand Up @@ -890,7 +883,6 @@ def __init__(self, x0=0., y0=0., z0=0., r2=1., up=True, **kwargs):
self.up = up

__neg__ = XConeOneSided.__neg__
__pos__ = XConeOneSided.__pos__


class Polygon(CompositeSurface):
Expand Down
30 changes: 15 additions & 15 deletions openmc/region.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from collections.abc import MutableSequence
from copy import deepcopy
Expand Down Expand Up @@ -30,8 +31,9 @@ def __and__(self, other):
def __or__(self, other):
return Union((self, other))

def __invert__(self):
return Complement(self)
@abstractmethod
def __invert__(self) -> Region:
pass

@abstractmethod
def __contains__(self, point):
Expand Down Expand Up @@ -442,6 +444,9 @@ def __iand__(self, other):
self.append(other)
return self

def __invert__(self) -> Union:
return Union(~n for n in self)

# Implement mutable sequence protocol by delegating to list
def __getitem__(self, key):
return self._nodes[key]
Expand Down Expand Up @@ -530,6 +535,9 @@ def __ior__(self, other):
self.append(other)
return self

def __invert__(self) -> Intersection:
return Intersection(~n for n in self)

# Implement mutable sequence protocol by delegating to list
def __getitem__(self, key):
return self._nodes[key]
Expand Down Expand Up @@ -603,7 +611,7 @@ class Complement(Region):
"""

def __init__(self, node):
def __init__(self, node: Region):
self.node = node

def __contains__(self, point):
Expand All @@ -622,6 +630,9 @@ def __contains__(self, point):
"""
return point not in self.node

def __invert__(self) -> Region:
return self.node

def __str__(self):
return '~' + str(self.node)

Expand All @@ -637,18 +648,7 @@ def node(self, node):

@property
def bounding_box(self) -> BoundingBox:
# Use De Morgan's laws to distribute the complement operator so that it
# only applies to surface half-spaces, thus allowing us to calculate the
# bounding box in the usual recursive manner.
if isinstance(self.node, Union):
temp_region = Intersection(~n for n in self.node)
elif isinstance(self.node, Intersection):
temp_region = Union(~n for n in self.node)
elif isinstance(self.node, Complement):
temp_region = self.node.node
else:
temp_region = ~self.node
return temp_region.bounding_box
return (~self.node).bounding_box

def get_surfaces(self, surfaces=None):
"""Recursively find and return all the surfaces referenced by the node
Expand Down
3 changes: 2 additions & 1 deletion openmc/surface.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from collections.abc import Iterable
from copy import deepcopy
Expand Down Expand Up @@ -2631,7 +2632,7 @@ def __or__(self, other):
else:
return Union((self, other))

def __invert__(self):
def __invert__(self) -> Halfspace:
return -self.surface if self.side == '+' else +self.surface

def __contains__(self, point):
Expand Down
2 changes: 1 addition & 1 deletion tests/regression_tests/filter_mesh/inputs_true.dat
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</materials>
<geometry>
<cell id="1" material="1" region="1 -2 3 -4 10 -9" universe="1"/>
<cell id="2" material="2" region="~(1 -2 3 -4) (5 -6 7 -8) 10 -9" universe="1"/>
<cell id="2" material="2" region="(-1 | 2 | -3 | 4) (5 -6 7 -8) 10 -9" universe="1"/>
<surface coeffs="-5.0" id="1" name="minimum x" type="x-plane"/>
<surface coeffs="5.0" id="2" name="maximum x" type="x-plane"/>
<surface coeffs="-5.0" id="3" name="minimum y" type="y-plane"/>
Expand Down
2 changes: 1 addition & 1 deletion tests/regression_tests/filter_translations/inputs_true.dat
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</materials>
<geometry>
<cell id="1" material="1" region="1 -2 3 -4 10 -9" universe="1"/>
<cell id="2" material="2" region="~(1 -2 3 -4) (5 -6 7 -8) 10 -9" universe="1"/>
<cell id="2" material="2" region="(-1 | 2 | -3 | 4) (5 -6 7 -8) 10 -9" universe="1"/>
<surface coeffs="-5.0" id="1" name="minimum x" type="x-plane"/>
<surface coeffs="5.0" id="2" name="maximum x" type="x-plane"/>
<surface coeffs="-5.0" id="3" name="minimum y" type="y-plane"/>
Expand Down
2 changes: 1 addition & 1 deletion tests/regression_tests/mgxs_library_mesh/inputs_true.dat
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</materials>
<geometry>
<cell id="1" material="1" region="1 -2 3 -4 10 -9" universe="1"/>
<cell id="2" material="2" region="~(1 -2 3 -4) (5 -6 7 -8) 10 -9" universe="1"/>
<cell id="2" material="2" region="(-1 | 2 | -3 | 4) (5 -6 7 -8) 10 -9" universe="1"/>
<surface coeffs="-5.0" id="1" name="minimum x" type="x-plane"/>
<surface coeffs="5.0" id="2" name="maximum x" type="x-plane"/>
<surface coeffs="-5.0" id="3" name="minimum y" type="y-plane"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<geometry>
<cell id="1" material="void" region="-1 2 -3" universe="1"/>
<cell id="2" material="1" region="-1 3 -4" universe="1"/>
<cell id="3" material="void" region="~(-1 2 -4)" universe="1"/>
<cell id="3" material="void" region="1 | -2 | 4" universe="1"/>
<surface boundary="vacuum" coeffs="0.0 0.0 1.0" id="1" type="x-cylinder"/>
<surface boundary="vacuum" coeffs="-1.0" id="2" type="x-plane"/>
<surface coeffs="1.0" id="3" type="x-plane"/>
Expand Down
2 changes: 1 addition & 1 deletion tests/regression_tests/photon_production/inputs_true.dat
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<geometry>
<cell id="1" material="void" region="-1 2 -3" universe="1"/>
<cell id="2" material="1" region="-1 3 -4" universe="1"/>
<cell id="3" material="void" region="~(-1 2 -4)" universe="1"/>
<cell id="3" material="void" region="1 | -2 | 4" universe="1"/>
<surface boundary="vacuum" coeffs="0.0 0.0 1.0" id="1" type="x-cylinder"/>
<surface boundary="vacuum" coeffs="-1.0" id="2" type="x-plane"/>
<surface coeffs="1.0" id="3" type="x-plane"/>
Expand Down
2 changes: 1 addition & 1 deletion tests/regression_tests/score_current/inputs_true.dat
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</materials>
<geometry>
<cell id="1" material="1" region="1 -2 3 -4 10 -9" universe="1"/>
<cell id="2" material="2" region="~(1 -2 3 -4) (5 -6 7 -8) 10 -9" universe="1"/>
<cell id="2" material="2" region="(-1 | 2 | -3 | 4) (5 -6 7 -8) 10 -9" universe="1"/>
<surface coeffs="-5.0" id="1" name="minimum x" type="x-plane"/>
<surface coeffs="5.0" id="2" name="maximum x" type="x-plane"/>
<surface coeffs="-5.0" id="3" name="minimum y" type="y-plane"/>
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_complement(reset):
assert_unbounded(outside_equiv)

# string represention
assert str(inside) == '~(1 | -2 | 3)'
assert str(inside) == '(-1 2 -3)'

# evaluate method
assert (0, 0, 0) in inside
Expand Down

0 comments on commit 91fd60b

Please sign in to comment.