[docs]
class BlackBodyWeightedSource(BlackBodySimpleSource):
"""
Weighted blackbody packet source for Monte Carlo simulations.
This class generates blackbody packets with energies weighted according
to their contribution to the Planck distribution, rather than uniform
energy distribution.
Parameters
----------
radius : astropy.units.Quantity, optional
Initial packet radius. Default is None.
temperature : astropy.units.Quantity, optional
Absolute temperature. Default is None.
base_seed : int, optional
Base seed for random number generator. Default is None.
legacy_secondary_seed : int, optional
Secondary seed for global numpy rng (Deprecated: Legacy reasons only).
Default is None.
**kwargs : Any
Additional keyword arguments passed to parent class.
Attributes
----------
nus : astropy.units.Quantity
Cached packet frequencies for energy weighting.
"""
hdf_properties = ["radius", "temperature", "base_seed"]
hdf_name = "black_body_weighted_source"
def __init__(self, **kwargs):
"""
Initialize BlackBodyWeightedSource.
Parameters
----------
**kwargs
Additional keyword arguments passed to parent class.
"""
super().__init__(**kwargs)
if self.base_seed is not None:
self._reseed(
self.base_seed
) # Needed because base_source doesn't seed by default
[docs]
def create_packet_nus(self, no_of_packets, l_samples=1000):
"""
Create packet frequencies distributed uniformly over blackbody bounds.
Creates frequencies uniformly distributed over the range of frequencies
that would be generated by the base BlackBodySimpleSource distribution.
Parameters
----------
no_of_packets : int
Number of packets to create.
l_samples : int, optional
Number of l_samples needed for sampling from BlackBodySimpleSource.
Default is 1000.
Returns
-------
astropy.units.Quantity
Array of packet frequencies.
"""
nus = super().create_packet_nus(no_of_packets, l_samples)
nu_min = nus.min()
nu_max = nus.max()
self.nus = (
self.rng.uniform(nu_min.cgs.value, nu_max.cgs.value, no_of_packets)
* nus.unit
)
return self.nus
[docs]
def create_packet_energies(self, no_of_packets):
"""
Create packet energies weighted by Planck distribution.
Set energy weight for each packet from the relative contribution to
the Planck distribution, rather than uniform distribution.
Parameters
----------
no_of_packets : int
Number of packets to create.
Returns
-------
astropy.units.Quantity
Array of weighted packet energies in erg.
"""
if not hasattr(self, "nus"):
self.nus = self.create_packet_nus(no_of_packets)
intensity = intensity_black_body(self.nus.cgs.value, self.temperature)
return intensity / intensity.sum() * u.erg