Skip to content

Commit

Permalink
Merge pull request #4 from paulromano/dagset-hash
Browse files Browse the repository at this point in the history
Add `__hash__` method to DAGSet
  • Loading branch information
pshriwise authored Feb 2, 2024
2 parents 002dfc6 + 2c28814 commit 38aeb4a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 4 additions & 1 deletion dagmc/dagnav.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ def _check_category_and_dimension(self):
raise ValueError(f"{identifier} has no category or geom_dimension tags assigned.")

def __eq__(self, other):
return self.handle == other.handle
return self.model == other.model and self.handle == other.handle

def __hash__(self):
return hash((self.handle, id(self.model)))

def __repr__(self):
return f'{type(self).__name__} {self.id}, {self.num_triangles()} triangles'
Expand Down
30 changes: 29 additions & 1 deletion test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def test_group_merge(request):
new_group = dagmc.Group.create(model, 'mat:fuel')
assert orig_group != new_group


# check that we can update a set ID
assert new_group.id == -1
new_group.id = 100
Expand Down Expand Up @@ -126,6 +125,21 @@ def test_volume(request):
assert v1 not in model.groups['mat:fuel']


def test_hash(request):
test_file = str(request.path.parent / 'fuel_pin.h5m')
model = dagmc.DAGModel(test_file)

s = set(model.volumes)
d = {group: group.name for group in model.groups.values()}

# check that an entry for the same volume with a different model can be entered
# into the dict
model1 = dagmc.DAGModel(test_file)

d.update({group: group.name for group in model1.groups.values()})

assert len(d) == len(model.groups) + len(model1.groups)

def test_compressed_coords(request, capfd):
test_file = str(request.path.parent / 'fuel_pin.h5m')
groups = dagmc.DAGModel(test_file).groups
Expand All @@ -145,6 +159,7 @@ def test_compressed_coords(request, capfd):
assert (conn_map[tris[0]].size == 3)
assert (coords[conn_map[tris[0]]].size == 9)


def test_coords(request, capfd):
test_file = str(request.path.parent / 'fuel_pin.h5m')
model = dagmc.DAGModel(test_file)
Expand Down Expand Up @@ -226,3 +241,16 @@ def test_missing_tags(cls):
handle = model.mb.create_meshset()
with pytest.raises(ValueError):
cls(model, handle)


def test_eq(request):
test_file = str(request.path.parent / 'fuel_pin.h5m')
model1 = dagmc.DAGModel(test_file)
model2 = dagmc.DAGModel(test_file)

model1_v0 = model1.volumes[1]
model2_v0 = model2.volumes[1]

assert model1_v0.handle == model2_v0.handle

assert model1_v0 != model2_v0

0 comments on commit 38aeb4a

Please sign in to comment.