Exemple #1
0
def test_camb_de_model():
    """Check that the dark energy model for CAMB has been properly defined."""
    with pytest.raises(ValueError):
        cosmo = ccl.CosmologyVanillaLCDM(
            transfer_function='boltzmann_camb',
            extra_parameters={"camb": {
                "dark_energy_model": "pf"
            }})
        ccl.linear_matter_power(cosmo, 1, 1)
    """Check that w is not less than -1, if the chosen dark energy model for
    CAMB is fluid."""
    with pytest.raises(ValueError):
        cosmo = ccl.CosmologyVanillaLCDM(transfer_function='boltzmann_camb',
                                         w0=-1,
                                         wa=-1)
        ccl.linear_matter_power(cosmo, 1, 1)
    """Check that ppf is running smoothly."""
    cosmo = ccl.CosmologyVanillaLCDM(
        transfer_function='boltzmann_camb',
        w0=-1,
        wa=-1,
        extra_parameters={"camb": {
            "dark_energy_model": "ppf"
        }})
    assert np.isfinite(ccl.linear_matter_power(cosmo, 1, 1))
Exemple #2
0
def test_spline1d():
    cosmo = ccl.CosmologyVanillaLCDM()
    cosmo.compute_distances()

    chi_gsl_spline = cosmo.cosmo.data.chi
    a_arr, chi_arr = ccl.pyutils._get_spline1d_arrays(chi_gsl_spline)
    chi = ccl.comoving_radial_distance(cosmo, a_arr)

    assert np.allclose(chi_arr, chi)
Exemple #3
0
def test_cosmology_lcdm():
    """Check that the default vanilla cosmology behaves
    as expected"""
    c1 = ccl.Cosmology(Omega_c=0.25,
                       Omega_b=0.05,
                       h=0.67, n_s=0.96,
                       sigma8=0.81)
    c2 = ccl.CosmologyVanillaLCDM()
    assert_(ccl.comoving_radial_distance(c1, 0.5) ==
            ccl.comoving_radial_distance(c2, 0.5))
Exemple #4
0
 def get_hm_correction(self):
     """Get Halo Model Correction name."""
     name = self.p["mcmc"]["hm_correction"]
     if name == "HALOFIT":
         hmcorr = HM_halofit(ccl.CosmologyVanillaLCDM(), self.get_hmc())
         hmcorr = hmcorr.rk_interp
     elif name == "Mead":
         hmcorr = name
     elif name == "None":
         hmcorr = None
     else:
         raise ValueError("HM correction model not recognized.")
     return hmcorr
Exemple #5
0
    def __init__(self, m, kmax=np.inf):
        self.name = m['name']
        self.type = m['type']
        self.beam = m['beam']
        self.syst = m.get('systematics')
        self.lmax = np.inf
        self.profile = None
        self.tracer = None
        if m['type'] == 'y':
            self.profile = ccl.halos.HaloProfilePressureGNFW()
        else:
            cM = m["halo_concentration"]

            if m['type'] == 'g':
                # truncate N(z)'s
                self.dndz = m['dndz']
                self.z, self.nz = np.loadtxt(self.dndz).T
                self.nzf = interp1d(self.z,
                                    self.nz,
                                    kind='cubic',
                                    bounds_error=False,
                                    fill_value=0.)
                self.z_avg = np.average(self.z, weights=self.nz)
                self.zrange = self.z[self.nz >= 0.005].take([0, -1])
                # determine max ell
                cosmo = ccl.CosmologyVanillaLCDM()
                chimean = ccl.comoving_radial_distance(cosmo,
                                                       1 / (1 + self.z_avg))
                self.lmax = kmax * chimean - 0.5
                self.bz = np.ones_like(self.z)
                ns_ind = m.get("ns_independent", False)
                self.profile = ccl.halos.HaloProfileHOD(c_m_relation=cM,
                                                        ns_independent=ns_ind)

            elif m['type'] == 'k':
                self.profile = ccl.halos.HaloProfileNFW(c_m_relation=cM)
        if self.profile is not None:
            try:
                func = self.profile.update_parameters
                if hasattr(func, "__wrapped__"):
                    # bypass the decorator
                    func = func.__wrapped__
                args = func.__code__.co_varnames
                code = func.__code__
                count = code.co_argcount + code.co_kwonlyargcount
                self.args = args[1:count]  # discard self & locals
            except AttributeError:  # profile has no parameters
                self.args = {}
