Ejemplo n.º 1
0
def cl_WL_tSZ(config_file, cosmology, fwhm_k, fwhm_y, kk, yy, ky, zsfile, 
              P01, P02, P03, xc1, xc2, xc3, beta1, beta2, beta3, 
              omega_m0, sigma_8, odir, ofile):
    '''
    Compute WL X tSZ halomodel for a given source redshift distribution 
    '''
    if ky:
        sigma_k = fwhm_k * np.pi / 2.355 / 60. /180. #angle in radian
        sigma_y = fwhm_y * np.pi / 2.355 / 60. /180. #angle in radian
        sigmasq = sigma_k * sigma_y
    elif kk:
        sigma_k = fwhm_k * np.pi / 2.355 / 60. /180. #angle in radian
        sigmasq = sigma_k * sigma_k
    elif yy:
        sigma_y = fwhm_y * np.pi / 2.355 / 60. /180. #angle in radian
        sigmasq = sigma_y * sigma_y
    else:
        raise ValueError('Either kk, yy or ky should be True')


    config = ConfigParser()
    config.read(config_file)

    if omega_m0 is None:
        omega_m0 = config.getfloat(cosmology, 'omega_m0')
    if sigma_8 is None:
        sigma_8 = config.getfloat(cosmology, 'sigma_8')

    omega_l0 = 1. - omega_m0
    omega_b0 = config.getfloat(cosmology, 'omega_b0')
    h = config.getfloat(cosmology, 'h')
    n_scalar = config.getfloat(cosmology, 'n_scalar')    
    cosmo_dict = {'omega_m0':omega_m0, 'omega_l0':omega_l0, 'omega_b0':omega_b0, 'h':h, 'sigma_8':sigma_8, 'n_scalar':n_scalar}

    cosmo0 = CosmologyFunctions(0, config_file, cosmo_dict)
    cosmo_h = cosmo0._h

    save_clfile = config.getboolean('halomodel', 'save_clfile')
    print_cl = config.getboolean('halomodel', 'print_cl')
    light_speed = config.getfloat('halomodel', 'light_speed') #km/s
    mpctocm = config.getfloat('halomodel', 'mpctocm')
    kB_kev_K = config.getfloat('halomodel', 'kB_kev_K')
    sigma_t_cm = config.getfloat('halomodel', 'sigma_t_cm') #cm^2
    rest_electron_kev = config.getfloat('halomodel', 'rest_electron_kev') #keV
    constk = 3. * omega_m0 * (cosmo_h * 100. / light_speed)**2. / 2. #Mpc^-2
    consty = mpctocm * sigma_t_cm / rest_electron_kev 

    zsarr, Ns = np.genfromtxt(zsfile, unpack=True)
    if np.isscalar(zsarr):
        zsarr = np.array([zsarr])
        Ns = np.array([Ns])
    else:
        zint = np.sum(Ns) * (zsarr[1] - zsarr[0])
        Ns /= zint
    
    if P01 is None:
        P01 = config.getfloat('halomodel', 'P01')
    if P02 is None:
        P02 = config.getfloat('halomodel', 'P02')
    if P03 is None:
        P03 = config.getfloat('halomodel', 'P03')
    if xc1 is None:
        xc1 = config.getfloat('halomodel', 'xc1')
    if xc2 is None:
        xc2 = config.getfloat('halomodel', 'xc2')
    if xc3 is None:
        xc3 = config.getfloat('halomodel', 'xc3')
    if beta1 is None:
        beta1 = config.getfloat('halomodel', 'beta1')
    if beta2 is None:
        beta2 = config.getfloat('halomodel', 'beta2')
    if beta3 is None:
        beta3 = config.getfloat('halomodel', 'beta3')

    kRmax = config.getfloat('halomodel', 'kRmax') 
    kRspace = config.getint('halomodel', 'kRspace')
    yRmax = config.getfloat('halomodel', 'yRmax')
    yRspace = config.getint('halomodel', 'yRspace')

    kmin = config.getfloat('halomodel', 'kmin') #1/Mpc
    kmax = config.getfloat('halomodel', 'kmax')
    kspace = config.getint('halomodel', 'kspace')

    ellmin = config.getint('halomodel', 'ellmin') 
    ellmax = config.getfloat('halomodel', 'ellmax')
    ellspace = config.getint('halomodel', 'ellspace')

    mmin = config.getfloat('halomodel', 'mmin') 
    mmax = config.getfloat('halomodel', 'mmax')
    mspace = config.getfloat('halomodel', 'mspace')

    zmin = config.getfloat('halomodel', 'zmin')
    zmax = config.getfloat('halomodel', 'zmax')
    zspace = config.getfloat('halomodel', 'zspace')

    dlnk = np.log(kmax/kmin) / kspace
    lnkarr = np.linspace(np.log(kmin), np.log(kmax), kspace)
    karr = np.exp(lnkarr).astype(np.float64)
    #No little h
    #Input Mpc/h to power spectra and get Mpc^3/h^3
    pk_arr = np.array([cosmo0.linear_power(k/cosmo0._h) for k in karr]).astype(np.float64) / cosmo0._h / cosmo0._h / cosmo0._h
    #np.savetxt('pk_vinu.dat', np.transpose((karr/cosmo0._h, pk_arr*cosmo0._h**3)))
    #sys.exit()

    #kc, pkc = np.genfromtxt('matterpower.dat', unpack=True)
    #pkspl = InterpolatedUnivariateSpline(kc, pkc, k=2) 
    #pkh_arr = pkspl(karr/cosmo0._h)
    #pk_arr = pkh_arr / cosmo0._h / cosmo0._h / cosmo0._h

    pkspl = InterpolatedUnivariateSpline(karr, pk_arr, k=2) 
    #pl.loglog(karr, pk_arr)
    #pl.show()

    dlnm = np.log(mmax/mmin) / mspace
    lnmarr = np.linspace(np.log(mmin), np.log(mmax), mspace)
    marr = np.exp(lnmarr).astype(np.float64)
    lnzarr = np.linspace(np.log(1.+zmin), np.log(1.+zmax), zspace)
    zarr = np.exp(lnzarr) - 1.0
    dlnz = np.log((1.+zmax)/(1.+zmin)) / zspace

    print('dlnk, dlnm dlnz', dlnk, dlnm, dlnz)
    if doPrintCl:
        print('dlnk, dlnm dlnz', dlnk, dlnm, dlnz)
    #No little h
    #Need to give mass * h and get the sigma without little h
    #The following lines are used only used for ST MF and ST bias
    sigma_m0 = np.array([cosmo0.sigma_m(m * cosmo0._h) for m in marr])
    rho_norm0 = cosmo0.rho_bar()
    lnMassSigmaSpl = InterpolatedUnivariateSpline(lnmarr, sigma_m0, k=3)

    hzarr, BDarr, rhobarr, chiarr, dVdzdOm, rho_crit_arr = [], [], [], [], [], []
    bias, bias_t, Darr = [], [], []
    marr2, mf, dlnmdlnm, Deltaarr = [], [], [], []

    if config.get('halomodel', 'MF') == 'Tinker' and config.get('halomodel', 'MassToIntegrate') == 'm200m':
        #Mass using critical density (ie. m200c)
        tm200c = np.logspace(np.log10(1e8), np.log10(1e17), 50)
        #m200m mass using mean mass density
        tmarr = np.exp(lnmarr).astype(np.float64)

    #tf = np.genfromtxt('../data/z_m_relation.dat')
    #tz = tf[:,0]
    #tmv = tf[:,1]
    #tm200c = tf[:,2]
    #tm200m = tf[:,3]
    for i, zi in enumerate(zarr):
        cosmo = CosmologyFunctions(zi, config_file, cosmo_dict)
        rcrit = cosmo.rho_crit() * cosmo._h * cosmo._h
        rbar = cosmo.rho_bar() * cosmo._h * cosmo._h
        bn = cosmo.BryanDelta()
        BDarr.append(bn) #OK
        rho_crit_arr.append(rcrit) #OK
        rhobarr.append(rbar)
        chiarr.append(cosmo.comoving_distance() / cosmo._h)
        hzarr.append(cosmo.E0(zi))
        DD = bn/cosmo.omega_m()
        DD200 = 200./cosmo.omega_m()
        #Number of Msun objects/Mpc^3 (i.e. unit is 1/Mpc^3)
        
        if config.get('halomodel', 'MF') == 'Tinker':
            if config.get('halomodel', 'MassToIntegrate') == 'virial':
                if DD > 200:
                    mFrac = marr * cosmo_h 
                    print(1)
                    mFrac = marr * h0 
                    #print bn, cosmo.omega_m(), bn/cosmo.omega_m()
                    mf.append(bias_mass_func_tinker(zi, config_file, cosmo_dict, mFrac.min(), mFrac.max(), mspace, bias=False, Delta=DD, marr=mFrac, reduced=False)[1])
                    marr2.append(marr)
                    dlnmdlnm.append(np.ones_like(marr))
                else:
                    mFrac = np.array([HuKravtsov(zi, mv, rcrit, bn, config.getfloat('halomodel', 'MassDef') * cosmo.omega_m(), cosmo_h, 1)[2] for mv in marr]) * cosmo_h 
                    mf.append(bias_mass_func_tinker(zi, config_file, cosmo_dict,  mFrac.min(), mFrac.max(), mspace, bias=False, Delta=config.getfloat('halomodel', 'MassDef'), marr=mFrac, reduced=False)[1])
                    marr2.append(marr)
                    dlnmdlnm.append([dlnMdensitydlnMcritOR200(config.getfloat('halomodel', 'MassDef') * cosmo.omega_m(), bn, mFm/cosmo_h, mv, zi, cosmo_h, 1) for mv,mFm in zip(marr, mFrac)]) #dlnmFrac/dlnMv. In the bias_mass_func_tinker() I have computed dn/dlnM where M is in the unit of Msol. I have therefore include h in that mass function. Therefore, I just need to multiply dlnmFrac/dlnMv only 
                    print(2)
                    mFrac = np.array([HuKravtsov(zi, mv, rcrit, bn, float(config['massfunc']['MassDef'])/cosmo.omega_m(), h0, 1)[2] for mv in marr]) * h0 
                    mf.append(bias_mass_func_tinker(zi, mFrac.min(), mFrac.max(), mspace, bias=False, Delta=float(config['massfunc']['MassDef']), marr=mFrac, reduced=False)[1])
                    marr2.append(marr)
                    dlnmdlnm.append([dlnMdensitydlnMcritOR200(float(config['massfunc']['MassDef']) / cosmo.omega_m(), bn, mFm/h0, mv, zi, h0, 1) for mv,mFm in zip(marr, mFrac)]) #dlnmFrac/dlnMv. In the bias_mass_func_tinker() I have computed dn/dlnM where M is in the unit of Msol. I have therefore include h in that mass function. Therefore, I just need to multiply dlnmFrac/dlnMv only 
                #print dlnmdlnm
                #print a
                input_mvir = 1
            elif config.get('halomodel', 'MassToIntegrate') == 'm200c':
                #XXX
                #m200m = np.array([HuKravtsov(zi, mv, rcrit, 200, 200*cosmo.omega_m(), cosmo_h, 0)[2] for mv in marr]) #* cosmo_h
                #print m200m
                #XXX
                if DD200 > 200:
                    mFrac = marr * cosmo_h 
                    #print 200, cosmo.omega_m(), 200/cosmo.omega_m()
                    mf.append(bias_mass_func_tinker(zi, config_file, cosmo_dict, mFrac.min(), mFrac.max(), mspace, bias=False, Delta=DD200, marr=mFrac, reduced=False)[1])
                    marr2.append(marr)
                    dlnmdlnm.append(np.ones_like(marr))
                else:
                    mFrac = np.array([HuKravtsov(zi, m2c, rcrit, 200, config.getfloat('halomodel', 'MassDef') * cosmo.omega_m(), cosmo_h, 0)[2] for m2c in marr]) * cosmo_h
                    mf.append(bias_mass_func_tinker(zi, config_file, cosmo_dict, mFrac.min(), mFrac.max(), mspace, bias=False, Delta=config.getfloat('halomodel', 'MassDef'), marr=mFrac)[1])
                    marr2.append(marr)
                    for m2,mFm in zip(marr, mFrac):
                        dlnmdlnm.append(dlnMdensitydlnMcritOR200(config.getfloat('halomodel', 'MassDef') * cosmo.omega_m(), 200., mFm/cosmo_h, m2, zi, cosmo_h, 0)) #dlnM200m/dlnMv. In the bias_mass_func_tinker() I have computed dn/dlnM where M is in the unit of Msol. I have therefore include h in that mass function. Therefore, I just need to multiply dlnM200m/dlnMv only
                    mFrac = np.array([HuKravtsov(zi, m2c, rcrit, 200, float(config['massfunc']['MassDef'])/cosmo.omega_m(), h0, 0)[2] for m2c in marr]) * h0
                    mf.append(bias_mass_func_tinker(zi, mFrac.min(), mFrac.max(), mspace, bias=False, Delta=float(config['massfunc']['MassDef']), marr=mFrac)[1])
                    marr2.append(marr)
                    for m2,mFm in zip(marr, mFrac):
                        dlnmdlnm.append(dlnMdensitydlnMcritOR200(float(config['massfunc']['MassDef']) / cosmo.omega_m(), 200., mFm/h0, m2, zi, h0, 0)) #dlnM200m/dlnMv. In the bias_mass_func_tinker() I have computed dn/dlnM where M is in the unit of Msol. I have therefore include h in that mass function. Therefore, I just need to multiply dlnM200m/dlnMv only
                input_mvir = 0
            elif config.get('halomodel', 'MassToIntegrate') == 'm200m':
                #raise ValueError('Use MassToIntegrate=virial/m200c. m200m is not working')
                #Temporary mass array of m200m from m200c
                tm200m = np.array([HuKravtsov(zi, tt, rcrit, 200, 200.*cosmo.omega_m(), cosmo._h, 0)[2] for tt in tm200c])
                #m200m vs m200c spline 
                tmspl = InterpolatedUnivariateSpline(tm200m, tm200c)
                #Now m200c from m200m, i.e. tmarr which is the integrating
                #variable
                m200c = tmspl(tmarr)
                #m200m Msol/h
                m200m = tmarr * cosmo_h 
                marr2.append(m200c)
                mf.append(bias_mass_func_tinker(zi, config_file, cosmo_dict, m200m.min(), m200m.max(), mspace, bias=False, Delta=200, marr=m200m)[1])
                input_mvir = 0
        elif config.get('halomodel', 'MF') == 'Bocquet':
            if config.get('halomodel', 'MassToIntegrate') == 'virial':
                m200 = np.array([HuKravtsov(zi, mv, rcrit, bn, 200, cosmo_h, 1)[2] for mv in marr])
                mf.append(bias_mass_func_bocquet(zi, config_file, cosmo_dict, m200.min(), m200.max(), mspace, bias=False, marr=m200)[1])
                marr2.append(marr)
                for mv,m2 in zip(marr, m200):
                    dlnmdlnm.append(dlnMdensitydlnMcritOR200(200., bn, m2, mv, zi, cosmo_h, 1))
                input_mvir = 1
            elif config.get('halomodel', 'MassToIntegrate') == 'm200':
                tmf = bias_mass_func_bocquet(zi, config_file, cosmo_dict, marr.min(), marr.max(), mspace, bias=False, marr=marr)[1]
                mf.append(tmf)
                dlnmdlnm.append(np.ones(len(tmf)))
                input_mvir = 0
        elif config.get('halomodel', 'MF') == 'ST':
            mf.append(bias_mass_func_st(zi, config_file, cosmo_dict, marr.min(), marr.max(), mspace, bias=False, marr=marr)[1])
            marr2.append(marr)
            dlnmdlnm.append(np.ones_like(marr))
            input_mvir = 1
            #raise ValueError('MF should be Tinker or Bocquet')
        #Bias is calculated by assuming that the mass is virial. I need to change that
        #print DD
        #for m in marr:
        #    print '%.2e T %.2f'%(m, halo_bias_tinker(DD, cosmo.delta_c() / cosmo._growth / lnMassSigmaSpl(np.log(m))))
        #    print '%.2e ST %.2f'%(m, halo_bias_st(cosmo.delta_c() * cosmo.delta_c() / cosmo._growth / cosmo._growth / lnMassSigmaSpl(np.log(m)) / lnMassSigmaSpl(np.log(m))))
        #sys.exit()
        if config.get('halomodel', 'MF') == 'Tinker':
            if DD >= 200:
                bias.append(np.array([halo_bias_tinker(DD, cosmo.delta_c() / cosmo._growth / lnMassSigmaSpl(np.log(m))) for m in marr]))
            else:
                bias.append(np.array([halo_bias_tinker(config.getfloat('halomodel', 'MassDef') * cosmo.omega_m(), cosmo.delta_c() / cosmo._growth / lnMassSigmaSpl(np.log(m))) for m in mFrac/cosmo_h]))
        elif config.get('halomodel', 'MF') == 'ST' or config.get('halomodel', 'MF') == 'Bocquet':
            bias.append(np.array([halo_bias_st(cosmo.delta_c() * cosmo.delta_c() / cosmo._growth / cosmo._growth / lnMassSigmaSpl(np.log(m)) / lnMassSigmaSpl(np.log(m))) for m in marr]))
        dVdzdOm.append(cosmo.E(zi) / cosmo._h) #Mpc/h, It should have (km/s/Mpc)^-1 but in the cosmology code the speed of light is removed  
        Darr.append(cosmo._growth)

        #pl.title('%.2f'%zi)
        #pl.scatter(np.array([halo_bias_st(cosmo.delta_c() * cosmo.delta_c() / cosmo._growth / cosmo._growth / lnMassSigmaSpl(np.log(m)) / lnMassSigmaSpl(np.log(m))) for m in marr]), np.array([halo_bias_tinker(200, cosmo.delta_c() / cosmo._growth / lnMassSigmaSpl(np.log(m))) for m in marr]))
        #pl.show()

    if config.get('halomodel', 'MF') =='Tinker' and config.get('halomodel', 'MassToIntegrate') == 'm200m':  
        mf = np.array(mf).flatten()
    else: 
        mf = np.array(mf).flatten()  * np.array(dlnmdlnm).flatten()
    hzarr = np.array(hzarr)
    BDarr = np.array(BDarr)
    rhobarr = np.array(rhobarr)
    chiarr = np.array(chiarr)
    dVdzdOm = np.array(dVdzdOm) * chiarr * chiarr
    rho_crit_arr = np.array(rho_crit_arr)
    marr2 = np.array(marr2).flatten()
    zchispl = InterpolatedUnivariateSpline(zarr, chiarr, k=2)
    chisarr = zchispl(zsarr)
    bias = np.array(bias).flatten()
    bias_t = np.array(bias_t).flatten()
    Darr = np.array(Darr)

    #ellarr = np.linspace(1, 10001, 10)
    ellarr = np.logspace(np.log10(ellmin), np.log10(ellmax), ellspace)
    cl_arr, cl1h_arr, cl2h_arr = [], [], []
    for ell in ellarr:
        pk = pkspl(ell/chiarr)
        if ky: 
            cl1h, cl2h, cl = integrate_kyhalo(ell, lnzarr, chiarr, dVdzdOm, marr2, mf, BDarr, rhobarr, rho_crit_arr, bias, Darr, pk, zsarr, chisarr, Ns, dlnz, dlnm, omega_b0, omega_m0, cosmo_h, constk, consty, input_mvir, mspace, kRmax, kRspace, yRmax, yRspace, P01, P02, P03, xc1, xc2, xc3, beta1, beta2, beta3)
        if kk:
            cl1h, cl2h, cl = integrate_kkhalo(ell, lnzarr, chiarr, dVdzdOm, marr2, mf, BDarr, rhobarr, rho_crit_arr, bias, Darr, pk, zsarr, chisarr, Ns, dlnz, dlnm, omega_b0, omega_m0, cosmo_h, constk, consty, input_mvir, mspace, kRmax, kRspace)
        if yy:
            cl1h, cl2h, cl = integrate_yyhalo(ell, lnzarr, chiarr, dVdzdOm, marr2, mf, BDarr, rhobarr, rho_crit_arr, bias, Darr, pk, dlnz, dlnm, omega_b0, omega_m0, cosmo_h, constk, consty, input_mvir, mspace, yRmax, yRspace, P01, P02, P03, xc1, xc2, xc3, beta1, beta2, beta3)
        cl_arr.append(cl)
        cl1h_arr.append(cl1h)
        cl2h_arr.append(cl2h)
        if print_cl:
            print('l %.2 Cl_1h %.2e Cl_2h %.2e Cl %.2e'%(ell, cl1h, cl2h, cl))
        if doPrintCl:
            print(ell, cl1h, cl2h, cl)

    convolve = np.exp(-1 * sigmasq * ellarr * ellarr)# i.e. the output is Cl by convolving by exp(-sigma^2 l^2)
    cl = np.array(cl_arr) * convolve
    cl1h = np.array(cl1h_arr) * convolve
    cl2h = np.array(cl2h_arr) * convolve
    
    if save_clfile:
        np.savetxt(os.path.join(odir, ofile), np.transpose((ellarr, cl1h, cl2h, cl)), fmt='%.2f %.3e %.3e %.3e', header='l Cl1h Cl2h Cl')

    return ellarr, cl1h, cl2h, cl
