Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf8 -*-
- dimension = 800 #(0.01 of A.U.)
- dt = 0.001
- delay = 1
- gM = 1.
- scale = 50
- IterMult = 100
- import numpy as np
- import Tkinter
- import itertools
- current_time = 0
- class Planet:
- def __init__(self,canv,x,y,vx,vy,mass,color,index):
- self.x = float(x)
- self.y = float(y)
- self.vx = float(vx)
- self.vy = float(vy)
- self.mass = float(mass)
- self.color = color
- self.canv = canv
- self.index = index
- self.position=self.canv.create_oval(dimension/2+int(scale*self.x)-5,dimension/2+int(scale*self.y)-5,
- dimension/2+int(scale*self.x)+5,dimension/2+int(scale*self.y)+5,fill=self.color,outline='white')
- self.energy = self.canv.create_text(100,100*(self.index+0.5),text=
- str("T+U: ")+str(0.5 * self.mass * (self.vx**2+self.vy**2)-gM * self.mass/np.sqrt(self.x**2 + self.y**2)),fill=self.color)
- self.momentum = self.canv.create_text(100,100*(self.index+0.5)+50,text=
- str("Lz: ")+str(self.x*self.vy-self.y*self.vx),fill=self.color)
- def time_step(self):
- canv.delete(self.position)
- del self.position
- canv.delete(self.energy)
- del self.energy
- canv.delete(self.momentum)
- del self.momentum
- r = np.sqrt (self.x**2 + self.y**2)
- self.energy = self.canv.create_text(100,100*(self.index+0.5),text=
- str("T+U: ")+str(0.5 * self.mass * (self.vx**2+self.vy**2)-gM * self.mass/r),fill=self.color)
- self.momentum = self.canv.create_text(100,100*(self.index+0.5)+50,text=
- str("Lz: ")+str(self.x*self.vy-self.y*self.vx),fill=self.color)
- #self.canv.create_oval(dimension/2+int(scale*self.x),dimension/2+int(scale*self.y),
- #dimension/2+int(scale*self.x),dimension/2+int(scale*self.y),fill=self.color,outline=self.color)
- self.x,self.y,self.vx,self.vy = RK4(self.x,self.y,self.vx,self.vy,dt)
- self.position=self.canv.create_oval(dimension/2+int(scale*self.x)-5,dimension/2+int(scale*self.y)-5,
- dimension/2+int(scale*self.x)+5,dimension/2+int(scale*self.y)+5,fill=self.color,outline='white')
- def RK1(x,y,vx,vy,t):
- r = np.sqrt (x**2 + y**2)
- vx = vx - gM * x / r**3 * t
- vy = vy - gM * y / r**3 * t
- x = x + vx * t
- y = y + vy * t
- return x,y,vx,vy
- def RK4(x,y,vx,vy,t):
- x1 = x
- y1 = y
- vx1 = vx
- vy1 = vy
- r = np.sqrt (x**2 + y**2)
- ax1 = - gM * x / r**3
- ay1 = - gM * y / r**3
- r1 = np.sqrt (x1**2 + y1**2)
- x2 = x + 0.5 * t * vx1
- y2 = y + 0.5 * t * vy1
- vx2 = vx - 0.5 * t * gM * x1 / r1**3
- vy2 = vy - 0.5 * t * gM * y1 / r1**3
- r2 = np.sqrt (x2**2 + y2**2)
- ax2 = - gM * x2 / r2**3
- ay2 = - gM * y2 / r2**3
- x3 = x + 0.5 * t * vx2
- y3 = y + 0.5 * t * vy2
- vx3 = vx - 0.5 * t * gM * x2 / r2**3
- vy3 = vy - 0.5 * t * gM * y2 / r2**3
- r3 = np.sqrt (x3**2 + y3**2)
- ax3 = - gM * x3 / r3**3
- ay3 = - gM * y3 / r3**3
- x4 = x + t * vx3
- y4 = y + t * vy3
- vx4 = vx - gM * x3 / r3**3 * t
- vy4 = vy - gM * y3 / r3**3 * t
- r4 = np.sqrt (x4**2 + y4**2)
- ax4 = - gM * x4 / r4**3
- ay4 = - gM * y4 / r4**3
- return x + t*(vx1+vx4+2*(vx2+vx3))/6.,y + t*(vy1+vy4+2*(vy2+vy3))/6.,vx + t*(ax1+ax4+2*(ax2+ax3))/6.,vy + t*(ay1+ay4+2*(ay2+ay3))/6.,
- root = Tkinter.Tk()
- canv = Tkinter.Canvas(root, width = dimension, height = dimension, bg = 'black')
- root.attributes('-fullscreen',True)
- canv.pack(expand='yes',fill='both')
- canv.create_oval(dimension/2-scale/10,dimension/2-scale/10,dimension/2+scale/10,dimension/2+scale/10,fill="white",outline="white")
- mercury = Planet(canv,0.459,0.,0.,1.327,0.0553,"orange",1)
- venus = Planet(canv,0.716,0.,0.,1.188,0.815,"yellow",2)
- earth = Planet(canv,1.00000,0.,0.,1.0,1.0,"blue",3)
- mars = Planet(canv,1.6,0.,0.,0.750,0.107,"red",4)
- juppiter = Planet(canv,5.369,0.,0.,0.425,317.83,"brown",5)
- saturn = Planet(canv,9.957,0.,0.,0.310,95.159,"dark goldenrod",6)
- uranus = Planet(canv,19.748,0.,0.,0.222,14.536,"sky blue",7)
- neptune = Planet(canv,29.886,0.,0.,0.183,17.147,"royal blue",8)
- #guest = Planet(canv,0.8,10,0.,-0.5,0.25,"purple",0)
- system=[]
- system.append(mercury)
- system.append(venus)
- system.append(earth)
- system.append(mars)
- system.append(juppiter)
- system.append(saturn)
- system.append(uranus)
- system.append(neptune)
- #system.append(guest)
- timetext = canv.create_text(dimension/2,dimension/8,text=str("Time: ")+str(current_time)+str(" days"),fill='white')
- def on_timer():
- global current_time, timetext
- canv.delete(timetext)
- del timetext
- current_time += 365*IterMult*dt/2/np.pi
- timetext = canv.create_text(dimension/2,dimension/8,text=str("Time: ")+str(current_time)+str(" days"),fill='white')
- for p in system:
- for _ in itertools.repeat(None, IterMult):
- p.time_step()
- root.after(delay, on_timer)
- on_timer()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment