Ejemplo n.º 1
0
def test_box_power_spectrum():
    """Check that the theoretical and box power spectra can be calculated."""

    # Realise Gaussian box
    np.random.seed(14)
    box = CosmoBox(cosmo=default_cosmo,
                   box_scale=(1e3, 1e3, 1e3),
                   nsamp=64,
                   realise_now=False)
    box.realise_density()

    # Calculate binned power spectrum and theoretical power spectrum
    re_k, re_pk, re_stddev = box.binned_power_spectrum()
    th_k, th_pk = box.theoretical_power_spectrum()

    # Check that sigma(R) and sigma_8 can be calculated
    sigR = box.sigmaR(R=8.)  # R in units of Mpc/h
    sig8 = box.sigma8()
    assert np.isclose(sigR, sig8)

    # Run built-in test to print a report on sampling accuracy
    box.test_sampling_error()

    # Check that sigma_8 calculated from box is close to input cosmo sigma_8
    # (this depends on box size/resolution)
    assert np.abs(sig8 - box.cosmo['sigma8']) < 0.09  # 0.09 is empirical
Ejemplo n.º 2
0
def test_gaussian_box():
    """Generate Gaussian density field in box."""
    # Realise Gaussian box
    np.random.seed(11)
    box = CosmoBox(cosmo=default_cosmo,
                   box_scale=(1e2, 1e2, 1e2),
                   nsamp=16,
                   realise_now=False)
    box.realise_density()

    # Check that density field is valid
    assert box.delta_x.shape == (16, 16, 16)
    assert box.delta_x.dtype == np.float64
    assert np.all(~np.isnan(box.delta_x))

    # Realise density field with same random seed and realise_now=True, and
    # manually setting the redshift and a single box_scale
    np.random.seed(11)
    box2 = CosmoBox(cosmo=default_cosmo,
                    box_scale=1e2,
                    nsamp=16,
                    redshift=0.,
                    realise_now=True)

    assert np.allclose(box.delta_x, box2.delta_x)

    # Check that pixel resolution etc. is correct
    assert box.Lx == box.Ly == box.Lz == 1e2
    assert box.x.size == box.y.size == box.z.size == 16
    assert np.isclose(np.max(box.x) - np.min(box.x), 1e2)

    # Check that cuboidal boxes work
    box3 = CosmoBox(cosmo=default_cosmo,
                    box_scale=(1e2, 2e2, 1e3),
                    nsamp=16,
                    redshift=1.,
                    realise_now=True)
    assert box3.delta_x.shape == (16, 16, 16)
    assert box3.delta_x.dtype == np.float64
    assert np.all(~np.isnan(box3.delta_x))
Ejemplo n.º 3
0
def test_box_redshift_space_density():
    """Check that a redshift-space density field can be generated."""

    # Realise Gaussian box and velocity field
    np.random.seed(11)
    box = CosmoBox(cosmo=default_cosmo,
                   box_scale=(1e2, 1e2, 1e2),
                   nsamp=16,
                   realise_now=False)
    box.realise_density()
    box.realise_velocity()

    # Get redshift-space density field
    vel_z = np.fft.ifftn(box.velocity_k[2]).real
    delta_s = box.redshift_space_density(delta_x=box.delta_x,
                                         velocity_z=vel_z,
                                         sigma_nl=200.,
                                         method='linear')

    # Check that redshift-space density field is valid
    assert delta_s.shape == (16, 16, 16)
    # assert delta_s.dtype == np.float64
    assert np.all(~np.isnan(delta_s))
Ejemplo n.º 4
0
#!/usr/bin/env python

import numpy as np
import pylab as plt
from fastbox.box import CosmoBox, default_cosmo
from numpy import fft

# Gaussian box
np.random.seed(10)
box = CosmoBox(cosmo=default_cosmo,
               box_scale=(1e2, 1e2, 1e2),
               nsamp=128,
               realise_now=False)
box.realise_density()
box.realise_velocity()

# Plot real-space density field
plt.matshow(box.delta_x[0], vmin=-1., vmax=20., cmap='cividis')
plt.title("Real-space")
plt.colorbar()

# Get redshift-space density field
vel_z = fft.ifftn(box.velocity_k[2]).real

delta_s = box.redshift_space_density(delta_x=box.delta_x,
                                     velocity_z=vel_z,
                                     sigma_nl=200.,
                                     method='linear')

plt.matshow(delta_s[0], vmin=-1., vmax=20., cmap='cividis')
plt.title("Redshift-space")