Author: BL de Vries. PhD and Scientific visualisation advisor at SURF.
This python package is written to accompany the Black Hole Render Engine for Blender. But this package can be used for other projects just as well. It calculates geodesics (trajectories) of massive particles and photons in curved space-time (different metrics). Both the Schwarzschild and Kerr metric is implemented.
If you find curvedpy usefull and use it in your projects, please give me credit by mentioning this repo!
Just install from pip from this in your preferred environment. I need to upload a new version to pypi, for now install directly from github: pip install git+https://github.com/bldevries/curvedpy
I will fix things so that this will give you the proper version again: pip install curvedpy
The dependencies are few, most notably it uses sympy, numpy, scipy and pillow.
For the basics on how to use the package, see this notebook
This section is for those interested in the physics and maths. I will loosely explain how the path travelled by bodies in four-dimensional space-times is calculated given a metric. I will leave out most of the math and proper definitions. It needs to be readable by people that have studied some calculus and mechanics. If you have studied some General Relativity and/or differential geometry, please keep the goal of the oversimplifications I make in mind. :)
So, let’s start with Newtons second law. In flat space the motion of bodies (in the classical limit) is described by the second law of Newton:
Where F is the net force acting on the body, m is the body's mass and x is the location of the body. If no force is acting on the body this reduces to:
Which states that the velocity of the body is constant and
In General Relativity a similar equation as the second law of Newton exists and is called the geodesic equation:
This equation describes the motion of a body in four-dimensional space (thus including time). At first sight this equation does not look similar to Newtons second law but let’s have a look. As with Newtons equation the
Now
would not exist, the geodesic equation would reduce to Newtons equation (with F=0) and taking
The term with
(Note to self: Add more information on different and equivalent definitions of a straight line?)
The
Where
Even though you have used the metric of flat Euclidean space many times, you might not have realized. This is because the metric in Euclidian space is:
Here I have written the metric in Matrix notation. (For the connoisseur, the metric is a rank-2 tensor
Here we use standard matrix multiplication,
A nice example of a more complicated metric in a familiar setting is that of the surface of a sphere. If you are interested in what this metric looks like and how you can use it, see my blog Calculating lengths in curved spaces using SymPy’s symbolic mathematics, Python and Matplotlib.
One of the metrics implemented in curvedpy is the Schwarzschild metric that describes space-time around a spherically symmetric blackhole. In Spherical coordinates this looks like:
For the code I have implemented I have used techniques from several peer-reviewed papers and books, among which:
- Bronzwaer et al 2018: https://arxiv.org/abs/1801.10452
- Davelaar et al 2023: https://arxiv.org/abs/2303.15522
- Moscibrodzka et al 2023: https://arxiv.org/abs/2302.02733
- A first course in general relativity. Bernard Schutz
- Spacetime and geometry. Carroll
Let me briefly explain how I solve the geodesic equation. The geodesic equation is a not quite linear second order differential equation, so we need to use some numerical techniques to tackle it. The scipy packages has some nice integrators which we can use through the function solve_ivp. For a simple example of how you can use this function check out my blog Simply solving differential equations using Python, scipy and solve_ivp.
For solve_ivp to work we need to split the geodesic equation in first order equations by introducing the "velocity"
Now we can integrate these equations (these are 8 equations since the indices can take on 4 values) if we have an initial condition for the location
To learn more about the metric, you can start with this jupyter notebook. To learn more about how to solve geodesics in curved spacetime, see this tutorial

