DEV Community


GSoC 2020: Blog 5 - Adding Kerr Null Geodesics functionality to EinsteinPy

jes24 profile image Jyotirmaya Shivottam ・5 min read

Null Geodesics functionality has been implemented into EinsteinPy, with PR#527, having been merged πŸŽ‰πŸŽ‰. I apologize for no blogs in the past 3 weeks. There was a COVID situation here, that required multiple tests and isolation and all that it entails. This led to me foregoing an entire week. And, when that had settled, I had to take the call on abandoning the plan of numerically integrating the Geodesics equations, due to the massive error accumulation, as discussed in my last blog. A confusing fact about that, was that Mathematica could still keep the error build-up to a minimum, while Python simply could not, even with adaptive and symplectic schemes. But the symplectic schemes did bring the error down, by around 2 orders of magnitude, which gave me the idea to take a Hamiltonian approach, which would increase the number of ODEs to solve, but drop the order by 1. And, as it turns out, the Kerr Hamiltonian is separable (Carter, 1968a [1]), which makes the implementation even simpler. In this blog, I will be discussing this approach, which has finally led to proper geodesic calculations. I have also included some plots (and a cool animation) for Kerr & Schwarzschild Null-like (and Time-like) geodesics.

Some Physics...

In Chapter 33 of Gravitation [2], the authors expound on Carter's seminal paper from 1968, titled, "Global Structure of the Kerr Family of Gravitational Fields", and present some nice results from it, one of which is a derivation of the Kerr (super-)Hamiltonian, which can be written as follows (in the M-Unit system ( G=c=M=1G = c = M = 1 ):

H=βˆ’(a4(E2βˆ’2pr2)βˆ’8aELrβˆ’2r(pΞΈ2(βˆ’2+r)+pr2(βˆ’2+r)2rβˆ’E2r3)+a2(2L2βˆ’2pΞΈ2βˆ’4pr2(βˆ’2+r)r+E2r(2+3r))+(a2+(βˆ’2+r)r)(a2E2cos⁑2ΞΈβˆ’2L2csc⁑θ2)4(a2+(βˆ’2+r)r)(r2+a2cos⁑θ2)) \mathcal{H} = -\frac{(a^4 (E^2 - 2 p_r^2) - 8 a E L r - 2 r (p_\theta^2 (-2 + r) + p_r^2 (-2 + r)^2 r - E^2 r^3) + a^2 (2 L^2 - 2 p_\theta^2 - 4 p_r^2 (-2 + r) r + E^2 r (2 + 3 r)) + (a^2 + (-2 + r) r) (a^2 E^2 \cos 2\theta - 2 L^2 \csc\theta^2)}{4 (a^2 + (-2 + r) r) (r^2 + a^2 \cos\theta^2))}

where, E=βˆ’ptE = -p_t and L=pΟ•L = p_\phi are the energy and orbital angular momentum of the test particle, respectively. Note that, this Hamiltonian is for a general test particle, i.e., it can be massive or massless. Then, the dynamical equations of motion can be derived easily, using Hamilton's principle, i.e.:
dqidΞ»=βˆ‚Hβˆ‚pidpidΞ»=βˆ’βˆ‚Hβˆ‚qi \frac{\mathrm{d}q_i}{\mathrm{d}\lambda} = \frac{\partial\mathcal{H}}{\partial p_i} \quad \frac{\mathrm{d}p_i}{\mathrm{d}\lambda} = -\frac{\partial\mathcal{H}}{\partial q_i}

I calculated these in Mathematica, and the corresponding notebooks and the Python code, making use of these, can be accessed here.

...and some plots

Unfortunately, even with these first order ODEs, the error accumulation issue in Python persisted, as can be observed in the plots below. Note that, these results were obtained with a symplectic leapfrog solver, which should, in principal keep the error build-up to a minimum.

Python 1
Kerr Null-like Escape

Although, for shorter integration durations, the results were good.

Python 2
Kerr Null-like Capture

