Beispiel #1
0
def test_hubble(threshold=1e-7):
    cosmo = cosmo_wmap_5()
    print "Comparing hubble constant with calculations from http://icosmo.org/"

    # load external distance calculations
    # z H(z)
    hz_file = os.path.dirname(os.path.abspath(__file__))
    hz_file = os.path.join(hz_file, 'icosmo_testdata', 'h_z.txt')
    ic_hz = numpy.loadtxt(hz_file)

    z = ic_hz[:, 0]

    cd_hz = cd.hubble_z(z, **cosmo) * cc.Mpc_km

    label = r"$H(z)$"
    pylab.figure()
    pylab.plot(z, ic_hz[:, 1], label=label + ' IC', ls=':')
    pylab.plot(z, cd_hz, label=label + ' distance.py', ls='-')
    pylab.legend(loc='best')

    pylab.figure()
    diff = (ic_hz[:, 1] - cd_hz) / ic_hz[:, 1]

    maxdiff = numpy.max(numpy.abs(diff))
    print "Maximum fraction difference in %s is %e." % (label, maxdiff)
    if maxdiff > threshold:
        print "Warning: difference exceeds threshold %e !!!" % threshold
    assert (maxdiff < threshold)

    pylab.plot(z, diff, label=label, ls='-')
    pylab.legend(loc='best')
Beispiel #2
0
def interp_z_dependent_velocity_factors(z, deltaz=1e-3):
    from cosmolopy.perturbation import fgrowth
    zgrid = np.arange(np.min(z)-deltaz, np.max(z)+deltaz, deltaz)
    hz_grid = distance.hubble_z(zgrid, **cosmo)
    fgrowth_grid = fgrowth(zgrid, cosmo['omega_M_0'])
    a_grid = 1./(1.+zgrid)
    tmp_grid = fgrowth_grid*a_grid*hz_grid
    from scipy import interpolate
    f = interpolate.interp1d(zgrid, tmp_grid)
    return f(z)
Beispiel #3
0
def calculate_rhoc_cosmopy(redshift=None, aexp=None,
                   cosmology=_get_cosmology()) :
    pass

    redshift = check_redshift_kwargs(redshift=redshift,aexp=aexp)

    H_z = cdist.hubble_z(redshift, **cosmology) # /s

    assert(3.*H_z**2/(8.*np.pi*constants.gravc) == \
               cdens.cosmo_densities(**cosmology)[0]*cdist.e_z(redshift,**cosmology)**2) 

    return 3.*H_z**2/(8.*np.pi*constants.gravc)
Beispiel #4
0
def corr_delta_vel(r, z=0.4, kmin=1e-3, kmax=0.2):
    # r is in Mpc
    a = 1./(1.+z)
    hz = distance.hubble_z(z, **cosmo)
    fgrowth = perturbation.fgrowth(z, cosmo['omega_M_0'])
    k = np.arange(kmin,kmax,kmin)
    dk = k[1]-k[0]
    corr = []
    pk = perturbation.power_spectrum(k, z, **cosmo)
    for this_r in r:
        this_corr = a*hz*fgrowth/2./np.pi**2. * np.sum(dk*k*pk*spherical_bessel_j1(k*this_r))
        corr.append(this_corr)
    return np.array(corr)
Beispiel #5
0
def test_figure6():
    """Plot Hogg fig. 6: The dimensionless lookback time t_L/t_H and age t/t_H.

    The three curves are for the three world models, 

    - Einstein-de Sitter (omega_M, omega_lambda) = (1, 0) [solid]
    
    : Low-density (0.05, 0) [dotted]

    -- High lambda, (0.2, 0.8) [dashed]

    Hubble distance DH = c / H0

    z from 0--5
    t/th from 0--1.2

    """

    z = numpy.arange(0, 5.05, 0.05)

    cosmo = {}
    cosmo['omega_M_0'] = numpy.array([[1.0], [0.05], [0.2]])
    cosmo['omega_lambda_0'] = numpy.array([[0.0], [0.0], [0.8]])
    cosmo['h'] = 0.5
    cd.set_omega_k_0(cosmo)

    linestyle = ['-', ':', '--']

    th = 1 / cd.hubble_z(0, **cosmo)

    tl = cd.lookback_time(z, **cosmo)
    age = cd.age(z, **cosmo)

    pylab.figure(figsize=(6, 6))
    for i in range(len(linestyle)):
        pylab.plot(z, (tl / th)[i], ls=linestyle[i])
        pylab.plot(z, (age / th)[i], ls=linestyle[i])
    pylab.xlim(0, 5)
    pylab.ylim(0, 1.2)
    pylab.xlabel("redshift z")
    pylab.ylabel(r"lookback timne $t_L/t_H$")
    pylab.title("compare to " + inspect.stack()[0][3].replace('test_', '') +
                " (astro-ph/9905116v4)")
