Example #1
0
def model(theta, R, h=1, Om=0.315, Ol=0.685):

    from itertools import count, izip

    # HMF set up parameters - fixed and not setable from config file.

    expansion = 100
    expansion_stars = 160

    n_bins = 10000

    M_min = 8.5
    M_max = 15.5
    step = (M_max - M_min) / 100  # or n_bins

    k_min = -6.0  # ln!!! not log10!
    k_max = 9.0  # ln!!! not log10!
    k_step = (k_max - k_min) / n_bins

    k_range = np.arange(k_min, k_max, k_step)
    k_range_lin = np.exp(k_range)

    M_star_min = 8.5  # Halo mass bin range
    M_star_max = 15.5
    step_star = (M_star_max - M_star_min) / 100

    mass_range = np.arange(M_min, M_max, step)
    mass_range_lin = 10.0 ** (mass_range)

    mass_star_log = np.arange(M_star_min, M_star_max, step_star, dtype=np.longdouble)
    mass_star = 10.0 ** (mass_star_log)

    # Setting parameters from config file

    omegab = 0.0455  # To be replaced by theta
    omegac = 0.226  # To be replaced by theta
    omegav = 0.726  # To be replaced by theta

    sigma_c, alpha_s, A, M_1, gamma_1, gamma_2, b_0, b_1, b_2, alpha_star, beta_gas, r_t0, r_c0, z, M_bin_min1, M_bin_min2, M_bin_max1, M_bin_max2 = (
        theta
    )

    M_bin_min = [M_bin_min1, M_bin_min2]  # Expanded according to number of bins!
    M_bin_max = [M_bin_max1, M_bin_max2]  # Expanded according to number of bins!

    hod_mass = [
        (10.0 ** (np.arange(Mi, Mx, (Mx - Mi) / 100, dtype=np.longdouble))) for Mi, Mx in izip(M_bin_min, M_bin_max)
    ]

    r_t0 = r_t0 * np.ones(100)
    r_c0 = r_c0 * np.ones(100)

    H0 = 70.4  # To be replaced by theta
    rho_crit = 2.7763458 * (10.0 ** 11.0)  # in M_sun*h^2/Mpc^3 # To be called from nfw_utils!

    cosmology_params = {
        "sigma_8": 0.81,
        "H0": 70.4,
        "omegab": 0.0455,
        "omegac": 0.226,
        "omegav": 0.728,
        "n": 0.967,
        "lnk_min": k_min,
        "lnk_max": k_max,
        "dlnk": k_step,
        "transfer_fit": "CAMB",
        "z": z,
    }  # Values to be read in from theta

    # ---------------------------------------

    hmf = Mass_Function(
        M_min, M_max, step, k_min, k_max, k_step, "Tinker10", **cosmology_params
    )  # Tinker10 should also be read from theta!

    mass_func = hmf.dndlnm
    # power = hmf.power
    rho_mean_int = rho_crit * (hmf.omegac + hmf.omegab)
    rho_mean = Integrate(mass_func, mass_range_lin)

    radius_range_lin = ht.mass_to_radius(mass_range_lin, rho_mean) / ((200) ** (1.0 / 3.0))
    radius_range = np.log10(radius_range_lin)
    radius_range_3d = 10.0 ** np.arange(-3.0, 2.9, (2.9 - (-3.0)) / (100))

    radius_range_3d_i = 10.0 ** np.arange(-2.9, 1.0, (1.0 - (-2.9)) / (50))
    radius_range_2d_i = 10.0 ** np.arange(-2.5, 1.0, (1.0 - (-2.5)) / (50))  # THIS IS R!

    # Calculating halo model

    ngal = np.array(
        [
            n_gal(
                z,
                hmf,
                ngm(hmf, i[0], mass_range_lin, sigma_c, alpha_s, A, M_1, gamma_1, gamma_2, b_0, b_1, b_2),
                mass_range_lin,
                radius_range_lin,
            )
            for i in izip(hod_mass)
        ]
    )
    rho_dm = baryons.rhoDM(hmf, mass_range_lin, omegab, omegac)
    rho_stars = np.array(
        [
            baryons.rhoSTARS(hmf, i[0], mass_range_lin, sigma_c, alpha_s, A, M_1, gamma_1, gamma_2, b_0, b_1, b_2)
            for i in izip(hod_mass)
        ]
    )
    rho_gas = np.array(
        [
            baryons.rhoGAS(
                hmf,
                rho_crit,
                omegab,
                omegac,
                i[0],
                mass_range_lin,
                sigma_c,
                alpha_s,
                A,
                M_1,
                gamma_1,
                gamma_2,
                b_0,
                b_1,
                b_2,
            )
            for i in izip(hod_mass)
        ]
    )[:, 0]
    F = np.array(
        [
            baryons.rhoGAS(
                hmf,
                rho_crit,
                omegab,
                omegac,
                i[0],
                mass_range_lin,
                sigma_c,
                alpha_s,
                A,
                M_1,
                gamma_1,
                gamma_2,
                b_0,
                b_1,
                b_2,
            )
            for i in izip(hod_mass)
        ]
    )[:, 1]

    norm2 = rho_mean_int / rho_mean

    effective_mass = np.array(
        [
            eff_mass(
                z,
                hmf,
                ngm(hmf, i[0], mass_range_lin, sigma_c, alpha_s, A, M_1, gamma_1, gamma_2, b_0, b_1, b_2),
                mass_range_lin,
            )
            for i in izip(hod_mass)
        ]
    )
    effective_mass_dm = np.array(
        [
            eff_mass(
                z,
                hmf,
                ncm(hmf, i[0], mass_range_lin, sigma_c, alpha_s, A, M_1, gamma_1, gamma_2, b_0, b_1, b_2),
                mass_range_lin,
            )
            * baryons.f_dm(omegab, omegac)
            for i in izip(hod_mass)
        ]
    )
    effective_mass2 = effective_mass * (omegab / (omegac + omegab))
    effective_mass_bar = np.array(
        [
            effective_mass2
            * (baryons.f_stars(i[0], effective_mass2, sigma_c, alpha_s, A, M_1, gamma_1, gamma_2, b_0, b_1, b_2))
            for i in izip(hod_mass)
        ]
    )

    T_dm = np.array(
        [
            T_table_multi(
                expansion,
                rho_dm,
                z,
                mass_range_lin,
                radius_range_lin,
                i[0],
                "dm",
                omegab,
                omegac,
                0,
                0,
                sigma_c,
                alpha_s,
                A,
                M_1,
                gamma_1,
                gamma_2,
                b_0,
                b_1,
                b_2,
            )
            for i in izip(hod_mass)
        ]
    )
    T_stars = np.array(
        [
            T_table_multi(
                expansion_stars,
                rho_mean,
                z,
                mass_range_lin,
                radius_range_lin,
                i[0],
                "stars",
                omegab,
                omegac,
                alpha_star,
                r_t0,
                sigma_c,
                alpha_s,
                A,
                M_1,
                gamma_1,
                gamma_2,
                b_0,
                b_1,
                b_2,
            )
            for i in izip(hod_mass)
        ]
    )
    T_gas = np.array(
        [
            T_table_multi(
                expansion,
                rho_mean,
                z,
                mass_range_lin,
                radius_range_lin,
                i[0],
                "gas",
                omegab,
                omegac,
                beta_gas,
                r_c0,
                sigma_c,
                alpha_s,
                A,
                M_1,
                gamma_1,
                gamma_2,
                b_0,
                b_1,
                b_2,
            )
            for i in izip(hod_mass)
        ]
    )
    T_tot = np.array([T_dm[i][0:1:1, :] + T_stars[i][0:1:1, :] + T_gas[i][0:1:1, :] for i in range(len(M_bin_min))])

    F_k1 = f_k(k_range_lin)

    pop_c = np.array(
        [
            ncm(hmf, i[0], mass_range_lin, sigma_c, alpha_s, A, M_1, gamma_1, gamma_2, b_0, b_1, b_2)
            for i in izip(hod_mass)
        ]
    )
    pop_s = np.array(
        [
            nsm(hmf, i[0], mass_range_lin, sigma_c, alpha_s, A, M_1, gamma_1, gamma_2, b_0, b_1, b_2)
            for i in izip(hod_mass)
        ]
    )
    pop_g = np.array(
        [
            ngm(hmf, i[0], mass_range_lin, sigma_c, alpha_s, A, M_1, gamma_1, gamma_2, b_0, b_1, b_2)
            for i in izip(hod_mass)
        ]
    )

    # Galaxy - dark matter spectra

    Pg_2h = np.array(
        [
            TwoHalo(hmf, ngal_i, pop_g_i, k_range_lin, radius_range_lin, mass_range_lin)
            for ngal_i, pop_g_i in izip(ngal, pop_g)
        ]
    )

    Pg_c = np.array(
        [
            F_k1
            * GM_cen_spectrum(
                hmf,
                z,
                rho_dm,
                rho_mean,
                expansion,
                pop_c_i,
                ngal_i,
                k_range_lin,
                radius_range_lin,
                mass_range_lin,
                T_dm_i,
                T_tot_i,
            )
            for pop_c_i, ngal_i, T_dm_i, T_tot_i in izip(pop_c, ngal, T_dm, T_tot)
        ]
    )
    Pg_s = np.array(
        [
            F_k1
            * GM_sat_spectrum(
                hmf,
                z,
                rho_dm,
                rho_mean,
                expansion,
                pop_s_i,
                ngal_i,
                k_range_lin,
                radius_range_lin,
                mass_range_lin,
                T_dm_i,
                T_tot_i,
            )
            for pop_s_i, ngal_i, T_dm_i, T_tot_i in izip(pop_s, ngal, T_dm, T_tot)
        ]
    )

    # Galaxy - stars spectra

    Ps_c = np.array(
        [
            F_k1
            * baryons.GS_cen_spectrum(
                hmf,
                z,
                rho_stars_i,
                rho_mean,
                expansion_stars,
                pop_c_i,
                ngal_i,
                k_range_lin,
                radius_range_lin,
                mass_range_lin,
                T_stars_i,
                T_tot_i,
            )
            for rho_stars_i, pop_c_i, ngal_i, T_stars_i, T_tot_i in izip(rho_stars, pop_c, ngal, T_stars, T_tot)
        ]
    )
    Ps_s = np.array(
        [
            F_k1
            * baryons.GS_sat_spectrum(
                hmf,
                z,
                rho_stars_i,
                rho_mean,
                expansion,
                pop_s_i,
                ngal_i,
                k_range_lin,
                radius_range_lin,
                mass_range_lin,
                T_dm_i,
                T_stars_i,
                T_tot_i,
            )
            for rho_stars_i, pop_s_i, ngal_i, T_dm_i, T_stars_i, T_tot_i in izip(
                rho_stars, pop_s, ngal, T_dm, T_stars, T_tot
            )
        ]
    )

    # Galaxy - gas spectra

    Pgas_c = np.array(
        [
            F_k1
            * baryons.GGas_cen_spectrum(
                hmf,
                z,
                F_i,
                rho_gas_i,
                rho_mean,
                expansion,
                pop_c_i,
                ngal_i,
                k_range_lin,
                radius_range_lin,
                mass_range_lin,
                T_gas_i,
                T_tot_i,
            )
            for F_i, rho_gas_i, pop_c_i, ngal_i, T_gas_i, T_tot_i in izip(F, rho_gas, pop_c, ngal, T_gas, T_tot)
        ]
    )
    Pgas_s = np.array(
        [
            F_k1
            * baryons.GGas_sat_spectrum(
                hmf,
                z,
                F_i,
                rho_gas_i,
                rho_mean,
                expansion,
                pop_s_i,
                ngal_i,
                k_range_lin,
                radius_range_lin,
                mass_range_lin,
                T_dm_i,
                T_gas_i,
                T_tot_i,
            )
            for F_i, rho_gas_i, pop_s_i, ngal_i, T_dm_i, T_gas_i, T_tot_i in izip(
                F, rho_gas, pop_s, ngal, T_dm, T_gas, T_tot
            )
        ]
    )

    # Combined (all) by type

    Pg_k_dm = np.array(
        [
            (ngal_i * rho_dm * (Pg_c_i + Pg_s_i + Pg_2h_i * rho_mean / rho_dm)) / (rho_mean * ngal_i)
            for ngal_i, Pg_c_i, Pg_s_i, Pg_2h_i in izip(ngal, Pg_c, Pg_s, Pg_2h)
        ]
    )
    Pg_k_s = np.array(
        [
            (ngal_i * rho_stars_i * (Ps_c_i + Ps_s_i)) / (rho_mean * ngal_i)
            for ngal_i, rho_stars_i, Ps_c_i, Ps_s_i in izip(ngal, rho_stars, Ps_c, Ps_s)
        ]
    )
    Pg_k_g = np.array(
        [
            (ngal_i * rho_gas_i * (Pgas_c_i + Pgas_s_i)) / (rho_mean * ngal_i)
            for ngal_i, rho_gas_i, Pgas_c_i, Pgas_s_i in izip(ngal, rho_gas, Pgas_c, Pgas_s)
        ]
    )

    Pg_k = np.array(
        [
            (
                ngal_i * rho_dm * (Pg_c_i + Pg_s_i + Pg_2h_i * rho_mean / rho_dm)
                + ngal_i * rho_stars_i * (Ps_c_i + Ps_s_i)
                + ngal_i * rho_gas_i * (Pgas_c_i + Pgas_s_i)
            )
            / (rho_mean * ngal_i)
            for ngal_i, Pg_c_i, Pg_s_i, Pg_2h_i, rho_stars_i, Ps_c_i, Ps_s_i, rho_gas_i, Pgas_c_i, Pgas_s_i in izip(
                ngal, Pg_c, Pg_s, Pg_2h, rho_stars, Ps_c, Ps_s, rho_gas, Pgas_c, Pgas_s
            )
        ]
    )  # all components

    # Normalized sattelites and centrals for sigma and d_sigma

    Pg_c2 = np.array([(rho_dm / rho_mean) * Pg_c_i for Pg_c_i in izip(Pg_c)])
    Pg_s2 = np.array([(rho_dm / rho_mean) * Pg_s_i for Pg_s_i in izip(Pg_s)])

    Ps_c2 = np.array([(rho_stars_i / rho_mean) * Ps_c_i for rho_stars_i, Ps_c_i in izip(rho_stars, Ps_c)])
    Ps_s2 = np.array([(rho_stars_i / rho_mean) * Ps_s_i for rho_stars_i, Ps_s_i in izip(rho_stars, Ps_s)])

    Pgas_c2 = np.array([(rho_gas_i / rho_mean) * Pgas_c_i for rho_gas_i, Pgas_c_i in izip(rho_gas, Pgas_c)])
    Pgas_s2 = np.array([(rho_gas_i / rho_mean) * Pgas_s_i for rho_gas_i, Pgas_s_i in izip(rho_gas, Pgas_s)])

    lnPg_k = np.array([np.log(Pg_k_i) for Pg_k_i in izip(Pg_k)])  # Total

    P_inter2 = [scipy.interpolate.UnivariateSpline(k_range, np.nan_to_num(lnPg_k_i), s=0) for lnPg_k_i in izip(lnPg_k)]

    """
    # Correlation functions
    """

    xi2 = np.zeros((len(M_bin_min), len(radius_range_3d)))
    for i in range(len(M_bin_min)):
        xi2[i, :] = power_to_corr_multi(P_inter2[i], radius_range_3d)
        xi2[xi2 <= 0.0] = np.nan
        xi2[i, :] = fill_nan(xi2[i, :])

    """
    # Projected surface density
    """

    sur_den2 = np.array(
        [np.nan_to_num(sigma(xi2_i, rho_mean, radius_range_3d, radius_range_3d_i)) for xi2_i in izip(xi2)]
    )
    sur_den2[sur_den2 >= 10.0 ** 16.0] = np.nan
    sur_den2[sur_den2 <= 0.0] = np.nan
    for i in range(len(M_bin_min)):
        sur_den2[i, :] = fill_nan(sur_den2[i, :])

    """
    # Excess surface density
    """

    d_sur_den2 = np.array(
        [np.nan_to_num(d_sigma(sur_den2_i, radius_range_3d_i, radius_range_2d_i)) for sur_den2_i in izip(sur_den2)]
    )

    # print d_sur_den2/10**12.0

    return [d_sur_den2 / 10 ** 12.0, xi2, lnPg_k]  # Add other outputs as needed. Total ESD should always be first!
