Skip to content

Commit

Permalink
Adding energy axis units to plot xs (#2876)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
  • Loading branch information
shimwell and paulromano committed Feb 29, 2024
1 parent 02c0a09 commit b75ccd5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
18 changes: 15 additions & 3 deletions openmc/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _get_title(reactions):
def plot_xs(reactions, divisor_types=None, temperature=294., axis=None,
sab_name=None, ce_cross_sections=None, mg_cross_sections=None,
enrichment=None, plot_CE=True, orders=None, divisor_orders=None,
**kwargs):
energy_axis_units="eV", **kwargs):
"""Creates a figure of continuous-energy cross sections for this item.
Parameters
Expand Down Expand Up @@ -148,6 +148,10 @@ def plot_xs(reactions, divisor_types=None, temperature=294., axis=None,
**kwargs :
All keyword arguments are passed to
:func:`matplotlib.pyplot.figure`.
energy_axis_units : {'eV', 'keV', 'MeV'}
Units used on the plot energy axis
.. versionadded:: 0.14.1
Returns
-------
Expand All @@ -161,6 +165,9 @@ def plot_xs(reactions, divisor_types=None, temperature=294., axis=None,
import matplotlib.pyplot as plt

cv.check_type("plot_CE", plot_CE, bool)
cv.check_value("energy_axis_units", energy_axis_units, {"eV", "keV", "MeV"})

axis_scaling_factor = {"eV": 1.0, "keV": 1e-3, "MeV": 1e-6}

# Generate the plot
if axis is None:
Expand Down Expand Up @@ -217,6 +224,8 @@ def plot_xs(reactions, divisor_types=None, temperature=294., axis=None,
if divisor_types[line] != 'unity':
types[line] += ' / ' + divisor_types[line]

E *= axis_scaling_factor[energy_axis_units]

# Plot the data
for i in range(len(data)):
data[i, :] = np.nan_to_num(data[i, :])
Expand All @@ -232,9 +241,12 @@ def plot_xs(reactions, divisor_types=None, temperature=294., axis=None,
ax.set_xscale('log')
ax.set_yscale('log')

ax.set_xlabel('Energy [eV]')
ax.set_xlabel(f"Energy [{energy_axis_units}]")
if plot_CE:
ax.set_xlim(_MIN_E, _MAX_E)
ax.set_xlim(
_MIN_E * axis_scaling_factor[energy_axis_units],
_MAX_E * axis_scaling_factor[energy_axis_units],
)
else:
ax.set_xlim(E[-1], E[0])

Expand Down
11 changes: 10 additions & 1 deletion tests/unit_tests/test_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ def test_plot_xs(this):
from matplotlib.figure import Figure
assert isinstance(openmc.plot_xs({this: ['total', 'elastic']}), Figure)


def test_plot_xs_mat(test_mat):
from matplotlib.figure import Figure
assert isinstance(openmc.plot_xs({test_mat: ['total']}), Figure)


@pytest.mark.parametrize("units", ["eV", "keV", "MeV"])
def test_plot_xs_energy_axis(units):
plot = openmc.plot_xs({'Be9': ['(n,2n)']}, energy_axis_units=units)
axis_text = plot.get_axes()[0].get_xaxis().get_label().get_text()
assert axis_text == f'Energy [{units}]'


def test_plot_axes_labels():
# just nuclides
axis_label = openmc.plotter._get_yaxis_label(
Expand Down Expand Up @@ -156,4 +165,4 @@ def test_get_title():
mat1.set_density('g/cm3', 1)
mat1.name = 'my_mat'
title = openmc.plotter._get_title(reactions={mat1: [205]})
assert title == 'Cross Section Plot For my_mat'
assert title == 'Cross Section Plot For my_mat'

0 comments on commit b75ccd5

Please sign in to comment.