def get_camb_theory(self, pars): kmax = 15 * acc results = camb.get_background(pars) k_per_logint = None if self.use_Weyl: PKWeyl = camb.get_matter_power_interpolator( pars, nonlinear=True, hubble_units=False, k_hunit=False, kmax=kmax, k_per_logint=k_per_logint, var1=model.Transfer_Weyl, var2=model.Transfer_Weyl, zmax=self.zmax, extrap_kmax=500 * acc) else: PKWeyl = None PKdelta = camb.get_matter_power_interpolator(pars, nonlinear=True, hubble_units=False, k_hunit=False, kmax=kmax, k_per_logint=k_per_logint, zmax=self.zmax, extrap_kmax=500 * acc) return results, PKdelta, PKWeyl
def __init__(self,omega_m,sigma_8,h,z): self.omega_m = omega_m self.sigma_8 = sigma_8 self.h = h self.z = z self.k_max = 10.0 self.c = 2.99792e+5 #========================= cosmo = camb.CAMBparams() cosmo.set_cosmology(H0=100.0*self.h, ombh2=0.048*(self.h**2.0), omch2=(self.omega_m - 0.048)*(self.h**2.0), mnu=0.06, omk=0, tau=0.06) cosmo.InitPower.set_params(As=2.0e-9, ns=0.973) results = camb.get_results(cosmo) cosmo.set_matter_power(redshifts=[0.0], kmax=10.0) cambres= camb.get_transfer_functions(cosmo) cosmo.NonLinear = model.NonLinear_both kh, z, pk = cambres.get_matter_power_spectrum(minkh=1e-3, maxkh=1.0, npoints = 10) sigma_8_temp = cambres.get_sigma8() As_new = ((self.sigma_8/sigma_8_temp)**2.0)*(2.0e-9) cosmo.InitPower.set_params(As=As_new, ns=0.973) cambres = camb.get_results(cosmo) backres = camb.get_background(cosmo) self.chi = backres.comoving_radial_distance(self.z) self.PK = camb.get_matter_power_interpolator(cosmo, nonlinear=True, hubble_units=False, k_hunit=False, kmax=self.k_max, zmin = 0.0, zmax=self.z[-1]) self.H_z = (backres.hubble_parameter(self.z))/self.c #Hubble parameter in 1/Mpc
def __initialize_CAMB(self): #{{{ # adds linear matter power interpolator print 'Initializing CAMB.' pars = camb.CAMBparams() pars.set_cosmology(H0=self.H0, ombh2=self.Ob * self.h**2., omch2=self.Oc * self.h**2., omk=0., mnu=self.Mnu, standard_neutrino_neff=self.Neff, nnu=self.Neff, TCMB=self.TCMB) pars.InitPower.set_params(ns=self.ns, As=self.As, pivot_scalar=self.pivot_scalar) pars.NonLinear = model.NonLinear_none # print(pars) self.my_PK = get_matter_power_interpolator(pars, zmin=0., zmax=6., nz_step=150, kmax=101., nonlinear=False, hubble_units=True, k_hunit=True).P pars.set_matter_power(redshifts=[0.], kmax=20.) results = camb.get_results(pars) self.sigma8 = results.get_sigma8()[0] print 'sigma8 = ' + str(self.sigma8)
def __initialize_nonlin_CAMB(self): print 'Initializing nonlinear CAMB.' parsnl = camb.CAMBparams() parsnl.set_cosmology(H0=self.H0, ombh2=self.Ob * self.h**2., omch2=self.Oc * self.h**2., omk=0., mnu=self.Mnu, standard_neutrino_neff=self.Neff, nnu=self.Neff, TCMB=self.TCMB) parsnl.InitPower.set_params(ns=self.ns, As=self.As, pivot_scalar=self.pivot_scalar) # parsnl.NonLinear = model.NonLinear_both parsnl.set_matter_power(redshifts=[0.], kmax=101.) self.my_PK_psi = get_matter_power_interpolator( parsnl, zmin=0., zmax=1100., nz_step=150, kmax=101., nonlinear=True, hubble_units=True, k_hunit=True, var1=model.Transfer_Weyl, var2=model.Transfer_Weyl).P
def _get_matter_power(self, zs, ks, nonlinear=False): PK = camb.get_matter_power_interpolator(self.pars, nonlinear=nonlinear, hubble_units=False, k_hunit=False, kmax=ks.max(), zmax=zs.max() + 1.) return (self.as8**2.) * PK.P(zs, ks, grid=True)
def nonlin_ps(z, l_max): pars_nl = model.CAMBparams(NonLinear = 1, WantTransfer = True, H0=h * 100, omch2=omch2, ombh2=ombh2, YHe = YHe) pars_nl.DarkEnergy.set_params(w=-1.13) pars_nl.set_for_lmax(lmax = l_max) pars_nl.InitPower.set_params(ns=ns, As = 2.196e-09) results = camb.get_background(pars_nl) k = np.linspace(- 5, k_max(z, l_max), 1000) return get_matter_power_interpolator(pars_nl, nonlinear=True, kmax = k_max(z, l_max), k_hunit = True, hubble_units = True)
def get_interpolated(): #For calculating large-scale structure and lensing results yourself, get a power spectrum #interpolation object. In this example we calculate the CMB lensing potential power #spectrum using the Limber approximation, using PK=camb.get_matter_power_interpolator() function. #calling PK(z, k) will then get power spectrum at any k and redshift z in range. nz = 100 #number of steps to use for the radial/redshift integration kmax=10 #kmax to use #First set up parameters as usual pars = camb.CAMBparams() #pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122) #pars.InitPower.set_params(ns=0.965) pars.set_cosmology(H0=100*para5[i, 3], ombh2=para5[i, 1], omch2=para5[i, 0] - para5[i, 1], mnu=0.0, omk=0, tau=0.0) #pars.set_dark_energy(w=-1.0, sound_speed=1.0, dark_energy_model='fluid') # pars.set_dark_energy() # re-set defaults pars.InitPower.set_params(ns=para5[i, 4], r=0) # pars.set_for_lmax(lmax, lens_potential_accuracy=0); #For Limber result, want integration over \chi (comoving radial distance), from 0 to chi_*. #so get background results to find chistar, set up arrage in chi, and calculate corresponding redshifts results= camb.get_background(pars) chistar = results.conformal_time(0)- model.tau_maxvis.value chis = np.linspace(0,chistar,nz) zs=results.redshift_at_comoving_radial_distance(chis) #Calculate array of delta_chi, and drop first and last points where things go singular dchis = (chis[2:]-chis[:-2])/2 chis = chis[1:-1] zs = zs[1:-1] #Get the matter power spectrum interpolation object (based on RectBivariateSpline) #Here for lensing we want the power spectrum of the Weyl potential. #PK = camb.get_matter_power_interpolator(pars, nonlinear=True,hubble_units=False, k_hunit=False, kmax=kmax,var1=model.Transfer_Weyl,var2=model.Transfer_Weyl, zmax=zs[-1]) PK = camb.get_matter_power_interpolator(pars, nonlinear=True, hubble_units=False, k_hunit=False, kmax=kmax, zmax = zs[-1]) sigma8_camb = results.get_sigma8() # present value of sigma_8 --- check kman, mikh etc #--------------------------------------------------- sigma8_input = para5[i, 2] r = (sigma8_input ** 2) / (sigma8_camb ** 2) # rescale factor #Have a look at interpolated power spectrum results for a range of redshifts #Expect linear potentials to decay a bit when Lambda becomes important, and change from non-linear growth plt.figure(figsize=(8,5)) #k=np.exp(np.log(10)*np.linspace(-4,5,1000)) k = kPk[:,0] #zplot = [0, 0.5, 1, 4 ,20] zplot = [0.0]
def get_power_spectra(params): Plin = camb.get_matter_power_interpolator(params, nonlinear=False, hubble_units=False, k_hunit=False, kmax=100, zmax=1200) Plin = Plin.P Pnonlin = camb.get_matter_power_interpolator(params, nonlinear=True, hubble_units=False, k_hunit=False, kmax=100, zmax=1200) Pnonlin = Pnonlin.P return Plin, Pnonlin
def pk_camb(self,k,z, kmax=10.0): pars = camb.CAMBparams() pars.set_cosmology(H0=100*self.h, ombh2=self.ob*self.h**2, omch2=self.ocdm*self.h**2) pars.InitPower.set_params(ns=self.ns) pars.set_matter_power(redshifts=[z], kmax=kmax) PK = get_matter_power_interpolator(pars); return PK.P(z,k)
def setPkInterpolator(self, aCambParams, aIsNonLinear=True): #If this is set, Limber integrals use P(z,k) instead of P(z=0,k)*D^2 self.p_kInterpolator = camb.get_matter_power_interpolator( aCambParams, kmax=1500.0, nonlinear=aIsNonLinear, hubble_units=False, k_hunit=False) return
def get_camb_theory(self, pars): kmax = 15 * acc results = camb.get_background(pars) k_per_logint = None if self.use_Weyl: PKWeyl = camb.get_matter_power_interpolator(pars, nonlinear=True, hubble_units=False, k_hunit=False, kmax=kmax, k_per_logint=k_per_logint, var1=model.Transfer_Weyl, var2=model.Transfer_Weyl, zmax=self.zmax, extrap_kmax=500 * acc) else: PKWeyl = None PKdelta = camb.get_matter_power_interpolator(pars, nonlinear=True, hubble_units=False, k_hunit=False, kmax=kmax, k_per_logint=k_per_logint, zmax=self.zmax, extrap_kmax=500 * acc) return results, PKdelta, PKWeyl
def pk_lin_camb(self,k,z, kmax=10.0): pars = camb.CAMBparams() pars.set_cosmology(H0=self.H0, ombh2=self.ob*self.h**2, omch2=self.ocdm*self.h**2) pars.InitPower.set_params(ns=self.ns) pars.set_matter_power(redshifts=[z], kmax=2.0) pk = get_matter_power_interpolator(pars) return pk
def get_nlpower_interpolatekz(self, zscatter=params['zscatter']): pkinterp = camb.get_matter_power_interpolator(self.pars, nonlinear=True, hubble_units=True, k_hunit=True, log_interp=True, kmax=5., zmax=1.01 * zscatter) self.nlpower_interpolatekz = np.vectorize(pkinterp.P) return self.nlpower_interpolatekz ## To be called as pkinterp.P(z, k);
def getPKinterp(nz=100, kmax=10, myVar=model.Transfer_tot): """ example code from http://camb.readthedocs.io/en/latest/CAMBdemo.html (modified to have nz,kmax,myVar as inputs) Purpose: For calculating large-scale structure and lensing results yourself, get a power spectrum interpolation object. In this example we calculate the CMB lensing potential power spectrum using the Limber approximation, using PK=camb.get_matter_power_interpolator() function. calling PK(z, k) will then get power spectrum at any k and redshift z in range. Inputs: nz: number of steps to use for the radial/redshift integration kmax: kmax to use myVar: the variable to get autopower spectrum of default: model.Transfer_tot for delta_tot Returns: the PK(z,k) interpolator chistar chis (array of chi values) dchis (delta chi array) zs (redshift array) pars (CAMB parameters) """ #First set up parameters as usual #pars = camb.CAMBparams() #pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122) #pars.InitPower.set_params(ns=0.965) pars = getPars() #For Limber result, want integration over \chi (comoving radial distance), from 0 to chi_*. #so get background results to find chistar, set up arrage in chi, and calculate corresponding redshifts results = camb.get_background(pars) chistar = results.conformal_time(0) - model.tau_maxvis.value chis = np.linspace(0, chistar, nz) zs = results.redshift_at_comoving_radial_distance(chis) #Calculate array of delta_chi, and drop first and last points where things go singular dchis = (chis[2:] - chis[:-2]) / 2 chis = chis[1:-1] zs = zs[1:-1] #Get the matter power spectrum interpolation object (based on RectBivariateSpline). #Here for lensing we want the power spectrum of the Weyl potential. PK = camb.get_matter_power_interpolator(pars, nonlinear=True, hubble_units=False, k_hunit=False, kmax=kmax, var1=myVar, var2=myVar, zmax=zs[-1]) return PK, chistar, chis, dchis, zs, pars
def _initPower(self,pkgrid_override=None): print("initializing power...") if pkgrid_override is None: self.PK = camb.get_matter_power_interpolator(self.pars, nonlinear=self.nonlinear,hubble_units=False, k_hunit=False, kmax=self.kmax, zmax=self.zmax) else: class Ptemp: def __init__(self,pkgrid): self.pk = pkgrid def P(self,zs,ks,grid=True): ks = np.asarray(ks) zs = np.asarray(zs) return self.pk(ks,zs,grid=grid).T self.PK = Ptemp(pkgrid_override)
def testPowers(self): pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.07, omk=0) pars.set_dark_energy() # re-set defaults pars.InitPower.set_params(ns=0.965, As=2e-9) self.assertAlmostEqual(pars.scalar_power(1), 1.801e-9, 4) self.assertAlmostEqual(pars.scalar_power([1, 1.5])[0], 1.801e-9, 4) pars.set_matter_power(redshifts=[0., 0.17, 3.1]) pars.NonLinear = model.NonLinear_none data = camb.get_results(pars) kh, z, pk = data.get_matter_power_spectrum(1e-4, 1, 20) kh2, z2, pk2 = data.get_linear_matter_power_spectrum() s8 = data.get_sigma8() self.assertAlmostEqual(s8[0], 0.24686, 4) self.assertAlmostEqual(s8[2], 0.80044, 4) pars.NonLinear = model.NonLinear_both data.calc_power_spectra(pars) kh3, z3, pk3 = data.get_matter_power_spectrum(1e-4, 1, 20) self.assertAlmostEqual(pk[-1][-3], 51.909, 2) self.assertAlmostEqual(pk3[-1][-3], 57.697, 2) self.assertAlmostEqual(pk2[-2][-4], 53.47, 2) camb.set_feedback_level(0) cls = data.get_cmb_power_spectra(pars) cls_tot = data.get_total_cls(2000) cls_scal = data.get_unlensed_scalar_cls(2000) cls_tensor = data.get_tensor_cls(2000) cls_lensed = data.get_lensed_scalar_cls(2000) cls_phi = data.get_lens_potential_cls(2000) PKnonlin = camb.get_matter_power_interpolator(pars, nonlinear=True) pars.set_matter_power( redshifts=[0, 0.09, 0.15, 0.42, 0.76, 1.5, 2.3, 5.5, 8.9], kmax=10, k_per_logint=5) pars.NonLinear = model.NonLinear_both results = camb.get_results(pars) kh, z, pk = results.get_nonlinear_matter_power_spectrum() pk_interp = PKnonlin.P(z, kh) self.assertTrue(np.sum((pk / pk_interp - 1)**2) < 0.005) camb.set_halofit_version('mead') _, _, pk = results.get_nonlinear_matter_power_spectrum( params=pars, var1='delta_cdm', var2='delta_cdm') self.assertAlmostEqual(pk[0][160], 232.08, 1)
def _init_interpolation(self): '''setup interpolation''' # log-spaced z samples for interpolation zs = np.concatenate([np.array([0.]), np.logspace(np.log10(0.001), np.log10(1200.), 699)]) # chi(z) & z(chi) chis = self.chi(zs, interp=False) self.z2chi_interp = interpolate.interp1d(zs, chis, kind='cubic', bounds_error=False, fill_value='extrapolate') self.chi2z_interp = interpolate.interp1d(chis, zs, kind='cubic', bounds_error=False, fill_value='extrapolate') # D_unnorm(z) D_unnorms = self.D_unnorm(zs, interp=False) self.z2D_unnorm_interp = interpolate.interp1d(zs, D_unnorms, kind='cubic', bounds_error=False, fill_value='extrapolate') # T(k, z=0) matter_trans = self.camb_results.get_matter_transfer_data() ks = matter_trans.q # in Mpc^{-1} Tks = matter_trans.transfer_data[camb.model.Transfer_tot-1, :, -1] Tks_norm = Tks / Tks[0] # normalize to one at low k self.Tk0 = Tks[0] self.Tk_interp = interpolate.interp1d(ks, Tks_norm, kind='cubic', bounds_error=False, fill_value='extrapolate') # P(k, z=0) Pks_linear = self.camb_results.get_linear_matter_power_spectrum( hubble_units=False, k_hunit=False)[2][0] Pks_nonlin = self.camb_results.get_nonlinear_matter_power_spectrum( hubble_units=False, k_hunit=False)[2][0] self.Pk_linear_interp = interpolate.interp1d(ks, Pks_linear, kind='cubic', bounds_error=False, fill_value='extrapolate') self.Pk_nonlin_interp = interpolate.interp1d(ks, Pks_nonlin, kind='cubic', bounds_error=False, fill_value='extrapolate') # P(k, z) with CAMB matter power interpolator self.camb_Pk_interp = camb.get_matter_power_interpolator(camb_pars, zmin=0, zmax=10, kmax=15, nonlinear=False, hubble_units=False, k_hunit=False) self.camb_Pk_interp_nl = camb.get_matter_power_interpolator(camb_pars, zmin=0, zmax=10, kmax=15, nonlinear=True, hubble_units=False, k_hunit=False)
def camb_matter_power_interpolator(zs, nonlinear=True): pars = camb.CAMBparams() pars.set_cosmology(H0=cosmo.H0, ombh2=cosmo.Ombh2, omch2=(cosmo.Omh2 - cosmo.Ombh2), omk=cosmo.Ok0) pars.InitPower.set_params(ns=cosmo.ns) pk_interp = camb.get_matter_power_interpolator(pars, zs=np.linspace( np.min(zs), np.max(zs), 150), kmax=kmax, nonlinear=nonlinear) return pk_interp
def testPowers(self): pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.07, omk=0) pars.set_dark_energy() # re-set defaults pars.InitPower.set_params(ns=0.965, As=2e-9) self.assertAlmostEqual(pars.scalar_power(1), 1.801e-9, 4) self.assertAlmostEqual(pars.scalar_power([1, 1.5])[0], 1.801e-9, 4) pars.set_matter_power(redshifts=[0., 0.17, 3.1]) pars.NonLinear = model.NonLinear_none data = camb.get_results(pars) kh, z, pk = data.get_matter_power_spectrum(1e-4, 1, 20) kh2, z2, pk2 = data.get_linear_matter_power_spectrum() s8 = data.get_sigma8() self.assertAlmostEqual(s8[0], 0.24686, 4) self.assertAlmostEqual(s8[2], 0.80044, 4) pars.NonLinear = model.NonLinear_both data.calc_power_spectra(pars) kh3, z3, pk3 = data.get_matter_power_spectrum(1e-4, 1, 20) self.assertAlmostEqual(pk[-1][-3], 51.909, 2) self.assertAlmostEqual(pk3[-1][-3], 57.697, 2) self.assertAlmostEqual(pk2[-2][-4], 53.47, 2) camb.set_feedback_level(0) cls = data.get_cmb_power_spectra(pars) cls_tot = data.get_total_cls(2000) cls_scal = data.get_unlensed_scalar_cls(2000) cls_tensor = data.get_tensor_cls(2000) cls_lensed = data.get_lensed_scalar_cls(2000) cls_phi = data.get_lens_potential_cls(2000) PKnonlin = camb.get_matter_power_interpolator(pars, nonlinear=True) pars.set_matter_power(redshifts=[0, 0.09, 0.15, 0.42, 0.76, 1.5, 2.3, 5.5, 8.9], kmax=10, k_per_logint=5) pars.NonLinear = model.NonLinear_both results = camb.get_results(pars) kh, z, pk = results.get_nonlinear_matter_power_spectrum() pk_interp = PKnonlin.P(z, kh) self.assertTrue(np.sum((pk / pk_interp - 1) ** 2) < 0.005) camb.set_halofit_version('mead') _, _, pk = results.get_nonlinear_matter_power_spectrum(params=pars, var1='delta_cdm', var2='delta_cdm') self.assertAlmostEqual(pk[0][160], 232.08,1)
def lin_ps(z, l_max): pars = model.CAMBparams(NonLinear=0, WantTransfer=True, H0=h * 100, omch2=omch2, ombh2=ombh2, YHe=YHe) pars.DarkEnergy.set_params(w=-1.13) pars.set_for_lmax(lmax=l_max) pars.InitPower.set_params(ns=ns, As=2.196e-09) results = camb.get_background(pars) PK = get_matter_power_interpolator(pars, nonlinear=False, kmax=k_max(z, l_max), k_hunit=True, hubble_units=True) return PK.P(z, k)
def nonlin_ps(z, l_max): pars_nl = model.CAMBparams(NonLinear=1, WantTransfer=True, H0=67.3, omch2=0.1199, ombh2=0.02205, YHe=0.24770) pars_nl.DarkEnergy.set_params(w=-1.13) pars_nl.set_for_lmax(lmax=l_max) pars_nl.InitPower.set_params(ns=0.9603, As=2.196e-09) results = camb.get_background(pars_nl) k = 10**np.linspace(-6, k_lim(z, l_max), 1000) return get_matter_power_interpolator(pars_nl, nonlinear=True, kmax=k_max(z, l_max), k_hunit=True, hubble_units=True)
def cps(z): pars = model.CAMBparams(NonLinear=1, WantTransfer=True, H0=67.3, omch2=0.1199, ombh2=0.02205, YHe=0.24770) pars.DarkEnergy.set_params(w=-1.13) #pars.set_for_lmax(lmax = l_max) pars.InitPower.set_params(ns=0.9603, As=2.196e-09) results = camb.get_background(pars) PK = get_matter_power_interpolator(pars, nonlinear=True, kmax=10, k_hunit=True, hubble_units=True) PK.P(z, k) return PK.P(z, k)
def get_interpolated(): #For calculating large-scale structure and lensing results yourself, get a power spectrum #interpolation object. In this example we calculate the CMB lensing potential power #spectrum using the Limber approximation, using PK=camb.get_matter_power_interpolator() function. #calling PK(z, k) will then get power spectrum at any k and redshift z in range. nz = 100 #number of steps to use for the radial/redshift integration kmax=10 #kmax to use #First set up parameters as usual pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122) pars.InitPower.set_params(ns=0.965) #For Limber result, want integration over \chi (comoving radial distance), from 0 to chi_*. #so get background results to find chistar, set up arrage in chi, and calculate corresponding redshifts results= camb.get_background(pars) chistar = results.conformal_time(0)- model.tau_maxvis.value chis = np.linspace(0,chistar,nz) zs=results.redshift_at_comoving_radial_distance(chis) #Calculate array of delta_chi, and drop first and last points where things go singular dchis = (chis[2:]-chis[:-2])/2 chis = chis[1:-1] zs = zs[1:-1] #Get the matter power spectrum interpolation object (based on RectBivariateSpline) #Here for lensing we want the power spectrum of the Weyl potential. #PK = camb.get_matter_power_interpolator(pars, nonlinear=True,hubble_units=False, k_hunit=False, kmax=kmax,var1=model.Transfer_Weyl,var2=model.Transfer_Weyl, zmax=zs[-1]) PK = camb.get_matter_power_interpolator(pars, nonlinear=True, hubble_units=False, k_hunit=False, kmax=kmax) #Have a look at interpolated power spectrum results for a range of redshifts #Expect linear potentials to decay a bit when Lambda becomes important, and change from non-linear growth plt.figure(figsize=(8,5)) k=np.exp(np.log(10)*np.linspace(-4,5,1000)) zplot = [0, 0.5, 1, 4 ,20] for z in zplot: ax0.loglog(k, PK.P(z,k)) #plt.xlim([1e-4,kmax]) #plt.xlabel('k Mpc') #plt.ylabel('$P_\Psi\, Mpc^{-3}$') plt.legend(['z=%s'%z for z in zplot]);
def P_lin(self,ks,zs,knorm = 1e-4,kmax = 0.1): """ This function will provide the linear matter power spectrum used in calculation of sigma2. It is written as P_lin(k,z) = norm(z) * T(k)**2 where T(k) is the Eisenstein, Hu, 1998 transfer function. Care has to be taken about interpreting this beyond LCDM. For example, the transfer function can be inaccurate for nuCDM and wCDM cosmologies. If this function is only used to model sigma2 -> N(M,z) -> halo model power spectra at small scales, and cosmological dependence is obtained through an accurate CAMB based P(k), one should be fine. """ tk = self.Tk(ks,'eisenhu_osc') assert knorm<kmax PK = camb.get_matter_power_interpolator(self.pars, nonlinear=False, hubble_units=False, k_hunit=False, kmax=kmax, zmax=zs.max()+1.) pnorm = PK.P(zs, knorm,grid=True) tnorm = self.Tk(knorm,'eisenhu_osc') * knorm**(self.params['ns']) plin = (pnorm/tnorm) * tk**2. * ks**(self.params['ns']) return (self.as8**2.) *plin
def Pk_mm(ks, zs, pars, nonlinear=None): from camb import model # Parameters kmax = 100. if (nonlinear == None): nl = False nlmod = 'mead2020' else: nl = True nlmod = nonlinear # Get non-linear power from CAMB pars.set_matter_power(redshifts=zs, kmax=kmax) pars.NonLinearModel.set_params(halofit_version=nlmod) #results = camb.get_results(pars) #ks_CAMB, _, Pk_CAMB = results.get_nonlinear_matter_power_spectrum(params=pars) # Create a power spectrum interpolation object Pk_CAMB_interp = camb.get_matter_power_interpolator( pars, nonlinear=nl, hubble_units=True, k_hunit=True, kmax=kmax, var1=model.Transfer_tot, var2=model.Transfer_tot, zmax=max(zs), ) # Evaluate interpolator at the desired k and z points Pk_CAMB = Pk_CAMB_interp.P(zs, ks) return Pk_CAMB
WantTransfer=True, H0=100 * h, omch2=omegach2, ombh2=omegabh2, YHe=YHe) pars.DarkEnergy.set_params(w=-1) pars.set_for_lmax(lmax=l_ul) pars.InitPower.set_params(ns=ns, As=As) #results = camb.get_background(pars) results = camb.get_results(pars) #print(results.get_sigma8()) #-- 0.84166148 k = np.linspace(10**(-5), k_max(l_ul, z_s), 1000) #print() PK = get_matter_power_interpolator(pars, nonlinear=False, kmax=np.max(k), k_hunit=False, hubble_units=False) #----------------------------------------------------------------------- ######################################################################## ## compute and plot ######################################################################## ### canvas fig, ax = plt.subplots(figsize=(7, 7)) fs = 14 mfs = 12 ax.tick_params(axis='both', which='major', labelsize=fs) ax.tick_params(axis='both', which='minor', labelsize=mfs)
def run_on_single_catalog(self, catalog_instance, catalog_name, output_dir): ''' run test on a single catalog ''' # check if needed quantities exist if not catalog_instance.has_quantities([self.z, self.ra, self.dec]): return TestResult(skipped=True, summary='do not have needed location quantities') if not catalog_instance.has_quantities([self.e1, self.e2, self.kappa]): return TestResult(skipped=True, summary='do not have needed shear quantities') catalog_data = self.get_catalog_data( catalog_instance, [self.z, self.ra, self.dec, self.e1, self.e2, self.kappa], filters=self.filters) #TODO: ns set to 0.963 for now, as this isn't within astropy's cosmology dictionaries. cosmo = catalog_instance.cosmology pars.set_cosmology(H0=cosmo.H0.value, ombh2=cosmo.Ob0 * (cosmo.H0.value / 100.)**2, omch2=(cosmo.Om0 - cosmo.Ob0) * (cosmo.H0.value / 100.)**2) #TODO: set sigma8 value to catalog value when this becomes possible pars.InitPower.set_params(ns=0.963, As=2.168e-9) #pars.InitPower.set_params(ns=0.963,As = 2.168e-9*(sigma8/0.8 )**2) camb.set_halofit_version(version='takahashi') p = camb.get_matter_power_interpolator(pars, nonlinear=True, k_hunit=False, hubble_units=False, kmax=100., zmax=1100., k_per_logint=False).P chi_recomb = cosmo.comoving_distance(1100.).value print(cosmo) # read in shear values and check limits e1 = catalog_data[self.e1] max_e1 = np.max(e1) min_e1 = np.min(e1) e2 = catalog_data[self.e2] max_e2 = np.max(e2) min_e2 = np.min(e2) if ((min_e1 < (-1.)) or (max_e1 > 1.0)): return TestResult(skipped=True, summary='e1 values out of range [-1,+1]') if ((min_e2 < (-1.)) or (max_e2 > 1.0)): return TestResult(skipped=True, summary='e2 values out of range [-1,+1]') # compute shear auto-correlation cat_s = treecorr.Catalog( ra=catalog_data[self.ra], dec=catalog_data[self.dec], g1=catalog_data[self.e1] - np.mean(catalog_data[self.e1]), g2=-(catalog_data[self.e2] - np.mean(catalog_data[self.e2])), ra_units='deg', dec_units='deg') gg = treecorr.GGCorrelation(nbins=self.nbins, min_sep=self.min_sep, max_sep=self.max_sep, sep_units='arcmin', bin_slop=self.bin_slop, verbose=True) gg.process(cat_s) r = np.exp(gg.meanlogr) #NOTE: We are computing 10^6 x correlation function for easier comparison xip = gg.xip * 1.e6 xim = gg.xim * 1.e6 #sig = np.sqrt(gg.varxi) # this is shape noise only - should be very low for simulation data do_jackknife = self.do_jackknife # Diagonal covariances for error bars on the plots. Use full covariance matrix for chi2 testing. if do_jackknife: cp_xip, cp_xim = self.jackknife(catalog_data, xip, xim) print(cp_xip) sig_jack = np.zeros((self.nbins)) sigm_jack = np.zeros((self.nbins)) for i in range(self.nbins): sig_jack[i] = np.sqrt(cp_xip[i][i]) sigm_jack[i] = np.sqrt(cp_xim[i][i]) else: sig_jack = np.zeros((self.nbins)) sigm_jack = np.zeros((self.nbins)) for i in range(self.nbins): sig_jack[i] = np.sqrt(gg.varxi[i]) * 1.e6 sigm_jack[i] = np.sqrt(gg.varxi[i]) * 1.e6 n_z = catalog_data[self.z] xvals, theory_plus, theory_minus = self.theory_corr( n_z, r, 10000, cosmo, p, chi_recomb) theory_plus = theory_plus * 1.e6 theory_minus = theory_minus * 1.e6 if do_jackknife: chi2_dof_1 = self.get_score( xip, theory_plus, cp_xip, opt='diagonal' ) #NOTE: correct this to opt=cov if you want full covariance matrix else: chi2_dof_1 = self.get_score(xip, theory_plus, 0, opt='nojack') # correct this print(theory_plus) print(theory_minus) print(xip) print(xim) #The following are further treecorr correlation functions that could be added in later to extend the test #treecorr.NNCorrelation(nbins=20, min_sep=2.5, max_sep=250, sep_units='arcmin') #treecorr.NGCorrelation(nbins=20, min_sep=2.5, max_sep=250, sep_units='arcmin') # count-shear (i.e. <gamma_t>(R)) #treecorr.NKCorrelation(nbins=20, min_sep=2.5, max_sep=250, sep_units='arcmin') # count-kappa (i.e. <kappa>(R)) #treecorr.KKCorrelation(nbins=20, min_sep=2.5, max_sep=250, sep_units='arcmin') # count-kappa (i.e. <kappa>(R)) fig, ax = plt.subplots(2, sharex=True) for ax_this in (ax, self.summary_ax): ax_this[0].errorbar(r, xip, sig_jack, lw=0.6, marker='o', ls='', color="#3f9b0b", label=r'$\chi_{+}$') ax_this[0].plot(xvals, theory_plus, 'o', color="#9a0eea", label=r'$\chi_{+}$' + " theory") ax_this[1].errorbar(r, xim, sigm_jack, lw=0.6, marker='o', ls='', color="#3f9b0b", label=r'$\chi_{-}$') ax_this[1].plot(xvals, theory_minus, 'o', color="#9a0eea", label=r'$\chi_{-}$' + " theory") self.post_process_plot(ax) fig.savefig(os.path.join(output_dir, 'plot.png')) plt.close(fig) score = chi2_dof_1 #calculate your summary statistics #TODO: This criteria for the score is effectively a placeholder if jackknifing isn't used and assumes a diagonal covariance if it is # Proper validation criteria need to be assigned to this test if score < 2: return TestResult(score, inspect_only=True) else: return TestResult(score, passed=False)
nz = 100 #number of steps to use for the radial/redshift integration kmax=10 #kmax to use pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122) pars.InitPower.set_params(ns=0.965) cambdata= camb.get_background(pars) chistar = cambdata.conformal_time(0)- model.tau_maxvis.value chis = np.linspace(0,chistar,nz) zs=cambdata.redshift_at_comoving_radial_distance(chis) dchis = (chis[2:]-chis[:-2])/2 chis = chis[1:-1] zs = zs[1:-1] PK = camb.get_matter_power_interpolator(pars, nonlinear=True, hubble_units=False, k_hunit=False, kmax=kmax, var1=model.Transfer_Weyl,var2=model.Transfer_Weyl, zmax=zs[-1]) plt.figure(figsize=(8,5)) k=np.exp(np.log(10)*np.linspace(-4,2,200)) zplot = [0, 0.5, 1, 4 ,20] for z in zplot: plt.loglog(k, PK.P(z,k)) plt.xlim([1e-4,kmax]) plt.xlabel('k Mpc') plt.ylabel('$P_\Psi\, Mpc^{-3}$') plt.legend(['z=%s'%z for z in zplot]); plt.xlabel('$L$'); plt.savefig(path + 'psii.pdf')
l_max = 600 pars = model.CAMBparams(NonLinear=0, WantTransfer=True, H0=67.3, omch2=0.1199, ombh2=0.02205, YHe=0.24770) pars.DarkEnergy.set_params(w=-1.13) pars.set_for_lmax(lmax=l_max) pars.InitPower.set_params(ns=0.9603, As=2.196e-09) results = camb.get_background(pars) k = 10**np.linspace(-6, 1, 1000) PK = get_matter_power_interpolator(pars, nonlinear=False, kmax=1, k_hunit=True, hubble_units=True) pars_nl = model.CAMBparams(NonLinear=1, WantTransfer=True, H0=67.3, omch2=0.1199, ombh2=0.02205, YHe=0.24770) pars_nl.DarkEnergy.set_params(w=-1.13) pars_nl.set_for_lmax(lmax=l_max) pars_nl.InitPower.set_params(ns=0.9603, As=2.196e-09) results = camb.get_background(pars_nl) k = 10**np.linspace(-6, 1, 1000) PK_nl = get_matter_power_interpolator(pars_nl,
k_lim = 1 k_min = -2 pars = model.CAMBparams(NonLinear=1, WantTransfer=True, H0=67.3, omch2=0.1199, ombh2=0.02205, YHe=0.24770) pars.DarkEnergy.set_params(w=-1.13) pars.set_for_lmax(lmax=l_max) pars.InitPower.set_params(ns=0.9603, As=2.196e-09) results = camb.get_background(pars) k = 10**np.linspace(k_min, k_lim, 1000) PK = get_matter_power_interpolator(pars, nonlinear=True, kmax=k_max, k_hunit=True, hubble_units=True) p = Tz(redshift)**2 * PK.P(redshift, k) plt.plot(k, p, label='21 cm') plt.plot(k, PK.P(redshift, k), label='Matter') #------------------------------------------------------------------------------ ############################################################################### # CII ############################################################################### pk3_2pi_file, k_file = np.loadtxt( '/home/dyskun/Documents/Utility/Git/Msaharan/C2SNR/C2/data-files/ps_CII_z6.txt', unpack=True) p_file = pk3_2pi_file * 2 * 3.14**2 / k_file**3
def testPowers(self): pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.07, omk=0) pars.set_dark_energy() # re-set defaults pars.InitPower.set_params(ns=0.965, As=2e-9) self.assertAlmostEqual(pars.scalar_power(1), 1.801e-9, 4) self.assertAlmostEqual(pars.scalar_power([1, 1.5])[0], 1.801e-9, 4) pars.set_matter_power(nonlinear=True) self.assertEqual(pars.NonLinear, model.NonLinear_pk) pars.set_matter_power(redshifts=[0., 0.17, 3.1], nonlinear=False) data = camb.get_results(pars) kh, z, pk = data.get_matter_power_spectrum(1e-4, 1, 20) kh2, z2, pk2 = data.get_linear_matter_power_spectrum() s8 = data.get_sigma8() self.assertAlmostEqual(s8[0], 0.24686, 3) self.assertAlmostEqual(s8[2], 0.80044, 3) pars.NonLinear = model.NonLinear_both data.calc_power_spectra(pars) kh3, z3, pk3 = data.get_matter_power_spectrum(1e-4, 1, 20) self.assertAlmostEqual(pk[-1][-3], 51.909, 2) self.assertAlmostEqual(pk3[-1][-3], 57.697, 2) self.assertAlmostEqual(pk2[-2][-4], 53.47, 2) camb.set_feedback_level(0) PKnonlin = camb.get_matter_power_interpolator(pars, nonlinear=True) pars.set_matter_power(redshifts=[0, 0.09, 0.15, 0.42, 0.76, 1.5, 2.3, 5.5, 8.9], kmax=10, k_per_logint=5) pars.NonLinear = model.NonLinear_both results = camb.get_results(pars) kh, z, pk = results.get_nonlinear_matter_power_spectrum() pk_interp = PKnonlin.P(z, kh) self.assertTrue(np.sum((pk / pk_interp - 1) ** 2) < 0.005) camb.set_halofit_version('mead') _, _, pk = results.get_nonlinear_matter_power_spectrum(params=pars, var1='delta_cdm', var2='delta_cdm') self.assertAlmostEqual(pk[0][160], 824.6, delta=0.5) lmax = 4000 pars.set_for_lmax(lmax) cls = data.get_cmb_power_spectra(pars) data.get_total_cls(2000) data.get_unlensed_scalar_cls(2500) data.get_tensor_cls(2000) cls_lensed = data.get_lensed_scalar_cls(3000) data.get_lens_potential_cls(2000) # check lensed CL against python; will only agree well for high lmax as python has no extrapolation template cls_lensed2 = correlations.lensed_cls(cls['unlensed_scalar'], cls['lens_potential'][:, 0], delta_cls=False) self.assertTrue(np.all(np.abs(cls_lensed2[2:2000, 2] / cls_lensed[2:2000, 2] - 1) < 1e-3)) self.assertTrue(np.all(np.abs(cls_lensed2[2:3000, 0] / cls_lensed[2:3000, 0] - 1) < 1e-3)) self.assertTrue(np.all(np.abs(cls_lensed2[2:3000, 1] / cls_lensed[2:3000, 1] - 1) < 1e-3)) self.assertTrue(np.all(np.abs((cls_lensed2[2:3000, 3] - cls_lensed[2:3000, 3]) / np.sqrt(cls_lensed2[2:3000, 0] * cls_lensed2[2:3000, 1])) < 1e-4)) corr, xvals, weights = correlations.gauss_legendre_correlation(cls['lensed_scalar']) clout = correlations.corr2cl(corr, xvals, weights, 2500) self.assertTrue(np.all(np.abs(clout[2:2300, 2] / cls['lensed_scalar'][2:2300, 2] - 1) < 1e-3))
### linear pars_l = model.CAMBparams(NonLinear=0, WantTransfer=True, H0=100 * h, omch2=omegach2, ombh2=omegabh2, YHe=YHe) pars_l.DarkEnergy.set_params(w=-1) pars_l.set_for_lmax(lmax=l_ul) pars_l.InitPower.set_params(ns=ns, As=As) #results_l = camb.get_background(pars_l) results_l = camb.get_results(pars_l) k_l = np.linspace(10**(-5), k_max(l_ul, z_s), k_step) PK_l = get_matter_power_interpolator(pars_l, nonlinear=False, kmax=np.max(k_l), k_hunit=False, hubble_units=False) ### non-linear pars_nl = model.CAMBparams(NonLinear=1, WantTransfer=True, H0=100 * h, omch2=omegach2, ombh2=omegabh2, YHe=YHe) pars_nl.DarkEnergy.set_params(w=-1) pars_nl.set_for_lmax(lmax=l_ul) pars_nl.InitPower.set_params(ns=ns, As=As) #results_nl = camb.get_background(pars_nl) results_nl = camb.get_results(pars_nl)
def testPowers(self): pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.07, omk=0) pars.set_dark_energy() # re-set defaults pars.InitPower.set_params(ns=0.965, As=2e-9) pars.NonLinearModel.set_params(halofit_version='takahashi') self.assertAlmostEqual(pars.scalar_power(1), 1.801e-9, 4) self.assertAlmostEqual(pars.scalar_power([1, 1.5])[0], 1.801e-9, 4) pars.set_matter_power(nonlinear=True) self.assertEqual(pars.NonLinear, model.NonLinear_pk) pars.set_matter_power(redshifts=[0., 0.17, 3.1], silent=True, nonlinear=False) data = camb.get_results(pars) kh, z, pk = data.get_matter_power_spectrum(1e-4, 1, 20) kh2, z2, pk2 = data.get_linear_matter_power_spectrum() s8 = data.get_sigma8() self.assertAlmostEqual(s8[0], 0.24686, 3) self.assertAlmostEqual(s8[2], 0.80044, 3) pars.NonLinear = model.NonLinear_both data.calc_power_spectra(pars) kh3, z3, pk3 = data.get_matter_power_spectrum(1e-4, 1, 20) self.assertAlmostEqual(pk[-1][-3], 51.909, 2) self.assertAlmostEqual(pk3[-1][-3], 57.709, 2) self.assertAlmostEqual(pk2[-2][-4], 56.436, 2) camb.set_feedback_level(0) PKnonlin = camb.get_matter_power_interpolator(pars, nonlinear=True) pars.set_matter_power(redshifts=[0, 0.09, 0.15, 0.42, 0.76, 1.5, 2.3, 5.5, 8.9], silent=True, kmax=10, k_per_logint=5) pars.NonLinear = model.NonLinear_both results = camb.get_results(pars) kh, z, pk = results.get_nonlinear_matter_power_spectrum() pk_interp = PKnonlin.P(z, kh) self.assertTrue(np.sum((pk / pk_interp - 1) ** 2) < 0.005) PKnonlin2 = results.get_matter_power_interpolator(nonlinear=True, extrap_kmax=500) pk_interp2 = PKnonlin2.P(z, kh) self.assertTrue(np.sum((pk_interp / pk_interp2 - 1) ** 2) < 0.005) pars.NonLinearModel.set_params(halofit_version='mead') _, _, pk = results.get_nonlinear_matter_power_spectrum(params=pars, var1='delta_cdm', var2='delta_cdm') self.assertAlmostEqual(pk[0][160], 824.6, delta=0.5) lmax = 4000 pars.set_for_lmax(lmax) cls = data.get_cmb_power_spectra(pars) data.get_total_cls(2000) data.get_unlensed_scalar_cls(2500) data.get_tensor_cls(2000) cls_lensed = data.get_lensed_scalar_cls(3000) cphi = data.get_lens_potential_cls(2000) # check lensed CL against python; will only agree well for high lmax as python has no extrapolation template cls_lensed2 = correlations.lensed_cls(cls['unlensed_scalar'], cls['lens_potential'][:, 0], delta_cls=False) np.testing.assert_allclose(cls_lensed2[2:2000, 2], cls_lensed[2:2000, 2], rtol=1e-3) np.testing.assert_allclose(cls_lensed2[2:2000, 1], cls_lensed[2:2000, 1], rtol=1e-3) np.testing.assert_allclose(cls_lensed2[2:2000, 0], cls_lensed[2:2000, 0], rtol=1e-3) self.assertTrue(np.all(np.abs((cls_lensed2[2:3000, 3] - cls_lensed[2:3000, 3]) / np.sqrt(cls_lensed2[2:3000, 0] * cls_lensed2[2:3000, 1])) < 1e-4)) corr, xvals, weights = correlations.gauss_legendre_correlation(cls['lensed_scalar']) clout = correlations.corr2cl(corr, xvals, weights, 2500) self.assertTrue(np.all(np.abs(clout[2:2300, 2] / cls['lensed_scalar'][2:2300, 2] - 1) < 1e-3)) pars = camb.CAMBparams() pars.set_cosmology(H0=78, YHe=0.22) pars.set_for_lmax(2000, lens_potential_accuracy=1) pars.WantTensors = True results = camb.get_transfer_functions(pars) from camb import initialpower cls = [] for r in [0, 0.2, 0.4]: inflation_params = initialpower.InitialPowerLaw() inflation_params.set_params(ns=0.96, r=r, nt=0) results.power_spectra_from_transfer(inflation_params, silent=True) cls += [results.get_total_cls(CMB_unit='muK')] self.assertTrue(np.allclose((cls[1] - cls[0])[2:300, 2] * 2, (cls[2] - cls[0])[2:300, 2], rtol=1e-3)) # Check generating tensors and scalars together pars = camb.CAMBparams() pars.set_cosmology(H0=67) lmax = 2000 pars.set_for_lmax(lmax, lens_potential_accuracy=1) pars.InitPower.set_params(ns=0.96, r=0) pars.WantTensors = False results = camb.get_results(pars) cl1 = results.get_total_cls(lmax, CMB_unit='muK') pars.InitPower.set_params(ns=0.96, r=0.1, nt=0) pars.WantTensors = True results = camb.get_results(pars) cl2 = results.get_lensed_scalar_cls(lmax, CMB_unit='muK') ctensor2 = results.get_tensor_cls(lmax, CMB_unit='muK') results = camb.get_transfer_functions(pars) results.Params.InitPower.set_params(ns=1.1, r=1) inflation_params = initialpower.InitialPowerLaw() inflation_params.set_params(ns=0.96, r=0.05, nt=0) results.power_spectra_from_transfer(inflation_params, silent=True) cl3 = results.get_lensed_scalar_cls(lmax, CMB_unit='muK') ctensor3 = results.get_tensor_cls(lmax, CMB_unit='muK') self.assertTrue(np.allclose(ctensor2, ctensor3 * 2, rtol=1e-4)) self.assertTrue(np.allclose(cl1, cl2, rtol=1e-4)) # These are identical because all scalar spectra were identical (non-linear corrections change it otherwise) self.assertTrue(np.allclose(cl1, cl3, rtol=1e-4)) pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.07, omk=0) pars.set_for_lmax(2500) pars.min_l = 2 res = camb.get_results(pars) cls = res.get_lensed_scalar_cls(2000) pars.min_l = 1 res = camb.get_results(pars) cls2 = res.get_lensed_scalar_cls(2000) np.testing.assert_allclose(cls[2:, 0:2], cls2[2:, 0:2], rtol=1e-4) self.assertAlmostEqual(cls2[1, 0], 1.30388e-10, places=13) self.assertAlmostEqual(cls[1, 0], 0)
def testPowers(self): pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.07, omk=0) pars.set_dark_energy() # re-set defaults pars.InitPower.set_params(ns=0.965, As=2e-9) self.assertAlmostEqual(pars.scalar_power(1), 1.801e-9, 4) self.assertAlmostEqual(pars.scalar_power([1, 1.5])[0], 1.801e-9, 4) pars.set_matter_power(redshifts=[0., 0.17, 3.1]) pars.NonLinear = model.NonLinear_none data = camb.get_results(pars) kh, z, pk = data.get_matter_power_spectrum(1e-4, 1, 20) kh2, z2, pk2 = data.get_linear_matter_power_spectrum() s8 = data.get_sigma8() self.assertAlmostEqual(s8[0], 0.24686, 4) self.assertAlmostEqual(s8[2], 0.80044, 4) pars.NonLinear = model.NonLinear_both data.calc_power_spectra(pars) kh3, z3, pk3 = data.get_matter_power_spectrum(1e-4, 1, 20) self.assertAlmostEqual(pk[-1][-3], 51.909, 2) self.assertAlmostEqual(pk3[-1][-3], 57.697, 2) self.assertAlmostEqual(pk2[-2][-4], 53.47, 2) camb.set_feedback_level(0) PKnonlin = camb.get_matter_power_interpolator(pars, nonlinear=True) pars.set_matter_power(redshifts=[0, 0.09, 0.15, 0.42, 0.76, 1.5, 2.3, 5.5, 8.9], kmax=10, k_per_logint=5) pars.NonLinear = model.NonLinear_both results = camb.get_results(pars) kh, z, pk = results.get_nonlinear_matter_power_spectrum() pk_interp = PKnonlin.P(z, kh) self.assertTrue(np.sum((pk / pk_interp - 1) ** 2) < 0.005) camb.set_halofit_version('mead') _, _, pk = results.get_nonlinear_matter_power_spectrum(params=pars, var1='delta_cdm', var2='delta_cdm') self.assertTrue(np.abs(pk[0][160] / 232.08 - 1) < 1e-3) lmax = 4000 pars.set_for_lmax(lmax) cls = data.get_cmb_power_spectra(pars) cls_tot = data.get_total_cls(2000) cls_scal = data.get_unlensed_scalar_cls(2500) cls_tensor = data.get_tensor_cls(2000) cls_lensed = data.get_lensed_scalar_cls(3000) cls_phi = data.get_lens_potential_cls(2000) # check lensed CL against python; will only agree well for high lmax as python has no extrapolation template cls_lensed2 = correlations.lensed_cls(cls['unlensed_scalar'], cls['lens_potential'][:, 0], delta_cls=False) self.assertTrue(np.all(np.abs(cls_lensed2[2:2000, 2] / cls_lensed[2:2000, 2] - 1) < 1e-3)) self.assertTrue(np.all(np.abs(cls_lensed2[2:3000, 0] / cls_lensed[2:3000, 0] - 1) < 1e-3)) self.assertTrue(np.all(np.abs(cls_lensed2[2:3000, 1] / cls_lensed[2:3000, 1] - 1) < 1e-3)) self.assertTrue(np.all(np.abs((cls_lensed2[2:3000, 3] - cls_lensed[2:3000, 3]) / np.sqrt(cls_lensed2[2:3000, 0] * cls_lensed2[2:3000, 1])) < 1e-4)) corr, xvals, weights = correlations.gauss_legendre_correlation(cls['lensed_scalar']) clout = correlations.corr2cl(corr, xvals, weights, 2500) self.assertTrue(np.all(np.abs(clout[2:2300, 2] / cls['lensed_scalar'][2:2300, 2] - 1) < 1e-3))
def get_cl_kappa(self, z, kmax=100, nz=100, lmax=10000): # For Limber result, want integration over \chi # (comoving radial distance) from 0 to chi_*. # so get background results to find chistar, # set up arrage in chi, and calculate corresponding redshifts pars = self.pars results = self.results chistar = results.comoving_radial_distance(z) chis = np.linspace(0, chistar, nz) zs = results.redshift_at_comoving_radial_distance(chis) #-- Calculate array of delta_chi, and drop first and last points #-- where things go singular dchis = (chis[2:] - chis[:-2]) / 2 chis = chis[1:-1] zs = zs[1:-1] #-- Get the matter power spectrum interpolation object #-- (based on RectBivariateSpline). #-- Here for lensing we want the power spectrum of the Weyl potential. PK = camb.get_matter_power_interpolator(pars, nonlinear=True, hubble_units=False, k_hunit=False, kmax=kmax, zmax=zs[-1]) #var1=camb.model.Transfer_Weyl, #var2=camb.model.Transfer_Weyl) #win = ((chistar-chis)/(chis**2*chistar))**2 win = ((chistar - chis) / (chistar / (1 + zs)))**2 #Do integral over chi ls = np.arange(2, lmax, dtype=float) cl_kappa = np.zeros(ls.shape) #this is just used to set to zero k values out of range of interpolation w = np.ones(len(chis)) for i, l in enumerate(ls): #k=(l+0.5)/chis k = (l) / chis w[:] = 1 w[k < 1e-4] = 0 w[k >= kmax] = 0 #cl_kappa[i] = np.dot(dchis, w*PK.P(zs, k, grid=False)*win/k**4) cl_kappa[i] = np.dot(dchis, w * PK.P(zs, k, grid=False) * win) #convert kappa power to [l(l+1)]^2C_phi/2pi (what cl_camb is) #cl_kappa *= (ls*(ls+1))**2 * 4 / (2*np.pi) cl_kappa *= 9 / 4 * pars.omegam**2 * (pars.H0 / 299792.458)**4 ell = np.zeros(ls.size + 2) cell = np.zeros(ls.size + 2) ell[1] = 1 ell[2:] = ls cell[2:] = cl_kappa self.PK = PK self.ell = ell self.cell = cell return ell, cell