Itsy-bitsy-teeny-weeny-black-hole-visualisation-machiney
A little long weekend project: relativistic ray tracing in Zig, visualizing thin accretion discs around a Kerr black hole with a handful of configurable parameters:
- observer position
- disc inner and outer radius
- black hole mass and spin
git clone https://github.com/fjebaker/zigularity
cd zigularity
zigmod fetch
zig build -Drelease-fast
Default is to try and calculate the coordinate-time of each traced photon (pixel) -- darker colours indicate things further away to give a slight semblance of three-dimensionality. The program should output something similar to this:
The program will write the output to a plain-text file "disc.data"
, which may be plotted with your plotting package of choice. As an example, here is a little bit of Python:
import matplotlib.pyplot as plt
import numpy as np
with open("disc.data") as f:
data = f.read()
image = []
for line in data.split("\n"):
row = line.split(",")[:-1]
if row:
image.append(row)
img = np.array(image, dtype=float)
plt.imshow(
img.transpose(),
# only want to color close to the black hole
vmin = 900,
vmax = 1100,
cmap = "Greys"
)
plt.show()
It can also be used to calculate the so-called shadow of a black hole, which traces the apparent shape of the event horizon:
Or for a non-spinning black hole:
- fix bad pixels bugs
- severely clean and refactor the code
- interpolate on integrator callback to smoothen the disc
-
(stretch) multi-threading cus it's a wee bit slowNot available on self-hosted Zig compiler yet.
Requires nektro/zigmod to install dependencies:
I write relativistic ray tracing codes as part of my research, feel free to take a look (~:
Relativistic ray-tracing is similar to regular ray-tracing, except that the trajectory of light may be altered due to the locally curved spacetime under strong gravity (see gravitional lensing). The trajectories may be traced by solving the geodesic equation -- a 2nd order ODE system which can be quite stiff -- however certain formulations of curved space permit a 1st order set of equations.
One such spacetime is the spacetime of the Kerr metric, describing a black hole with angular momentum. This metric is parameterised by a black hole mass
This code implements equations from Bardeen et al. (1972) and Fanton et al. (1997) to construct the 1st order equations, and integrates them using a simple adaptive Runge-Kutta algorithm.