diff --git a/openmc/capi/__init__.py b/openmc/capi/__init__.py index 355304a310..4cea121f71 100644 --- a/openmc/capi/__init__.py +++ b/openmc/capi/__init__.py @@ -35,4 +35,5 @@ from .core import * from .nuclide import * from .material import * from .cell import * +from .filter import * from .tally import * diff --git a/openmc/capi/filter.py b/openmc/capi/filter.py new file mode 100644 index 0000000000..f671ce4016 --- /dev/null +++ b/openmc/capi/filter.py @@ -0,0 +1,15 @@ +from ctypes import c_int, c_int32, c_double, c_char_p, POINTER + +from . import _dll +from .error import _error_handler + + +__all__ = [] + +# Tally functions +_dll.openmc_extend_filters.argtypes = [c_int32, POINTER(c_int32), POINTER(c_int32)] +_dll.openmc_extend_filters.restype = c_int +_dll.openmc_extend_filters.errcheck = _error_handler +_dll.openmc_filter_set_type.argtypes = [c_int32, c_char_p] +_dll.openmc_filter_set_type.restype = c_int +_dll.openmc_filter_set_type.errcheck = _error_handler diff --git a/src/api.F90 b/src/api.F90 index f083c7a468..975fb36a93 100644 --- a/src/api.F90 +++ b/src/api.F90 @@ -19,6 +19,7 @@ module openmc_api use random_lcg, only: seed, initialize_prng use tally_header use tally_filter_header + use tally_filter, only: openmc_filter_set_type use simulation, only: openmc_run use string, only: to_f_string use volume_calc, only: openmc_calculate_volumes @@ -31,6 +32,7 @@ module openmc_api public :: openmc_cell_set_temperature public :: openmc_extend_filters public :: openmc_extend_tallies + public :: openmc_filter_set_type public :: openmc_finalize public :: openmc_find public :: openmc_get_cell diff --git a/src/error.F90 b/src/error.F90 index 9dec93147c..b7736d0a71 100644 --- a/src/error.F90 +++ b/src/error.F90 @@ -27,6 +27,7 @@ module error integer(C_INT), public, bind(C) :: E_TALLY_INVALID_ID = -12 integer(C_INT), public, bind(C) :: E_INVALID_SIZE = -13 integer(C_INT), public, bind(C) :: E_CELL_NO_MATERIAL = -14 + integer(C_INT), public, bind(C) :: E_ALREADY_ALLOCATED = -15 ! Warning codes integer(C_INT), public, bind(C) :: W_BELOW_MIN_BOUND = 1 diff --git a/src/tallies/tally_filter.F90 b/src/tallies/tally_filter.F90 index 7c3706dc6b..e5026f95ca 100644 --- a/src/tallies/tally_filter.F90 +++ b/src/tallies/tally_filter.F90 @@ -5,11 +5,12 @@ module tally_filter use algorithm, only: binary_search use constants, only: ONE, NO_BIN_FOUND, FP_PRECISION, ERROR_REAL use dict_header, only: DictIntInt + use error use geometry_header use hdf5_interface use particle_header, only: Particle use surface_header - use string, only: to_str + use string, only: to_str, to_f_string use tally_filter_header ! Inherit other filters @@ -1144,4 +1145,63 @@ contains end do end subroutine find_offset +!=============================================================================== +! C API FUNCTIONS +!=============================================================================== + + function openmc_filter_set_type(index, type) result(err) bind(C) + ! Set the type of a filter + integer(C_INT32_T), value, intent(in) :: index + character(kind=C_CHAR), intent(in) :: type(*) + integer(C_INT) :: err + + character(:), allocatable :: type_ + + type_ = to_f_string(type) + + err = 0 + if (index >= 1 .and. index <= n_filters) then + if (allocated(filters(index) % obj)) then + err = E_ALREADY_ALLOCATED + else + select case (type_) + case ('azimuthal') + allocate(AzimuthalFilter :: filters(index) % obj) + case ('cell') + allocate(CellFilter :: filters(index) % obj) + case ('cellborn') + allocate(CellbornFilter :: filters(index) % obj) + case ('cellfrom') + allocate(CellfromFilter :: filters(index) % obj) + case ('delayedgroup') + allocate(DelayedGroupFilter :: filters(index) % obj) + case ('distribcell') + allocate(DistribcellFilter :: filters(index) % obj) + case ('energy') + allocate(EnergyFilter :: filters(index) % obj) + case ('energyout') + allocate(EnergyoutFilter :: filters(index) % obj) + case ('energyfunction') + allocate(EnergyFunctionFilter :: filters(index) % obj) + case ('material') + allocate(MaterialFilter :: filters(index) % obj) + case ('mesh') + allocate(MeshFilter :: filters(index) % obj) + case ('mu') + allocate(MuFilter :: filters(index) % obj) + case ('polar') + allocate(PolarFilter :: filters(index) % obj) + case ('surface') + allocate(SurfaceFilter :: filters(index) % obj) + case ('universe') + allocate(UniverseFilter :: filters(index) % obj) + case default + err = E_UNASSIGNED + end select + end if + else + err = E_OUT_OF_BOUNDS + end if + end function openmc_filter_set_type + end module tally_filter