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
This commit is contained in:
Andrew Johnson 2019-09-20 09:14:09 -05:00
parent 1300ca2678
commit 66593fd7e1
No known key found for this signature in database
GPG key ID: 253418E91B7F6FEB
4 changed files with 52 additions and 1 deletions

View file

@ -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);

View file

@ -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)

View file

@ -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)
{

View file

@ -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)