diff --git a/docs/source/capi/index.rst b/docs/source/capi/index.rst index 274a5240f0..354ee28784 100644 --- a/docs/source/capi/index.rst +++ b/docs/source/capi/index.rst @@ -335,6 +335,15 @@ Functions :return: Return status (negative if an error occurs) :rtype: int +.. c:function:: int openmc_material_get_density(int32_t index, double* density) + + Get density of a material. + + :param int32_t index: Index in the materials array + :param double* denity: Pointer to a density + :return Return status (negative if an error occurs) + :rtype: int + .. c:function:: int openmc_material_get_id(int32_t index, int32_t* id) Get the ID of a material diff --git a/include/openmc/capi.h b/include/openmc/capi.h index 1f6686df1a..27b7e17a9c 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -50,6 +50,7 @@ extern "C" { int openmc_material_get_densities(int32_t index, int** nuclides, double** densities, int* n); int openmc_material_get_id(int32_t index, int32_t* id); int openmc_material_get_fissionable(int32_t index, bool* fissionable); + int openmc_material_get_density(int32_t index, double* density); int openmc_material_get_volume(int32_t index, double* volume); int openmc_material_set_density(int32_t index, double density, const char* units); int openmc_material_set_densities(int32_t index, int n, const char** name, const double* density); diff --git a/openmc/capi/material.py b/openmc/capi/material.py index 9811853380..43959e2446 100644 --- a/openmc/capi/material.py +++ b/openmc/capi/material.py @@ -32,6 +32,9 @@ _dll.openmc_material_get_densities.argtypes = [ POINTER(c_int)] _dll.openmc_material_get_densities.restype = c_int _dll.openmc_material_get_densities.errcheck = _error_handler +_dll.openmc_material_get_density.argtypes = [c_int32, POINTER(c_double)] +_dll.openmc_material_get_density.restype = c_int +_dll.openmc_material_get_density.errcheck = _error_handler _dll.openmc_material_get_volume.argtypes = [c_int32, POINTER(c_double)] _dll.openmc_material_get_volume.restype = c_int _dll.openmc_material_get_volume.errcheck = _error_handler @@ -139,6 +142,15 @@ class Material(_FortranObjectWithID): return self._get_densities()[0] return nuclides + @property + def density(self): + density = c_double() + try: + _dll.openmc_material_get_density(self._index, density) + except OpenMCError: + return None + return density.value + @property def densities(self): return self._get_densities()[1] diff --git a/src/material.cpp b/src/material.cpp index b5cb2c47a7..ec76eb9884 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -1203,6 +1203,19 @@ openmc_material_get_densities(int32_t index, int** nuclides, double** densities, } } +extern "C" int +openmc_material_get_density(int32_t index, double* density) +{ + if (index >= 0 && index < model::materials.size()) { + auto& mat = model::materials[index]; + *density = mat->density_gpcc_; + return 0; + } else { + set_errmsg("Index in materials array is out of bounds."); + return OPENMC_E_OUT_OF_BOUNDS; + } +} + extern "C" int openmc_material_get_fissionable(int32_t index, bool* fissionable) { diff --git a/tests/unit_tests/test_capi.py b/tests/unit_tests/test_capi.py index 07fef85166..418fe1d3f1 100644 --- a/tests/unit_tests/test_capi.py +++ b/tests/unit_tests/test_capi.py @@ -120,6 +120,9 @@ def test_material(capi_init): m.set_density(rho) assert sum(m.densities) == pytest.approx(rho) + m.set_density(0.1, 'g/cm3') + assert m.density == pytest.approx(0.1) + def test_new_material(capi_init): with pytest.raises(exc.AllocationError):