Esempio n. 1
0
def test_deprecated_init():

    # all valid deprecated kwargs
    with pytest.warns(FutureWarning):
        c1 = Cosmology(H0=67.6, Om0=0.31, flat=True)
        c2 = Cosmology(H0=67.6, Om0=0.31, Ode0=0.7, flat=False, w0=-0.9)

    # missing valid and deprecated
    with pytest.raises(Exception):
        c = Cosmology(h=0.7, flat=True)

    # passing arguments and mixing
    with pytest.raises(Exception):
        c = Cosmology(0.7, flat=True)

    # parameter conflict
    with pytest.raises(Exception):
        c3 = Cosmology(H0=70., flat=True, h=0.7)

    assert_allclose(c1.h, 0.676)
    assert_allclose(c2.h, 0.676)
    assert_allclose(c1.Om0, 0.31)
    assert_allclose(c2.Om0, 0.31)
    assert_allclose(c1.Ok0, 0.)
    assert_allclose(c2.Ode0, 0.7)
    assert_allclose(c2.w0_fld, -0.9)
Esempio n. 2
0
def test_bad_input():

    with pytest.raises(ValueError):
        c = Cosmology(gauge='BAD')

    # specifying w0_fld + Omega_Lambda is inconsistent
    with pytest.raises(ValueError):
        c = Cosmology(Omega_Lambda=0.7, w0_fld=-0.9)
Esempio n. 3
0
def test_set_Omega0_cb():

    c = Cosmology().match(Omega0_cb=0.4)

    assert_allclose(c.Omega0_cb, 0.4)

    c = Cosmology().match(Omega0_m=0.4)
    assert_allclose(c.Omega0_m, 0.4)
Esempio n. 4
0
def test_cosmology_clone():
    c = Cosmology(gauge='synchronous')

    c1 = Cosmology(gauge='newtonian')
    assert 'newtonian' in c1.parameter_file

    c2 = Cosmology(P_k_max=1.01234567)
    assert '1.01234567' in c2.parameter_file
Esempio n. 5
0
def test_massive_neutrinos():

    # single massive neutrino
    c = Cosmology(m_ncdm=0.6)
    assert c.N_ncdm == 1

    # do not need 0 values
    with pytest.raises(ValueError):
        c = Cosmology(m_ncdm=[0.6, 0.])
Esempio n. 6
0
def test_conflicts():

    # h is the correct param
    with pytest.raises(Exception):
        c = Cosmology(h=0.7, H0=70)

    with pytest.raises(Exception):
        c = Cosmology(Omega0_b=0.04, Omega_b=0.04)

    # Omega_b is the correct param
    with pytest.raises(Exception):
        c = Cosmology(Omega0_b=0.04, omega_b=0.02)
Esempio n. 7
0
def test_cosmology_a_max():
    c = Cosmology(gauge='synchronous', a_max=2.0)
    print(c.parameter_file)
    assert c.a_max == 2.0
    t = c.Omega_m(-0.1)
    t = c.efunc(-0.1)
    t = c.scale_independent_growth_factor(-0.1)
Esempio n. 8
0
def test_cosmology_sane():
    c = Cosmology(gauge='synchronous', verbose=True)

    assert_allclose(c.Omega_cdm(0), c.Omega0_cdm)
    assert_allclose(c.Omega_g(0), c.Omega0_g)
    assert_allclose(c.Omega_b(0), c.Omega0_b)
    assert_allclose(c.Omega_ncdm(0), c.Omega0_ncdm)
    assert_allclose(c.Omega_ur(0), c.Omega0_ur)
    assert_allclose(c.Omega_ncdm(0), c.Omega0_ncdm_tot)

    assert_allclose(c.Omega_pncdm(0), c.Omega0_pncdm)
    assert_allclose(c.Omega_m(0), c.Omega0_m)
    assert_allclose(c.Omega_r(0), c.Omega0_r)

    # total density in 10e10 Msun/h unit
    assert_allclose(c.rho_tot(0), 27.754999)

    # comoving distance to z=1.0 in Mpc/h unit.
    assert_allclose(c.comoving_distance(1.0), 3396.157391 * c.h)

    # conformal time in Mpc unit.
    assert_allclose(c.tau(1.0), 3396.157391)

    assert_allclose(c.efunc(0), 1.)  # hubble in Mpc/h km/s unit
    assert_allclose(c.efunc(0) - c.efunc(1 / 0.9999 - 1),
                    0.0001 * c.efunc_prime(0),
                    rtol=1e-3)
