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
def test_init(self): lens_model_list = ['FLEXION', 'SIS_TRUNCATED', 'SERSIC', 'SERSIC_ELLIPSE', 'PJAFFE', 'PJAFFE_ELLIPSE', 'HERNQUIST_ELLIPSE', 'INTERPOL', 'INTERPOL_SCALED', 'SHAPELETS_POLAR', 'DIPOLE', 'GAUSSIAN_KAPPA_ELLIPSE', 'MULTI_GAUSSIAN_KAPPA' , 'MULTI_GAUSSIAN_KAPPA_ELLIPSE', 'CHAMELEON', 'DOUBLE_CHAMELEON'] lensModel = LensModel(lens_model_list) assert len(lensModel.lens_model_list) == len(lens_model_list) lens_model_list = ['NFW'] lensModel = LensModel(lens_model_list) x,y = 0.2,1 kwargs = [{'theta_Rs':1, 'Rs': 0.5, 'center_x':0, 'center_y':0}] value = lensModel.potential(x,y,kwargs) nfw_interp = NFW(interpol=True, lookup=True) value_interp_lookup = nfw_interp.function(x, y, **kwargs[0]) npt.assert_almost_equal(value, value_interp_lookup, decimal=4)
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)
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)
class CNFW(object): """ this class computes the lensing quantities of a cored NFW profile: rho = rho0 * (r + r_core)^-1 * (r + rs)^-2 """ param_names = ['Rs', 'theta_Rs', 'r_core', 'center_x', 'center_y'] lower_limit_default = { 'Rs': 0, 'theta_Rs': 0, 'r_core': 0, 'center_x': -100, 'center_y': -100 } upper_limit_default = { 'Rs': 100, 'theta_Rs': 10, 'r_core': 100, 'center_x': 100, 'center_y': 100 } def __init__(self): """ :param interpol: bool, if True, interpolates the functions F(), g() and h() """ self._nfw = NFW() def function(self, x, y, Rs, theta_Rs, r_core, center_x=0, center_y=0): """ :param x: angular position :param y: angular position :param Rs: angular turn over point :param theta_Rs: deflection at Rs :param center_x: center of halo :param center_y: center of halo :return: """ warnings.warn('Potential for cored NFW potential not yet implemented. ' 'Using the expression for the NFW ' 'potential instead.') pot = self._nfw.function(x, y, Rs, theta_Rs, center_x=center_x, center_y=center_y) return pot def _nfw_func(self, x): """ Classic NFW function in terms of arctanh and arctan :param x: r/Rs :return: """ c = 0.000001 if isinstance(x, np.ndarray): x[np.where(x < c)] = c nfwvals = np.ones_like(x) inds1 = np.where(x < 1) inds2 = np.where(x > 1) nfwvals[inds1] = (1 - x[inds1]**2)**-.5 * np.arctanh( (1 - x[inds1]**2)**.5) nfwvals[inds2] = (x[inds2]**2 - 1)**-.5 * np.arctan( (x[inds2]**2 - 1)**.5) return nfwvals elif isinstance(x, float) or isinstance(x, int): x = max(x, c) if x == 1: return 1 if x < 1: return (1 - x**2)**-.5 * np.arctanh((1 - x**2)**.5) else: return (x**2 - 1)**-.5 * np.arctan((x**2 - 1)**.5) def _F(self, X, b, c=0.001): """ analytic solution of the projection integral :param x: a dimensionless quantity, either r/rs or r/rc :type x: float >0 """ if b == 1: b = 1 + c prefac = (b - 1)**-2 if isinstance(X, np.ndarray): X[np.where(X == 1)] = 1 - c output = np.empty_like(X) inds1 = np.where(np.absolute(X - b) < c) output[inds1] = prefac * ( -2 - b + (1 + b + b**2) * self._nfw_func(b)) * (1 + b)**-1 inds2 = np.where(np.absolute(X - b) >= c) output[inds2] = prefac * ((X[inds2] ** 2 - 1) ** -1 * (1 - b - (1 - b * X[inds2] ** 2) * self._nfw_func(X[inds2])) - \ self._nfw_func(X[inds2] * b ** -1)) else: if X == 1: X = 1 - c if np.absolute(X - b) < c: output = prefac * ( -2 - b + (1 + b + b**2) * self._nfw_func(b)) * (1 + b)**-1 else: output = prefac * ((X ** 2 - 1) ** -1 * (1 - b - (1 - b * X ** 2) * self._nfw_func(X)) - \ self._nfw_func(X * b ** -1)) return output def _G(self, X, b, c=0.00000001): """ analytic solution of integral for NFW profile to compute deflection angel and gamma :param x: R/Rs :type x: float >0 """ if b == 1: b = 1 + c b2 = b**2 x2 = X**2 fac = (1 - b)**2 prefac = fac**-1 if isinstance(X, np.ndarray): output = np.ones_like(X) inds1 = np.where(np.absolute(X - b) <= c) inds2 = np.where(np.absolute(X - b) > c) output[inds1] = prefac * ( 2 * (1 - 2 * b + b**3) * self._nfw_func(b) + fac * (-1.38692 + np.log(b2)) - b2 * np.log(b2)) output[inds2] = prefac * (fac * np.log(0.25 * x2[inds2]) - b2 * np.log(b2) + \ 2 * (b2 - x2[inds2]) * self._nfw_func(X[inds2] * b**-1) + 2 * (1+b*(x2[inds2] - 2))* self._nfw_func(X[inds2])) return 0.5 * output else: if np.absolute(X - b) <= c: output = prefac * (2*(1-2*b+b**3)*self._nfw_func(b) + \ fac * (-1.38692 + np.log(b2)) - b2*np.log(b2)) else: output = prefac * (fac * np.log(0.25 * x2) - b2 * np.log(b2) + \ 2 * (b2 - x2) * self._nfw_func(X * b**-1) + 2 * (1+b*(x2 - 2))* self._nfw_func(X)) return 0.5 * output def derivatives(self, x, y, Rs, theta_Rs, r_core, center_x=0, center_y=0): rho0_input = self._alpha2rho0(theta_Rs=theta_Rs, Rs=Rs, r_core=r_core) if Rs < 0.0000001: Rs = 0.0000001 x_ = x - center_x y_ = y - center_y R = np.sqrt(x_**2 + y_**2) f_x, f_y = self.cnfwAlpha(R, Rs, rho0_input, r_core, x_, y_) return f_x, f_y def hessian(self, x, y, Rs, theta_Rs, r_core, center_x=0, center_y=0): #raise Exception('Hessian for truncated nfw profile not yet implemented.') """ returns Hessian matrix of function d^2f/dx^2, d^f/dy^2, d^2/dxdy """ rho0_input = self._alpha2rho0(theta_Rs=theta_Rs, Rs=Rs, r_core=r_core) if Rs < 0.0001: Rs = 0.0001 x_ = x - center_x y_ = y - center_y R = np.sqrt(x_**2 + y_**2) kappa = self.density_2d(x_, y_, Rs, rho0_input, r_core) gamma1, gamma2 = self.cnfwGamma(R, Rs, rho0_input, r_core, x_, y_) f_xx = kappa + gamma1 f_yy = kappa - gamma1 f_xy = gamma2 return f_xx, f_yy, f_xy def density(self, R, Rs, rho0, r_core): """ three dimenstional truncated NFW profile :param R: radius of interest :type R: float/numpy array :param Rs: scale radius :type Rs: float :param rho0: density normalization (central core density) :type rho0: float :return: rho(R) density """ M0 = 4 * np.pi * rho0 * Rs**3 return (M0 / 4 / np.pi) * ((r_core + R) * (R + Rs)**2)**-1 def density_2d(self, x, y, Rs, rho0, r_core, center_x=0, center_y=0): """ projected two dimenstional NFW profile (kappa*Sigma_crit) :param R: radius of interest :type R: float/numpy array :param Rs: scale radius :type Rs: float :param rho0: density normalization (characteristic density) :type rho0: float :param r200: radius of (sub)halo :type r200: float>0 :return: Epsilon(R) projected density at radius R """ x_ = x - center_x y_ = y - center_y R = np.sqrt(x_**2 + y_**2) b = r_core * Rs**-1 x = R * Rs**-1 Fx = self._F(x, b) return 2 * rho0 * Rs * Fx def mass_3d(self, R, Rs, rho0, r_core): """ mass enclosed a 3d sphere or radius r :param r: :param Ra: :param Rs: :return: """ b = r_core * Rs**-1 x = R * Rs**-1 M_0 = 4 * np.pi * Rs**3 * rho0 return M_0 * (x * (1 + x)**-1 * (-1 + b)**-1 + (-1 + b)**-2 * ( (2 * b - 1) * np.log(1 / (1 + x)) + b**2 * np.log(x / b + 1))) def cnfwAlpha(self, R, Rs, rho0, r_core, ax_x, ax_y): """ deflection angel of NFW profile along the projection to coordinate axis :param R: radius of interest :type R: float/numpy array :param Rs: scale radius :type Rs: float :param rho0: density normalization (characteristic density) :type rho0: float :param r200: radius of (sub)halo :type r200: float>0 :param axis: projection to either x- or y-axis :type axis: same as R :return: Epsilon(R) projected density at radius R """ if isinstance(R, int) or isinstance(R, float): R = max(R, 0.00001) else: R[R <= 0.00001] = 0.00001 x = R / Rs b = r_core * Rs**-1 b = max(b, 0.000001) gx = self._G(x, b) a = 4 * rho0 * Rs * gx / x**2 return a * ax_x, a * ax_y def cnfwGamma(self, R, Rs, rho0, r_core, ax_x, ax_y): """ shear gamma of NFW profile (times Sigma_crit) along the projection to coordinate 'axis' :param R: radius of interest :type R: float/numpy array :param Rs: scale radius :type Rs: float :param rho0: density normalization (characteristic density) :type rho0: float :param r200: radius of (sub)halo :type r200: float>0 :param axis: projection to either x- or y-axis :type axis: same as R :return: Epsilon(R) projected density at radius R """ c = 0.000001 if isinstance(R, int) or isinstance(R, float): R = max(R, c) else: R[R <= c] = c x = R * Rs**-1 b = r_core * Rs**-1 b = max(b, c) gx = self._G(x, b) Fx = self._F(x, b) a = 2 * rho0 * Rs * (2 * gx / x**2 - Fx ) # /x #2*rho0*Rs*(2*gx/x**2 - Fx)*axis/x return a * (ax_y**2 - ax_x**2) / R**2, -a * 2 * (ax_x * ax_y) / R**2 def mass_2d(self, R, Rs, rho0, r_core): """ analytic solution of the projection integral (convergence) :param x: R/Rs :type x: float >0 """ x = R / Rs b = r_core / Rs b = max(b, 0.000001) gx = self._G(x, b) #m_2d = 4 * np.pi* rho0 * Rs**3 * gx m_2d = 4 * np.pi * rho0 * Rs * R**2 * gx / x**2 return m_2d def _alpha2rho0(self, theta_Rs, Rs, r_core): b = r_core * Rs**-1 gx = self._G(1., b) rho0 = theta_Rs * (4 * Rs**2 * gx)**-1 return rho0 def _rho2alpha(self, rho0, Rs, r_core): b = r_core * Rs**-1 gx = self._G(1., b) alpha = 4 * Rs**2 * gx * rho0 return alpha
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)
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)
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)
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)
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)
class Testcnfw(object): """ tests the Gaussian methods """ def setup(self): self.cn = CNFW() self.n = NFW() def test_pot(self): pot1 = self.cn.function(2, 0, 1, 1, 0.5) pot2 = self.n.function(2, 0, 1, 1) npt.assert_almost_equal(pot1, pot2) def _kappa_integrand(self, x, y, Rs, m0, r_core): return 2 * np.pi * x * self.cn.density_2d(x, y, Rs, m0, r_core) def test_derivatives(self): Rs = 10. rho0 = 1. r_core = 7. R = np.linspace(0.1 * Rs, 4 * Rs, 1000) alpha = self.cn.cnfwAlpha(R, Rs, rho0, r_core, R, 0)[0] alpha_theory = self.cn.mass_2d(R, Rs, rho0, r_core) / np.pi / R theta_Rs = self.cn._rho2alpha(rho0, Rs, r_core) alpha_derivatives = self.cn.derivatives(R, 0, Rs, theta_Rs, r_core)[0] npt.assert_almost_equal(alpha / alpha_theory, 1) npt.assert_almost_equal(alpha / alpha_derivatives, 1) def test_mproj(self): Rs = 10. r_core = 0.7 * Rs Rmax = np.linspace(0.6 * Rs, 1.1 * Rs, 1000) dr = Rmax[1] - Rmax[0] m0 = 1 m2d = self.cn.mass_2d(Rmax, Rs, m0, r_core) integrand = np.gradient(m2d, dr) kappa_integrand = self._kappa_integrand(Rmax, 0, Rs, m0, r_core) mean_diff = np.absolute(kappa_integrand - integrand) * len(Rmax)**-1 npt.assert_almost_equal(mean_diff, 0, decimal=3) def test_GF(self): x_array = np.array([0.5, 0.8, 1.2]) b = 0.7 Garray = self.cn._G(x_array, b) Farray = self.cn._F(x_array, b) for i in range(0, len(x_array)): npt.assert_almost_equal(Farray[i], self.cn._F(x_array[i], b)) npt.assert_almost_equal(Garray[i], self.cn._G(x_array[i], b)) def test_gamma(self): Rs = 10. rho0 = 1. r_core = 0.7 * Rs R = np.array([0.5 * Rs, 0.8 * Rs, 1.1 * Rs]) g1_array, g2_array = self.cn.cnfwGamma(R, Rs, rho0, r_core, R, 0.6 * Rs) for i in range(0, len(R)): g1, g2 = self.cn.cnfwGamma(R[i], Rs, rho0, r_core, R[i], 0.6 * Rs) npt.assert_almost_equal(g1_array[i], g1) npt.assert_almost_equal(g2_array[i], g2) def test_rho_angle_transform(self): Rs = float(10) rho0 = float(1) r_core = float(7) theta_Rs = self.cn._rho2alpha(rho0, Rs, r_core) theta_rs_2 = self.cn.cnfwAlpha(Rs, Rs, rho0, r_core, Rs, 0)[0] npt.assert_almost_equal(theta_Rs * theta_rs_2**-1, 1) rho0_2 = self.cn._alpha2rho0(theta_Rs, Rs, r_core) npt.assert_almost_equal(rho0, rho0_2)
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)
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)
class Testcnfw(object): """ tests the Gaussian methods """ def setup(self): self.cn = CNFW() self.n = NFW() def test_pot(self): # this test requires that the CNFW profile with a very small core results in the potential of the NFW profile pot1 = self.cn.function(x=2, y=0, Rs=1, alpha_Rs=1, r_core=0.001) pot2 = self.n.function(x=2, y=0, Rs=1, alpha_Rs=1) npt.assert_almost_equal(pot1 / pot2, 1, decimal=3) def _kappa_integrand(self, x, y, Rs, m0, r_core): return 2 * np.pi * x * self.cn.density_2d(x, y, Rs, m0, r_core) def test_derivatives(self): Rs = 10. rho0 = 1. r_core = 7. R = np.linspace(0.1 * Rs, 4 * Rs, 1000) alpha_Rs = self.cn._rho2alpha(rho0, Rs, r_core) alpha = self.cn.alpha_r(R, Rs, rho0, r_core) alpha_theory = self.cn.mass_2d(R, Rs, rho0, r_core) / np.pi / R alpha_derivatives = self.cn.derivatives(R, 0, Rs, alpha_Rs, r_core)[0] npt.assert_almost_equal(alpha_derivatives / alpha_theory, 1) npt.assert_almost_equal(alpha / alpha_theory, 1) npt.assert_almost_equal(alpha / alpha_derivatives, 1) def test_mass_3d(self): Rs = 10. rho0 = 1. r_core = 7. R = np.linspace(0.1 * Rs, 4 * Rs, 1000) alpha_Rs = self.cn._rho2alpha(rho0, Rs, r_core) m3d = self.cn.mass_3d(R, Rs, rho0, r_core) m3d_lens = self.cn.mass_3d_lens(R, Rs, alpha_Rs, r_core) npt.assert_almost_equal(m3d, m3d_lens, decimal=8) def test_mproj(self): Rs = 10. r_core = 0.7 * Rs Rmax = np.linspace(0.6 * Rs, 1.1 * Rs, 1000) dr = Rmax[1] - Rmax[0] m0 = 1 m2d = self.cn.mass_2d(Rmax, Rs, m0, r_core) integrand = np.gradient(m2d, dr) kappa_integrand = self._kappa_integrand(Rmax, 0, Rs, m0, r_core) mean_diff = np.absolute(kappa_integrand - integrand) * len(Rmax)**-1 npt.assert_almost_equal(mean_diff, 0, decimal=3) def test_GF(self): x_array = np.array([0.5, 0.8, 1.2]) b = 0.7 Garray = self.cn._G(x_array, b) Farray = self.cn._F(x_array, b) for i in range(0, len(x_array)): npt.assert_almost_equal(Farray[i], self.cn._F(x_array[i], b)) npt.assert_almost_equal(Garray[i], self.cn._G(x_array[i], b)) def test_gamma(self): Rs = 10. rho0 = 1. r_core = 0.7 * Rs R = np.array([0.5 * Rs, 0.8 * Rs, 1.1 * Rs]) g1_array, g2_array = self.cn.cnfwGamma(R, Rs, rho0, r_core, R, 0.6 * Rs) for i in range(0, len(R)): g1, g2 = self.cn.cnfwGamma(R[i], Rs, rho0, r_core, R[i], 0.6 * Rs) npt.assert_almost_equal(g1_array[i], g1) npt.assert_almost_equal(g2_array[i], g2)