def __init__(self): """ """ self.tnfw = TNFW() self._diff = 0.0000000001 super(TNFW_ELLIPSE, self).__init__()
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)
def __init__(self): self.deflections = 10**log_deflection_angle self.tau = np.arange(1, 31, 1) self.beta = np.arange(0.0025, 1.005, 0.005) log_xnfw = np.log10(np.logspace(-3, 2, 100)) self.split = [] self.log_xmin, self.log_xmax = log_xnfw[0], log_xnfw[-1] self._betamin = self.beta[0] self._betamax = self.beta[-1] self._delta_beta = self.beta[2] - self.beta[1] self._logx_domain = log_xnfw self._tau_min = self.tau[0] self._tau_max = self.tau[-1] self._delta_tau = self.tau[1] - self.tau[0] interp_list = [] for i, bi in enumerate(self.beta): interp_list_tau = [] for j, tj in enumerate(self.tau): interp = interp1d(log_xnfw, log_deflection_angle[i, j, :]) interp_list_tau.append(interp) interp_list.append(interp_list_tau) self._interp_list = interp_list self._tnfw_profile = TNFW()
def setup(self): self.nfw = NFW() self.tnfw = TNFW()
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)
def _import_class(lens_type, custom_class, z_lens=None, z_source=None): """ :param lens_type: string, lens model type :param custom_class: custom class :param z_lens: lens redshift # currently only used in NFW_MC model as this is redshift dependent :param z_source: source redshift # currently only used in NFW_MC model as this is redshift dependent :return: class instance of the lens model type """ if lens_type == 'SHIFT': from lenstronomy.LensModel.Profiles.alpha_shift import Shift return Shift() elif lens_type == 'SHEAR': from lenstronomy.LensModel.Profiles.shear import Shear return Shear() elif lens_type == 'SHEAR_GAMMA_PSI': from lenstronomy.LensModel.Profiles.shear import ShearGammaPsi return ShearGammaPsi() elif lens_type == 'CONVERGENCE': from lenstronomy.LensModel.Profiles.convergence import Convergence return Convergence() elif lens_type == 'FLEXION': from lenstronomy.LensModel.Profiles.flexion import Flexion return Flexion() elif lens_type == 'FLEXIONFG': from lenstronomy.LensModel.Profiles.flexionfg import Flexionfg return Flexionfg() elif lens_type == 'POINT_MASS': from lenstronomy.LensModel.Profiles.point_mass import PointMass return PointMass() elif lens_type == 'SIS': from lenstronomy.LensModel.Profiles.sis import SIS return SIS() elif lens_type == 'SIS_TRUNCATED': from lenstronomy.LensModel.Profiles.sis_truncate import SIS_truncate return SIS_truncate() elif lens_type == 'SIE': from lenstronomy.LensModel.Profiles.sie import SIE return SIE() elif lens_type == 'SPP': from lenstronomy.LensModel.Profiles.spp import SPP return SPP() elif lens_type == 'NIE': from lenstronomy.LensModel.Profiles.nie import NIE return NIE() elif lens_type == 'NIE_SIMPLE': from lenstronomy.LensModel.Profiles.nie import NIEMajorAxis return NIEMajorAxis() elif lens_type == 'CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import Chameleon return Chameleon() elif lens_type == 'DOUBLE_CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import DoubleChameleon return DoubleChameleon() elif lens_type == 'TRIPLE_CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import TripleChameleon return TripleChameleon() elif lens_type == 'SPEP': from lenstronomy.LensModel.Profiles.spep import SPEP return SPEP() elif lens_type == 'SPEMD': from lenstronomy.LensModel.Profiles.spemd import SPEMD return SPEMD() elif lens_type == 'SPEMD_SMOOTH': from lenstronomy.LensModel.Profiles.spemd_smooth import SPEMD_SMOOTH return SPEMD_SMOOTH() elif lens_type == 'NFW': from lenstronomy.LensModel.Profiles.nfw import NFW return NFW() elif lens_type == 'NFW_ELLIPSE': from lenstronomy.LensModel.Profiles.nfw_ellipse import NFW_ELLIPSE return NFW_ELLIPSE() elif lens_type == 'NFW_ELLIPSE_GAUSS_DEC': from lenstronomy.LensModel.Profiles.gauss_decomposition import NFWEllipseGaussDec return NFWEllipseGaussDec() elif lens_type == 'TNFW': from lenstronomy.LensModel.Profiles.tnfw import TNFW return TNFW() elif lens_type == 'CNFW': from lenstronomy.LensModel.Profiles.cnfw import CNFW return CNFW() elif lens_type == 'CNFW_ELLIPSE': from lenstronomy.LensModel.Profiles.cnfw_ellipse import CNFW_ELLIPSE return CNFW_ELLIPSE() elif lens_type == 'CTNFW_GAUSS_DEC': from lenstronomy.LensModel.Profiles.gauss_decomposition import CTNFWGaussDec return CTNFWGaussDec() elif lens_type == 'NFW_MC': from lenstronomy.LensModel.Profiles.nfw_mass_concentration import NFWMC return NFWMC(z_lens=z_lens, z_source=z_source) elif lens_type == 'SERSIC': from lenstronomy.LensModel.Profiles.sersic import Sersic return Sersic() elif lens_type == 'SERSIC_ELLIPSE_POTENTIAL': from lenstronomy.LensModel.Profiles.sersic_ellipse_potential import SersicEllipse return SersicEllipse() elif lens_type == 'SERSIC_ELLIPSE_KAPPA': from lenstronomy.LensModel.Profiles.sersic_ellipse_kappa import SersicEllipseKappa return SersicEllipseKappa() elif lens_type == 'SERSIC_ELLIPSE_GAUSS_DEC': from lenstronomy.LensModel.Profiles.gauss_decomposition \ import SersicEllipseGaussDec return SersicEllipseGaussDec() elif lens_type == 'PJAFFE': from lenstronomy.LensModel.Profiles.p_jaffe import PJaffe return PJaffe() elif lens_type == 'PJAFFE_ELLIPSE': from lenstronomy.LensModel.Profiles.p_jaffe_ellipse import PJaffe_Ellipse return PJaffe_Ellipse() elif lens_type == 'HERNQUIST': from lenstronomy.LensModel.Profiles.hernquist import Hernquist return Hernquist() elif lens_type == 'HERNQUIST_ELLIPSE': from lenstronomy.LensModel.Profiles.hernquist_ellipse import Hernquist_Ellipse return Hernquist_Ellipse() elif lens_type == 'GAUSSIAN': from lenstronomy.LensModel.Profiles.gaussian_potential import Gaussian return Gaussian() elif lens_type == 'GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.gaussian_kappa import GaussianKappa return GaussianKappa() elif lens_type == 'GAUSSIAN_ELLIPSE_KAPPA': from lenstronomy.LensModel.Profiles.gaussian_ellipse_kappa import GaussianEllipseKappa return GaussianEllipseKappa() elif lens_type == 'GAUSSIAN_ELLIPSE_POTENTIAL': from lenstronomy.LensModel.Profiles.gaussian_ellipse_potential import GaussianEllipsePotential return GaussianEllipsePotential() elif lens_type == 'MULTI_GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappa return MultiGaussianKappa() elif lens_type == 'MULTI_GAUSSIAN_KAPPA_ELLIPSE': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappaEllipse return MultiGaussianKappaEllipse() elif lens_type == 'INTERPOL': from lenstronomy.LensModel.Profiles.interpol import Interpol return Interpol() elif lens_type == 'INTERPOL_SCALED': from lenstronomy.LensModel.Profiles.interpol import InterpolScaled return InterpolScaled() elif lens_type == 'SHAPELETS_POLAR': from lenstronomy.LensModel.Profiles.shapelet_pot_polar import PolarShapelets return PolarShapelets() elif lens_type == 'SHAPELETS_CART': from lenstronomy.LensModel.Profiles.shapelet_pot_cartesian import CartShapelets return CartShapelets() elif lens_type == 'DIPOLE': from lenstronomy.LensModel.Profiles.dipole import Dipole return Dipole() elif lens_type == 'CURVED_ARC': from lenstronomy.LensModel.Profiles.curved_arc import CurvedArc return CurvedArc() elif lens_type == 'ARC_PERT': from lenstronomy.LensModel.Profiles.arc_perturbations import ArcPerturbations return ArcPerturbations() elif lens_type == 'coreBURKERT': from lenstronomy.LensModel.Profiles.coreBurkert import CoreBurkert return CoreBurkert() elif lens_type == 'CORED_DENSITY': from lenstronomy.LensModel.Profiles.cored_density import CoredDensity return CoredDensity() elif lens_type == 'CORED_DENSITY_2': from lenstronomy.LensModel.Profiles.cored_density_2 import CoredDensity2 return CoredDensity2() elif lens_type == 'CORED_DENSITY_MST': from lenstronomy.LensModel.Profiles.cored_density_mst import CoredDensityMST return CoredDensityMST(profile_type='CORED_DENSITY') elif lens_type == 'CORED_DENSITY_2_MST': from lenstronomy.LensModel.Profiles.cored_density_mst import CoredDensityMST return CoredDensityMST(profile_type='CORED_DENSITY_2') elif lens_type == 'NumericalAlpha': from lenstronomy.LensModel.Profiles.numerical_deflections import NumericalAlpha return NumericalAlpha(custom_class) else: raise ValueError('%s is not a valid lens model' % lens_type)
def __init__(self, lens_model_list, **kwargs): """ :param lens_model_list: list of strings with lens model names :param foreground_shear: bool, when True, models a foreground non-linear shear distortion """ self.func_list = [] self._foreground_shear = False for i, lens_type in enumerate(lens_model_list): if lens_type == 'SHEAR': from lenstronomy.LensModel.Profiles.external_shear import ExternalShear self.func_list.append(ExternalShear()) elif lens_type == 'CONVERGENCE': from lenstronomy.LensModel.Profiles.mass_sheet import MassSheet self.func_list.append(MassSheet()) elif lens_type == 'FLEXION': from lenstronomy.LensModel.Profiles.flexion import Flexion self.func_list.append(Flexion()) elif lens_type == 'POINT_MASS': from lenstronomy.LensModel.Profiles.point_mass import PointMass self.func_list.append(PointMass()) elif lens_type == 'SIS': from lenstronomy.LensModel.Profiles.sis import SIS self.func_list.append(SIS()) elif lens_type == 'SIS_TRUNCATED': from lenstronomy.LensModel.Profiles.sis_truncate import SIS_truncate self.func_list.append(SIS_truncate()) elif lens_type == 'SIE': from lenstronomy.LensModel.Profiles.sie import SIE self.func_list.append(SIE()) elif lens_type == 'SPP': from lenstronomy.LensModel.Profiles.spp import SPP self.func_list.append(SPP()) elif lens_type == 'NIE': from lenstronomy.LensModel.Profiles.nie import NIE self.func_list.append(NIE()) elif lens_type == 'NIE_SIMPLE': from lenstronomy.LensModel.Profiles.nie import NIE_simple self.func_list.append(NIE_simple()) elif lens_type == 'CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import Chameleon self.func_list.append(Chameleon()) elif lens_type == 'DOUBLE_CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import DoubleChameleon self.func_list.append(DoubleChameleon()) elif lens_type == 'SPEP': from lenstronomy.LensModel.Profiles.spep import SPEP self.func_list.append(SPEP()) elif lens_type == 'SPEMD': from lenstronomy.LensModel.Profiles.spemd import SPEMD self.func_list.append(SPEMD()) elif lens_type == 'SPEMD_SMOOTH': from lenstronomy.LensModel.Profiles.spemd_smooth import SPEMD_SMOOTH self.func_list.append(SPEMD_SMOOTH()) elif lens_type == 'NFW': from lenstronomy.LensModel.Profiles.nfw import NFW self.func_list.append(NFW(**kwargs)) elif lens_type == 'NFW_ELLIPSE': from lenstronomy.LensModel.Profiles.nfw_ellipse import NFW_ELLIPSE self.func_list.append( NFW_ELLIPSE(interpol=False, num_interp_X=1000, max_interp_X=100)) elif lens_type == 'TNFW': from lenstronomy.LensModel.Profiles.tnfw import TNFW self.func_list.append(TNFW()) elif lens_type == 'SERSIC': from lenstronomy.LensModel.Profiles.sersic import Sersic self.func_list.append(Sersic()) elif lens_type == 'SERSIC_ELLIPSE': from lenstronomy.LensModel.Profiles.sersic_ellipse import SersicEllipse self.func_list.append(SersicEllipse()) elif lens_type == 'PJAFFE': from lenstronomy.LensModel.Profiles.p_jaffe import PJaffe self.func_list.append(PJaffe()) elif lens_type == 'PJAFFE_ELLIPSE': from lenstronomy.LensModel.Profiles.p_jaffe_ellipse import PJaffe_Ellipse self.func_list.append(PJaffe_Ellipse()) elif lens_type == 'HERNQUIST': from lenstronomy.LensModel.Profiles.hernquist import Hernquist self.func_list.append(Hernquist()) elif lens_type == 'HERNQUIST_ELLIPSE': from lenstronomy.LensModel.Profiles.hernquist_ellipse import Hernquist_Ellipse self.func_list.append(Hernquist_Ellipse()) elif lens_type == 'GAUSSIAN': from lenstronomy.LensModel.Profiles.gaussian_potential import Gaussian self.func_list.append(Gaussian()) elif lens_type == 'GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.gaussian_kappa import GaussianKappa self.func_list.append(GaussianKappa()) elif lens_type == 'GAUSSIAN_KAPPA_ELLIPSE': from lenstronomy.LensModel.Profiles.gaussian_kappa_ellipse import GaussianKappaEllipse self.func_list.append(GaussianKappaEllipse()) elif lens_type == 'MULTI_GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappa self.func_list.append(MultiGaussianKappa()) elif lens_type == 'MULTI_GAUSSIAN_KAPPA_ELLIPSE': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappaEllipse self.func_list.append(MultiGaussianKappaEllipse()) elif lens_type == 'INTERPOL': from lenstronomy.LensModel.Profiles.interpol import Interpol_func self.func_list.append( Interpol_func(grid=False, min_grid_number=100)) elif lens_type == 'INTERPOL_SCALED': from lenstronomy.LensModel.Profiles.interpol import Interpol_func_scaled self.func_list.append( Interpol_func_scaled(grid=False, min_grid_number=100)) elif lens_type == 'SHAPELETS_POLAR': from lenstronomy.LensModel.Profiles.shapelet_pot_polar import PolarShapelets self.func_list.append(PolarShapelets()) elif lens_type == 'SHAPELETS_CART': from lenstronomy.LensModel.Profiles.shapelet_pot_cartesian import CartShapelets self.func_list.append(CartShapelets()) elif lens_type == 'DIPOLE': from lenstronomy.LensModel.Profiles.dipole import Dipole self.func_list.append(Dipole()) elif lens_type == 'FOREGROUND_SHEAR': from lenstronomy.LensModel.Profiles.external_shear import ExternalShear self.func_list.append(ExternalShear()) self._foreground_shear = True self._foreground_shear_idex = i else: raise ValueError('%s is not a valid lens model' % lens_type) self._model_list = lens_model_list
class TNFW_ELLIPSE(LensProfileBase): """ this class contains functions concerning the truncated NFW profile with an ellipticity defined in the potential parameterization of alpha_Rs, Rs and r_trunc is the same as for the spherical NFW profile from Glose & Kneib: https://cds.cern.ch/record/529584/files/0112138.pdf relation are: R_200 = c * Rs """ profile_name = 'TNFW_ELLIPSE' param_names = [ 'Rs', 'alpha_Rs', 'r_trunc', 'e1', 'e2', 'center_x', 'center_y' ] lower_limit_default = { 'Rs': 0, 'alpha_Rs': 0, 'r_trunc': 0, 'e1': -0.5, 'e2': -0.5, 'center_x': -100, 'center_y': -100 } upper_limit_default = { 'Rs': 100, 'alpha_Rs': 10, 'r_trunc': 100, 'e1': 0.5, 'e2': 0.5, 'center_x': 100, 'center_y': 100 } def __init__(self): """ """ self.tnfw = TNFW() self._diff = 0.0000000001 super(TNFW_ELLIPSE, self).__init__() def function(self, x, y, Rs, alpha_Rs, r_trunc, e1, e2, center_x=0, center_y=0): """ returns elliptically distorted NFW lensing potential :param x: angular position (normally in units of arc seconds) :param y: angular position (normally in units of arc seconds) :param Rs: turn over point in the slope of the NFW profile in angular unit :param alpha_Rs: deflection (angular units) at projected Rs :param r_trunc: truncation radius :param e1: eccentricity component in x-direction :param e2: eccentricity component in y-direction :param center_x: center of halo (in angular units) :param center_y: center of halo (in angular units) :return: lensing potential """ x_, y_ = param_util.transform_e1e2_square_average( x, y, e1, e2, center_x, center_y) R_ = np.sqrt(x_**2 + y_**2) rho0_input = self.tnfw.alpha2rho0(alpha_Rs=alpha_Rs, Rs=Rs) Rs = np.maximum(Rs, 0.0000001) #if Rs < 0.0000001: # Rs = 0.0000001 f_ = self.tnfw.nfwPot(R_, Rs, rho0_input, r_trunc) return f_ def derivatives(self, x, y, Rs, alpha_Rs, r_trunc, e1, e2, center_x=0, center_y=0): """ returns df/dx and df/dy of the function, calculated as an elliptically distorted deflection angle of the spherical NFW profile :param x: angular position (normally in units of arc seconds) :param y: angular position (normally in units of arc seconds) :param Rs: turn over point in the slope of the NFW profile in angular unit :param alpha_Rs: deflection (angular units) at projected Rs :param r_trunc: truncation radius :param e1: eccentricity component in x-direction :param e2: eccentricity component in y-direction :param center_x: center of halo (in angular units) :param center_y: center of halo (in angular units) :return: deflection in x-direction, deflection in y-direction """ x_, y_ = param_util.transform_e1e2_square_average( x, y, e1, e2, center_x, center_y) phi_G, q = param_util.ellipticity2phi_q(e1, e2) cos_phi = np.cos(phi_G) sin_phi = np.sin(phi_G) e = param_util.q2e(q) # e = abs(1 - q) R_ = np.sqrt(x_**2 + y_**2) rho0_input = self.tnfw.alpha2rho0(alpha_Rs=alpha_Rs, Rs=Rs) Rs = np.maximum(Rs, 0.0000001) #if Rs < 0.0000001: # Rs = 0.0000001 f_x_prim, f_y_prim = self.tnfw.nfwAlpha(R_, Rs, rho0_input, r_trunc, x_, y_) f_x_prim *= np.sqrt(1 - e) f_y_prim *= np.sqrt(1 + e) f_x = cos_phi * f_x_prim - sin_phi * f_y_prim f_y = sin_phi * f_x_prim + cos_phi * f_y_prim return f_x, f_y def hessian(self, x, y, Rs, alpha_Rs, r_trunc, e1, e2, center_x=0, center_y=0): """ returns Hessian matrix of function d^2f/dx^2, d^f/dy^2, d^2/dxdy the calculation is performed as a numerical differential from the deflection field. Analytical relations are possible :param x: angular position (normally in units of arc seconds) :param y: angular position (normally in units of arc seconds) :param Rs: turn over point in the slope of the NFW profile in angular unit :param alpha_Rs: deflection (angular units) at projected Rs :param r_trunc: truncation radius :param e1: eccentricity component in x-direction :param e2: eccentricity component in y-direction :param center_x: center of halo (in angular units) :param center_y: center of halo (in angular units) :return: d^2f/dx^2, d^2/dxdy, d^2/dydx, d^f/dy^2 """ alpha_ra, alpha_dec = self.derivatives(x, y, Rs, alpha_Rs, r_trunc, e1, e2, center_x, center_y) diff = self._diff alpha_ra_dx, alpha_dec_dx = self.derivatives(x + diff, y, Rs, alpha_Rs, r_trunc, e1, e2, center_x, center_y) alpha_ra_dy, alpha_dec_dy = self.derivatives(x, y + diff, Rs, alpha_Rs, r_trunc, e1, e2, center_x, center_y) f_xx = (alpha_ra_dx - alpha_ra) / diff f_xy = (alpha_ra_dy - alpha_ra) / diff f_yx = (alpha_dec_dx - alpha_dec) / diff f_yy = (alpha_dec_dy - alpha_dec) / diff return f_xx, f_xy, f_yx, f_yy def mass_3d_lens(self, r, Rs, alpha_Rs, r_trunc, e1=1, e2=0): """ :param r: radius (in angular units) :param Rs: turn-over radius of NFW profile :param alpha_Rs: deflection at Rs :param r_trunc: truncation radius :param e1: eccentricity component in x-direction :param e2: eccentricity component in y-direction :return: """ return self.tnfw.mass_3d_lens(r, Rs, alpha_Rs, r_trunc) def density_lens(self, r, Rs, alpha_Rs, r_trunc, e1=1, e2=0): """ computes the density at 3d radius r given lens model parameterization. The integral in the LOS projection of this quantity results in the convergence quantity. :param r: 3d radios :param Rs: turn-over radius of NFW profile :param alpha_Rs: deflection at Rs :param r_trunc: truncation radius :param e1: eccentricity component in x-direction :param e2: eccentricity component in y-direction :return: density rho(r) """ return self.tnfw.density_lens(r, Rs, alpha_Rs, r_trunc)
def setup(self): self.interp = CoreTNFWDeflection() self.tnfw = TNFW()
def _import_class(self, lens_type, i, custom_class): if lens_type == 'SHIFT': from lenstronomy.LensModel.Profiles.alpha_shift import Shift return Shift() elif lens_type == 'SHEAR': from lenstronomy.LensModel.Profiles.shear import Shear return Shear() elif lens_type == 'CONVERGENCE': from lenstronomy.LensModel.Profiles.convergence import Convergence return Convergence() elif lens_type == 'FLEXION': from lenstronomy.LensModel.Profiles.flexion import Flexion return Flexion() elif lens_type == 'POINT_MASS': from lenstronomy.LensModel.Profiles.point_mass import PointMass return PointMass() elif lens_type == 'SIS': from lenstronomy.LensModel.Profiles.sis import SIS return SIS() elif lens_type == 'SIS_TRUNCATED': from lenstronomy.LensModel.Profiles.sis_truncate import SIS_truncate return SIS_truncate() elif lens_type == 'SIE': from lenstronomy.LensModel.Profiles.sie import SIE return SIE() elif lens_type == 'SPP': from lenstronomy.LensModel.Profiles.spp import SPP return SPP() elif lens_type == 'NIE': from lenstronomy.LensModel.Profiles.nie import NIE return NIE() elif lens_type == 'NIE_SIMPLE': from lenstronomy.LensModel.Profiles.nie import NIE_simple return NIE_simple() elif lens_type == 'CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import Chameleon return Chameleon() elif lens_type == 'DOUBLE_CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import DoubleChameleon return DoubleChameleon() elif lens_type == 'SPEP': from lenstronomy.LensModel.Profiles.spep import SPEP return SPEP() elif lens_type == 'SPEMD': from lenstronomy.LensModel.Profiles.spemd import SPEMD return SPEMD() elif lens_type == 'SPEMD_SMOOTH': from lenstronomy.LensModel.Profiles.spemd_smooth import SPEMD_SMOOTH return SPEMD_SMOOTH() elif lens_type == 'NFW': from lenstronomy.LensModel.Profiles.nfw import NFW return NFW() elif lens_type == 'NFW_ELLIPSE': from lenstronomy.LensModel.Profiles.nfw_ellipse import NFW_ELLIPSE return NFW_ELLIPSE() elif lens_type == 'TNFW': from lenstronomy.LensModel.Profiles.tnfw import TNFW return TNFW() elif lens_type == 'CNFW': from lenstronomy.LensModel.Profiles.cnfw import CNFW return CNFW() elif lens_type == 'SERSIC': from lenstronomy.LensModel.Profiles.sersic import Sersic return Sersic() elif lens_type == 'SERSIC_ELLIPSE': from lenstronomy.LensModel.Profiles.sersic_ellipse import SersicEllipse return SersicEllipse() elif lens_type == 'PJAFFE': from lenstronomy.LensModel.Profiles.p_jaffe import PJaffe return PJaffe() elif lens_type == 'PJAFFE_ELLIPSE': from lenstronomy.LensModel.Profiles.p_jaffe_ellipse import PJaffe_Ellipse return PJaffe_Ellipse() elif lens_type == 'HERNQUIST': from lenstronomy.LensModel.Profiles.hernquist import Hernquist return Hernquist() elif lens_type == 'HERNQUIST_ELLIPSE': from lenstronomy.LensModel.Profiles.hernquist_ellipse import Hernquist_Ellipse return Hernquist_Ellipse() elif lens_type == 'GAUSSIAN': from lenstronomy.LensModel.Profiles.gaussian_potential import Gaussian return Gaussian() elif lens_type == 'GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.gaussian_kappa import GaussianKappa return GaussianKappa() elif lens_type == 'GAUSSIAN_KAPPA_ELLIPSE': from lenstronomy.LensModel.Profiles.gaussian_kappa_ellipse import GaussianKappaEllipse return GaussianKappaEllipse() elif lens_type == 'MULTI_GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappa return MultiGaussianKappa() elif lens_type == 'MULTI_GAUSSIAN_KAPPA_ELLIPSE': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappaEllipse return MultiGaussianKappaEllipse() elif lens_type == 'INTERPOL': from lenstronomy.LensModel.Profiles.interpol import Interpol return Interpol(grid=False, min_grid_number=100) elif lens_type == 'INTERPOL_SCALED': from lenstronomy.LensModel.Profiles.interpol import InterpolScaled return InterpolScaled() elif lens_type == 'SHAPELETS_POLAR': from lenstronomy.LensModel.Profiles.shapelet_pot_polar import PolarShapelets return PolarShapelets() elif lens_type == 'SHAPELETS_CART': from lenstronomy.LensModel.Profiles.shapelet_pot_cartesian import CartShapelets return CartShapelets() elif lens_type == 'DIPOLE': from lenstronomy.LensModel.Profiles.dipole import Dipole return Dipole() elif lens_type == 'FOREGROUND_SHEAR': from lenstronomy.LensModel.Profiles.shear import Shear self._foreground_shear = True self._foreground_shear_idex = i return Shear() elif lens_type == 'coreBURKERT': from lenstronomy.LensModel.Profiles.coreBurkert import coreBurkert return coreBurkert() elif lens_type == 'NumericalAlpha': from lenstronomy.LensModel.Profiles.numerical_deflections import NumericalAlpha return NumericalAlpha(custom_class[i]) else: raise ValueError('%s is not a valid lens model' % lens_type)
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)
def _import_class(lens_type, custom_class, kwargs_interp, z_lens=None, z_source=None): """ :param lens_type: string, lens model type :param custom_class: custom class :param z_lens: lens redshift # currently only used in NFW_MC model as this is redshift dependent :param z_source: source redshift # currently only used in NFW_MC model as this is redshift dependent :param kwargs_interp: interpolation keyword arguments specifying the numerics. See description in the Interpolate() class. Only applicable for 'INTERPOL' and 'INTERPOL_SCALED' models. :return: class instance of the lens model type """ if lens_type == 'SHIFT': from lenstronomy.LensModel.Profiles.constant_shift import Shift return Shift() elif lens_type == 'NIE_POTENTIAL': from lenstronomy.LensModel.Profiles.nie_potential import NIE_POTENTIAL return NIE_POTENTIAL() elif lens_type == 'CONST_MAG': from lenstronomy.LensModel.Profiles.const_mag import ConstMag return ConstMag() elif lens_type == 'SHEAR': from lenstronomy.LensModel.Profiles.shear import Shear return Shear() elif lens_type == 'SHEAR_GAMMA_PSI': from lenstronomy.LensModel.Profiles.shear import ShearGammaPsi return ShearGammaPsi() elif lens_type == 'SHEAR_REDUCED': from lenstronomy.LensModel.Profiles.shear import ShearReduced return ShearReduced() elif lens_type == 'CONVERGENCE': from lenstronomy.LensModel.Profiles.convergence import Convergence return Convergence() elif lens_type == 'HESSIAN': from lenstronomy.LensModel.Profiles.hessian import Hessian return Hessian() elif lens_type == 'FLEXION': from lenstronomy.LensModel.Profiles.flexion import Flexion return Flexion() elif lens_type == 'FLEXIONFG': from lenstronomy.LensModel.Profiles.flexionfg import Flexionfg return Flexionfg() elif lens_type == 'POINT_MASS': from lenstronomy.LensModel.Profiles.point_mass import PointMass return PointMass() elif lens_type == 'SIS': from lenstronomy.LensModel.Profiles.sis import SIS return SIS() elif lens_type == 'SIS_TRUNCATED': from lenstronomy.LensModel.Profiles.sis_truncate import SIS_truncate return SIS_truncate() elif lens_type == 'SIE': from lenstronomy.LensModel.Profiles.sie import SIE return SIE() elif lens_type == 'SPP': from lenstronomy.LensModel.Profiles.spp import SPP return SPP() elif lens_type == 'NIE': from lenstronomy.LensModel.Profiles.nie import NIE return NIE() elif lens_type == 'NIE_SIMPLE': from lenstronomy.LensModel.Profiles.nie import NIEMajorAxis return NIEMajorAxis() elif lens_type == 'CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import Chameleon return Chameleon() elif lens_type == 'DOUBLE_CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import DoubleChameleon return DoubleChameleon() elif lens_type == 'TRIPLE_CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import TripleChameleon return TripleChameleon() elif lens_type == 'SPEP': from lenstronomy.LensModel.Profiles.spep import SPEP return SPEP() elif lens_type == 'PEMD': from lenstronomy.LensModel.Profiles.pemd import PEMD return PEMD() elif lens_type == 'SPEMD': from lenstronomy.LensModel.Profiles.spemd import SPEMD return SPEMD() elif lens_type == 'EPL': from lenstronomy.LensModel.Profiles.epl import EPL return EPL() elif lens_type == 'EPL_NUMBA': from lenstronomy.LensModel.Profiles.epl_numba import EPL_numba return EPL_numba() elif lens_type == 'SPL_CORE': from lenstronomy.LensModel.Profiles.splcore import SPLCORE return SPLCORE() elif lens_type == 'NFW': from lenstronomy.LensModel.Profiles.nfw import NFW return NFW() elif lens_type == 'NFW_ELLIPSE': from lenstronomy.LensModel.Profiles.nfw_ellipse import NFW_ELLIPSE return NFW_ELLIPSE() elif lens_type == 'NFW_ELLIPSE_GAUSS_DEC': from lenstronomy.LensModel.Profiles.gauss_decomposition import NFWEllipseGaussDec return NFWEllipseGaussDec() elif lens_type == 'NFW_ELLIPSE_CSE': from lenstronomy.LensModel.Profiles.nfw_ellipse_cse import NFW_ELLIPSE_CSE return NFW_ELLIPSE_CSE() elif lens_type == 'TNFW': from lenstronomy.LensModel.Profiles.tnfw import TNFW return TNFW() elif lens_type == 'TNFW_ELLIPSE': from lenstronomy.LensModel.Profiles.tnfw_ellipse import TNFW_ELLIPSE return TNFW_ELLIPSE() elif lens_type == 'CNFW': from lenstronomy.LensModel.Profiles.cnfw import CNFW return CNFW() elif lens_type == 'CNFW_ELLIPSE': from lenstronomy.LensModel.Profiles.cnfw_ellipse import CNFW_ELLIPSE return CNFW_ELLIPSE() elif lens_type == 'CTNFW_GAUSS_DEC': from lenstronomy.LensModel.Profiles.gauss_decomposition import CTNFWGaussDec return CTNFWGaussDec() elif lens_type == 'NFW_MC': from lenstronomy.LensModel.Profiles.nfw_mass_concentration import NFWMC return NFWMC(z_lens=z_lens, z_source=z_source) elif lens_type == 'SERSIC': from lenstronomy.LensModel.Profiles.sersic import Sersic return Sersic() elif lens_type == 'SERSIC_ELLIPSE_POTENTIAL': from lenstronomy.LensModel.Profiles.sersic_ellipse_potential import SersicEllipse return SersicEllipse() elif lens_type == 'SERSIC_ELLIPSE_KAPPA': from lenstronomy.LensModel.Profiles.sersic_ellipse_kappa import SersicEllipseKappa return SersicEllipseKappa() elif lens_type == 'SERSIC_ELLIPSE_GAUSS_DEC': from lenstronomy.LensModel.Profiles.gauss_decomposition import SersicEllipseGaussDec return SersicEllipseGaussDec() elif lens_type == 'PJAFFE': from lenstronomy.LensModel.Profiles.p_jaffe import PJaffe return PJaffe() elif lens_type == 'PJAFFE_ELLIPSE': from lenstronomy.LensModel.Profiles.p_jaffe_ellipse import PJaffe_Ellipse return PJaffe_Ellipse() elif lens_type == 'HERNQUIST': from lenstronomy.LensModel.Profiles.hernquist import Hernquist return Hernquist() elif lens_type == 'HERNQUIST_ELLIPSE': from lenstronomy.LensModel.Profiles.hernquist_ellipse import Hernquist_Ellipse return Hernquist_Ellipse() elif lens_type == 'HERNQUIST_ELLIPSE_CSE': from lenstronomy.LensModel.Profiles.hernquist_ellipse_cse import HernquistEllipseCSE return HernquistEllipseCSE() elif lens_type == 'GAUSSIAN': from lenstronomy.LensModel.Profiles.gaussian_potential import Gaussian return Gaussian() elif lens_type == 'GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.gaussian_kappa import GaussianKappa return GaussianKappa() elif lens_type == 'GAUSSIAN_ELLIPSE_KAPPA': from lenstronomy.LensModel.Profiles.gaussian_ellipse_kappa import GaussianEllipseKappa return GaussianEllipseKappa() elif lens_type == 'GAUSSIAN_ELLIPSE_POTENTIAL': from lenstronomy.LensModel.Profiles.gaussian_ellipse_potential import GaussianEllipsePotential return GaussianEllipsePotential() elif lens_type == 'MULTI_GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappa return MultiGaussianKappa() elif lens_type == 'MULTI_GAUSSIAN_KAPPA_ELLIPSE': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappaEllipse return MultiGaussianKappaEllipse() elif lens_type == 'INTERPOL': from lenstronomy.LensModel.Profiles.interpol import Interpol return Interpol(**kwargs_interp) elif lens_type == 'INTERPOL_SCALED': from lenstronomy.LensModel.Profiles.interpol import InterpolScaled return InterpolScaled(**kwargs_interp) elif lens_type == 'SHAPELETS_POLAR': from lenstronomy.LensModel.Profiles.shapelet_pot_polar import PolarShapelets return PolarShapelets() elif lens_type == 'SHAPELETS_CART': from lenstronomy.LensModel.Profiles.shapelet_pot_cartesian import CartShapelets return CartShapelets() elif lens_type == 'DIPOLE': from lenstronomy.LensModel.Profiles.dipole import Dipole return Dipole() elif lens_type == 'CURVED_ARC_CONST': from lenstronomy.LensModel.Profiles.curved_arc_const import CurvedArcConst return CurvedArcConst() elif lens_type == 'CURVED_ARC_CONST_MST': from lenstronomy.LensModel.Profiles.curved_arc_const import CurvedArcConstMST return CurvedArcConstMST() elif lens_type == 'CURVED_ARC_SPP': from lenstronomy.LensModel.Profiles.curved_arc_spp import CurvedArcSPP return CurvedArcSPP() elif lens_type == 'CURVED_ARC_SIS_MST': from lenstronomy.LensModel.Profiles.curved_arc_sis_mst import CurvedArcSISMST return CurvedArcSISMST() elif lens_type == 'CURVED_ARC_SPT': from lenstronomy.LensModel.Profiles.curved_arc_spt import CurvedArcSPT return CurvedArcSPT() elif lens_type == 'CURVED_ARC_TAN_DIFF': from lenstronomy.LensModel.Profiles.curved_arc_tan_diff import CurvedArcTanDiff return CurvedArcTanDiff() elif lens_type == 'ARC_PERT': from lenstronomy.LensModel.Profiles.arc_perturbations import ArcPerturbations return ArcPerturbations() elif lens_type == 'coreBURKERT': from lenstronomy.LensModel.Profiles.coreBurkert import CoreBurkert return CoreBurkert() elif lens_type == 'CORED_DENSITY': from lenstronomy.LensModel.Profiles.cored_density import CoredDensity return CoredDensity() elif lens_type == 'CORED_DENSITY_2': from lenstronomy.LensModel.Profiles.cored_density_2 import CoredDensity2 return CoredDensity2() elif lens_type == 'CORED_DENSITY_EXP': from lenstronomy.LensModel.Profiles.cored_density_exp import CoredDensityExp return CoredDensityExp() elif lens_type == 'CORED_DENSITY_MST': from lenstronomy.LensModel.Profiles.cored_density_mst import CoredDensityMST return CoredDensityMST(profile_type='CORED_DENSITY') elif lens_type == 'CORED_DENSITY_2_MST': from lenstronomy.LensModel.Profiles.cored_density_mst import CoredDensityMST return CoredDensityMST(profile_type='CORED_DENSITY_2') elif lens_type == 'CORED_DENSITY_EXP_MST': from lenstronomy.LensModel.Profiles.cored_density_mst import CoredDensityMST return CoredDensityMST(profile_type='CORED_DENSITY_EXP') elif lens_type == 'NumericalAlpha': from lenstronomy.LensModel.Profiles.numerical_deflections import NumericalAlpha return NumericalAlpha(custom_class) elif lens_type == 'MULTIPOLE': from lenstronomy.LensModel.Profiles.multipole import Multipole return Multipole() elif lens_type == 'CSE': from lenstronomy.LensModel.Profiles.cored_steep_ellipsoid import CSE return CSE() elif lens_type == 'ElliSLICE': from lenstronomy.LensModel.Profiles.elliptical_density_slice import ElliSLICE return ElliSLICE() elif lens_type == 'ULDM': from lenstronomy.LensModel.Profiles.uldm import Uldm return Uldm() elif lens_type == 'CORED_DENSITY_ULDM_MST': from lenstronomy.LensModel.Profiles.cored_density_mst import CoredDensityMST return CoredDensityMST(profile_type='CORED_DENSITY_ULDM') else: raise ValueError( '%s is not a valid lens model. Supported are: %s.' % (lens_type, _SUPPORTED_MODELS))
def _deflection_function(self, x, y, rs, r_core, r_trunc, norm): tnfw = TNFW() return tnfw.derivatives(x, y, rs, norm, r_trunc)
def setup(self): self.tnfw = TNFW() self.nfw_e = NFW_ELLIPSE() self.tnfw_e = TNFW_ELLIPSE()
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)