Skip to content

Commit

Permalink
Merge pull request #21 from gonuke/new_id
Browse files Browse the repository at this point in the history
Allow the id.setter to take value of None
  • Loading branch information
pshriwise committed May 2, 2024
2 parents 1141d5d + 71619e3 commit 1783f5e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
23 changes: 13 additions & 10 deletions dagmc/dagnav.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,13 @@ def id(self) -> int:
@id.setter
def id(self, i: int):
"""Set the DAGMC set's ID."""
if i in self.model.used_ids[type(self)]:
if i is None:
i = max(self.model.used_ids[type(self)], default=0) + 1
elif i in self.model.used_ids[type(self)]:
raise ValueError(f'{self.category} ID {i} is already in use in this model.')
else:
self.model.used_ids[type(self)].discard(self.id)
self.model.used_ids[type(self)].add(i)

self.model.used_ids[type(self)].discard(self.id)
self.model.used_ids[type(self)].add(i)

self._tag_set_data(self.model.id_tag, i)

Expand Down Expand Up @@ -367,7 +369,12 @@ def get_triangle_coordinate_mapping(self, compress=False):
return tri_map, coords

def delete(self):
"""Delete this group from the DAGMC file."""
"""Delete this set from the MOAB database, but doesn't
delete this DAGSet object. The object remains but no
longer refers to anything in the model. In many cases, it may
make sense to delete this DAGSet object immediately following
this operation."""
self.model.used_ids[type(self)].discard(self.id)
self.model.mb.delete_entity(self.handle)
self.handle = None
self.model = None
Expand All @@ -381,8 +388,7 @@ def create(cls, model: DAGModel, global_id: Optional[int] = None) -> DAGSet:
ent_set.category = cls._category
# Now that the entity set has proper tags, create derived class and return
out = cls(model, ent_set.handle)
if global_id is not None:
out.id = global_id
out.id = global_id
return out


Expand Down Expand Up @@ -678,9 +684,6 @@ def create(cls, model: DAGModel, name: Optional[str] = None, group_id: Optional[
# Now that entity set has proper tags, create Group, assign name, and return
group = cls(model, ent_set.handle)

if group_id is None:
group_id = max((grp.id for grp in model.groups), default=0) + 1

group.id = group_id

if name is not None:
Expand Down
17 changes: 17 additions & 0 deletions test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ def test_id_safety(request):
v1.id = safe_vol_id
assert v1.id == safe_vol_id

v2 = Volume.create(model)
assert v2.id == safe_vol_id + 1

safe_vol_id = 101
v1.id = safe_vol_id
del v2

v3 = Volume.create(model)
assert v3.id == safe_vol_id + 1

s1 = model.surfaces_by_id[1]

used_surf_id = 2
Expand All @@ -195,6 +205,13 @@ def test_id_safety(request):
s1.id = safe_surf_id
assert s1.id == safe_surf_id

s2 = model.surfaces_by_id[2]
s2.id = None
assert s2.id == safe_surf_id + 1

s2.id = 2
assert s2.id == 2

g1 = model.groups_by_name['mat:fuel']

used_grp_id = 2
Expand Down

0 comments on commit 1783f5e

Please sign in to comment.