From 66593fd7e1d072e1ad07e4fb5d6d3724cbad0ac6 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Fri, 20 Sep 2019 09:14:09 -0500 Subject: [PATCH] Expose tally.writeable_ through openmc.lib Add two functions to capi.h: openmc_tally_set_writeable and openmc_tally_get_writeable which are exposed to the Python API through the openmc.lib.Tally.writeable property. These functions are very similar to the set/get active counterparts as both act on a boolean Tally attribute A new unit test test_tally_writeable has been added that toggles the writeable state of a tally. It is important to note that the writeable state must be reset, otherwise tallies in subsequent tests will be skewed. This is because the test_tally_writeable function requires the capi_simulation_init fixture and the tallies are shared by those functions that use the capi_run fixture --- include/openmc/capi.h | 2 ++ openmc/lib/tally.py | 16 ++++++++++++++++ src/tallies/tally.cpp | 24 ++++++++++++++++++++++++ tests/unit_tests/test_lib.py | 11 ++++++++++- 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/include/openmc/capi.h b/include/openmc/capi.h index 3d2e8f57bf..327594c3e4 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -110,6 +110,7 @@ extern "C" { int openmc_tally_get_nuclides(int32_t index, int** nuclides, int* n); int openmc_tally_get_scores(int32_t index, int** scores, int* n); int openmc_tally_get_type(int32_t index, int32_t* type); + int openmc_tally_get_writeable(int32_t index, bool* writeable); int openmc_tally_reset(int32_t index); int openmc_tally_results(int32_t index, double** ptr, size_t shape_[3]); int openmc_tally_set_active(int32_t index, bool active); @@ -119,6 +120,7 @@ extern "C" { int openmc_tally_set_nuclides(int32_t index, int n, const char** nuclides); int openmc_tally_set_scores(int32_t index, int n, const char** scores); int openmc_tally_set_type(int32_t index, const char* type); + int openmc_tally_set_writeable(int32_t index, bool writeable); int openmc_zernike_filter_get_order(int32_t index, int* order); int openmc_zernike_filter_get_params(int32_t index, double* x, double* y, double* r); int openmc_zernike_filter_set_order(int32_t index, int order); diff --git a/openmc/lib/tally.py b/openmc/lib/tally.py index 97b7437ea3..906084c4b7 100644 --- a/openmc/lib/tally.py +++ b/openmc/lib/tally.py @@ -53,6 +53,9 @@ _dll.openmc_tally_get_scores.errcheck = _error_handler _dll.openmc_tally_get_type.argtypes = [c_int32, POINTER(c_int32)] _dll.openmc_tally_get_type.restype = c_int _dll.openmc_tally_get_type.errcheck = _error_handler +_dll.openmc_tally_get_writeable.argtypes = [c_int32, POINTER(c_bool)] +_dll.openmc_tally_get_writeable.restype = c_int +_dll.openmc_tally_get_writeable.errcheck = _error_handler _dll.openmc_tally_reset.argtypes = [c_int32] _dll.openmc_tally_reset.restype = c_int _dll.openmc_tally_reset.errcheck = _error_handler @@ -81,6 +84,9 @@ _dll.openmc_tally_set_scores.errcheck = _error_handler _dll.openmc_tally_set_type.argtypes = [c_int32, c_char_p] _dll.openmc_tally_set_type.restype = c_int _dll.openmc_tally_set_type.errcheck = _error_handler +_dll.openmc_tally_set_writeable.argtypes = [c_int32, c_bool] +_dll.openmc_tally_set_writeable.restype = c_int +_dll.openmc_tally_set_writeable.errcheck = _error_handler _dll.tallies_size.restype = c_size_t @@ -344,6 +350,16 @@ class Tally(_FortranObjectWithID): return std_dev + @property + def writeable(self): + writeable = c_bool() + _dll.openmc_tally_get_writeable(self._index, writeable) + return writeable.value + + @writeable.setter + def writeable(self, writeable): + _dll.openmc_tally_set_writeable(self._index, writeable) + def reset(self): """Reset results and num_realizations of tally""" _dll.openmc_tally_reset(self._index) diff --git a/src/tallies/tally.cpp b/src/tallies/tally.cpp index 563beee766..96253285e2 100644 --- a/src/tallies/tally.cpp +++ b/src/tallies/tally.cpp @@ -1220,6 +1220,30 @@ openmc_tally_set_active(int32_t index, bool active) return 0; } +extern "C" int +openmc_tally_get_writeable(int32_t index, bool* writeable) +{ + if (index < 0 || index >= model::tallies.size()) { + set_errmsg("Index in tallies array is out of bounds."); + return OPENMC_E_OUT_OF_BOUNDS; + } + *writeable = model::tallies[index]->writeable_; + + return 0; +} + +extern "C" int +openmc_tally_set_writeable(int32_t index, bool writeable) +{ + if (index < 0 || index >= model::tallies.size()) { + set_errmsg("Index in tallies array is out of bounds."); + return OPENMC_E_OUT_OF_BOUNDS; + } + model::tallies[index]->writeable_ = writeable; + + return 0; +} + extern "C" int openmc_tally_get_scores(int32_t index, int** scores, int* n) { diff --git a/tests/unit_tests/test_lib.py b/tests/unit_tests/test_lib.py index 2f39f0b0d3..3736ddc1e0 100644 --- a/tests/unit_tests/test_lib.py +++ b/tests/unit_tests/test_lib.py @@ -256,9 +256,18 @@ def test_tally_activate(capi_simulation_init): assert t.active +def test_tally_writeable(capi_simulation_init): + t = openmc.lib.tallies[1] + assert t.writeable + t.writeable = False + assert not t.writeable + # Revert tally to writeable state for capi_run fixtures + t.writeable = True + + def test_tally_results(capi_run): t = openmc.lib.tallies[1] - assert t.num_realizations == 10 # t was made active in test_tally + assert t.num_realizations == 10 # t was made active in test_tally_active assert np.all(t.mean >= 0) nonzero = (t.mean > 0.0) assert np.all(t.std_dev[nonzero] >= 0)