diff --git a/openmc/model/model.py b/openmc/model/model.py index a512c4d38..0920b79e4 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -2584,9 +2584,16 @@ class Model: # TODO: Can this be done without having to init/finalize? for univ in self.geometry.get_all_universes().values(): if isinstance(univ, openmc.DAGMCUniverse): + # Initialize in stochastic volume mode (non-transport mode) + # This mode doesn't require + # valid transport settings like particles/batches + original_run_mode = self.settings.run_mode + self.settings.run_mode = 'volume' self.init_lib(directory=tmpdir) self.sync_dagmc_universes() self.finalize_lib() + # Restore original run mode + self.settings.run_mode = original_run_mode break # Make sure all materials have a name, and that the name is a valid HDF5 diff --git a/tests/unit_tests/dagmc/test_convert_to_multigroup.py b/tests/unit_tests/dagmc/test_convert_to_multigroup.py new file mode 100644 index 000000000..069dc658e --- /dev/null +++ b/tests/unit_tests/dagmc/test_convert_to_multigroup.py @@ -0,0 +1,53 @@ +"""Test that convert_to_multigroup works with DAGMC models without requiring +particles/batches to be set beforehand. +""" + +from pathlib import Path +import pytest +import openmc +import openmc.lib + +pytestmark = pytest.mark.skipif( + not openmc.lib._dagmc_enabled(), + reason="DAGMC CAD geometry is not enabled.") + + +def test_convert_to_multigroup_without_particles_batches(run_in_tmpdir): + """Test that convert_to_multigroup works with DAGMC model without + setting particles/batches beforehand.""" + openmc.reset_auto_ids() + + mat = openmc.Material(name="mat") + mat.add_nuclide("Fe56", 1.0) + mat.set_density("g/cm3", 7.0) + + # Use minimal tetrahedral DAGMC file + dagmc_file = Path(__file__).parent / "dagmc_tetrahedral_no_graveyard.h5m" + dagmc_univ = openmc.DAGMCUniverse(dagmc_file, auto_geom_ids=True) + bound_dagmc_univ = dagmc_univ.bounded_universe(padding_distance=1) + + # Create model WITHOUT setting particles or batches + model = openmc.Model() + model.materials = openmc.Materials([mat]) + model.geometry = openmc.Geometry(bound_dagmc_univ) + model.settings = openmc.Settings() # Note: no particles or batches set! + + model.settings.run_mode = 'fixed source' + + # Create a point source + my_source = openmc.IndependentSource() + my_source.space = openmc.stats.Point((0.25, 0.25, 0.25)) + my_source.energy = openmc.stats.delta_function(14e6) + model.settings.source = my_source + + # This should work without requiring particles/batches to be set + # convert_to_multigroup handles initialization internally using non-transport mode + model.convert_to_multigroup( + method='material_wise', + groups='CASMO-2', + nparticles=10, + overwrite_mgxs_library=True + ) + + # Verify the model was converted successfully + assert model.settings.energy_mode == 'multi-group'