Esempio n. 9
0
def test_set_sigma8():

    # set sigma8 by adjusting A_s internally
    c = Cosmology().match(sigma8=0.80)

    # run CLASS and compute sigma8
    assert_allclose(c.sigma8, 0.80)
Esempio n. 10
0
def test_cosmology_get_pk():
    c = Cosmology()
    p = c.get_pk(z=0, k=0.1)
    p1 = c.Spectra.get_pk(z=0, k=0.1)

    # ensure the dro did use get_pk of Spectra rather than that from Primordial
    assert_allclose(p, p1)
Esempio n. 11
0
def save_powerspec(
    omega0: float = 0.288,
    omegab: float = 0.0472,
    hubble: float = 0.7,
    scalar_amp: float = 2.427e-9,
    ns: float = 0.97,
    outfile: str = "my_pk_linear.txt",
):
    """Generate linear powerspec and save into file"""
    omegacdm = (omega0 - omegab
                )  # dark matter density = total mass density - baryon density

    MYPlanck = Cosmology(
        m_ncdm=[],  # no massive neutrino
        Omega0_b=omegab,
        Omega0_cdm=omegacdm,
        h=hubble,
        n_s=ns,
        A_s=scalar_amp,
    )  # \
    # .match(sigma8=0.8159)
    pklin0 = LinearPower(MYPlanck, redshift=0.0)

    k = numpy.logspace(-3, 2, 10000, endpoint=True)

    numpy.savetxt(outfile, list(zip(k, pklin0(k))))
Esempio n. 12
0
def test_linear():

    # linear power
    c = Cosmology()
    Plin = LinearPower(c, redshift=0)

    # desired separation (in Mpc/h)
    r = numpy.logspace(0, numpy.log10(150), 500)

    # linear correlation
    CF = CorrelationFunction(Plin)
    CF.sigma8 = 0.82
    xi1 = CF(100.)

    assert_allclose(CF.redshift, CF.attrs['redshift'])
    assert_allclose(CF.sigma8, CF.attrs['sigma8'])

    # change sigma8
    CF.sigma8 = 0.75
    xi2 = CF(100.)
    assert_allclose(xi1/xi2, (0.82/0.75)**2, rtol=1e-2)

    # change redshift
    CF.redshift = 0.55
    xi3 = CF(100.)
    D2 = CF.cosmo.scale_independent_growth_factor(0.)
    D3 = c.scale_independent_growth_factor(0.55)
    assert_allclose(xi2.max()/xi3.max(), (D2/D3)**2, rtol=1e-2)
Esempio n. 13
0
def test_linear_nbody():
    from nbodykit.cosmology import Cosmology
    cosmo = Cosmology(Omega0_cdm=0.26,
                      Omega0_b=0.04,
                      m_ncdm=[0.6],
                      P_k_max=1e-2)

    linearnbody = LinearNbody(cosmo, c_b=0, c_ncdm_1ev_z0=0)

    a0 = 0.01

    k, q0, p0 = linearnbody.seed_from_synchronous(cosmo, a0)

    k, qt, pt = linearnbody.seed_from_synchronous(cosmo, 1.0)

    a, q, p = linearnbody.integrate(k, q0, p0, [a0, 1.0])

    # shall be within 5% to class solution
    assert_allclose(q[-1] / qt, 1., rtol=0.1)

    # need very high precision for reversibility.
    a, q, p = linearnbody.integrate(k, qt, pt, [1.0, a0], rtol=1e-9)
    a, q, p = linearnbody.integrate(k, q[-1], p[-1], [a0, 1.0], rtol=1e-9)

    assert_allclose(q[-1], qt, rtol=1e-4)
    assert_allclose(p[-1], pt, rtol=1e-4)
