From c769c35c302a882f4bb326c8310c22a0a8895886 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Fri, 17 Aug 2018 07:28:47 -0500 Subject: [PATCH] Add Python bindings to C API material volume funcs --- openmc/capi/material.py | 21 ++++++++++++++++++++- tests/unit_tests/test_capi.py | 4 ++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/openmc/capi/material.py b/openmc/capi/material.py index a6c29a3751..9548c0b79f 100644 --- a/openmc/capi/material.py +++ b/openmc/capi/material.py @@ -5,7 +5,7 @@ from weakref import WeakValueDictionary import numpy as np from numpy.ctypeslib import as_array -from openmc.exceptions import AllocationError, InvalidIDError +from openmc.exceptions import AllocationError, InvalidIDError, OpenMCError from . import _dll, Nuclide from .core import _FortranObjectWithID from .error import _error_handler @@ -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_volume.argtypes = [c_int32, POINTER(c_double)] +_dll.openmc_material_get_volume.restype = c_int +_dll.openmc_material_get_volume.errcheck = _error_handler _dll.openmc_material_set_density.argtypes = [c_int32, c_double] _dll.openmc_material_set_density.restype = c_int _dll.openmc_material_set_density.errcheck = _error_handler @@ -42,6 +45,9 @@ _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_material_set_volume.argtypes = [c_int32, c_double] +_dll.openmc_material_set_volume.restype = c_int +_dll.openmc_material_set_volume.errcheck = _error_handler class Material(_FortranObjectWithID): @@ -113,6 +119,19 @@ class Material(_FortranObjectWithID): def id(self, mat_id): _dll.openmc_material_set_id(self._index, mat_id) + @property + def volume(self): + volume = c_double() + try: + _dll.openmc_material_get_volume(self._index, volume) + except OpenMCError: + return None + return volume.value + + @volume.setter + def volume(self, volume): + _dll.openmc_material_set_volume(self._index, volume) + @property def nuclides(self): return self._get_densities()[0] diff --git a/tests/unit_tests/test_capi.py b/tests/unit_tests/test_capi.py index 92ca0aca9e..fa348f59bd 100644 --- a/tests/unit_tests/test_capi.py +++ b/tests/unit_tests/test_capi.py @@ -101,6 +101,10 @@ def test_material(capi_init): m.set_densities(m.nuclides, test_dens) assert m.densities == pytest.approx(test_dens) + assert m.volume is None + m.volume = 10.0 + assert m.volume == 10.0 + rho = 2.25e-2 m.set_density(rho) assert sum(m.densities) == pytest.approx(rho)