Ejemplo n.º 2
0

if __name__ == '__main__':
    z = 0.07
    Mvir = 1e15
    cosmo = CosmologyFunctions(z, 'wlsz.ini', 'battaglia')
    omega_b = cosmo._omega_b0
    omega_m = cosmo._omega_m0
    cosmo_h = cosmo._h
    BryanDelta = cosmo.BryanDelta()
    rho_critical = cosmo.rho_crit() * cosmo._h * cosmo._h
    rho_bar = cosmo.rho_bar() * cosmo._h * cosmo._h
    for D in np.arange(1, 100, 10):
        M, R, Mfrac, Rfrac, rho_s, Rs = HuKravtsov(z, Mvir, rho_critical,
                                                   BryanDelta,
                                                   D * cosmo.omega_m(),
                                                   cosmo_h, True)
        print('%.2e %.2f %.2e %.2f %.2e %.2f' %
              (M, R, Mfrac, Rfrac, rho_s, Rs))
    sys.exit()
    print('rho_critical = %.2e , rho_bar = %.2e' % (rho_critical, rho_bar))
    print('Mvir, Rvir, Mfrac, Rfrac, rho_s, Rs')
    Mvir, Rvir, Mfrac, Rfrac, rho_s, Rs = MvirToMRfrac(Mvir, z, BryanDelta,
                                                       rho_critical, cosmo_h)
    print('%.2e %.2f %.2e %.2f %.2e %.2f' %
          (Mvir, Rvir, Mfrac, Rfrac, rho_s, Rs))
    Mvir, Rvir, Mfrac, Rfrac, rho_s, Rs = MfracToMvir(Mfrac,
                                                      z,
                                                      BryanDelta,
                                                      rho_critical,
                                                      cosmo_h,