Esempio n. 14
0
 def __init__(self, datar, randomr, nofz, universe_params=None):
     #
     # make sure catalogs have the desired format
     #
     # cosmology
     if universe_params is None:
         universe_params = universeparams
     cosmo = Cosmology(Om0=universe_params['Om0'],
                       H0=universe_params['H0'],
                       flat=universe_params['flat'])
     data = catit(datar)
     randoms = catit(randomr)
     data = nb.ArrayCatalog(data)
     randoms = nb.ArrayCatalog(randoms)
     data['Position'] = SkyToCartesian(data['RA'],
                                       data['DEC'],
                                       data['Z'],
                                       cosmo=cosmo)
     randoms['Position'] = SkyToCartesian(randoms['RA'],
                                          randoms['DEC'],
                                          randoms['Z'],
                                          cosmo=cosmo)
     #
     self.data = data
     self.randoms = randoms
     self.nofz = nofz
Esempio n. 15
0
def test_halofit():

    # initialize the power
    c = Cosmology().match(sigma8=0.82)
    P = HalofitPower(c, redshift=0)

    # k is out of range
    with pytest.raises(ValueError):
        Pk = P(2 * c.P_k_max)

    # compute for scalar
    Pk = P(0.1)

    # compute for array
    k = numpy.logspace(-3, numpy.log10(0.99 * c.P_k_max), 100)
    Pk1 = P(k)

    # change sigma8
    P.sigma8 = 0.75
    Pk2 = P(k)
    assert_allclose(Pk1.max() / Pk2.max(), (0.82 / 0.75)**2, rtol=1e-2)

    # change redshift
    P.redshift = 0.55
    Pk3 = P(k)
    D2 = c.scale_independent_growth_factor(0.)
    D3 = c.scale_independent_growth_factor(0.55)
    assert_allclose(Pk2.max() / Pk3.max(), (D2 / D3)**2, rtol=1e-2)
Esempio n. 16
0
def test_immutable():

    c = Cosmology()
    with pytest.raises(ValueError):
        c.A_s = 2e-9  # immutable

    # can add non-CLASS attributes still
    c.test = 'TEST'
Esempio n. 17
0
def test_sigma8_z():

    z = numpy.linspace(0, 1, 100)
    c = Cosmology()

    s8_z = c.sigma8_z(z)
    D_z = c.scale_independent_growth_factor(z)
    assert_allclose(s8_z, D_z * c.sigma8, rtol=1e-3)
Esempio n. 18
0
def test_astropy_compat():
    c = Cosmology(gauge='synchronous', m_ncdm=[0.06])

    assert_allclose(c.Odm(0), c.Odm0)
    assert_allclose(c.Ogamma(0), c.Ogamma0)
    assert_allclose(c.Ob(0), c.Ob0)
    assert_allclose(c.Onu(0), c.Onu0)
    assert_allclose(c.Ok(0), c.Ok0)
    assert_allclose(c.Ode(0), c.Ode0)
    assert_array_equal(c.has_massive_nu, True)
Esempio n. 19
0
def test_cosmology_density():
    c = Cosmology(gauge='synchronous')
    z = [0, 1, 2, 5, 9, 99]
    assert_allclose(c.rho_cdm(z), c.Omega_cdm(z) * c.rho_tot(z))
    assert_allclose(c.rho_g(z), c.Omega_g(z) * c.rho_tot(z))
    assert_allclose(c.rho_ncdm(z), c.Omega_ncdm(z) * c.rho_tot(z))
    assert_allclose(c.rho_b(z), c.Omega_b(z) * c.rho_tot(z))
    assert_allclose(c.rho_m(z), c.Omega_m(z) * c.rho_tot(z))
    assert_allclose(c.rho_r(z), c.Omega_r(z) * c.rho_tot(z))
    assert_allclose(c.rho_ur(z), c.Omega_ur(z) * c.rho_tot(z))
