def extract_initial_power_params(block, config, more_config): optional_param_names = ["nrun", "nrunrun", "nt", "ntrun", "r"] optional_params = get_optional_params(block, cosmo, optional_param_names) init_power = camb.InitialPowerLaw() init_power.set_params(ns=block[cosmo, 'n_s'], As=block[cosmo, 'A_s'], **optional_params, **more_config["initial_power_params"]) return init_power
def testInitialPower(self): pars = camb.CAMBparams() pars.set_cosmology(H0=67) import ctypes P = camb.InitialPowerLaw() P2 = ctypes.pointer(P) self.assertEqual(P.As, pars.InitPower.As) As = 1.8e-9 ns = 0.8 P.set_params(As=As, ns=ns) self.assertEqual(P.As, As) self.assertEqual(P2.contents.As, As) pars2 = camb.CAMBparams() pars2.set_cosmology(H0=67) pars2.InitPower.set_params(As=1.7e-9, ns=ns) self.assertEqual(pars2.InitPower.As, 1.7e-9) pars.set_initial_power(pars2.InitPower) self.assertEqual(pars.InitPower.As, 1.7e-9) pars.set_initial_power(P) self.assertEqual(pars.InitPower.As, As) ks = np.logspace(-5.5, 2, 1000) pk = (ks / P.pivot_scalar) ** (ns - 1) * As pars2.set_initial_power_table(ks, pk) self.assertAlmostEqual(pars2.scalar_power(1.1), pars.scalar_power(1.1), delta=As * 1e-4) sp = camb.SplinedInitialPower(ks=ks, PK=pk) pars2.set_initial_power(sp) self.assertAlmostEqual(pars2.scalar_power(1.1), pars.scalar_power(1.1), delta=As * 1e-4) self.assertFalse(sp.has_tensors()) self.assertFalse(pars2.InitPower.has_tensors()) sp = camb.SplinedInitialPower() sp.set_scalar_log_regular(10 ** (-5.5), 10. ** 2, pk) pars2.set_initial_power(sp) self.assertAlmostEqual(pars2.scalar_power(1.1), pars.scalar_power(1.1), delta=As * 1e-4) sp.set_tensor_log_regular(10 ** (-5.5), 10. ** 2, pk) pars2.set_initial_power(sp) self.assertAlmostEqual(pars2.tensor_power(1.1), pars.scalar_power(1.1), delta=As * 1e-4) self.assertTrue(sp.has_tensors()) sp.set_tensor_table([], []) self.assertFalse(sp.has_tensors()) pars2.set_initial_power(sp) results = camb.get_results(pars2) cl = results.get_lensed_scalar_cls(CMB_unit='muK') pars.InitPower.set_params(As=As, ns=ns) results2 = camb.get_results(pars) cl2 = results2.get_lensed_scalar_cls(CMB_unit='muK') self.assertTrue(np.allclose(cl, cl2, rtol=1e-4)) P = camb.InitialPowerLaw(As=2.1e-9, ns=0.9) pars2.set_initial_power(P) pars.InitPower.set_params(As=2.1e-9, ns=0.9) self.assertAlmostEqual(pars2.scalar_power(1.1), pars.scalar_power(1.1), delta=As * 1e-4) def PK(k, As, ns): return As * (k / 0.05) ** (ns - 1) * (1 + 0.1 * np.sin(10 * k)) pars.set_initial_power_function(PK, args=(3e-9, 0.95)) P = pars.scalar_power(ks) np.testing.assert_almost_equal(P, PK(ks, 3e-9, 0.95), decimal=4)
k_max = 20.0 halo_model_mode = pyhmx.constants.HMCode2016 # Get linear power spectrum # Set up CAMB p = camb.CAMBparams( WantTransfer=True, WantCls=False, Want_CMB_lensing=False, DoLensing=False, NonLinearModel=None, ) p.set_cosmology(H0=h * 100, omch2=omc * h**2, ombh2=omb * h**2, mnu=mnu) p.set_dark_energy(w=w, wa=wa) p.set_initial_power(camb.InitialPowerLaw(As=As, ns=ns)) z_lin = np.linspace(0, z_max, n_z, endpoint=True) p.set_matter_power(redshifts=z_lin, kmax=k_max, nonlinear=False) # Let CAMB do its thing with timer() as t_CAMB_lin: r = camb.get_results(p) # Get sigma_8, linear power spectrum, and Omega_m sigma8 = r.get_sigma8()[-1] k_lin, z_lin, pofk_lin_camb = r.get_matter_power_spectrum(minkh=1e-3, maxkh=20.0, npoints=128) omv = r.omega_de + r.get_Omega("photon") + r.get_Omega("neutrino")