Skip to content

Commit

Permalink
Merge branch 'main' of github.com:fusion-energy/openmc_source_plotter…
Browse files Browse the repository at this point in the history
… into main
  • Loading branch information
shimwell committed Jul 11, 2022
2 parents 0eed5ef + ed66434 commit d16613d
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 25 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ pip install openmc_source_plotter
```

temporary fix
For fixed source sources it is currently necessary to use openmc version 0.11
and also to point the ```openmc_exec``` path to the openmc executable
For position and direction plotting with fixed source sources it is currently
necessary to use openmc version 0.11 and also to point the ```openmc_exec```
path to the openmc executable.

This can be installed with:
```bash
conda install -c conda-forge openmc=0.11
Expand Down Expand Up @@ -47,7 +49,6 @@ plot = osp.plot_source_energy(
source=my_source,
number_of_particles=2000,
energy_bins=np.linspace(0, 20e6, 100),
openmc_exec="/home/jshim/miniconda3/envs/openmc_0_11_0/bin/openmc",
)

plot.show()
Expand Down Expand Up @@ -78,7 +79,6 @@ plot = osp.plot_source_energy(
source=[my_dt_source, my_dd_source],
number_of_particles=10000,
energy_bins=np.linspace(0, 20e6, 100),
openmc_exec="/home/jshim/miniconda3/envs/openmc_0_11_0/bin/openmc",
)

plot.show()
Expand Down
1 change: 0 additions & 1 deletion examples/example_plot_source_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
source=my_source,
number_of_particles=10000,
energy_bins=np.linspace(0, 20e6, 100),
openmc_exec="/home/jshim/miniconda3/envs/openmc_0_11_0/bin/openmc",
)

plot.show()
1 change: 0 additions & 1 deletion examples/example_plot_two_source_energies.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
source=[my_dt_source, my_dd_source],
number_of_particles=10000,
energy_bins=np.linspace(0, 20e6, 100),
openmc_exec="/home/jshim/miniconda3/envs/openmc_0_11_0/bin/openmc",
)

plot.show()
24 changes: 6 additions & 18 deletions openmc_source_plotter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
def plot_source_energy(
source: Union[openmc.Source, List[openmc.Source]],
number_of_particles: int = 2000,
openmc_exec="openmc",
energy_bins: np.array = np.linspace(0, 20e6, 50),
energy_bins: Union[np.array, str] = "auto",
filename: str = None,
):
"""makes a plot of the energy distribution OpenMC source(s)
Expand All @@ -25,10 +24,10 @@ def plot_source_energy(
source: The openmc.Source object or list of openmc.Source objects to plot.
number_of_particles: The number of source samples to obtain, more will
take longer but produce a smoother plot.
energy_bins: A numpy array of energy bins to use as energy bins.
openmc_exec: The path of the openmc executable to use
energy_bins: A numpy array of energy bins to use as energy bins. 'Auto'
is also accepted and this picks the bins for you using numpy
filename: the filename to save the plot as should end with the correct
extention supported by matplotlib (e.g .png) or plotly (e.g .html)
extension supported by matplotlib (e.g .png) or plotly (e.g .html)
"""

figure = go.Figure()
Expand All @@ -37,21 +36,11 @@ def plot_source_energy(
source = [source]

for single_source in source:
tmp_filename = tempfile.mkstemp(suffix=".h5", prefix=f"openmc_source_")[1]
create_initial_particles(
source=single_source,
number_of_particles=number_of_particles,
openmc_exec=openmc_exec,
output_source_filename=tmp_filename,
)

print("getting particle data", tmp_filename)
data = get_particle_data(tmp_filename)

e_values = data["e_values"]
e_values = single_source.energy.sample(n_samples=number_of_particles)

# Calculate pdf for source energies
probability, bin_edges = np.histogram(e_values, energy_bins, density=True)
probability, bin_edges = np.histogram(e_values, bins=energy_bins, density=True)

# Plot source energy histogram
figure.add_trace(
Expand All @@ -60,7 +49,6 @@ def plot_source_energy(
y=probability * np.diff(energy_bins),
line={"shape": "hv"},
hoverinfo="text",
name=tmp_filename,
)
)

Expand Down
1 change: 0 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def test_energy_plot(self):
source=self.my_source,
number_of_particles=10000,
energy_bins=np.linspace(0, 20e6, 100),
openmc_exec=self.openmc_exec_dict[self.current_computer],
)
assert isinstance(plot, go.Figure)

Expand Down

0 comments on commit d16613d

Please sign in to comment.