Simulating the Universe in a browser

A few weeks ago Chris North asked me if I'd make something to demonstrate some of the science that comes from ESA's Planck satellite. The aim was to show how tweaking the ingredients of the Universe affects the appearance of the earliest light we can see. That light set off around 400,000 years after the big bang and is called the cosmic microwave background radiation or CMB.

The demo was for a Planck stand at the Royal Society Summer Science Exhibition held in London. Chris and I wanted the demo to have a life beyond the exhibition so we created it as a webpage (and put the code on Github). I've put a copy of it on my site so you can simulate the Universe in your own browser.



The Science Bit

There are three sliders to change the amounts of normal matter (baryons that make up you, me, the Earth, the Sun etc), dark matter (something with gravity that doesn't seem to interact with light) and dark energy (something that makes the Universe's expansion accelerate). As you change the sliders we update the view of the CMB as seen on a patch of sky 10° across. Have a play and see how each of the 3 types of ingredient can change the view. The red patches are slightly hotter directions in the sky and the blue patches are slightly colder directions. The variation in temperature is at the level of 0.00001 degrees Kelvin so is pretty tiny but is measurable with sensitive instruments like those on Planck.

The good thing about the simulator is that you can try out different Universes. If you make a 1960s view of the Universe - not knowing about dark-matter-like or dark-energy-like contributions - you'll see that the result doesn't look like what we now measure our Universe to be like. Making the simulator also made me realise that if you have no normal matter in the Universe you don't see any fluctuations in the CMB as light had nothing to interact with and therefore doesn't show the clumpiness of the dark matter. Thank goodness for normal matter or we'd be in trouble (in many ways).

As you change the ingredients some of the changes can be a bit too subtle to see by eye. It can be more useful to get a mathematical representation of the CMB and that is called the power spectrum. Basically we graph how much change there is in temperature when you compare patches of sky of certain sizes. It is similar to displaying an audio track as a frequency spectrum but this shows you which spatial frequencies are the most dominant. Wayne Hu has a good background to the peaks and troughs seen in the power spectrum if you want to know why each one exists and why their relative heights and locations are what they are.

Building a Universe

To get all this working in a browser requires quite a bit of fiddling with Javascript and a few approximations. To keep it feeling responsive when changing the sliders, a compromise was made to reduce the amount of data sent to the browser. I down-sampled the power spectrum that Chris produced (he used CAMB) so that it only really contains the points for the peaks and troughs and then I draw nice curves through them using Raphael.js. That means the curves are approximate and if you over-plotted a genuine power spectrum on top there would be some minor differences.

To get the image of the sky I do a 2D FFT (thanks to a Javascript FFT library made by wellflat) of Gaussian noise, apply an interpolated version of the down-sampled power spectrum to it and FFT it back into real space. I then apply the Planck colour scale and draw the result to an HTML5 <canvas>. Basically, there is a lot of calculation to do there so these rough approximations were needed to keep it fast. Despite that, the result is good enough to show the point.

I've added keyboard shortcuts: pressing 'b', 'c' or 'l' on your keyboard will focus on the normal matter, dark matter, and dark energy sliders and you can use the arrow keys to slide them. The page should also re-jiggle itself to fit a variety of screen resolutions although the Javascript processing can be a bit slow to update the CMB on underpowered mobile phones.

I had fun making this and hopefully it'll be useful to those trying to get to grips with the CMB. I certainly learned a thing or two whilst making it.

Posted in astro blog by Stuart on Wednesday 10th Jul 2013 (01:01 BST) | Permalink

Virtual Sky - The Galactic Update

I quite like to make things. One of the things I've made is VirtualSky - my attempt to recreate Stellarium in a web browser. I created it during my time at LCOGT and, because they are awesome about open source, the code is on GitHub.

It has been a while since I last added improvements. Today I've worked on a few updates and I'm beta testing them. I'm calling this the Galaxy Update as the main change is that it will now display an outline of the Milky Way similar to the one in the excellent Norton's Star Atlas (19th Edition). Of course in Norton's it is a shaded galaxy but, as it is actually pretty tricky to only draw parts of filled polygons (when one part of them may be on one side of the screen and the other part, separately, on the opposite side due to the map projection), I've gone with just an outline for now.

The updates are:

  • Display an outline of the Milky Way by pressing 'M' (capital m) on your keyboard. Also by using the showgalaxy=true option.
  • Cycle through the various map projections by pressing 'h' on your keyboard. Each of these projections could already be selected using e.g projection=stereo but this now makes it possible to switch easily.



I'd be grateful if people could test this beta version and let me know of any bugs or issues.

P.S. Please don't embed this beta version in your sites. Stick to embedding via the LCOGT stable version.

Posted in astro blog by Stuart on Monday 08th Jul 2013 (23:36 BST) | Permalink
[an error occurred while processing this directive]