Skip to content

Commit 183d9c7

Browse files
authored
Merge pull request #2161 from lewisgross1296/delete_tally
Add extern function to delete a tally by its index in the tallies vector
2 parents 48fb58a + 5003cf4 commit 183d9c7

File tree

7 files changed

+51
-3
lines changed

7 files changed

+51
-3
lines changed

docs/source/capi/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,13 @@ Functions
460460
:return: Return status (negative if an error occurs)
461461
:rtype: int
462462
463+
.. c:function:: int openmc_remove_tally(int32_t index);
464+
465+
Given an index of a tally, remove it from the tallies array
466+
:param int index: Index in tallies array
467+
:return: Return status (negative if an error occurs)
468+
:rtype: int
469+
463470
.. c:function:: int openmc_run()
464471
465472
Run a simulation

include/openmc/capi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ int openmc_regular_mesh_get_params(
118118
int openmc_regular_mesh_set_dimension(int32_t index, int n, const int* dims);
119119
int openmc_regular_mesh_set_params(int32_t index, int n, const double* ll,
120120
const double* ur, const double* width);
121+
int openmc_remove_tally(int32_t index);
121122
int openmc_reset();
122123
int openmc_reset_timers();
123124
int openmc_run();

include/openmc/tallies/tally.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ class Tally {
4747

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

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

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

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

openmc/lib/tally.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@
8787
_dll.openmc_tally_set_writable.argtypes = [c_int32, c_bool]
8888
_dll.openmc_tally_set_writable.restype = c_int
8989
_dll.openmc_tally_set_writable.errcheck = _error_handler
90+
_dll.openmc_remove_tally.argtypes = [c_int32]
91+
_dll.openmc_remove_tally.restype = c_int
92+
_dll.openmc_remove_tally.errcheck = _error_handler
9093
_dll.tallies_size.restype = c_size_t
9194

9295

@@ -405,4 +408,8 @@ def __len__(self):
405408
def __repr__(self):
406409
return repr(dict(self))
407410

411+
def __delitem__(self, key):
412+
"""Delete a tally from tally vector and remove the ID,index pair from tally"""
413+
_dll.openmc_remove_tally(self[key]._index)
414+
408415
tallies = _TallyMapping()

src/tallies/tally.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace openmc {
4646
//==============================================================================
4747

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

1275+
// given a tally ID, remove it from the tallies vector
1276+
extern "C" int openmc_remove_tally(int32_t index)
1277+
{
1278+
// check that id is in the map
1279+
if (index < 0 || index > model::tallies.size()) {
1280+
return OPENMC_E_OUT_OF_BOUNDS;
1281+
}
1282+
// grab tally so it's ID can be obtained to remove the (ID,index) pair from tally_map
1283+
auto& tally = model::tallies[index];
1284+
// delete the tally via iterator pointing to correct position
1285+
// this calls the Tally destructor, removing the tally from the map as well
1286+
model::tallies.erase(model::tallies.begin() + index);
1287+
1288+
return 0;
1289+
}
1290+
12741291
} // namespace openmc

tests/unit_tests/test_lib.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,20 @@ def test_new_tally(lib_init):
340340
assert len(openmc.lib.tallies) == 5
341341

342342

343+
def test_delete_tally(lib_init):
344+
# delete tally 10 which was added in the above test
345+
# check length is one less than before
346+
del openmc.lib.tallies[10]
347+
assert len(openmc.lib.tallies) == 4
348+
349+
350+
def test_invalid_tally_id(lib_init):
351+
# attempt to access a tally that is guaranteed not to have a valid index
352+
max_id = max(openmc.lib.tallies.keys())
353+
with pytest.raises(KeyError):
354+
openmc.lib.tallies[max_id+1]
355+
356+
343357
def test_tally_activate(lib_simulation_init):
344358
t = openmc.lib.tallies[1]
345359
assert not t.active

tests/unit_tests/test_tallies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ def test_xml_roundtrip(run_in_tmpdir):
3838
assert len(new_tally.triggers) == 1
3939
assert new_tally.triggers[0].trigger_type == tally.triggers[0].trigger_type
4040
assert new_tally.triggers[0].threshold == tally.triggers[0].threshold
41-
assert new_tally.triggers[0].scores == tally.triggers[0].scores
41+
assert new_tally.triggers[0].scores == tally.triggers[0].scores

0 commit comments

Comments
 (0)