Skip to content

Commit ab4fba8

Browse files
authored
Merge pull request #53 from paulromano/boundary-property
Add boundary property on Surface
2 parents 1af3754 + 0775624 commit ab4fba8

3 files changed

Lines changed: 63 additions & 11 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ classifiers = [
3131
"Topic :: Scientific/Engineering",
3232
"Programming Language :: Python :: 3",
3333
]
34-
requires-python = ">=3.8"
34+
requires-python = ">=3.9"
3535
# TODO: add PyMOAB once on PyPI
3636
dependencies = ["numpy"]
3737

@@ -49,4 +49,4 @@ ci = ["pytest-cov"]
4949
[tool.setuptools_scm]
5050

5151
[tool.setuptools]
52-
package-dir = {"" = "src"}
52+
package-dir = {"" = "src"}

src/pydagmc/dagnav.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def volumes_by_material(self) -> Dict[str, list[Volume]]:
8686
A dictionary where keys are material names (str) and values
8787
are lists of Volume objects.
8888
"""
89-
material_map: DefaultDict[str, list[Volume]] = defaultdict(list)
89+
material_map: defaultdict[str, list[Volume]] = defaultdict(list)
9090
for volume in self.volumes:
9191
if volume.material is None:
9292
continue
@@ -347,7 +347,7 @@ def _check_category_and_dimension(self):
347347
raise ValueError(f"{identifier} has no category or geom_dimension tags assigned.")
348348

349349
def __eq__(self, other):
350-
return type(other) == type(self) and \
350+
return type(other) is type(self) and \
351351
self.model == other.model and \
352352
self.handle == other.handle
353353

@@ -403,6 +403,11 @@ def category(self, category: str):
403403
"""Set the DAGMC set's category."""
404404
self._tag_set_data(self.model.category_tag, category)
405405

406+
@property
407+
def groups(self) -> list[Group]:
408+
"""Get list of groups containing this DAGMC set."""
409+
return [group for group in self.model.groups if self in group]
410+
406411
@abstractmethod
407412
def _get_triangle_sets(self):
408413
"""Retrieve all (surface) sets under this set that contain triangle elements.
@@ -584,6 +589,34 @@ def reverse_volume(self) -> Optional[Volume]:
584589
def reverse_volume(self, volume: Volume):
585590
self.senses = [self.forward_volume, volume]
586591

592+
@property
593+
def _boundary_group(self) -> Optional[Group]:
594+
for group in self.groups:
595+
if "boundary:" in group.name:
596+
return group
597+
return None
598+
599+
@property
600+
def boundary(self) -> Optional[str]:
601+
"""Name of the boundary assigned to this surface."""
602+
group = self._boundary_group
603+
if group is not None:
604+
return group.name.removeprefix('boundary:')
605+
return None
606+
607+
@boundary.setter
608+
def boundary(self, name: Optional[str]):
609+
group = self._boundary_group
610+
611+
if group is not None:
612+
# Remove surface from existing group
613+
group.remove_set(self)
614+
615+
# create a new group or get an existing group
616+
if name is not None:
617+
group = Group.create(self.model, name=f"boundary:{name}")
618+
group.add_set(self)
619+
587620
@property
588621
def volumes(self) -> list[Volume]:
589622
"""Get the parent volumes of this surface.
@@ -619,12 +652,7 @@ def __init__(self, model: Model, handle: np.uint64):
619652
self._check_category_and_dimension()
620653

621654
@property
622-
def groups(self) -> list[Group]:
623-
"""Get list of groups containing this volume."""
624-
return [group for group in self.model.groups if self in group]
625-
626-
@property
627-
def _material_group(self):
655+
def _material_group(self) -> Optional[Group]:
628656
for group in self.groups:
629657
if "mat:" in group.name:
630658
return group

test/test_dagnav.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ def test_surface_sense_value_error_on_wrong_length():
917917

918918
# Create dummy volumes for valid input
919919
vol1 = model.create_volume(global_id=1)
920-
920+
921921
# Empty list
922922
with pytest.raises(ValueError, match="Senses should be a list of two volumes."):
923923
surf.senses = []
@@ -948,6 +948,30 @@ def test_surface_create_invalid_filename():
948948
model.create_surface(filename='my_model.step')
949949

950950

951+
def test_surface_boundary():
952+
"""Test the boundary property of Surface."""
953+
model = pydagmc.Model()
954+
surf = model.create_surface(global_id=1)
955+
956+
# Initially, boundary should be None
957+
assert surf.boundary is None
958+
959+
# Set a valid boundary condition
960+
surf.boundary = 'Reflecting'
961+
assert surf.boundary == 'Reflecting'
962+
assert [1] == sorted(model.groups_by_name['boundary:Reflecting'].surface_ids)
963+
964+
# Change the boundary condition
965+
surf.boundary = 'Vacuum'
966+
assert surf.boundary == 'Vacuum'
967+
assert [1] == sorted(model.groups_by_name['boundary:Vacuum'].surface_ids)
968+
assert [] == sorted(model.groups_by_name['boundary:Reflecting'].surface_ids)
969+
970+
# Remove the boundary condition by setting it to None
971+
surf.boundary = None
972+
assert surf.boundary is None
973+
974+
951975
def test_geometryset_category_runtime_error(request):
952976
"""Test category returns None when tag is missing."""
953977
model = pydagmc.Model()

0 commit comments

Comments
 (0)