Skip to content

Commit

Permalink
Merge pull request #2161 from lewisgross1296/delete_tally
Browse files Browse the repository at this point in the history
Add extern function to delete a tally by its index in the tallies vector
  • Loading branch information
paulromano authored Aug 18, 2022
2 parents 48fb58a + 5003cf4 commit 183d9c7
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 3 deletions.
7 changes: 7 additions & 0 deletions docs/source/capi/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,13 @@ Functions
:return: Return status (negative if an error occurs)
:rtype: int
.. c:function:: int openmc_remove_tally(int32_t index);
Given an index of a tally, remove it from the tallies array
:param int index: Index in tallies array
:return: Return status (negative if an error occurs)
:rtype: int

.. c:function:: int openmc_run()
Run a simulation
Expand Down
1 change: 1 addition & 0 deletions include/openmc/capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ int openmc_regular_mesh_get_params(
int openmc_regular_mesh_set_dimension(int32_t index, int n, const int* dims);
int openmc_regular_mesh_set_params(int32_t index, int n, const double* ll,
const double* ur, const double* width);
int openmc_remove_tally(int32_t index);
int openmc_reset();
int openmc_reset_timers();
int openmc_run();
Expand Down
6 changes: 4 additions & 2 deletions include/openmc/tallies/tally.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ class Tally {

void set_nuclides(const vector<std::string>& nuclides);

const vector<int32_t>& filters() const { return filters_; }
//! returns vector of indices corresponding to the tally this is called on
const vector<int32_t>& filters() const { return filters_; }

int32_t filters(int i) const { return filters_[i]; }
//! \brief Returns the tally filter at index i
int32_t filters(int i) const { return filters_[i]; }

void set_filters(gsl::span<Filter*> filters);

Expand Down
7 changes: 7 additions & 0 deletions openmc/lib/tally.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
_dll.openmc_tally_set_writable.argtypes = [c_int32, c_bool]
_dll.openmc_tally_set_writable.restype = c_int
_dll.openmc_tally_set_writable.errcheck = _error_handler
_dll.openmc_remove_tally.argtypes = [c_int32]
_dll.openmc_remove_tally.restype = c_int
_dll.openmc_remove_tally.errcheck = _error_handler
_dll.tallies_size.restype = c_size_t


Expand Down Expand Up @@ -405,4 +408,8 @@ def __len__(self):
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[key]._index)

tallies = _TallyMapping()
17 changes: 17 additions & 0 deletions src/tallies/tally.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace openmc {
//==============================================================================

namespace model {
//! a mapping of tally ID to index in the tallies vector
std::unordered_map<int, int> tally_map;
vector<unique_ptr<Tally>> tallies;
vector<int> active_tallies;
Expand Down Expand Up @@ -1271,4 +1272,20 @@ extern "C" size_t tallies_size()
return model::tallies.size();
}

// given a tally ID, remove it from the tallies vector
extern "C" int openmc_remove_tally(int32_t index)
{
// check that id is in the map
if (index < 0 || index > model::tallies.size()) {
return OPENMC_E_OUT_OF_BOUNDS;
}
// grab tally so it's ID can be obtained to remove the (ID,index) pair from tally_map
auto& tally = model::tallies[index];
// delete the tally via iterator pointing to correct position
// this calls the Tally destructor, removing the tally from the map as well
model::tallies.erase(model::tallies.begin() + index);

return 0;
}

} // namespace openmc
14 changes: 14 additions & 0 deletions tests/unit_tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,20 @@ def test_new_tally(lib_init):
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
del openmc.lib.tallies[10]
assert len(openmc.lib.tallies) == 4


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):
t = openmc.lib.tallies[1]
assert not t.active
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_tallies.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ def test_xml_roundtrip(run_in_tmpdir):
assert len(new_tally.triggers) == 1
assert new_tally.triggers[0].trigger_type == tally.triggers[0].trigger_type
assert new_tally.triggers[0].threshold == tally.triggers[0].threshold
assert new_tally.triggers[0].scores == tally.triggers[0].scores
assert new_tally.triggers[0].scores == tally.triggers[0].scores

0 comments on commit 183d9c7

Please sign in to comment.