mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-28 14:15:42 -04:00
Replace lib.Material.density property with get_density method
This commit is contained in:
parent
024b1080fd
commit
731f2975bb
2 changed files with 36 additions and 16 deletions
|
|
@ -87,8 +87,6 @@ class Material(_FortranObjectWithID):
|
|||
ID of the material
|
||||
nuclides : list of str
|
||||
List of nuclides in the material
|
||||
density : float
|
||||
Density of the material in [g/cm^3]
|
||||
densities : numpy.ndarray
|
||||
Array of densities in atom/b-cm
|
||||
name : str
|
||||
|
|
@ -176,15 +174,6 @@ 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]
|
||||
|
|
@ -226,6 +215,32 @@ class Material(_FortranObjectWithID):
|
|||
"""
|
||||
_dll.openmc_material_add_nuclide(self._index, name.encode(), density)
|
||||
|
||||
def get_density(self, units='atom/b-cm'):
|
||||
"""Get density of a material.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
units : {'atom/b-cm', 'g/cm3'}
|
||||
Units for density
|
||||
|
||||
Returns
|
||||
-------
|
||||
float
|
||||
Density in requested units
|
||||
|
||||
"""
|
||||
if units == 'atom/b-cm':
|
||||
return self.densities.sum()
|
||||
elif units == 'g/cm3':
|
||||
density = c_double()
|
||||
try:
|
||||
_dll.openmc_material_get_density(self._index, density)
|
||||
except OpenMCError:
|
||||
return None
|
||||
return density.value
|
||||
else:
|
||||
raise ValueError("Units must be 'atom/b-cm' or 'g/cm3'")
|
||||
|
||||
def set_density(self, density, units='atom/b-cm'):
|
||||
"""Set density of a material.
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
from collections.abc import Mapping
|
||||
from ctypes import ArgumentError
|
||||
import os
|
||||
|
||||
import numpy as np
|
||||
|
|
@ -186,7 +187,7 @@ def test_material(lib_init):
|
|||
assert sum(m.densities) == pytest.approx(rho)
|
||||
|
||||
m.set_density(0.1, 'g/cm3')
|
||||
assert m.density == pytest.approx(0.1)
|
||||
assert m.get_density('g/cm3') == pytest.approx(0.1)
|
||||
assert m.name == "Hot borated water"
|
||||
m.name = "Not hot borated water"
|
||||
assert m.name == "Not hot borated water"
|
||||
|
|
@ -194,16 +195,20 @@ def test_material(lib_init):
|
|||
|
||||
def test_properties_density(lib_init):
|
||||
m = openmc.lib.materials[1]
|
||||
orig_density = m.density
|
||||
orig_density = m.get_density('atom/b-cm')
|
||||
orig_density_gpcc = m.get_density('g/cm3')
|
||||
|
||||
# Export properties and change density
|
||||
openmc.lib.export_properties('properties.h5')
|
||||
m.set_density(orig_density*2, 'g/cm3')
|
||||
assert m.density == pytest.approx(orig_density*2)
|
||||
m.set_density(orig_density_gpcc*2, 'g/cm3')
|
||||
assert m.get_density() == pytest.approx(orig_density*2)
|
||||
|
||||
# Import properties and check that density was restored
|
||||
openmc.lib.import_properties('properties.h5')
|
||||
assert m.density == pytest.approx(orig_density)
|
||||
assert m.get_density() == pytest.approx(orig_density)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
m.get_density('🥏')
|
||||
|
||||
|
||||
def test_material_add_nuclide(lib_init):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue