def mass_sigma(mas): r = peaks.lagrangianR(10**mas) #Mpc/h if bool( r.any() ) == False: #need this check because matplotlib can check empty values to create new axis and cosmo.sigma needs an np.array sigm = np.array([]) else: sigm = cosmo.sigma(r, z=z_snap) x_ = np.log10(1 / sigm) return x_
def mass_function_rseppi(Mass): cosmo = cosmology.getCurrent() delta_c = peaks.collapseOverdensity(z=z) R = peaks.lagrangianR(Mass) sigma = cosmo.sigma(R=R, z=z) nu = delta_c / sigma nu2 = nu**2 zp1 = 1.0 + z A = 0.333 * zp1**-0.11 a = 0.788 * zp1**-0.01 p = 0.807 q = 1.795 f = A * np.sqrt(2 / np.pi) * np.exp( -a * nu2 * 0.5) * (1.0 + (nu2 * a)**-p) * (nu * np.sqrt(a))**q d_ln_sigma_d_ln_R = cosmo.sigma(R, z, derivative=True) 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
def peak_mass(x): M=10**x r=peaks.lagrangianR(M)#*h) sigma=cosmo.sigma(r,z=0) nu=dc/sigma return nu
for i, p2s in enumerate(path_2_snapshot_data): print('HMD') print(i,' of ', len(path_2_snapshot_data)) aexp = float(os.path.basename(p2s[:-8]).split('_')[1]) z_snap = 1/aexp -1 print('z=%.3g'%(z_snap)) E_z = cosmo.Ez(z=z_snap) Vol1 = (4e3/(1+z_snap))**3 dc = peaks.collapseOverdensity(z = z_snap) rho_m = cosmo.rho_m(z=z_snap)*1e9 hd1 = fits.open(p2s) mass1=hd1[1].data['Mvir'] logmass1 = np.log10(mass1) R_1 = peaks.lagrangianR(mass1) sigf_1 = cosmo.sigma(R_1,z=z_snap) log1_sigf_1 = np.log10(1/sigf_1) Rvir1 = hd1[1].data['Rvir'] Rs1 = hd1[1].data['Rs'] xoff_data1 = np.log10(hd1[1].data['Xoff']/hd1[1].data['Rvir']) spin1 = hd1[1].data['Spin'] spinpar1 = hd1[1].data['Spin_Bullock'] conc1 = Rvir1/Rs1 peak_bins = np.arange(0.9,5.,0.02) peak_array = (peak_bins[:-1]+peak_bins[1:])/2. def get_average(x,sel): return np.average(x[sel]),np.std(x[sel]),np.sum(sel)
displ_lc_concat_sort_mann = np.sort(displ_lightcone_concat_mann_) cdf_lightcone_concat_mann = np.arange( 1, len(displ_lc_concat_sort_mann) + 1) / len(displ_lc_concat_sort_mann) #make prediction from the model model = Table.read(path_2_model) pars_model = model['pars'] zevo = Table.read(path_2_zevo) zevo_pars = zevo['pars'] parameters = np.append(pars_model, zevo_pars) #colossus wants masses in Msun/h, so if I want to use physical 5e13 Msun, I will give him 5e13*h= 3.39e13 Msun/h M1 = 5e13 * h R1 = peaks.lagrangianR(M1) sigma1 = cosmo.sigma(R1, z=0) log1_sigma1 = np.log10(1 / sigma1) M2 = 2e14 * h R2 = peaks.lagrangianR(M2) sigma2 = cosmo.sigma(R2, z=0) log1_sigma2 = np.log10(1 / sigma2) M3 = 1e15 * h R3 = peaks.lagrangianR(M3) sigma3 = cosmo.sigma(R3, z=0) log1_sigma3 = np.log10(1 / sigma3) print('%.3g Msun' % (M1 / h), ' is ', log1_sigma1) print('%.3g Msun' % (M2 / h), ' is ', log1_sigma2)
for i, p2s in enumerate(path_2_snapshot_data): print('HMD') print(i, ' of ', len(path_2_snapshot_data)) aexp = float(os.path.basename(p2s[:-8]).split('_')[1]) z_snap = 1 / aexp - 1 print('z=%.3g' % (z_snap)) E_z = cosmo.Ez(z=z_snap) Vol1 = (4e3 / (1 + z_snap))**3 dc = peaks.collapseOverdensity(z=z_snap) rho_m = cosmo.rho_m(z=z_snap) * 1e9 hd1 = fits.open(p2s) mass1 = hd1[1].data['Mvir'] logmass1 = np.log10(mass1) R_1 = peaks.lagrangianR(mass1) sigf_1 = cosmo.sigma(R_1, z=z_snap) log1_sigf_1 = np.log10(1 / sigf_1) Rvir1 = hd1[1].data['Rvir'] Rs1 = hd1[1].data['Rs'] xoff_data1 = np.log10(hd1[1].data['Xoff']) #/hd1[1].data['Rvir']) spin1 = hd1[1].data['Spin'] spinpar1 = hd1[1].data['Spin_Bullock'] conc1 = Rvir1 / Rs1 peak_bins = np.arange(0.9, 5., 0.02) peak_array = (peak_bins[:-1] + peak_bins[1:]) / 2. def get_average(x, sel): return np.average(x[sel]), np.std(x[sel]), np.sum(sel)
choose_snap = 3 aexp = float( os.path.basename(path_2_snapshot_data[choose_snap][:-8]).split('_')[1]) z_snap = 1 / aexp - 1 print('z=%.3g' % (z_snap)) cosmo = cosmology.setCosmology('multidark-planck') E_z = cosmo.Ez(z=z_snap) Vol1 = (4e3 / (1 + z_snap))**3 dc = peaks.collapseOverdensity(z=z_snap) rho_m = cosmo.rho_m(z=z_snap) * 1e9 h = cosmo.Hz(z=0) / 100 hd1 = fits.open(path_2_snapshot_data[choose_snap]) mass1 = hd1[1].data['Mvir'] logmass1 = np.log10(mass1) R_1 = peaks.lagrangianR(mass1) sigf_1 = cosmo.sigma(R_1, z=z_snap) log1_sigf_1 = np.log10(1 / sigf_1) Rvir1 = hd1[1].data['Rvir'] Rs1 = hd1[1].data['Rs'] #xoff = np.log10(hd1[1].data['Xoff']/1000)#/hd1[1].data['Rvir']) xoff = np.log10(hd1[1].data['Xoff']) #/hd1[1].data['Rvir']) #spin1 = hd1[1].data['Spin'] spinpar1 = hd1[1].data['Spin'] conc1 = Rvir1 / Rs1 print('min = ', min(log1_sigf_1)) print('max = ', max(log1_sigf_1)) #sys.exit() if choose_snap == 0:
#open the data print('Reading halos...') hd1 = fits.open(file_dir) mass = hd1[1].data['Mvir'] #Msun/h #now we start computing the number density of halos #compute number of halos in mass bins print('Computing mass function...') mass_edges = 10**np.linspace( np.log10(np.min(mass)) + 0.5, np.log10(np.max(mass)), 50) cts = np.histogram(mass, bins=mass_edges)[0] #number mass_bins = (mass_edges[1:] + mass_edges[:-1]) / 2. #compute lagrangian radius corresponding to each middle value of mass bin R = peaks.lagrangianR(mass_bins) # Mpc/h #compute rms variance inside these radii sigma = cosmo.sigma(R, z=z_snap) #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
print('HMD') aexp = float(os.path.basename(path_2_snapshot_data[0][:-8]).split('_')[1]) z_snap = 1/aexp -1 print('z=%.3g'%(z_snap)) cosmo = cosmology.setCosmology('multidark-planck') E_z = cosmo.Ez(z=z_snap) Vol1 = (4e3/(1+z_snap))**3 dc = peaks.collapseOverdensity(z = z_snap) rho_m = cosmo.rho_m(z=z_snap)*1e9 h = cosmo.Hz(z=0)/100 hd1 = fits.open(path_2_snapshot_data[0]) mass1=hd1[1].data['Mvir'] logmass1 = np.log10(mass1) R_1 = peaks.lagrangianR(mass1) sigf_1 = cosmo.sigma(R_1,z=z_snap) log1_sigf_1 = np.log10(1/sigf_1) Rvir1 = hd1[1].data['Rvir'] Rs1 = hd1[1].data['Rs'] xoff = np.log10(hd1[1].data['Xoff'])#/hd1[1].data['Rvir']) conc1 = Rvir1/Rs1 print('min = ',min(log1_sigf_1)) print('max = ',max(log1_sigf_1)) #sys.exit() #log1sig_intervals = [-0.15, -0.11, -0.08, -0.01, 0.07, 0.20, 0.4] #log1sig_intervals = [-0.1, 0.08, 0.11, 0.14, 0.19, 0.22, 0.44] #log1sig_intervals = [-0.05, 0.13, 0.16, 0.2, 0.25, 0.28, 0.48] #log1sig_intervals = [0.1, 0.18, 0.2, 0.23, 0.27, 0.3, 0.52]
def solve_spherical_collapse_for_M(zi, zg, M, delta_i, cosmo_model): """ setup initial conditions and construct a solution of spherical collapse model for an initial perturbation with specified mass M, initial overdensity delta_i at z_i for a cosmological model specified in the dictionary cosmo_model (to be passed to the colossus routines). WARNING! This routine is currently set up to produce solutions ONLY FOR COSMOLOGIES WITHOUT DARK ENERGY (Omega_Lambda=0) Parameters: ----------- zi: float initial redshift zg: array of floats grid of redshifts for which to compute solution M: float mass of the perturbation in /h Msun delta_i: float initial overdensity cosmo_model: colossus cosmology dictionary with the desired cosmology Returns: -------- R: float numpy vector of physical radii of the perturbation at different t t: float numpy vector: the times at which solution is given RH: float numpy vector, expansion factor of the universe normalized to the initial physical radius of the perturbation, delta: float numpy vector: overdensities of spherical collapse solution at t """ cosmo = cosmology.setCosmology('runcosmo', cosmo_model ) ag = 1.0/(1.+zg) ai = 1.0/(1.+zi) ti = cosmo.age(zi) Omz = cosmo.Om(zi) # Omega_m(zi) R_L = peaks.lagrangianR(M) Mi = M / cosmo.h # corresponding peak height nu_i = 1.69 / cosmo.sigma(R_L, z=0, j=0) # Lagrangian radius of the perturbation # Actual initial physical radius of the perturbation in Mpc Rip = R_L * ai / (1.0 + delta_i)**(1./3.) / cosmo.h #DDr = growth_factor_derivative() # dDdt = dD_+/dt = dD_+/dz*dz/dt dDdt = cosmo.growthFactor(zi, derivative=1) * cosmo.age(ti, derivative=1, inverse=True) # DDr = dD_+(ti)/dt/ D_+(ti) DDr = dDdt/cosmo.growthFactor(zi)/(1.e9*yr) #DDr2 = cosmo.Hz(zi) * 3.24078e-20 # initial velocity VHi = cosmo.Hz(zi) * Rip Vi = VHi - Rip * Mpc2km/3.*delta_i/(1.+delta_i)*DDr delta_lin0 = delta_i / cosmo.growthFactor(zi) if (Omz < 1.0) and (delta_i < 1./Omz-1.): print("error: perturbation with specified parameters will not collapse") print("error: delta_i must be smaller than 1/Omega(zi)-1 for open cosmologies") return if delta_lin0 < 1.69: print("warning: delta_lin(z=0)=%.3f and perturbation will not collapse by z=0"%delta_lin0) A, B, Ei = AB(Rip, Mi, Vi) tcoll = B * 2.0 * np.pi tuni = cosmo.age(zg) eta = np.zeros_like(tuni) # for each time solve for corresponding eta for i, td in enumerate(tuni): toB = td/B eta[i] = fsolve(eta_func, x0=1., args=(toB))[0] # compute solution for perturbation evolution in the parametric form t = B * (eta - np.sin(eta)) + ti R = A * (1.0 - np.cos(eta)) # expansion factor normalized to the size of the pertubation at zi RH = Rip * (ag/ai) # mean density of the universe at each z of the grid rhom = cosmo.rho_m(zg)*cosmo.h**2 # h^2 Msun/kpc^3 -> Msun/Mpc^3 # density within collapsing perturbation rho = 3.0 * Mi/(4.*np.pi*(np.clip(R,1.e-7,1.e30))**3) * 1.e-9 # overdensity of the perturbation at redshifts in zg delta = rho/rhom - 1.0 return R, t, RH, delta, tcoll
#import colossus from colossus.cosmology import cosmology from colossus.lss import mass_function as mf from colossus.lss import peaks import numpy as np #import matplotlib and numpy import matplotlib.pyplot as plt import numpy as np #set cosmology cosmo = cosmology.setCosmology('multidark-planck') #define variance, xoff, spin arrays mass_ = np.logspace(13.5, 16, 60) R = peaks.lagrangianR(mass_) sigma = cosmo.sigma(R, z=0) print(sigma) xoff = np.logspace(-3.5, -0.3, 50) spin = np.logspace(-3.5, -0.3, 50) xoff2 = np.logspace(-3.5, -1.5, 30) spin2 = np.logspace(-3.5, -1.5, 30) xoff3 = np.array([0.1]) spin3 = np.array([0.1]) #build model and integrate it g_sigma_xoff = model_seppi20.seppi20(sigma, z=0, xoff=xoff, int_xoff=False) g_sigma_spin = model_seppi20.seppi20(sigma, z=0, spin=spin, int_spin=False) g_xoff_spin = model_seppi20.seppi20(sigma=None,
cosmo = cosmology.setCosmology('multidark-planck') h = cosmo.Hz(0) / 100 E_z = cosmo.Ez(z=z_snap) Vol1 = (4e3 / (1 + z_snap))**3 dc = peaks.collapseOverdensity(z=z_snap) rho_m = cosmo.rho_m(z=z_snap) * 1e9 R = cosmo.sigma(1 / 10**(-0.1), z=0, inverse=True) M = peaks.lagrangianM(R) print(M / h) R = cosmo.sigma(1 / 10**(0.19), z=0, inverse=True) M = peaks.lagrangianM(R) print(M / h) #sys.exit() print('1e15 Msun is log1_sigma = ', np.log10(1 / cosmo.sigma(peaks.lagrangianR(1e15 * h), z=z_snap))) print('1e14 Msun is log1_sigma = ', np.log10(1 / cosmo.sigma(peaks.lagrangianR(1e14 * h), z=z_snap))) print('1e13 Msun is log1_sigma = ', np.log10(1 / cosmo.sigma(peaks.lagrangianR(1e13 * h), z=z_snap))) print('reading data...') hd1 = fits.open(path_2_snapshot_data[0]) mass1 = hd1[1].data['Mmvir_all'] logmass1 = np.log10(mass1) R_1 = peaks.lagrangianR(mass1) sigf_1 = cosmo.sigma(R_1, z=z_snap) log1_sigf_1 = np.log10(1 / sigf_1) Rvir1 = hd1[1].data['Rvir'] Rs1 = hd1[1].data['Rs'] xoff_data1 = (hd1[1].data['Xoff']) #/hd1[1].data['Rvir'])
print('processing ',z_snap,' ....') E_z = cosmo.Ez(z=z_snap) h = cosmo.Hz(z=0.0)/100 Vol1 = (4e3/(1+z_snap))**3 Vol2 = (2.5e3/(1+z_snap))**3 Vol3 = (1e3/(1+z_snap))**3 dc = peaks.collapseOverdensity(z = z_snap) print(dc) rho_m = cosmo.rho_m(z=z_snap)*1e9 hd1 = fits.open(p2d4_0) #mass1_=hd1[1].data['Mmvir_all'] mass1_=hd1[1].data['Mvir'] R1 = peaks.lagrangianR(mass1_) sigma1 = cosmo.sigma(R1,z=z_snap) log1_sigma1 = np.log10(1/sigma1) logxoff_data1 = np.log10(hd1[1].data['Xoff']/hd1[1].data['Rvir']) log_spin1 = np.log10(hd1[1].data['Spin']) hd2 = fits.open(path_2_snapshot_data2_5[i]) mass2_=hd2[1].data['Mvir'] R2 = peaks.lagrangianR(mass2_) sigma2 = cosmo.sigma(R2,z=z_snap) log1_sigma2 = np.log10(1/sigma2) logxoff_data2 = np.log10(hd2[1].data['Xoff']/hd2[1].data['Rvir']) log_spin2 = np.log10(hd2[1].data['Spin']) hd3 = fits.open(path_2_snapshot_data1_0[i])