General Relativity Ray Tracing

trajectory
bars bars2
milikyway milkyway2 helix

Overview

General relativity has always been a big interest of mine ever since I took an introductory class in my physics degree; I find it fascinating that a change perspective, looking at gravity as curvature of space-time rather than a force leads to many interesting phenomena. Unfortunately, we live in a three-dimensional world and the four-dimensional aspect of spacetime is often hard to visualize; thus I created a general relativity ray tracer to help visualize and bring some intuition behind

First, I look at a 2D state space to utilize the symmetries of the Schwarzschild metric, which simplifies the derivations. I impose the null condition on the Schwarzschild metric to derive the lightlike geodesics and their first-order equations of motion via the Euler-Lagrange equation, parametrized by an affine parameter based on the constants in the system (the Schwarzschild metric is time-independent and symmetric to a polar angle -> energy and angular momentum are conserved via Noether's theorem). Each ray in the original 3D space can be expressed in the 2D state space through a constructed orthogonal basis, integrated, and then a change of basis is conducted to revert to the original 3d cartesian space.

An observing plane is placed parallel to an image plane, and rays are shot out from each pixel perpendicularly from the observing plane toward the image plane. When a ray intersects with the image plane, the color is mapped to the location on the observation plane. This gives us a plane-parallel orthographic camera projection. There is a black hole placed in between the planes, giving rise to the curvature described by the Schwarzschild metric. I use a fourth-order Runge-Kutta integrator to numerically determine the path of the null geodesic along each pixel, and track its path throughout space.

Initially, the ray tracing was done completely on my CPU, but this took upwards of 50 minutes for an 800,000 pixel large image. I added GPU acceleration via the python numba library, converting my ray tracing code into a CUDA compatible kernel, and having the computation parallelized by each individual ray. These optimizations cut down my runtime to just about 10 seconds for the same image.

Features

  • Simulation of a Schwarzschild black hole on a background image
  • GPU acceleration through CUDA

Technologies / Skills

Python (Numpy, Pandas, Numba, Matplotlib), CUDA, Computational Physics

Future Work

Future work will be to visualize null geodesics in metrics given by other solutions to Einstein's equations, like the Kerr metric of a spinning black hole. Additionally, I want to implement other camera projections to offer different perspectives of the black hole, as well as add functionality to create animations. A long term goal will be to optimize the algorithm enough to render frames in realtime.