class SPEP(LensProfileBase): """ class for Softened power-law elliptical potential (SPEP) """ param_names = ['theta_E', 'gamma', 'e1', 'e2', 'center_x', 'center_y'] lower_limit_default = { 'theta_E': 0, 'gamma': 0, 'e1': -0.5, 'e2': -0.5, 'center_x': -100, 'center_y': -100 } upper_limit_default = { 'theta_E': 100, 'gamma': 100, 'e1': 0.5, 'e2': 0.5, 'center_x': 100, 'center_y': 100 } def __init__(self): self.spp = SPP() super(SPEP, self).__init__() def function(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param x: set of x-coordinates :type x: array of size (n) :param theta_E: Einstein radius of lense :type theta_E: float. :param gamma: power law slope of mass profifle :type gamma: <2 float :param e1: eccentricity :type e1: -1<e1<1 :param e2: eccentricity :type e2: -1<e1<1 :returns: function :raises: AttributeError, KeyError """ phi_G, q = param_util.ellipticity2phi_q(e1, e2) gamma, q = self._param_bounds(gamma, q) theta_E *= q x_shift = x - center_x y_shift = y - center_y E = theta_E / (((3 - gamma) / 2.)**(1. / (1 - gamma)) * np.sqrt(q)) #E = phi_E eta = -gamma + 3 xt1 = np.cos(phi_G) * x_shift + np.sin(phi_G) * y_shift xt2 = -np.sin(phi_G) * x_shift + np.cos(phi_G) * y_shift p2 = xt1**2 + xt2**2 / q**2 s2 = 0. # softening return 2 * E**2 / eta**2 * ((p2 + s2) / E**2)**(eta / 2) def derivatives(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): phi_G, q = param_util.ellipticity2phi_q(e1, e2) gamma, q = self._param_bounds(gamma, q) phi_E_new = theta_E * q x_shift = x - center_x y_shift = y - center_y E = phi_E_new / (((3 - gamma) / 2.)**(1. / (1 - gamma)) * np.sqrt(q)) # E = phi_E eta = float(-gamma + 3) cos_phi = np.cos(phi_G) sin_phi = np.sin(phi_G) xt1 = cos_phi * x_shift + sin_phi * y_shift xt2 = -sin_phi * x_shift + cos_phi * y_shift xt2difq2 = xt2 / (q * q) P2 = xt1 * xt1 + xt2 * xt2difq2 if isinstance(P2, int) or isinstance(P2, float): a = max(0.000001, P2) else: a = np.empty_like(P2) p2 = P2[P2 > 0] #in the SIS regime a[P2 == 0] = 0.000001 a[P2 > 0] = p2 fac = 1. / eta * (a / (E * E))**(eta / 2 - 1) * 2 f_x_prim = fac * xt1 f_y_prim = fac * xt2difq2 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, theta_E, gamma, e1, e2, center_x=0, center_y=0): phi_G, q = param_util.ellipticity2phi_q(e1, e2) gamma, q = self._param_bounds(gamma, q) phi_E_new = theta_E * q #x_shift = x - center_x #y_shift = y - center_y # shift x_ = x - center_x y_ = y - center_y # rotate x__, y__ = util.rotate(x_, y_, phi_G) E = phi_E_new / (((3 - gamma) / 2.)**(1. / (1 - gamma)) * np.sqrt(q)) if E <= 0: return np.zeros_like(x), np.zeros_like(x), np.zeros_like( x), np.zeros_like(x) # E = phi_E eta = float(-gamma + 3) #xt1 = np.cos(phi_G)*x_shift+np.sin(phi_G)*y_shift #xt2 = -np.sin(phi_G)*x_shift+np.cos(phi_G)*y_shift xt1, xt2 = x__, y__ P2 = xt1**2 + xt2**2 / q**2 if isinstance(P2, int) or isinstance(P2, float): a = max(0.000001, P2) else: a = np.empty_like(P2) p2 = P2[P2 > 0] #in the SIS regime a[P2 == 0] = 0.000001 a[P2 > 0] = p2 s2 = 0. # softening kappa = 1. / eta * (a / E**2)**(eta / 2 - 1) * ( (eta - 2) * (xt1**2 + xt2**2 / q**4) / a + (1 + 1 / q**2)) gamma1_value = 1. / eta * (a / E**2)**( eta / 2 - 1) * (1 - 1 / q**2 + (eta / 2 - 1) * (2 * xt1**2 - 2 * xt2**2 / q**4) / a) gamma2_value = 4 * xt1 * xt2 / q**2 * (1. / 2 - 1 / eta) * ( a / E**2)**(eta / 2 - 2) / E**2 gamma1 = np.cos(2 * phi_G) * gamma1_value - np.sin( 2 * phi_G) * gamma2_value gamma2 = +np.sin(2 * phi_G) * gamma1_value + np.cos( 2 * phi_G) * gamma2_value f_xx = kappa + gamma1 f_yy = kappa - gamma1 f_xy = gamma2 return f_xx, f_xy, f_xy, f_yy def mass_3d_lens(self, r, theta_E, gamma, e1=None, e2=None): """ computes the spherical power-law mass enclosed (with SPP routine) :param r: radius within the mass is computed :param theta_E: Einstein radius :param gamma: power-law slope :param e1: eccentricity component (not used) :param e2: eccentricity component (not used) :return: mass enclosed a 3D radius r """ return self.spp.mass_3d_lens(r, theta_E, gamma) def density_lens(self, r, theta_E, gamma, e1=None, e2=None): """ 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: radius within the mass is computed :param theta_E: Einstein radius :param gamma: power-law slope :param e1: eccentricity component (not used) :param e2: eccentricity component (not used) :return: mass enclosed a 3D radius r """ return self.spp.density_lens(r, theta_E, gamma) @staticmethod def _param_bounds(gamma, q): """ bounds parameters :param gamma: :param q: :return: """ if gamma < 1.4: gamma = 1.4 if gamma > 2.9: gamma = 2.9 if q < 0.01: q = 0.01 return float(gamma), q
class PEMD(LensProfileBase): """ class for power law ellipse mass density profile. This class effectively calls the class SPEMD_SMOOTH with a fixed and very small central smoothing scale to perform the numerical integral using the FASTELL code by Renan Barkana. The Einstein ring parameter converts to the definition used by GRAVLENS as follow: (theta_E / theta_E_gravlens) = sqrt[ (1+q^2) / (2 q) ] """ param_names = ['theta_E', 'gamma', 'e1', 'e2', 'center_x', 'center_y'] lower_limit_default = { 'theta_E': 0, 'gamma': 1.5, 'e1': -0.5, 'e2': -0.5, 'center_x': -100, 'center_y': -100 } upper_limit_default = { 'theta_E': 100, 'gamma': 2.5, 'e1': 0.5, 'e2': 0.5, 'center_x': 100, 'center_y': 100 } def __init__(self, suppress_fastell=False): """ :param suppress_fastell: bool, if True, does not raise if fastell4py is not installed """ self._s_scale = 0.0001 # smoothing scale as used to numerically compute a power-law profile self.spp = SPP() self.spemd_smooth = SPEMD(suppress_fastell=suppress_fastell) super(PEMD, self).__init__() def function(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param x: x-coordinate (angle) :param y: y-coordinate (angle) :param theta_E: Einstein radius (angle), pay attention to specific definition! :param gamma: logarithmic slope of the power-law profile. gamma=2 corresponds to isothermal :param e1: eccentricity component :param e2: eccentricity component :param center_x: x-position of lens center :param center_y: y-position of lens center :return: lensing potential """ return self.spemd_smooth.function(x, y, theta_E, gamma, e1, e2, self._s_scale, center_x, center_y) def derivatives(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param x: x-coordinate (angle) :param y: y-coordinate (angle) :param theta_E: Einstein radius (angle), pay attention to specific definition! :param gamma: logarithmic slope of the power-law profile. gamma=2 corresponds to isothermal :param e1: eccentricity component :param e2: eccentricity component :param center_x: x-position of lens center :param center_y: y-position of lens center :return: deflection angles alpha_x, alpha_y """ return self.spemd_smooth.derivatives(x, y, theta_E, gamma, e1, e2, self._s_scale, center_x, center_y) def hessian(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param x: x-coordinate (angle) :param y: y-coordinate (angle) :param theta_E: Einstein radius (angle), pay attention to specific definition! :param gamma: logarithmic slope of the power-law profile. gamma=2 corresponds to isothermal :param e1: eccentricity component :param e2: eccentricity component :param center_x: x-position of lens center :param center_y: y-position of lens center :return: Hessian components f_xx, f_yy, f_xy """ return self.spemd_smooth.hessian(x, y, theta_E, gamma, e1, e2, self._s_scale, center_x, center_y) def mass_3d_lens(self, r, theta_E, gamma, e1=None, e2=None): """ computes the spherical power-law mass enclosed (with SPP routine) :param r: radius within the mass is computed :param theta_E: Einstein radius :param gamma: power-law slope :param e1: eccentricity component (not used) :param e2: eccentricity component (not used) :return: mass enclosed a 3D radius r """ return self.spp.mass_3d_lens(r, theta_E, gamma) def density_lens(self, r, theta_E, gamma, e1=None, e2=None): """ 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: radius within the mass is computed :param theta_E: Einstein radius :param gamma: power-law slope :param e1: eccentricity component (not used) :param e2: eccentricity component (not used) :return: mass enclosed a 3D radius r """ return self.spp.density_lens(r, theta_E, gamma)
class PEMD(LensProfileBase): """ class for power law ellipse mass density profile. This class effectively calls the class SPEMD_SMOOTH with a fixed and very small central smoothing scale to perform the numerical integral using the FASTELL code by Renan Barkana. .. math:: \\kappa(x, y) = \\frac{3-\\gamma}{2} \\left(\\frac{\\theta_{E}}{\\sqrt{q x^2 + y^2/q}} \\right)^{\\gamma-1} with :math:`\\theta_{E}` is the (circularized) Einstein radius, :math:`\\gamma` is the negative power-law slope of the 3D mass distributions, :math:`q` is the minor/major axis ratio, and :math:`x` and :math:`y` are defined in a coordinate system aligned with the major and minor axis of the lens. In terms of eccentricities, this profile is defined as .. math:: \\kappa(r) = \\frac{3-\\gamma}{2} \\left(\\frac{\\theta'_{E}}{r \\sqrt{1 − e*\\cos(2*\\phi)}} \\right)^{\\gamma-1} with :math:`\\epsilon` is the ellipticity defined as .. math:: \\epsilon = \\frac{1-q^2}{1+q^2} And an Einstein radius :math:`\\theta'_{\\rm E}` related to the definition used is .. math:: \\left(\\frac{\\theta'_{\\rm E}}{\\theta_{\\rm E}}\\right)^{2} = \\frac{2q}{1+q^2}. """ param_names = ['theta_E', 'gamma', 'e1', 'e2', 'center_x', 'center_y'] lower_limit_default = { 'theta_E': 0, 'gamma': 1.5, 'e1': -0.5, 'e2': -0.5, 'center_x': -100, 'center_y': -100 } upper_limit_default = { 'theta_E': 100, 'gamma': 2.5, 'e1': 0.5, 'e2': 0.5, 'center_x': 100, 'center_y': 100 } def __init__(self, suppress_fastell=False): """ :param suppress_fastell: bool, if True, does not raise if fastell4py is not installed """ self._s_scale = 0.0000001 # smoothing scale as used to numerically compute a power-law profile self.spp = SPP() self.spemd_smooth = SPEMD(suppress_fastell=suppress_fastell) super(PEMD, self).__init__() def function(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param x: x-coordinate (angle) :param y: y-coordinate (angle) :param theta_E: Einstein radius (angle), pay attention to specific definition! :param gamma: logarithmic slope of the power-law profile. gamma=2 corresponds to isothermal :param e1: eccentricity component :param e2: eccentricity component :param center_x: x-position of lens center :param center_y: y-position of lens center :return: lensing potential """ return self.spemd_smooth.function(x, y, theta_E, gamma, e1, e2, self._s_scale, center_x, center_y) def derivatives(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param x: x-coordinate (angle) :param y: y-coordinate (angle) :param theta_E: Einstein radius (angle), pay attention to specific definition! :param gamma: logarithmic slope of the power-law profile. gamma=2 corresponds to isothermal :param e1: eccentricity component :param e2: eccentricity component :param center_x: x-position of lens center :param center_y: y-position of lens center :return: deflection angles alpha_x, alpha_y """ return self.spemd_smooth.derivatives(x, y, theta_E, gamma, e1, e2, self._s_scale, center_x, center_y) def hessian(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param x: x-coordinate (angle) :param y: y-coordinate (angle) :param theta_E: Einstein radius (angle), pay attention to specific definition! :param gamma: logarithmic slope of the power-law profile. gamma=2 corresponds to isothermal :param e1: eccentricity component :param e2: eccentricity component :param center_x: x-position of lens center :param center_y: y-position of lens center :return: Hessian components f_xx, f_xy, f_yx, f_yy """ return self.spemd_smooth.hessian(x, y, theta_E, gamma, e1, e2, self._s_scale, center_x, center_y) def mass_3d_lens(self, r, theta_E, gamma, e1=None, e2=None): """ computes the spherical power-law mass enclosed (with SPP routine) :param r: radius within the mass is computed :param theta_E: Einstein radius :param gamma: power-law slope :param e1: eccentricity component (not used) :param e2: eccentricity component (not used) :return: mass enclosed a 3D radius r """ return self.spp.mass_3d_lens(r, theta_E, gamma) def density_lens(self, r, theta_E, gamma, e1=None, e2=None): """ 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: radius within the mass is computed :param theta_E: Einstein radius :param gamma: power-law slope :param e1: eccentricity component (not used) :param e2: eccentricity component (not used) :return: mass enclosed a 3D radius r """ return self.spp.density_lens(r, theta_E, gamma)
class EPL(LensProfileBase): """" Elliptical Power Law mass profile .. math:: \\kappa(x, y) = \\frac{3-\\gamma}{2} \\left(\\frac{\\theta_{E}}{\\sqrt{q x^2 + y^2/q}} \\right)^{\\gamma-1} with :math:`\\theta_{E}` is the (circularized) Einstein radius, :math:`\\gamma` is the negative power-law slope of the 3D mass distributions, :math:`q` is the minor/major axis ratio, and :math:`x` and :math:`y` are defined in a coordinate sys- tem aligned with the major and minor axis of the lens. In terms of eccentricities, this profile is defined as .. math:: \\kappa(r) = \\frac{3-\\gamma}{2} \\left(\\frac{\\theta'_{E}}{r \\sqrt{1 − e*\\cos(2*\\phi)}} \\right)^{\\gamma-1} with :math:`\\epsilon` is the ellipticity defined as .. math:: \\epsilon = \\frac{1-q^2}{1+q^2} And an Einstein radius :math:`\\theta'_{\\rm E}` related to the definition used is .. math:: \\left(\\frac{\\theta'_{\\rm E}}{\\theta_{\\rm E}}\\right)^{2} = \\frac{2q}{1+q^2}. The mathematical form of the calculation is presented by Tessore & Metcalf (2015), https://arxiv.org/abs/1507.01819. The current implementation is using hyperbolic functions. The paper presents an iterative calculation scheme, converging in few iterations to high precision and accuracy. A (faster) implementation of the same model using numba is accessible as 'EPL_NUMBA' with the iterative calculation scheme. """ param_names = ['theta_E', 'gamma', 'e1', 'e2', 'center_x', 'center_y'] lower_limit_default = { 'theta_E': 0, 'gamma': 1.5, 'e1': -0.5, 'e2': -0.5, 'center_x': -100, 'center_y': -100 } upper_limit_default = { 'theta_E': 100, 'gamma': 2.5, 'e1': 0.5, 'e2': 0.5, 'center_x': 100, 'center_y': 100 } def __init__(self): self.epl_major_axis = EPLMajorAxis() self.spp = SPP() super(EPL, self).__init__() def param_conv(self, theta_E, gamma, e1, e2): """ converts parameters as defined in this class to the parameters used in the EPLMajorAxis() class :param theta_E: Einstein radius as defined in the profile class :param gamma: negative power-law slope :param e1: eccentricity modulus :param e2: eccentricity modulus :return: b, t, q, phi_G """ if self._static is True: return self._b_static, self._t_static, self._q_static, self._phi_G_static return self._param_conv(theta_E, gamma, e1, e2) @staticmethod def _param_conv(theta_E, gamma, e1, e2): """ convert parameters from :math:`R = \\sqrt{q x^2 + y^2/q}` to :math:`R = \\sqrt{q^2 x^2 + y^2}` :param gamma: power law slope :param theta_E: Einstein radius :param e1: eccentricity component :param e2: eccentricity component :return: critical radius b, slope t, axis ratio q, orientation angle phi_G """ t = gamma - 1 phi_G, q = param_util.ellipticity2phi_q(e1, e2) b = theta_E * np.sqrt(q) return b, t, q, phi_G def set_static(self, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param theta_E: Einstein radius :param gamma: power law slope :param e1: eccentricity component :param e2: eccentricity component :param center_x: profile center :param center_y: profile center :return: self variables set """ self._static = True self._b_static, self._t_static, self._q_static, self._phi_G_static = self._param_conv( theta_E, gamma, e1, e2) def set_dynamic(self): """ :return: """ self._static = False if hasattr(self, '_b_static'): del self._b_static if hasattr(self, '_t_static'): del self._t_static if hasattr(self, '_phi_G_static'): del self._phi_G_static if hasattr(self, '_q_static'): del self._q_static def function(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param x: x-coordinate in image plane :param y: y-coordinate in image plane :param theta_E: Einstein radius :param gamma: power law slope :param e1: eccentricity component :param e2: eccentricity component :param center_x: profile center :param center_y: profile center :return: lensing potential """ b, t, q, phi_G = self.param_conv(theta_E, gamma, e1, e2) # shift x_ = x - center_x y_ = y - center_y # rotate x__, y__ = util.rotate(x_, y_, phi_G) # evaluate f_ = self.epl_major_axis.function(x__, y__, b, t, q) # rotate back return f_ def derivatives(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param x: x-coordinate in image plane :param y: y-coordinate in image plane :param theta_E: Einstein radius :param gamma: power law slope :param e1: eccentricity component :param e2: eccentricity component :param center_x: profile center :param center_y: profile center :return: alpha_x, alpha_y """ b, t, q, phi_G = self.param_conv(theta_E, gamma, e1, e2) # shift x_ = x - center_x y_ = y - center_y # rotate x__, y__ = util.rotate(x_, y_, phi_G) # evaluate f__x, f__y = self.epl_major_axis.derivatives(x__, y__, b, t, q) # rotate back f_x, f_y = util.rotate(f__x, f__y, -phi_G) return f_x, f_y def hessian(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param x: x-coordinate in image plane :param y: y-coordinate in image plane :param theta_E: Einstein radius :param gamma: power law slope :param e1: eccentricity component :param e2: eccentricity component :param center_x: profile center :param center_y: profile center :return: f_xx, f_xy, f_yx, f_yy """ b, t, q, phi_G = self.param_conv(theta_E, gamma, e1, e2) # shift x_ = x - center_x y_ = y - center_y # rotate x__, y__ = util.rotate(x_, y_, phi_G) # evaluate f__xx, f__xy, f__yx, f__yy = self.epl_major_axis.hessian( x__, y__, b, t, q) # rotate back kappa = 1. / 2 * (f__xx + f__yy) gamma1__ = 1. / 2 * (f__xx - f__yy) gamma2__ = f__xy gamma1 = np.cos(2 * phi_G) * gamma1__ - np.sin(2 * phi_G) * gamma2__ gamma2 = +np.sin(2 * phi_G) * gamma1__ + np.cos(2 * phi_G) * gamma2__ f_xx = kappa + gamma1 f_yy = kappa - gamma1 f_xy = gamma2 return f_xx, f_xy, f_xy, f_yy def mass_3d_lens(self, r, theta_E, gamma, e1=None, e2=None): """ computes the spherical power-law mass enclosed (with SPP routine) :param r: radius within the mass is computed :param theta_E: Einstein radius :param gamma: power-law slope :param e1: eccentricity component (not used) :param e2: eccentricity component (not used) :return: mass enclosed a 3D radius r """ return self.spp.mass_3d_lens(r, theta_E, gamma) def density_lens(self, r, theta_E, gamma, e1=None, e2=None): """ 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: radius within the mass is computed :param theta_E: Einstein radius :param gamma: power-law slope :param e1: eccentricity component (not used) :param e2: eccentricity component (not used) :return: mass enclosed a 3D radius r """ return self.spp.density_lens(r, theta_E, gamma)