def test_sfrd():
    """ Check conversion of Lmin -> SFRD.

    At z=7:
    Lmin = 0.2 L*z=6 -> Mmin=-18.5 -> log(rho/erg/s/Hz/Mpc^3)=25.5 ->
    SFRD = 10^-2.32 Msun/yr^-1/Mpc^-3.

    From Oesch et al (2009 ApJ 690 1350).
    """

    cosmo = cparam.WMAP7_BAO_H0_mean(flat=True)


#     # From Bouwens 2008ApJ...686..230B
#     lfhist = luminosityfunction.LFHistory(params=luminosityfunction.B2008,
#                                           **cosmo)
    
#     mStarz6 = lfhist.params_z(6.0)['MStar']

    alpha = -1.74
    mStarz6 = -20.24
    mStarz7 = -19.7
    phiStar = 1.4e-3
    
    lStarz6 = magnitudes.L_nu_from_magAB(mStarz6)
    lmin = 0.2 * lStarz6
    magmin = magnitudes.magnitude_AB_from_L_nu(lmin)
    ltot = luminosityfunction.schechterCumuLM(magnitudeAB=magmin,
                                              MStar=mStarz7,
                                              phiStar=phiStar,
                                              alpha=alpha)
    sfrd = luminosityfunction.sfr_from_L_nu(ltot)

    print """Lmin = 0.2 L*z=6 -> Lmin/erg/s/Hz = %.3g -> Mmin = %.3g ->
    log(rho/(erg/s/Hz/Mpc^3)) = %.3g -> log(SFRD/(MSun/yr)) = %.3g"""\
    % (lmin, magmin, numpy.log10(ltot), numpy.log10(sfrd))

    ltotz6 = luminosityfunction.schechterCumuLM(magnitudeAB=magmin,
                                                MStar=mStarz6,
                                                phiStar=phiStar,
                                                alpha=alpha)

    print "luminosity density increase from z=7 to z=6 is %.2g percent." \
          % (100 * (1. - ltot/ltotz6))


    assert numpy.abs(numpy.round(1. - ltot/ltotz6, 1) - 0.5) < 0.1
    assert numpy.round(magmin, 1) == -18.5
    assert numpy.abs(numpy.round(numpy.log10(ltot), 1) - 25.5) < 0.2
    assert numpy.abs(numpy.round(numpy.log10(sfrd), 1) - -2.32) < 0.05
def test_tau_instant():
    """Check the optical depth we get using the WMAP z_reion. 
    """
    dz = 0.1
    z = numpy.arange(80., 0. - 1.5 * dz, -1. * dz)

    # Can't match WMAP7 optical depths exactly. Need to look into new
    # treatment in CAMB as mentioned in the WMAP7 paper (see
    # parameters.py).
    cosmos = [
        cparam.WMAP7_BAO_H0_mean(flat=True),
        cparam.WMAP7_ML(flat=True),
        cparam.WMAP5_BAO_SN_mean(flat=True),
        cparam.WMAP5_ML(flat=True),
        cparam.WMAP5_mean(flat=True)
    ]

    # The WMAP5 numbers apparently assume He is neutral, while WMAP7
    # includes simultaneous singly ionized He plus Helium (double)
    # reionization at z=3.5.
    x_ionHe_list = [1.0, 1.0, 0, 0, 0]
    z_rHe_list = [3.5, 3.5, None, None, None]  #[3.5, None, None, None]
    for cosmo in cosmos:
        # Fully ionized H
        x_ionH = 1.0

        # He ionization with H?
        x_ionHe = x_ionHe_list.pop(0)

        # Redshift for Helium double reionization?
        z_rHe = z_rHe_list.pop(0)

        zr = cosmo['z_reion']
        tau_zr = cosmo['tau']
        tau_calc = cr.optical_depth_instant(zr,
                                            x_ionH=x_ionH,
                                            x_ionHe=x_ionHe,
                                            z_rHe=z_rHe,
                                            verbose=1,
                                            **cosmo)

        print "z_r = %f, testing tau:" % zr,
        print tu.fractional_diff_string(tau_zr, tau_calc, 3)

        ntest.assert_approx_equal(tau_calc,
                                  tau_zr,
                                  significant=2,
                                  err_msg="Optical depth doesn't match WMAP")
def test_t_0():
    """Check the age of the universe we get using WMAP cosmologies.

    We only find agreement to 3 sig figs, not the 4 specified in the
    WMAP paper.

    The results of test_age.py show that we're doing the integral
    correctly, so I think the problem is that we're not taking into
    account some of the higher-order effects included in the WMAP
    numbers.

    """

    dz = 0.1
    z = numpy.arange(80., 0. - 1.5 * dz, -1. * dz)

    flat = True
    cosmos = [
        cparam.WMAP7_BAO_H0_mean(flat=True),
        cparam.WMAP7_ML(flat=True),
        cparam.WMAP5_BAO_SN_mean(flat=True),
        cparam.WMAP5_ML(flat=True),
        cparam.WMAP5_mean(flat=True)
    ]

    for cosmo in cosmos:
        age = cd.age(0.0, **cosmo)
        age_flat = cd.age_flat(0.0, **cosmo)
        gyr = 1e9 * cc.yr_s
        age /= gyr
        age_flat /= gyr

        print "integrated age: ",
        print tu.fractional_diff_string(age, cosmo['t_0'], 4)
        ntest.assert_approx_equal(age,
                                  cosmo['t_0'],
                                  significant=3,
                                  err_msg="Integrated age doesn't match WMAP")

        print "analytical age: ",
        print tu.fractional_diff_string(age_flat, cosmo['t_0'], 4)
        ntest.assert_approx_equal(age_flat,
                                  cosmo['t_0'],
                                  significant=3,
                                  err_msg="Analytical age doesn't match WMAP")
