Skip to content

Commit bcaebb1

Browse files
authored
Merge pull request #38 from fusion-energy/adding_units_and_scale_to_plot
added axis type (log linear) and axis units (MeV, eV) to energy plot
2 parents f015229 + 7552ace commit bcaebb1

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

src/openmc_source_plotter/core.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def sample_initial_particles(self, n_samples: int = 1000, prn_seed: int = None):
3737
materials = openmc.Materials()
3838
model.materials = materials
3939

40-
sph = openmc.Sphere(r=9999999999, boundary_type="vacuum")
40+
sph = openmc.Sphere(r=99999999999, boundary_type="vacuum")
4141
cell = openmc.Cell(region=-sph)
4242
geometry = openmc.Geometry([cell])
4343
model.geometry = geometry
@@ -72,6 +72,9 @@ def plot_source_energy(
7272
prn_seed: int = 1,
7373
energy_bins: typing.Union[str, np.array] = "auto",
7474
name: typing.Optional[str] = None,
75+
yaxis_type: str = "linear",
76+
xaxis_type: str = "linear",
77+
xaxis_units: str = "MeV",
7578
):
7679
"""makes a plot of the initial creation positions of an OpenMC source
7780
@@ -88,14 +91,22 @@ def plot_source_energy(
8891
Numpy bins can also be manually set by passing in a numpy array
8992
of bin edges.
9093
name: the legend name to use
94+
yaxis_type: The type (scale) to use for the Y axis. Options are 'log'
95+
or 'linear.
96+
xaxis_type: The type (scale) to use for the Y axis. Options are 'log'
97+
or 'linear.
98+
xaxis_units: The units to use for the x axis. Options are 'eV' or 'MeV'.
9199
"""
92100

101+
if xaxis_units not in ["eV", "MeV"]:
102+
raise ValueError(f"xaxis_units must be either 'eV' or 'MeV' not {xaxis_units}")
103+
93104
if figure is None:
94105
figure = plotly.graph_objects.Figure()
95106
figure.update_layout(
96107
title="Particle energy",
97-
xaxis={"title": "Energy (eV)"},
98-
yaxis={"title": "Probability"},
108+
xaxis={"title": f"Energy [{xaxis_units}]", "type": xaxis_type},
109+
yaxis={"title": "Probability", "type": yaxis_type},
99110
showlegend=True,
100111
)
101112

@@ -109,11 +120,13 @@ def plot_source_energy(
109120
# scaling by strength
110121
if isinstance(self, openmc.SourceBase):
111122
probability = probability * self.strength
112-
123+
energy = bin_edges[:-1]
124+
if xaxis_units == "MeV":
125+
energy = energy / 1e6
113126
# Plot source energy histogram
114127
figure.add_trace(
115128
plotly.graph_objects.Scatter(
116-
x=bin_edges[:-1],
129+
x=energy,
117130
y=probability * np.diff(bin_edges),
118131
line={"shape": "hv"},
119132
hoverinfo="text",

tests/test_core_with_source.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ def test_energy_plot(test_source):
4848
assert len(plot.data[0]["x"]) == 1
4949

5050

51+
def test_energy_plot_axis(test_source):
52+
plot = test_source.plot_source_energy(
53+
n_samples=10, xaxis_type="log", yaxis_type="linear", xaxis_units="eV"
54+
)
55+
plot = test_source.plot_source_energy(
56+
n_samples=10, xaxis_type="linear", yaxis_type="log", xaxis_units="MeV"
57+
)
58+
assert isinstance(plot, go.Figure)
59+
assert len(plot.data[0]["x"]) == 1
60+
61+
5162
def test_position_plot(test_source):
5263
plot = test_source.plot_source_position(n_samples=10)
5364
assert isinstance(plot, go.Figure)

0 commit comments

Comments
 (0)