Lines from Galaxy objects

To generate lines from components of a Galaxy (i.e. parametric or particle based stars or black holes) synthesizer provides get_lines methods on component and galaxy objects. These methods are analogous to those on a grid, and return a LineCollection containing the requested lines (which can either be singular, doublets, triplets, or more).

[1]:
from unyt import Msun, Myr, kelvin

from synthesizer.emission_models import BimodalPacmanEmission
from synthesizer.emission_models.attenuation import PowerLaw
from synthesizer.emission_models.dust.emission import Blackbody
from synthesizer.emissions.utils import O2, O3, Hb, O3b, O3r
from synthesizer.grid import Grid
from synthesizer.parametric import SFH, Stars, ZDist

# Get a grid
grid_dir = "../../../tests/test_grid"
grid_name = "test_grid"
grid = Grid(grid_name, grid_dir=grid_dir)

# Make a parametric galaxy
stellar_mass = 10**12 * Msun
sfh = SFH.Constant(max_age=100 * Myr)
metal_dist = ZDist.Normal(mean=0.01, sigma=0.05)
stars = Stars(
    grid.log10age,
    grid.metallicity,
    sf_hist=sfh,
    metal_dist=metal_dist,
    initial_mass=stellar_mass,
)

# Set up the emission model
model = BimodalPacmanEmission(
    grid=grid,
    tau_v_ism=0.5,
    tau_v_birth=0.7,
    dust_curve_ism=PowerLaw(slope=-1.3),
    dust_curve_birth=PowerLaw(slope=-0.7),
    dust_emission_ism=Blackbody(temperature=100 * kelvin),
    dust_emission_birth=Blackbody(temperature=30 * kelvin),
    fesc=0.2,
    fesc_ly_alpha=0.9,
)

# To get the dust emission at a line we need to first run the
# spectra generation
stars.get_spectra(model)

# Get the lines
lines = stars.get_lines((Hb, O3r, O3b), model)

