I'm aiming to compute planetary motion using Verlet algorithms.
The problem is, the code takes roughly 30 minutes to do a full cycle. This is basic computation so I have no idea why it's taking so long.
I've read around on here a bit and using numpy
should be faster right?
How do I implement it though? Also, it should be coming full circle and stopping at 0
( or 2*pi*r_earth
, if it's calculating distance travelled ), but it doesn't stop at all, just carries on, can anyone see why my limit isn't working?
Here's my code:
grav = 6.673481*(10**-11) # = 6.673481e-11 # a direct way
m_sun = 1.989*(10**30) # = 1.989e+30
m_earth = 5.972*(10**24) # = 5.972e+24
r_earth = 149.59787*(10**9) # = 1.4959787e+11
def verlet_v( prev_v, prev_f, mass ):
current_v = ( prev_v + ( prev_f / mass ) )
print "New Velocity: %f" % current_v
return current_v
def verlet_x( prev_x, current_v ):
current_x = prev_x + current_v
print "New Position: %f" % current_x
return current_x
verlet_v( 20, 50, 3 )
v = 29.8*(10**3) # = 2.98e+04
x = 0
f = ( -grav * ( ( m_earth * m_sun ) / r_earth**2 ) )
v_history = []
x_history = []
while( abs(x) > ( -0.1 ) ): # ABS( <_anything_> ) is ALWAYS > -0.1
print "Mod(x): %f" % abs(x)
print "Limit: %f" % (0)
v = verlet_v( v, f, m_earth )
x = verlet_x( x, v )
v_history.append(v)
x_history.append(x)
print v_history
print x_history