Ejemplo n.º 1
0
class TestcoreTNFWDeflection(object):

    def setup(self):

        self.interp = CoreTNFWDeflection()
        self.tnfw = TNFW()

    def test_deflection_point(self):

        x = 0.
        y = 0.
        Rs = 0.1
        r_core = 0.04
        r_trunc = 0.5
        norm = 1.

        alpha_origin = self.interp(x, y, Rs, r_core, r_trunc, norm)
        npt.assert_almost_equal(alpha_origin, 0.)

        x = Rs/np.sqrt(2)
        y = Rs/np.sqrt(2)
        alpha_Rs = 10.
        alpha_tnfw,_ = self.tnfw.derivatives(Rs, 0., Rs, alpha_Rs, r_trunc)

        alpha_rs_x, alpha_rs_y = self.interp(x, y, Rs, r_core, r_trunc, norm=1.)
        alpha_rs_interp_x, alpha_rs_interp_y = self.interp(x, y, Rs, r_core,
                                                           r_trunc, norm=alpha_tnfw)

        npt.assert_almost_equal(alpha_rs_interp_x / alpha_rs_x, alpha_tnfw)
        npt.assert_almost_equal(alpha_rs_interp_y / alpha_rs_y, alpha_tnfw)

    def test_deflection_array(self):

        x = np.array([0., 0.])
        y = 0.
        Rs = 0.1
        r_core = 0.04
        r_trunc = 0.5
        norm = 1.

        alpha_origin_x, alpha_origin_y = self.interp(x, y, Rs, r_core, r_trunc, norm)
        npt.assert_almost_equal(alpha_origin_y, 0.)
        npt.assert_almost_equal(alpha_origin_x, 0.)

        x = np.array([Rs / np.sqrt(2), Rs/np.sqrt(2)])
        y = np.array([Rs / np.sqrt(2), Rs/np.sqrt(2)])
        alpha_Rs = 10.
        alpha_tnfw, _ = self.tnfw.derivatives(Rs, 0., Rs, alpha_Rs, r_trunc)

        alpha_rs_interp_x, alpha_rs_interp_y = self.interp(x, y, Rs, r_core, r_trunc, norm=1.)
        alpha_rs = self.interp(x, y, Rs, r_core, r_trunc, norm=alpha_tnfw)
        npt.assert_almost_equal(alpha_rs / alpha_rs_interp_x, alpha_tnfw)
        npt.assert_almost_equal(alpha_rs / alpha_rs_interp_y, alpha_tnfw)
Ejemplo n.º 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(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)

        Rs = 0.2
        alpha_Rs = 0.1
        r_trunc = 1000000000000 * Rs

        x = np.linspace(0.1, 0.7, 100)

        pot1 = self.tnfw.function(x, 0, Rs, alpha_Rs, r_trunc)
        pot_nfw1 = self.nfw.function(x, 0, Rs, alpha_Rs)
        npt.assert_almost_equal(pot1, pot_nfw1, 5)

    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)
Ejemplo n.º 3
0
    def _deflection_function(self, x, y, rs, r_core, r_trunc, norm):

        tnfw = TNFW()

        return tnfw.derivatives(x, y, rs, norm, r_trunc)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