Esempio n. 20
0
def test_zeldovich():

    # zeldovich power
    Pzel = ZeldovichPower(Cosmology(), redshift=0)

    # desired separation (in Mpc/h)
    r = numpy.logspace(0, numpy.log10(150), 500)

    # zeldovich correlation
    CF = CorrelationFunction(Pzel)

    xi = CF(r)
Esempio n. 21
0
def test_halofit():

    # nonlinear power
    Pnl = HalofitPower(Cosmology(), redshift=0)

    # desired separation (in Mpc/h)
    r = numpy.logspace(0, numpy.log10(150), 500)

    # nonlinear correlation
    CF = CorrelationFunction(Pnl)

    xi = CF(r)
Esempio n. 22
0
def test_deprecated_ehpower():

    c = Cosmology()
    with pytest.warns(FutureWarning):
        Plin1 = EHPower(c, redshift=0)
        Plin2 = LinearPower(c, 0., transfer='EisensteinHu')
        assert_allclose(Plin1(0.1), Plin2(0.1))

    with pytest.warns(FutureWarning):
        Plin1 = NoWiggleEHPower(c, redshift=0)
        Plin2 = LinearPower(c, 0., transfer='NoWiggleEisensteinHu')
        assert_allclose(Plin1(0.1), Plin2(0.1))
Esempio n. 23
0
def test_old_Omega_syntax():

    c1 = Cosmology(Omega_b=0.04)
    c2 = Cosmology(Omega0_b=0.04)
    assert c1.Omega0_b == c2.Omega0_b

    c1 = Cosmology(T_cmb=2.7)
    c2 = Cosmology(T0_cmb=2.7)
    assert c1.T0_cmb == c2.T0_cmb

    c1 = Cosmology(Omega0_k=0.05)
    c2 = Cosmology(Omega_k=0.05)
    assert c1.Omega0_k == c2.Omega0_k

    c1 = Cosmology(Omega0_lambda=0.7)
    c2 = Cosmology(Omega_lambda=0.7)
    c3 = Cosmology(Omega0_Lambda=0.7)
    assert c1.Omega0_lambda == c2.Omega0_lambda
    assert c1.Omega0_lambda == c3.Omega0_lambda
Esempio n. 24
0
def test_mcfit():

    c = Cosmology()
    Plin = LinearPower(c, redshift=0)

    # do Pk to CF
    k = numpy.logspace(-4, 2, 1024)
    CF = pk_to_xi(k, Plin(k))

    # do CF to Pk
    r = numpy.logspace(-3, 3, 1024)
    Pk2 = xi_to_pk(r, CF(r))(k)

    idx = (k>1e-2)&(k<10.)
    assert_allclose(Pk2[idx], Plin(k[idx]), rtol=1e-2)
Esempio n. 25
0
def test_large_scales():

    c = Cosmology()
    k = numpy.logspace(-5, -2, 100)

    # linear power
    Plin = LinearPower(c, redshift=0)

    # nonlinear power
    Pnl = HalofitPower(c, redshift=0)

    # zeldovich power
    Pzel = ZeldovichPower(c, redshift=0)

    assert_allclose(Plin(k), Pnl(k), rtol=1e-2)
    assert_allclose(Plin(k), Pzel(k), rtol=1e-2)
Esempio n. 26
0
def test_mcfit():

    c = Cosmology()
    Plin = LinearPower(c, redshift=0)

    for ell in [0, 2, 4]:
        # do Pk to CF; use Plin for ell>0 just for testing
        k = numpy.logspace(-4, 2, 1024)
        CF = pk_to_xi(k, Plin(k), ell=ell)

        # do CF to Pk; use Plin for ell>0 just for testing
        r = numpy.logspace(-2, 4, 1024)
        Pk2 = xi_to_pk(r, CF(r), ell=ell)(k)

        idx = (k > 1e-2) & (k < 10.)
        assert_allclose(Pk2[idx], Plin(k[idx]), rtol=1e-2)
