Add Python bindings to C API material volume funcs

This commit is contained in:
Paul Romano 2018-08-17 07:28:47 -05:00
parent 8106329fe8
commit c769c35c30
2 changed files with 24 additions and 1 deletions

View file

@ -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]

View file

@ -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)