Playing with Gravity to Learn CUDA: A Fundamental Force
Playing With Gravity to Learn CUDA: A Fundamental Force

We started out this series on learning CUDA by diving in and writing a couple CUDA programs that we then got up and running on an nVidia graphics card. That was a great start that gave us an immediate feeling of accomplishment, but to keep advancing toward our goal of building a multi-body gravity simulation, we're going to have to take a break from CUDA and make sure we understand gravity a bit more. Gravity can be modeled at different levels of complexity, so we'll want to decide at what level we want to model it. We'll certainly start simple, but it's still good to know enough about gravity to know where we could go if we wanted and where it's not worth it to explore.What is Gravity?While scientists don't know exactly what gravity is—whether it's associated with an undiscovered particle, governed by a field, or purely a property of spacetime—we do know it is one of four fundamental forces in the universe. Gravity along with the forces of electromagnetism and the weak and strong nuclear forces govern all known interactions that we can observe between matter and radiation, and gravity happens to be the weakest of these forces. This fact may seem surprising, but consider that even a relatively weak fridge magnet can overcome the force of gravity generated by the entire planet Earth, and you get a sense of how weak gravity really is.While gravity may be weak, it's effects are the most far-reaching of the four forces. The weak and strong nuclear forces are limited to the scale of the nucleus of an atom, so their effects are extremely local. Electromagnetism is a polarized force, having positive and negative charges. Over large distances these opposite charges tend to balance, cancelling out the effect of the force at those distances. Arguably, photons carry the electromagnetic force as radiation over large distances, but the effect of the force is still localized relative to the photon. Gravity, on the other hand, doesn't have a polarity and it only decays with the square of the distance, so the effects of a gravitational field can reach much farther through space than the other forces.In more technical terms, gravity is an attractive force between objects that varies proportionately with the mass of those objects and how far apart they are from each other. More massive objects spaced more closely together will generate a stronger force between those objects. This conception of gravity is the Newtonian theory of gravity. Einstein added to Newton's theory with the theory of general relativity, which more accurately describes the properties and behaviors of gravity. Instead of the concept of masses acting directly on each other at a distance through an instantaneous force, general relativity introduces the idea that masses cause spacetime to curve, and curved spacetime determines how objects move through it. General relativity has additional implications for objects in motion. Instead of objects in orbit, like planets, revolving around their parent stars for all eternity, planets will actually dissipate energy (extremely slowly) into the fabric of spacetime, and their orbits will decay over very, very long timescales. Gravity will also affect massless objects, like photons, because the curvature of spacetime will affect the path of a photon and cause it to curve around large masses when it gets near them due to the large curvature of spacetime around those large masses. General relativity still makes all of the same calculations and predictions that Newtonian gravity does, but it extends into realms where Newtonian gravity either can't cope or gets things wrong.How Should We Model Gravity?We have a lot of potential things to model here with our gravity simulation. Objects could have multiple kinds of forces affecting them, and gravity can have secondary effects due to Einstein's general relativity. However, we're going to simplify things both by the nature of what we're going to model and because simpler calculations will allow us to scale to bigger systems. Because gravity is such a weak force that doesn't have noticeable effects until the masses are planetary-sized or larger, we'll be modeling systems with objects and distances on at least solar system scales. At those scales the electromagnetic, weak nuclear, and strong nuclear forces are negligible, so we're going to ignore them. We'll also start with the simplifying assumption that objects will be point-masses with no size to themselves. We may try to model masses with size eventually, but it turns out that at stellar and definitely interstellar distances, most objects are effectively point-masses anyway.We're also going to ignore general relativity and instead stick with Newtonian gravity for this model. While general relativity is much more accurate when it comes to describing the cosmos, it really comes into play with extremely massive and dense objects, like black holes and neutron stars, or over extremely long timescales, like the perihelion precession of Mercury. Because we're already modeling objects as point-masses and not over long enough timescales, we'll start with Newtonian gravity.The other major thing we have to decide how to deal with is time. For a multi-body system, each object's mass is going to affect every other object continuously over time, but we're simulating with the finite resources of a computer so we won't be able to do continuous calculations exactly. We'll simplify by calculating the forces on each object all at once in the same instant in time, and then calculate where those objects will be in space one timestep in the future. The size of the timestep can be configurable, and it will be a trade-off between accuracy and total simulated time.Oh, one last thing. We'll start with a 2D simulation. We may add a third dimension later because expanding the calculations to 3D is straightforward, but the additional calculations are time-consuming. We should get a good sense of how the systems work without the third dimension, and we'll be able to scale up to larger systems by working in only two dimensions.The Mathematics of GravityWe're going with the Newtonian definition of gravity in 2D, so what is that going to look like for the actual simulation? We need to express the definition of gravity in terms that a computer can calculate. That means a mathematical equation. As it turns out, we have one of those:F is the force on the corresponding mass m. G is the gravitational constant, 6.674×10−11 m3⋅kg−1⋅s−2. And r is the distance between the two masses. We'll need to calculate this as a vector for two dimensional space, so the equation has an additional vector term in practice:Where r̂21 is the unit vector from object 1 to object 2. There would also be an equal but opposite force F12 from object 2 to object 1. Once we have all of these forces between objects calculated and summed up for each object, we need to translate the forces into motion of the objects. This is Newton's second law of motion:F = maWhere a is another vector for acceleration in the same direction as F. We can use the calculated acceleration vectors and the known velocity and position vectors from the the previous timestep to arrive at the velocity and position vectors for the timestep being calculated:v = v0 + atd = d0 + vt + 0.5at2These equations give us the position of every object in the simulation at each timestep, which is all we need to run the simulation. We'll need to provide some initial conditions for v0 and d0 for every object at the start of the simulation to kick-start the first timestep, but after that, the simulation will determine these initial conditions for every subsequent timestep.Now that we understand a bit more about gravity and how we'll simplify things to build a tractable simulation, we're ready to see if we can actually do it. Next time we'll put these equations to use and see if we can write a program that simulates at least two massive objects using CUDA on the GPU.