After discussions with my mentors, I looked into other languages, that could help and we chose Julia, due to its excellent DifferentialEquations.jl suite and "closeness" with Python. Another key bit is that, the HamiltonianProblem type, offered by DiffEqPhysics, immensely simplifies the process of solving the system, as it uses Forward Mode Automatic Differentiation to automatically calculate the partial derivatives from the Hamiltonian. The separable nature of the Hamiltonian helps here. Considering all this, I implemented a module in Julia and voilΓ , the results are accurate, even for some quirky geodesics.

Python 1
Kerr Null-like Capture (Plotted using `Plots.jl`)

Python 2
Kerr Time-like Whirl (Plotted using `Plots.jl`)

Now came the problem of integrating the Julia code with EinsteinPy, for which I looked towards PyJulia. However, it has some issues with installation on *nix systems. So, I opted to write my own wrapper, using Python's subprocess. and, with the help of my GSoC mentor, Shreyas, packaged the Julia module and the Python wrapper into what is now einsteinpy_geodesics, an add-on module to EinsteinPy.

GitHub logo einsteinpy / einsteinpy-geodesics

Python wrapper for a Julia solver for geodesics in the Kerr family of spacetimes. Maintainer : @jes24

EinsteinPy Logo
Name: EinsteinPy Geodesics
Version: 0.2.dev0

mailing Join the chat at riotchat license docs

EinsteinPy Geodesics is an addon package for EinsteinPy, that wraps over Julia's excellent DifferentialEquations.jl suite and provides a python interface to solve for geodesics in Kerr & Schwarzschild spacetime EinsteinPy is an open source pure Python package, dedicated to problems arising in General Relativity and Gravitational Physics As with EinsteinPy, EinsteinPy Geodesics is released under the MIT license.



Complete documentation for this module can be accessed at (Courtesy: Read the Docs).


EinsteinPy Geodesics requires Python >= 3.7, Julia >= 1.5 and the following Julia packages:

  • Julia
    • DifferentialEquations.jl >= 6.15
    • ODEInterfaceDiffEq.jl >= 3.7


First, ensure that, Julia is installed in your system and added to PATH. See for platform specific binaries and installation instructions. einsteinpy_geodesics also requires DifferentialEquations.jl and ODEInterfaceDiffEq.jl. You can add them, like so:

$ julia
julia> using Pkg
julia> Pkg.add("DifferentialEquations")
julia> Pkg.add("ODEInterfaceDiffEq")

Finally, einsteinpy_geodesics can…

On top of this, I also overhauled the geodesic plotting module and added support for 3D animations, parametric plots and choice of spatial coordinates in 2D plots, in both Static and Interactive modes (that use matplotlib and plotly respectively). I present some of the plots, produced through the final API, below. The plots shown here, have a mix of both Static and Interactive back-ends, as well as time-like and null-like geodesics.

Kerr Null-like Geodesic

Kerr Time-like Geodesic

Kerr Frame Dragging

Schwarzschild Precession

Schwarzschild Time-like Closed Orbit

Schwarzschild Time-like Closed Orbit Parametric Plot

Until next time...

The EinsteinPy geodesics API currently provides a choice of solvers, between a python back-end and a julia back-end, through the optional einsteinpy_geodesics add-on. I will continue to work on improving the python back-end, but for now, einsteinpy_geodesics adds proper & accurate geodesic calculations to EinsteinPy, in the Kerr family of spacetimes (that includes Schwarzschild). Also, a notable aspect of the HamiltonianProblem approach is that, in principle, it should be easily extensible to Kerr-Newman geodesics, which is something, I'd like to explore, as soon as my GSoC commitment is over. I have another short blog coming up, that explains how to use the API (and has more cool plots), that will probably also be the last GSoC blog. Till then, I leave you with a nice animation, created entirely with EinsteinPy.

Python 2
(Extremal) Kerr Time-like Constant Radius Orbit


[1]: Carter, Brandon; Global Structure of the Kerr Family of Gravitational Fields, 1968 , Physical Review, 174(5), pp. 1559-1571

[2]: Misner, Charles W. and Thorne, K.S. and Wheeler, J.A; Gravitation, 1973, W. H. Freeman, ISBN: 978-0-7167-0344-0, 978-0-691-17779-3

Discussion (0)

Editor guide