Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 12 additions & 1 deletion openmc/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ def average_molar_mass(self) -> float:
mass += nuc.percent

# Compute and return the molar mass
if moles == 0.0:
raise ValueError("Material has no nuclides; cannot compute molar mass")
return mass / moles

@property
Expand Down Expand Up @@ -2287,7 +2289,7 @@ def deplete(
multigroup_fluxes: Sequence[Sequence[float]]
Energy-dependent multigroup flux values, where each sublist corresponds
to a specific material. Will be normalized so that it sums to 1.
energy_group_structures': Sequence[Sequence[float] | str]
energy_group_structures: Sequence[Sequence[float] | str]
Energy group boundaries in [eV] or the name of the group structure.
timesteps : iterable of float or iterable of tuple
Array of timesteps. Note that values are not cumulative. The units are
Expand Down Expand Up @@ -2322,6 +2324,11 @@ def deplete(
for mat in self:
mat.depletable = True

if len(multigroup_fluxes) != len(self):
raise ValueError("multigroup_fluxes length must match number of materials")
if len(energy_group_structures) != len(self):
raise ValueError("energy_group_structures length must match number of materials")

chain = _get_chain(chain_file)

# Create MicroXS objects for all materials
Expand All @@ -2332,6 +2339,10 @@ def deplete(
for material, flux, energy in zip(
self, multigroup_fluxes, energy_group_structures
):
if material.volume is None:
raise ValueError(
f"Material {material.id} has no volume; cannot deplete"
)
temperature = material.temperature or 293.6
micro_xs = openmc.deplete.MicroXS.from_multigroup_flux(
energies=energy,
Expand Down
49 changes: 49 additions & 0 deletions tests/unit_tests/test_materials.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pathlib import Path

import pytest

import openmc
from openmc.deplete import Chain

Expand Down Expand Up @@ -78,3 +80,50 @@ def test_export_duplicate_materials_to_xml(run_in_tmpdir):

materials_in = openmc.Materials.from_xml("materials.xml")
assert len(materials_in) == 2


def test_materials_deplete_length_mismatch():
mats = openmc.Materials([openmc.Material()])

with pytest.raises(ValueError, match="multigroup_fluxes length"):
mats.deplete(
multigroup_fluxes=[],
energy_group_structures=["VITAMIN-J-42"],
timesteps=[1.0],
source_rates=1.0,
)

with pytest.raises(ValueError, match="energy_group_structures length"):
mats.deplete(
multigroup_fluxes=[[1.0]],
energy_group_structures=[],
timesteps=[1.0],
source_rates=1.0,
)


def test_materials_deplete_missing_volume(monkeypatch):
mat = openmc.Material()
mat.add_nuclide("Ni58", 1.0)
mat.set_density("g/cm3", 7.87)

mats = openmc.Materials([mat])

class DummySession:
def __enter__(self):
return self

def __exit__(self, exc_type, exc, tb):
return False

monkeypatch.setattr(openmc.lib, "TemporarySession", DummySession)

chain = Path(__file__).parents[1] / "chain_ni.xml"
with pytest.raises(ValueError, match="has no volume"):
mats.deplete(
multigroup_fluxes=[[1.0]],
energy_group_structures=["VITAMIN-J-42"],
timesteps=[1.0],
source_rates=1.0,
chain_file=chain,
)
Loading