A schematic image of a House
Rendering a Galaxy with the density wave theory
Simulating a galaxy without solving the n-body problem


Image of the Whirlpool-Galaxy

Image 1: Spiral galaxy M51. The galaxy has a clearly visible spiral structure with an abundance of H II regions shining red due to the radiation released by newly born stars.

This article explains how to set up a realistically looking spiral galaxy using basic astronomical and physical principles. The scientific background is explained briefly but only to the extend needed to understand the simulation. The article is accompanied by C++/SDL/OpenGL source code with project files for the CodeBlocks IDE.

Spiral galaxies are amongst the most beautiful observable objects in the night sky. Take for instance the whirlpool galaxy as shown in image 1. The picture was taken with the NASA/ESA Hubble space telescope. It's one of my favorite images as it features all the the components of a spiral galaxy in great detail. One can see a clear spiral structure with a lot of brigth red H-II-regions. In addition to that the spiral arms show a fine pattern of darker lines which are actually formed by interstellar gas and dust clouds. The gas and dust clouds provide gas and heavy elements needed to form new stars and planetary systems. Spiral arms are the places where new stars are born.

Pretty as it is it's just an image. How might a rotating galaxy look like? To find out one might use N-Body simulations but they are quite complicated (when done properly) and requires more computational time than desirable. What we need is a "shortcut", a way to spare ourself the complexity of calculating the interactions between thousands of stars without oversimplifying the problem. For this simulation the "shortcut" is the density wave theory. It will enable us to render a realistically looking galaxy without taking care of the complex n-body interactions involved in the real world.

Density Wave theory

The simulation is based on priciples laid out in the so called density wave theory. This theory was proposed int the mid 1960s by C.C. Lin and Frank Shu in order to explain the spiral arm structure of galaxies[1]. The information presented in this section is based on the Wikipedia article on density wave theory [2]. Originally, astronomers had the idea that the arms of a spiral galaxy were material. However, if this were the case, then the arms would become more and more tightly wound, since the matter nearer to the center of the galaxy rotates faster than the matter at the edge of the galaxy (see Animation 2). The arms would become indistinguishable from the rest of the galaxy after only a few orbits. This is called the winding problem [3]. Lin and Shu proposed in 1964 that the arms were not material in nature, but instead made up of areas of greater density, similar to a traffic jam on a highway.[1] The cars move through the traffic jam: the density of cars increases in the middle of it. The traffic jam itself, however, does not move (or not a great deal, in comparison to the cars). In the galaxy, stars, gas, dust, and other components move through the density waves, are compressed, and then move out of them.

Animation 1: If the spiral arms were rigid mass concentration the galaxy must rotate as a whole around its center in order to maintain it's spiral structure. According to Lindblads observation and the laws of physics this is not the case.

Animation 2: Differential rotation as observed by Lindblad would dissolve spiral arms in a short period of time were they composed of fixed mass concentrations.

Animation 3: Orbits predicted by the density wave theory allow the existance of stable spiral arms. Stars move in and out of the spiral arms as they orbit the galaxy.

Classification of spiral galaxies

In order to classify spiral galaxies a classification scheme was devised by Edwin Hubble in 1936. This classification scheme is still in use today. Hubble distinguished between elliptical galaxies and spiral galaxies. The elliptical galaxies were rated based on their excentricity and given identifier ranging from E0 to E7 with the excentricity increasing towards E7. The type S0 is an intermediary form of galaxy that marks the transition to the spiral galaxies. Spiral galaxies were separated into two categories on their own. The spiral galaxies and (Sa bis Sc) and the barred spiral galaxies (SBa bis SBc).

Galaxientypen nach Hubble
Image 2: Classification of galaxies according to Edwin Hubble; Courtesy of: Ville Koistinen (CC BY-SA 3.0)

For the rest of this article only spiral galaxies will matter. According to the classification scheme the following observations can be made for the different types of spiral galaxies:

Trajectories of the stars in a spiral galaxy

