Simple particles simulation (gravity)

Click For Summary

Discussion Overview

The discussion revolves around a simulation of gravitational interactions between two particles, focusing on the implementation of the code and the numerical methods used for simulating orbits. Participants explore issues related to the accuracy of the simulation, the choice of time step, and the numerical integration method employed.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses uncertainty about the correctness of their equations and the functioning of their orbit simulation code.
  • Another participant notes the omission of particle position updates in the provided code and questions the strength of gravitational force between the particles given their masses and distance.
  • Concerns are raised about the choice of a 1-second time step, suggesting it may lead to precision issues and error accumulation.
  • There is a suggestion to replace the Euler method with a 4th order Runge-Kutta method for improved accuracy and stability in the simulation.
  • A participant questions the relationship between time step size and acceleration, noting that changes in the time step seem to affect the orbit shape, which they believe should not occur.

Areas of Agreement / Disagreement

Participants generally agree that the Euler method is not ideal for this type of simulation and that a more accurate numerical integration method should be used. However, there is no consensus on the specific implementation details or the implications of time step size on the simulation's accuracy.

Contextual Notes

Limitations include potential missing updates for particle positions in the code, the dependence on the chosen time step for accuracy, and unresolved issues regarding the gravitational force calculations.

qwe
Messages
25
Reaction score
0
i believe I'm using the correct equations, but the orbit won't work. here is the relevant code for a 2 particle simple orbit simulation. I'm not sure what I'm doing wrong

Code:
...

timeStep = 1.0
gravityConstant = 6.67384 * 10.0 ^ -11.0

...

// (initialize particles)
// p[n].m: mass, p[n].x: initial x position, p[n].vz: initial velocity vector along z axis

            p[0].m = 1.0
            p[1].m = 1.0

            p[0].t = 0
            p[0].x = 10.0

            p[1].t = 0
            p[1].x = -10.0

// initial velocity = sqrt(m2 ^2 * G / m1 + m2 * distance)

            p[0].vz = sqrt( ( p[1].m * p[1].m * gravityConstant ) / ( p[0].m + p[1].m * 10.0 ) )
            p[1].vz = sqrt( ( p[0].m * p[0].m * gravityConstant ) / ( p[1].m + p[0].m * 10.0 ) ) * -1

...

// (each loop, for each particle... p[n] is current particle, p[m] is other particle)

                        // calculate distance
                        dx# = p[m].x - p[n].x
                        dy# = p[m].y - p[n].y
                        dz# = p[m].z - p[n].z
                        distance# = sqrt( dx# * dx# + dy# * dy# + dz# * dz# )
                        // calculate force
                        force# = (gravityConstant * p[n].m * p[m].m) / (distance# * distance#)
                        // calculate acceleration, include timestep
                        acceleration# = (force# / p[n].m) * timeStep
                        // calculate unit vector and calculate magnitude of vector (acceleration)
                        p[n].vx = p[n].vx + dx# * (acceleration# / distance# )
                        p[n].vy = p[n].vy + dy# * (acceleration# / distance# )
                        p[n].vz = p[n].vz + dz# * (acceleration# / distance# )
 
Last edited:
Physics news on Phys.org
Without going through the code in details:
1. I don't see where you update particle position. I assume you just omitted it for brevity.
2. Gravitation force between 2 1kg masses at 10 meters is not exactly very strong.
3. With a time step of 1s your are going to have a lot of very small steps, running into precision and error accumulation issues
4. Euler method sux. Bite the bullet and code up 4th order Runge-Kutta, then you can increase your step size to a sensible value.
 
am i doing the timestep correctly? the bigger the timestep, the more acceleration applied. that doesn't seem right, the orbit shape changes based on the timestep, but it shouldn't
 
Numerical integration suffers problems with accuracy and stability. The simple method you are using is called Euler method and isn't good. As Delta said, you should try 4th order Runge-Kutta.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 29 ·
Replies
29
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K