Esempio n. 27
0
    def setUp(self):
        """
        Do some basic setup of the unit testing suite
        """
        # basic logging setup
        logging.basicConfig()

        # initialize a particle mesh object
        from pmesh.pm import ParticleMesh
        from mpi4py import MPI
        self.pm = ParticleMesh(Nmesh=[128]*3, BoxSize=2.0, comm=MPI.COMM_WORLD)

        # a cosmology
        self.cosmo = Cosmology()
        
        # default size of data and randoms
        self.N = 100
Esempio n. 28
0
def test_cosmology_vect():
    c = Cosmology(gauge='synchronous')

    assert_allclose(c.Omega_cdm([0]), c.Omega0_cdm)

    assert_array_equal(c.Omega_cdm([]).shape, [0])
    assert_array_equal(c.Omega_cdm([0]).shape, [1])
    assert_array_equal(c.Omega_cdm([[0]]).shape, [1, 1])

    assert_array_equal(c.rho_k([[0]]).shape, [1, 1])

    k, z = numpy.meshgrid([0, 1], [0.01, 0.05, 0.1, 0.5],
                          sparse=True,
                          indexing='ij')

    pk = c.get_pk(z=z, k=k)
    assert_array_equal(pk.shape, [2, 4])
Esempio n. 29
0
 def surveydata(self, edges=np.linspace(0.1, 10., 20+1),  universe_params=None):
     # cosmology
     if universe_params is None:
         universe_params = universeparams
     cosmo = Cosmology(Om0=universe_params['Om0'], 
                       H0=universe_params['H0'], 
                       flat=universe_params['flat'])
     dd = nb.SurveyDataPairCount({'1d'}, self.data, edges, cosmo, ra='RA', 
                             dec='DEC', weight='Weight', redshift='Z')
     dr = nb.SurveyDataPairCount({'1d'}, self.data, edges, cosmo, ra='RA', 
                             dec='DEC', weight='Weight', redshift='Z',second=self.random)
     rr = nb.SurveyDataPairCount({'1d'}, self.random, edges, cosmo, ra='RA', 
                             dec='DEC', weight='Weight', redshift='Z',second=self.random)
     dd = dd.result
     dr = dr.result
     rr = rr.result
     f  = self.data.csize/self.random.csize
     return (dd, rr, dr, f, edges)
Esempio n. 30
0
def make_cosmo(param_names, param_vals):
    """
    Frequently, the parameters of our input will not align with what we need.
    So, we convert them to the appropriate format.
    :param param_names:
    :param param_vals:
    :return:
    """
    param_dict = dict([(pn, param_vals[pn]) for pn in param_names])
    # TODO it'd be nice if this could be somehow generalized.
    param_dict['N_ncdm'] = 3.0
    param_dict['N_ur'] = param_dict['Neff'] - 3.0
    del param_dict['Neff']

    #param_dict['h'] = param_dict['H0']/100
    #del param_dict['H0']
    param_dict['h'] = param_dict['h0']
    del param_dict['h0']

    #param_dict['w0_fld'] = param_dict['w']
    w = param_dict['w']
    del param_dict['w']

    param_dict['Omega_cdm'] = param_dict['Omega_m'] - param_dict['Omega_b']
    del param_dict['Omega_b']
    del param_dict['Omega_m']

    param_dict['Omega_ncdm'] = [
        param_dict['Omeganuh2'] / (param_dict['h']**2), 0.0, 0.0
    ]
    #param_dict['m_ncdm']=None
    #param_dict['omch2'] = (param_dict['Omega_m'] - param_dict['Omega_b'] - param_dict['Omega0_ncdm_tot'])*(param_dict['h']**2)
    del param_dict['Omeganuh2']

    #param_dict['A_s'] = 10**(np.log10(np.exp(param_dict['ln_1e10_A_s']))-10.0)
    param_dict['A_s'] = 10**(-10) * np.exp(param_dict['ln(10^{10}A_s)'])
    del param_dict['ln(10^{10}A_s)']

    #print param_dict
    # this is seriously what i have to do here.
    C = Cosmology()
    C2 = C.from_dict(param_dict)
    C3 = C2.clone(w0_fld=w)
    return C3  #Cosmology(**param_dict)