def ESDsat(theta, Rp): logM, Mprm = theta #amp,rs,r200 = haloparams(logM,con) #sumESDsat = np.zeros(3077) #modelsat = ZuMandelbaum15Sats() nbins = 50 mbins = np.linspace((np.log10(3.0) + logM), 16.0, nbins) nsat = np.zeros(nbins) mfunc_so = np.zeros(nbins) totalESDsat = np.zeros(3077) for j in range(nbins): Mhalo = 10.0**(mbins[j]) mfunc_so[j] = mass_function.massFunction(mbins[j], zl, q_in='M', q_out='dndlnM', mdef='vir', model='tinker08') nsat[j] = ((Mhalo - 3.0 * 10.0**(logM)) / 10.0**(Mprm)) con = 4.67 * (10.0**logM / 1.0e+14)**(-0.11) amp, rs, r200 = haloparams(mbins[j], con) sumESDsat = np.zeros(3077) for i in range(nx): rpp = tabs[i, :, 0] * rs struc = nfwfuncs(rpp, rs) wt = struc['SigmaRp'] ix = rpp >= r200 wt[ix] = 0.0 dsig = tabs[i, :, 1] sumESDsat = sumESDsat + wt * dsig * amp totalESDsat = totalESDsat + mfunc_so[j] * nsat[j] * sumESDsat / wt.sum( ) return totalESDsat / (mfunc_so * nsat).sum()
def get_halos(z, Vol, dlog10m=0.005, hmf_choice='despali16', mdef='vir'): ####### set cosmology ######### cosmol = cosmo.setCosmology('planck15') cosmo.setCurrent(cosmol) ############################## Mvir = 10**np.arange(10., 16, dlog10m) #Mh/h if hmf_choice == 'despali16': massfunct = massFunction( x=Mvir, z=z, mdef=mdef, model='despali16', q_out='dndlnM') * np.log(10) #dn/dlog10M elif hmf_choice == 'rodriguezpuebla16': massfunct = hmf_rp16(Mvir, z) * Mvir / np.log10(np.exp(1)) massfunct = massfunct * (cosmol.h)**3 #convert from massf*h**3 total_haloMF = massfunct.copy() #massfunct = massFunction(x=Mvir, z=z, q_in='M',mdef='vir', model='despali16', q_out='dndlnM')*np.log(10) #dn/dlog10M Mvir = np.log10(Mvir) Mvir = Mvir - np.log10(cosmol.h) #convert from M/h Ncum = Vol * (np.cumsum((total_haloMF * dlog10m)[::-1])[::-1]) halos = extract_catalog(Ncum, Mvir) return halos
def dndlnm(self, M, z): """ Halo mass function for a given halo mass and redshift Use Despali (2016) halo mass function to admit any definition of virial overdensity """ return mass_function.massFunction( M / (M_s / self.cosmo.h), z, q_in="M", q_out="dndlnM", **self.hmf_kwargs) * (Mpc / self.cosmo.h)**-3
def bias_avg(Mmin, Mmax, z): size = int(1e5) M = np.linspace(Mmin, Mmax, size) hmf = massFunction( M * h, z, q_in='M', q_out='dndlnM', mdef='200c', model='tinker08') * (h**3) / M nu = get_nu(z, Mmin=Mmin, Mmax=Mmax, lenght=size) bh = bias_nu(nu) numerator = simps(M * hmf * bh, M) denominator = simps(M * hmf, M) return numerator / denominator
def massfunc(m,z,model='tinker08',mdef='200m'): ''' dark matter mass function dN/dlog_10M/dV (h^3 Mpc^-3) Args: mass, float, msolar/h z, float, redshift model, string, dark matter prescription (see colossus documentation) mdef, string, mass definition. Returns: dark matter mass function dN/dlog_10M/dV (h^3 Mpc^-3) ''' return mass_function.massFunction(m,z,mdef=mdef,model=model, q_out='dndlnM')*np.log(10.)
def massf(self, Mvir=None): if Mvir is None: dlog10m = 0.005 Mvir = 10**np.arange(11., 15, dlog10m) #Mh/h massfunct = massFunction(x=Mvir, z=self.z, mdef=self.mdef, model='despali16', q_out='dndlnM') * np.log(10) #dn/dlog10M massfunct = massfunct * (cosmol.h)**3 #convert from massf*h**3 Mvir = np.log10(Mvir) Mvir = Mvir - np.log10(cosmol.h) #convert from M/h return massfunct, Mvir # h-less units
def get_mfs(model, mdef): m_arr = np.geomspace(1E11, 1E15, 9) z_arr = np.array([0., 0.5, 1.]) d_out = [] d_out.append(m_arr) normfac = h**3 * np.log(10) for z in z_arr: d_out.append( mass_function.massFunction( m_arr * h, z, mdef=mdef, model=model, q_out='dndlnM') * normfac) np.savetxt("../hmf_" + model + ".txt", np.transpose(d_out), header='M nM(z=0) nM(z=0.5) nM(z=1)')
def hmfunc(self, z): nbins = 100 mbins = np.zeros(nbins) mfunc_so = np.zeros(nbins) for i in range(nbins): mbins[i] = 10**(8 + i * 0.1) mfunc_so[i] = mass_function.massFunction(mbins[i], z, q_in='M', q_out='dndlnM', mdef='vir', model='tinker08') return {'mbins': mbins, 'hmf': mfunc_so}
def get_dndm_bias(M_mat, z_array, mdef): dndm_array_Mz, bm_array_Mz = np.zeros(M_mat.shape), np.zeros(M_mat.shape) for j in range(len(z_array)): M_array = M_mat[j, :] dndm_array_Mz[j, :] = (1. / M_array) * mass_function.massFunction( M_array, z_array[j], mdef=mdef, model=dndm_model, q_out='dndlnM') bm_array_Mz[j, :] = bias.haloBias(M_array, z_array[j], model=bias_model, mdef=mdef) return dndm_array_Mz, bm_array_Mz
def hmf(self, z, Mmin, Mmax, q_out = 'dndlnM', hmf_code='colossus', print_cosmo=False): if(hmf_code=='colossus'): params = {'flat': True, 'H0': self.H0, 'Om0': self.om, 'Ob0': self.ob, 'sigma8': self.sigma8, 'ns': self.ns} col_cosmology.addCosmology('myCosmo', params) colcosmo=col_cosmology.setCosmology('myCosmo') if print_cosmo: print(colcosmo) #Mass_bin = np.logspace(np.log10(Mmin),np.log10(Mmax), num=500) Mh = 10**(np.linspace(np.log10(Mmin),np.log10(Mmax), num=200)) Mh=Mh/self.h #mfunc = mass_function.massFunction(Mh, z, mdef = self.halo_model_mdef, model = self.halo_model, q_out = q_out) mfunc = mass_function.massFunction(Mh, z, mdef = '200m', model = 'tinker08', q_out = q_out) mpc_to_m= 3.086e+22 mfunc*=(mpc_to_m)**-3 dndm=mfunc return Mh, dndm if(hmf_code=='hmf'): from hmf import cosmo as cosmo_hmf my_cosmo = cosmo_hmf.Cosmology() my_cosmo.update(cosmo_params={"H0":self.H0,"Om0":self.om,"Ode0":self.ol,"Ob0":self.ob}) mf=MassFunction(Mmin=np.log10(Mmin), Mmax=np.log10(Mmax), hmf_model= 'ST') mf.update(z=z) mpc_to_m= 3.086e+22 #hm, dndm= mf.m, mf.dndlnm hm, dndm= mf.m/self.h, mf.dndlnm *(mpc_to_m)**-3 #* self.h**4*(mpc_to_m)**-3 # dndm*=(mpc_to_m)**-3 return hm, dndm
def zint(mass, zmin, zmax, nstep=1000): z = np.linspace(zmin, zmax, nstep) integral = 0 if mass > 100: #Shitty check if mass was given as just the exponant mass = np.log(mass) for i in range(len(z) - 1): ztemp = (z[i + 1] + z[i]) / 2 print(i) integrand = lambda m: mass_function.massFunction( m, ztemp, mdef='500m', model='tinker08', q_out='dndlnM') integrated_density = log_int(integrand, mass, 20.5) #Comoving volume computed over full sky i.e. 4pi steradians. Scale accordingly comov_vol = Planck15.comoving_volume(ztemp).value integral += integrated_density * comov_vol * (z[i + 1] - z[i]) return integral
m_min = min( h_mass_crocs.min(), h_mass_cholla.min() ) n_bins = 20 bins = np.linspace( np.log10(m_min), np.log10(m_max), n_bins ) bin_centers, mf_ch, cmf_ch = Get_Mass_Function( h_mass_cholla, bins=bins ) bin_centers, mf_cr, cmf_cr = Get_Mass_Function( h_mass_crocs, bins=bins ) data[snap_id] = { 'bin_centers':bin_centers, 'mf_ch':mf_ch, 'mf_cr':mf_cr } bin_centers = 10**bin_centers mass_bins = bin_centers mass_bins_edges_log = bins delta_logM = mass_bins_edges_log[1:] - mass_bins_edges_log[:-1] model = 'tinker08' z = z_vals[snap_id] dn_dlogM = mass_function.massFunction( mass_bins, z, model=model, mdef='200m', q_out='dndlnM' ) n_halos = dn_dlogM * delta_logM N_halos = n_halos * Lbox**3 / cosmo_h / cosmo_h data[snap_id]['mf_an'] = N_halos figure_width = 6 fig_width = figure_width fig_height = 3*figure_width nrows = 4 ncols = 3 h_length = 4 main_length = 3 label_size = 14
Runo, z=z_snap, derivative=True) * (-3.0) fsigma_unrel = dn_dlnM1_unrel * M1 / rho_m / cosmo.sigma( Runo, z=z_snap, derivative=True) * (-3.0) ftoterr_ = np.sqrt(1 / counts_tot + 0.02**2) * (fsigma_tot) f_tot_err = 1 / np.log(10) * ftoterr_ / fsigma_tot frelerr_ = np.sqrt(1 / counts_rel + 0.02**2) * (fsigma_rel) f_rel_err = 1 / np.log(10) * frelerr_ / fsigma_rel funrelerr_ = np.sqrt(1 / counts_unrel + 0.02**2) * (fsigma_unrel) f_unrel_err = 1 / np.log(10) * funrelerr_ / fsigma_unrel mf_comparat = mf.massFunction(1 / (10**log1sig1_bin), q_in='sigma', z=z_snap, mdef='vir', model='comparat17', q_out='f') ratio_rel = (fsigma_rel - mf_comparat) / mf_comparat ratio_unrel = (fsigma_unrel - mf_comparat) / mf_comparat ratio_comp = (fsigma_tot - mf_comparat) / mf_comparat def Mass_sigma(x): r = cosmo.sigma(1 / 10**x, z=z_snap, inverse=True) M = peaks.lagrangianM(r) #/h return np.log10(M) print('plotting...')
#compute the width of each mass bin in natural log units dlnM = np.diff(np.log(mass_edges)) #compute number density of halos weighted by the mass bin width dn_dlnM = cts / Vol / dlnM #(Mpc/h)^-3 f = dn_dlnM * mass_bins / rho_m / cosmo.sigma(R, z=z_snap, derivative=True) * ( -3.) # (Mpc/h)^-3 * Msun/h * (Msun h^2/Mpc^3)^-1 = number #compute the error: poisson count 1/sqrt(N) and cosmic variance (~2% in HMD) ferr = np.sqrt(1 / cts + 0.02**2) * f #add three models for comparison mf_tinker = mf.massFunction(sigma, q_in='sigma', z=z_snap, mdef='vir', model='tinker08', q_out='f') mf_despali = mf.massFunction(sigma, q_in='sigma', z=z_snap, mdef='vir', model='despali16', q_out='f') mf_comparat = mf.massFunction(sigma, q_in='sigma', z=z_snap, mdef='vir', model='comparat17', q_out='f')
mass_bins_edges_log = np.log(mass_bins_edges) delta_M = mass_bins_edges[1:] - mass_bins_edges[:-1] delta_logM = mass_bins_edges_log[1:] - mass_bins_edges_log[:-1] mass_bins = np.sqrt(mass_bins_edges[:-1] * mass_bins_edges[1:]) radius_bins = get_virial_radius(mass_bins, cosmo_h, rho_crit) T_vals = [] z_vals = np.linspace(0, 100, 1000) model = 'sheth99' for z in z_vals: current_a = 1. / (z + 1) dn_dlogM = mass_function.massFunction( mass_bins, z, model=model, q_out='dndlnM') #Press& Schechter 1974 n_halos = dn_dlogM * delta_logM N_halos = n_halos * Lbox**3 M_halos = N_halos * mass_bins T_halos = get_virial_temp(mass_bins, radius_bins, cosmo_h, z) T_vir_halos = (T_halos * M_halos).sum() / M_total M_igm = M_total - M_halos.sum() T_igm = T_start * (a_start / current_a)**2 * M_igm / M_total T_mean = T_vir_halos + T_igm print( f'z: {z:0.1f} Mfrac_halos: {M_halos.sum()/M_total:.2e} T_mean: {T_mean:.2e}' ) T_vals.append(T_mean) T_vals = np.array(T_vals) data = np.array([z_vals, T_vals])
def dndlnM(M, z): # dndlnM units of (h/Mpc)^3 return mass_function.massFunction(M, z, q_out="dndlnM", model="sheth99")
import matplotlib.pyplot as plt from colossus.cosmology import cosmology from colossus.halo import profile_nfw from colossus.halo.concentration import concentration from colossus.lss.mass_function import massFunction from scipy.special import erf # %% cosmo = cosmology.setCosmology('planck15') z_l = 0.5 z_s = 1.0 log_mvir = np.linspace(10.0, 15.0, 100) dn_dln_mvir = massFunction(10**log_mvir, z_l, q_out='dndlnM') dn = dn_dln_mvir * np.diff(np.log(10**log_mvir))[0] log_mmin = 13.2 sigma_logm = 0.1 n_gal = 0.5 * (1 + erf((log_mvir - log_mmin) / sigma_logm)) dn *= n_gal print('Number density: {:.1e} h^3 Mpc^-3'.format(np.sum(dn))) # %% rp = np.logspace(np.log10(np.amin(zebu.rp_bins)), np.log10(np.amax(zebu.rp_bins)), 200)
area2 = np.zeros(len(z)) area3 = np.zeros(len(z)) area3_tink = np.zeros(len(z)) #Mass Function for planck18 cosmology plt.figure() plt.title('HMF - Planck18', fontsize=25) plt.xlabel(r'$ Mvir\ [M_\odot]$', fontsize=18) plt.ylabel(r'$dn/dln(M)\ [Mpc^{-3}]$', fontsize=18) plt.ylim(1e-7, 1e-1) plt.xscale('log') plt.yscale('log') for i in range(len(z)): mass_func = mf.massFunction(M, z[i], mdef='vir', model='comparat17', q_out='dndlnM') mass_func_tink = mf.massFunction(M, z[i], mdef='200m', model='tinker08', q_out='dndlnM') plt.plot(M, mass_func, label='z=%.1f' % (z[i])) for j in range(len(M) - 1): area[i] = area[i] + mass_func[j] * (M[j + 1] - M[j]) area_simp[i] = simps(mass_func, M) area_tink[i] = simps(mass_func_tink, M) plt.legend() plt.tight_layout() plt.grid(True)
outfig = os.path.join('/home/rseppi/HMF_seppi20', 'figures', 'MF_cosmo.png') cosmo = cosmology.setCosmology('multidark-planck') omega = lambda zz: cosmo.Om0 * (1 + zz)**3. / cosmo.Ez(zz)**2 DeltaVir_bn98 = lambda zz: (18. * np.pi**2. + 82. * (omega(zz) - 1) - 39. * (omega(zz) - 1)**2.) / omega(zz) Mass = np.geomspace(1e12, 8e15, 50) redshift = np.array([0, 2.0]) fig0, ax0 = plt.subplots(1, 1, figsize=(6, 6)) colors = ['C0', 'C1'] for jj, (z, col) in enumerate(zip(redshift, colors)): mfunc = mf.massFunction(Mass, q_in='M', z=z, mdef='vir', model='despali16', q_out='dndlnM') if jj == 0: ax0.loglog(Mass, mfunc, label='Planck', lw=4, ls='solid', c=col) else: ax0.loglog(Mass, mfunc, lw=4, ls='solid', c=col) params1 = { 'flat': True, 'H0': 67.2, 'Om0': 0.35, 'Ob0': 0.049, 'sigma8': 0.85, 'ns': 0.96 }
def MfunctionColossusLog(m, z): return cosmo.h**3 * mass_function.massFunction( m * cosmo.h, z, q_in='M', model='sheth99', q_out='dndlnM')
ax2.set_ylim(-0.1,0.1) ax2.grid(True) plt.tight_layout() outfig_mass = os.path.join(this_dir,'results','zevo','red_%.3f'%(z_snap),'f_spin_integral_Rvir.png') os.makedirs(os.path.dirname(outfig_mass), exist_ok=True) plt.savefig(outfig_mass,overwrite=True) plt.close() def Mass_sigma(x): r=cosmo.sigma(1/10**x,z=z_snap,inverse=True) M=peaks.lagrangianM(r)/h return np.log10(M) # plot multiplicity function f = integral g(sigma,xoff,lambda)dxoff dlambda if(j==0): mf_comparat=mf.massFunction(1/(10**s_bins_all[:len_s_bins[j]]),q_in='sigma', z=z_snap, mdef = 'vir', model = 'comparat17', q_out = 'f') else: mf_comparat=mf.massFunction(1/(10**s_bins_all[low:up]),q_in='sigma', z=z_snap, mdef = 'vir', model = 'comparat17', q_out = 'f') ratio1 = (f_integral_model-mf_comparat)/mf_comparat ratio2 = (f_integral-f_integral_model)/f_integral_model fig = plt.figure(figsize=(10,10)) gs = fig.add_gridspec(3,1) ax1 = fig.add_subplot(gs[0:2, :]) ax2 = fig.add_subplot(gs[2, :]) #yerr = ferr yerr = 1/np.log(10)*err_f_sigma/f_integral #ax1.plot(s_bins,np.log10(f_integral), label='integral data',linewidth=6,c='C9') if(j==0): ax1.fill_between(s_bins_all[:len_s_bins[j]],np.log10(f_integral)-yerr, np.log10(f_integral)+yerr,label='integral data',alpha=0.8,color='C9') ax1.plot(s_bins_all[:len_s_bins[j]],np.log10(f_integral_model), label='seppi20',linewidth=6,c='C0') ax1.plot(s_bins_all[:len_s_bins[j]],np.log10(mf_comparat),label='comparat17',linewidth=2,c='C4')
spin=spin, int_xoff=False, int_sigma=True) f_spin = model_seppi20.seppi20(sigma, z=0, xoff=xoff, spin=spin, int_spin=False, int_sigma=True) f_sigma2 = model_seppi20.seppi20(sigma, z=0, xoff=xoff2, spin=spin2) f_sigma3 = model_seppi20.seppi20(sigma, z=0, xoff=xoff3, spin=spin3) #use bhattacharya11, despali16 and comparat17 for comparison mf_tinker = mf.massFunction(sigma, q_in='sigma', z=0, mdef='vir', model='tinker08', q_out='f') mf_bhattacharya = mf.massFunction(sigma, q_in='sigma', z=0, model='bhattacharya11', q_out='f') mf_comparat = mf.massFunction(sigma, q_in='sigma', z=0, mdef='vir', model='comparat17', q_out='f') mf_despali = mf.massFunction(sigma, q_in='sigma',
def dndlnM_CDM(M, z): return mass_function.massFunction(M, z, q_out="dndlnM", model="sheth99")
def MfunctionColossus(m, z): return 1 / m * cosmo.h**3 * mass_function.massFunction( m * cosmo.h, z, mdef='200m', model='tinker08', q_out='dndlnM')
@author: anirbanroy """ import numpy as np import matplotlib.pyplot as plt from hmf import MassFunction from colossus.lss import mass_function from colossus.cosmology import cosmology cosmology.setCosmology('planck15') Mass_bin = np.logspace(np.log10(1e8), np.log10(1e15), num=100) mfunc = mass_function.massFunction(Mass_bin, 6.0, mdef='200c', model='tinker08', q_out='dndlnM') mf = MassFunction(z=6.0, hmf_model="Tinker08", mdef_model='SOCritical') plt.plot(mf.m, mf.dndm, label="HMF") plt.plot(Mass_bin, mfunc, label="colossus") plt.xscale('log') plt.yscale('log') plt.xlabel(r"Mass, $[h^{-1}M_\odot]$") plt.ylabel(r"$dn/dm$, $[h^{4}{\rm Mpc}^{-3}M_\odot^{-1}]$")
rho_Mpc = cosmo.rho_m(0.0) * 1E9 mass_func_model = -1 / 3 * f * rho_Mpc / Mass * d_ln_sigma_d_ln_R return mass_func_model mass_func_model = mass_function_rseppi(mass_bins_pl, A0, a0, p0, q0) #print('model values = ', mass_func_model) #mass_func_model=mf.massFunction(mass_bins_pl,z=z,mdef = 'vir', model = 'tinker08', q_out = 'dndlnM') figure, ax = plt.subplots(2, 1) ax[0].loglog() ax[1].set_xlabel(r'M [$M_{\odot}$/h]') ax[1].set_ylabel('ratio') ax[0].set_ylabel(r'dn/dlnM [$(Mpc/h)^{-3}$]') mf_test = mf.massFunction(mass_bins_pl, z=z, mdef='fof', model='bhattacharya11', q_out='dndlnM') ax[0].plot(mass_bins_pl, mf_test, label='Bhattacharya11') ax[0].plot(mass_bins_pl, mass_func_model, label='model_rseppi') ax[1].plot(mass_bins_pl, mass_func_model / mf_test, color='r') ax[0].legend() #plt.show() # Use MCMC method - emcee #import lmfit import pymultinest from tqdm import tqdm #import emcee #p=lmfit.Parameters() #p.add_many(('A0', 0.333, True, 0.001,0.5),('a0', 0.788, True,0.2,4.0),('p0', 0.807, True,-2.0,7.0),('q0', 1.795, True,0.5,10.0))