mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-28 22:26:08 -04:00
added deletion operator to TallyMapping class. used in tests and changed delete invalid id to access invalid id
This commit is contained in:
parent
9d82e412da
commit
d1775181e9
2 changed files with 23 additions and 12 deletions
|
|
@ -390,13 +390,7 @@ class Tally(_FortranObjectWithID):
|
|||
|
||||
class _TallyMapping(Mapping):
|
||||
def __getitem__(self, key):
|
||||
index = c_int32()
|
||||
try:
|
||||
_dll.openmc_get_tally_index(key, index)
|
||||
except (AllocationError, InvalidIDError) as e:
|
||||
# __contains__ expects a KeyError to work correctly
|
||||
raise KeyError(str(e))
|
||||
return Tally(index=index.value)
|
||||
return Tally(index=self._get_tally_index(key))
|
||||
|
||||
def __iter__(self):
|
||||
for i in range(len(self)):
|
||||
|
|
@ -408,4 +402,18 @@ class _TallyMapping(Mapping):
|
|||
def __repr__(self):
|
||||
return repr(dict(self))
|
||||
|
||||
def __delitem__(self,key):
|
||||
"""Delete a tally from tally vector and remove the ID,index pair from tally"""
|
||||
_dll.openmc_remove_tally(self._get_tally_index(key))
|
||||
|
||||
def _get_tally_index(self,key):
|
||||
"""Given the ID, return the index"""
|
||||
index = c_int32()
|
||||
try:
|
||||
_dll.openmc_get_tally_index(key, index)
|
||||
except (AllocationError, InvalidIDError) as e:
|
||||
# __contains__ expects a KeyError to work correctly
|
||||
raise KeyError(str(e))
|
||||
return index.value
|
||||
|
||||
tallies = _TallyMapping()
|
||||
|
|
|
|||
|
|
@ -339,16 +339,19 @@ def test_new_tally(lib_init):
|
|||
new_tally_with_id.scores = ['flux']
|
||||
assert len(openmc.lib.tallies) == 5
|
||||
|
||||
|
||||
def test_delete_tally(lib_init):
|
||||
# delete tally 10 which was added in the above test
|
||||
# check length is one less than before
|
||||
openmc.lib.openmc_remove_tally(openmc.lib.get_tally_index(10))
|
||||
del openmc.lib.tallies[10]
|
||||
assert len(openmc.lib.tallies) == 4
|
||||
|
||||
def test_delete_invalid_id(lib_init):
|
||||
# attempt to delete a tally that is guaranteed not to have a valid index
|
||||
with pytest.raises(exc.InvalidIDError):
|
||||
openmc.lib.openmc_remove_tally(np.max(id)+1)
|
||||
|
||||
def test_invalid_tally_id(lib_init):
|
||||
# attempt to access a tally that is guaranteed not to have a valid index
|
||||
max_id = max(openmc.lib.tallies.keys())
|
||||
with pytest.raises(KeyError):
|
||||
openmc.lib.tallies[max_id+1]
|
||||
|
||||
|
||||
def test_tally_activate(lib_simulation_init):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue