From 3ef54ec349ac7a847df8c0b2de02c2bd2cf3155f Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 14 Aug 2024 05:43:44 +0100 Subject: [PATCH] Replacing endf c functions with package (#3101) --- openmc/data/_endf.pyx | 8 ------ openmc/data/endf.c | 57 ------------------------------------------- openmc/data/endf.py | 10 +------- pyproject.toml | 1 + setup.py | 2 +- 5 files changed, 3 insertions(+), 75 deletions(-) delete mode 100644 openmc/data/_endf.pyx delete mode 100644 openmc/data/endf.c diff --git a/openmc/data/_endf.pyx b/openmc/data/_endf.pyx deleted file mode 100644 index 991ee015b..000000000 --- a/openmc/data/_endf.pyx +++ /dev/null @@ -1,8 +0,0 @@ -# cython: c_string_type=str, c_string_encoding=ascii - -cdef extern from "endf.c": - double cfloat_endf(const char* buffer, int n) - -def float_endf(s): - cdef const char* c_string = s - return cfloat_endf(c_string, len(s)) diff --git a/openmc/data/endf.c b/openmc/data/endf.c deleted file mode 100644 index dd5eee541..000000000 --- a/openmc/data/endf.c +++ /dev/null @@ -1,57 +0,0 @@ -#include - -//! Convert string representation of a floating point number into a double -// -//! This function handles converting floating point numbers from an ENDF 11 -//! character field into a double, covering all the corner cases. Floating point -//! numbers are allowed to contain whitespace (which is ignored). Also, in -//! exponential notation, it allows the 'e' to be omitted. A field containing -//! only whitespace is to be interpreted as a zero. -// -//! \param buffer character input from an ENDF file -//! \param n Length of character input -//! \return Floating point number - -double cfloat_endf(const char* buffer, int n) -{ - char arr[13]; // 11 characters plus e and a null terminator - int j = 0; // current position in arr - int found_significand = 0; - int found_exponent = 0; - - // limit n to 11 characters - n = n > 11 ? 11 : n; - - int i; - for (i = 0; i < n; ++i) { - char c = buffer[i]; - - // Skip whitespace characters - if (c == ' ') continue; - - if (found_significand) { - if (!found_exponent) { - if (c == '+' || c == '-') { - // In the case that we encounter +/- and we haven't yet encountered - // e/E, we manually add it - arr[j++] = 'e'; - found_exponent = 1; - - } else if (c == 'e' || c == 'E' || c == 'd' || c == 'D') { - arr[j++] = 'e'; - found_exponent = 1; - continue; - } - } - } else if (c == '.' || (c >= '0' && c <= '9')) { - found_significand = 1; - } - - // Copy character - arr[j++] = c; - } - - // Done copying. Add null terminator and convert to double - arr[j] = '\0'; - return atof(arr); -} diff --git a/openmc/data/endf.py b/openmc/data/endf.py index 73299723a..63cd092eb 100644 --- a/openmc/data/endf.py +++ b/openmc/data/endf.py @@ -14,11 +14,7 @@ import numpy as np from .data import gnds_name from .function import Tabulated1D -try: - from ._endf import float_endf - _CYTHON = True -except ImportError: - _CYTHON = False +from endf.records import float_endf _LIBRARY = {0: 'ENDF/B', 1: 'ENDF/A', 2: 'JEFF', 3: 'EFF', @@ -91,10 +87,6 @@ def py_float_endf(s): return float(ENDF_FLOAT_RE.sub(r'\1e\2\3', s)) -if not _CYTHON: - float_endf = py_float_endf - - def int_endf(s): """Convert string of integer number in ENDF to int. diff --git a/pyproject.toml b/pyproject.toml index 2b2764cea..98b1d152f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,7 @@ dependencies = [ "lxml", "uncertainties", "setuptools", + "endf", ] [project.optional-dependencies] diff --git a/setup.py b/setup.py index 4e24f48a1..88a45a360 100755 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ from Cython.Build import cythonize kwargs = { - # Cython is used to add resonance reconstruction and fast float_endf + # Cython is used to add resonance reconstruction 'ext_modules': cythonize('openmc/data/*.pyx'), 'include_dirs': [np.get_include()] }