# n-bodies: a parallel TBB solution: basic simulation loop

Simulation technology has become quite complex in order to mimic the real world but it starts with some basic principles. Dynamic simulation using a fixed time step is a simple place to start. Last time I took a first stab at a data structure; this time I'll make use of it.

```    for (double s = 0.; s < STEPLIMIT; s += TIMESTEP) {
// Compute the accelerations of the bodies
// Apply the accelerations and update body positions
}```

I pick a fixed time interval (TIMESTEP) and a duration for the simulation (STEPLIMIT) and in each of these steps I compute the effect of the bodies upon each other in their current positions, then apply that effect upon them to advance those positions. (And yes, despite comments to the contrary, I’m still using accelerations as the unit of exchange for these bodies. Like a fixed time step simulation, I’ll get off the path a little bit as I make my way, and hopefully generate a “teachable moment”-popular term these days-in my enlightened stumbling.) Fleshing this loop out a bit,

```    for (double s = 0.; s < STEPLIMIT; s += TIMESTEP) {
int i, j;
// Compute the accelerations of the bodies
// Apply the accelerations and update body positions
for (i = 0; i < n; ++i) {
for (j = 0; j < 3; ++j) {
body[i].vel[j] += body[i].acc[j] * TIMESTEP;
body[i].pos[j] += body[i].vel[j] * TIMESTEP;
body[i].acc[j] = 0.;
}
}
}```

I’ve introduced n, which will be the number of bodies in our current simulation. It will probably be a parameter to the function that this code eventually sits in. For each of the bodies then, using those equations of motion I compute a new velocity in three dimensions and then apply that new velocity to compute a new position. I’ll get into the details of computing the accelerations next time, but the last thing I do for each body is reset the acceleration to be ready for the next simulation step.

Next time: computing accelerations

For more complete information about compiler optimizations, see our Optimization Notice.