class TestExternalShear(object): """ tests the Gaussian methods """ def setup(self): self.flex = Flexion() g1, g2, g3, g4 = 0.01, 0.01, 0.01, 0.01 self.kwargs_lens = {'g1': g1, 'g2': g2, 'g3': g3, 'g4': g4} def test_function(self): x = np.array([1]) y = np.array([2]) values = self.flex.function(x, y, **self.kwargs_lens) npt.assert_almost_equal(values[0], 0.025, decimal=5) x = np.array([0]) y = np.array([0]) values = self.flex.function(x, y, **self.kwargs_lens) npt.assert_almost_equal(values[0], 0, decimal=5) x = np.array([2, 3, 4]) y = np.array([1, 1, 1]) values = self.flex.function(x, y, **self.kwargs_lens) npt.assert_almost_equal(values[0], 0.025, decimal=5) npt.assert_almost_equal(values[1], 0.066666666666666666, decimal=5) def test_derivatives(self): x = np.array([1]) y = np.array([2]) f_x, f_y = self.flex.derivatives(x, y, **self.kwargs_lens) npt.assert_almost_equal(f_x[0], 0.018333333333333333, decimal=5) npt.assert_almost_equal(f_y[0], 0.028333333333333335, decimal=5) x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) values = self.flex.derivatives(x, y, **self.kwargs_lens) npt.assert_almost_equal(values[0][0], 0.018333333333333333, decimal=5) npt.assert_almost_equal(values[1][0], 0.028333333333333335, decimal=5) def test_hessian(self): x = np.array(1) y = np.array(2) f_xx, f_yy, f_xy = self.flex.hessian(x, y, **self.kwargs_lens) npt.assert_almost_equal(f_xx, 0.016666666666666666, decimal=5) npt.assert_almost_equal(f_yy, 0.023333333333333334, decimal=5) npt.assert_almost_equal(f_xy, 0.01, decimal=5) x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) values = self.flex.hessian(x, y, **self.kwargs_lens) npt.assert_almost_equal(values[0][0], 0.016666666666666666, decimal=5) npt.assert_almost_equal(values[1][0], 0.023333333333333334, decimal=5) npt.assert_almost_equal(values[2][0], 0.01, decimal=5)
class Flexionfg(LensProfileBase): """ Flexion consist of basis F flexion and G flexion (F1,F2,G1,G2), see formulas 2.54, 2.55 in Massimo Meneghetti 2017 - "Introduction to Gravitational Lensing". """ param_names = ['F1', 'F2', 'G1', 'G2', 'ra_0', 'dec_0'] lower_limit_default = { 'F1': -0.1, 'F2': -0.1, 'G1': -0.1, 'G2': -0.1, 'ra_0': -100, 'dec_0': -100 } upper_limit_default = { 'F1': 0.1, 'F2': 0.1, 'G1': 0.1, 'G2': 0.1, 'ra_0': 100, 'dec_0': 100 } def __init__(self): self.flexion_cart = Flexion() super(Flexionfg, self).__init__() def function(self, x, y, F1, F2, G1, G2, ra_0=0, dec_0=0): """ lensing potential :param x: x-coordinate :param y: y-coordinate :param F1: F1 flexion, derivative of kappa in x direction :param F2: F2 flexion, derivative of kappa in y direction :param G1: G1 flexion :param G2: G2 flexion :param ra_0: center x-coordinate :param dec_0: center y-coordinate :return: lensing potential """ _g1, _g2, _g3, _g4 = self.transform_fg(F1, F2, G1, G2) return self.flexion_cart.function(x, y, _g1, _g2, _g3, _g4, ra_0, dec_0) def derivatives(self, x, y, F1, F2, G1, G2, ra_0=0, dec_0=0): """ deflection angle :param x: x-coordinate :param y: y-coordinate :param F1: F1 flexion, derivative of kappa in x direction :param F2: F2 flexion, derivative of kappa in y direction :param G1: G1 flexion :param G2: G2 flexion :param ra_0: center x-coordinate :param dec_0: center x-coordinate :return: deflection angle """ _g1, _g2, _g3, _g4 = self.transform_fg(F1, F2, G1, G2) return self.flexion_cart.derivatives(x, y, _g1, _g2, _g3, _g4, ra_0, dec_0) def hessian(self, x, y, F1, F2, G1, G2, ra_0=0, dec_0=0): """ Hessian matrix :param x: x-coordinate :param y: y-coordinate :param F1: F1 flexion, derivative of kappa in x direction :param F2: F2 flexion, derivative of kappa in y direction :param G1: G1 flexion :param G2: G2 flexion :param ra_0: center x-coordinate :param dec_0: center y-coordinate :return: second order derivatives f_xx, f_yy, f_xy """ _g1, _g2, _g3, _g4 = self.transform_fg(F1, F2, G1, G2) return self.flexion_cart.hessian(x, y, _g1, _g2, _g3, _g4, ra_0, dec_0) def transform_fg(self, F1, F2, G1, G2): """ basis transform from (F1,F2,G1,G2) to (g1,g2,g3,g4) :param f1: F1 flexion, derivative of kappa in x direction :param f2: F2 flexion, derivative of kappa in y direction :param g1: G1 flexion :param g2: G2 flexion :return: g1,g2,g3,g4 (phi_xxx, phi_xxy, phi_xyy, phi_yyy) """ g1 = (3 * F1 + G1) * 0.5 g2 = (3 * F2 + G2) * 0.5 g3 = (F1 - G1) * 0.5 g4 = (F2 - G2) * 0.5 return g1, g2, g3, g4
class TestExternalShear(object): """ tests the Gaussian methods """ def setup(self): self.flex = Flexion() g1, g2, g3, g4 = 0.01, 0.02, 0.03, 0.04 self.kwargs_lens = {'g1': g1, 'g2': g2, 'g3': g3, 'g4': g4} def test_function(self): x = np.array([1]) y = np.array([2]) values = self.flex.function(x, y, **self.kwargs_lens) npt.assert_almost_equal(values[0], 0.135, decimal=5) x = np.array([0]) y = np.array([0]) values = self.flex.function(x, y, **self.kwargs_lens) npt.assert_almost_equal(values[0], 0, decimal=5) x = np.array([2, 3, 4]) y = np.array([1, 1, 1]) values = self.flex.function(x, y, **self.kwargs_lens) npt.assert_almost_equal(values[0], 0.09, decimal=5) npt.assert_almost_equal(values[1], 0.18666666666666668, decimal=5) def test_derivatives(self): x = np.array([1]) y = np.array([2]) f_x, f_y = self.flex.derivatives(x, y, **self.kwargs_lens) npt.assert_almost_equal(f_x[0], 0.105, decimal=5) npt.assert_almost_equal(f_y[0], 0.15, decimal=5) x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) values = self.flex.derivatives(x, y, **self.kwargs_lens) npt.assert_almost_equal(values[0][0], 0.105, decimal=5) npt.assert_almost_equal(values[1][0], 0.15, decimal=5) def test_hessian(self): x = np.array(1) y = np.array(2) f_xx, f_yy, f_xy = self.flex.hessian(x, y, **self.kwargs_lens) npt.assert_almost_equal(f_xx, 0.05, decimal=5) npt.assert_almost_equal(f_yy, 0.11, decimal=5) npt.assert_almost_equal(f_xy, 0.08, decimal=5) x = np.array([1,3,4]) y = np.array([2,1,1]) values = self.flex.hessian(x, y, **self.kwargs_lens) npt.assert_almost_equal(values[0][0], 0.05, decimal=5) npt.assert_almost_equal(values[1][0], 0.11, decimal=5) npt.assert_almost_equal(values[2][0], 0.08, decimal=5) def test_flexion(self): x = np.array(0) y = np.array(2) flex = LensModel(['FLEXION']) f_xxx, f_xxy, f_xyy, f_yyy = flex.flexion(x, y, [self.kwargs_lens]) npt.assert_almost_equal(f_xxx, self.kwargs_lens['g1'], decimal=9) npt.assert_almost_equal(f_xxy, self.kwargs_lens['g2'], decimal=9) npt.assert_almost_equal(f_xyy, self.kwargs_lens['g3'], decimal=9) npt.assert_almost_equal(f_yyy, self.kwargs_lens['g4'], decimal=9) def test_magnification(self): ra_0, dec_0 = 1, -1 flex = LensModel(['FLEXION']) g1, g2, g3, g4 = 0.01, 0.02, 0.03, 0.04 kwargs = {'g1': g1, 'g2': g2, 'g3': g3, 'g4': g4, 'ra_0': ra_0, 'dec_0': dec_0} mag = flex.magnification(ra_0, dec_0, [kwargs]) npt.assert_almost_equal(mag, 1, decimal=8)