Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions openmc/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ class Material(IDManagerMixin):
temperature : float, optional
Temperature of the material in Kelvin. If not specified, the material
inherits the default temperature applied to the model.
density : float, optional
Density of the material (units defined separately)
density_units : str
Units used for `density`. Can be one of 'g/cm3', 'g/cc', 'kg/m3',
'atom/b-cm', 'atom/cm3', 'sum', or 'macro'. The 'macro' unit only
applies in the case of a multi-group calculation.
depletable : bool, optional
Indicate whether the material is depletable. Defaults to False.
volume : float, optional
Volume of the material in cm^3. This can either be set manually or
calculated in a stochastic volume calculation and added via the
:meth:`Material.add_volume_information` method.

Attributes
----------
Expand Down Expand Up @@ -111,17 +123,26 @@ class Material(IDManagerMixin):
next_id = 1
used_ids = set()

def __init__(self, material_id=None, name='', temperature=None):
def __init__(
self,
material_id: int | None = None,
name: str = "",
temperature: float | None = None,
density: float | None = None,
density_units: str | None = None,
depletable: bool | None = False,
volume: float | None = None,
):
# Initialize class attributes
self.id = material_id
self.name = name
self.temperature = temperature
self._density = None
self._density_units = 'sum'
self._depletable = False
self._depletable = depletable
self._paths = None
self._num_instances = None
self._volume = None
self._volume = volume
self._atoms = {}
self._isotropic = []
self._ncrystal_cfg = None
Expand All @@ -136,6 +157,17 @@ def __init__(self, material_id=None, name='', temperature=None):
# If specified, a list of table names
self._sab = []

# Set density if provided
if (density is not None and density_units is None) or (
density is None and density_units is not None
):
raise ValueError(
"Both density and density_units must be provided together."
)
elif density is not None and density_units is not None:
self.set_density(density_units, density)
Comment on lines 162 to 164
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about having density_units default to "g/cm3"? In that case, the first branch of the if could just be removed.

Copy link
Member Author

@shimwell shimwell Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be happy with that density of 'g/cm3'.

If we make 'g/cm3' the default should we also make the the default units when using material.set_density() which I see defaults to None currently?



def __repr__(self) -> str:
string = 'Material\n'
string += '{: <16}=\t{}\n'.format('\tID', self._id)
Expand Down
31 changes: 31 additions & 0 deletions tests/unit_tests/test_material.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,3 +767,34 @@ def test_mean_free_path():
mat2.add_nuclide('Pb208', 1.0)
mat2.set_density('g/cm3', 11.34)
assert mat2.mean_free_path(energy=14e6) == pytest.approx(5.65, abs=1e-2)


def test_material_from_constructor():

mat1 = openmc.Material(
**{
"material_id": 1,
"name": "neutron_star",
"density": 1e17,
"density_units": "kg/m3",
}
)
assert mat1.id == 1
assert mat1.name == "neutron_star"
assert mat1._density == 1e17
assert mat1._density_units == "kg/m3"
assert mat1.nuclides == []

mat2 = openmc.Material(
material_id=42,
name="plasma",
temperature=None,
density=1e-7,
density_units="g/cm3",
)
assert mat2.id == 42
assert mat2.name == "plasma"
assert mat2.temperature is None
assert mat2.density == 1e-7
assert mat2.density_units == "g/cm3"
assert mat2.nuclides == []
Loading