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
#plt.matshow(cleaned_cube4[0,:,:], vmin=-1., vmax=2.) #plt.title("FG-subtracted (4 modes)") #plt.colorbar() ##plt.show() #plt.matshow(cleaned_cube4_hp[0,:,:].real, vmin=-1., vmax=2.) #plt.title("FG-subtracted (4 modes, high-pass)") #plt.colorbar() #plt.matshow(cleaned_cube8[0,:,:], vmin=-1., vmax=2.) #plt.title("FG-subtracted (8 modes)") #plt.colorbar() # Power spectra sig_k, sig_pk, sig_stddev = box.binned_power_spectrum(delta_x=signal_cube) proc4_k, proc4_pk, proc4_stddev = box.binned_power_spectrum( delta_x=cleaned_cube4) proc8_k, proc8_pk, proc8_stddev = box.binned_power_spectrum( delta_x=cleaned_cube8) proc4hp_k, proc4hp_pk, proc4hp_stddev = box.binned_power_spectrum( delta_x=cleaned_cube4_hp) th_k, th_pk = box.theoretical_power_spectrum() amp_fac = (tracer.signal_amplitude() * tracer.bias_HI())**2. plt.figure() plt.subplot(111) plt.plot(th_k, th_pk * amp_fac, 'k-') plt.errorbar(sig_k, sig_pk, yerr=sig_stddev, color='r', marker='.') plt.errorbar(proc4_k, proc4_pk, yerr=proc4_stddev, color='b', marker='x')
import pylab as plt from fastbox.box import CosmoBox, default_cosmo from nbodykit.algorithms.fftpower import FFTPower from nbodykit.lab import ArrayMesh from numpy import fft import sys # Use linear matter power for log-normal field #default_cosmo['matter_power_spectrum'] = 'linear' # Gaussian box np.random.seed(11) box = CosmoBox(cosmo=default_cosmo, box_scale=(1e2, 1e2, 1e2), nsamp=64, realise_now=False) box.realise_density() re_k, re_pk, re_stddev = box.binned_power_spectrum() th_k, th_pk = box.theoretical_power_spectrum() plt.matshow(box.delta_x[0].real, vmin=-1., vmax=5., cmap='cividis') plt.title("Density field (real)") plt.colorbar() plt.matshow(box.delta_x[0].imag, vmin=-1., vmax=5., cmap='cividis') plt.title("Density field (imag)") plt.colorbar() plt.show() sys.exit(0)