ETikhonov

Solar System, simple

Jul 14th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.87 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf8 -*-
  3. dimension = 800 #(0.01 of A.U.)
  4. dt = 0.001
  5. delay = 1
  6. gM = 1.
  7. scale = 50
  8. IterMult = 100
  9. import numpy as np
  10. import Tkinter
  11. import itertools
  12. current_time = 0
  13. class Planet:
  14.     def __init__(self,canv,x,y,vx,vy,mass,color,index):
  15.         self.x = float(x)
  16.         self.y = float(y)
  17.         self.vx = float(vx)
  18.         self.vy = float(vy)
  19.         self.mass = float(mass)
  20.         self.color = color
  21.         self.canv = canv
  22.         self.index = index
  23.         self.position=self.canv.create_oval(dimension/2+int(scale*self.x)-5,dimension/2+int(scale*self.y)-5,
  24.         dimension/2+int(scale*self.x)+5,dimension/2+int(scale*self.y)+5,fill=self.color,outline='white')
  25.         self.energy = self.canv.create_text(100,100*(self.index+0.5),text=
  26.         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)
  27.         self.momentum = self.canv.create_text(100,100*(self.index+0.5)+50,text=
  28.         str("Lz: ")+str(self.x*self.vy-self.y*self.vx),fill=self.color)
  29.     def time_step(self):
  30.         canv.delete(self.position)
  31.         del self.position
  32.         canv.delete(self.energy)
  33.         del self.energy
  34.         canv.delete(self.momentum)
  35.         del self.momentum
  36.         r = np.sqrt (self.x**2 + self.y**2)
  37.         self.energy = self.canv.create_text(100,100*(self.index+0.5),text=
  38.         str("T+U: ")+str(0.5 * self.mass * (self.vx**2+self.vy**2)-gM * self.mass/r),fill=self.color)
  39.         self.momentum = self.canv.create_text(100,100*(self.index+0.5)+50,text=
  40.         str("Lz: ")+str(self.x*self.vy-self.y*self.vx),fill=self.color)
  41.         #self.canv.create_oval(dimension/2+int(scale*self.x),dimension/2+int(scale*self.y),
  42.         #dimension/2+int(scale*self.x),dimension/2+int(scale*self.y),fill=self.color,outline=self.color)
  43.         self.x,self.y,self.vx,self.vy = RK4(self.x,self.y,self.vx,self.vy,dt)
  44.         self.position=self.canv.create_oval(dimension/2+int(scale*self.x)-5,dimension/2+int(scale*self.y)-5,
  45.         dimension/2+int(scale*self.x)+5,dimension/2+int(scale*self.y)+5,fill=self.color,outline='white')    
  46.  
  47. def RK1(x,y,vx,vy,t):
  48.     r = np.sqrt (x**2 + y**2)
  49.     vx = vx - gM * x / r**3 * t
  50.     vy = vy - gM * y / r**3 * t
  51.     x = x + vx * t
  52.     y = y + vy * t
  53.     return x,y,vx,vy
  54.  
  55.  
  56. def RK4(x,y,vx,vy,t):
  57.     x1 = x
  58.     y1 = y
  59.     vx1 = vx
  60.     vy1 = vy
  61.     r = np.sqrt (x**2 + y**2)
  62.     ax1 =  - gM * x / r**3
  63.     ay1 =  - gM * y / r**3
  64.     r1 = np.sqrt (x1**2 + y1**2)
  65.     x2 = x + 0.5 * t * vx1
  66.     y2 = y + 0.5 * t * vy1
  67.     vx2 = vx - 0.5 * t * gM * x1 / r1**3
  68.     vy2 = vy - 0.5 * t * gM * y1 / r1**3
  69.     r2 = np.sqrt (x2**2 + y2**2)
  70.     ax2 =  - gM * x2 / r2**3
  71.     ay2 =  - gM * y2 / r2**3
  72.     x3 = x + 0.5 * t * vx2
  73.     y3 = y + 0.5 * t * vy2
  74.     vx3 = vx - 0.5 * t * gM * x2 / r2**3
  75.     vy3 = vy - 0.5 * t * gM * y2 / r2**3
  76.     r3 = np.sqrt (x3**2 + y3**2)
  77.     ax3 =  - gM * x3 / r3**3
  78.     ay3 =  - gM * y3 / r3**3
  79.     x4 = x + t * vx3
  80.     y4 = y + t * vy3
  81.     vx4 = vx - gM * x3 / r3**3 * t
  82.     vy4 = vy - gM * y3 / r3**3 * t
  83.     r4 = np.sqrt (x4**2 + y4**2)
  84.     ax4 =  - gM * x4 / r4**3
  85.     ay4 =  - gM * y4 / r4**3
  86.     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.,
  87.  
  88.  
  89. root = Tkinter.Tk()
  90. canv = Tkinter.Canvas(root, width = dimension, height = dimension, bg = 'black')
  91. root.attributes('-fullscreen',True)
  92. canv.pack(expand='yes',fill='both')
  93. canv.create_oval(dimension/2-scale/10,dimension/2-scale/10,dimension/2+scale/10,dimension/2+scale/10,fill="white",outline="white")
  94. mercury = Planet(canv,0.459,0.,0.,1.327,0.0553,"orange",1)
  95. venus = Planet(canv,0.716,0.,0.,1.188,0.815,"yellow",2)
  96. earth = Planet(canv,1.00000,0.,0.,1.0,1.0,"blue",3)
  97. mars = Planet(canv,1.6,0.,0.,0.750,0.107,"red",4)
  98. juppiter = Planet(canv,5.369,0.,0.,0.425,317.83,"brown",5)
  99. saturn = Planet(canv,9.957,0.,0.,0.310,95.159,"dark goldenrod",6)
  100. uranus = Planet(canv,19.748,0.,0.,0.222,14.536,"sky blue",7)
  101. neptune = Planet(canv,29.886,0.,0.,0.183,17.147,"royal blue",8)
  102. #guest = Planet(canv,0.8,10,0.,-0.5,0.25,"purple",0)
  103. system=[]
  104. system.append(mercury)
  105. system.append(venus)
  106. system.append(earth)
  107. system.append(mars)
  108. system.append(juppiter)
  109. system.append(saturn)
  110. system.append(uranus)
  111. system.append(neptune)
  112. #system.append(guest)
  113.  
  114. timetext = canv.create_text(dimension/2,dimension/8,text=str("Time: ")+str(current_time)+str(" days"),fill='white')
  115.  
  116. def on_timer():
  117.     global current_time, timetext
  118.     canv.delete(timetext)
  119.     del timetext
  120.     current_time += 365*IterMult*dt/2/np.pi
  121.     timetext = canv.create_text(dimension/2,dimension/8,text=str("Time: ")+str(current_time)+str(" days"),fill='white')
  122.  
  123.     for p in system:
  124.         for _ in itertools.repeat(None, IterMult):
  125.             p.time_step()
  126.     root.after(delay, on_timer)
  127. on_timer()
  128. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment