# Scene and Objects for Animation
scene = display(title='Orbital Motion', width=500, height=300, background=color.black)
sun = sphere(pos=vector(0,0,0), color=color.yellow, radius=6.96342e9)
comet = sphere(pos=vector(8.59E10,0,0), radius=4.0E9, make_trail=True,
trail_radius=6E8, interval=10, retain=1000)
# Parameters and Initial Conditions
G = 6.67408e-11 # universal gravitational constant (Nm^2/kg^2)
sun.mass = 1.989e30 # Sun's mass (kg)
comet.pos = vector(8.59E10, 0, 0) # initial vector position (m), starting at perigee on the x axis
comet.v = vector(0,54.55E3, 0) # initial vector velocity (m/s), starting at perigee
comet.a = vector(0,0,0) # just to initialize - this gets updated later
t = 0.0 # start time (s)
dt = 3600*6 # time step (s)
tsteps = 27375*24/6 # total number of iterations to run simulation
tmax = dt * tsteps # end time of simulation
# Plot
f1 = graph(ytitle='r [m]',xtitle='time [days]',width=500,height=200,xmax=tmax/(3600*24),ymax=4.1E8)
f1 = series(color=color.blue)
# Calculation loop
last_r = comet.r
last_t = 0
while t < tmax + dt: # tabbed lines that follow repeat until t <= tmax
rate(1e9) # set how fast the animation runs (frames/second)
comet.r = mag(comet.pos) # Sun-Comet distance
## ADD APPROPRIATE PHYSICS:
comet.a = vector(-G*sun.mass*comet.pos.x/(((comet.pos.x*comet.pos.x)+(comet.pos.y*comet.pos.y))**(3/2)),-G*sun.mass*comet.pos.y/(((comet.pos.x*comet.pos.x)+(comet.pos.y*comet.pos.y))**(3/2)),0) # acceleration
#halley.pos = halley.pos + halley.v*dt # update the position for Euler method, comment out for Euler-Cromer
comet.v = comet.v + comet.a*dt # update the velocity
comet.pos = comet.pos + comet.v*dt # update the position for Euler-Cromer method
tdays = t/(3600*24)
f1.plot(tdays,comet.r)
t=t+dt
print('time =', tdays, 'days') # print time, velocity,
print('velocity =', comet.v, 'm/s') # and position at each step
print("Semi-major axis = ", comet.r)
print("Acceleration x vs y: ", comet.a)
f1.plot(tdays,comet.r)
This is my code, when I try to increase dt, the comets doesn't seem to come back.When dt is small enough that it stays accurate, the simulation of the orbit takes too much time
I tried to increase dt, which resulted in a faster simulation, but it was inaccurate as the comet didn't seem to be in its orbit anymore. I want to be able to simulate an actual 75 year orbital period in the program, but I do not want it to take more than 5 minutes.