From b615070ad95577d8faa8b4b96bac07d4e8ac4f1f Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Mon, 27 Jul 2020 16:17:18 -0500 Subject: [PATCH] Add Python bindings to one_group_xs --- openmc/lib/nuclide.py | 14 ++++++++++++++ src/nuclide.cpp | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/openmc/lib/nuclide.py b/openmc/lib/nuclide.py index 571dcc6551..4dd90f9cae 100644 --- a/openmc/lib/nuclide.py +++ b/openmc/lib/nuclide.py @@ -2,6 +2,9 @@ from collections.abc import Mapping from ctypes import c_int, c_double, c_char_p, POINTER, c_size_t from weakref import WeakValueDictionary +from numpy.ctypeslib import ndpointer +import numpy as np + from ..exceptions import DataError, AllocationError from . import _dll from .core import _FortranObject @@ -10,6 +13,8 @@ from .error import _error_handler __all__ = ['Nuclide', 'nuclides', 'load_nuclide'] +_array_1d_dble = ndpointer(dtype=np.double, ndim=1, flags='CONTIGUOUS') + # Nuclide functions _dll.openmc_get_nuclide_index.argtypes = [c_char_p, POINTER(c_int)] _dll.openmc_get_nuclide_index.restype = c_int @@ -20,6 +25,8 @@ _dll.openmc_load_nuclide.errcheck = _error_handler _dll.openmc_nuclide_name.argtypes = [c_int, POINTER(c_char_p)] _dll.openmc_nuclide_name.restype = c_int _dll.openmc_nuclide_name.errcheck = _error_handler +_dll.openmc_nuclide_one_group_xs.argtypes = [c_int, c_int, _array_1d_dble, + _array_1d_dble, c_int, POINTER(c_double)] _dll.nuclides_size.restype = c_size_t @@ -70,6 +77,13 @@ class Nuclide(_FortranObject): _dll.openmc_nuclide_name(self._index, name) return name.value.decode() + def one_group_xs(self, MT, energy, flux): + energy = np.asarray(energy, dtype=float) + flux = np.asarray(flux, dtype=float) + xs = c_double() + _dll.openmc_nuclide_one_group_xs(self._index, MT, energy, flux, len(flux), xs) + return xs.value + class _NuclideMapping(Mapping): """Provide mapping from nuclide name to index in nuclides array.""" diff --git a/src/nuclide.cpp b/src/nuclide.cpp index 66236441ff..d6da7d60f8 100644 --- a/src/nuclide.cpp +++ b/src/nuclide.cpp @@ -1043,6 +1043,18 @@ openmc_nuclide_name(int index, const char** name) } } +extern "C" int +openmc_nuclide_one_group_xs(int index, int MT, const double* energy, const double* flux, int n, double* xs) +{ + if (index < 0 || index >= data::nuclides.size()) { + set_errmsg("Index in nuclides vector is out of bounds."); + return OPENMC_E_OUT_OF_BOUNDS; + } + + *xs = data::nuclides[index]->one_group_xs(MT, {energy, energy + n + 1}, {flux, flux + n}); + return 0; +} + void nuclides_clear() { data::nuclides.clear();