The first problem of modelling a realistically looking galaxy is getting its shape right. One theory for explaining the spiral structure is the density wave theory. It explains the typical spiral arm structure as a result of regions with higher star density caused by slight variations in the orbits of its stars. These regions are not static as stars will move in and out of them as they orbit the galaxy. The Spiral arms can be modelled by using elliptical orbits that are tilted as their radii grow. Although this is a simplification of the orbits it will serve us well for getting a realisticlly looking shape of the galaxy.

Gegeneinander um einen kleinen Winkel verschobene Elliptische Bahnkurven

Image 3: A number of ellipsis slightly tilted against one another as their diameter is increasing.

Gegeneinander um einen kleinen Winkel verschobene Elliptische Bahnkurven

Image 4: Orbits of a galaxy similar to those predicted by the density wave theory. The bulge area is marked with yellow. The innermost spiral structure slightly resembles a bar. The green circle marks the edge of the galaxy.

I will use the following simplified assumptions for the orbits in the galaxy:

To make this clear: In reality the orbits look different. They are not closed like an ellipses but open like a rosette. This difference however would'nt make a singnificant change to the visual appearance of the galaxy. Taking Keplers first law for approximating the orbits isn't very accurate either bacause the mass of a galaxy is not concentrated in its center but spread over the disk and bulge areas. However most mass is concentrated in the centre so there is some justification for this assumtion.

Image 3 shows elliptical orbits which exhibit a slight angular offset in their orientation as their diameter is increasing. Even this simple construction exposed clear indications of a spiral structure. The spiral arms start in the centre of the galaxy and extend to the edge. There is no distinct bulge area visible.

