예제 #1
0
    def test_interpol(self):
        Rs = 3
        alpha_Rs = 1
        x = np.array([2, 3, 4])
        y = np.array([1, 1, 1])

        nfw = NFW(interpol=False)
        nfw_interp = NFW(interpol=True)
        nfw_interp_lookup = NFW(interpol=True, lookup=True)

        values = nfw.function(x, y, Rs, alpha_Rs)
        values_interp = nfw_interp.function(x, y, Rs, alpha_Rs)
        values_interp_lookup = nfw_interp_lookup.function(x, y, Rs, alpha_Rs)
        npt.assert_almost_equal(values, values_interp, decimal=4)
        npt.assert_almost_equal(values, values_interp_lookup, decimal=4)

        values = nfw.derivatives(x, y, Rs, alpha_Rs)
        values_interp = nfw_interp.derivatives(x, y, Rs, alpha_Rs)
        values_interp_lookup = nfw_interp_lookup.derivatives(x, y, Rs, alpha_Rs)
        npt.assert_almost_equal(values, values_interp, decimal=4)
        npt.assert_almost_equal(values, values_interp_lookup, decimal=4)

        values = nfw.hessian(x, y, Rs, alpha_Rs)
        values_interp = nfw_interp.hessian(x, y, Rs, alpha_Rs)
        values_interp_lookup = nfw_interp_lookup.hessian(x, y, Rs, alpha_Rs)
        npt.assert_almost_equal(values, values_interp, decimal=4)
        npt.assert_almost_equal(values, values_interp_lookup, decimal=4)
    def test_nfw_sersic(self):
        kwargs_lens_nfw = {'alpha_Rs': 1.4129647849966354, 'Rs': 7.0991113634274736}
        kwargs_lens_sersic = {'k_eff': 0.24100561407593576, 'n_sersic': 1.8058507329346063, 'R_sersic': 1.0371803141813705}
        from lenstronomy.LensModel.Profiles.nfw import NFW
        from lenstronomy.LensModel.Profiles.sersic import Sersic
        nfw = NFW()
        sersic = Sersic()
        theta_E = 1.5
        n_comp = 10
        rs = np.logspace(-2., 1., 100) * theta_E
        f_xx_nfw, f_xy_nfw, f_yx_nfw, f_yy_nfw = nfw.hessian(rs, 0, **kwargs_lens_nfw)
        f_xx_s, f_xy_s, f_yx_s, f_yy_s = sersic.hessian(rs, 0, **kwargs_lens_sersic)
        kappa = 1 / 2. * (f_xx_nfw + f_xx_s + f_yy_nfw + f_yy_s)
        amplitudes, sigmas, norm = mge.mge_1d(rs, kappa, N=n_comp)
        kappa_mge = self.multiGaussian.function(rs, np.zeros_like(rs), amp=amplitudes, sigma=sigmas)
        from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappa
        mge_kappa = MultiGaussianKappa()
        f_xx_mge, f_xy_mge, f_yx_mge, f_yy_mge = mge_kappa.hessian(rs, np.zeros_like(rs), amp=amplitudes, sigma=sigmas)
        for i in range(0, 80):
            npt.assert_almost_equal(kappa_mge[i], 1. / 2 * (f_xx_mge[i] + f_yy_mge[i]), decimal=1)
            npt.assert_almost_equal((kappa[i] - kappa_mge[i]) / kappa[i], 0, decimal=1)

        f_nfw = nfw.function(theta_E, 0, **kwargs_lens_nfw)
        f_s = sersic.function(theta_E, 0, **kwargs_lens_sersic)
        f_mge = mge_kappa.function(theta_E, 0, sigma=sigmas, amp=amplitudes)
        npt.assert_almost_equal(f_mge / (f_nfw + f_s), 1, decimal=2)
