From 933a5c47704c5d2074e156a829f7fb00d41076d0 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Thu, 18 Jul 2019 22:21:55 -0500 Subject: [PATCH] Updates based on suggestions from @paulromano. --- include/openmc/cell.h | 7 ++++--- include/openmc/surface.h | 4 ++-- openmc/capi/cell.py | 8 ++++---- openmc/capi/core.py | 10 ++++++---- openmc/capi/material.py | 16 +++++++-------- src/cell.cpp | 22 ++++++++++----------- src/geometry.cpp | 1 + tests/regression_tests/dagmc/legacy/test.py | 3 +-- 8 files changed, 36 insertions(+), 35 deletions(-) diff --git a/include/openmc/cell.h b/include/openmc/cell.h index 3a0c816467..752945ceec 100644 --- a/include/openmc/cell.h +++ b/include/openmc/cell.h @@ -211,7 +211,7 @@ protected: bool contains_simple(Position r, Direction u, int32_t on_surface) const; bool contains_complex(Position r, Direction u, int32_t on_surface) const; BoundingBox bounding_box_simple() const; - BoundingBox bounding_box_complex(std::vector rpn) const; + static BoundingBox bounding_box_complex(std::vector rpn); }; //============================================================================== @@ -220,9 +220,7 @@ protected: class DAGCell : public Cell { public: - moab::DagMC* dagmc_ptr_; DAGCell(); - int32_t dag_index_; bool contains(Position r, Direction u, int32_t on_surface) const; @@ -232,6 +230,9 @@ public: BoundingBox bounding_box() const; void to_hdf5(hid_t group_id) const; + + moab::DagMC* dagmc_ptr_; //!< Pointer to DagMC instance + int32_t dag_index_; //!< DagMC index of cell }; #endif diff --git a/include/openmc/surface.h b/include/openmc/surface.h index a7d107ee31..5feb35701c 100644 --- a/include/openmc/surface.h +++ b/include/openmc/surface.h @@ -171,8 +171,8 @@ public: void to_hdf5(hid_t group_id) const; - moab::DagMC* dagmc_ptr_; - int32_t dag_index_; + moab::DagMC* dagmc_ptr_; //!< Pointer to the DagMC instance + int32_t dag_index_; //!< DagMC index of surface }; #endif //============================================================================== diff --git a/openmc/capi/cell.py b/openmc/capi/cell.py index 1388d97327..af948c92de 100644 --- a/openmc/capi/cell.py +++ b/openmc/capi/cell.py @@ -1,5 +1,5 @@ from collections.abc import Mapping, Iterable -from ctypes import byref, c_int, c_int32, c_double, c_char_p, POINTER +from ctypes import c_int, c_int32, c_double, c_char_p, POINTER from weakref import WeakValueDictionary import numpy as np @@ -111,7 +111,7 @@ class Cell(_FortranObjectWithID): @property def name(self): name = c_char_p() - _dll.openmc_cell_get_name(self._index, byref(name)) + _dll.openmc_cell_get_name(self._index, name) return name.value.decode() @name.setter @@ -185,8 +185,8 @@ class Cell(_FortranObjectWithID): @property def bounding_box(self): - llc = np.zeros((3,), dtype=float) - urc = np.zeros((3,), dtype=float) + llc = np.zeros(3) + urc = np.zeros(3) _dll.openmc_bounding_box(b'Cell', self.id, llc.ctypes.data_as(POINTER(c_double)), urc.ctypes.data_as(POINTER(c_double))) diff --git a/openmc/capi/core.py b/openmc/capi/core.py index 686dd78621..0e226d3a16 100644 --- a/openmc/capi/core.py +++ b/openmc/capi/core.py @@ -83,15 +83,17 @@ _dll.openmc_global_bounding_box.argtypes = [POINTER(c_double), _dll.openmc_global_bounding_box.restype = c_int _dll.openmc_global_bounding_box.errcheck = _error_handler + def global_bounding_box(): """Calculate a global bounding box for the model""" - llc = np.zeros((3,), dtype=float) - urc = np.zeros((3,), dtype=float) + 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))) return llc, urc + def bounding_box(geom_type, geom_id): """Get a bounding box for a geometric object @@ -103,8 +105,8 @@ def bounding_box(geom_type, geom_id): Id of the object. Can be positive or negative for surfaces. """ geomt = c_char_p(geom_type.encode()) - llc = np.zeros((3,), dtype=float) - urc = np.zeros((3,), dtype=float) + llc = np.zeros(3) + urc = np.zeros(3) _dll.openmc_bounding_box(geomt, geom_id, llc.ctypes.data_as(POINTER(c_double)), diff --git a/openmc/capi/material.py b/openmc/capi/material.py index 3f98e9f67d..f0ecac7610 100644 --- a/openmc/capi/material.py +++ b/openmc/capi/material.py @@ -1,5 +1,5 @@ from collections.abc import Mapping -from ctypes import byref, c_int, c_int32, c_double, c_char_p, POINTER, c_size_t +from ctypes import c_int, c_int32, c_double, c_char_p, POINTER, c_size_t from weakref import WeakValueDictionary import numpy as np @@ -48,12 +48,12 @@ _dll.openmc_material_set_densities.errcheck = _error_handler _dll.openmc_material_set_id.argtypes = [c_int32, c_int32] _dll.openmc_material_set_id.restype = c_int _dll.openmc_material_set_id.errcheck = _error_handler -_dll.openmc_cell_get_name.argtypes = [c_int32, POINTER(c_char_p)] -_dll.openmc_cell_get_name.restype = c_int -_dll.openmc_cell_get_name.errcheck = _error_handler -_dll.openmc_cell_set_name.argtypes = [c_int32, c_char_p] -_dll.openmc_cell_set_name.restype = c_int -_dll.openmc_cell_set_name.errcheck = _error_handler +_dll.openmc_material_get_name.argtypes = [c_int32, POINTER(c_char_p)] +_dll.openmc_material_get_name.restype = c_int +_dll.openmc_material_get_name.errcheck = _error_handler +_dll.openmc_material_set_name.argtypes = [c_int32, c_char_p] +_dll.openmc_material_set_name.restype = c_int +_dll.openmc_material_set_name.errcheck = _error_handler _dll.openmc_material_set_volume.argtypes = [c_int32, c_double] _dll.openmc_material_set_volume.restype = c_int _dll.openmc_material_set_volume.errcheck = _error_handler @@ -133,7 +133,7 @@ class Material(_FortranObjectWithID): @property def name(self): name = c_char_p() - _dll.openmc_material_get_name(self._index, byref(name)) + _dll.openmc_material_get_name(self._index, name) return name.value.decode() @name.setter diff --git a/src/cell.cpp b/src/cell.cpp index d77e0ead79..35e295f78b 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -1,11 +1,12 @@ #include "openmc/cell.h" +#include #include #include #include #include -#include +#include #include "openmc/capi.h" #include "openmc/constants.h" @@ -586,7 +587,7 @@ BoundingBox CSGCell::bounding_box_simple() const { return bbox; } -BoundingBox CSGCell::bounding_box_complex(std::vector rpn) const { +BoundingBox CSGCell::bounding_box_complex(std::vector rpn) { std::reverse(rpn.begin(), rpn.end()); @@ -599,7 +600,7 @@ BoundingBox CSGCell::bounding_box_complex(std::vector rpn) const { int32_t two = rpn.back(); rpn.pop_back(); // the first token should always be a surface - assert(one < OP_UNION); + Expects(one < OP_UNION); if (two >= OP_UNION) { if (two == OP_UNION) { @@ -608,14 +609,16 @@ BoundingBox CSGCell::bounding_box_complex(std::vector rpn) const { current &= model::surfaces[abs(one)-1]->bounding_box(one > 0); } } else { - // two surfaces in a row, create sub-rpn for region in parenthesis + // two surfaces in a row (left parenthesis), + // create sub-rpn for region in parenthesis 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 - while ((subrpn.back() < OP_UNION) || (*(subrpn.rbegin() + 1) < OP_UNION)) { + // (indicates a right parenthesis) + while (!((subrpn.back() >= OP_UNION) && (*(subrpn.rbegin() + 1) >= OP_UNION))) { subrpn.push_back(rpn.back()); rpn.pop_back(); } @@ -649,12 +652,7 @@ BoundingBox CSGCell::bounding_box_complex(std::vector rpn) const { } BoundingBox CSGCell::bounding_box() const { - if (simple_) { - return bounding_box_simple(); - } - else { - return bounding_box_complex(rpn_); - } + return simple_ ? bounding_box_simple() : bounding_box_complex(rpn_); } //============================================================================== @@ -1091,7 +1089,7 @@ openmc_cell_get_name(int32_t index, const char** name) { return OPENMC_E_OUT_OF_BOUNDS; } - *name = model::cells[index]->name_.c_str(); + *name = model::cells[index]->name_.data(); return 0; } diff --git a/src/geometry.cpp b/src/geometry.cpp index 43620644f9..b699f22687 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -501,6 +501,7 @@ openmc_bounding_box(const char* geom_type, const int32_t id, double* llc, double } else { std::stringstream msg; msg << "Geometry type: " << gtype << " is invalid."; + set_errmsg(msg); return OPENMC_E_GEOMETRY; } diff --git a/tests/regression_tests/dagmc/legacy/test.py b/tests/regression_tests/dagmc/legacy/test.py index b6f2f55e2e..d48ae98719 100644 --- a/tests/regression_tests/dagmc/legacy/test.py +++ b/tests/regression_tests/dagmc/legacy/test.py @@ -45,5 +45,4 @@ def test_dagmc(): mats = openmc.Materials([u235, water]) model.materials = mats - harness = PyAPITestHarness('statepoint.5.h5', model=model) - harness.main() + model.export_to_xml()