Since observations show that most spiral galaxies have a distinct bulge area the visual model needs to be tweaked to represent this fact properly. The bulge area is often circular in shape and so the overall model should allow such shapes. Barred galaxies have a central bar and spiral arms starting at the boundary between bulge area and disk. For this reason the model is extended to always have a circularely shaped core. The excentricities than increase towards the bulge-disk boundary (yellow circle in image 4 and from there they again change towards the edge of the galactic disk. Beyond the disc the excentricities will again decrease until they reach circular shapes at twice the diameter of the galactic disc. You can see it as the galactic halo but only few stars will be rendered there in the simulation later. (For simplicity Image 4 does omit these orbits as they don't contribute much to the overall shape of the galaxy.)

Surface brightness distribution of a spiral galaxy

Once the orbits are known the galaxy can be populated with stars. This section will provide some background information on the distribution of stars in a spiral galaxy. It will lead to a model allowing the distribution of the stars in a way similar to what is observable in real galaxies. The basic assumption is that Stars should be distributed in such a way that the surface brightness distribution of a real galaxy is matched. The surface brightness distribution of the bulge is similar to those of an elliptical galaxy. It can be estimated using the de Vaucouleurs-law:

IR = I 0 e - κ R 1 4


Central Intensity

According to Freeman[3] the surface brightness distribution of the galactic disc can be described using an exponential law:

IR = I 0 e - R R D


Central intensity
Characteristic scale length (Distance in which the intensity drops to 50%)

The total surface brightness distribution can be obtained by combining both equations as shown in Image 5. For the modelling of the galaxy it is assumed that the distribution function of the stars is similar to the surface brightness function. Applying such a distribution function to the density wave diagrams shown earlier yields the result shown in image 5 (left). The image begins to look like a galaxy even though the stars are drawn as plain white dots at the moment. The coloring of the galaxy will be discussed later on.

Simulated galaxy Brightness distribution of a galaxy.

Image 5 Left: The modell of a galaxy featuring a star distribution that closely resembles the surface brightness distribution of observed galaxies; Right: Schematic surface brightness distribution of a spiral galaxy according to Freeman and de Vaucouleurs combined into a single diagram.

The color of stars

In the previous section the galaxy and its distribution of stars was discussed. For a realistic rendering however it is crucial to apply a realistic coloring scheme as stars in a galaxy may only have distinct colors. The color of a star greatly depends on its surface temperaturs. The underlying physical principle is the so called back body radiation.

temperature spectrum

Bild 6: Color temperature accosring to Plancks law of radiation.

A realistic color scheme should be restricted to colors which are plausible according to Plancks law. For this model the surface temperature of the stars in the simulated galaxy lies between 3000 and 9000 Kelvin. In reality the surface temperature of a star will be subject change throughout its lifespan as it progresses in its path in the so called Hertzsprung Russell diagram. Calculating the color based on a given temperature is a pretty complex process which i won't discuss here in detail. I will use a program called specrend.c which was written by John Walker[5]. For those interested in the physical background of his program i recommend reading his web site where he explains the algorithm in depth. For all others it will suffice to say that his program computes the RGB values for a given temperature. Image 7 shows the result of applying coloring based on plancks law to the galaxy shown in Image 5. The exponential increase in star density towards the center is now better visible as star colors differ. However the galaxy as a whole still appears to be consisting of a number of more or less unrelated points. At this point the simulation is already rendered by using approximately 60000 stars. Simply rendering more stars doesn't appear to be viable option due to performance constraints. In reality galaxies consist of billions of stars. Simply rendering more stars won't do the trick since the resolution in a photographic image of a galaxy is't high enough to display distinct stars (unless they are extrordinary bright) anyway. What is visible in images is a kind of nebulea which would turn into distinct stars only if you would be able to zoom in greatly. For this reason the model needs an additional feature representing all those stars which are to small to be seen separately. In addition to the stars nebulea will be added to the galaxy. When using OpenGL one can use the pointsprite extension to get the desired effect. For simplicity the nebulea will be distributed evenly over the galactic disc. The result is shown in Image 8.

Simulated galaxy Simulated galaxy Simulated galaxy

Image 7: A rendered galaxie with colors based on the color temperatur predicted by a simple blackbody model.

Image 8: Galaxy with stars and nebalue based on OpenGL point sprites. The spiral structure is now more prominent.

Image 9: The final result shows a galaxy with stars, nebulea and H-II reggions concentrated in the spiral arms of the galaxy.

Image 8 is already a pretty good approximation of the galaxy but one crucial thing is still missing: H-II regions. If you look carefull at Image 1 you will see an adundance of bright red nebulea distributed across the spiral arms. These nebulae are so called H-II regions, huge intergalactic clouds composed of hydrogen. They are galactic nurseries full of newly born bright hot stars whose emmissions make the surrounding hydrogen glow in a characteristic red color. In order to match the appearance of a real galaxy the model also needs H-II regions and they should be located in the spiral arms just like in reality. This will be achieved with a trick, first H-II regions are evenly distributed amongst the galactic disc much like ordinary stars except they are rendered as red nebulae. But in order to restrain their visibility to the spiral arms their rendering size is proportional to the distance of two adjacent density waves with a cutoff size below a certain threshold. As a result they only appear in the spiral arms but move exactly like ordinary stars.

Rotational curve of a galaxy

Until now the motion of the galaxy was ignored. This last section will finally discuss the motion of a galaxy as giving a realistic impression on the rotation is one of the main objectives of this article.

rotation curve

Image 10: Rotational curve of a galaxy. Image courtesy of Phil Hibbs (CC BY-SA 3.0)

So lets look at a galaxies rotation curve. A rotation curve shows how fast the stars and gas components of a galaxy move as a function of their distance from the galactic centre. Observations show that the inner part of a galaxy is almost moving like a rigid body. This is causing a steep linear ascent in the rotation curve close to the galactic centre. With increasing distance from the centre the rotation curve flattens and is almost becoming a constant (Image 10; Kurve B). Interestingly enough this is a contradiction to models based on the distribution of the visible mass of a galaxy (Image 10; Curve A) that led to the hypothesis of the existance of dark matter. Dark matter is an unknown kind of matter that does not interact with visible matter in any form except by its gravitational force. So its not just dark, it's invisible, virtually undetectable except through its gravitational force. It is in fact so mysterious that various attemps have been made to get rid of the concept of dark matter altogether and explain it's effects by tweaking the law of gravity but so far dark matter is still the most accepted explanation for the observed rotation curves of galaxies.

The only thing left for the model to do is to make sure its rotation curve is similar to the one displayed in Image 10. The final result is shown in Animation 4. It shown a galaxy with two spiral arms and H-II regions. The galaxy is of hubble type Sb its rotation curve is similar to the rotation curves of real world galaxies.

Animation 4: The final result of the animation showing a galaxy with H-II regions spinning as observed galaxies.

You might also like: