From 5ad4f94f3adcb2653b7c91d8c6c636e4238d1e96 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 24 Jul 2019 13:10:03 -0500 Subject: [PATCH] Removing CAPI function for bounding box of any type. Moving to a cell-only function. Tests are adjusted as needed. --- include/openmc/capi.h | 2 +- openmc/capi/cell.py | 7 ++++- openmc/capi/core.py | 27 ------------------- src/cell.cpp | 28 ++++++++++++++++--- src/geometry.cpp | 51 ----------------------------------- tests/unit_tests/test_capi.py | 46 ------------------------------- 6 files changed, 32 insertions(+), 129 deletions(-) diff --git a/include/openmc/capi.h b/include/openmc/capi.h index 3dd9ae6976..ded41f738d 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -31,7 +31,7 @@ extern "C" { int openmc_filter_set_id(int32_t index, int32_t id); 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_cell_bounding_box(const int32_t index, 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); diff --git a/openmc/capi/cell.py b/openmc/capi/cell.py index af948c92de..de8ee9f43d 100644 --- a/openmc/capi/cell.py +++ b/openmc/capi/cell.py @@ -49,6 +49,11 @@ _dll.openmc_get_cell_index.argtypes = [c_int32, POINTER(c_int32)] _dll.openmc_get_cell_index.restype = c_int _dll.openmc_get_cell_index.errcheck = _error_handler _dll.cells_size.restype = c_int +_dll.openmc_cell_bounding_box.argtypes = [c_int, + POINTER(c_double), + POINTER(c_double)] +_dll.openmc_cell_bounding_box.restype = c_int +_dll.openmc_cell_bounding_box.errcheck = _error_handler class Cell(_FortranObjectWithID): @@ -187,7 +192,7 @@ class Cell(_FortranObjectWithID): def bounding_box(self): llc = np.zeros(3) urc = np.zeros(3) - _dll.openmc_bounding_box(b'Cell', self.id, + _dll.openmc_cell_bounding_box(self._index, llc.ctypes.data_as(POINTER(c_double)), urc.ctypes.data_as(POINTER(c_double))) return llc, urc diff --git a/openmc/capi/core.py b/openmc/capi/core.py index c3f5ea1b62..cd7401a235 100644 --- a/openmc/capi/core.py +++ b/openmc/capi/core.py @@ -73,11 +73,6 @@ _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 @@ -93,28 +88,6 @@ def global_bounding_box(): return llc, urc - -def bounding_box(geom_type, geom_id): - """Get a bounding box for a geometric object - - Parameters - ---------- - geom_type : str - Type of geometry object. One of ('surface', 'cell', 'universe') - geom_id : int - ID of the object. Can be positive or negative for surfaces. - """ - geomt = c_char_p(geom_type.encode()) - llc = np.zeros(3) - urc = np.zeros(3) - _dll.openmc_bounding_box(geomt, - geom_id, - llc.ctypes.data_as(POINTER(c_double)), - urc.ctypes.data_as(POINTER(c_double))) - - return llc, urc - - def calculate_volumes(): """Run stochastic volume calculation""" _dll.openmc_calculate_volumes() diff --git a/src/cell.cpp b/src/cell.cpp index 510002fa97..3c079f7ae5 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -587,6 +587,7 @@ BoundingBox CSGCell::bounding_box_simple() const { return bbox; } + void CSGCell::apply_demorgan(std::vector& rpn) { for (auto& token : rpn) { if (token < OP_UNION) { token *= -1; } @@ -601,11 +602,12 @@ BoundingBox CSGCell::bounding_box_complex(std::vector rpn) { // sub-region that the complement connects to. This indicates // that the entire region is a complement and we can apply // De Morgan's laws immediately - if ((rpn.back() == OP_COMPLEMENT)) { + if (rpn.back() == OP_COMPLEMENT) { rpn.pop_back(); apply_demorgan(rpn); } + // reverse the rpn to make popping easier std::reverse(rpn.begin(), rpn.end()); BoundingBox current = model::surfaces[abs(rpn.back()) - 1]->bounding_box(rpn.back() > 0); @@ -631,8 +633,6 @@ BoundingBox CSGCell::bounding_box_complex(std::vector rpn) { std::vector subrpn; subrpn.push_back(one); subrpn.push_back(two); - int32_t sone = one; - int32_t stwo = two; // add until last two tokens in the sub-rpn are operators // (indicates a right parenthesis) while (!((subrpn.back() >= OP_UNION) && (*(subrpn.rbegin() + 1) >= OP_UNION))) { @@ -1094,6 +1094,28 @@ openmc_cell_get_temperature(int32_t index, const int32_t* instance, double* T) return 0; } +//! Get the bounding box of a cell +extern "C" int +openmc_cell_bounding_box(const int32_t index, double* llc, double* urc) { + + BoundingBox bbox; + + const auto& c = model::cells[index]; + bbox = c->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; +} + //! Get the name of a cell extern "C" int openmc_cell_get_name(int32_t index, const char** name) { diff --git a/src/geometry.cpp b/src/geometry.cpp index ae48a0acb9..556d106508 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -476,57 +476,6 @@ openmc_find_cell(const double* xyz, int32_t* index, int32_t* instance) return 0; } -extern "C" int -openmc_bounding_box(const char* geom_type, const int32_t id, double* llc, double* urc) { - - BoundingBox bbox; - - std::string gtype(geom_type); - to_lower(gtype); - - // negative ids only for surfaces - if (id < 0 && gtype != "surface") { - std::stringstream msg; - msg << "Negative ID " << id << " passed to bounding box for " - << "non-surface geom type '" << geom_type << "'" << std::endl; - set_errmsg(msg); - return OPENMC_E_INVALID_ID; - } - // id should never be zero - if (id == 0) { - set_errmsg("Invalid ID 0 for surface bounding box."); - return OPENMC_E_INVALID_ID; - } - - if (gtype == "universe") { - const auto& u = model::universes[model::universe_map.at(id)]; - bbox = u->bounding_box(); - } else if (gtype == "cell") { - const auto& c = model::cells[model::cell_map.at(id)]; - bbox = c->bounding_box(); - } else if (gtype == "surface") { - const auto& s = model::surfaces[model::surface_map.at(abs(id))]; - bbox = s->bounding_box(id > 0); - } else { - std::stringstream msg; - msg << "Geometry type: " << gtype << " is invalid."; - set_errmsg(msg); - return OPENMC_E_INVALID_TYPE; - } - - // 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; -} - extern "C" int openmc_global_bounding_box(double* llc, double* urc) { auto bbox = model::universes.at(model::root_universe)->bounding_box(); diff --git a/tests/unit_tests/test_capi.py b/tests/unit_tests/test_capi.py index 3fdd4db660..d1c578ba05 100644 --- a/tests/unit_tests/test_capi.py +++ b/tests/unit_tests/test_capi.py @@ -475,52 +475,6 @@ def test_position(capi_init): assert tuple(pos) == (1.3, 2.3, 3.3) -def test_bounding_box(capi_init): - inf = sys.float_info.max - - expected_llc = (-inf, -0.63, -inf) - expected_urc = (inf, inf, inf) - - llc, urc = openmc.capi.bounding_box("Surface", 5) - - 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.InvalidIDError): - openmc.capi.bounding_box("Cell", -1) - - with pytest.raises(openmc.exceptions.InvalidIDError): - openmc.capi.bounding_box("Surface", 0) - - with pytest.raises(openmc.exceptions.InvalidTypeError): - openmc.capi.bounding_box("Region", 1) - def test_global_bounding_box(capi_init): inf = sys.float_info.max