def test_interp_ray_paths(self): cosmo = Cosmology() x = [1.4, -1.] y = [-0.4, 0.2] lens_model = LensModel(['SIS'], z_source=2., lens_redshift_list=[0.5], multi_plane=True) kwargs_lens = [{'theta_E': 1., 'center_x': 0., 'center_y': 0.}] interpx, interpy = interpolate_ray_paths(x, y, lens_model, kwargs_lens, 2., terminate_at_source=False, source_x=None, source_y=None, evaluate_at_mean=False) zarray = np.linspace(0., 2., 100) dc = [cosmo.D_C_transverse(zi) for zi in zarray] interpx_mean, interpy_mean = interpolate_ray_paths( x, y, lens_model, kwargs_lens, 2., terminate_at_source=False, source_x=None, source_y=None, evaluate_at_mean=True, cosmo=cosmo) for dci in dc: x, y = 0.5 * (interpx[0](dci) + interpx[1] (dci)), 0.5 * (interpy[0](dci) + interpy[1](dci)) npt.assert_almost_equal(x, interpx_mean[0](dci)) npt.assert_almost_equal(y, interpy_mean[0](dci)) x = [1.4, -1.] y = [-0.4, 0.2] source_x, source_y = 0.2, 0.5 interpx, interpy = interpolate_ray_paths(x, y, lens_model, kwargs_lens, 2., terminate_at_source=True, source_x=source_x, source_y=source_y, evaluate_at_mean=True) npt.assert_almost_equal(interpx[0](dc[-1]), source_x) npt.assert_almost_equal(interpy[0](dc[-1]), source_y)
def setup(self): realization = SingleHalo(10**8, 0., 0., 'TNFW', 0.1, 0.5, 1.2, subhalo_flag=False) zlist = np.arange(0.1, 1.2, 0.05) rmax = 0.3 for i, zi in enumerate(zlist): xi, yi = 0., 0. mi = np.random.uniform(7, 8) single_halo = SingleHalo(10**mi, xi, yi, 'TNFW', zi, 0.5, 1.2, subhalo_flag=False) realization = realization.join(single_halo) cosmo = Cosmology() self.realization = realization self.rmax = rmax zlist = np.arange(0.00, 1.2, 0.02) x_image = [0.] * len(zlist) y_image = [0.] * len(zlist) dlist = [cosmo.D_C_transverse(zi) for zi in zlist] self.x_image_interp_list = [interp1d(dlist, x_image)] self.y_image_interp_list = [interp1d(dlist, y_image)]
class TestCosmology(object): def setup(self): self.arcsec = 2 * np.pi / 360 / 3600 self.zlens = 1 self.zsource = 2 self.angle_diameter = 2 / self.arcsec self.angle_radius = 0.5 * self.angle_diameter self.H0 = 70 self.omega_baryon = 0.03 self.omega_DM = 0.25 self.sigma8 = 0.82 curvature = 'flat' self.ns = 0.9608 cosmo_params = { 'H0': self.H0, 'Om0': self.omega_baryon + self.omega_DM, 'Ob0': self.omega_baryon, 'sigma8': self.sigma8, 'ns': self.ns, 'curvature': curvature } self._dm, self._bar = self.omega_DM, self.omega_baryon self.cosmo = Cosmology(cosmo_kwargs=cosmo_params) self.geometry = Geometry(self.cosmo, self.zlens, self.zsource, self.angle_diameter, 'DOUBLE_CONE') def test_cosmo(self): da_true = self.cosmo.D_A(0, 1.824) da_interp = self.cosmo.D_A_z(1.824) npt.assert_almost_equal(da_true / da_interp, 1, 5) dc_true = self.cosmo.astropy.comoving_transverse_distance(1.4).value dc_interp = self.cosmo.D_C_z(1.4) dc_astropy = self.cosmo.astropy.comoving_distance(1.4).value npt.assert_almost_equal(dc_true / dc_interp, 1) npt.assert_almost_equal(dc_astropy / dc_true, 1) dc_transverse = self.cosmo.D_C_transverse(0.8) dc = self.cosmo.astropy.comoving_transverse_distance(0.8).value npt.assert_almost_equal(dc / dc_transverse, 1) ez = self.cosmo.E_z(0.8) ez_astropy = self.cosmo.astropy.efunc(0.8) npt.assert_almost_equal(ez / ez_astropy, 1) kpc_per_asec = self.cosmo.kpc_proper_per_asec(0.5) kpc_per_arcsec_true = self.cosmo.astropy.kpc_proper_per_arcmin( 0.5).value / 60 npt.assert_almost_equal(kpc_per_asec, kpc_per_arcsec_true, 2) rho_crit_0 = self.cosmo.rho_crit(0) rho_pc = un.Quantity(self.cosmo.astropy.critical_density(0), unit=un.Msun / un.pc**3) rho_Mpc = rho_pc.value * (1e+6)**3 npt.assert_almost_equal(rho_crit_0 / rho_Mpc, 1, 3) rho_crit = self.cosmo.rho_crit(0.6) rho_pc = un.Quantity(self.cosmo.astropy.critical_density(0.6), unit=un.Msun / un.m**3) rho_Mpc = rho_pc.value * self.cosmo.Mpc**3 npt.assert_almost_equal(rho_crit / rho_Mpc, 1, 3) rho_crit_dark_matter = self.cosmo.rho_dark_matter_crit rho_crit_DM_astropy = self.cosmo.astropy.critical_density(0.).value * \ self.cosmo.density_to_MsunperMpc * self.cosmo.astropy.Odm(0.) npt.assert_almost_equal(rho_crit_DM_astropy, rho_crit_dark_matter) rho_crit = self.cosmo.rho_crit(0.3) rho_pc = un.Quantity(self.cosmo.astropy.critical_density(0.3), unit=un.Msun / un.pc**3) rho_Mpc = rho_pc.value * (1e+6)**3 npt.assert_almost_equal(rho_crit / rho_Mpc, 1, 3) colossus = self.cosmo.colossus npt.assert_almost_equal(colossus.Om0, self.omega_DM + self.omega_baryon) npt.assert_almost_equal(colossus.Ob0, self.omega_baryon) npt.assert_almost_equal(colossus.H0, self.H0) npt.assert_almost_equal(colossus.sigma8, self.sigma8) npt.assert_almost_equal(colossus.ns, self.ns) halo_collapse_z = 9. age_today = self.cosmo.astropy.age(0.).value age_z = self.cosmo.astropy.age(halo_collapse_z).value age = age_today - age_z npt.assert_almost_equal(age, self.cosmo.halo_age(0., zform=halo_collapse_z)) npt.assert_almost_equal(self.cosmo.scale_factor(0.7), self.cosmo.astropy.scale_factor(0.7))
def test_add_pbh(self): kwargs_halo = {'c_scatter': False} realization = SingleHalo(10**9, 0., 0., 'TNFW', 0.1, 0.5, 1.5, subhalo_flag=False, kwargs_halo=kwargs_halo) zlist = [0.2, 0.4, 0.6] rmax = 0.3 for i, zi in enumerate(zlist): theta = np.random.uniform(0., 2 * np.pi) r = np.random.uniform(0, rmax**2)**0.5 xi, yi = np.cos(theta) * r, np.sin(theta) * r mi = np.random.uniform(8, 9) single_halo = SingleHalo(10**mi, xi, yi, 'TNFW', zi, 0.5, 1.5, subhalo_flag=False, kwargs_halo=kwargs_halo) realization = realization.join(single_halo) lens_model_list_init, _, kwargs_init, _ = realization.lensing_quantities( ) ext = RealizationExtensions(realization) mass_fraction = 0.1 kwargs_mass_function = { 'mass_function_type': 'DELTA', 'logM': 5., 'mass_fraction': 0.5 } fraction_in_halos = 0.5 zlist = np.arange(0.00, 1.02, 0.02) x_image = [0.] * len(zlist) y_image = [0.] * len(zlist) cosmo = Cosmology() dlist = [cosmo.D_C_transverse(zi) for zi in zlist] x_image_interp_list = [interp1d(dlist, x_image)] y_image_interp_list = [interp1d(dlist, y_image)] pbh_realization = ext.add_primordial_black_holes( mass_fraction, kwargs_mass_function, fraction_in_halos, x_image_interp_list, y_image_interp_list, rmax) lens_model_list, _, kwargs, _ = pbh_realization.lensing_quantities() for i, halo in enumerate(pbh_realization.halos): r2d = np.hypot(halo.x, halo.y) npt.assert_equal(r2d <= np.sqrt(2) * rmax, True) condition1 = 'PT_MASS' == halo.mdef condition2 = 'TNFW' == halo.mdef npt.assert_equal(np.logical_or(condition1, condition2), True)