class TestNFWMC(object):
    """
    tests the Gaussian methods
    """
    def setup(self):
        self.z_lens, self.z_source = 0.5, 2
        from astropy.cosmology import FlatLambdaCDM
        cosmo = FlatLambdaCDM(H0=70, Om0=0.3, Ob0=0.05)
        self.nfw = NFW()
        self.nfwmc = NFWMC(z_source=self.z_source, z_lens=self.z_lens, cosmo=cosmo)
        self.lensCosmo = LensCosmo(z_lens=self.z_lens, z_source=self.z_source, cosmo=cosmo)

    def test_function(self):
        x, y = 1., 1.
        logM = 12
        concentration = 10
        f_mc = self.nfwmc.function(x, y, logM, concentration, center_x=0, center_y=0)
        Rs, alpha_Rs = self.lensCosmo.nfw_physical2angle(10**logM, concentration)
        f_ = self.nfw.function(x, y, Rs, alpha_Rs, center_x=0, center_y=0)
        npt.assert_almost_equal(f_mc, f_, decimal=8)

    def test_derivatives(self):
        x, y = 1., 1.
        logM = 12
        concentration = 10
        f_x_mc, f_y_mc = self.nfwmc.derivatives(x, y, logM, concentration, center_x=0, center_y=0)
        Rs, alpha_Rs = self.lensCosmo.nfw_physical2angle(10 ** logM, concentration)
        f_x, f_y = self.nfw.derivatives(x, y, Rs, alpha_Rs, center_x=0, center_y=0)
        npt.assert_almost_equal(f_x_mc, f_x, decimal=8)
        npt.assert_almost_equal(f_y_mc, f_y, decimal=8)

    def test_hessian(self):
        x, y = 1., 1.
        logM = 12
        concentration = 10
        f_xx_mc, f_xy_mc, f_yx_mc, f_yy_mc = self.nfwmc.hessian(x, y, logM, concentration, center_x=0, center_y=0)
        Rs, alpha_Rs = self.lensCosmo.nfw_physical2angle(10 ** logM, concentration)
        f_xx, f_xy, f_yx, f_yy = self.nfw.hessian(x, y, Rs, alpha_Rs, center_x=0, center_y=0)
        npt.assert_almost_equal(f_xx_mc, f_xx, decimal=8)
        npt.assert_almost_equal(f_yy_mc, f_yy, decimal=8)
        npt.assert_almost_equal(f_xy_mc, f_xy, decimal=8)
        npt.assert_almost_equal(f_yx_mc, f_yx, decimal=8)

    def test_static(self):
        x, y = 1., 1.
        logM = 12
        concentration = 10
        f_ = self.nfwmc.function(x, y, logM, concentration, center_x=0, center_y=0)
        self.nfwmc.set_static(logM, concentration)
        f_static = self.nfwmc.function(x, y, 0, 0, center_x=0, center_y=0)
        npt.assert_almost_equal(f_, f_static, decimal=8)
        self.nfwmc.set_dynamic()
        f_dyn = self.nfwmc.function(x, y, 11, 20, center_x=0, center_y=0)
        assert f_dyn != f_static
