def compute_basic_power_spectrum(self, om): """Computes the smoothed linear power spectrum and the wiggle ratio Parameters ---------- om : float The Omega_m value to generate a power spectrum for Returns ------- pk_smooth_lin : np.ndarray The power spectrum smoothed out pk_ratio : np.ndarray the ratio pk_lin / pk_smooth, transitioned using sigma_nl """ # Get base linear power spectrum from camb res = self.camb.get_data(om=om, h0=self.camb.h0) pk_smooth_lin = smooth( self.camb.ks, res["pk_lin"], method=self.smooth_type, om=om, h0=self.camb.h0) # Get the smoothed power spectrum pk_ratio = res["pk_lin"] / pk_smooth_lin - 1.0 # Get the ratio return pk_smooth_lin, pk_ratio
def compute_basic_power_spectrum(self, om): """ Computes the smoothed linear power spectrum and the wiggle ratio. Uses a fixed h0 as determined by the dataset cosmology. Parameters ---------- om : float The Omega_m value to generate a power spectrum for Returns ------- array pk_smooth - The power spectrum smoothed out array pk_ratio_dewiggled - the ratio pk_lin / pk_smooth """ # Get base linear power spectrum from camb r_s, pk_lin, _ = self.camb.get_data(om=om, h0=self.camb.h0) pk_smooth_lin = smooth( self.camb.ks, pk_lin, method=self.smooth_type, om=om, h0=self.camb.h0) # Get the smoothed power spectrum pk_ratio = pk_lin / pk_smooth_lin - 1.0 # Get the ratio return pk_smooth_lin, pk_ratio
def precompute(self, camb, om, h0): c = camb.get_data(om, h0) ks = c["ks"] r_drag = c["r_s"] pk_lin = c["pk_lin"] pk_nonlin_0 = c["pk_nl_0"] pk_nonlin_z = c["pk_nl_z"] r, xs, J00, J01, J11 = self.get_extra() # Get the spherical bessel functions j0 = jn(0, r_drag * ks) j2 = jn(2, r_drag * ks) # Get the smoothed linear power spectrum which we need to calculate the # BAO damping and SPT integrals used in the Noda2017 model pk_smooth_lin = smooth(ks, pk_lin, method=self.smooth_type, om=om, h0=h0) pk_smooth_nonlin_0 = smooth(ks, pk_nonlin_0, method=self.smooth_type, om=om, h0=h0) pk_smooth_nonlin_z = smooth(ks, pk_nonlin_z, method=self.smooth_type, om=om, h0=h0) pk_smooth_spline = splrep(ks, pk_smooth_lin) # Sigma^2_dd,rs, Sigma^2_ss,rs (Noda2019 model) sigma_dd_rs = integrate.simps(pk_smooth_lin * (1.0 - j0 + 2.0 * j2), ks) / (6.0 * np.pi**2) sigma_ss_rs = integrate.simps(pk_smooth_lin * j2, ks) / (2.0 * np.pi**2) # I_00/P_sm,lin, I_01/P_sm,lin, I_02/P_sm,lin Pdd_spt = np.zeros(ks.shape) Pdt_spt = np.zeros(ks.shape) Ptt_spt = np.zeros(ks.shape) for k, kval in enumerate(ks): rvals = r[k, 0:] rx = np.outer(rvals, xs) y = kval * np.sqrt(-2.0 * rx.T + 1.0 + rvals**2) pk_smooth_interp = splev(y, pk_smooth_spline) index = np.where(np.logical_and(y < camb.k_min, y > camb.k_max)) pk_smooth_interp[index] = 0.0 IP0 = kval**2 * ( (-10.0 * rx * xs + 7.0 * xs).T + 3.0 * rvals) / (y**2) IP1 = kval**2 * ((-6.0 * rx * xs + 7.0 * xs).T - rvals) / (y**2) Pdd_spt[k] = integrate.simps( pk_smooth_lin * integrate.simps(pk_smooth_interp * IP0 * IP0, xs, axis=0), rvals) Pdt_spt[k] = integrate.simps( pk_smooth_lin * integrate.simps(pk_smooth_interp * IP0 * IP1, xs, axis=0), rvals) Ptt_spt[k] = integrate.simps( pk_smooth_lin * integrate.simps(pk_smooth_interp * IP1 * IP1, xs, axis=0), rvals) Pdd_spt *= ks**3 / (392.0 * np.pi**2) / pk_smooth_lin Pdt_spt *= ks**3 / (392.0 * np.pi**2) / pk_smooth_lin Ptt_spt *= ks**3 / (392.0 * np.pi**2) / pk_smooth_lin # Add on k^2[J_00, J_01, J_11] to obtain P_sm,spt/P_sm,L - 1 Pdd_spt += ks**2 * integrate.simps(pk_smooth_lin * J00, ks, axis=1) / (1008.0 * np.pi**2) Pdt_spt += ks**2 * integrate.simps(pk_smooth_lin * J01, ks, axis=1) / (1008.0 * np.pi**2) Ptt_spt += ks**2 * integrate.simps(pk_smooth_lin * J11, ks, axis=1) / (336.0 * np.pi**2) # Compute the non linear correction to the power spectra using the fitting formulae from Jennings2012 growth_0, growth_z = self.get_growth_factor_Linder( om, 1.0e-4), self.get_growth_factor_Linder(om, camb.redshift) cfactor = (growth_z + growth_z**2 + growth_z**3) / (growth_0 + growth_0**2 + growth_0**3) Pdt_0 = (-12483.8 * np.sqrt(pk_smooth_nonlin_0) + 2.554 * pk_smooth_nonlin_0**2) / (1381.29 + 2.540 * pk_smooth_nonlin_0) Ptt_0 = (-12480.5 * np.sqrt(pk_smooth_nonlin_0) + 1.824 * pk_smooth_nonlin_0**2) / (2165.87 + 1.796 * pk_smooth_nonlin_0) Pdt_z = cfactor**2 * (Pdt_0 - pk_smooth_nonlin_0) + pk_smooth_nonlin_z Ptt_z = cfactor**2 * (Ptt_0 - pk_smooth_nonlin_0) + pk_smooth_nonlin_z Pdd_halofit = pk_smooth_nonlin_z / pk_smooth_lin - 1.0 Pdt_halofit = Pdt_z / pk_smooth_lin - 1.0 Ptt_halofit = Ptt_z / pk_smooth_lin - 1.0 return { "sigma_dd_rs": sigma_dd_rs, "sigma_ss_rs": sigma_ss_rs, "Pdd_spt": Pdd_spt, "Pdt_spt": Pdt_spt, "Ptt_spt": Ptt_spt, "Pdd_halofit": Pdd_halofit, "Pdt_halofit": Pdt_halofit, "Ptt_halofit": Ptt_halofit, }
cs = ["#CAF270", "#84D57B", "#4AB482", "#219180", "#1A6E73", "#234B5B", "#232C3B"] data = PowerSpectrum_DESIMockChallenge(recon=True, isotropic=False, fit_poles=[0, 2], min_k=0.02, max_k=0.45) # c = data.get_data()[0]["cosmology"] # generator = CambGenerator(om_resolution=1, h0_resolution=1, h0=c["h0"], ob=c["ob"], ns=c["ns"], redshift=c["z"], # mnu=c["mnu"]) # generator.load_data(can_generate=True) model = PowerBeutler2017(recon=True, isotropic=False, fix_params=["om"], poly_poles=[0, 2], correction=Correction.NONE, marg="full") pklin = np.array(pd.read_csv("../barry/data/desi_mock_challenge_post_recon/Pk_Planck15_Table4.txt", delim_whitespace=True, header=None)) model.set_fix_params(["om"]) model.set_data(data.get_data()) model.kvals = pklin[:, 0] model.pksmooth = smooth(model.kvals, pklin[:, 1]) model.pkratio = pklin[:, 1] / model.pksmooth - 1.0 # Power spectrum ls = "-" names = [f"Xinyi-std Pk", f"Pedro-std Pk", f"Baojiu-std Pk", f"Xinyi-Hada Pk", f"Hee-Jong-std Pk", f"Yu-Yu-std Pk", f"Javier-std Pk"] for i in range(7): data.set_realisation(i) fitter.add_model_and_dataset(model, data, name=names[i], color=cs[i], realisation=i, ls=ls) # Correlation Function data = CorrelationFunction_DESIMockChallenge(recon=True, isotropic=False, fit_poles=[0, 2]) model = CorrBeutler2017(recon=True, isotropic=False, fix_params=["om"], poly_poles=[0, 2], correction=Correction.NONE, marg="full") ls = "--"
def _generate_points(self, indexes): self.logger.info("Generating PT data") os.makedirs(self.data_dir, exist_ok=True) # Compute the smoothing kernels (assumes a Gaussian smoothing kernel) smoothing_kernel = np.exp(-self.CAMBGenerator.ks**2 * self.recon_smoothing_scale**2 / 2.0) # Run CAMBGenerator.get_data once to ensure the data is loaded under CAMBGenerator.data _, _, _ = self.CAMBGenerator.get_data() # Generate a grid of values for R1, R2, Imn and Jmn nx = 200 xs = np.linspace(-0.999, 0.999, nx) r = np.outer(1.0 / self.CAMBGenerator.ks, self.CAMBGenerator.ks) R1 = -(1.0 + r**2) / (24.0 * r**2) * ( 3.0 - 14.0 * r**2 + 3.0 * r**4) + (r**2 - 1.0)**4 / ( 16.0 * r**3) * np.log(np.fabs((1.0 + r) / (1.0 - r))) R2 = (1.0 - r**2) / (24.0 * r**2) * (3.0 - 2.0 * r**2 + 3.0 * r**4) + ( r**2 - 1.0)**3 * (1.0 + r**2) / (16.0 * r**3) * np.log( np.fabs((1.0 + r) / (1.0 - r))) J00 = ( 12.0 / r**2 - 158.0 + 100.0 * r**2 - 42.0 * r**4 + 3.0 * (r**2 - 1.0)**3 * (2.0 + 7.0 * r**2) / r**3 * np.log(np.fabs((1.0 + r) / (1.0 - r)))) J01 = ( 24.0 / r**2 - 202.0 + 56.0 * r**2 - 30.0 * r**4 + 3.0 * (r**2 - 1.0)**3 * (4.0 + 5.0 * r**2) / r**3 * np.log(np.fabs((1.0 + r) / (1.0 - r)))) J11 = 12.0 / r**2 - 82.0 + 4.0 * r**2 - 6.0 * r**4 + 3.0 * ( r**2 - 1.0)**3 * (2.0 + r**2) / r**3 * np.log( np.fabs((1.0 + r) / (1.0 - r))) # We get NaNs in R1, R2 etc., when r = 1.0 (diagonals). We manually set these to the correct values. # We also get numerical issues for large/small r, so we set these manually to asymptotic limits R1[np.diag_indices(len(self.CAMBGenerator.ks))] = 2.0 / 3.0 R2[np.diag_indices(len(self.CAMBGenerator.ks))] = 0.0 J00[np.diag_indices(len(self.CAMBGenerator.ks))] = -88.0 J01[np.diag_indices(len(self.CAMBGenerator.ks))] = -152.0 J11[np.diag_indices(len(self.CAMBGenerator.ks))] = -72.0 index = np.where(r < 1.0e-3) R1[index] = 16.0 / 15.0 * r[index]**2 R2[index] = 4.0 / 15.0 * r[index]**2 J00[index] = -168.0 J01[index] = -168.0 J11[index] = -56.0 index = np.where(r > 1.0e2) R1[index] = 16.0 / 15.0 R2[index] = 4.0 / 15.0 J00[index] = -97.6 J01[index] = -200.0 J11[index] = -100.8 rank = 0 if self.mpi_comm is None else mpi_comm.Get_rank() data = {} for key in [ "sigma", "sigma_dd", "sigma_ss", "sigma_nl", "sigma_dd_nl", "sigma_sd_nl", "sigma_ss_nl", "sigma_dd_rs", "sigma_ss_rs" ]: data[key] = np.zeros((self.CAMBGenerator.om_resolution, self.CAMBGenerator.h0_resolution)) for key in [ "R1", "R2", "Pdd_spt", "Pdt_spt", "Ptt_spt", "Pdd_halofit", "Pdt_halofit", "Ptt_halofit" ]: data[key] = np.zeros( (self.CAMBGenerator.om_resolution, self.CAMBGenerator.h0_resolution, self.CAMBGenerator.k_num)) for i, j in indexes: omch2 = self.CAMBGenerator.omch2s[i] h0 = self.CAMBGenerator.h0s[j] self.logger.debug("Rank %d Generating %d:%d %0.3f %0.3f" % (rank, i, j, omch2, h0)) # Get the CAMB power spectrum and spline it r_drag = self.CAMBGenerator.data[i, j, 0] pk_lin = self.CAMBGenerator.data[i, j, 1:1 + self.CAMBGenerator.k_num] pk_nonlin_0 = self.CAMBGenerator.data[i, j, 1 + self.CAMBGenerator.k_num:1 + 2 * self.CAMBGenerator.k_num] pk_nonlin_z = self.CAMBGenerator.data[i, j, 1 + 2 * self.CAMBGenerator.k_num:] # Get the spherical bessel functions j0 = special.jn(0, r_drag * self.CAMBGenerator.ks) j2 = special.jn(2, r_drag * self.CAMBGenerator.ks) # Get the smoothed linear power spectrum which we need to calculate the # BAO damping and SPT integrals used in the Noda2017 model om = omch2 / (h0 * h0) + self.CAMBGenerator.omega_b pk_smooth_lin = smooth(self.CAMBGenerator.ks, pk_lin, method=self.smooth_type, om=om, h0=h0) pk_smooth_nonlin_0 = smooth(self.CAMBGenerator.ks, pk_nonlin_0, method=self.smooth_type, om=om, h0=h0) pk_smooth_nonlin_z = smooth(self.CAMBGenerator.ks, pk_nonlin_z, method=self.smooth_type, om=om, h0=h0) pk_smooth_spline = interpolate.splrep(self.CAMBGenerator.ks, pk_smooth_lin) # Sigma^2 data["sigma"][i, j] = integrate.simps( pk_lin, self.CAMBGenerator.ks) / (6.0 * np.pi**2) # Sigma^2_dd, Sigma^2_ss (Seo2016-LPT model) data["sigma_dd"][i, j] = integrate.simps( pk_lin * (1.0 - smoothing_kernel)**2, self.CAMBGenerator.ks) / (6.0 * np.pi**2) data["sigma_ss"][i, j] = integrate.simps( pk_lin * smoothing_kernel**2, self.CAMBGenerator.ks) / (6.0 * np.pi**2) # Sigma^2_nl, Sigma^2_dd,nl, Sigma^2_sd,nl Sigma^2_ss,nl (Ding2018-EFT model) data["sigma_nl"][i, j] = integrate.simps( pk_lin * (1.0 - j0), self.CAMBGenerator.ks) / (6.0 * np.pi**2) data["sigma_dd_nl"][i, j] = integrate.simps( pk_lin * (1.0 - smoothing_kernel)**2 * (1.0 - j0), self.CAMBGenerator.ks) / (6.0 * np.pi**2) data["sigma_sd_nl"][i, j] = integrate.simps( pk_lin * (0.5 * (smoothing_kernel**2 + (1.0 - smoothing_kernel)**2) - j0 * smoothing_kernel * (1.0 - smoothing_kernel)), self.CAMBGenerator.ks, ) / (6.0 * np.pi**2) data["sigma_ss_nl"][i, j] = integrate.simps( pk_lin * smoothing_kernel**2 * (1.0 - j0), self.CAMBGenerator.ks) / (6.0 * np.pi**2) # Sigma^2_dd,rs, Sigma^2_ss,rs (Noda2019 model) data["sigma_dd_rs"][i, j] = integrate.simps( pk_smooth_lin * (1.0 - j0 + 2.0 * j2), self.CAMBGenerator.ks) / (6.0 * np.pi**2) data["sigma_ss_rs"][i, j] = integrate.simps( pk_smooth_lin * j2, self.CAMBGenerator.ks) / (2.0 * np.pi**2) # R_1/P_lin, R_2/P_lin data["R1"][i, j, :] = self.CAMBGenerator.ks**2 * integrate.simps( pk_lin * R1, self.CAMBGenerator.ks, axis=1) / (4.0 * np.pi**2) data["R2"][i, j, :] = self.CAMBGenerator.ks**2 * integrate.simps( pk_lin * R2, self.CAMBGenerator.ks, axis=1) / (4.0 * np.pi**2) # I_00/P_sm,lin, I_01/P_sm,lin, I_02/P_sm,lin for k, kval in enumerate(self.CAMBGenerator.ks): rvals = r[k, 0:] rx = np.outer(rvals, xs) y = kval * np.sqrt(-2.0 * rx.T + 1.0 + rvals**2) pk_smooth_interp = interpolate.splev(y, pk_smooth_spline) index = np.where( np.logical_and(y < self.CAMBGenerator.k_min, y > self.CAMBGenerator.k_max)) pk_smooth_interp[index] = 0.0 IP0 = kval**2 * ( (-10.0 * rx * xs + 7.0 * xs).T + 3.0 * rvals) / (y**2) IP1 = kval**2 * ( (-6.0 * rx * xs + 7.0 * xs).T - rvals) / (y**2) data["Pdd_spt"][i, j, k] = integrate.simps( pk_smooth_lin * integrate.simps(pk_smooth_interp * IP0 * IP0, xs, axis=0), rvals) data["Pdt_spt"][i, j, k] = integrate.simps( pk_smooth_lin * integrate.simps(pk_smooth_interp * IP0 * IP1, xs, axis=0), rvals) data["Ptt_spt"][i, j, k] = integrate.simps( pk_smooth_lin * integrate.simps(pk_smooth_interp * IP1 * IP1, xs, axis=0), rvals) data["Pdd_spt"][i, j, :] *= self.CAMBGenerator.ks**3 / ( 392.0 * np.pi**2) / pk_smooth_lin data["Pdt_spt"][i, j, :] *= self.CAMBGenerator.ks**3 / ( 392.0 * np.pi**2) / pk_smooth_lin data["Ptt_spt"][i, j, :] *= self.CAMBGenerator.ks**3 / ( 392.0 * np.pi**2) / pk_smooth_lin # Add on k^2[J_00, J_01, J_11] to obtain P_sm,spt/P_sm,L - 1 data["Pdd_spt"][ i, j, :] += self.CAMBGenerator.ks**2 * integrate.simps( pk_smooth_lin * J00, self.CAMBGenerator.ks, axis=1) / (1008.0 * np.pi**2) data["Pdt_spt"][ i, j, :] += self.CAMBGenerator.ks**2 * integrate.simps( pk_smooth_lin * J01, self.CAMBGenerator.ks, axis=1) / (1008.0 * np.pi**2) data["Ptt_spt"][ i, j, :] += self.CAMBGenerator.ks**2 * integrate.simps( pk_smooth_lin * J11, self.CAMBGenerator.ks, axis=1) / (336.0 * np.pi**2) # Compute the non linear correction to the power spectra using the fitting formulae from Jennings2012 growth_0, growth_z = Growth_factor_Linder( om, 1.0e-4), Growth_factor_Linder(om, self.CAMBGenerator.redshift) cfactor = (growth_z + growth_z**2 + growth_z**3) / (growth_0 + growth_0**2 + growth_0**3) Pdt_0 = (-12483.8 * np.sqrt(pk_smooth_nonlin_0) + 2.554 * pk_smooth_nonlin_0**2) / ( 1381.29 + 2.540 * pk_smooth_nonlin_0) Ptt_0 = (-12480.5 * np.sqrt(pk_smooth_nonlin_0) + 1.824 * pk_smooth_nonlin_0**2) / ( 2165.87 + 1.796 * pk_smooth_nonlin_0) Pdt_z = cfactor**2 * (Pdt_0 - pk_smooth_nonlin_0) + pk_smooth_nonlin_z Ptt_z = cfactor**2 * (Ptt_0 - pk_smooth_nonlin_0) + pk_smooth_nonlin_z data["Pdd_halofit"][ i, j, :] = pk_smooth_nonlin_z / pk_smooth_lin - 1.0 data["Pdt_halofit"][i, j, :] = Pdt_z / pk_smooth_lin - 1.0 data["Ptt_halofit"][i, j, :] = Ptt_z / pk_smooth_lin - 1.0 return data
h0=c["h0"], ob=c["ob"], ns=c["ns"], redshift=c["z"]) generator.load_data(can_generate=True) pt_generator = PTGenerator(generator, recon_smoothing_scale=15) pt_generator.load_data(can_generate=True) pt_generator.get_data() n = 1000 print("Takes on average, %.1f microseconds" % (timeit.timeit(test_rand_h0const(), number=n) * 1e6 / n)) pk_lin = generator.get_data(0.3)[1] pk_smooth_lin = smooth(generator.ks, pk_lin, method=pt_generator.smooth_type) plt.plot(generator.ks, pt_generator.get_data(0.2)["Pdd_spt"], color="b", linestyle="-", label=r"$\mathrm{SPT}\,\Omega_{m}=0.2$") plt.plot(generator.ks, pt_generator.get_data(0.3)["Pdd_spt"], color="r", linestyle="-", label=r"$\mathrm{SPT}\,\Omega_{m}=0.3$") plt.plot(generator.ks, pt_generator.get_data(0.2)["Pdd_halofit"], color="b",