
Hii, I made this simulation of bending of light in the presence of a heavy object/ black hole i.e. gravitational lensing. The first one shows how light rays that are coming from infinity bends near blackhole and I even found an unstable orbit for which the ray orbits the blackhole 3 times before moving out.
I used pygame to create this 2D simulation. The main reason to do it in 2D instead of 3D was my potato laptop, it doesn't have a dedicated gpu. I watched two videos on YouTube on pygame and cpp simulations before making this (credits: https://youtu.be/8-B6ryuBkCM?si=iSMmUiJ-6KkQQTHq , https://youtu.be/WTLPmUHTPqo?si=HR5Xwaobzu8fG5qf).
For the theory part, starting with the schwarzschild metric, then using the concept of symmetries and killing vectors and also the normalisation condition for null geodesic, you will get all the equations needed to get the path of light around any mass in the spacetime. And for the simulation, I decided to use euler's method to solve those equations.
I know euler's method is not very accurate and smooth, and I should have used RK4 instead. I tried, for some reason it is not working as intended and the rays were getting stuck in a closed orbit, I tried a lot but couldn't figure out the issue.
Btw I think my simulation is working as intended, but I am not fully sure if it is the actual, accurate thing or not. Also there might be some scaling issues. So if anyone want to check it out or correct/improve my code, or maybe try the RK4 method, please feel free to check this out: https://github.com/suvojit1999/Simulation-of-Bending-of-light-due-to-blackhole. Btw I am not very good at coding, so you might find my code to be messy, let me know if you find any issues with it..
(Btw I had to upload it as gif because videos are not allowed here, sorry for the quality drop).
Thank you.
https://i.redd.it/9ztxacczni5g1.gif

5 Comments
This is a neat project, good job. I think if higher order methods like RK4 are resulting in photons getting captured in closed orbits, then it seems likely that there might be something wrong with the dynamics equations, and Euler method is just letting photons escape from higher order error terms. But it’s hard to diagnose without seeing the code, of course.
First, try reducing the time step size. (Euler’s metblhod will likely require impractically small steps for sufficient accuracy, though.) A smaller step size will improve the accuracy, but that may not sufficiently solve your problem.
RK4 and Euler do not conserve energy over time. Euler gains energy. RK4 loses energy. Getting stuck in a closed orbit is consistent with a loss of energy. That’s generally more of an issue over long time scales (e.g., many orbits). RK4 (with a sufficiently small time step) is often sufficient for basic/casual simulations of orbits. But maybe not in your relativistic scenario? To conserve energy, one would use a [symplectic integration method](https://en.wikipedia.org/wiki/Symplectic_integrator), such as velocity Verlet or Wisdom-Holman. If a smaller time step with RK4 doesn’t work, try using a symplectic method. Velocity Verlet would be a relatively simple symplectic method, appropriate for at least Newtonian orbits. But I think that may not work well for relativistic mechanics, which is out of my wheelhouse.
interesting! good work!!!!
Wait, in theory we could wrap the light from our own Sun around another star and point it back at us? Almost like a view into the past? Awesome.
Very awesome project, you’ve given me some ideas.