class TestHernquistEllipseCSE(object): """ tests the Gaussian methods """ def setup(self): self.hernquist = Hernquist() self.hernquist_cse = HernquistEllipseCSE() def test_function(self): x = np.linspace(0.01, 2, 10) y = np.zeros_like(x) kwargs = {'sigma0': 2, 'Rs': 2, 'center_x': 0, 'center_y': 0} f_nfw = self.hernquist.function(x, y, **kwargs) f_cse = self.hernquist_cse.function(x, y, e1=0, e2=0, **kwargs) npt.assert_almost_equal(f_cse / f_nfw, 1, decimal=5) def test_derivatives(self): x = np.linspace(0.01, 2, 10) y = np.zeros_like(x) kwargs = {'sigma0': 0.5, 'Rs': 2, 'center_x': 0, 'center_y': 0} f_x_nfw, f_y_nfw = self.hernquist.derivatives(x, y, **kwargs) f_x_cse, f_y_cse = self.hernquist_cse.derivatives(x, y, e1=0, e2=0, **kwargs) npt.assert_almost_equal(f_x_cse, f_x_nfw, decimal=5) npt.assert_almost_equal(f_y_cse, f_y_nfw, decimal=5) def test_hessian(self): x = np.linspace(0.01, 5, 30) y = np.zeros_like(x) kwargs = {'sigma0': 0.5, 'Rs': 2, 'center_x': 0, 'center_y': 0} f_xx_nfw, f_xy_nfw, f_yx_nfw, f_yy_nfw = self.hernquist.hessian(x, y, **kwargs) f_xx_cse, f_xy_cse, f_yx_cse, f_yy_cse = self.hernquist_cse.hessian(x, y, e1=0, e2=0, **kwargs) npt.assert_almost_equal(f_xx_cse / f_xx_nfw, 1, decimal=2) npt.assert_almost_equal(f_xy_cse, f_xy_nfw, decimal=5) npt.assert_almost_equal(f_yx_cse, f_yx_nfw, decimal=5) npt.assert_almost_equal(f_yy_cse, f_yy_nfw, decimal=5) def test_mass_3d_lens(self): R = 1 Rs = 3 alpha_Rs = 1 m_3d_nfw = self.hernquist.mass_3d_lens(R, Rs, alpha_Rs) m_3d_cse = self.hernquist_cse.mass_3d_lens(R, Rs, alpha_Rs) npt.assert_almost_equal(m_3d_nfw, m_3d_cse, decimal=8)
class TestHernquist(object): def setup(self): self.profile = Hernquist() def test_function(self): x = np.array([1]) y = np.array([2]) Rs = 1. sigma0 = 0.5 values = self.profile.function(x, y, sigma0, Rs) npt.assert_almost_equal(values[0], 0.66514613455415028, decimal=8) x = np.array([0]) y = np.array([0]) Rs = 1. sigma0 = 0.5 values = self.profile.function(x, y, sigma0, Rs) npt.assert_almost_equal(values[0], 0, decimal=6) x = np.array([2, 3, 4]) y = np.array([1, 1, 1]) values = self.profile.function(x, y, sigma0, Rs) npt.assert_almost_equal(values[0], 0.66514613455415028, decimal=8) npt.assert_almost_equal(values[1], 0.87449395673649566, decimal=8) npt.assert_almost_equal(values[2], 1.0549139073851708, decimal=8) def test_derivatives(self): x = 1 y = 2 Rs = 1. sigma0 = 0.5 f_x, f_y = self.profile.derivatives(x, y, sigma0, Rs) npt.assert_almost_equal(f_x, 0.11160641027573866, decimal=8) npt.assert_almost_equal(f_y, 0.22321282055147731, decimal=8) x = np.array([0]) y = np.array([0]) f_x, f_y = self.profile.derivatives(x, y, sigma0, Rs) npt.assert_almost_equal(f_x, 0, decimal=8) npt.assert_almost_equal(f_y, 0, decimal=8) def test_hessian(self): x = np.array([1]) y = np.array([2]) Rs = 1. sigma0 = 0.5 f_xx, f_xy, f_yx, f_yy = self.profile.hessian(x, y, sigma0, Rs) npt.assert_almost_equal(f_xx[0], 0.0779016004481825, decimal=6) npt.assert_almost_equal(f_yy[0], -0.023212809452388683, decimal=6) npt.assert_almost_equal(f_xy[0], -0.0674096084507525, decimal=6) npt.assert_almost_equal(f_xy, f_yx, decimal=8) def test_mass_tot(self): rho0 = 1 Rs = 3 m_tot = self.profile.mass_tot(rho0, Rs) npt.assert_almost_equal(m_tot, 169.64600329384882, decimal=6) def test_grav_pot(self): x, y = 1, 0 rho0 = 1 Rs = 3 grav_pot = self.profile.grav_pot(x, y, rho0, Rs, center_x=0, center_y=0) npt.assert_almost_equal(grav_pot, 42.411500823462205, decimal=8) def test_sigma0_definition(self): Rs = 2. sigma0 = 0.5 f_x, f_y = self.profile.derivatives(Rs, 0, sigma0, Rs) alpha = f_x npt.assert_almost_equal(alpha, 2 / 3. * sigma0 * Rs, decimal=5)
class Hernquist_Ellipse(object): """ this class contains functions concerning the NFW profile relation are: R_200 = c * Rs """ def __init__(self): self.spherical = Hernquist() self._diff = 0.000001 def function(self, x, y, sigma0, Rs, q, phi_G, center_x=0, center_y=0): """ returns double integral of NFW profile """ x_shift = x - center_x y_shift = y - center_y cos_phi = np.cos(phi_G) sin_phi = np.sin(phi_G) e = abs(1 - q) x_ = (cos_phi * x_shift + sin_phi * y_shift) * np.sqrt(1 - e) y_ = (-sin_phi * x_shift + cos_phi * y_shift) * np.sqrt(1 + e) f_ = self.spherical.function(x_, y_, sigma0, Rs) return f_ def derivatives(self, x, y, sigma0, Rs, q, phi_G, center_x=0, center_y=0): """ returns df/dx and df/dy of the function (integral of NFW) """ x_shift = x - center_x y_shift = y - center_y cos_phi = np.cos(phi_G) sin_phi = np.sin(phi_G) e = abs(1 - q) x_ = (cos_phi * x_shift + sin_phi * y_shift) * np.sqrt(1 - e) y_ = (-sin_phi * x_shift + cos_phi * y_shift) * np.sqrt(1 + e) f_x_prim, f_y_prim = self.spherical.derivatives(x_, y_, sigma0, Rs) f_x_prim *= np.sqrt(1 - e) f_y_prim *= np.sqrt(1 + e) f_x = cos_phi * f_x_prim - sin_phi * f_y_prim f_y = sin_phi * f_x_prim + cos_phi * f_y_prim return f_x, f_y def hessian(self, x, y, sigma0, Rs, q, phi_G, center_x=0, center_y=0): """ returns Hessian matrix of function d^2f/dx^2, d^f/dy^2, d^2/dxdy """ alpha_ra, alpha_dec = self.derivatives(x, y, sigma0, Rs, q, phi_G, center_x, center_y) diff = self._diff alpha_ra_dx, alpha_dec_dx = self.derivatives(x + diff, y, sigma0, Rs, q, phi_G, center_x, center_y) alpha_ra_dy, alpha_dec_dy = self.derivatives(x, y + diff, sigma0, Rs, q, phi_G, center_x, center_y) f_xx = (alpha_ra_dx - alpha_ra) / diff f_xy = (alpha_ra_dy - alpha_ra) / diff #f_yx = (alpha_dec_dx - alpha_dec)/diff f_yy = (alpha_dec_dy - alpha_dec) / diff return f_xx, f_yy, f_xy
class Hernquist_Ellipse(LensProfileBase): """ this class contains functions for the elliptical Hernquist profile. Ellipticity is defined in the potential. """ param_names = ['sigma0', 'Rs', 'e1', 'e2', 'center_x', 'center_y'] lower_limit_default = { 'sigma0': 0, 'Rs': 0, 'e1': -0.5, 'e2': -0.5, 'center_x': -100, 'center_y': -100 } upper_limit_default = { 'sigma0': 100, 'Rs': 100, 'e1': 0.5, 'e2': 0.5, 'center_x': 100, 'center_y': 100 } def __init__(self): self.spherical = Hernquist() self._diff = 0.0000000001 super(Hernquist_Ellipse, self).__init__() def function(self, x, y, sigma0, Rs, e1, e2, center_x=0, center_y=0): """ returns double integral of NFW profile """ phi_G, q = param_util.ellipticity2phi_q(e1, e2) x_shift = x - center_x y_shift = y - center_y cos_phi = np.cos(phi_G) sin_phi = np.sin(phi_G) e = abs(1 - q) x_ = (cos_phi * x_shift + sin_phi * y_shift) * np.sqrt(1 - e) y_ = (-sin_phi * x_shift + cos_phi * y_shift) * np.sqrt(1 + e) f_ = self.spherical.function(x_, y_, sigma0, Rs) return f_ def derivatives(self, x, y, sigma0, Rs, e1, e2, center_x=0, center_y=0): """ returns df/dx and df/dy of the function (integral of NFW) """ phi_G, q = param_util.ellipticity2phi_q(e1, e2) x_shift = x - center_x y_shift = y - center_y cos_phi = np.cos(phi_G) sin_phi = np.sin(phi_G) e = abs(1 - q) x_ = (cos_phi * x_shift + sin_phi * y_shift) * np.sqrt(1 - e) y_ = (-sin_phi * x_shift + cos_phi * y_shift) * np.sqrt(1 + e) f_x_prim, f_y_prim = self.spherical.derivatives(x_, y_, sigma0, Rs) f_x_prim *= np.sqrt(1 - e) f_y_prim *= np.sqrt(1 + e) f_x = cos_phi * f_x_prim - sin_phi * f_y_prim f_y = sin_phi * f_x_prim + cos_phi * f_y_prim return f_x, f_y def hessian(self, x, y, sigma0, Rs, e1, e2, center_x=0, center_y=0): """ returns Hessian matrix of function d^2f/dx^2, d^f/dy^2, d^2/dxdy """ alpha_ra, alpha_dec = self.derivatives(x, y, sigma0, Rs, e1, e2, center_x, center_y) diff = self._diff alpha_ra_dx, alpha_dec_dx = self.derivatives(x + diff, y, sigma0, Rs, e1, e2, center_x, center_y) alpha_ra_dy, alpha_dec_dy = self.derivatives(x, y + diff, sigma0, Rs, e1, e2, center_x, center_y) f_xx = (alpha_ra_dx - alpha_ra) / diff f_xy = (alpha_ra_dy - alpha_ra) / diff #f_yx = (alpha_dec_dx - alpha_dec)/diff f_yy = (alpha_dec_dy - alpha_dec) / diff return f_xx, f_yy, f_xy def density(self, r, rho0, Rs, e1=0, e2=0): """ computes the 3-d density :param r: 3-d radius :param rho0: density normalization :param Rs: Hernquist radius :return: density at radius r """ return self.spherical.density(r, rho0, Rs) def density_lens(self, r, sigma0, Rs, e1=0, e2=0): """ Density as a function of 3d radius in lensing parameters This function converts the lensing definition sigma0 into the 3d density :param r: 3d radius :param sigma0: rho0 * Rs (units of projected density) :param Rs: Hernquist radius :return: enclosed mass in 3d """ return self.spherical.density_lens(r, sigma0, Rs) def density_2d(self, x, y, rho0, Rs, e1=0, e2=0, center_x=0, center_y=0): """ projected density along the line of sight at coordinate (x, y) :param x: x-coordinate :param y: y-coordinate :param rho0: density normalization :param Rs: Hernquist radius :param center_x: x-center of the profile :param center_y: y-center of the profile :return: projected density """ return self.spherical.density_2d(x, y, rho0, Rs, center_x, center_y) def mass_2d_lens(self, r, sigma0, Rs, e1=0, e2=0): """ mass enclosed projected 2d sphere of radius r Same as mass_2d but with input normalization in units of projected density :param r: projected radius :param sigma0: rho0 * Rs (units of projected density) :param Rs: Hernquist radius :return: mass enclosed 2d projected radius """ return self.spherical.mass_2d_lens(r, sigma0, Rs) def mass_2d(self, r, rho0, Rs, e1=0, e2=0): """ mass enclosed projected 2d sphere of radius r :param r: projected radius :param rho0: density normalization :param Rs: Hernquist radius :return: mass enclosed 2d projected radius """ return self.spherical.mass_2d(r, rho0, Rs) def mass_3d(self, r, rho0, Rs, e1=0, e2=0): """ mass enclosed a 3d sphere or radius r :param r: 3-d radius within the mass is integrated (same distance units as density definition) :param rho0: density normalization :param Rs: Hernquist radius :return: enclosed mass """ return self.spherical.mass_3d(r, rho0, Rs)
class Hernquist_Ellipse(object): """ this class contains functions concerning the NFW profile relation are: R_200 = c * Rs """ param_names = ['sigma0', 'Rs', 'e1', 'e2', 'center_x', 'center_y'] lower_limit_default = { 'sigma0': 0, 'Rs': 0, 'e1': -0.5, 'e2': -0.5, 'center_x': -100, 'center_y': -100 } upper_limit_default = { 'sigma0': 100, 'Rs': 100, 'e1': 0.5, 'e2': 0.5, 'center_x': 100, 'center_y': 100 } def __init__(self): self.spherical = Hernquist() self._diff = 0.0000000001 def function(self, x, y, sigma0, Rs, e1, e2, center_x=0, center_y=0): """ returns double integral of NFW profile """ phi_G, q = param_util.ellipticity2phi_q(e1, e2) x_shift = x - center_x y_shift = y - center_y cos_phi = np.cos(phi_G) sin_phi = np.sin(phi_G) e = abs(1 - q) x_ = (cos_phi * x_shift + sin_phi * y_shift) * np.sqrt(1 - e) y_ = (-sin_phi * x_shift + cos_phi * y_shift) * np.sqrt(1 + e) f_ = self.spherical.function(x_, y_, sigma0, Rs) return f_ def derivatives(self, x, y, sigma0, Rs, e1, e2, center_x=0, center_y=0): """ returns df/dx and df/dy of the function (integral of NFW) """ phi_G, q = param_util.ellipticity2phi_q(e1, e2) x_shift = x - center_x y_shift = y - center_y cos_phi = np.cos(phi_G) sin_phi = np.sin(phi_G) e = abs(1 - q) x_ = (cos_phi * x_shift + sin_phi * y_shift) * np.sqrt(1 - e) y_ = (-sin_phi * x_shift + cos_phi * y_shift) * np.sqrt(1 + e) f_x_prim, f_y_prim = self.spherical.derivatives(x_, y_, sigma0, Rs) f_x_prim *= np.sqrt(1 - e) f_y_prim *= np.sqrt(1 + e) f_x = cos_phi * f_x_prim - sin_phi * f_y_prim f_y = sin_phi * f_x_prim + cos_phi * f_y_prim return f_x, f_y def hessian(self, x, y, sigma0, Rs, e1, e2, center_x=0, center_y=0): """ returns Hessian matrix of function d^2f/dx^2, d^f/dy^2, d^2/dxdy """ alpha_ra, alpha_dec = self.derivatives(x, y, sigma0, Rs, e1, e2, center_x, center_y) diff = self._diff alpha_ra_dx, alpha_dec_dx = self.derivatives(x + diff, y, sigma0, Rs, e1, e2, center_x, center_y) alpha_ra_dy, alpha_dec_dy = self.derivatives(x, y + diff, sigma0, Rs, e1, e2, center_x, center_y) f_xx = (alpha_ra_dx - alpha_ra) / diff f_xy = (alpha_ra_dy - alpha_ra) / diff #f_yx = (alpha_dec_dx - alpha_dec)/diff f_yy = (alpha_dec_dy - alpha_dec) / diff return f_xx, f_yy, f_xy