def test_str_repr(): """ Tests, if the ``__str__`` and ``__repr__`` messages match """ t = 0. M = 1e25 x_vec = np.array([306., np.pi / 2, np.pi / 2]) v_vec = np.array([0., 0.01, 10.]) ms_cov = Schwarzschild(M=M) x_4vec = four_position(t, x_vec) ms_cov_mat = ms_cov.metric_covariant(x_4vec) init_vec = stacked_vec(ms_cov_mat, t, x_vec, v_vec, time_like=True) end_lambda = 1. step_size = 0.4e-6 geod = Geodesic(metric=ms_cov, init_vec=init_vec, end_lambda=end_lambda, step_size=step_size) assert str(geod) == repr(geod)
def test_calculate_trajectory_iterator_schwarzschild(x_vec, v_vec, t, M, end_lambda, step_size, OdeMethodKwargs, return_cartesian): ms_cov = Schwarzschild(M=M) x_4vec = four_position(t, x_vec) ms_cov_mat = ms_cov.metric_covariant(x_4vec) init_vec = stacked_vec(ms_cov_mat, t, x_vec, v_vec, time_like=True) geod = Geodesic(metric=ms_cov, init_vec=init_vec, end_lambda=end_lambda, step_size=step_size, return_cartesian=return_cartesian) traj = geod.trajectory traj_iter = geod.calculate_trajectory_iterator( OdeMethodKwargs=OdeMethodKwargs, return_cartesian=return_cartesian) traj_iter_list = list() for _, val in zip(range(50), traj_iter): traj_iter_list.append(val[1]) traj_iter_arr = np.array(traj_iter_list) assert_allclose(traj[:50, :], traj_iter_arr, rtol=1e-10)
def test_calculate_trajectory2_schwarzschild(): # based on the revolution of earth around sun # data from https://en.wikipedia.org/wiki/Earth%27s_orbit t = 0. M = 1.989e30 distance_at_perihelion = 147.10e9 speed_at_perihelion = 30290 angular_vel = (speed_at_perihelion / distance_at_perihelion) x_vec = np.array([distance_at_perihelion, np.pi / 2, 0]) v_vec = np.array([0.0, 0.0, angular_vel]) ms_cov = Schwarzschild(M=M) x_4vec = four_position(t, x_vec) ms_cov_mat = ms_cov.metric_covariant(x_4vec) init_vec = stacked_vec(ms_cov_mat, t, x_vec, v_vec, time_like=True) end_lambda = 3.154e7 geod = Geodesic(metric=ms_cov, init_vec=init_vec, end_lambda=end_lambda, step_size=end_lambda / 2e3, return_cartesian=False) ans = geod.trajectory # velocity should be 29.29 km/s at aphelion(where r is max) i = np.argmax(ans[:, 1]) # index where radial distance is max v_aphelion = (((ans[i][1] * ans[i][7]) * (u.m / u.s)).to(u.km / u.s)).value assert_allclose(v_aphelion, 29.29, rtol=0.01)
def test_compare_vt_schwarzschild(): """ Tests, if the value of timelike component of 4-Velocity in Schwarzschild spacetime, \ calculated using ``einsteinpy.coordindates.utils.v0()`` is the same as that calculated by \ brute force """ # Calculated using v0() M = 1e24 * u.kg sph = SphericalDifferential(0. * u.s, 1. * u.m, np.pi / 2 * u.rad, 0.1 * u.rad, -0.1 * u.m / u.s, -0.01 * u.rad / u.s, 0.05 * u.rad / u.s) ms = Schwarzschild(coords=sph, M=M) x_vec = sph.position() ms_mat = ms.metric_covariant(x_vec) v_vec = sph.velocity(ms) sph.v_t = (ms, ) # Setting v_t vt_s = sph.v_t # Getting v_t # Calculated by brute force A = ms_mat[0, 0] C = ms_mat[1, 1] * v_vec[1]**2 + ms_mat[2, 2] * v_vec[2]**2 + ms_mat[ 3, 3] * v_vec[3]**2 - _c**2 D = -4 * A * C vt_sb = np.sqrt(D) / (2 * A) assert_allclose(vt_s.value, vt_sb, rtol=1e-8)
def test_four_velocity(v_vec, time_like): """ Tests, if the 4-Velocity in KerrNewman Metric is the same as that in Kerr Metric, \ in the limit Q -> 0 and if it becomes the same as that in Schwarzschild \ Metric, in the limits, a -> 0 & Q -> 0 """ M = 1e24 x_vec = np.array([1.0, np.pi / 2, 0.1]) x_vec = np.array([1.0, np.pi / 2, 0.1]) ms = Schwarzschild(M=M) mk = Kerr(coords="BL", M=M, a=0.5) mk0 = Kerr(coords="BL", M=M, a=0.) mkn = KerrNewman(coords="BL", M=M, a=0.5, Q=0.) mkn0 = KerrNewman(coords="BL", M=M, a=0., Q=0.) ms_mat = ms.metric_covariant(x_vec) mk_mat = mk.metric_covariant(x_vec) mk0_mat = mk0.metric_covariant(x_vec) mkn_mat = mkn.metric_covariant(x_vec) mkn0_mat = mkn0.metric_covariant(x_vec) v4vec_s = four_velocity(ms_mat, v_vec, time_like) v4vec_k = four_velocity(mk_mat, v_vec, time_like) v4vec_k0 = four_velocity(mk0_mat, v_vec, time_like) v4vec_kn = four_velocity(mkn_mat, v_vec, time_like) v4vec_kn0 = four_velocity(mkn0_mat, v_vec, time_like) assert_allclose(v4vec_s, v4vec_k0, rtol=1e-8) assert_allclose(v4vec_k, v4vec_kn, rtol=1e-8) assert_allclose(v4vec_kn0, v4vec_s, rtol=1e-8)
def test_calculate_trajectory_iterator_RuntimeWarning_schwarzschild(): t = 0. M = 1e25 x_vec = np.array([306., np.pi / 2, np.pi / 2]) v_vec = np.array([0., 0.01, 10.]) ms_cov = Schwarzschild(M=M) x_4vec = four_position(t, x_vec) ms_cov_mat = ms_cov.metric_covariant(x_4vec) init_vec = stacked_vec(ms_cov_mat, t, x_vec, v_vec, time_like=True) end_lambda = 1. stepsize = 0.4e-6 OdeMethodKwargs = {"stepsize": stepsize} geod = Geodesic(metric=ms_cov, init_vec=init_vec, end_lambda=end_lambda, step_size=stepsize, return_cartesian=False) with warnings.catch_warnings(record=True) as w: it = geod.calculate_trajectory_iterator( OdeMethodKwargs=OdeMethodKwargs, ) for _, _ in zip(range(1000), it): pass assert len(w) >= 1
def test_compare_metrics_under_limits(): """ Tests if KerrNewman Metric reduces to Kerr Metric, in the limit Q -> 0 and to Schwarzschild \ Metric, in the limits, a -> 0 & Q -> 0 """ r, theta = 99.9, 5 * np.pi / 6 M = 6.73317655e26 x_vec = np.array([0., r, theta, 0.]) ms = Schwarzschild(M=M) mk = Kerr(coords="BL", M=M, a=0.5) mk0 = Kerr(coords="BL", M=M, a=0.) mkn = KerrNewman(coords="BL", M=M, a=0.5, Q=0.) mkn0 = KerrNewman(coords="BL", M=M, a=0., Q=0.) ms_mat = ms.metric_covariant(x_vec) mk_mat = mk.metric_covariant(x_vec) mk0_mat = mk0.metric_covariant(x_vec) mkn_mat = mkn.metric_covariant(x_vec) mkn0_mat = mkn0.metric_covariant(x_vec) assert_allclose(ms_mat, mk0_mat, rtol=1e-8) assert_allclose(mk_mat, mkn_mat, rtol=1e-8) assert_allclose(mkn0_mat, ms_mat, rtol=1e-8)
def test_compare_vt_schwarzschild(): """ Tests, if the value of timelike component of 4-Velocity in Schwarzschild spacetime, \ calculated using ``einsteinpy.coordindates.utils.v_t()`` is the same as, that calculated by \ brute force """ # Calculated using v_t() M = 1e24 x_vec = np.array([1.0, np.pi / 2, 0.1]) v_vec = np.array([-0.1, -0.01, 0.05]) ms = Schwarzschild(M=M) ms_mat = ms.metric_covariant(x_vec) vt_s = v_t(ms_mat, v_vec) # Calculated by brute force A = ms_mat[0, 0] C = ms_mat[1, 1] * v_vec[0]**2 + ms_mat[2, 2] * v_vec[1]**2 + ms_mat[ 3, 3] * v_vec[2]**2 - 1 D = -4 * A * C vt_sb = np.sqrt(D) / (2 * A) assert_allclose(vt_s, vt_sb, rtol=1e-8)
def test_compare_vt_schwarzschild_kerr_kerrnewman(): """ Tests, whether the timelike component of 4-Velocity in KerrNewman Metric is the same as that \ in Kerr Metric, in the limit Q -> 0 and if it becomes the same as that in Schwarzschild \ Metric, in the limits, a -> 0 & Q -> 0 """ M = 1e24 x_vec = np.array([1.0, np.pi / 2, 0.1]) v_vec = np.array([-0.1, -0.01, 0.05]) ms = Schwarzschild(M=M) mk = Kerr(coords="BL", M=M, a=0.5) mk0 = Kerr(coords="BL", M=M, a=0.) mkn = KerrNewman(coords="BL", M=M, a=0.5, Q=0.) mkn0 = KerrNewman(coords="BL", M=M, a=0., Q=0.) ms_mat = ms.metric_covariant(x_vec) mk_mat = mk.metric_covariant(x_vec) mk0_mat = mk0.metric_covariant(x_vec) mkn_mat = mkn.metric_covariant(x_vec) mkn0_mat = mkn0.metric_covariant(x_vec) vt_s = v_t(ms_mat, v_vec) vt_k = v_t(mk_mat, v_vec) vt_k0 = v_t(mk0_mat, v_vec) vt_kn = v_t(mkn_mat, v_vec) vt_kn0 = v_t(mkn0_mat, v_vec) assert_allclose(vt_s, vt_k0, rtol=1e-8) assert_allclose(vt_k, vt_kn, rtol=1e-8) assert_allclose(vt_kn0, vt_s, rtol=1e-8)
def perihelion(): """ An example to showcase the usage of the various modules in ``einsteinpy.metric``. \ Here, we assume a Schwarzschild spacetime and obtain & plot the apsidal precession of \ test particle orbit in it. Returns ------- geod: ~einsteinpy.geodesic.Geodesic Geodesic defining test particle trajectory """ M = 6e24 # Mass t = 10000 # Coordinate Time (has no effect in this case - Schwarzschild) x_vec = np.array([130.0, np.pi / 2, -np.pi / 8]) # 3-Pos v_vec = np.array([0.0, 0.0, 1900.0]) # 3-Vel # Schwarzschild Metric Object ms_cov = Schwarzschild(M=M) # Getting Position 4-Vector x_4vec = four_position(t, x_vec) # Calculating Schwarzschild Metric at x_4vec ms_cov_mat = ms_cov.metric_covariant(x_4vec) # Getting stacked (Length-8) initial vector, containing 4-Pos and 4-Vel init_vec = stacked_vec(ms_cov_mat, t, x_vec, v_vec, time_like=True) # Calculating Geodesic geod = Geodesic(metric=ms_cov, init_vec=init_vec, end_lambda=0.002, step_size=5e-8) return geod
def test_calculate_trajectory_iterator( pos_vec, vel_vec, time, M, start_lambda, end_lambda, OdeMethodKwargs, return_cartesian, ): cl1 = Schwarzschild.from_spherical(pos_vec, vel_vec, time, M) arr1 = cl1.calculate_trajectory( start_lambda=start_lambda, end_lambda=end_lambda, OdeMethodKwargs=OdeMethodKwargs, return_cartesian=return_cartesian, )[1] cl2 = Schwarzschild.from_spherical(pos_vec, vel_vec, time, M) it = cl2.calculate_trajectory_iterator( start_lambda=start_lambda, OdeMethodKwargs=OdeMethodKwargs, return_cartesian=return_cartesian, ) arr2_list = list() for _, val in zip(range(100), it): arr2_list.append(val[1]) arr2 = np.array(arr2_list) assert_allclose(arr1[:100, :], arr2, rtol=1e-10)
def test_compare_metrics_under_limits(sph, bl): """ Tests if KerrNewman Metric reduces to Kerr Metric, in the limit Q -> 0 and to Schwarzschild \ Metric, in the limits, a -> 0 & Q -> 0 """ M = 6.73317655e26 * u.kg a1, a2 = 0.5 * u.one, 0. * u.one Q = 0. * u.C ms = Schwarzschild(coords=sph, M=M) mk = Kerr(coords=bl, M=M, a=a1) mk0 = Kerr(coords=bl, M=M, a=a2) mkn = KerrNewman(coords=bl, M=M, a=a1, Q=Q) mkn0 = KerrNewman(coords=bl, M=M, a=a2, Q=Q) x_vec_sph = sph.position() x_vec_bl = bl.position() ms_mat = ms.metric_covariant(x_vec_sph) mk_mat = mk.metric_covariant(x_vec_bl) mk0_mat = mk0.metric_covariant(x_vec_bl) mkn_mat = mkn.metric_covariant(x_vec_bl) mkn0_mat = mkn0.metric_covariant(x_vec_bl) assert_allclose(ms_mat, mk0_mat, rtol=1e-8) assert_allclose(mk_mat, mkn_mat, rtol=1e-8) assert_allclose(mkn0_mat, ms_mat, rtol=1e-8)
def test_f_vec_bl_schwarzschild(): M = 6.73317655e26 * u.kg sph = SphericalDifferential( t=0. * u.s, r=1e6 * u.m, theta=4 * np.pi / 5 * u.rad, phi=0. * u.rad, v_r=0. * u.m / u.s, v_th=0. * u.rad / u.s, v_p=2e6 * u.rad / u.s ) f_vec_expected = np.array( [ 3.92128321e+03, 0.00000000e+00, 0.00000000e+00, 2.00000000e+06, -0.00000000e+00, 1.38196394e+18, -1.90211303e+12, -0.00000000e+00 ] ) ms = Schwarzschild(coords=sph, M=M) state = np.hstack((sph.position(), sph.velocity(ms))) f_vec = ms._f_vec(0., state) f_vec assert isinstance(f_vec, np.ndarray) assert_allclose(f_vec_expected, f_vec, rtol=1e-8)
def dummy_data(): M = 6e24 t = 0. x_vec = np.array([130.0, np.pi / 2, -np.pi / 8]) v_vec = np.array([0.0, 0.0, 1900.0]) metric = Schwarzschild(M=M) x_4vec = four_position(t, x_vec) metric_mat = metric.metric_covariant(x_4vec) init_vec = stacked_vec(metric_mat, t, x_vec, v_vec, time_like=True) end_lambda = 0.002 step_size = 5e-8 return metric, init_vec, end_lambda, step_size
def plot_trajectory(self, coords, end_lambda, step_size, color): """ Parameters ---------- coords : ~einsteinpy.coordinates.velocity.SphericalDifferential Initial position and velocity of particle in Spherical coordinates. end_lambda : float, optional Lambda where iteartions will stop. step_size : float, optional Step size for the ODE. color : string Color of the Geodesic """ swc = Schwarzschild.from_spherical(coords, self.mass, self.time) vals = swc.calculate_trajectory( end_lambda=end_lambda, OdeMethodKwargs={"stepsize": step_size})[1] # time = np.array([coord[0] for coord in vals]) r = np.array([coord[1] for coord in vals]) # theta = np.array([coord[2] for coord in vals]) phi = np.array([coord[3] for coord in vals]) x = r * np.cos(phi) y = r * np.sin(phi) lines = self.ax.plot(x, y, "--", color=color) return lines, x[-1], y[-1]
def test_v_t_getter_setter0(cartesian_differential, spherical_differential, bl_differential): M = 1e24 * u.kg a = 0. * u.one ms = Schwarzschild(coords=spherical_differential, M=M) mk = Kerr(coords=bl_differential, M=M, a=a) def cd_vt(cartesian_differential, ms): cartesian_differential.v_t = (ms, ) def sd_vt(spherical_differential, mk): spherical_differential.v_t = (mk, ) # This should not raise CoordinateError try: spherical_differential.v_t = (ms, ) except CoordinateError: pytest.fail("Unexpected TypeError!") # These 2 should raise CoordinateError with pytest.raises(CoordinateError): cd_vt(cartesian_differential, ms) with pytest.raises(CoordinateError): sd_vt(spherical_differential, mk)
def test_v_t_raises_CoordinateError(cartesian_differential, spherical_differential, bl_differential): M = 1e24 * u.kg a = 0. * u.one ms = Schwarzschild(coords=spherical_differential, M=M) mk = Kerr(coords=bl_differential, M=M, a=a) cd, sd, bd = cartesian_differential, spherical_differential, bl_differential def cd_s(cd, ms): return cd.velocity(metric=ms) def bd_s(bd, ms): return bd.velocity(metric=ms) def cd_k(cd, mk): return cd.velocity(metric=mk) def sd_k(sd, mk): return sd.velocity(metric=mk) with pytest.raises(CoordinateError): cd_s(cd, ms) with pytest.raises(CoordinateError): bd_s(bd, ms) with pytest.raises(CoordinateError): cd_k(cd, mk) with pytest.raises(CoordinateError): sd_k(sd, mk)
def test_calculate_trajectory3(): # same test as with test_calculate_trajectory2(), # but initialialized with cartesian coordinates # and function returning cartesian coordinates M = 1.989e30 * u.kg distance_at_perihelion = 147.10e6 * u.km speed_at_perihelion = 30.29 * u.km / u.s pos_vec = [ distance_at_perihelion / np.sqrt(2), distance_at_perihelion / np.sqrt(2), 0 * u.km, ] vel_vec = [ -1 * speed_at_perihelion / np.sqrt(2), speed_at_perihelion / np.sqrt(2), 0 * u.km / u.h, ] end_lambda = ((1 * u.year).to(u.s)).value cl = Schwarzschild.from_cartesian(pos_vec, vel_vec, 0 * u.min, M) ans = cl.calculate_trajectory( start_lambda=0.0, end_lambda=end_lambda, return_cartesian=True, OdeMethodKwargs={"stepsize": end_lambda / 2e3}, )[1] # velocity should be 29.29 km/s at apehelion(where r is max) R = np.sqrt(ans[:, 1] ** 2 + ans[:, 2] ** 2 + ans[:, 3] ** 2) i = np.argmax(R) # index whre radial distance is max v_apehelion = ( (np.sqrt(ans[i, 5] ** 2 + ans[i, 6] ** 2 + ans[i, 7] ** 2) * (u.m / u.s)).to( u.km / u.s ) ).value assert_allclose(v_apehelion, 29.29, rtol=0.01)
def perihelion(): """ An example to showcase the usage of the various modules in ``einsteinpy``. \ Here, we assume a Schwarzschild spacetime and obtain & plot the apsidal precession of \ test particle orbit in it. Returns ------- geod: ~einsteinpy.geodesic.Geodesic Geodesic defining test particle trajectory """ # Mass of the black hole in SI M = 6e24 * u.kg # Defining the initial coordinates of the test particle # in SI sph = SphericalDifferential( t=10000.0 * u.s, r=130.0 * u.m, theta=np.pi / 2 * u.rad, phi=-np.pi / 8 * u.rad, v_r=0.0 * u.m / u.s, v_th=0.0 * u.rad / u.s, v_p=1900.0 * u.rad / u.s, ) # Schwarzschild Metric Object ms = Schwarzschild(coords=sph, M=M) # Calculating Geodesic geod = Timelike(metric=ms, coords=sph, end_lambda=0.002, step_size=5e-8) return geod
def test_calculate_state_raises_TypeError(): """ Tests, if ``_calculate_state`` raises TypeError, in case of \ coordinate mismatch """ distance_at_perihelion = 147.10e9 speed_at_perihelion = 29290 x_sph = CartesianDifferential( t=0.0 * u.s, x=distance_at_perihelion / np.sqrt(2) * u.m, y=distance_at_perihelion / np.sqrt(2) * u.m, z=0. * u.m, v_x=-speed_at_perihelion / np.sqrt(2) * u.m / u.s, v_y=speed_at_perihelion / np.sqrt(2) * u.m / u.s, v_z=0 * u.m / u.s) # .spherical_differential() metric = Schwarzschild(coords=x_sph.spherical_differential(), M=1.989e30 * u.kg) end_lambda = 3.154e7 with pytest.raises(TypeError): geod = Timelike( metric=metric, coords=x_sph, end_lambda=end_lambda, step_size=end_lambda / 2e3, )
def test_str_repr(): """ Tests, if the ``__str__`` and ``__repr__`` messages match """ M = 1e25 * u.kg sph = SphericalDifferential( t=0.0 * u.s, r=306.0 * u.m, theta=np.pi / 2 * u.rad, phi=-np.pi / 2 * u.rad, v_r=0.0 * u.m / u.s, v_th=0.01 * u.rad / u.s, v_p=10.0 * u.rad / u.s, ) ms = Schwarzschild(coords=sph, M=M) end_lambda = 1. step_size = 0.4e-6 geod = Timelike(metric=ms, coords=sph, end_lambda=end_lambda, step_size=step_size) assert str(geod) == repr(geod)
def plot(self, coords, end_lambda=10, step_size=1e-3): """ Parameters ---------- coords : ~einsteinpy.coordinates.velocity.SphericalDifferential Position and velocity components of particle in Spherical Coordinates. end_lambda : float, optional Lambda where iteartions will stop. step_size : float, optional Step size for the ODE. """ swc = Schwarzschild.from_spherical(coords, self.mass, self.time) vals = swc.calculate_trajectory( end_lambda=end_lambda, OdeMethodKwargs={"stepsize": step_size})[1] time = vals[:, 0] r = vals[:, 1] # Currently not being used (might be useful in future) # theta = vals[:, 2] phi = vals[:, 3] pos_x = r * np.cos(phi) pos_y = r * np.sin(phi) plt.scatter(pos_x, pos_y, s=1, c=time, cmap=self.cmap_color) if not self._attractor_present: self._plot_attractor()
def test_calculate_trajectory2(): # based on the revolution of earth around sun # data from https://en.wikipedia.org/wiki/Earth%27s_orbit M = 1.989e30 * u.kg distance_at_perihelion = 147.10e6 * u.km speed_at_perihelion = 30.29 * u.km / u.s angular_vel = (speed_at_perihelion / distance_at_perihelion) * u.rad sph_obj = SphericalDifferential( distance_at_perihelion, np.pi / 2 * u.rad, 0 * u.rad, 0 * u.km / u.s, 0 * u.rad / u.s, angular_vel, ) end_lambda = ((1 * u.year).to(u.s)).value cl = Schwarzschild.from_spherical(sph_obj, M) ans = cl.calculate_trajectory( start_lambda=0.0, end_lambda=end_lambda, OdeMethodKwargs={"stepsize": end_lambda / 2e3}, )[1] # velocity should be 29.29 km/s at apehelion(where r is max) i = np.argmax(ans[:, 1]) # index whre radial distance is max v_apehelion = (((ans[i][1] * ans[i][7]) * (u.m / u.s)).to(u.km / u.s)).value assert_allclose(v_apehelion, 29.29, rtol=0.01)
def __get_x_y(self, coords, end_lambda, step_size): """ Parameters ---------- coords : ~einsteinpy.coordinates.velocity.SphericalDifferential Initial position and velocity of particle in Spherical coordinates. end_lambda : float, optional Lambda where iteartions will stop. step_size : float, optional Step size for the ODE. """ swc = Schwarzschild.from_spherical(coords, self.mass, self.time) vals = swc.calculate_trajectory( end_lambda=end_lambda, OdeMethodKwargs={"stepsize": step_size})[1] # time = np.array([coord[0] for coord in vals]) r = np.array([coord[1] for coord in vals]) # theta = np.array([coord[2] for coord in vals]) phi = np.array([coord[3] for coord in vals]) x = r * np.cos(phi) y = r * np.sin(phi) self.__xarr = x self.__yarr = y return x, y
def test_calculate_trajectory2_schwarzschild(): # based on the revolution of earth around sun # data from https://en.wikipedia.org/wiki/Earth%27s_orbit distance_at_perihelion = 147.10e9 speed_at_perihelion = 29290 angular_vel = (speed_at_perihelion / distance_at_perihelion) sph = SphericalDifferential( t=0.0 * u.s, r=distance_at_perihelion * u.m, theta=np.pi / 2 * u.rad, phi=0.0 * u.rad, v_r=0.0 * u.m / u.s, v_th=0.0 * u.rad / u.s, v_p=angular_vel * u.rad / u.s, ) metric = Schwarzschild(coords=sph, M=1.989e30 * u.kg) end_lambda = 3.154e7 geod = Timelike(metric=metric, coords=sph, end_lambda=end_lambda, step_size=end_lambda / 2e3, return_cartesian=False) ans = geod.trajectory # velocity should be 29.29 km/s at aphelion(where r is max) i = np.argmax(ans[:, 1]) # index where radial distance is max v_aphelion = (((ans[i][1] * ans[i][7]) * (u.m / u.s)).to(u.km / u.s)).value assert_allclose(v_aphelion, 29.29, rtol=0.01)
def plot(self, pos_vec, vel_vec, end_lambda=10, step_size=1e-3): """ Parameters ---------- pos_vec : list list of r, theta & phi components along with ~astropy.units. vel_vec : list list of velocities of r, theta & phi components along with ~astropy.units. end_lambda : float, optional Lambda where iteartions will stop. step_size : float, optional Step size for the ODE. """ swc = Schwarzschild.from_spherical(pos_vec, vel_vec, self.time, self.mass) vals = swc.calculate_trajectory( end_lambda=end_lambda, OdeMethodKwargs={"stepsize": step_size})[1] time = vals[:, 0] r = vals[:, 1] # Currently not being used (might be useful in future) # theta = vals[:, 2] phi = vals[:, 3] pos_x = r * np.cos(phi) pos_y = r * np.sin(phi) plt.scatter(pos_x, pos_y, s=1, c=time, cmap="Oranges") if not self._attractor_present: self._plot_attractor()
def test_wrong_or_no_units_in_init(M, a, Q, q): """ Tests, if wrong or no units are flagged as error, while instantiation """ sph = SphericalDifferential( t=10000.0 * u.s, r=130.0 * u.m, theta=np.pi / 2 * u.rad, phi=-np.pi / 8 * u.rad, v_r=0.0 * u.m / u.s, v_th=0.0 * u.rad / u.s, v_p=0.0 * u.rad / u.s, ) bl = BoyerLindquistDifferential( t=10000.0 * u.s, r=130.0 * u.m, theta=np.pi / 2 * u.rad, phi=-np.pi / 8 * u.rad, v_r=0.0 * u.m / u.s, v_th=0.0 * u.rad / u.s, v_p=0.0 * u.rad / u.s, ) try: bm = BaseMetric(coords=sph, M=M, a=a, Q=Q) ms = Schwarzschild(coords=sph, M=M) mk = Kerr(coords=bl, M=M, a=a) mkn = KerrNewman(coords=bl, M=M, a=a, Q=Q, q=q) assert False except (u.UnitsError, TypeError): assert True
def test_singularities_raises_NotImplementedError(sph, bl): """ Tests, if ``singularities()`` raises a NotImplementedError, \ when there is a coordinate mismatch between supplied coordinate \ object and the coordinate object, metric was instantiated with """ M = 5e27 * u.kg a = 0. * u.one Q = 0. * u.C theta = np.pi / 4 ms = Schwarzschild(coords=bl, M=M) mk = Kerr(coords=sph, M=M, a=a) mkn = KerrNewman(coords=sph, M=M, a=a, Q=Q) def mssing(ms): mssing = ms.singularities() def mksing(mk): mksing = mk.singularities() def mknsing(mkn): mknsing = mkn.singularities() with pytest.raises(NotImplementedError): mssing(ms) with pytest.raises(NotImplementedError): mksing(mk) with pytest.raises(NotImplementedError): mknsing(mkn)
def test_calculate_trajectory_iterator_RuntimeWarning_schwarzschild(): M = 1e25 * u.kg sph = SphericalDifferential( t=0.0 * u.s, r=306.0 * u.m, theta=np.pi / 2 * u.rad, phi=-np.pi / 2 * u.rad, v_r=0.0 * u.m / u.s, v_th=0.01 * u.rad / u.s, v_p=10.0 * u.rad / u.s, ) ms = Schwarzschild(coords=sph, M=M) end_lambda = 1. stepsize = 0.4e-6 OdeMethodKwargs = {"stepsize": stepsize} geod = Timelike(metric=ms, coords=sph, end_lambda=end_lambda, step_size=stepsize, return_cartesian=False) with warnings.catch_warnings(record=True) as w: it = geod.calculate_trajectory_iterator( OdeMethodKwargs=OdeMethodKwargs, ) for _, _ in zip(range(1000), it): pass assert len(w) >= 1
def test_velocity2(spherical_differential, bl_differential): M = 1e24 * u.kg a = 0. * u.one ms = Schwarzschild(coords=spherical_differential, M=M) mk = Kerr(coords=bl_differential, M=M, a=a) sd, bd = spherical_differential, bl_differential assert_allclose(sd.velocity(metric=ms)[1:], [sd.v_r.value, sd.v_th.value, sd.v_p.value]) assert_allclose(bd.velocity(metric=mk)[1:], [bd.v_r.value, bd.v_th.value, bd.v_p.value])