From d5eaa3b5b768a67a3c80b4d033ecc7dbc6695db3 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 24 Jul 2019 14:36:55 -0500 Subject: [PATCH] Converting double max to np.inf on the Python side for bounding boxes. --- openmc/capi/cell.py | 8 ++++++++ openmc/capi/core.py | 7 +++++++ tests/unit_tests/test_capi.py | 7 ++----- tests/unit_tests/test_complex_cell_capi.py | 18 ++++++++++-------- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/openmc/capi/cell.py b/openmc/capi/cell.py index de8ee9f43..784ebd087 100644 --- a/openmc/capi/cell.py +++ b/openmc/capi/cell.py @@ -1,3 +1,5 @@ +import sys + from collections.abc import Mapping, Iterable from ctypes import c_int, c_int32, c_double, c_char_p, POINTER from weakref import WeakValueDictionary @@ -190,11 +192,17 @@ class Cell(_FortranObjectWithID): @property def bounding_box(self): + inf = sys.float_info.max llc = np.zeros(3) urc = np.zeros(3) _dll.openmc_cell_bounding_box(self._index, llc.ctypes.data_as(POINTER(c_double)), urc.ctypes.data_as(POINTER(c_double))) + llc[llc == inf] = np.inf + urc[urc == inf] = np.inf + llc[llc == -inf] = -np.inf + urc[urc == -inf] = -np.inf + return llc, urc class _CellMapping(Mapping): diff --git a/openmc/capi/core.py b/openmc/capi/core.py index cd7401a23..a470f0665 100644 --- a/openmc/capi/core.py +++ b/openmc/capi/core.py @@ -1,3 +1,5 @@ +import sys + from contextlib import contextmanager from ctypes import (CDLL, c_bool, c_int, c_int32, c_int64, c_double, c_char_p, c_char, POINTER, Structure, c_void_p, create_string_buffer) @@ -81,10 +83,15 @@ _dll.openmc_global_bounding_box.errcheck = _error_handler def global_bounding_box(): """Calculate a global bounding box for the model""" + inf = sys.float_info.max llc = np.zeros(3) urc = np.zeros(3) _dll.openmc_global_bounding_box(llc.ctypes.data_as(POINTER(c_double)), urc.ctypes.data_as(POINTER(c_double))) + llc[llc == inf] = np.inf + urc[urc == inf] = np.inf + llc[llc == -inf] = -np.inf + urc[urc == -inf] = -np.inf return llc, urc diff --git a/tests/unit_tests/test_capi.py b/tests/unit_tests/test_capi.py index d1c578ba0..6953186eb 100644 --- a/tests/unit_tests/test_capi.py +++ b/tests/unit_tests/test_capi.py @@ -1,6 +1,5 @@ from collections.abc import Mapping import os -import sys import numpy as np import pytest @@ -477,10 +476,8 @@ def test_position(capi_init): def test_global_bounding_box(capi_init): - inf = sys.float_info.max - - expected_llc = (-0.63, -0.63, -inf) - expected_urc = (0.63, 0.63, inf) + expected_llc = (-0.63, -0.63, -np.inf) + expected_urc = (0.63, 0.63, np.inf) llc, urc = openmc.capi.global_bounding_box() diff --git a/tests/unit_tests/test_complex_cell_capi.py b/tests/unit_tests/test_complex_cell_capi.py index e5983d2d5..365ce4808 100644 --- a/tests/unit_tests/test_complex_cell_capi.py +++ b/tests/unit_tests/test_complex_cell_capi.py @@ -1,5 +1,3 @@ -import sys - import numpy as np import openmc.capi import pytest @@ -82,13 +80,17 @@ def complex_cell(run_in_tmpdir): openmc.capi.finalize() -inf = sys.float_info.max -expected_results = ( (1, (( -4., -4., -inf), ( 4., 4., inf))), - (2, (( -7., -7., -inf), ( 7., 7., inf))), - (3, ((-10., -10., -inf), (10., 10., inf))), - (4, ((-10., -10., -inf), (10., 10., inf))), - (5, ((-inf, -inf, -inf), (inf, inf, inf))) ) +expected_results = ( (1, (( -4., -4., -np.inf), + ( 4., 4., np.inf))), + (2, (( -7., -7., -np.inf), + ( 7., 7., np.inf))), + (3, ((-10., -10., -np.inf), + ( 10., 10., np.inf))), + (4, ((-10., -10., -np.inf), + ( 10., 10., np.inf))), + (5, ((-np.inf, -np.inf, -np.inf), + ( np.inf, np.inf, np.inf))) ) @pytest.mark.parametrize("cell_id,expected_box", expected_results) def test_cell_box(cell_id, expected_box): cell_box = openmc.capi.cells[cell_id].bounding_box