Fix for plotting model with multi-group cross sections (#3748)

Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
This commit is contained in:
GuySten 2026-01-30 08:10:16 +02:00 committed by GitHub
parent f7a734189a
commit 7b4617affb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 49 additions and 0 deletions

View file

@ -1160,6 +1160,16 @@ class Model:
y_min = (origin[y] - 0.5*width[1]) * axis_scaling_factor[axis_units]
y_max = (origin[y] + 0.5*width[1]) * axis_scaling_factor[axis_units]
# Determine whether any materials contains macroscopic data and if so,
# set energy mode accordingly and check that mg cross sections path is accessible
for mat in self.geometry.get_all_materials().values():
if mat._macroscopic is not None:
self.settings.energy_mode = 'multi-group'
if 'mg_cross_sections' not in openmc.config:
raise RuntimeError("'mg_cross_sections' path must be set in "
"openmc.config before plotting.")
break
# Get ID map from the C API
id_map = self.id_map(
origin=origin,

View file

@ -1,3 +1,5 @@
from pathlib import Path
import lxml.etree as ET
import numpy as np
import openmc
@ -104,6 +106,43 @@ def test_plot(run_in_tmpdir, sphere_model):
plt.close('all')
def test_mg_plot(run_in_tmpdir):
# Create a simple universe with macroscopic data
h2o_data = openmc.Macroscopic('LWTR')
water = openmc.Material(name='Water')
water.set_density('macro', 1.0)
water.add_macroscopic(h2o_data)
sph = openmc.Sphere(r=10, boundary_type="vacuum")
cell = openmc.Cell(region=-sph, fill=water)
univ = openmc.Universe(cells=[cell])
# Create MGXS library and export to HDF5
groups = openmc.mgxs.EnergyGroups([1e-5, 20.0e6])
h2o_xsdata = openmc.XSdata('LWTR', groups)
h2o_xsdata.order = 0
h2o_xsdata.set_total([1.0])
h2o_xsdata.set_absorption([0.5])
scatter_matrix = np.array([[[0.5]]])
scatter_matrix = np.rollaxis(scatter_matrix, 0, 3)
h2o_xsdata.set_scatter_matrix(scatter_matrix)
mg_library = openmc.MGXSLibrary(groups)
mg_library.add_xsdatas([h2o_xsdata])
mgxs_path = Path.cwd() / 'mgxs.h5'
mg_library.export_to_hdf5(mgxs_path)
# Set MG cross sections in config and plot
with openmc.config.patch('mg_cross_sections', mgxs_path):
univ.plot(width=(200, 200), basis='yz', color_by='cell')
univ.plot(width=(200, 200), basis='yz', color_by='material')
with pytest.raises(RuntimeError):
univ.plot(width=(200, 200), basis='yz', color_by='cell')
# Close plots to avoid warning
import matplotlib.pyplot as plt
plt.close('all')
def test_get_nuclides(uo2):
c = openmc.Cell(fill=uo2)
univ = openmc.Universe(cells=[c])