def test_ogamma(self): z = tensor([1, 10, 500, 1000]) for Neff, Tcmb0, vals in ( # (3, 0, [1651.9, 858.2, 26.855, 13.642]), # cannot have Or0=0 (3, 2.725, [1651.8, 857.9, 26.767, 13.582]), (3, 4, [1651.4, 856.6, 26.489, 13.405]), # (3.04, 0, [1651.91, 858.205, 26.8586, 13.6469]), # cannot have Or0=0 (3.04, 2.725, [1651.76, 857.817, 26.7688, 13.5841]), (3.04, 4, [1651.21, 856.411, 26.4845, 13.4028]), ): cosmo = self.flat_cosmo_cls() cosmo.H0, cosmo.Om0, cosmo.Neff, cosmo.Tcmb0 = H70, 0.3, Neff, Tcmb0 * kelvin assert close(cosmo.angular_diameter_distance(z).to(Mpc).value, vals, rtol=5e-4) # from astropy: Just to be really sure, we also do a version where the # integral is analytic, which is a Ode = 0 flat universe. In this case # Integrate(1/E(x),{x,0,z}) = 2 ( sqrt((1+Or z)/(1+z)) - 1 )/(Or - 1) # Recall that c/H0 * Integrate(1/E) is FLRW.comoving_distance. hubdis = (299792.458 / 70.0) Neff = 3.04 for Tcmb0 in (2.725, 5): Ogamma0h2 = 4 * 5.670373e-8 / 299792458**3 * Tcmb0**4 / 1.87837e-26 Onu0h2 = Ogamma0h2 * 7 / 8 * (4 / 11)**(4 / 3) * Neff Or0 = (Ogamma0h2 + Onu0h2) / 0.7**2 vals = 2 * hubdis * (((1 + Or0 * z) / (1 + z))**0.5 - 1) / (Or0 - 1) cosmo = self.flat_cosmo_cls() cosmo.H0, cosmo.Neff, cosmo.Tcmb0, cosmo.Ode0 = H70, Neff, Tcmb0 * kelvin, 0 assert close(cosmo.comoving_distance(z).to(Mpc).value, vals)
def test_hyp2f1(a: complex, b: complex, c: complex, z: complex, expected: complex): res = hyp2f1(a, b, c, z) expected = tensor(expected, dtype=res.dtype) assert ((close(res.real, expected.real, equal_nan=True) and close(res.imag, expected.imag, equal_nan=True)) or close(abs(res - expected), 0, atol=1e-6))
def test_negdistmod(self): cosmo = self.cosmo_cls() cosmo.H0, cosmo.Om0, cosmo.Ode0 = H70, 0.2, 1.3 z = tensor([50, 100]) assert close( cosmo.luminosity_distance(z).to(Mpc).value, [16612.44047622, -46890.79092244]) assert close(cosmo.distmod(z), [46.102167189, 48.355437790944])
def test_flat_open_closed_icosmo(self, Om0, Ode0, data): cosmo = self.cosmo_cls() cosmo.H0, cosmo.Om0, cosmo.Ode0 = H70, Om0, Ode0 z, dm, da, dl = (tensor(_, dtype=torch.get_default_dtype()) for _ in np.loadtxt(StringIO(data), unpack=True)) assert close(cosmo.comoving_transverse_distance(z).to(Mpc).value, dm) assert close(cosmo.angular_diameter_distance(z).to(Mpc).value, da) assert close(cosmo.luminosity_distance(z).to(Mpc).value, dl)
def test_xtfuncs(): cosmo = AbstractLambdaCDMR() cosmo.H0, cosmo.Om0, cosmo.Ode0, cosmo.Neff, cosmo.Tcmb0 = H70, 0.3, 0.5, 3.04, 2.725 * kelvin z = tensor([2, 3.2]) assert close(cosmo.lookback_time_integrand(tensor(3)), 0.052218976654969378) assert close(cosmo.lookback_time_integrand(z), [0.10333179, 0.04644541]) assert close(cosmo.abs_distance_integrand(tensor(3)), 3.3420145059180402) assert close(cosmo.abs_distance_integrand(z), [2.7899584, 3.44104758])
def test_matter(): cosmo = AbstractFlatLambdaCDMR() cosmo.Om0 = 0.3 cosmo.Ob0 = 0.045 assert cosmo.Om(0) == 0.3 assert cosmo.Ob(0) == 0.045 assert close(cosmo.Om(Z), [0.3, 0.59124088, 0.77419355, 0.92045455]) assert close(cosmo.Ob(Z), [0.045, 0.08868613, 0.11612903, 0.13806818]) assert close(cosmo.Odm(Z), [0.255, 0.50255474, 0.65806452, 0.78238636]) assert close(cosmo.Ob(Z) + cosmo.Odm(Z), cosmo.Om(Z))
def test_flat_z1(self, func, vals, unit, rtol): cosmo = self.flat_cosmo_cls() cosmo.H0 = H70 cosmo.Om0 = 0.27 assert close(getattr(cosmo, func.__name__)(1).to(unit).value, vals, rtol=rtol)
def test_comoving_volume(self, Om0, Ode0, vals): z = tensor([0.5, 1, 2, 3, 5, 9]) # for (Om0, Ode0), vals in zip( # ((0.27, 0.73), (0.27, 0), (2, 0)), # # Form Ned Wright's calculator: not very *accurate* (sic), so # # like astropy, test to very low precision # ((29.123, 159.529, 630.427, 1178.531, 2181.485, 3654.802), # (20.501, 99.019, 380.278, 747.049, 1558.363, 3123.814), # (12.619, 44.708, 114.904, 173.709, 258.82, 358.992)) # ): c = self.cosmo_cls() c.H0, c.Om0, c.Ode0 = H70, Om0, Ode0 assert close(c.comoving_volume(z).to(Gpc**3).value, vals, rtol=1e-2)
def test_angular_diameter_distance_z1z2(self): cosmo = self.flat_cosmo_cls() cosmo.H0, cosmo.Om0 = H704, 0.272 with pytest.raises(RuntimeError): cosmo.angular_diameter_distance_z1z2(tensor((1, 2)), tensor((3, 4, 5))) assert close( cosmo.angular_diameter_distance_z1z2(1, 2).to(Mpc).value, 646.22968662822018) assert close( cosmo.angular_diameter_distance_z1z2(tensor([0, 0, 2, 0.5, 1]), tensor([2, 1, 1, 2.5, 1.1])).to(Mpc).value, [ 1760.0628637762106, 1670.7497657219858, -969.34452994, 1159.0970895962193, 115.72768186186921 ]) assert close( cosmo.angular_diameter_distance_z1z2( 0.1, tensor([0.1, 0.2, 0.5, 1.1, 2])).to(Mpc).value, [0, 332.09893173, 986.35635069, 1508.37010062, 1621.07937976]) # Non-flat (positive Ok0) test cosmo = self.cosmo_cls() cosmo.H0, cosmo.Om0, cosmo.Ode0 = H704, 0.2, 0.5 assert close( cosmo.angular_diameter_distance_z1z2(1, 2).to(Mpc).value, 620.1175337852428) # Non-flat (negative Ok0) test cosmo = self.cosmo_cls() cosmo.Om0, cosmo.Ode0 = 2, 1 assert close( cosmo.angular_diameter_distance_z1z2(1, 2).to(Mpc).value, 228.42914659246014)
def test_critical_density(): fac = (Newton_G / codata2014.G).to(Unit()) cosmo = AbstractFlatLambdaCDMR() cosmo.H0 = H704 cosmo.Om0 = 0.272 # constants defined only so accurately assert ((cosmo.critical_density0 * fac).to(gram / cm**3) - 9.309668456020899e-30) < 1e-9 assert cosmo.critical_density0 == cosmo.critical_density(0) assert close( (cosmo.critical_density(tensor([1, 5])) * fac).to(gram / cm**3).value, [2.70352772e-29, 5.53739080e-28])
def test_ocurv(): cosmo = AbstractFlatLambdaCDMR() cosmo.Om0 = 0.3 assert cosmo.Ok0 == 0 assert cosmo.Ok(0) == 0 assert (cosmo.Ok(Z) == ZERO).all() cosmo = AbstractLambdaCDMR() cosmo.Om0 = 0.3 cosmo.Ode0 = 0.5 assert abs(cosmo.Ok0 - 0.2) < SMALL assert abs(cosmo.Ok(0) - 0.2) < SMALL assert close(cosmo.Ok(Z), [0.2, 0.22929936, 0.21621622, 0.17307692]) assert (cosmo.Ok(Z) + cosmo.Om(Z) + cosmo.Ode(Z) == ONE).all()
def test_comoving_distance_z1z2(self): cosmo = self.cosmo_cls() cosmo.Om0, cosmo.Ode0 = 0.3, 0.8 with pytest.raises(RuntimeError): cosmo.comoving_distance_z1z2(tensor((1, 2)), tensor((3, 4, 5))) assert cosmo.comoving_distance_z1z2( 1, 2) == -cosmo.comoving_distance_z1z2(2, 1) assert close( cosmo.comoving_distance_z1z2(tensor([0, 0, 2, 0.5, 1]), tensor([2, 1, 1, 2.5, 1.1])).to(Mpc).value, [ 3767.90579253, 2386.25591391, -1381.64987862, 2893.11776663, 174.1524683 ])
def test_comoving_transverse_distance_z1z2(self): z1, z2 = tensor([0, 0, 2, 0.5, 1]), tensor([2, 1, 1, 2.5, 1.1]) cosmo = self.flat_cosmo_cls() cosmo.Om0 = 0.3 with pytest.raises(RuntimeError): cosmo.comoving_transverse_distance_z1z2(tensor((1, 2)), tensor((3, 4, 5))) assert close( cosmo.comoving_transverse_distance_z1z2(1, 2).to(Mpc).value, 1313.2232194828466) assert close( cosmo.comoving_distance_z1z2(z1, z2).to(Mpc).value, cosmo.comoving_transverse_distance_z1z2(z1, z2).to(Mpc).value) cosmo = self.flat_cosmo_cls() cosmo.Om0 = 1.5 assert close( cosmo.comoving_transverse_distance_z1z2(z1, z2).to(Mpc).value, [ 2202.72682564, 1559.51679971, -643.21002593, 1408.36365679, 85.09286258 ]) assert close( cosmo.comoving_distance_z1z2(z1, z2).to(Mpc).value, cosmo.comoving_transverse_distance_z1z2(z1, z2).to(Mpc).value) cosmo = self.cosmo_cls() cosmo.Om0, cosmo.Ode0 = 0.3, 0.5 assert close( cosmo.comoving_transverse_distance_z1z2(z1, z2).to(Mpc).value, [ 3535.931375645655, 2226.430046551708, -1208.6817970036532, 2595.567367601969, 151.36592003406884 ]) cosmo = self.cosmo_cls() cosmo.Om0, cosmo.Ode0 = 1, 0.2 assert close( cosmo.comoving_transverse_distance_z1z2( 0.1, tensor([0, 0.1, 0.2, 0.5, 1.1, 2])).to(Mpc).value, [ -281.31602666724865, 0, 248.58093707820436, 843.9331377460543, 1618.6104987686672, 2287.5626543279927 ])
def test_values(self, func_with_args, theirs): ours, args = func_with_args assert close(ours(*args).real, theirs(*args))
def test_absorption_distance(self): cosmo = self.flat_cosmo_cls() cosmo.H0, cosmo.Om0 = H704, 0.272 assert close(cosmo.absorption_distance(3), 7.98685853) assert close(cosmo.absorption_distance(tensor([1, 3])), [1.72576635, 7.98685853])
def test_definitions(self, x, y, z): assert close(elliprc(x, y), elliprf(x, y, y)) assert close(elliprd(x, y, z), elliprj(x, y, z, z))
def test_distance_in_special_cosmologies(self, Om0, val): cosmo = self.flat_cosmo_cls() cosmo.Om0 = Om0 assert close(cosmo.comoving_distance(0).to(Mpc).value, 0) assert close(cosmo.comoving_distance(1).to(Mpc).value, val)
def _test(args: torch.Tensor): assert all( close(f1, f2) for f1, f2 in combinations(( func(*arg, *args[nsym:]) for arg in permutations(args[:nsym])), 2))
def test_zeta(self, z, expected): assert close(zeta(z), expected)
def test_ode(): cosmo = AbstractFlatLambdaCDMR() cosmo.Om0 = 0.3 assert cosmo.Ode(0) == cosmo.Ode0 assert close(cosmo.Ode(Z), [0.7, 0.408759, 0.2258065, 0.07954545])
def test_distmod(self): cosmo = self.flat_cosmo_cls() cosmo.H0, cosmo.Om0 = H704, 0.272 assert cosmo.hubble_distance.to(Mpc) == 4258.415596590909 assert close(cosmo.distmod(tensor([1, 5])), [44.124857, 48.40167258])
def test_nontrivial_zeros(self, y): # we're not super good at zeros, so we only really get the first few... assert close(zeta(0.5 + 1j * y), 0, atol=1e-5) assert close(zeta(0.5 - 1j * y), 0, atol=1e-5)