예제 #1
0
class TNFW_ELLIPSE(LensProfileBase):
    """
    this class contains functions concerning the truncated NFW profile with an ellipticity defined in the potential
    parameterization of alpha_Rs, Rs and r_trunc is the same as for the spherical NFW profile

    from Glose & Kneib: https://cds.cern.ch/record/529584/files/0112138.pdf

    relation are: R_200 = c * Rs
    """
    profile_name = 'TNFW_ELLIPSE'
    param_names = [
        'Rs', 'alpha_Rs', 'r_trunc', 'e1', 'e2', 'center_x', 'center_y'
    ]
    lower_limit_default = {
        'Rs': 0,
        'alpha_Rs': 0,
        'r_trunc': 0,
        'e1': -0.5,
        'e2': -0.5,
        'center_x': -100,
        'center_y': -100
    }
    upper_limit_default = {
        'Rs': 100,
        'alpha_Rs': 10,
        'r_trunc': 100,
        'e1': 0.5,
        'e2': 0.5,
        'center_x': 100,
        'center_y': 100
    }

    def __init__(self):
        """

        """
        self.tnfw = TNFW()
        self._diff = 0.0000000001
        super(TNFW_ELLIPSE, self).__init__()

    def function(self,
                 x,
                 y,
                 Rs,
                 alpha_Rs,
                 r_trunc,
                 e1,
                 e2,
                 center_x=0,
                 center_y=0):
        """
        returns elliptically distorted NFW lensing potential

        :param x: angular position (normally in units of arc seconds)
        :param y: angular position (normally in units of arc seconds)
        :param Rs: turn over point in the slope of the NFW profile in angular unit
        :param alpha_Rs: deflection (angular units) at projected Rs
        :param r_trunc: truncation radius
        :param e1: eccentricity component in x-direction
        :param e2: eccentricity component in y-direction
        :param center_x: center of halo (in angular units)
        :param center_y: center of halo (in angular units)
        :return: lensing potential
        """
        x_, y_ = param_util.transform_e1e2_square_average(
            x, y, e1, e2, center_x, center_y)
        R_ = np.sqrt(x_**2 + y_**2)
        rho0_input = self.tnfw.alpha2rho0(alpha_Rs=alpha_Rs, Rs=Rs)
        Rs = np.maximum(Rs, 0.0000001)
        #if Rs < 0.0000001:
        #    Rs = 0.0000001
        f_ = self.tnfw.nfwPot(R_, Rs, rho0_input, r_trunc)
        return f_

    def derivatives(self,
                    x,
                    y,
                    Rs,
                    alpha_Rs,
                    r_trunc,
                    e1,
                    e2,
                    center_x=0,
                    center_y=0):
        """
        returns df/dx and df/dy of the function, calculated as an elliptically distorted deflection angle of the
        spherical NFW profile

        :param x: angular position (normally in units of arc seconds)
        :param y: angular position (normally in units of arc seconds)
        :param Rs: turn over point in the slope of the NFW profile in angular unit
        :param alpha_Rs: deflection (angular units) at projected Rs
        :param r_trunc: truncation radius
        :param e1: eccentricity component in x-direction
        :param e2: eccentricity component in y-direction
        :param center_x: center of halo (in angular units)
        :param center_y: center of halo (in angular units)
        :return: deflection in x-direction, deflection in y-direction
        """
        x_, y_ = param_util.transform_e1e2_square_average(
            x, y, e1, e2, center_x, center_y)
        phi_G, q = param_util.ellipticity2phi_q(e1, e2)
        cos_phi = np.cos(phi_G)
        sin_phi = np.sin(phi_G)
        e = param_util.q2e(q)
        # e = abs(1 - q)
        R_ = np.sqrt(x_**2 + y_**2)
        rho0_input = self.tnfw.alpha2rho0(alpha_Rs=alpha_Rs, Rs=Rs)
        Rs = np.maximum(Rs, 0.0000001)
        #if Rs < 0.0000001:
        #    Rs = 0.0000001
        f_x_prim, f_y_prim = self.tnfw.nfwAlpha(R_, Rs, rho0_input, r_trunc,
                                                x_, y_)
        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,
                Rs,
                alpha_Rs,
                r_trunc,
                e1,
                e2,
                center_x=0,
                center_y=0):
        """
        returns Hessian matrix of function d^2f/dx^2, d^f/dy^2, d^2/dxdy
        the calculation is performed as a numerical differential from the deflection field. Analytical relations are possible

        :param x: angular position (normally in units of arc seconds)
        :param y: angular position (normally in units of arc seconds)
        :param Rs: turn over point in the slope of the NFW profile in angular unit
        :param alpha_Rs: deflection (angular units) at projected Rs
        :param r_trunc: truncation radius
        :param e1: eccentricity component in x-direction
        :param e2: eccentricity component in y-direction
        :param center_x: center of halo (in angular units)
        :param center_y: center of halo (in angular units)
        :return: d^2f/dx^2, d^2/dxdy, d^2/dydx, d^f/dy^2
        """
        alpha_ra, alpha_dec = self.derivatives(x, y, Rs, alpha_Rs, r_trunc, e1,
                                               e2, center_x, center_y)
        diff = self._diff
        alpha_ra_dx, alpha_dec_dx = self.derivatives(x + diff, y, Rs, alpha_Rs,
                                                     r_trunc, e1, e2, center_x,
                                                     center_y)
        alpha_ra_dy, alpha_dec_dy = self.derivatives(x, y + diff, Rs, alpha_Rs,
                                                     r_trunc, 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_xy, f_yx, f_yy

    def mass_3d_lens(self, r, Rs, alpha_Rs, r_trunc, e1=1, e2=0):
        """

        :param r: radius (in angular units)
        :param Rs: turn-over radius of NFW profile
        :param alpha_Rs: deflection at Rs
        :param r_trunc: truncation radius
        :param e1: eccentricity component in x-direction
        :param e2: eccentricity component in y-direction
        :return:
        """
        return self.tnfw.mass_3d_lens(r, Rs, alpha_Rs, r_trunc)

    def density_lens(self, r, Rs, alpha_Rs, r_trunc, e1=1, e2=0):
        """
        computes the density at 3d radius r given lens model parameterization.
        The integral in the LOS projection of this quantity results in the convergence quantity.

        :param r: 3d radios
        :param Rs: turn-over radius of NFW profile
        :param alpha_Rs: deflection at Rs
        :param r_trunc: truncation radius
        :param e1: eccentricity component in x-direction
        :param e2: eccentricity component in y-direction
        :return: density rho(r)
        """
        return self.tnfw.density_lens(r, Rs, alpha_Rs, r_trunc)
예제 #2
0
class TestTNFW(object):
    def setup(self):
        self.nfw = NFW()
        self.tnfw = TNFW()

    def test_deflection(self):
        Rs = 0.2
        alpha_Rs = 0.1
        r_trunc = 1000000000000 * Rs
        x = np.linspace(0.0 * Rs, 5 * Rs, 1000)
        y = np.linspace(0., 1, 1000)

        xdef_t, ydef_t = self.tnfw.derivatives(x, y, Rs, alpha_Rs, r_trunc)
        xdef, ydef = self.nfw.derivatives(x, y, Rs, alpha_Rs)

        np.testing.assert_almost_equal(xdef_t, xdef, 5)
        np.testing.assert_almost_equal(ydef_t, ydef, 5)

    def test_potential_limit(self):

        Rs = 0.2
        alpha_Rs = 0.1
        r_trunc = 1000000000000 * Rs
        x = np.linspace(0.1 * Rs, 5 * Rs, 1000)
        y = np.linspace(0.2, 1, 1000)

        pot_t = self.tnfw.function(x, y, Rs, alpha_Rs, r_trunc)
        pot = self.nfw.function(x, y, Rs, alpha_Rs)

        np.testing.assert_almost_equal(pot, pot_t, 4)

    def test_potential_derivative(self):

        Rs = 1.2
        alpha_Rs = 1
        r_trunc = 3 * Rs
        R = np.linspace(0.5 * Rs, 2.2 * Rs, 5000)
        dx = R[1] - R[0]

        alpha_tnfw = self.tnfw.nfwAlpha(R, Rs, 1, r_trunc, R, 0)[0]

        potential_array = self.tnfw.nfwPot(R, Rs, 1, r_trunc)
        alpha_tnfw_num_array = np.gradient(potential_array, dx)

        potential_from_float = [
            self.tnfw.nfwPot(R_i, Rs, 1, r_trunc) for R_i in R
        ]
        alpha_tnfw_num_from_float = np.gradient(potential_from_float, dx)

        npt.assert_almost_equal(alpha_tnfw_num_array, alpha_tnfw, 4)
        npt.assert_almost_equal(alpha_tnfw_num_from_float, alpha_tnfw, 4)

    def test_gamma(self):
        Rs = 0.2
        alpha_Rs = 0.1
        r_trunc = 1000000000000 * Rs
        x = np.linspace(0.1 * Rs, 5 * Rs, 1000)
        y = np.linspace(0.2, 1, 1000)

        g1t, g2t = self.tnfw.nfwGamma((x**2 + y**2)**.5, Rs, alpha_Rs, r_trunc,
                                      x, y)
        g1, g2 = self.nfw.nfwGamma((x**2 + y**2)**.5, Rs, alpha_Rs, x, y)

        np.testing.assert_almost_equal(g1t, g1, 5)
        np.testing.assert_almost_equal(g2t, g2, 5)

    def test_hessian(self):
        Rs = 0.2
        alpha_Rs = 0.1
        r_trunc = 1000000000000 * Rs
        x = np.linspace(0.1 * Rs, 5 * Rs, 100)
        y = np.linspace(0.2, 1, 100)

        xxt, yyt, xyt = self.tnfw.hessian(x, y, Rs, alpha_Rs, r_trunc)
        xx, yy, xy = self.nfw.hessian(x, y, Rs, alpha_Rs)

        np.testing.assert_almost_equal(xy, xyt, 4)
        np.testing.assert_almost_equal(yy, yyt, 4)
        np.testing.assert_almost_equal(xy, xyt, 4)

        Rs = 0.2
        r_trunc = 5
        xxt, yyt, xyt = self.tnfw.hessian(Rs, 0, Rs, alpha_Rs, r_trunc)
        xxt_delta, yyt_delta, xyt_delta = self.tnfw.hessian(
            Rs + 0.000001, 0, Rs, alpha_Rs, r_trunc)
        npt.assert_almost_equal(xxt, xxt_delta, decimal=6)

    def test_density_2d(self):
        Rs = 0.2
        alpha_Rs = 0.1
        r_trunc = 1000000000000 * Rs
        x = np.linspace(0.1 * Rs, 3 * Rs, 1000)
        y = np.linspace(0.2, 0.5, 1000)

        kappa_t = self.tnfw.density_2d(x, y, Rs, alpha_Rs, r_trunc)
        kappa = self.nfw.density_2d(x, y, Rs, alpha_Rs)
        np.testing.assert_almost_equal(kappa, kappa_t, 5)

    def test_transform(self):

        rho0, Rs = 1, 2

        trs = self.tnfw._rho02alpha(rho0, Rs)
        rho_out = self.tnfw._alpha2rho0(trs, Rs)

        npt.assert_almost_equal(rho0, rho_out)

    def test_numerical_derivatives(self):

        Rs = 0.2
        alpha_Rs = 0.1
        r_trunc = 1.5 * Rs

        diff = 1e-9

        x0, y0 = 0.1, 0.1

        x_def_t, y_def_t = self.tnfw.derivatives(x0, y0, Rs, alpha_Rs, r_trunc)
        x_def_t_deltax, _ = self.tnfw.derivatives(x0 + diff, y0, Rs, alpha_Rs,
                                                  r_trunc)
        x_def_t_deltay, y_def_t_deltay = self.tnfw.derivatives(
            x0, y0 + diff, Rs, alpha_Rs, r_trunc)
        actual = self.tnfw.hessian(x0, y0, Rs, alpha_Rs, r_trunc)

        f_xx_approx = (x_def_t_deltax - x_def_t) * diff**-1
        f_yy_approx = (y_def_t_deltay - y_def_t) * diff**-1
        f_xy_approx = (x_def_t_deltay - y_def_t) * diff**-1
        numerical = [f_xx_approx, f_yy_approx, f_xy_approx]

        for (approx, true) in zip(numerical, actual):
            npt.assert_almost_equal(approx, true)

    def test_F_function_at_one(self):

        f_tnfw = self.tnfw.F(1.)
        npt.assert_(f_tnfw == 1)
        f_tnfw = self.tnfw.F(np.ones((2, 2)))
        f_tnfw = f_tnfw.ravel()
        for value in f_tnfw:
            npt.assert_(value == 1)