def test_redshifts_from_comoving_density(): from skypy.galaxy.redshift import redshifts_from_comoving_density from astropy.cosmology import LambdaCDM # random cosmology H0 = np.random.uniform(50, 100) Om = np.random.uniform(0.1, 0.9) Ol = np.random.uniform(0.1, 0.9) cosmo = LambdaCDM(H0=H0, Om0=Om, Ode0=Ol) # fixed comoving density of Ngal galaxies total Ngal = 1000 redshift = np.arange(0.0, 2.001, 0.1) density = Ngal/cosmo.comoving_volume(redshift[-1]).to_value('Mpc3') fsky = 1.0 # sample galaxies over full sky without Poisson noise z_gal = redshifts_from_comoving_density(redshift, density, fsky, cosmo, noise=False) # make sure number of galaxies is correct (no noise) assert np.isclose(len(z_gal), Ngal, atol=1, rtol=0) # test the distribution of the sample against the analytical CDF V_max = cosmo.comoving_volume(redshift[-1]).to_value('Mpc3') D, p = kstest(z_gal, lambda z: cosmo.comoving_volume(z).to_value('Mpc3')/V_max) assert p > 0.01, 'D = {}, p = {}'.format(D, p)
def test_redshifts_from_comoving_density(): from skypy.galaxy.redshift import redshifts_from_comoving_density from astropy.cosmology import LambdaCDM from astropy import units # random cosmology H0 = np.random.uniform(50, 100) Om = np.random.uniform(0.1, 0.9) Ol = np.random.uniform(0.1, 0.9) cosmo = LambdaCDM(H0=H0, Om0=Om, Ode0=Ol) # fixed comoving density of Ngal galaxies total Ngal = 1000 redshift = np.arange(0.0, 2.001, 0.1) density = Ngal / cosmo.comoving_volume(redshift[-1]).to_value('Mpc3') sky_area = 4 * np.pi * units.steradian # sample galaxies over full sky without Poisson noise z_gal = redshifts_from_comoving_density(redshift, density, sky_area, cosmo, noise=False) # make sure number of galaxies is correct (no noise) assert np.isclose(len(z_gal), Ngal, atol=1, rtol=0) # test the distribution of the sample against the analytical CDF V_max = cosmo.comoving_volume(redshift[-1]).to_value('Mpc3') D, p = kstest(z_gal, lambda z: cosmo.comoving_volume(z).to_value('Mpc3') / V_max) assert p > 0.01, 'D = {}, p = {}'.format(D, p) # Test that an error is raised if sky_area has the wrong units sky_area = 1 * units.degree with pytest.raises(units.UnitsError): redshifts_from_comoving_density(redshift, density, sky_area, cosmo, noise=False) # Test that an error is raised if sky_area has no units sky_area = 1 with pytest.raises(TypeError): redshifts_from_comoving_density(redshift, density, sky_area, cosmo, noise=False)
def choose_z_volume_SFR(n_req=10000, zmax=8.0, binsize=0.01): """ :return: """ z = np.arange(0.0, zmax, binsize) z_dz = np.arange(0.0 + binsize, zmax + binsize, binsize) cosmo = LambdaCDM(H0=70, Om0=0.3, Ode0=0.7) v_z = cosmo.comoving_volume(z) v_z_dz = cosmo.comoving_volume(z_dz) v_dz = v_z_dz - v_z norm_v_dz = v_dz / np.nanmax(v_dz) sfr_z = calculate_SFR(z) sfr_norm = sfr_z / np.nanmax(sfr_z) volumetric_rate = norm_v_dz * sfr_norm normed_volumetric_rate = volumetric_rate / np.nanmax(volumetric_rate) pdf = InterpolatedUnivariateSpline(z, normed_volumetric_rate) n = 0 z_sim = [] while n < n_req: x = np.random.random() * zmax y = np.random.random() if y <= pdf(x): z_sim.append(x) n += 1 return z_sim
simlib07 = SIMLIBReader(simlib_file=simlib_file07) simlib_array = np.array([simlib05, simlib06, simlib07]) # mag_offsets = lsstt.sims.choose_magoffset(n = n_sne_req) # z_obs = lsstt.sims.choose_z_volume_SFR(n_req=n_sne_req, zmax=z_max) ## This is faster than choose_z_volume_SFR - ## choose_z_volume_SFR is faster to generate large numbers binsize = 0.01 z = np.arange(0.0, z_max, binsize) z_dz = np.arange(0.0 + binsize, z_max + binsize, binsize) cosmo = LambdaCDM(H0=70, Om0=0.3, Ode0=0.7) v_z = cosmo.comoving_volume(z) v_z_dz = cosmo.comoving_volume(z_dz) v_dz = v_z_dz - v_z norm_v_dz = v_dz / np.nanmax(v_dz) sfr_z = lsstt.sims.calculate_SFR(z) sfr_norm = sfr_z / np.nanmax(sfr_z) volumetric_rate = norm_v_dz * sfr_norm normed_volumetric_rate = volumetric_rate / np.nanmax(volumetric_rate) ## Generate probability density function pdf = InterpolatedUnivariateSpline(z, normed_volumetric_rate) info = pcc.InfoClass()
return sky_distances ################################################ if not build_skydistances: print("Skipping Sky Distances...") else: ## Build Sky Distance Objects ## print("\n\nBuilding Sky Distances...") sky_distances = [] for i in range(len(distance_at_resolution_change)): start_vol = 0.0 * u.Mpc**3 end_vol = cosmo.comoving_volume( (Distance(distance_at_resolution_change[i], u.Mpc)).z) start_distance = 0.0 * u.Mpc if i > 0: start_vol = cosmo.comoving_volume( (Distance(distance_at_resolution_change[i - 1], u.Mpc)).z) start_distance = sky_distances[-1].D2 * u.Mpc sky_distances += compute_sky_distance(start_vol, end_vol, steps_in_resolution_bin[i], start_distance, frac[i], nsides[i]) if isDEBUG: print("\nGenerated distances:\n") for d in sky_distances:
def ChooseTABLE_z_V(MODnamef): OmegaMf = 0.3 # Omega M OmegaLf = 0.7 # Omega vac OmegaKf = 1. - OmegaMf - OmegaLf Hcf = 70.0 ######### TABLE V - Z ########### print " # DEFAULT COSMOLOGY: OmegaM = 0.3, OmegaL = 0.7, Hubble constant = 70 (km/s)/Mpc" print " # CHOSEN TABLE redshift(z) - Comoving Volume (CV): VOLUME_table_0p004_Om03_H70.txt #" print " # > step in z is: dz = step in z from the RATE #" print " # Enter 0 if that is ok #" print " # Enter 1 if you want the more refined table VOLUME_table_em6_Om03_H70.txt #" print " # Enter 2 if you want a more refined table to be built #" print " # Enter 3 if you want to change cosmology and redefined the table #" print " # NB: OPTION 3 AVAILABLE ONLY FOR User and Mandel rate models for concistency #" invalid_option = True NameFILE_TABf = "" while invalid_option: OPT = input(" # Enter the number: ") if (OPT == 0): invalid_option = False NameFILE_TABf = "VOLUME_table_0p004_Om03_H70.txt" TABzVCf = prefix + "z_CV_TABLES/" + NameFILE_TABf #sh.cp(TABzVC,"TEMP/.") elif (OPT == 1): invalid_option = False NameFILE_TABf = "VOLUME_table_em6_Om03_H70.txt" TABzVCf = prefix + "z_CV_TABLES/" + NameFILE_TABf elif (OPT == 2): invalid_option = False dz = input("Enter desired resolution in redshift dz: ") from astropy.cosmology import FlatLambdaCDM Vp = cosmo.comoving_volume(1) cosmo_FL = FlatLambdaCDM(H0=70, Om0=0.3) MINz = 0.0 MAXz = np.max(zR) ROUGH_dV_Om03_H70 = [] ROUGH_Vcum_Om03_H70 = [] Z_Vec = [] zi = MINz V_old = cosmo_FL.comoving_volume(zi) if (zi > 0.0000): Z_Vec.append(zi) ROUGH_dV_Om03_H70.append(V_old.value * Mpc**3) ROUGH_Vcum_Om03_H70.append(V_old.value * Mpc**3) while zi < MAXz: zi = zi + dz Z_Vec.append(zi) Vi = cosmo_FL.comoving_volume(zi) DV_i = Vi.value - V_old.value ROUGH_dV_Om03_H70.append(DV_i * Mpc**3) ROUGH_Vcum_Om03_H70.append(Vi.value * Mpc**3) V_old = Vi print len(ROUGH_dV_Om03_H70) print len(ROUGH_Vcum_Om03_H70) print len(Z_Vec) NameFILE_TABf = "VOLUME_table_%.2E_Om03_H70.txt" % dz TABzVCf = prefix + "z_CV_TABLES/" + NameFILE_TABf print TABzVCf with open(TABzVCf, 'w') as f: writer = csv.writer(f, delimiter='\t') writer.writerows( zip(Z_Vec, ROUGH_dV_Om03_H70, ROUGH_Vcum_Om03_H70)) elif (OPT == 3): if (MODnamef != "User" and MODnamef != "Mandel2017"): print " # Invalid option for rate ", MODnamef continue invalid_option = False invalidCOSM = True while invalidCOSM: Ode0 = input('# Cosmological constant: ') Om0 = input('# Matter constant: ') OmegaKf = input('# Curvature : ') H0 = input('# Hubble constant [km/s/Mpc]: ') if (np.abs(Ode0 + Om0 + OmegaKf - 1.) < 0.000001): invalidCOSM = False else: print " # INVALID COSMSOLOGY, constants not sum to 1" if OmegaKf == 0: from astropy.cosmology import FlatLambdaCDM Vp = cosmo.comoving_volume(1) cosmo_FL = FlatLambdaCDM(H0, Om0) dz = input('# Enter desired resolution in redshift dz: ') MINz = 0.0 MAXz = input( '# Enter maximum redshift for table building (please consider the redshifts required by the rate model): ' ) MAXz = MAXz + dz ROUGH_dV = [] ROUGH_Vcum = [] Z_Vec = [] zi = MINz V_old = cosmo_FL.comoving_volume(zi) if (zi > 0.0000): Z_Vec.append(zi) ROUGH_dV.append(V_old.value * Mpc**3) ROUGH_Vcum.append(V_old.value * Mpc**3) while zi < MAXz: zi = zi + dz Z_Vec.append(zi) Vi = cosmo_FL.comoving_volume(zi) DV_i = Vi.value - V_old.value ROUGH_dV.append(DV_i * Mpc**3) ROUGH_Vcum.append(Vi.value * Mpc**3) V_old = Vi print len(ROUGH_dV) print len(ROUGH_Vcum) print len(Z_Vec) NameFILE_TABf = "VOLUME_table_%.2E_Ode%.2f_Om%.2f_H%2.1f.txt" % ( dz, Ode0, Om0, H0) TABzVCf = prefix + "z_CV_TABLES/" + NameFILE_TABf print TABzVCf with open(TABzVCf, 'w') as f: writer = csv.writer(f, delimiter='\t') writer.writerows(zip(Z_Vec, ROUGH_dV, ROUGH_Vcum)) else: from astropy.cosmology import LambdaCDM Vp = cosmo.comoving_volume(1) cosmo_LCDM = LambdaCDM(H0, Om0, Ode0) dz = input('# Enter desired resolution in redshift dz: ') MINz = 0.0 MAXz = input( '# Enter maximum redshift for table building (please consider the redshifts required by the rate model): ' ) ROUGH_dV = [] ROUGH_Vcum = [] Z_Vec = [] zi = MINz V_old = cosmo_LCDM.comoving_volume(zi) if (zi > 0.0000): Z_Vec.append(zi) ROUGH_dV.append(V_old.value * Mpc**3) ROUGH_Vcum.append(V_old.value * Mpc**3) while zi < MAXz: zi = zi + dz Z_Vec.append(np.round(zi, 2)) Vi = cosmo_LCDM.comoving_volume(zi) DV_i = Vi.value - V_old.value ROUGH_dV.append(DV_i * Mpc**3) ROUGH_Vcum.append(Vi.value * Mpc**3) V_old = Vi print len(ROUGH_dV) print len(ROUGH_Vcum) print len(Z_Vec) NameFILE_TABf = "VOLUME_table_%.2E_Om%.2f_H%2.1f.txt" % ( dz, Om0, H0) TABzVCf = prefix + "z_CV_TABLES/" + NameFILE_TABf print TABzVCf with open(TABzVCf, 'w') as f: writer = csv.writer(f, delimiter='\t') writer.writerows(zip(Z_Vec, ROUGH_dV, ROUGH_Vcum)) Hcf = H0 OmegaLf = Ode0 OmegaMf = Om0 else: print "Invalid OPTION" print " ############################################################################################################" outTAB_Qf = [NameFILE_TABf, TABzVCf, Hcf, OmegaMf, OmegaLf, OmegaKf] return outTAB_Qf
def com_vol(z_2, z_1=0): obj = LambdaCDM(H0=69.6, Om0=0.286, Ode0=1 - 0.286) vol_2 = obj.comoving_volume(z_2).value # MPC^3 vol_1 = obj.comoving_volume(z_1).value # MPC^3 vol = vol_2 - vol_1 return vol