Skip to content

Commit 7b4617a

Browse files
GuyStenpaulromano
andauthored
Fix for plotting model with multi-group cross sections (#3748)
Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
1 parent f7a7341 commit 7b4617a

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

openmc/model/model.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,16 @@ def plot(
11601160
y_min = (origin[y] - 0.5*width[1]) * axis_scaling_factor[axis_units]
11611161
y_max = (origin[y] + 0.5*width[1]) * axis_scaling_factor[axis_units]
11621162

1163+
# Determine whether any materials contains macroscopic data and if so,
1164+
# set energy mode accordingly and check that mg cross sections path is accessible
1165+
for mat in self.geometry.get_all_materials().values():
1166+
if mat._macroscopic is not None:
1167+
self.settings.energy_mode = 'multi-group'
1168+
if 'mg_cross_sections' not in openmc.config:
1169+
raise RuntimeError("'mg_cross_sections' path must be set in "
1170+
"openmc.config before plotting.")
1171+
break
1172+
11631173
# Get ID map from the C API
11641174
id_map = self.id_map(
11651175
origin=origin,

tests/unit_tests/test_universe.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import Path
2+
13
import lxml.etree as ET
24
import numpy as np
35
import openmc
@@ -104,6 +106,43 @@ def test_plot(run_in_tmpdir, sphere_model):
104106
plt.close('all')
105107

106108

109+
def test_mg_plot(run_in_tmpdir):
110+
# Create a simple universe with macroscopic data
111+
h2o_data = openmc.Macroscopic('LWTR')
112+
water = openmc.Material(name='Water')
113+
water.set_density('macro', 1.0)
114+
water.add_macroscopic(h2o_data)
115+
sph = openmc.Sphere(r=10, boundary_type="vacuum")
116+
cell = openmc.Cell(region=-sph, fill=water)
117+
univ = openmc.Universe(cells=[cell])
118+
119+
# Create MGXS library and export to HDF5
120+
groups = openmc.mgxs.EnergyGroups([1e-5, 20.0e6])
121+
h2o_xsdata = openmc.XSdata('LWTR', groups)
122+
h2o_xsdata.order = 0
123+
h2o_xsdata.set_total([1.0])
124+
h2o_xsdata.set_absorption([0.5])
125+
scatter_matrix = np.array([[[0.5]]])
126+
scatter_matrix = np.rollaxis(scatter_matrix, 0, 3)
127+
h2o_xsdata.set_scatter_matrix(scatter_matrix)
128+
mg_library = openmc.MGXSLibrary(groups)
129+
mg_library.add_xsdatas([h2o_xsdata])
130+
mgxs_path = Path.cwd() / 'mgxs.h5'
131+
mg_library.export_to_hdf5(mgxs_path)
132+
133+
# Set MG cross sections in config and plot
134+
with openmc.config.patch('mg_cross_sections', mgxs_path):
135+
univ.plot(width=(200, 200), basis='yz', color_by='cell')
136+
univ.plot(width=(200, 200), basis='yz', color_by='material')
137+
138+
with pytest.raises(RuntimeError):
139+
univ.plot(width=(200, 200), basis='yz', color_by='cell')
140+
141+
# Close plots to avoid warning
142+
import matplotlib.pyplot as plt
143+
plt.close('all')
144+
145+
107146
def test_get_nuclides(uo2):
108147
c = openmc.Cell(fill=uo2)
109148
univ = openmc.Universe(cells=[c])

0 commit comments

Comments
 (0)