From 3247587d4920fa3c9a4e3e3da79adc20e23eb1f7 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Thu, 2 Jul 2026 11:54:12 -0500 Subject: [PATCH] Ensure photon cross sections are loaded when FileSource contains photons (#3988) --- src/source.cpp | 9 ++++++++- tests/unit_tests/test_source_file.py | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/source.cpp b/src/source.cpp index bee20d5c3..1e783b623 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -523,9 +523,16 @@ void FileSource::load_sites_from_file(const std::string& path) file_close(file_id); } - // Make sure particles in source file have valid types + // Make sure particles in source file have valid types. If any particle is a + // photon, electron, or positron, enable photon transport so that the + // appropriate cross sections are loaded. for (const auto& site : this->sites_) { validate_particle_type(site.particle, "FileSource"); + if (site.particle == ParticleType::photon() || + site.particle == ParticleType::electron() || + site.particle == ParticleType::positron()) { + settings::photon_transport = true; + } } } diff --git a/tests/unit_tests/test_source_file.py b/tests/unit_tests/test_source_file.py index 3e84fbe51..f19ea74a6 100644 --- a/tests/unit_tests/test_source_file.py +++ b/tests/unit_tests/test_source_file.py @@ -145,3 +145,27 @@ def test_source_file_transport(run_in_tmpdir): # Try running OpenMC model.run() + + +def test_source_file_photon_transport(run_in_tmpdir): + # Create a source file containing a photon. Note that photon_transport is + # not explicitly enabled in the settings -- it should be turned on + # automatically because the source file contains a photon. + particle = openmc.SourceParticle(E=1.0e6, particle='photon') + openmc.write_source_file([particle], 'photon_source.h5') + + # Create simple model to use the photon source file + model = openmc.Model() + al = openmc.Material() + al.add_element('Al', 1.0) + al.set_density('g/cm3', 2.7) + sph = openmc.Sphere(r=10.0, boundary_type='vacuum') + cell = openmc.Cell(fill=al, region=-sph) + model.geometry = openmc.Geometry([cell]) + model.settings.source = openmc.FileSource(path='photon_source.h5') + model.settings.particles = 10 + model.settings.batches = 3 + model.settings.run_mode = 'fixed source' + + # Running OpenMC should succeed + model.run()