From a10570245520dee4d1aafcfcc42a356ffa7c8f91 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 9 Jul 2019 16:23:24 -0500 Subject: [PATCH] Updates to CAPI bounding box methods. Addition of global bounding box method. Adding tests for PWR pincell model. --- include/openmc/capi.h | 1 + openmc/capi/core.py | 14 +++++++- src/geometry.cpp | 24 ++++++++++--- tests/unit_tests/test_capi.py | 67 ++++++++++++++++++++++++++++++----- 4 files changed, 92 insertions(+), 14 deletions(-) diff --git a/include/openmc/capi.h b/include/openmc/capi.h index ad2adae24..11b8a6ef6 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -30,6 +30,7 @@ extern "C" { int openmc_finalize(); int openmc_find_cell(const double* xyz, int32_t* index, int32_t* instance); int openmc_bounding_box(const char* geom_type, const int32_t id, double* llc, double* urc); + int openmc_global_bounding_box(double* llc, double* urc); int openmc_fission_bank(void** ptr, int64_t* n); int openmc_get_cell_index(int32_t id, int32_t* index); int openmc_get_filter_index(int32_t id, int32_t* index); diff --git a/openmc/capi/core.py b/openmc/capi/core.py index 5744e123c..ed78b38b6 100644 --- a/openmc/capi/core.py +++ b/openmc/capi/core.py @@ -73,11 +73,23 @@ _dll.openmc_simulation_finalize.errcheck = _error_handler _dll.openmc_statepoint_write.argtypes = [c_char_p, POINTER(c_bool)] _dll.openmc_statepoint_write.restype = c_int _dll.openmc_statepoint_write.errcheck = _error_handler - _dll.openmc_bounding_box.argtypes = [c_char_p, c_int, POINTER(c_double), POINTER(c_double)] _dll.openmc_bounding_box.restype = c_int _dll.openmc_bounding_box.errcheck = _error_handler +_dll.openmc_global_bounding_box.argtypes = [POINTER(c_double), + POINTER(c_double)] +_dll.openmc_global_bounding_box.restype = c_int +_dll.openmc_global_bounding_box.errcheck = _error_handler + +def global_bounding_box(): + + llc = np.zeros((3,), dtype=float) + urc = np.zeros((3,), dtype=float) + _dll.openmc_global_bounding_box(llc.ctypes.data_as(POINTER(c_double)), + urc.ctypes.data_as(POINTER(c_double))) + + return llc, urc def bounding_box(geom_type, geom_id): diff --git a/src/geometry.cpp b/src/geometry.cpp index ed75444ec..43620644f 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -484,19 +484,18 @@ openmc_bounding_box(const char* geom_type, const int32_t id, double* llc, double std::string gtype(geom_type); to_lower(gtype); - std::cout << gtype << std::endl; - if (gtype == "universe") { // negative ids only apply to surfaces - if (id < 0) { return OPENMC_E_GEOMETRY; } + if (id <= 0) { return OPENMC_E_GEOMETRY; } const auto& u = model::universes[model::universe_map[id]]; bbox = u->bounding_box(); } else if (gtype == "cell") { // negative ids only apply to surfaces - if (id < 0) { return OPENMC_E_GEOMETRY; } + if (id <= 0) { return OPENMC_E_GEOMETRY; } const auto& c = model::cells[model::cell_map[id]]; bbox = c->bounding_box(); } else if (gtype == "surface") { + if (id == 0) { return OPENMC_E_GEOMETRY; } const auto& s = model::surfaces[model::surface_map[abs(id)]]; bbox = s->bounding_box(id > 0); } else { @@ -518,4 +517,21 @@ openmc_bounding_box(const char* geom_type, const int32_t id, double* llc, double return 0; } +extern "C" int openmc_global_bounding_box(double* llc, double* urc) { + auto bbox = model::universes[model::root_universe]->bounding_box(); + + // set lower left corner values + llc[0] = bbox.xmin; + llc[1] = bbox.ymin; + llc[2] = bbox.zmin; + + // set upper right corner values + urc[0] = bbox.xmax; + urc[1] = bbox.ymax; + urc[2] = bbox.zmax; + + return 0; +} + + } // namespace openmc diff --git a/tests/unit_tests/test_capi.py b/tests/unit_tests/test_capi.py index aa938f8b2..d611c20c7 100644 --- a/tests/unit_tests/test_capi.py +++ b/tests/unit_tests/test_capi.py @@ -1,5 +1,6 @@ from collections.abc import Mapping import os +import sys import numpy as np import pytest @@ -472,15 +473,63 @@ def test_position(capi_init): def test_bounding_box(capi_init): - expected_llc = (-0.63, -0.63, -np.inf) - expected_urc = (0.63, 0.63, np.inf) + inf = sys.float_info.max - llc, urc = openmc.capi.bounding_box("Universe", 0) + expected_llc = (-inf, -0.63, -inf) + expected_urc = (inf, inf, inf) - assert llc[0] == expected_llc[0] - assert llc[1] == expected_llc[1] - assert llc[2] < 1.0E10 + llc, urc = openmc.capi.bounding_box("Surface", 5) - assert urc[0] == expected_urc[0] - assert urc[1] == expected_urc[1] - assert urc[2] > 1.E10 + print(llc) + print(urc) + + assert tuple(llc) == expected_llc + assert tuple(urc) == expected_urc + + + expected_llc = (-inf, -inf, -inf) + expected_urc = (inf, -0.63, inf) + + llc, urc = openmc.capi.bounding_box("Surface", -5) + + assert tuple(llc) == expected_llc + assert tuple(urc) == expected_urc + + expected_llc = (-0.39218, -0.39218, -inf) + expected_urc = (0.39218, 0.39218, inf) + + llc, urc = openmc.capi.bounding_box("Cell", 1) + + assert tuple(llc) == expected_llc + assert tuple(urc) == expected_urc + + expected_llc = (-0.45720, -0.45720, -inf) + expected_urc = (0.45720, 0.45720, inf) + + llc, urc = openmc.capi.bounding_box("Cell", 2) + + assert tuple(llc) == expected_llc + assert tuple(urc) == expected_urc + + # make sure that proper assertions are raised + with pytest.raises(openmc.exceptions.GeometryError): + openmc.capi.bounding_box("Cell", -1) + + with pytest.raises(openmc.exceptions.GeometryError): + openmc.capi.bounding_box("Surface", 0) + + with pytest.raises(openmc.exceptions.GeometryError): + openmc.capi.bounding_box("Region", 1) + + +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) + + llc, urc = openmc.capi.global_bounding_box() + + assert tuple(llc) == expected_llc + assert tuple(urc) == expected_urc