예제 #4
0
class TestNFW(object):
    """
    tests the Gaussian methods
    """
    def setup(self):
        self.nfw = NFW()
        self.nfwt = NFWt()

    def test_function(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        rho0 = 1
        theta_Rs = self.nfw._rho02alpha(rho0, Rs)
        f_ = self.nfw.function(x, y, Rs, theta_Rs)
        t = 10000
        f_t = self.nfwt.function(x, y, Rs, theta_Rs, t)
        #npt.assert_almost_equal(f[0], f_t[0], decimal=5)

    def test_derivatives(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        rho0 = 1
        theta_Rs = self.nfw._rho02alpha(rho0, Rs)
        f_x, f_y = self.nfw.derivatives(x, y, Rs, theta_Rs)
        t = 10000
        f_xt, f_yt = self.nfwt.derivatives(x, y, Rs, theta_Rs, t)
        #npt.assert_almost_equal(f_xt[0], f_x[0], decimal=5)
        #npt.assert_almost_equal(f_yt[0], f_y[0], decimal=5)

    def test_hessian(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        rho0 = 1
        t = 10000
        theta_Rs = self.nfw._rho02alpha(rho0, Rs)
        f_xx, f_yy, f_xy = self.nfw.hessian(x, y, Rs, theta_Rs)
        f_xxt, f_yyt, f_xyt = self.nfwt.hessian(x, y, Rs, theta_Rs, t)
예제 #5
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)
예제 #6
0
class NFWMC(LensProfileBase):
    """
    this class contains functions parameterises the NFW profile with log10 M200 and the concentration rs/r200
    relation are: R_200 = c * Rs

    ATTENTION: the parameterization is cosmology and redshift dependent!
    The cosmology to connect mass and deflection relations is fixed to default H0=70km/s Omega_m=0.3 flat LCDM.
    It is recommended to keep a given cosmology definition in the lens modeling as the observable reduced deflection
    angles are sensitive in this parameterization. If you do not want to impose a mass-concentration relation, it is
    recommended to use the default NFW lensing profile parameterized in reduced deflection angles.

    """
    param_names = ['logM', 'concentration', 'center_x', 'center_y']
    lower_limit_default = {
        'logM': 0,
        'concentration': 0.01,
        'center_x': -100,
        'center_y': -100
    }
    upper_limit_default = {
        'logM': 16,
        'concentration': 1000,
        'center_x': 100,
        'center_y': 100
    }

    def __init__(self, z_lens, z_source, cosmo=None, static=False):
        """

        :param z_lens: redshift of lens
        :param z_source: redshift of source
        :param cosmo: astropy cosmology instance
        :param static: boolean, if True, only operates with fixed parameter values
        """
        self._nfw = NFW()
        if cosmo is None:
            from astropy.cosmology import FlatLambdaCDM
            cosmo = FlatLambdaCDM(H0=70, Om0=0.3, Ob0=0.05)
        self._lens_cosmo = LensCosmo(z_lens, z_source, cosmo=cosmo)
        self._static = static
        super(NFWMC, self).__init__()

    def _m_c2deflections(self, logM, concentration):
        """

        :param logM: log10 mass in M200 stellar masses
        :param concentration: halo concentration c = r_200 / r_s
        :return: Rs (in arc seconds), alpha_Rs (in arc seconds)
        """
        if self._static is True:
            return self._Rs_static, self._alpha_Rs_static
        M = 10**logM
        Rs, alpha_Rs = self._lens_cosmo.nfw_physical2angle(M, concentration)
        return Rs, alpha_Rs

    def set_static(self, logM, concentration, center_x=0, center_y=0):
        """

        :param logM:
        :param concentration:
        :param center_x:
        :param center_y:
        :return:
        """
        self._static = True
        M = 10**logM
        self._Rs_static, self._alpha_Rs_static = self._lens_cosmo.nfw_physical2angle(
            M, concentration)

    def set_dynamic(self):
        """

        :return:
        """
        self._static = False
        if hasattr(self, '_Rs_static'):
            del self._Rs_static
        if hasattr(self, '_alpha_Rs_static'):
            del self._alpha_Rs_static

    def function(self, x, y, logM, concentration, center_x=0, center_y=0):
        """

        :param x: angular position
        :param y: angular position
        :param Rs: angular turn over point
        :param alpha_Rs: deflection at Rs
        :param center_x: center of halo
        :param center_y: center of halo
        :return:
        """
        Rs, alpha_Rs = self._m_c2deflections(logM, concentration)
        return self._nfw.function(x,
                                  y,
                                  alpha_Rs=alpha_Rs,
                                  Rs=Rs,
                                  center_x=center_x,
                                  center_y=center_y)

    def derivatives(self, x, y, logM, concentration, center_x=0, center_y=0):
        """
        returns df/dx and df/dy of the function (integral of NFW)
        """
        Rs, alpha_Rs = self._m_c2deflections(logM, concentration)
        return self._nfw.derivatives(x, y, Rs, alpha_Rs, center_x, center_y)

    def hessian(self, x, y, logM, concentration, center_x=0, center_y=0):
        """
        returns Hessian matrix of function d^2f/dx^2, d^f/dy^2, d^2/dxdy
        """
        Rs, alpha_Rs = self._m_c2deflections(logM, concentration)
        return self._nfw.hessian(x, y, Rs, alpha_Rs, center_x, center_y)
예제 #7
0
class TestNFWELLIPSE(object):
    """
    tests the Gaussian methods
    """
    def setup(self):
        self.nfw = NFW()
        self.nfw_e = NFW_ELLIPSE()

    def test_function(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        theta_Rs = 1.
        q = 1.
        phi_G = 0
        values = self.nfw.function(x, y, Rs, theta_Rs)
        values_e = self.nfw_e.function(x, y, Rs, theta_Rs, q, phi_G)
        npt.assert_almost_equal(values[0], values_e[0], decimal=5)
        x = np.array([0])
        y = np.array([0])

        q = .8
        phi_G = 0
        values = self.nfw_e.function(x, y, Rs, theta_Rs, q, phi_G)
        npt.assert_almost_equal(values[0], 0, decimal=4)

        x = np.array([2, 3, 4])
        y = np.array([1, 1, 1])
        values = self.nfw_e.function(x, y, Rs, theta_Rs, q, phi_G)
        npt.assert_almost_equal(values[0], 1.8827504143588476, decimal=5)
        npt.assert_almost_equal(values[1], 2.6436373117941852, decimal=5)
        npt.assert_almost_equal(values[2], 3.394127018818891, decimal=5)

    def test_derivatives(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        theta_Rs = 1.
        q = 1.
        phi_G = 0
        f_x, f_y = self.nfw.derivatives(x, y, Rs, theta_Rs)
        f_x_e, f_y_e = self.nfw_e.derivatives(x, y, Rs, theta_Rs, q, phi_G)
        npt.assert_almost_equal(f_x[0], f_x_e[0], decimal=5)
        npt.assert_almost_equal(f_y[0], f_y_e[0], decimal=5)
        x = np.array([0])
        y = np.array([0])
        theta_Rs = 0
        f_x, f_y = self.nfw_e.derivatives(x, y, Rs, theta_Rs, q, phi_G)
        npt.assert_almost_equal(f_x[0], 0, decimal=5)
        npt.assert_almost_equal(f_y[0], 0, decimal=5)

        x = np.array([1, 3, 4])
        y = np.array([2, 1, 1])
        theta_Rs = 1.
        q = .8
        phi_G = 0
        values = self.nfw_e.derivatives(x, y, Rs, theta_Rs, q, phi_G)
        npt.assert_almost_equal(values[0][0], 0.32458737284934414, decimal=5)
        npt.assert_almost_equal(values[1][0], 0.9737621185480323, decimal=5)
        npt.assert_almost_equal(values[0][1], 0.76249351329615234, decimal=5)
        npt.assert_almost_equal(values[1][1], 0.38124675664807617, decimal=5)

    def test_hessian(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        theta_Rs = 1.
        q = 1.
        phi_G = 0
        f_xx, f_yy, f_xy = self.nfw.hessian(x, y, Rs, theta_Rs)
        f_xx_e, f_yy_e, f_xy_e = self.nfw_e.hessian(x, y, Rs, theta_Rs, q,
                                                    phi_G)
        npt.assert_almost_equal(f_xx[0], f_xx_e[0], decimal=5)
        npt.assert_almost_equal(f_yy[0], f_yy_e[0], decimal=5)
        npt.assert_almost_equal(f_xy[0], f_xy_e[0], decimal=5)

        x = np.array([1, 3, 4])
        y = np.array([2, 1, 1])
        q = .8
        phi_G = 0
        values = self.nfw_e.hessian(x, y, Rs, theta_Rs, q, phi_G)
        npt.assert_almost_equal(values[0][0], 0.26998576668768592, decimal=5)
        npt.assert_almost_equal(values[1][0],
                                -0.0045328224507201753,
                                decimal=5)
        npt.assert_almost_equal(values[2][0], -0.16380454531672584, decimal=5)
        npt.assert_almost_equal(values[0][1], -0.014833136829928151, decimal=5)
        npt.assert_almost_equal(values[1][1], 0.31399726446723619, decimal=5)
        npt.assert_almost_equal(values[2][1], -0.13449884961325154, decimal=5)
예제 #8
0
class TestNFW(object):
    """
    tests the Gaussian methods
    """
    def setup(self):
        self.nfw = NFW()

    def test_function(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        rho0 = 1
        alpha_Rs = self.nfw._rho02alpha(rho0, Rs)
        values = self.nfw.function(x, y, Rs, alpha_Rs)
        npt.assert_almost_equal(values[0], 2.4764530888727556, decimal=5)
        x = np.array([0])
        y = np.array([0])
        Rs = 1.
        rho0 = 1
        alpha_Rs = self.nfw._rho02alpha(rho0, Rs)
        values = self.nfw.function(x, y, Rs, alpha_Rs)
        npt.assert_almost_equal(values[0], 0, decimal=4)

        x = np.array([2,3,4])
        y = np.array([1,1,1])
        values = self.nfw.function(x, y, Rs, alpha_Rs)
        npt.assert_almost_equal(values[0], 2.4764530888727556, decimal=5)
        npt.assert_almost_equal(values[1], 3.5400250357511416, decimal=5)
        npt.assert_almost_equal(values[2], 4.5623722261790647, decimal=5)

    def test_derivatives(self):
        Rs = .1
        alpha_Rs = 0.0122741127776
        x_array = np.array([0.0, 0.00505050505,0.0101010101,0.0151515152,0.0202020202,0.0252525253,
            0.0303030303,0.0353535354,0.0404040404,0.0454545455,0.0505050505,0.0555555556,0.0606060606,0.0656565657,0.0707070707,0.0757575758,0.0808080808,0.0858585859,0.0909090909,0.095959596,0.101010101,0.106060606,
            0.111111111,0.116161616,0.121212121,0.126262626,0.131313131,0.136363636,0.141414141,0.146464646,0.151515152,0.156565657,
            0.161616162,0.166666667,0.171717172,0.176767677,0.181818182,0.186868687,0.191919192,0.196969697,0.202020202,0.207070707,0.212121212,0.217171717,0.222222222,0.227272727,0.232323232,0.237373737,0.242424242,0.247474747,0.252525253,0.257575758,0.262626263,0.267676768,0.272727273,0.277777778,0.282828283,
            0.287878788,0.292929293,0.297979798,0.303030303,0.308080808,0.313131313,0.318181818,0.323232323,0.328282828,0.333333333,0.338383838,0.343434343,0.348484848,
            0.353535354,0.358585859,0.363636364,0.368686869,0.373737374,0.378787879,0.383838384,0.388888889,0.393939394,0.398989899,0.404040404,0.409090909,
            0.414141414,0.419191919,0.424242424,0.429292929,0.434343434,0.439393939,0.444444444,0.449494949,0.454545455,0.45959596,0.464646465,0.46969697,0.474747475,0.47979798,0.484848485,0.48989899,0.494949495,0.5])
        truth_alpha = np.array([0.0, 0.00321693283, 0.00505903212,
            0.00640987376,0.00746125453,0.00830491158, 0.00899473755, 0.00956596353,0.0100431963,0.0104444157,0.0107831983,0.0110700554,0.0113132882,0.0115195584,0.0116942837,0.0118419208,
            0.011966171,0.0120701346,0.012156428,0.0122272735,0.0122845699,0.0123299487,0.0123648177,0.0123903978,0.0124077515,0.0124178072,0.0124213787,0.0124191816,0.0124118471,0.0123999334,0.0123839353,0.0123642924,0.0123413964,
            0.0123155966,0.0122872054,0.0122565027,0.0122237393,0.0121891409,0.0121529102,0.0121152302,0.0120762657,0.0120361656,0.0119950646,0.0119530846,0.0119103359,0.0118669186,0.0118229235,0.0117784329,0.0117335217,
            0.011688258,0.0116427037,0.0115969149,0.0115509429,0.0115048343,0.0114586314,0.0114123729,0.011366094,0.0113198264,0.0112735995,0.0112274395,0.0111813706,0.0111354147,
            0.0110895915,0.011043919,0.0109984136,0.01095309,0.0109079617,0.0108630406,0.0108183376,0.0107738625,0.010729624,0.01068563,0.0106418875,0.0105984026,0.0105551809,0.0105122271,0.0104695455,0.0104271398,0.010385013,0.0103431679,0.0103016067,0.0102603311,
            0.0102193428,0.0101786427,0.0101382318,0.0100981105,0.0100582792,0.0100187377,0.00997948602,0.00994052364,0.00990184999,
            0.00986346433, 0.00982536573,0.00978755314, 0.00975002537, 0.0097127811, 0.00967581893, 0.00963913734, 0.00960273473, 0.00956660941])
        y_array = np.zeros_like(x_array)
        f_x, f_y = self.nfw.derivatives(x_array, y_array, Rs, alpha_Rs)
        #print(f_x/truth_alpha)
        for i in range(len(x_array)):
            npt.assert_almost_equal(f_x[i], truth_alpha[i], decimal=8)

    def test_hessian(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        rho0 = 1
        alpha_Rs = self.nfw._rho02alpha(rho0, Rs)
        f_xx, f_yy,f_xy = self.nfw.hessian(x, y, Rs, alpha_Rs)
        npt.assert_almost_equal(f_xx[0], 0.40855527280658294, decimal=5)
        npt.assert_almost_equal(f_yy[0], 0.037870368296371637, decimal=5)
        npt.assert_almost_equal(f_xy[0], -0.2471232696734742, decimal=5)

        x = np.array([1,3,4])
        y = np.array([2,1,1])
        values = self.nfw.hessian(x, y, Rs, alpha_Rs)
        npt.assert_almost_equal(values[0][0], 0.40855527280658294, decimal=5)
        npt.assert_almost_equal(values[1][0], 0.037870368296371637, decimal=5)
        npt.assert_almost_equal(values[2][0], -0.2471232696734742, decimal=5)
        npt.assert_almost_equal(values[0][1], -0.046377502475445781, decimal=5)
        npt.assert_almost_equal(values[1][1], 0.30577812878681554, decimal=5)
        npt.assert_almost_equal(values[2][1], -0.13205836172334798, decimal=5)

    def test_mass_3d_lens(self):
        R = 1
        Rs = 3
        alpha_Rs = 1
        m_3d = self.nfw.mass_3d_lens(R, Rs, alpha_Rs)
        npt.assert_almost_equal(m_3d, 1.1573795105019022, decimal=8)

    def test_interpol(self):
        Rs = 3
        alpha_Rs = 1
        x = np.array([2, 3, 4])
        y = np.array([1, 1, 1])

        nfw = NFW(interpol=False)
        nfw_interp = NFW(interpol=True)
        nfw_interp_lookup = NFW(interpol=True, lookup=True)

        values = nfw.function(x, y, Rs, alpha_Rs)
        values_interp = nfw_interp.function(x, y, Rs, alpha_Rs)
        values_interp_lookup = nfw_interp_lookup.function(x, y, Rs, alpha_Rs)
        npt.assert_almost_equal(values, values_interp, decimal=4)
        npt.assert_almost_equal(values, values_interp_lookup, decimal=4)

        values = nfw.derivatives(x, y, Rs, alpha_Rs)
        values_interp = nfw_interp.derivatives(x, y, Rs, alpha_Rs)
        values_interp_lookup = nfw_interp_lookup.derivatives(x, y, Rs, alpha_Rs)
        npt.assert_almost_equal(values, values_interp, decimal=4)
        npt.assert_almost_equal(values, values_interp_lookup, decimal=4)

        values = nfw.hessian(x, y, Rs, alpha_Rs)
        values_interp = nfw_interp.hessian(x, y, Rs, alpha_Rs)
        values_interp_lookup = nfw_interp_lookup.hessian(x, y, Rs, alpha_Rs)
        npt.assert_almost_equal(values, values_interp, decimal=4)
        npt.assert_almost_equal(values, values_interp_lookup, decimal=4)
예제 #9
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)
예제 #10
0
class TestNFWELLIPSE(object):
    """
    tests the Gaussian methods
    """
    def setup(self):
        self.nfw = NFW()
        self.nfw_cse = NFW_ELLIPSE_CSE(high_accuracy=True)
        self.nfw_cse_low_accuracy = NFW_ELLIPSE_CSE(high_accuracy=False)

    def test_function(self):
        x = np.linspace(0.01, 2, 10)
        y = np.zeros_like(x)
        kwargs = {'alpha_Rs': 2, 'Rs': 2, 'center_x': 0, 'center_y': 0}

        f_nfw = self.nfw.function(x, y, **kwargs)
        f_cse = self.nfw_cse.function(x, y, e1=0, e2=0, **kwargs)
        npt.assert_almost_equal(f_cse, f_nfw, decimal=5)
        f_cse_low = self.nfw_cse_low_accuracy.function(x,
                                                       y,
                                                       e1=0,
                                                       e2=0,
                                                       **kwargs)
        npt.assert_almost_equal(f_cse_low / f_nfw, 1, decimal=3)

    def test_derivatives(self):
        x = np.linspace(0.01, 2, 10)
        y = np.zeros_like(x)
        kwargs = {'alpha_Rs': 0.5, 'Rs': 2, 'center_x': 0, 'center_y': 0}

        f_x_nfw, f_y_nfw = self.nfw.derivatives(x, y, **kwargs)
        f_x_cse, f_y_cse = self.nfw_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)
        f_x_cse_low, f_y_cse_low = self.nfw_cse_low_accuracy.derivatives(
            x, y, e1=0, e2=0, **kwargs)
        npt.assert_almost_equal(f_x_cse_low / f_x_nfw, 1, decimal=2)
        npt.assert_almost_equal(f_y_cse_low, f_y_nfw, decimal=2)

    def test_hessian(self):
        x = np.linspace(0.01, 2, 10)
        y = np.zeros_like(x)
        kwargs = {'alpha_Rs': 0.5, 'Rs': 2, 'center_x': 0, 'center_y': 0}

        f_xx_nfw, f_xy_nfw, f_yx_nfw, f_yy_nfw = self.nfw.hessian(
            x, y, **kwargs)
        f_xx_cse, f_xy_cse, f_yx_cse, f_yy_cse = self.nfw_cse.hessian(x,
                                                                      y,
                                                                      e1=0,
                                                                      e2=0,
                                                                      **kwargs)
        npt.assert_almost_equal(f_xx_cse, f_xx_nfw, decimal=5)
        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)

        f_xx_cse, f_xy_cse, f_yx_cse, f_yy_cse = self.nfw_cse_low_accuracy.hessian(
            x, y, e1=0, e2=0, **kwargs)
        npt.assert_almost_equal(f_xx_cse / f_xx_nfw, 1, decimal=1)
        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, 1, decimal=1)

    def test_mass_3d_lens(self):
        R = 1
        Rs = 3
        alpha_Rs = 1
        m_3d_nfw = self.nfw.mass_3d_lens(R, Rs, alpha_Rs)
        m_3d_cse = self.nfw_cse.mass_3d_lens(R, Rs, alpha_Rs)
        npt.assert_almost_equal(m_3d_nfw, m_3d_cse, decimal=8)
예제 #11
0
class TestNFWELLIPSE(object):
    """
    tests the Gaussian methods
    """
    def setup(self):
        self.nfw = NFW()
        self.nfw_e = NFW_ELLIPSE()

    def test_function(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        alpha_Rs = 1.
        q = 1.
        phi_G = 0
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        values = self.nfw.function(x, y, Rs, alpha_Rs)
        values_e = self.nfw_e.function(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(values[0], values_e[0], decimal=5)
        x = np.array([0])
        y = np.array([0])

        q = .8
        phi_G = 0
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        values = self.nfw_e.function(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(values[0], 0, decimal=4)

        x = np.array([2, 3, 4])
        y = np.array([1, 1, 1])
        values = self.nfw_e.function(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(values[0], 1.8690403434928538, decimal=5)
        npt.assert_almost_equal(values[1], 2.6186971904371217, decimal=5)
        npt.assert_almost_equal(values[2], 3.360273255326431, decimal=5)

    def test_derivatives(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        alpha_Rs = 1.
        q = 1.
        phi_G = 0
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        f_x, f_y = self.nfw.derivatives(x, y, Rs, alpha_Rs)
        f_x_e, f_y_e = self.nfw_e.derivatives(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(f_x[0], f_x_e[0], decimal=5)
        npt.assert_almost_equal(f_y[0], f_y_e[0], decimal=5)
        x = np.array([0])
        y = np.array([0])
        alpha_Rs = 0
        f_x, f_y = self.nfw_e.derivatives(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(f_x[0], 0, decimal=5)
        npt.assert_almost_equal(f_y[0], 0, decimal=5)

        x = np.array([1, 3, 4])
        y = np.array([2, 1, 1])
        alpha_Rs = 1.
        q = .8
        phi_G = 0
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        values = self.nfw_e.derivatives(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(values[0][0], 0.31473652125391116, decimal=5)
        npt.assert_almost_equal(values[1][0], 0.9835516289184723, decimal=5)
        npt.assert_almost_equal(values[0][1], 0.7525519008422061, decimal=5)
        npt.assert_almost_equal(values[1][1], 0.39195411502198224, decimal=5)

    def test_hessian(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        alpha_Rs = 1.
        q = 1.
        phi_G = 0
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        f_xx, f_xy, f_yx, f_yy = self.nfw.hessian(x, y, Rs, alpha_Rs)
        f_xx_e, f_xy_e, f_yx_e, f_yy_e = self.nfw_e.hessian(
            x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(f_xx[0], f_xx_e[0], decimal=5)
        npt.assert_almost_equal(f_yy[0], f_yy_e[0], decimal=5)
        npt.assert_almost_equal(f_xy[0], f_xy_e[0], decimal=5)
        npt.assert_almost_equal(f_yx[0], f_yx_e[0], decimal=5)

        x = np.array([1, 3, 4])
        y = np.array([2, 1, 1])
        q = .8
        phi_G = 0
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        values = self.nfw_e.hessian(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(values[0][0], 0.26355306825820435, decimal=5)
        npt.assert_almost_equal(values[3][0], -0.008064660050877137, decimal=5)
        npt.assert_almost_equal(values[1][0], -0.159949276046234, decimal=5)
        npt.assert_almost_equal(values[0][1], -0.01251554415659939, decimal=5)
        npt.assert_almost_equal(values[3][1], 0.32051139520206107, decimal=5)
        npt.assert_almost_equal(values[1][1], -0.13717027513848734, decimal=5)

    def test_mass_3d_lens(self):
        R = 1
        Rs = 3
        alpha_Rs = 1
        m_3d = self.nfw_e.mass_3d_lens(R, Rs, alpha_Rs)
        npt.assert_almost_equal(m_3d, 1.1573795105019022, decimal=8)
예제 #12
0
class TestNFWELLIPSE(object):
    """
    tests the Gaussian methods
    """
    def setup(self):
        self.nfw = NFW()
        self.nfw_e = NFW_ELLIPSE()

    def test_function(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        alpha_Rs = 1.
        q = 1.
        phi_G = 0
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        values = self.nfw.function(x, y, Rs, alpha_Rs)
        values_e = self.nfw_e.function(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(values[0], values_e[0], decimal=5)
        x = np.array([0])
        y = np.array([0])

        q = .8
        phi_G = 0
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        values = self.nfw_e.function(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(values[0], 0, decimal=4)

        x = np.array([2, 3, 4])
        y = np.array([1, 1, 1])
        values = self.nfw_e.function(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(values[0], 1.8827504143588476, decimal=5)
        npt.assert_almost_equal(values[1], 2.6436373117941852, decimal=5)
        npt.assert_almost_equal(values[2], 3.394127018818891, decimal=5)

    def test_derivatives(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        alpha_Rs = 1.
        q = 1.
        phi_G = 0
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        f_x, f_y = self.nfw.derivatives(x, y, Rs, alpha_Rs)
        f_x_e, f_y_e = self.nfw_e.derivatives(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(f_x[0], f_x_e[0], decimal=5)
        npt.assert_almost_equal(f_y[0], f_y_e[0], decimal=5)
        x = np.array([0])
        y = np.array([0])
        alpha_Rs = 0
        f_x, f_y = self.nfw_e.derivatives(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(f_x[0], 0, decimal=5)
        npt.assert_almost_equal(f_y[0], 0, decimal=5)

        x = np.array([1, 3, 4])
        y = np.array([2, 1, 1])
        alpha_Rs = 1.
        q = .8
        phi_G = 0
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        values = self.nfw_e.derivatives(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(values[0][0], 0.32458737284934414, decimal=5)
        npt.assert_almost_equal(values[1][0], 0.9737621185480323, decimal=5)
        npt.assert_almost_equal(values[0][1], 0.76249351329615234, decimal=5)
        npt.assert_almost_equal(values[1][1], 0.38124675664807617, decimal=5)

    def test_hessian(self):
        x = np.array([1])
        y = np.array([2])
        Rs = 1.
        alpha_Rs = 1.
        q = 1.
        phi_G = 0
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        f_xx, f_xy, f_yx, f_yy = self.nfw.hessian(x, y, Rs, alpha_Rs)
        f_xx_e, f_xy_e, f_yx_e, f_yy_e = self.nfw_e.hessian(
            x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(f_xx[0], f_xx_e[0], decimal=5)
        npt.assert_almost_equal(f_yy[0], f_yy_e[0], decimal=5)
        npt.assert_almost_equal(f_xy[0], f_xy_e[0], decimal=5)
        npt.assert_almost_equal(f_yx[0], f_yx_e[0], decimal=5)

        x = np.array([1, 3, 4])
        y = np.array([2, 1, 1])
        q = .8
        phi_G = 0
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        values = self.nfw_e.hessian(x, y, Rs, alpha_Rs, e1, e2)
        npt.assert_almost_equal(values[0][0], 0.26998576668768592, decimal=5)
        npt.assert_almost_equal(values[3][0],
                                -0.0045328224507201753,
                                decimal=5)
        npt.assert_almost_equal(values[1][0], -0.16380454531672584, decimal=5)
        npt.assert_almost_equal(values[0][1], -0.014833136829928151, decimal=5)
        npt.assert_almost_equal(values[3][1], 0.31399726446723619, decimal=5)
        npt.assert_almost_equal(values[1][1], -0.13449884961325154, decimal=5)

    def test_mass_3d_lens(self):
        R = 1
        Rs = 3
        alpha_Rs = 1
        m_3d = self.nfw_e.mass_3d_lens(R, Rs, alpha_Rs)
        npt.assert_almost_equal(m_3d, 1.1573795105019022, decimal=8)