print(stars.lines["emergent"])
+--------------------------------------------------------------------------------+
|                                 LINECOLLECTION                                 |
+--------------------+-----------------------------------------------------------+
| Attribute          | Value                                                     |
+--------------------+-----------------------------------------------------------+
| nlines             | 3                                                         |
+--------------------+-----------------------------------------------------------+
| ndim               | 1                                                         |
+--------------------+-----------------------------------------------------------+
| nlam               | 3                                                         |
+--------------------+-----------------------------------------------------------+
| shape              | (3,)                                                      |
+--------------------+-----------------------------------------------------------+
| available_ratios   | [R3, ]                                                    |
+--------------------+-----------------------------------------------------------+
| elements           | [H, O, O]                                                 |
+--------------------+-----------------------------------------------------------+
| line_ids           | ['H 1 4861.32A' 'O 3 5006.84A' 'O 3 4958.91A']            |
+--------------------+-----------------------------------------------------------+
| lam                | [4861.32 5006.84 4958.91] Å                               |
+--------------------+-----------------------------------------------------------+
| luminosity         | [2.41350821e+44 8.44537141e+44 2.79460058e+44] erg/s      |
+--------------------+-----------------------------------------------------------+
| continuum          | [4.46107446e+31 5.39344450e+31 5.41298431e+31] erg/(Hz*s) |
+--------------------+-----------------------------------------------------------+
| cont               | [4.46107446e+31 5.39344450e+31 5.41298431e+31] erg/(Hz*s) |
+--------------------+-----------------------------------------------------------+
| continuum_llam     | [5.65915717e+42 6.44999667e+42 6.59910470e+42] erg/(s*Å)  |
+--------------------+-----------------------------------------------------------+
| energy             | [2.55042238 2.47629629 2.50023076] eV                     |
+--------------------+-----------------------------------------------------------+
| equivalent_width   | [ 42.64783846 130.93605845  42.34817757] Å                |
+--------------------+-----------------------------------------------------------+
| lum                | [2.41350821e+44 8.44537141e+44 2.79460058e+44] erg/s      |
+--------------------+-----------------------------------------------------------+
| nu                 | [6.16689414e+14 5.98765804e+14 6.04553134e+14] Hz         |
+--------------------+-----------------------------------------------------------+
| vacuum_wavelengths | [4862.6779924  5008.23663693 4960.29390118] Å             |
+--------------------+-----------------------------------------------------------+
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/unyt/array.py:1832: RuntimeWarning: overflow encountered in exp
  out_arr = func(np.asarray(inp), out=out_func, **kwargs)
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/unyt/array.py:1972: RuntimeWarning: overflow encountered in multiply
  out_arr = func(

In the case of a particle based galaxy you can either get the integrated line emission or per-particle line emission (by using a per particle model) with get_lines method.

[2]:
from synthesizer.emission_models import PacmanEmission
from synthesizer.load_data.load_camels import load_CAMELS_IllustrisTNG

# Get the stars from a particle based galaxy
stars = load_CAMELS_IllustrisTNG(
    "../../../tests/data/",
    snap_name="camels_snap.hdf5",
    group_name="camels_subhalo.hdf5",
    physical=True,
)[0].stars

# Set up the emission model
model = PacmanEmission(
    grid=grid,
    tau_v=0.7,
    dust_curve=PowerLaw(slope=-1.3),
    dust_emission=Blackbody(temperature=50 * kelvin),
    fesc=0.5,
    fesc_ly_alpha=1.0,
    per_particle=True,
)

# Get the spectra and lines
print(stars.nstars)
stars.get_spectra(model)
stars.get_lines((Hb, O3r, O3b, O2, O3), model)
print(stars.lines["emergent"])
print(stars.particle_lines["emergent"])
278
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/unyt/array.py:1972: RuntimeWarning: invalid value encountered in divide
  out_arr = func(
+-----------------------------------------------------------------------------------------------+
|                                        LINECOLLECTION                                         |
+-------------------------+---------------------------------------------------------------------+
| Attribute               | Value                                                               |
+-------------------------+---------------------------------------------------------------------+
| nlines                  | 5                                                                   |
+-------------------------+---------------------------------------------------------------------+
| ndim                    | 1                                                                   |
+-------------------------+---------------------------------------------------------------------+
| nlam                    | 5                                                                   |
+-------------------------+---------------------------------------------------------------------+
| shape                   | (5,)                                                                |
+-------------------------+---------------------------------------------------------------------+
| available_ratios        | [R2, R3, O32]                                                       |
+-------------------------+---------------------------------------------------------------------+
| elements                | [H, O, O, O, O, O, O]                                               |
+-------------------------+---------------------------------------------------------------------+
| line_ids (5,)           | [H 1 4861.32A, O 3 5006.84A, O 3 4958.91A, ...]                     |
+-------------------------+---------------------------------------------------------------------+
| lam (5,)                | 3.73e+03 Å -> 5.01e+03 Å (Mean: 4.71e+03 Å)                         |
+-------------------------+---------------------------------------------------------------------+
| luminosity (5,)         | 6.90e+32 erg/s -> 2.96e+37 erg/s (Mean: 6.16e+36 erg/s)             |
+-------------------------+---------------------------------------------------------------------+
| continuum (5,)          | 5.09e+27 erg/(Hz*s) -> 1.54e+28 erg/(Hz*s) (Mean: 8.43e+27 erg)     |
+-------------------------+---------------------------------------------------------------------+
| cont (5,)               | 5.09e+27 erg/(Hz*s) -> 1.54e+28 erg/(Hz*s) (Mean: 8.43e+27 erg)     |
+-------------------------+---------------------------------------------------------------------+
| continuum_llam (5,)     | 8.00e+38 erg/(s*Å) -> 1.86e+39 erg/(s*Å) (Mean: 1.12e+39 erg/(s*Å)) |
+-------------------------+---------------------------------------------------------------------+
| energy (5,)             | 2.48e+00 eV -> 3.33e+00 eV (Mean: 2.67e+00 eV)                      |
+-------------------------+---------------------------------------------------------------------+
| equivalent_width (5,)   | 7.34e-07 Å -> 3.70e-02 Å (Mean: 7.61e-03 Å)                         |
+-------------------------+---------------------------------------------------------------------+
| lum (5,)                | 6.90e+32 erg/s -> 2.96e+37 erg/s (Mean: 6.16e+36 erg/s)             |
+-------------------------+---------------------------------------------------------------------+
| nu (5,)                 | 5.99e+14 Hz -> 8.04e+14 Hz (Mean: 6.45e+14 Hz)                      |
+-------------------------+---------------------------------------------------------------------+
| vacuum_wavelengths (5,) | 3.73e+03 Å -> 5.01e+03 Å (Mean: 4.71e+03 Å)                         |
+-------------------------+---------------------------------------------------------------------+
+-------------------------------------------------------------------------------------------------+
|                                         LINECOLLECTION                                          |
+---------------------------+---------------------------------------------------------------------+
| Attribute                 | Value                                                               |
+---------------------------+---------------------------------------------------------------------+
| nlines                    | 5                                                                   |
+---------------------------+---------------------------------------------------------------------+
| ndim                      | 2                                                                   |
+---------------------------+---------------------------------------------------------------------+
| nlam                      | 5                                                                   |
+---------------------------+---------------------------------------------------------------------+
| shape                     | (278, 5)                                                            |
+---------------------------+---------------------------------------------------------------------+
| available_ratios          | [R2, R3, O32]                                                       |
+---------------------------+---------------------------------------------------------------------+
| elements                  | [H, O, O, O, O, O, O]                                               |
+---------------------------+---------------------------------------------------------------------+
| line_ids (5,)             | [H 1 4861.32A, O 3 5006.84A, O 3 4958.91A, ...]                     |
+---------------------------+---------------------------------------------------------------------+
| lam (5,)                  | 3.73e+03 Å -> 5.01e+03 Å (Mean: 4.71e+03 Å)                         |
+---------------------------+---------------------------------------------------------------------+
| luminosity (278, 5)       | 1.41e+22 erg/s -> 8.81e+35 erg/s (Mean: 2.21e+34 erg/s)             |
+---------------------------+---------------------------------------------------------------------+
| continuum (278, 5)        | 2.70e+24 erg/(Hz*s) -> 3.35e+26 erg/(Hz*s) (Mean: 3.03e+25 erg)     |
+---------------------------+---------------------------------------------------------------------+
| cont (278, 5)             | 2.70e+24 erg/(Hz*s) -> 3.35e+26 erg/(Hz*s) (Mean: 3.03e+25 erg)     |
+---------------------------+---------------------------------------------------------------------+
| continuum_llam (278, 5)   | 5.82e+35 erg/(s*Å) -> 4.05e+37 erg/(s*Å) (Mean: 4.04e+36 erg/(s*Å)) |
+---------------------------+---------------------------------------------------------------------+
| energy (5,)               | 2.48e+00 eV -> 3.33e+00 eV (Mean: 2.67e+00 eV)                      |
+---------------------------+---------------------------------------------------------------------+
| equivalent_width (278, 5) | 5.21e-15 Å -> 8.65e-01 Å (Mean: 8.81e-03 Å)                         |
+---------------------------+---------------------------------------------------------------------+
| lum (278, 5)              | 1.41e+22 erg/s -> 8.81e+35 erg/s (Mean: 2.21e+34 erg/s)             |
+---------------------------+---------------------------------------------------------------------+
| nu (5,)                   | 5.99e+14 Hz -> 8.04e+14 Hz (Mean: 6.45e+14 Hz)                      |
+---------------------------+---------------------------------------------------------------------+
| vacuum_wavelengths (5,)   | 3.73e+03 Å -> 5.01e+03 Å (Mean: 4.71e+03 Å)                         |
+---------------------------+---------------------------------------------------------------------+
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/unyt/array.py:1832: RuntimeWarning: overflow encountered in exp
  out_arr = func(np.asarray(inp), out=out_func, **kwargs)
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/unyt/array.py:1972: RuntimeWarning: overflow encountered in multiply
  out_arr = func(

The line luminosities themselves are extracted and stored in the "nebular" key of the lines dictionaries. Below we plot the line luminosities and continuum luminosities to compare the different emissions.

Note that we have skipped any lines with little or no contribution.

[3]:
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.grid(True)
ax.set_axisbelow(True)

# Get the colours
colors = {}
for ind, key in enumerate(stars.lines):
    colors[key] = plt.cm.tab10(ind)

for key in stars.lines:
    # Don't plot dust emission since it's effectively 0
    # for these lines
    if key == "dust_emission":
        continue
    ax.semilogy(
        stars.lines[key].lam,
        stars.lines[key].continuum,
        color=colors[key],
        marker="+",
        linestyle="None",
    )

ax.set_xlabel("Wavelength (Angstrom)")
ax.set_ylabel("Continuum (erg/s/Hz)")

# Create the legend
legend_handles = []
for key in stars.lines:
    # Don't plot dust emission since it's effectively 0
    # for these lines
    if key == "dust_emission":
        continue
    legend_handles.append(
        plt.Line2D(
            [0],
            [0],
            color=colors[key],
            label=key,
            marker="+",
            linestyle="None",
        )
    )

ax.legend(
    handles=legend_handles,
    loc="upper center",
    bbox_to_anchor=(0.5, -0.15),
    ncol=3,
)

plt.show()

fig = plt.figure()
ax = fig.add_subplot(111)
ax.grid(True)
ax.set_axisbelow(True)

# Get the colours
colors = {}
for ind, key in enumerate(stars.lines):
    colors[key] = plt.cm.tab10(ind)

for key in stars.lines:
    ax.semilogy(
        stars.lines[key].lam,
        stars.lines[key].luminosity,
        color=colors[key],
        marker="+",
        linestyle="None",
    )

ax.set_xlabel("Wavelength (Angstrom)")
ax.set_ylabel("Luminosity (erg/s)")

# Create the legend
legend_handles = []
for key in stars.lines:
    # Skip all spectra types where lines are 0
    if stars.lines[key].luminosity.sum() == 0:
        continue
    legend_handles.append(
        plt.Line2D(
            [0],
            [0],
            color=colors[key],
            label=key,
            marker="+",
            linestyle="None",
        )
    )

ax.legend(
    handles=legend_handles,
    loc="upper center",
    bbox_to_anchor=(0.5, -0.15),
    ncol=3,
)

plt.show()
../_images/lines_galaxy_lines_5_0.png
../_images/lines_galaxy_lines_5_1.png

Observer frame lines

Just like an Sed lines can be shifted into the observer frame by calling the get_flux method with a cosmology and redshift. This can either be done on a whole LineCollection or an individual Line.

[4]:
from astropy.cosmology import Planck18 as cosmo

# Use a LineCollection level method
stars.lines["emergent"].get_flux(cosmo, z=8)
for line in stars.lines["emergent"]:
    print(f"{line.id}: {line.flux} @ {line.obslam}")

# Extract a line an caluclate its flux (not the flux is both stored
# and returned)
print(stars.lines["nebular"]["H 1 4861.32A"].get_flux(cosmo, z=5))
H 1 4861.32A: [3.66041521e-23] erg/(cm**2*s) @ [43751.88] Å
O 3 5006.84A: [2.57434226e-27] erg/(cm**2*s) @ [45061.56] Å
O 3 4958.91A: [8.54233388e-28] erg/(cm**2*s) @ [44630.19] Å
O 2 3726.03A, O 2 3728.81A: [1.47624761e-24] erg/(cm**2*s) @ [33546.78] Å
O 3 4958.91A, O 3 5006.84A: [3.42015256e-27] erg/(cm**2*s) @ [44845.875] Å
[2.47416039e-22] erg/(cm**2*s)

Additionally, just like an Sed object, you can also calculate the rest frame flux at 10 parsecs using the get_flux0 method (again both on a Line and a LineCollection).

[5]:
# Use a LineCollection level method
stars.lines["emergent"].get_flux0()
for line in stars.lines["emergent"]:
    print(f"{line.id}: {line.flux} @ {line.obslam}")

stars.lines["emergent"].plot_lines(
    ylimits=(None, 10**38.0), xlimits=(None, 5050)
)
H 1 4861.32A: [0.00247251] erg/(cm**2*s) @ [4861.32] Å
O 3 5006.84A: [1.7388953e-07] erg/(cm**2*s) @ [5006.84] Å
O 3 4958.91A: [5.77010463e-08] erg/(cm**2*s) @ [4958.91] Å
O 2 3726.03A, O 2 3728.81A: [9.97163454e-05] erg/(cm**2*s) @ [3727.42] Å
O 3 4958.91A, O 3 5006.84A: [2.31021621e-07] erg/(cm**2*s) @ [4982.875] Å
[5]:
(<Figure size 800x600 with 1 Axes>,
 <Axes: xlabel='$ \\lambda / \\AA$', ylabel='$L / $erg s$^{-1}$'>)
../_images/lines_galaxy_lines_9_2.png