Beispiel #4
0
def plotLFevo(
        hist=None,
        params=B2008,
        extrap_var='t',
        #maglim = -21.07 - 2.5 * numpy.log10(0.2)):
        maglim=-21. - 2.5 * numpy.log10(0.2),
        z_max=20.0,
        skipIon=True):
    """Plot evolution of luminosity function params and total luminsity.

    Schechter function at each redshift is integrated up to maglim to
    find total luminsity.
    """

    for (k, v) in params.iteritems():
        params[k] = numpy.asarray(v)

    if hist is None:
        cosmo = cp.WMAP7_BAO_H0_mean(flat=True)
        hist = LFHistory(params, extrap_var=extrap_var, **cosmo)
    else:
        params = hist.params
        extrap_var = hist.extrap_var
        cosmo = hist.cosmo

    z = params['z']
    t = cd.age(z, **cosmo)[0] / cc.yr_s
    MStar = params['MStar']
    phiStar = params['phiStar']
    alpha = params['alpha']

    if maglim is None:
        ltot = schechterTotLM(MStar=MStar, phiStar=phiStar, alpha=alpha)
    else:
        ltot = schechterCumuLM(magnitudeAB=maglim,
                               MStar=MStar,
                               phiStar=phiStar,
                               alpha=alpha)

    print(hist._MStarfunc.extrap_string())

    zPlot = numpy.arange(z.min() - 0.1, z_max, 0.1)
    tPlot = cd.age(zPlot, **cosmo)[0] / cc.yr_s
    newparams = hist.params_z(zPlot)
    MPlot = newparams['MStar']
    phiPlot = newparams['phiStar']
    alphaPlot = newparams['alpha']

    if maglim is None:
        ltotPlot = hist.schechterTotLM(zPlot)
    else:
        ltotPlot = hist.schechterCumuLM(zPlot, magnitudeAB=maglim)
    # From Table 6 of 2008ApJ...686..230B
    lB2008 = 10.**numpy.array([26.18, 25.85, 25.72, 25.32, 25.14])

    iPhot = hist.iPhotonRateDensity_z(zPlot, maglim=maglim)
    #    iPhotFunc = lambda t1: cc.yr_s * hist.iPhotonRateDensity_t(t1,
    #                                                               maglim=maglim)

    if not skipIon:
        xH = hist.ionization(zPlot, maglim)
    import pylab
    pylab.figure(1)
    pylab.gcf().set_label('LFion_vs_z')
    if skipIon:
        pylab.subplot(111)
    else:
        pylab.subplot(211)
    pylab.plot(zPlot, iPhot)
    if not skipIon:
        pylab.subplot(212)
        pylab.plot(zPlot, xH)
        pylab.ylim(0.0, 1.5)

    pylab.figure(2)
    pylab.gcf().set_label('LFparams_vs_z')
    pylab.subplot(311)
    pylab.plot(z, MStar, '-')
    pylab.plot(z, MStar, 'o')
    pylab.plot(zPlot, MPlot, ':')

    pylab.subplot(312)
    pylab.plot(z, phiStar, '-')
    pylab.plot(z, phiStar, 'o')
    pylab.plot(zPlot, phiPlot, ':')

    pylab.subplot(313)
    pylab.plot(z, alpha, '-')
    pylab.plot(z, alpha, 'o')
    pylab.plot(zPlot, alphaPlot, ':')

    pylab.figure(3)
    pylab.gcf().set_label('LFparams_vs_t')
    pylab.subplot(311)
    pylab.plot(t, MStar, '-')
    pylab.plot(t, MStar, 'o')
    pylab.plot(tPlot, MPlot, ':')

    pylab.subplot(312)
    pylab.plot(t, phiStar, '-')
    pylab.plot(t, phiStar, '.')
    pylab.plot(tPlot, phiPlot, ':')

    pylab.subplot(313)
    pylab.plot(t, alpha, '-')
    pylab.plot(t, alpha, 'o')
    pylab.plot(tPlot, alphaPlot, ':')

    pylab.figure(4)
    pylab.gcf().set_label('LFlum_vs_z')
    pylab.subplot(121)
    pylab.plot(z, ltot, 'o')
    pylab.plot(z, lB2008, 'x')
    pylab.plot(zPlot, ltotPlot)

    pylab.subplot(122)
    pylab.plot(t, ltot, 'o')
    pylab.plot(t, lB2008, 'x')
    pylab.plot(tPlot, ltotPlot)
Beispiel #5
0
... } 
>>> mass = cp.virial_mass(1e4, 6.0, **cosmo)
>>> temp = cp.virial_temp(mass, 6.0, **cosmo)
>>> print "Mass = %.3g M_sun" % mass
Mass = 1.68e+08 M_sun
>>> print round(temp, 4)
10000.0

Calculate the critical and matter densities:

>>> from cosmolopy import *
>>> 'rho_crit=%.3g Msun/Mpc^3, rho_0=%.3g Msun/Mpc^3' % cden.cosmo_densities(**fidcosmo)
'rho_crit=1.38e+11 Msun/Mpc^3, rho_0=3.75e+10 Msun/Mpc^3'

Look in the tests/ and examples/ directories for more examples. 

"""

import cosmolopy.constants as cc
import cosmolopy.density as cden
import cosmolopy.distance as cd
import cosmolopy.perturbation as cp
import cosmolopy.reionization as cr
import cosmolopy.parameters as cparam
import cosmolopy.magnitudes as cmag
import cosmolopy.luminosity_function as cl
import cosmolopy.transfer_function
import cosmolopy.transfer_function_fit

fidcosmo = cparam.WMAP7_BAO_H0_mean(flat=True, extras=True)