def ps_2h_gal_dm_integrand(logM, k, z, CosPar): """ ps_2h_gal_dm_integrand(logM, k, z, CosPar): """ MM = np.exp(logM) dn_dlogM = halo_mass_function(MM, z, CosPar) bM = bias(MM, z, CosPar) # This is because I don't have 1 TB RAM ukm = np.zeros((k.size, logM.size)) progressbar_width = 80 progressbar_interval = logM.size/progressbar_width+1 # setup progress bar sys.stdout.write("[%s]" % (" " * progressbar_width)) sys.stdout.flush() sys.stdout.write("\b" * (progressbar_width+1)) # return to start of line, after '[' # Note ukm is physical, but k is comoving for i in np.arange(logM.size): ukm[:,i] = (NFW_ukm(k*(1.+z), MM[i], z, CosPar)).reshape(k.size) if (i%progressbar_interval==0): sys.stdout.write("-") sys.stdout.flush() sys.stdout.write("\n") return dn_dlogM*bM*ukm*MM/cosmo.rho_critical(z, CosPar)/cosmo.Omega_M_z(z, CosPar)
def NFW_2h_sigma_nocorr(R0, Mhalo0, z, CosPar, mu=1., fcorr=1., alpha=1.): Mhalo = np.array(Mhalo0) nMhalo = Mhalo.size Mhalo = Mhalo.reshape(nMhalo) #if nMhalo>1: raise ValueError("Mhalo must be a scalar") R = np.array(R0) nR = R.size R = R.reshape(nR) RR = np.ones(nMhalo)*R.reshape(nR,1) sigma_halo = NFW_approx_sigma_halo(Mhalo, z, CosPar) bMhalo = bias(Mhalo, z, CosPar) # get \xi(R) no bias xiR = xi_2h_gal_dm(R, 1E13, z, CosPar) # Integration over all masses M_min = 1E3 M_max = 1E17 dlogM = 1E-2 logM = np.arange(np.log(M_min)-2.*dlogM, np.log(M_max)+2.*dlogM, dlogM) MM = np.exp(logM) dn_dlogM = halo_mass_function(MM, z, CosPar) bM = bias(MM, z, CosPar) nlogM = logM.size sigma_halo_MM = NFW_approx_sigma_halo(MM, z, CosPar) sigma_virial_MM = NFW_approx_sigma_virial(MM, z, CosPar)*np.sqrt(3.) # Use the approximation to speed things up Integrand = (dn_dlogM*bM*MM/cosmo.rho_critical(z, CosPar)/cosmo.Omega_M_z(z, CosPar)*(sigma_halo_MM**2+(mu*sigma_virial_MM)**2)).reshape(1,1,nlogM)*(1.+bMhalo.reshape(1, nMhalo, 1)*bM.reshape(1,1,nlogM)*xiR.reshape(nR, nMhalo, 1)) sigma2_average = np.sum((Integrand[:,:,2:]+Integrand[:,:,:nlogM-2]+4.*Integrand[:,:,1:nlogM-1])/6.*dlogM, axis=2)#.reshape(nR, nMhalo) return np.sqrt((sigma_halo**2).reshape(1, nMhalo)+sigma2_average/(1.+bMhalo.reshape(1,nMhalo)*xiR))
def halo_mass_function(Mhalo, z, CosPar): print "IF YOU SEE ME TOO MANY TIMES, YOU SHOULD VECTORIZE YOUR CODE. MAYBE YOU SHOULD VECTORIZE ME!" M_min = 1E3 M_max = 1E17 dlogM = 1E-2 logM = np.arange(np.log(M_min)-2.*dlogM, np.log(M_max)+2.*dlogM, dlogM) nlogM = logM.size MM = np.exp(logM) sigma_M2 = sigma_M_sqr(MM, 0, CosPar) # at z=0 dsc = delta_sc(z, CosPar) nuM = dsc**2/sigma_M2/(cosmo.D_growth(z,CosPar)/cosmo.D_growth(0,CosPar))**2 #print min(nuM), max(nuM) dlognuM = np.zeros(nlogM) dlognuM[1:] = np.log(nuM[1:]/nuM[:nlogM-1]) dlognuM[0] = dlognuM[1] fM = nu_f_nu(nuM) nmz = fM*cosmo.rho_critical(z, CosPar)*cosmo.Omega_M_z(z, CosPar)/MM*dlognuM/dlogM Inte = fM*dlognuM normalization = np.sum(Inte[2:]+Inte[:nlogM-2]+4.*Inte[1:nlogM-1])/6. #print normalization f = interpolate.interp1d(MM, nmz) return f(Mhalo)/normalization
def NFW_approx_sigma_halo(Mhalo, z, CosPar, fcorr=1., alpha=1.): sigma_fit = 400. R_fit = 50. #R_scale = virial_radius(Mhalo, z, CosPar) R_scale = pow(3.*Mhalo/4./np.pi/cosmo.rho_critical(z, CosPar)/cosmo.Omega_M_z(z,CosPar), 1./3.)*(1.+z) #print R_scale eta = 0.85 return sigma_fit/(1.+pow(R_scale/R_fit, eta))
def rho_s(c, z, CosPar, alpha=1.): """ rho_s(c, z, CosPar, alpha=1.) """ if (alpha<3. and alpha>0.): if (alpha==1.): factor=(np.log(1.+c)-c/(1.+c)) else: factor = c**(3.-alpha)/(3.-alpha)*hyp2f1(3.-alpha, 3.-alpha, 4.-alpha, -c) else: raise ValueError("alpha has to be in the set (0,3)") return cosmo.rho_critical(z,CosPar)*cosmo.Omega_M_z(z, CosPar)*Delta_virial(z, CosPar)*c**3/3./factor
def NFW_2h_sigma_allmass(R0, Mhalo0, z, CosPar, mu=1., fcorr=1., alpha=1.): Mhalo = np.array(Mhalo0) nMhalo = Mhalo.size Mhalo = Mhalo.reshape(nMhalo) #if nMhalo>1: raise ValueError("Mhalo must be a scalar") R = np.array(R0) nR = R.size R = R.reshape(nR) RR = np.ones(nMhalo)*R.reshape(nR,1) sigma_halo = NFW_approx_sigma_halo(Mhalo, z, CosPar) bMhalo = bias(Mhalo, z, CosPar) # get \xi(R) no bias xiR = xi_2h_gal_dm(R, 1E13, z, CosPar) # This is impossible # Integration over all masses M_min = 1E3 M_max = 1E17 dlogM = 1E-2 logM = np.arange(np.log(M_min)-2.*dlogM, np.log(M_max)+2.*dlogM, dlogM) MM = np.exp(logM) dn_dlogM = halo_mass_function(MM, z, CosPar) bM = bias(MM, z, CosPar) nlogM = logM.size sigma_halo_MM = NFW_approx_sigma_halo(MM, z, CosPar) sigma_virial_MM = NFW_approx_sigma_virial(MM, z, CosPar)*np.sqrt(3.) # Use the approximation to speed things up sigma_correlation_sqr = np.zeros(nR*nMhalo*nlogM).reshape(nR, nMhalo, nlogM) sigma_Mhalo_corr = np.sqrt(1.-sigma_j_M_sqr(0, Mhalo, z, CosPar)**2/sigma_j_M_sqr(1,Mhalo,z,CosPar)/sigma_j_M_sqr(-1,Mhalo,z,CosPar)) sigma_MM_corr = np.sqrt(1.-sigma_j_M_sqr(0, MM, z, CosPar)**2/sigma_j_M_sqr(1,MM,z,CosPar)/sigma_j_M_sqr(-1,MM,z,CosPar)) prefix = (100.*CosPar['h']*f_Legendre(0,CosPar))**2 for j in np.arange(nMhalo): sigma_correlation_sqr[:,j,:] = sigma_j_r_M1_M2_sqr(-1, R, Mhalo[j], MM, z, CosPar)*prefix*sigma_Mhalo_corr[j]*sigma_MM_corr #for i in np.arange(nR): # sigma_correlation_sqr[i,j,:] = sigma_j_r_M1_M2_sqr(-1, R[i], Mhalo[j], MM, z, CosPar)*prefix*sigma_Mhalo_corr[j]*sigma_MM_corr # print np.median(np.sqrt(sigma_correlation_sqr)) # Integrand = (dn_dlogM*bM*MM/cosmo.rho_critical(z, CosPar)/cosmo.Omega_M_z(z, CosPar)*(sigma_halo_MM**2+(mu*sigma_virial_MM)**2)).reshape(1,1,nlogM)*(1.+bMhalo.reshape(1, nMhalo, 1)*bM.reshape(1,1,nlogM)*xiR.reshape(nR, nMhalo, 1)) Integrand = (dn_dlogM*bM*MM/cosmo.rho_critical(z, CosPar)/cosmo.Omega_M_z(z, CosPar)*((sigma_halo_MM**2+(mu*sigma_virial_MM)**2).reshape(1,1,nlogM)-2*sigma_correlation_sqr))*(1.+bMhalo.reshape(1, nMhalo, 1)*bM.reshape(1,1,nlogM)*xiR.reshape(nR, nMhalo, 1)) sigma2_average = np.sum((Integrand[:,:,2:]+Integrand[:,:,:nlogM-2]+4.*Integrand[:,:,1:nlogM-1])/6.*dlogM, axis=2)#.reshape(nR, nMhalo) return np.sqrt((sigma_halo**2).reshape(1, nMhalo)+sigma2_average/(1.+bMhalo.reshape(1,nMhalo)*xiR))
else: s_min = s_min_exclusion # s_max = s_min*6 # if s_max > R_max/(1.+z): # s_max = R_max/(1.+z) s_max = R_max / (1. + z) ss = np.exp(np.arange(np.log(s_min) + dlogy, np.log(s_max) + dlogy, dlogy)) nlogs = ss.size Integrand = Sigma_project_integrand(ss, s_min) Sigma_y[i] = np.sum( (Integrand[2:] + Integrand[:nlogs - 2] + 4. * Integrand[1:nlogs - 1]) / 6. * dlogy) if (i % progressbar_interval == 0): sys.stdout.write("-") sys.stdout.flush() sys.stdout.write("\n") Sigma_y = cosmo.rho_critical(z, CosPar) * cosmo.Omega_M_z( z, CosPar) * Sigma_y / 1E12 # Sigma_y = cosmo.rho_critical(z, CosPar)*cosmo.Omega_M_z(z, CosPar)*Sigma_y/1E12/(1.+z)**3 #Sigma_func = np.vectorize(lambda y: # integrate.quad(Sigma_project_integrand, y, R_max, limit=1000, # args=(y))) #Sigma_y = cosmo.rho_critical(z, CosPar)*cosmo.Omega_M_z(z, CosPar)*Sigma_func(yy) #np.savetxt('SigmaR_2h_no_bias_z0.52.dat', zip(yy, Sigma_y), fmt='%G %G') np.savetxt('linear_SigmaR_2h_no_bias_z0.52.dat', zip(yy, Sigma_y), fmt='%G %G')
def virial_radius(Mhalo, z, CosPar): """ virial_radius(Mhalo, z, CosPar): physical virial radius, multiply it by 1.+z to get comoving radius """ factor = 3./4./np.pi/cosmo.rho_critical(z, CosPar)/cosmo.Omega_M_z(z,CosPar)/Delta_virial(z, CosPar) return (factor*Mhalo)**(1./3.)
# 2-halo term for Sigma # Need to tabulate everything in the end import sys import numpy as np from scipy import fftpack from scipy import integrate from scipy import interpolate from scipy.special import hyp2f1, spence import cosmology as cosmo import powerspectra as ps circular_virial = lambda Mhalo, z, CosPar: np.sqrt(cosmo.GG_MSun*Mhalo/virial_radius(Mhalo, 0.52, CosPar)) delta_sc = lambda z, CosPar: 3./20.*pow(12.*np.pi, 2./3.)*(1.+0.013*np.log10(cosmo.Omega_M_z(z, CosPar))) concentration = lambda Mhalo, z, Mhalo_star: 9./(1.+z)*(Mhalo/Mhalo_star)**(-0.13) # There are other models Hu & Kravtsov sigma_M_sqr = lambda Mhalo, z, CosPar: ps.sigma_R_sqr(pow(3.*Mhalo/4./np.pi/cosmo.rho_critical(z, CosPar)/cosmo.Omega_M_z(z,CosPar), 1./3.)*(1.+z), z, CosPar) # Physical to Comoving radius sigma_j_M_sqr = lambda j, Mhalo, z, CosPar: ps.sigma_j_R_sqr(j, pow(3.*Mhalo/4./np.pi/cosmo.rho_critical(z, CosPar)/cosmo.Omega_M_z(z,CosPar), 1./3.)*(1.+z), z, CosPar) # Physical to Comoving radius sigma_j_r_M1_M2_sqr = lambda j, r, Mhalo1, Mhalo2, z, CosPar: sigma_j_r_R1_R2_sqr(j, r, pow(3.*Mhalo1/4./np.pi/cosmo.rho_critical(z, CosPar)/cosmo.Omega_M_z(z,CosPar), 1./3.)*(1.+z), pow(3.*Mhalo2/4./np.pi/cosmo.rho_critical(z, CosPar)/cosmo.Omega_M_z(z,CosPar), 1./3.)*(1.+z), z, CosPar) # Physical to Comoving radius nu_f_nu = lambda nu: 0.129*np.sqrt(nu/np.sqrt(2.))*(1.+pow(nu/np.sqrt(2.), -0.3))*np.exp(-nu/np.sqrt(2.)/2.) # Sheth & Tormen 1999 nu=[delta_sc/D_growth/sigma_M]^2. 0.129 is for the whole integral, need to calculate the normalization again bias_nu = lambda nu, d_sc: 1.+nu/np.sqrt(2.)/d_sc+0.35*pow(nu/np.sqrt(2.), 1.-0.8)/d_sc-pow(nu/np.sqrt(2.), 0.8)/(pow(nu/np.sqrt(2.), 0.8)+0.35*(1.-0.8)*(1.-0.8/2.))/d_sc*np.sqrt(np.sqrt(2.)) # Tinker+2005 bias_nu_st = lambda nu, d_sc: 1.+(0.73*nu-1.)/d_sc+2.*0.15/d_sc/(1.+pow(0.73*nu, 0.15)) # Sheth & Tormen 1999 f_sigma = lambda sigma_M: 0.186*(1.+pow(sigma_M/2.57, -1.47))*np.exp(-1.19/sigma_M**2) # Tinker+2008, Delta=200 f_Legendre = lambda z, CosPar: cosmo.Omega_M_z(z, CosPar)**0.55 #Mhalo_star: This has to be tabulated for different cosmology parameters. Calculate it on the fly takes too much time. (Compute_Mhalo_star(Cosmo)) M_star = 5.19E12 # MSun not h^-1 MSun # Even though it's vectorized, it requires a large amount of memory to have a 3-D array (1E4, 1E4, 1E4) def NFW_ukm_integrand(logR, k, Mhalo, z, CosPar): """ NFW_ukm_integrand(logR, kk, Mhalo, z, CosPar)