diff --git a/src/geometry.cpp b/src/geometry.cpp index 91e2fc245d..ae48a0acb9 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -484,18 +484,27 @@ openmc_bounding_box(const char* geom_type, const int32_t id, double* llc, double 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") { - // negative ids only apply to surfaces - if (id <= 0) { return OPENMC_E_INVALID_ID; } const auto& u = model::universes[model::universe_map.at(id)]; bbox = u->bounding_box(); } else if (gtype == "cell") { - // negative ids only apply to surfaces - if (id <= 0) { return OPENMC_E_INVALID_ID; } const auto& c = model::cells[model::cell_map.at(id)]; bbox = c->bounding_box(); } else if (gtype == "surface") { - if (id == 0) { return OPENMC_E_INVALID_ID; } const auto& s = model::surfaces[model::surface_map.at(abs(id))]; bbox = s->bounding_box(id > 0); } else { diff --git a/tests/unit_tests/test_capi.py b/tests/unit_tests/test_capi.py index b02e43815f..aa140a121b 100644 --- a/tests/unit_tests/test_capi.py +++ b/tests/unit_tests/test_capi.py @@ -508,13 +508,13 @@ def test_bounding_box(capi_init): assert tuple(urc) == expected_urc # make sure that proper assertions are raised - with pytest.raises(openmc.exceptions.GeometryError): + with pytest.raises(openmc.exceptions.InvalidIDError): openmc.capi.bounding_box("Cell", -1) - with pytest.raises(openmc.exceptions.GeometryError): + with pytest.raises(openmc.exceptions.InvalidIDError): openmc.capi.bounding_box("Surface", 0) - with pytest.raises(openmc.exceptions.GeometryError): + with pytest.raises(openmc.exceptions.InvalidTypeError): openmc.capi.bounding_box("Region", 1)