def __init__(self): """ :param interpol: bool, if True, interpolates the functions F(), g() and h() """ self._nfw = NFW() super(CNFW, self).__init__()
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)
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)
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 __init__(self, interpol=False, num_interp_X=1000, max_interp_X=10): """ :param interpol: bool, if True, interpolates the functions F(), g() and h() :param num_interp_X: int (only considered if interpol=True), number of interpolation elements in units of r/r_s :param max_interp_X: float (only considered if interpol=True), maximum r/r_s value to be interpolated (returning zeros outside) """ self.nfw = NFW(interpol=interpol, num_interp_X=num_interp_X, max_interp_X=max_interp_X) self._diff = 0.0000000001 super(NFW_ELLIPSE, self).__init__()
def test_profiles_nfw(plot=True): #x, y = np.linspace(-0.5, .5, 200), np.linspace(-0.5, 0.5, 200) x = np.loadtxt('xvalues.txt') y = np.linspace(0,0,len(x)) #xx, yy = np.meshgrid(x, y) from MagniPy.MassModels.NFW import NFW from lenstronomy.LensModel.Profiles.nfw import NFW as NFW_L from MagniPy.MassModels.nfwT_temp import NFWt from MagniPy.MassModels.TNFW import TNFW import matplotlib.pyplot as plt rt = 1000 fname = 'nfwdef_rt1000.txt' nfw = NFW() nfwL = NFW_L() TNFW = TNFW() nfwTL = NFWt() # nfw_params,nfw_params_L = nfw.params(x=0,y=0,mass=mass,mhm=mhm) # nfwt_params,nfwt_params_L = nfwT.params(x=0,y=0,mass=mass,mhm=mhm,trunc=rt) ks, rs = 0.1, 0.1 theta_rs = 4 * ks * rs * (1 + np.log(.5)) xdef, ydef = nfw.def_angle(x, y, center_x=0, center_y=0, theta_Rs=theta_rs, Rs=rs) xdef_t, ydef_t = TNFW.def_angle(x, y, center_x=0, center_y=0, theta_Rs=theta_rs, Rs=rs, r_trunc=rt) xdef_L, ydef_L = nfwL.derivatives(x=x, y=y, Rs=rs, theta_Rs=theta_rs) xdef_Lt, ydef_Lt = nfwTL.derivatives(x=x, y=y, Rs=rs, theta_Rs=theta_rs, t=rt) gravlens_xdef = np.loadtxt(fname) # xdef,ydef = nfw.def_angle(xx,yy,0,0,rs,ks) # xdef_L,ydef_L = nfwL.derivatives(x=xx,y=yy,**nfw_params_L) if plot: colors=['r','k','b','g'] labels = ['my nfw','lenstronomy nfw','my nfw rt=1000rs','lensmodel'] angles = [xdef,xdef_L,xdef_t,gravlens_xdef] for i,deflection in enumerate(angles[:-1]): plt.plot(x,deflection,color=colors[i],label=labels[i],alpha=0.5) plt.scatter(x, deflection, color=colors[i], label=labels[i], alpha=0.5) plt.plot(x,gravlens_xdef,color=colors[-1],label=labels[-1],alpha=0.5) plt.scatter(x, gravlens_xdef, color=colors[-1], label=labels[-1], alpha=0.5) plt.legend() plt.show() else: np.testing.assert_almost_equal(xdef, xdef_t, decimal=8) np.testing.assert_almost_equal(xdef, xdef_Lt, decimal=8) np.testing.assert_almost_equal(xdef, gravlens_xdef, decimal=8) np.testing.assert_almost_equal(xdef, xdef_L, decimal=8)
def __init__(self, high_accuracy=True): """ :param high_accuracy: boolean, if True uses a more accurate larger set of CSE profiles (see Oguri 2021) """ self.cse_major_axis_set = CSEMajorAxisSet() self.nfw = NFW() if high_accuracy is True: # Table 1 in Oguri 2021 self._s_list = [ 1.082411e-06, 8.786566e-06, 3.292868e-06, 1.860019e-05, 3.274231e-05, 6.232485e-05, 9.256333e-05, 1.546762e-04, 2.097321e-04, 3.391140e-04, 5.178790e-04, 8.636736e-04, 1.405152e-03, 2.193855e-03, 3.179572e-03, 4.970987e-03, 7.631970e-03, 1.119413e-02, 1.827267e-02, 2.945251e-02, 4.562723e-02, 6.782509e-02, 1.596987e-01, 1.127751e-01, 2.169469e-01, 3.423835e-01, 5.194527e-01, 8.623185e-01, 1.382737e+00, 2.034929e+00, 3.402979e+00, 5.594276e+00, 8.052345e+00, 1.349045e+01, 2.603825e+01, 4.736823e+01, 6.559320e+01, 1.087932e+02, 1.477673e+02, 2.495341e+02, 4.305999e+02, 7.760206e+02, 2.143057e+03, 1.935749e+03 ] self._a_list = [ 1.648988e-18, 6.274458e-16, 3.646620e-17, 3.459206e-15, 2.457389e-14, 1.059319e-13, 4.211597e-13, 1.142832e-12, 4.391215e-12, 1.556500e-11, 6.951271e-11, 3.147466e-10, 1.379109e-09, 3.829778e-09, 1.384858e-08, 5.370951e-08, 1.804384e-07, 5.788608e-07, 3.205256e-06, 1.102422e-05, 4.093971e-05, 1.282206e-04, 4.575541e-04, 7.995270e-04, 5.013701e-03, 1.403508e-02, 5.230727e-02, 1.898907e-01, 3.643448e-01, 7.203734e-01, 1.717667e+00, 2.217566e+00, 3.187447e+00, 8.194898e+00, 1.765210e+01, 1.974319e+01, 2.783688e+01, 4.482311e+01, 5.598897e+01, 1.426485e+02, 2.279833e+02, 5.401335e+02, 9.743682e+02, 1.775124e+03 ] else: # Table 3 in Oguri 2021 self._a_list = [ 1.434960e-16, 5.232413e-14, 2.666660e-12, 7.961761e-11, 2.306895e-09, 6.742968e-08, 1.991691e-06, 5.904388e-05, 1.693069e-03, 4.039850e-02, 5.665072e-01, 3.683242e+00, 1.582481e+01, 6.340984e+01, 2.576763e+02, 1.422619e+03 ] self._s_list = [ 4.041628e-06, 3.086267e-05, 1.298542e-04, 4.131977e-04, 1.271373e-03, 3.912641e-03, 1.208331e-02, 3.740521e-02, 1.153247e-01, 3.472038e-01, 1.017550e+00, 3.253031e+00, 1.190315e+01, 4.627701e+01, 1.842613e+02, 8.206569e+02 ] super(NFW_ELLIPSE_CSE, self).__init__()
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 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)
def rho02alpha(rho0, Rs): """ convert rho0 to angle at Rs; neglects the truncation :param rho0: density normalization (characteristic density) :param Rs: scale radius :return: deflection angle at RS """ return NFW.rho02alpha(rho0, Rs)
def alpha2rho0(alpha_Rs, Rs): """ convert angle at Rs into rho0; neglects the truncation :param alpha_Rs: deflection angle at RS :param Rs: scale radius :return: density normalization (characteristic density) """ return NFW.alpha2rho0(alpha_Rs, Rs)
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) values = nfw.function(x, y, Rs, alpha_Rs) values_interp = nfw_interp.function(x, y, Rs, alpha_Rs) npt.assert_almost_equal(values, values_interp, decimal=4) values = nfw.derivatives(x, y, Rs, alpha_Rs) values_interp = nfw_interp.derivatives(x, y, Rs, alpha_Rs) npt.assert_almost_equal(values, values_interp, decimal=4) values = nfw.hessian(x, y, Rs, alpha_Rs) values_interp = nfw_interp.hessian(x, y, Rs, alpha_Rs) npt.assert_almost_equal(values, values_interp, decimal=4)
class TestMassAngleConversion(object): """ test angular to mass unit conversions """ def setup(self): self.nfw = NFW() self.nfw_ellipse = NFW_ELLIPSE() def test_angle(self): x, y = 1, 0 alpha1, alpha2 = self.nfw.derivatives(x, y, alpha_Rs=1., Rs=1.) assert alpha1 == 1. def test_convertAngle2rho(self): rho0 = self.nfw._alpha2rho0(alpha_Rs=1., Rs=1.) assert rho0 == 0.81472283831773229 def test_convertrho02angle(self): alpha_Rs_in = 1.5 Rs = 1.5 rho0 = self.nfw._alpha2rho0(alpha_Rs=alpha_Rs_in, Rs=Rs) alpha_Rs_out = self.nfw._rho02alpha(rho0, Rs) assert alpha_Rs_in == alpha_Rs_out
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)
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)
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)
def setup(self): self.nfw = NFW() self.nfw_e = NFW_ELLIPSE()
def setup(self): self.numerical_alpha = NumericalAlpha(custom_class=TestClass()) self.nfw = NFW()
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
def test_all_nfw(self): lensModel = LensModel(['SPEP']) solver_nfw_ellipse = Solver2Point(lensModel, solver_type='ELLIPSE') solver_nfw_center = Solver2Point(lensModel, solver_type='CENTER') spep = LensModel(['SPEP']) image_position_nfw = LensEquationSolver(LensModel(['SPEP', 'NFW'])) sourcePos_x = 0.1 sourcePos_y = 0.03 deltapix = 0.05 numPix = 100 gamma = 1.9 Rs = 0.1 nfw = NFW() alpha_Rs = nfw._rho02alpha(1., Rs) phi_G, q = 0.5, 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) kwargs_lens = [{ 'theta_E': 1., 'gamma': gamma, 'e1': e1, 'e2': e2, 'center_x': 0.1, 'center_y': -0.1 }, { 'Rs': Rs, 'alpha_Rs': alpha_Rs, 'center_x': -0.5, 'center_y': 0.5 }] x_pos, y_pos = image_position_nfw.findBrightImage( sourcePos_x, sourcePos_y, kwargs_lens, numImages=2, min_distance=deltapix, search_window=numPix * deltapix) print(len(x_pos), 'number of images') x_pos = x_pos[:2] y_pos = y_pos[:2] kwargs_init = [{ 'theta_E': 1, 'gamma': gamma, 'e1': e1, 'e2': e2, 'center_x': 0., 'center_y': 0 }, { 'Rs': Rs, 'alpha_Rs': alpha_Rs, 'center_x': -0.5, 'center_y': 0.5 }] kwargs_out_center, precision = solver_nfw_center.constraint_lensmodel( x_pos, y_pos, kwargs_init) source_x, source_y = spep.ray_shooting(x_pos[0], y_pos[0], kwargs_out_center) x_pos_new, y_pos_new = image_position_nfw.findBrightImage( source_x, source_y, kwargs_out_center, numImages=2, min_distance=deltapix, search_window=numPix * deltapix) print(kwargs_out_center, 'kwargs_out_center') npt.assert_almost_equal(x_pos_new[0], x_pos[0], decimal=2) npt.assert_almost_equal(y_pos_new[0], y_pos[0], decimal=2) npt.assert_almost_equal(kwargs_out_center[0]['center_x'], kwargs_lens[0]['center_x'], decimal=2) npt.assert_almost_equal(kwargs_out_center[0]['center_y'], kwargs_lens[0]['center_y'], decimal=2) npt.assert_almost_equal(kwargs_out_center[0]['center_y'], -0.1, decimal=2) kwargs_init = [{ 'theta_E': 1., 'gamma': gamma, 'e1': 0, 'e2': 0, 'center_x': 0.1, 'center_y': -0.1 }, { 'Rs': Rs, 'alpha_Rs': alpha_Rs, 'center_x': -0.5, 'center_y': 0.5 }] kwargs_out_ellipse, precision = solver_nfw_ellipse.constraint_lensmodel( x_pos, y_pos, kwargs_init) npt.assert_almost_equal(kwargs_out_ellipse[0]['e1'], kwargs_lens[0]['e1'], decimal=2) npt.assert_almost_equal(kwargs_out_ellipse[0]['e2'], kwargs_lens[0]['e2'], decimal=2) npt.assert_almost_equal(kwargs_out_ellipse[0]['e1'], e1, decimal=2)
def __init__(self, interpol=False, num_interp_X=1000, max_interp_X=10): self.nfw = NFW(interpol=interpol, num_interp_X=num_interp_X, max_interp_X=max_interp_X) self._diff = 0.0000000001
def setup(self): self.nfw = NFW()
def __init__(self, interpol=False, num_interp_X=1000, max_interp_X=10): self.nfw = NFW(interpol=interpol, num_interp_X=num_interp_X, max_interp_X=max_interp_X) self._diff = 0.0000000001 super(NFW_ELLIPSE, self).__init__()
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 NFW_ELLIPSE(LensProfileBase): """ this class contains functions concerning the NFW profile with an ellipticity defined in the potential parameterization of alpha_Rs and Rs 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 = 'NFW_ELLIPSE' param_names = ['Rs', 'alpha_Rs', 'e1', 'e2', 'center_x', 'center_y'] lower_limit_default = { 'Rs': 0, 'alpha_Rs': 0, 'e1': -0.5, 'e2': -0.5, 'center_x': -100, 'center_y': -100 } upper_limit_default = { 'Rs': 100, 'alpha_Rs': 10, 'e1': 0.5, 'e2': 0.5, 'center_x': 100, 'center_y': 100 } def __init__(self, interpol=False, num_interp_X=1000, max_interp_X=10): """ :param interpol: bool, if True, interpolates the functions F(), g() and h() :param num_interp_X: int (only considered if interpol=True), number of interpolation elements in units of r/r_s :param max_interp_X: float (only considered if interpol=True), maximum r/r_s value to be interpolated (returning zeros outside) """ self.nfw = NFW(interpol=interpol, num_interp_X=num_interp_X, max_interp_X=max_interp_X) self._diff = 0.0000000001 super(NFW_ELLIPSE, self).__init__() def function(self, x, y, Rs, alpha_Rs, 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 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.nfw.alpha2rho0(alpha_Rs=alpha_Rs, Rs=Rs) if Rs < 0.0000001: Rs = 0.0000001 f_ = self.nfw.nfwPot(R_, Rs, rho0_input) return f_ def derivatives(self, x, y, Rs, alpha_Rs, 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 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 = abs(1 - q) R_ = np.sqrt(x_**2 + y_**2) rho0_input = self.nfw.alpha2rho0(alpha_Rs=alpha_Rs, Rs=Rs) if Rs < 0.0000001: Rs = 0.0000001 f_x_prim, f_y_prim = self.nfw.nfwAlpha(R_, Rs, rho0_input, 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, 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 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, e1, e2, center_x, center_y) diff = self._diff alpha_ra_dx, alpha_dec_dx = self.derivatives(x + diff, y, Rs, alpha_Rs, e1, e2, center_x, center_y) alpha_ra_dy, alpha_dec_dy = self.derivatives(x, y + diff, Rs, alpha_Rs, 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, e1=1, e2=0): """ :param R: radius (in angular units) :param Rs: :param alpha_Rs: :param e1: :param e2: :return: """ return self.nfw.mass_3d_lens(R, Rs, alpha_Rs) def density_lens(self, r, Rs, alpha_Rs, 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 :return: density rho(r) """ return self.nfw.density_lens(r, Rs, alpha_Rs)
def setup(self): self.nfw = NFW() self.tnfw = TNFW()
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 NFW_ELLIPSE(object): """ this class contains functions concerning the NFW profile relation are: R_200 = c * Rs """ param_names = ['Rs', 'alpha_Rs', 'e1', 'e2', 'center_x', 'center_y'] lower_limit_default = { 'Rs': 0, 'alpha_Rs': 0, 'e1': -0.5, 'e2': -0.5, 'center_x': -100, 'center_y': -100 } upper_limit_default = { 'Rs': 100, 'alpha_Rs': 10, 'e1': 0.5, 'e2': 0.5, 'center_x': 100, 'center_y': 100 } def __init__(self, interpol=False, num_interp_X=1000, max_interp_X=10): self.nfw = NFW(interpol=interpol, num_interp_X=num_interp_X, max_interp_X=max_interp_X) self._diff = 0.0000000001 def function(self, x, y, Rs, alpha_Rs, e1, e2, center_x=0, center_y=0): """ returns double integral of NFW profile """ phi_G, q = param_util.ellipticity2phi_q(e1, e2) x_shift = x - center_x y_shift = y - center_y cos_phi = np.cos(phi_G) sin_phi = np.sin(phi_G) e = min(abs(1. - q), 0.99) xt1 = (cos_phi * x_shift + sin_phi * y_shift) * np.sqrt(1 - e) xt2 = (-sin_phi * x_shift + cos_phi * y_shift) * np.sqrt(1 + e) R_ = np.sqrt(xt1**2 + xt2**2) rho0_input = self.nfw._alpha2rho0(alpha_Rs=alpha_Rs, Rs=Rs) if Rs < 0.0000001: Rs = 0.0000001 f_ = self.nfw.nfwPot(R_, Rs, rho0_input) return f_ def derivatives(self, x, y, Rs, alpha_Rs, e1, e2, center_x=0, center_y=0): """ returns df/dx and df/dy of the function (integral of NFW) """ phi_G, q = param_util.ellipticity2phi_q(e1, e2) x_shift = x - center_x y_shift = y - center_y cos_phi = np.cos(phi_G) sin_phi = np.sin(phi_G) e = min(abs(1. - q), 0.99) xt1 = (cos_phi * x_shift + sin_phi * y_shift) * np.sqrt(1 - e) xt2 = (-sin_phi * x_shift + cos_phi * y_shift) * np.sqrt(1 + e) R_ = np.sqrt(xt1**2 + xt2**2) rho0_input = self.nfw._alpha2rho0(alpha_Rs=alpha_Rs, Rs=Rs) if Rs < 0.0000001: Rs = 0.0000001 f_x_prim, f_y_prim = self.nfw.nfwAlpha(R_, Rs, rho0_input, xt1, xt2) 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, e1, e2, center_x=0, center_y=0): """ returns Hessian matrix of function d^2f/dx^2, d^f/dy^2, d^2/dxdy """ alpha_ra, alpha_dec = self.derivatives(x, y, Rs, alpha_Rs, e1, e2, center_x, center_y) diff = self._diff alpha_ra_dx, alpha_dec_dx = self.derivatives(x + diff, y, Rs, alpha_Rs, e1, e2, center_x, center_y) alpha_ra_dy, alpha_dec_dy = self.derivatives(x, y + diff, Rs, alpha_Rs, 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_yy, f_xy def mass_3d_lens(self, R, Rs, alpha_Rs, e1=1, e2=0): """ :param R: :param Rs: :param alpha_Rs: :param q: :param phi_G: :return: """ return self.nfw.mass_3d(R, Rs, alpha_Rs)
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)