示例#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
示例#2
0
sys.exit(0)


# Log-normal box
delta_ln = box.lognormal(box.delta_x)
lnre_k, lnre_pk, lnre_stddev \
    = box.binned_power_spectrum(delta_k=fft.fftn(delta_ln))

plt.matshow(delta_ln[0], vmin=-1., vmax=2., cmap='cividis')
plt.title("Log-normal density")
plt.colorbar()


# Tests
box.test_sampling_error()
box.test_parseval()


# Plot some stuff
fig = plt.figure()
plt.plot(th_k, th_pk, 'b-', label="Theoretical P(k)")
#plt.errorbar(re_k, re_pk, yerr=re_stddev, fmt=".", color='r')
plt.plot(re_k, re_pk, 'r.', label="P(k) from density field")
plt.plot(lnre_k, lnre_pk, 'gx', label="P(k) from log-normal")
plt.xscale('log')
plt.yscale('log')
plt.legend(loc='lower left', frameon=False)

"""
plt.subplot(212)