From 291797637f8af2c5bf200004820fd957b0e1e970 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Mon, 11 Jul 2022 17:43:49 +0100 Subject: [PATCH 1/3] added energy from sample method --- README.md | 8 ++++---- openmc_source_plotter/core.py | 24 ++++++------------------ tests/test_core.py | 1 - 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index f15a659..7b04a1a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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() @@ -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() diff --git a/openmc_source_plotter/core.py b/openmc_source_plotter/core.py index 6e061b0..3352c0c 100644 --- a/openmc_source_plotter/core.py +++ b/openmc_source_plotter/core.py @@ -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) @@ -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() @@ -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( @@ -60,7 +49,6 @@ def plot_source_energy( y=probability * np.diff(energy_bins), line={"shape": "hv"}, hoverinfo="text", - name=tmp_filename, ) ) diff --git a/tests/test_core.py b/tests/test_core.py index b2b17bf..f901458 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -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) From cfa7ee2aa30703a7115681c3f984e12dd108e05a Mon Sep 17 00:00:00 2001 From: shimwell Date: Mon, 11 Jul 2022 16:44:33 +0000 Subject: [PATCH 2/3] [skip ci] Apply formatting changes --- openmc_source_plotter/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc_source_plotter/core.py b/openmc_source_plotter/core.py index 3352c0c..4267dd6 100644 --- a/openmc_source_plotter/core.py +++ b/openmc_source_plotter/core.py @@ -15,7 +15,7 @@ def plot_source_energy( source: Union[openmc.Source, List[openmc.Source]], number_of_particles: int = 2000, - energy_bins: Union[np.array, str] = 'auto', + energy_bins: Union[np.array, str] = "auto", filename: str = None, ): """makes a plot of the energy distribution OpenMC source(s) From 0cbcb778b802c39081baff740c8bbecf7d0fbcec Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Mon, 11 Jul 2022 17:47:40 +0100 Subject: [PATCH 3/3] updated examples to work without openmc_exec for energy plots --- examples/example_plot_source_energy.py | 1 - examples/example_plot_two_source_energies.py | 1 - 2 files changed, 2 deletions(-) diff --git a/examples/example_plot_source_energy.py b/examples/example_plot_source_energy.py index 0c5b221..74f0c9c 100644 --- a/examples/example_plot_source_energy.py +++ b/examples/example_plot_source_energy.py @@ -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() diff --git a/examples/example_plot_two_source_energies.py b/examples/example_plot_two_source_energies.py index 2bd0dfd..ae4dd75 100644 --- a/examples/example_plot_two_source_energies.py +++ b/examples/example_plot_two_source_energies.py @@ -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()