Alexandre was just showing me this thread and walked me through how to make a source from a spectrum tally. I refactored the code into a method that could potentially be added to the EnergyFilter class in openmc/filter.py file. This makes it quite convenient when generating Tabular.
def get_tabular(self, values):
"""Creates a openmc.stats.Tabular distribution using the EnergyFilter
bins and the provided values. Intended use is to help convert a
spectrum tally into a source.
Parameters
----------
values : np.array
Array of numeric values, typically a tally.mean from a spectrum tally
Returns
-------
openmc.stats.Tabular
Tabular distribution with histogram interpolation
"""
spectrum_probability = values / sum(values)
proberbility_per_ev = spectrum_probability / np.diff(self.bins).flatten()
return openmc.stats.Tabular(
x = self.bins,
p=proberbility_per_ev,
interpolation='histogram'
)
example usage with spectrum tally
cell_tally = results.get_tally(name='cell_spectra_tally')
tab = energy_filter.get_tabular(values=cell_tally.mean.flatten())
source = openmc.Source()
source.energy = tab