def virial_radius(logMhalo, redshift, delta=200., rhocrit=True, BryanNorman=False, WMAP=False, COSHalos=False): """Calculate the virial radius of a galaxy. --- Inputs: --- logMhalo: Halo mass(es). redshift: Galaxy redshift delta=200: Overdensity (Default=200) rhocrit: Use the critical density (Default=True); alternately the mean density of the universe. WMAP: Use WMAP9 cosmology. (Default=False) BryanNorman: Use the Bryan & Norman (1998) scaling (Default=False) COSHalos: Use the COS-Halos assumptions (Default=False) """ import numpy as np import astropy.units as u import astropy.constants as c # Set some terms based on the COSHalos flag, which matches the calculations # from the COS-Halos work (Prochaska+ 2017). # # This overrides the other user-set flags. if COSHalos: BryanNorman=True WMAP=True rhocrit=True # Choose the cosmology. Default is Plank15. if WMAP: from astropy.cosmology import WMAP9 as cosmo else: from astropy.cosmology import Planck15 as cosmo # Use the Bryan & Norman (2008) definition of Rvir? Default: False # # This overrides user-set flags for delta and rhocrit. if BryanNorman: # Overdensity depends on redshift: x = cosmo.Om(redshift)-1. delta = (18.*np.pi**2+82.*x-39.*x**2) # Also assume critical density scaling: rhocrit = True # Choose whether to scale by mean or critical density. Default: Critical if rhocrit == True: rho = cosmo.critical_density(redshift) else: rho = cosmo.Om(redshift)*cosmo.critical_density(redshift) # Linear halo mass (requires logMhalo in numpy array for calculations. Mhalo = (10.**np.array(logMhalo))*u.M_sun # Calculate the virial radius. Rvir3 = (3./(4.*np.pi))*(Mhalo/(delta*rho.to('Msun/kpc3'))) Rvir = (Rvir3)**(1./3.) return Rvir
def test_delta_vir(self): """ Compute the calculated value of `~halotools.empirical_models.profile_helpers.delta_vir` at high-redshift where :math:`\\Omega_{\\rm m} = 1` should be a good approximation, and compare it to the analytical top-hat collapse result in this regime. """ bn98_result = delta_vir(WMAP9, 10.0) assert np.allclose(bn98_result, 18. * np.pi**2, rtol=0.01) # Choose a high-redshift where Om = 1 is a good approximation z = 10 rho_crit = WMAP9.critical_density(z) rho_crit = rho_crit.to(u.Msun / u.Mpc**3).value / WMAP9.h**2 rho_m = WMAP9.Om(z) * rho_crit wmap9_delta_vir_z10 = density_threshold(WMAP9, z, 'vir') / rho_m assert np.allclose(wmap9_delta_vir_z10, bn98_result, rtol=0.01)
def test_delta_vir(): r""" Compute the calculated value of `~halotools.empirical_models.halo_boundary_functions.delta_vir` at high-redshift where :math:`\Omega_{\rm m} = 1` should be a good approximation, and compare it to the analytical top-hat collapse result in this regime. """ bn98_result = delta_vir(WMAP9, 10.0) assert np.allclose(bn98_result, 18.*np.pi**2, rtol=0.01) # Choose a high-redshift where Om = 1 is a good approximation z = 10 rho_crit = WMAP9.critical_density(z) rho_crit = rho_crit.to(u.Msun/u.Mpc**3).value/WMAP9.h**2 rho_m = WMAP9.Om(z)*rho_crit wmap9_delta_vir_z10 = density_threshold(WMAP9, z, 'vir')/rho_m assert np.allclose(wmap9_delta_vir_z10, bn98_result, rtol=0.01)
def test_density_threshold(self): """ Verify that the `~halotools.empirical_models.profile_helpers.density_threshold` method returns the correct multiple of the appropriate density contrast over a range of redshifts and cosmologies. """ zlist = [0, 1, 5, 10] for z in zlist: for cosmo in (WMAP9, Planck13): rho_crit = WMAP9.critical_density(z) rho_crit = rho_crit.to(u.Msun / u.Mpc**3).value / WMAP9.h**2 rho_m = WMAP9.Om(z) * rho_crit wmap9_200c = density_threshold(WMAP9, z, '200c') / rho_crit assert np.allclose(wmap9_200c, 200.0, rtol=0.01) wmap9_2500c = density_threshold(WMAP9, z, '2500c') / rho_crit assert np.allclose(wmap9_2500c, 2500.0, rtol=0.01) wmap9_200m = density_threshold(WMAP9, z, '200m') / rho_m assert np.allclose(wmap9_200m, 200.0, rtol=0.01)
def test_density_threshold(): r""" Verify that the `~halotools.empirical_models.halo_boundary_functions.density_threshold` method returns the correct multiple of the appropriate density contrast over a range of redshifts and cosmologies. """ zlist = [0, 1, 5, 10] for z in zlist: for cosmo in (WMAP9, Planck13): rho_crit = WMAP9.critical_density(z) rho_crit = rho_crit.to(u.Msun/u.Mpc**3).value/WMAP9.h**2 rho_m = WMAP9.Om(z)*rho_crit wmap9_200c = density_threshold(WMAP9, z, '200c')/rho_crit assert np.allclose(wmap9_200c, 200.0, rtol=0.01) wmap9_2500c = density_threshold(WMAP9, z, '2500c')/rho_crit assert np.allclose(wmap9_2500c, 2500.0, rtol=0.01) wmap9_200m = density_threshold(WMAP9, z, '200m')/rho_m assert np.allclose(wmap9_200m, 200.0, rtol=0.01)
import numpy as np from astropy.cosmology import WMAP9 as cosmo from astropy import units as u ''' This code calculate the half-mode mass of WDM ''' wdm_m = 2.0 # keV Owdm = cosmo.Om0 h=cosmo.h mu=1.12 rho = cosmo.critical_density(0) # g/cm^3 rho = rho.to(u.Msun/u.Mpc**3) alpha = 0.049*(wdm_m)**(-1.11)*(Owdm/0.25)**0.11*(h/0.7)**1.22*(u.Mpc/u.h) # Mpc h-1 Mfs = 4.0*np.pi/3.0*rho*(alpha/2.0)**3 Mhm = 2.7e3*Mfs print Mhm*h**3
''' Defines mass profiles ''' import os import numpy as np import astropy.units as u import astropy.constants as const import pandas as pd from scipy.integrate import cumtrapz from scipy.interpolate import UnivariateSpline from astropy.cosmology import WMAP9 as cosmo RHO_CRIT = cosmo.critical_density(0.) class MassProfile: def __init__(self, profiletype, **kwargs): self.type = profiletype self.kwargs = kwargs def M(self): raise NotImplementedError("""Woops! You need to specify an actual halo type before you can get the mass profile!""") pass def Mprime(self, bs, profile): mprime = np.gradient(profile, bs, edge_order=2) self.mprime = mprime return mprime
def crit_density(z): #Msun/h /kpc^3 gcm3_to_msunkpc3 = 1.477543e31 density = cosmo.critical_density(z).value * gcm3_to_msunkpc3 #print "crit desity(%f): %f Msun/kpc^3"%(z,density) #print "crit desity(%f): %e Msun/kpc^3"%(z,density*1000**3/cosmo.h**2) return density
def get_box_density(dataset): """ gets the total box density in Msun / Mpc***3 for a yt dataset""" return WMAP9.critical_density(0).value * \ (3.086e24)**3 / 1.989e33 * dataset.omega_matter \ * YTArray(1., 'Msun') / YTArray(1., 'Mpc')**3