Calculating SFH with Diffstar

This notebook gives two basic illustrations of how to use diffstar to model the SFHs of individual and populations of galaxies.

SFH of an individual diffstar galaxy

In the cell below, we’ll grab the default diffmah and diffstar parameters, then we’l use the sfh_singlegal function to calculate the SFH.

[1]:
import numpy as np
from diffstar.defaults import DEFAULT_MAH_PARAMS
from diffstar.defaults import DEFAULT_DIFFSTAR_PARAMS

today_gyr = 13.8
tarr = np.linspace(0.1, today_gyr, 100)
[2]:
from diffstar import calc_sfh_singlegal

sfh_gal = calc_sfh_singlegal(
    DEFAULT_DIFFSTAR_PARAMS, DEFAULT_MAH_PARAMS, tarr)

SFHs of a population of diffstar galaxies

For purposes of this toy demonstration, we’ll first create a small diffstar population by randomly adding noise to the default diffstar parameters.

[3]:
n_gals = 5

mah_params_galpop = [x+np.random.uniform(-0.1, 0.1, n_gals) for x in DEFAULT_MAH_PARAMS]
ms_params_galpop = [x+np.random.uniform(-0.1, 0.1, n_gals) for x in DEFAULT_DIFFSTAR_PARAMS.ms_params]
q_params_galpop = [x+np.random.uniform(-0.1, 0.1, n_gals) for x in DEFAULT_DIFFSTAR_PARAMS.q_params]
sfh_params_galpop = ms_params_galpop, q_params_galpop
[4]:
mah_params_galpop[3].shape
[4]:
(5,)

The sfh_galpop calculates the SFH of an entire population at once. This calculation is vectorized with jax.vmap and so will be more efficient than a loop over successive calls to sfh_singlegal.

[5]:
from diffstar import calc_sfh_galpop

sfh_pop = calc_sfh_galpop(sfh_params_galpop, mah_params_galpop, tarr)
[6]:
from matplotlib import pyplot as plt

fig, ax = plt.subplots(1, 1)
ylim = ax.set_ylim(1e-3, 50)
yscale = ax.set_yscale('log')

__=ax.plot(tarr, sfh_gal, '--', color='k')

for igal in range(n_gals):
    __=ax.plot(tarr, sfh_pop[igal, :])


xlabel = ax.set_xlabel(r'${\rm cosmic\ time\ [Gyr]}$')
ylabel = ax.set_ylabel(r'${\rm SFR\ [M_{\odot}/yr]}$')
_images/demo_diffstar_sfh_8_0.png
[ ]: