Skip to content

Commit

Permalink
added padding arg to bounding_region (#2701)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
  • Loading branch information
shimwell and paulromano committed Sep 29, 2023
1 parent fd91729 commit eb3eec8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
28 changes: 28 additions & 0 deletions openmc/bounding_box.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from typing import Iterable

import numpy as np
Expand Down Expand Up @@ -87,3 +88,30 @@ def extent(self):
@property
def width(self):
return self.upper_right - self.lower_left

def extend(self, padding_distance: float) -> BoundingBox:
"""Returns an extended bounding box
Parameters
----------
padding_distance : float
The distance to enlarge the bounding box by
Returns
-------
An enlarged bounding box
"""
return BoundingBox(np.array(
[
self[0][0] - padding_distance,
self[0][1] - padding_distance,
self[0][2] - padding_distance
]
), np.array(
[
self[1][0] + padding_distance,
self[1][1] + padding_distance,
self[1][2] + padding_distance
]
))
25 changes: 17 additions & 8 deletions openmc/universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ def bounding_box(self):
coords = dagmc_file['tstt']['nodes']['coordinates'][()]
lower_left_corner = coords.min(axis=0)
upper_right_corner = coords.max(axis=0)
return (lower_left_corner, upper_right_corner)
return openmc.BoundingBox(lower_left_corner, upper_right_corner)

@property
def filename(self):
Expand Down Expand Up @@ -950,7 +950,13 @@ def create_xml_subelement(self, xml_element, memo=None):
dagmc_element.set('filename', str(self.filename))
xml_element.append(dagmc_element)

def bounding_region(self, bounded_type='box', boundary_type='vacuum', starting_id=10000):
def bounding_region(
self,
bounded_type: str = 'box',
boundary_type: str = 'vacuum',
starting_id: int = 10000,
padding_distance: float = 0.
):
"""Creates a either a spherical or box shaped bounding region around
the DAGMC geometry.
Expand All @@ -970,6 +976,10 @@ def bounding_region(self, bounded_type='box', boundary_type='vacuum', starting_i
Starting ID of the surface(s) used in the region. For bounded_type
'box', the next 5 IDs will also be used. Defaults to 10000 to reduce
the chance of an overlap of surface IDs with the DAGMC geometry.
padding_distance : float
Distance between the bounding region surfaces and the minimal
bounding box. Allows for the region to be larger than the DAGMC
geometry.
Returns
-------
Expand All @@ -983,16 +993,15 @@ def bounding_region(self, bounded_type='box', boundary_type='vacuum', starting_i
check_type('bounded type', bounded_type, str)
check_value('bounded type', bounded_type, ('box', 'sphere'))

bbox = self.bounding_box
bbox = self.bounding_box.extend(padding_distance)

if bounded_type == 'sphere':
bbox_center = (bbox[0] + bbox[1])/2
radius = np.linalg.norm(np.asarray(bbox))
radius = np.linalg.norm(bbox.upper_right - bbox.center)
bounding_surface = openmc.Sphere(
surface_id=starting_id,
x0=bbox_center[0],
y0=bbox_center[1],
z0=bbox_center[2],
x0=bbox.center[0],
y0=bbox.center[1],
z0=bbox.center[2],
boundary_type=boundary_type,
r=radius,
)
Expand Down
15 changes: 15 additions & 0 deletions tests/unit_tests/dagmc/test_bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,38 @@ def test_bounding_region(request):
assert isinstance(region, openmc.Region)
assert len(region) == 6
assert region[0].surface.type == "x-plane"
assert region[0].surface.x0 == -25.
assert region[1].surface.type == "x-plane"
assert region[1].surface.x0 == 25.
assert region[2].surface.type == "y-plane"
assert region[2].surface.y0 == -25.
assert region[3].surface.type == "y-plane"
assert region[3].surface.y0 == 25.
assert region[4].surface.type == "z-plane"
assert region[4].surface.z0 == -25.
assert region[5].surface.type == "z-plane"
assert region[5].surface.z0 == 25.
assert region[0].surface.boundary_type == "vacuum"
assert region[1].surface.boundary_type == "vacuum"
assert region[2].surface.boundary_type == "vacuum"
assert region[3].surface.boundary_type == "vacuum"
assert region[4].surface.boundary_type == "vacuum"
assert region[5].surface.boundary_type == "vacuum"
region = u.bounding_region(padding_distance=5)
assert region[0].surface.x0 == -30.
assert region[1].surface.x0 == 30.
assert region[2].surface.y0 == -30.
assert region[3].surface.y0 == 30.
assert region[4].surface.z0 == -30.
assert region[5].surface.z0 == 30.

region = u.bounding_region(bounded_type="sphere", boundary_type="reflective")
assert isinstance(region, openmc.Region)
assert isinstance(region, openmc.Halfspace)
assert region.surface.type == "sphere"
assert region.surface.boundary_type == "reflective"
larger_region = u.bounding_region(bounded_type="sphere", padding_distance=10)
assert larger_region.surface.r > region.surface.r


def test_bounded_universe(request):
Expand Down

0 comments on commit eb3eec8

Please sign in to comment.