def test_figure6():
    """Plot Hogg fig. 6: The dimensionless lookback time t_L/t_H and age t/t_H.

    The three curves are for the three world models, 

    - Einstein-de Sitter (omega_M, omega_lambda) = (1, 0) [solid]
    
    : Low-density (0.05, 0) [dotted]

    -- High lambda, (0.2, 0.8) [dashed]

    Hubble distance DH = c / H0

    z from 0--5
    t/th from 0--1.2

    """

    z = numpy.arange(0, 5.05, 0.05)

    cosmo = {}
    cosmo['omega_M_0'] = numpy.array([[1.0],[0.05],[0.2]])
    cosmo['omega_lambda_0'] = numpy.array([[0.0],[0.0],[0.8]])
    cosmo['h'] = 0.5
    cd.set_omega_k_0(cosmo)
    
    linestyle = ['-', ':', '--']

    th = 1/ cd.hubble_z(0, **cosmo)

    tl = cd.lookback_time(z, **cosmo)
    age = cd.age(z, **cosmo)

    pylab.figure(figsize=(6,6))
    for i in range(len(linestyle)):
        pylab.plot(z, (tl/th)[i], ls=linestyle[i])
        pylab.plot(z, (age/th)[i], ls=linestyle[i])
    pylab.xlim(0,5)
    pylab.ylim(0,1.2)
    pylab.xlabel("redshift z")
    pylab.ylabel(r"lookback timne $t_L/t_H$")
    pylab.title("compare to " + inspect.stack()[0][3].replace('test_', '') + 
                " (astro-ph/9905116v4)")
def test_hubble(threshold = 1e-7):
    cosmo = cosmo_wmap_5()
    print "Comparing hubble constant with calculations from http://icosmo.org/"

    # load external distance calculations
    # z H(z)
    hz_file = os.path.dirname(os.path.abspath(__file__))
    hz_file = os.path.join(hz_file, 'icosmo_testdata', 
                           'h_z.txt')
    ic_hz = numpy.loadtxt(hz_file)

    z = ic_hz[:,0]

    cd_hz = cd.hubble_z(z, **cosmo) * cc.Mpc_km

    label = r"$H(z)$"
    pylab.figure()
    pylab.plot(z, ic_hz[:,1], 
               label=label + ' IC', ls=':')
    pylab.plot(z, cd_hz, label=label + ' distance.py', ls='-')
    pylab.legend(loc='best')

    pylab.figure()
    diff = (ic_hz[:,1] - cd_hz) / ic_hz[:,1]
    
    maxdiff = numpy.max(numpy.abs(diff))
    print "Maximum fraction difference in %s is %e." % (label,
                                                        maxdiff)
    if maxdiff > threshold:
        print "Warning: difference exceeds threshold %e !!!" % threshold
    assert(maxdiff < threshold)

    pylab.plot(z,
               diff, 
               label=label, ls='-')
    pylab.legend(loc='best')
Beispiel #8
0
def integrate_optical_depth(x_ionH, x_ionHe, z, **cosmo):
    """The electron scattering optical depth given ionized filling
    factor vs. redshift.

    Parameters
    ----------
    
    x_ionH: array

       Ionized fraction of hydrogen as a function of z. Should be [0,1].

    x_ionHe: array 

       Set x_ionHE to X_HeII + 2 * X_HeIII, where X_HeII is the
       fraction of helium that is singly ionized, and X_HeII is the
       fraction of helium that is doubly ionized. See Notes below.
    
    z: array
       Redshift values at which the filling factor is specified.

    cosmo: cosmological parameters
    
       uses: 'X_H' and/or 'Y_He', plus parameters needed for hubble_z

    Returns
    -------

    tau: array
       The optical depth as a function of z.

    Notes
    -----

    The precision of your result depends on the spacing of the input
    arrays. When in doubt, try doubling your z resolution and see if
    the optical depth values have converged.

    100% singly ionized helium means x_ionHe = 1.0, 100% doubly
    ionized helium means x_ionHe = 2.0

    If you want helium to be singly ionized at the same rate as
    hydrogen, set x_ionHe = x_ionH.

    If you want helium to be doubly ionized at the same rate as
    hydrogen is ionized, set x_ionHe = 2 * x_ionH.

    """

    rho_crit, rho_0, n_He_0, n_H_0 = cden.baryon_densities(**cosmo)

    # comoving Mpc^-1
    n_p = n_H_0 + 2. * n_He_0

    # comoving Mpc^-1
    n_e = n_H_0 * x_ionH + n_He_0 * x_ionHe

    # fraction of electrons that are free
    x = n_e / n_p

    H_0 = cc.H100_s * cosmo['h']

    # Mpc s^-1 * Mpc^2 * Mpc^-3 / s^-1 -> unitless
    tau_star = cc.c_light_Mpc_s * cc.sigma_T_Mpc * n_p

    # s^-1
    H_z = cd.hubble_z(z, **cosmo)

    # Mpc^3 s^-1 * Mpc^-3 / s^-1 -> unitless
    integrand = -1. * tau_star * x * ((1. + z)**2.) / H_z

    integral = numpy.empty(integrand.shape)
    integral[..., 1:] = si.cumtrapz(integrand, z)
    integral[..., 0] = 0.0
    return numpy.abs(integral)