class TestNFWELLIPSE(object):
    """
    tests the Gaussian methods
    """
    def setup(self):
        self.tnfw = TNFW()
        self.nfw_e = NFW_ELLIPSE()
        self.tnfw_e = TNFW_ELLIPSE()

    def test_function(self):
        x = np.linspace(start=0.1, stop=10, num=10)
        y = np.linspace(start=0.1, stop=10, num=10)
        # test round case against TNFW
        kwargs_tnfw_e_round = {
            'Rs': 1,
            'alpha_Rs': 0.1,
            'r_trunc': 5,
            'e1': 0.,
            'e2': 0
        }
        kwargs_tnfw_round = {'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 5}
        f_e = self.tnfw_e.function(x, y, **kwargs_tnfw_e_round)
        f_r = self.tnfw.function(x, y, **kwargs_tnfw_round)
        npt.assert_almost_equal(f_e, f_r, decimal=5)

        # test elliptical case with r_trunc -> infinity against NFW_ELLIPSE
        kwargs_tnfw_e = {
            'Rs': 1,
            'alpha_Rs': 0.1,
            'r_trunc': 500,
            'e1': 0.2,
            'e2': -0.01
        }
        kwargs_nfw_e = {'Rs': 1, 'alpha_Rs': 0.1, 'e1': 0.2, 'e2': -0.01}
        f_te = self.tnfw_e.function(x, y, **kwargs_tnfw_e)
        f_e = self.nfw_e.function(x, y, **kwargs_nfw_e)
        npt.assert_almost_equal(f_te, f_e, decimal=3)

    def test_derivatives(self):
        x = np.linspace(start=0.1, stop=10, num=10)
        y = np.linspace(start=0.1, stop=10, num=10)
        # test round case against TNFW
        kwargs_tnfw_e_round = {
            'Rs': 1,
            'alpha_Rs': 0.1,
            'r_trunc': 5,
            'e1': 0.,
            'e2': 0
        }
        kwargs_tnfw_round = {'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 5}
        f_xe, f_ye = self.tnfw_e.derivatives(x, y, **kwargs_tnfw_e_round)
        f_xr, f_yr = self.tnfw.derivatives(x, y, **kwargs_tnfw_round)
        npt.assert_almost_equal(f_xe, f_xr, decimal=5)
        npt.assert_almost_equal(f_ye, f_yr, decimal=5)

        # test elliptical case with r_trunc -> infinity against NFW_ELLIPSE
        kwargs_tnfw_e = {
            'Rs': 1,
            'alpha_Rs': 0.1,
            'r_trunc': 500,
            'e1': 0.2,
            'e2': -0.01
        }
        kwargs_nfw_e = {'Rs': 1, 'alpha_Rs': 0.1, 'e1': 0.2, 'e2': -0.01}
        out_te = self.tnfw_e.derivatives(x, y, **kwargs_tnfw_e)
        out_e = self.nfw_e.derivatives(x, y, **kwargs_nfw_e)
        npt.assert_almost_equal(out_te, out_e, decimal=3)

    def test_hessian(self):
        x = np.linspace(start=0.1, stop=10, num=10)
        y = np.linspace(start=0.1, stop=10, num=10)
        # test round case against TNFW
        kwargs_tnfw_e_round = {
            'Rs': 1,
            'alpha_Rs': 0.1,
            'r_trunc': 5,
            'e1': 0.,
            'e2': 0
        }
        kwargs_tnfw_round = {'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 5}
        out_e = self.tnfw_e.hessian(x, y, **kwargs_tnfw_e_round)
        out_r = self.tnfw.hessian(x, y, **kwargs_tnfw_round)
        npt.assert_almost_equal(out_e, out_r, decimal=4)

        # test elliptical case with r_trunc -> infinity against NFW_ELLIPSE
        kwargs_tnfw_e = {
            'Rs': 1,
            'alpha_Rs': 0.1,
            'r_trunc': 500,
            'e1': 0.2,
            'e2': -0.01
        }
        kwargs_nfw_e = {'Rs': 1, 'alpha_Rs': 0.1, 'e1': 0.2, 'e2': -0.01}
        out_te = self.tnfw_e.hessian(x, y, **kwargs_tnfw_e)
        out_e = self.nfw_e.hessian(x, y, **kwargs_nfw_e)
        npt.assert_almost_equal(out_te, out_e, decimal=3)

    def test_mass_3d_lens(self):
        with npt.assert_raises(ValueError):
            kwargs_tnfw_e = {
                'Rs': 1,
                'alpha_Rs': 0.1,
                'r_trunc': 5,
                'e1': 0.1,
                'e2': -0.02
            }
            self.tnfw_e.mass_3d_lens(1, **kwargs_tnfw_e)

    def test_density_lens(self):
        with npt.assert_raises(ValueError):
            kwargs_tnfw_e = {
                'Rs': 1,
                'alpha_Rs': 0.1,
                'r_trunc': 5,
                'e1': 0.1,
                'e2': -0.02
            }
            self.tnfw_e.density_lens(1, **kwargs_tnfw_e)