diff --git a/openmc/capi/tally.py b/openmc/capi/tally.py index 799cb42ee8..7037484aea 100644 --- a/openmc/capi/tally.py +++ b/openmc/capi/tally.py @@ -4,6 +4,7 @@ from weakref import WeakValueDictionary from numpy.ctypeslib import as_array +from openmc.data.reaction import REACTION_NAME from . import _dll, Nuclide from .core import _FortranObjectWithID from .error import _error_handler, AllocationError, InvalidIDError @@ -30,6 +31,10 @@ _dll.openmc_tally_get_nuclides.argtypes = [ c_int32, POINTER(POINTER(c_int)), POINTER(c_int)] _dll.openmc_tally_get_nuclides.restype = c_int _dll.openmc_tally_get_nuclides.errcheck = _error_handler +_dll.openmc_tally_get_scores.argtypes = [ + c_int32, POINTER(POINTER(c_int)), POINTER(c_int)] +_dll.openmc_tally_get_scores.restype = c_int +_dll.openmc_tally_get_scores.errcheck = _error_handler _dll.openmc_tally_results.argtypes = [ c_int32, POINTER(POINTER(c_double)), POINTER(c_int*3)] _dll.openmc_tally_results.restype = c_int @@ -51,6 +56,15 @@ _dll.openmc_tally_set_type.restype = c_int _dll.openmc_tally_set_type.errcheck = _error_handler +_SCORES = { + -1: 'flux', -2: 'total', -3: 'scatter', -4: 'nu-scatter', + -9: 'absorption', -10: 'fission', -11: 'nu-fission', -12: 'kappa-fission', + -13: 'current', -18: 'events', -19: 'delayed-nu-fission', + -20: 'prompt-nu-fission', -21: 'inverse-velocity', -22: 'fission-q-prompt', + -23: 'fission-q-recoverable', -24: 'decay-rate' +} + + class Tally(_FortranObjectWithID): """Tally stored internally. @@ -161,7 +175,22 @@ class Tally(_FortranObjectWithID): @property def scores(self): - pass + scores_as_int = POINTER(c_int)() + n = c_int() + try: + _dll.openmc_tally_get_scores(self._index, scores_as_int, n) + except AllocationError: + return [] + else: + scores = [] + for i in range(n.value): + if scores_as_int[i] in _SCORES: + scores.append(_SCORES[scores_as_int[i]]) + elif scores_as_int[i] in REACTION_NAME: + scores.append(REACTION_NAME[scores_as_int[i]]) + else: + scores.append(str(scores_as_int[i])) + return scores @scores.setter def scores(self, scores): diff --git a/src/api.F90 b/src/api.F90 index f5b27e7181..933faaa09e 100644 --- a/src/api.F90 +++ b/src/api.F90 @@ -74,6 +74,7 @@ module openmc_api public :: openmc_tally_get_id public :: openmc_tally_get_filters public :: openmc_tally_get_nuclides + public :: openmc_tally_get_scores public :: openmc_tally_results public :: openmc_tally_set_filters public :: openmc_tally_set_id diff --git a/src/constants.F90 b/src/constants.F90 index 73aca25d29..f6a7e8da92 100644 --- a/src/constants.F90 +++ b/src/constants.F90 @@ -304,7 +304,8 @@ module constants EVENT_SCATTER = 1, & EVENT_ABSORB = 2 - ! Tally score type + ! Tally score type -- if you change these, make sure you also update the + ! _SCORES dictionary in openmc/capi/tally.py integer, parameter :: N_SCORE_TYPES = 24 integer, parameter :: & SCORE_FLUX = -1, & ! flux diff --git a/src/tallies/tally_header.F90 b/src/tallies/tally_header.F90 index df9d935e94..e137a13274 100644 --- a/src/tallies/tally_header.F90 +++ b/src/tallies/tally_header.F90 @@ -25,6 +25,7 @@ module tally_header public :: openmc_tally_get_id public :: openmc_tally_get_filters public :: openmc_tally_get_nuclides + public :: openmc_tally_get_scores public :: openmc_tally_results public :: openmc_tally_set_filters public :: openmc_tally_set_id @@ -535,6 +536,31 @@ contains end function openmc_tally_get_nuclides + function openmc_tally_get_scores(index, scores, n) result(err) bind(C) + ! Return the list of nuclides assigned to a tally + integer(C_INT32_T), value :: index + type(C_PTR), intent(out) :: scores + integer(C_INT), intent(out) :: n + integer(C_INT) :: err + + if (index >= 1 .and. index <= size(tallies)) then + associate (t => tallies(index) % obj) + if (allocated(t % score_bins)) then + scores = C_LOC(t % score_bins(1)) + n = size(t % score_bins) + err = 0 + else + err = E_ALLOCATE + call set_errmsg("Tally scores have not been allocated yet.") + end if + end associate + else + err = E_OUT_OF_BOUNDS + call set_errmsg('Index in tallies array is out of bounds.') + end if + end function openmc_tally_get_scores + + function openmc_tally_results(index, ptr, shape_) result(err) bind(C) ! Returns a pointer to a tally results array along with its shape. This ! allows a user to obtain in-memory tally results from Python directly.