Example #2
0
    print ("Min, max k:"), k_range_lin[0], k_range_lin[-1]

    # ---------- End parameters -----------

    # -------------------------------------

    # ---------- Begin calculations -------

    nphi = phi_i(hmf, mass_star, mass_range_lin, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    ngal = n_gal(
        z, hmf, ngm(hmf, hod_mass, mass_range_lin, 0, 0, 0, 0, 0, 0, 0, 0, 0), mass_range_lin, radius_range_lin
    )  # That is ok!

    rho_dm = baryons.rhoDM(hmf, mass_range_lin, omegab, omegac)
    rho_stars = baryons.rhoSTARS(hmf, hod_mass, mass_range_lin, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    rho_gas, F = baryons.rhoGAS(hmf, rho_crit, omegab, omegac, hod_mass, mass_range_lin, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    norm2 = rho_mean_int / rho_mean

    print ("F:"), F
    print ("DM density:"), rho_dm
    print ("Stars density:"), rho_stars
    print ("Gas density:"), rho_gas
    print ("Sum of densities:"), rho_dm + rho_stars + rho_gas
    print ("Mean density int:"), rho_mean_int
    print ("Mean density theory:"), rho_mean
    print ("Critical density:"), rho_crit
    print ("Ratio of MDI/MDT:"), norm2
    print ("Ratio of MDS/MDT:"), (rho_dm + rho_stars + rho_gas) / rho_mean

    effective_mass = eff_mass(z, hmf, ngm(hmf, hod_mass, mass_range_lin, 0, 0, 0, 0, 0, 0, 0, 0, 0), mass_range_lin)
    effective_mass_dm = eff_mass(