Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added bin_log_width to EnergyFilter() to help with plotting #2154

Merged

Conversation

shimwell
Copy link
Member

@shimwell shimwell commented Aug 8, 2022

Just wondering if we can add a few lines to the EnergyFilter class that allows one to conveniently access the log bin width.

This can useful when plotting spectra

import matplotlib.pyplot as plt

results = openmc.StatePoint(results_filename)
cell_tally = results.get_tally(name='cell_spectra_tally')
energy_filter = cell_tally.find_filter(filter_type=openmc.filter.EnergyFilter)

bin_boundaries = energy_filter.lethargy_bin_width
flux = cell_tally.mean.flatten()
norm_flux = flux / bin_boundaries

plt.figure()
plt.step(energy_filter.values[:-1], norm_flux)
plt.xscale('log')
plt.yscale('log')
plt.ylabel('Flux per unit lethargy [n/cm2-s]')
plt.show()

@eepeterson
Copy link
Contributor

If you want to add a function like this I'd recommend just a property called lethargy with return np.log(self.bins[:, 1]/self.bins[:, 0])

@paulromano
Copy link
Contributor

@eepeterson I think lethargy is usually defined relative to some reference energy, i.e. something like np.log10(E_reference / energies) where E_reference is a scalar and energies is an array. That is slightly different than what is proposed (ratio of successive energies).

@eepeterson
Copy link
Contributor

eepeterson commented Aug 8, 2022

@eepeterson I think lethargy is usually defined relative to some reference energy, i.e. something like np.log10(E_reference / energies) where E_reference is a scalar and energies is an array. That is slightly different than what is proposed (ratio of successive energies).

You're right, it could be called bin_lethargy to be clearer, but I think the point is to be able to plot spectra with different group structures on the same plot and have them be comparable. Alternatively, you could just use the highest energy bin boundary as the reference and take the difference of the actual lethargies of the energy bin boundaries.

@shimwell
Copy link
Member Author

shimwell commented Aug 8, 2022

Thanks for the feedback, I've renamed the method to bin_lethargy

We could add another method if people want ?

def lethargy(self, E_reference):
    return np.log10(E_reference / self.bins)

Copy link
Contributor

@paulromano paulromano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the line comments, this PR also needs a quick unit test.

openmc/filter.py Outdated Show resolved Hide resolved
openmc/filter.py Outdated Show resolved Hide resolved
shimwell and others added 2 commits August 8, 2022 22:21
Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
@shimwell
Copy link
Member Author

shimwell commented Aug 9, 2022

I would be keen to get rid of the need for users to do np.unique(energy_filter.bins)[:-1] when calling plt.step() as well.

What do people think of adding a def bin_edges(self) property that returns np.unique(self.bins) to make things even easier for the user or is that a step too far 🥁

@paulromano
Copy link
Contributor

@shimwell The values property already gives just an array of the energies. I think that's what you're looking for?

@shimwell
Copy link
Member Author

shimwell commented Aug 9, 2022

Oh yes, that works, thanks. I shall update the example at the top of this PR.

Copy link
Contributor

@paulromano paulromano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@paulromano paulromano merged commit 9060866 into openmc-dev:develop Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants