diff --git a/openmc/material.py b/openmc/material.py index 770722f748a..c6765178e9d 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -1718,6 +1718,36 @@ def from_xml_element(cls, elem: ET.Element) -> Material: return mat + def mean_free_path(self, energy: float) -> float: + """Calculate the mean free path of neutrons in the material at a given + energy. + + .. versionadded:: 0.15.3 + + Parameters + ---------- + energy : float + Neutron energy in eV + + Returns + ------- + float + Mean free path in cm + + """ + from openmc.plotter import _calculate_cexs_elem_mat + + energy_grid, cexs = _calculate_cexs_elem_mat( + this=self, + types=["total"], + ) + total_cexs = cexs[0] + + interpolated_cexs = float(np.interp(energy, energy_grid, total_cexs)) + + return 1.0 / interpolated_cexs + + class Materials(cv.CheckedList): """Collection of Materials used for an OpenMC simulation. diff --git a/tests/unit_tests/test_material.py b/tests/unit_tests/test_material.py index 8ad57956381..db5f4ce32d5 100644 --- a/tests/unit_tests/test_material.py +++ b/tests/unit_tests/test_material.py @@ -710,3 +710,16 @@ def test_avoid_subnormal(run_in_tmpdir): # When read back in, the density should be zero mats = openmc.Materials.from_xml() assert mats[0].get_nuclide_atom_densities()['H2'] == 0.0 + + +def test_mean_free_path(): + + mat1 = openmc.Material() + mat1.add_nuclide('Si28', 1.0) + mat1.set_density('g/cm3', 2.32) + assert mat1.mean_free_path(energy=14e6) == pytest.approx(11.41, abs=1e-2) + + mat2 = openmc.Material() + 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)