Exemple #6
0
def test_spline2d():
    x = np.linspace(0.1, 1, 10)
    log_y = np.linspace(-3, 1, 20)
    zarr_in = np.outer(x, np.exp(log_y))

    pk2d = ccl.Pk2D(a_arr=x, lk_arr=log_y, pk_arr=zarr_in, is_logp=False)

    pk2d_gsl_spline2d = pk2d.psp.fka
    xarr, yarr, zarr_out_spline = \
        ccl.pyutils._get_spline2d_arrays(pk2d_gsl_spline2d)

    cosmo = ccl.CosmologyVanillaLCDM()
    zarr_out_eval = pk2d.eval(k=np.exp(log_y), a=x[-1], cosmo=cosmo)

    assert np.allclose(x, xarr)
    assert np.allclose(log_y, yarr)
    assert np.allclose(zarr_in, zarr_out_spline)
    assert np.allclose(zarr_in[-1], zarr_out_eval)
Exemple #7
0
def test_timing():
    ls = np.unique(np.geomspace(2, 2000, 128).astype(int)).astype(float)
    nl = len(ls)
    b_g = np.array([
        1.376695, 1.451179, 1.528404, 1.607983, 1.689579, 1.772899, 1.857700,
        1.943754, 2.030887, 2.118943
    ])
    dNdz_file = np.load('benchmarks/data/dNdzs.npz')
    z_s = dNdz_file['z_sh']
    dNdz_s = dNdz_file['dNdz_sh'].T
    z_g = dNdz_file['z_cl']
    dNdz_g = dNdz_file['dNdz_cl'].T
    nt = len(dNdz_s) + len(dNdz_g)
    nx = (nt * (nt + 1)) // 2

    cosmo = ccl.CosmologyVanillaLCDM(transfer_function='boltzmann_class')
    cosmo.compute_nonlin_power()

    start = time.time()
    t_g = [
        ccl.NumberCountsTracer(cosmo,
                               True, (z_g, ng),
                               bias=(z_g, np.full(len(z_g), b)))
        for ng, b in zip(dNdz_g, b_g)
    ]
    t_s = [ccl.WeakLensingTracer(cosmo, (z_s, ns)) for ns in dNdz_s]
    t_all = t_g + t_s

    cls = np.zeros([nx, nl])
    ind1, ind2 = np.triu_indices(nt)
    for ix, (i1, i2) in enumerate(zip(ind1, ind2)):
        cls[ix, :] = ccl.angular_cl(cosmo, t_all[i1], t_all[i2], ls)
    end = time.time()
    t_seconds = end - start
    print(end - start)
    assert t_seconds < 3.
Exemple #8
0
def test_pk2d_from_model_raises():
    cosmo = ccl.CosmologyVanillaLCDM()
    assert_raises(ValueError, ccl.Pk2D.pk_from_model, cosmo, model='bbkss')
Exemple #9
0
import numpy as np
import pytest
from numpy.testing import assert_raises
import pyccl as ccl

NCHI = 100
CHIMIN = 100.
CHIMAX = 1000.
COSMO = ccl.CosmologyVanillaLCDM(transfer_function='bbks')


def tkkaf(k1, k2, a, alpha=1., beta=1.):
    return 1. / (k1**alpha * k2**beta)


def get_tk3d(alpha=1, beta=1):
    a_arr = np.linspace(0.1, 1., 10)
    k_arr = np.geomspace(1E-4, 1E3, 10)
    tkka_arr = np.array(
        [tkkaf(k_arr[None, :], k_arr[:, None], a, alpha, beta) for a in a_arr])
    return ccl.Tk3D(a_arr,
                    np.log(k_arr),
                    tkk_arr=np.log(tkka_arr),
                    is_logt=True)


def pred_covar(l1,
               l2,
               alpha=1.,
               beta=1.,
               prefac=1. / (4 * np.pi),
Exemple #10
0
def test_linear_matter_power_raises():
    cosmo = ccl.CosmologyVanillaLCDM(transfer_function=None)
    with pytest.raises(ccl.CCLError):
        ccl.linear_matter_power(cosmo, 1., 1.)
Exemple #11
0
def test_nonlin_power_raises():
    cosmo = ccl.CosmologyVanillaLCDM(transfer_function='bbks')
    with pytest.raises(KeyError):
        ccl.nonlin_power(cosmo, 1., 1., p_of_k_a='a:b')
Exemple #12
0
def test_nonlin_matter_power_raises():
    cosmo = ccl.CosmologyVanillaLCDM(matter_power_spectrum=None)
    with pytest.raises(ccl.CCLError):
        ccl.nonlin_matter_power(cosmo, 1., 1.)
Exemple #13
0
def test_cosmology_p18lcdm_raises():
    with pytest.raises(ValueError):
        kw = {'Omega_c': 0.1}
        ccl.CosmologyVanillaLCDM(**kw)
Exemple #14
0
def test_cosmology_setitem():
    cosmo = ccl.CosmologyVanillaLCDM()
    with pytest.raises(NotImplementedError):
        cosmo['a'] = 3
Exemple #15
0
def test_sM_raises():
    cosmo = ccl.CosmologyVanillaLCDM(transfer_function=None)
    with pytest.raises(ccl.CCLError):
        cosmo.compute_sigma()