So the other day, I was discussing with someone galaxy formation. I thought I'd show some 30-year old code I once wrote, a simple N-body simulation, to demonstrate that the basic physics of gravitational structure formation is no mystery. But then, I had a better idea. Why not ask one of our "little robot" friends (which is what my wife and I nowadays call large language models), Claude opus 3 in this particular case, to build a nice one?

Here is what I asked Claude to do:

  • I need to demonstrate to someone why the fundamentals of galaxy formation are no mystery, and I thought I would solicit your help. Please write for me an HTML + JavaScript page, without using third-party code, that performs an efficient N-body simulation, fast enough for real-time display with at least several hundred "stars". No need to be super-accurate, but make sure the system conserves energy and doesn't "blow up" with close two-body encounters. Set it up so that it shows how small self-gravitating structures, "galaxies", form. Include a field to control the number of "stars", and any other fields you deem necessary to control the simulation. Run the simulation in a worker thread, and let it periodically update a canvas showing the simulation result. Include a "start" and a "stop" button. Include a checkbox that, when set, causes the software to collect animation frames in a MediaRecorder object, and when the simulation is stopped, allows the user to save the collected frames as a webm video.

To be sure, it did not work flawlessly out of the box. I had to remind Claude not to draw white stars on a white background, and I also offered a correction to address computational stability. Later, I added the z-dimension "by hand", along with the ability to make distant stars appear fainter and redder. Oh, and I tweaked the CSS a little to line up the input fields.

But other than these minor tweaks, the entire application is Claude's doing. Worked (almost) out of the box.

I can spend many minutes watching this simulation unfold, with little "galaxies" forming and merging over time.

In case anyone cares, here's the source:

 
 
  

Claude even provided a helpful explanation of its code:

This code sets up an HTML page with a canvas to display the N-body simulation. The simulation is performed in a separate worker thread. The worker thread updates the positions of the stars based on gravitational forces and sends the updated positions back to the main thread, which then renders the stars on the canvas.

The user can control the number of stars, gravity constant, and damping factor using the input fields. The simulation can be started and stopped using the respective buttons. Additionally, there is a checkbox to enable recording the simulation as a video. When the simulation is stopped, the user can save the recorded frames as a WebM video.

The simulation uses a simple N-body algorithm with a softening factor to avoid singularities during close encounters. The positions and velocities of the stars are updated using the gravitational forces between them. The damping factor helps to prevent the system from "blowing up" and conserves energy.