# Computing gravity

1. Aug 3, 2004

### Alkatran

I'm attempting to make a program which simulates the gravity of bodies in space. However, I've run into a problem:

The program takes the acceleration/speed/position at a certain time and calculates the new values a (small value of time) later. However, this doesn't work because when two masses get VERY close together:
-The program ees that they are VERY close and gives them a MASSIVE acceleration.
-This results in the planets ending up far away from each other in the next frame.
-Energy isn't conserved and the whole thing is useless.

I was just wondering if anyone had the formula for average gravity over a distance over a period of time. I can make a basic formula for average acceleration assuming the planet continues moving the same speed through the distance. However, the planet will change velocity, so it still won't be accurate at very close distances.

2. Aug 3, 2004

### selfAdjoint

Staff Emeritus
You could maybe put conservation of energy in as a constraint on the program.

3. Aug 3, 2004

### Atheist

Well, I donÂ´t know how good you are at programming or how simple/sophisticated your program is going to be but if you treat your planets as spheres with a radius instead of pointsize particles you can either

a) add a collision detection. Regardless of what happens on collision (bounce off, stick together, break up, ...) the distances between the planets should always be big enough to avoid your problems. If you still have probs you can either make your timesteps smaller od switch to a better intergration method (simple forward steps are the most basic integration method).

b) Let the planets pass through each other but calculate the force acting on them correctly. F=0 for d(istance)=0, for example. Too lazy to calculate it out now and maybe you can well do that yourself. Intuitively it should be F=a*d + b*d for d<r1+r2 where a and b are typical constants for the planets.

EDIT: On 2nd though option b) seems rather stupid (what would your planets be, then? Crystalline gas?)

Last edited: Aug 3, 2004
4. Aug 3, 2004

### chroot

Staff Emeritus
You might want to have an adaptive time step. The program might use smaller time steps when the accelerations appear to be large. In other words, you first calculate the acceleration with the current time step. If that acceleration is very large, you halve the time step and try again. If, on the other hand, the acceleration is very small, you double the time step and try again. In this way, the program will never allow the acceleration to be change more than a certain amount from one frame to the next.

- Warren

5. Aug 3, 2004

### Atheist

While walking to the cinema earlier this evening I came up with an other alternative which would be my favorite atm:
When gravitational force between two objects exceeds a certain very large number of your choice:
Unless you accidentially have 3 bodies coming close to each other at the same time you can assume the force they excert on each other exceeds the forces from all other objects by far => neglect them => 2-body problem. The 2-body problem can be solved analytically so as long as both objects are within your critical range you move them along the trajectories of the 2-body problem. Once they get out of the critical range you proceed with your normal procedure. The error should be insignificant. You can even treat your objects as point-masses, then.

6. Aug 3, 2004

### Alkatran

These are all excellent logical ideas... the only problem is it's the type of solution I already had! (For example, the radius limitation was the current solution)

I suppose my real question is:
Assuming you know the inital speed, initial force, final force, distance, and time, can you calculate final speed or acceleration?

7. Aug 4, 2004

### dodo

I don't know how your program is done, but a common pitfall is to simply add up delta-Vs (or delta-Accelerations, or whatever delta) instead of integrating them for the timeframe. If the acceleration is a function of distance, and youv'e got a large acceleration due to close distance, AND you apply the same CONSTANT force through the next timeframe (incorrect), then your planets end up far away. If, instead, the result of the force is an integral of the acceleration over time (where the acceleration itself is a FUNCTION, probably a decreasing one), then maybe you get close to the real thing.
This is just general advise; the actual math should be shown by somebody else. :tongue2: . Hope this helps, anyway.

8. Aug 7, 2004

### pervect

Staff Emeritus
You should do some web searches for "numerical integration" and "runge-kutta" for a starter.

link

will at least get you somewhat started.

Runge-kutta, in spite of being one of the standard workhouse schemes of numerical integration, isn't necessarily the best choice for large gravity simulations, though. But it's probably a good enough to "play around" with. You'll need an adaptive timestep to deal with close approach, too.

9. Aug 7, 2004

### Alkatran

Yes, I knew that was the problem. But I couldn't figure out how to do it with high-school maths.

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook