Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.15 KB | None | 0 0
  1.  
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Mar  8 14:14:53 2018
  5.  
  6. @author: ip1016
  7. """
  8. import numpy as np
  9. import math as m
  10. import matplotlib.pyplot as plt
  11. import matplotlib.animation as animation
  12.  
  13.  
  14. #r=np.array([0,0])
  15. #v=np.array([0,0])
  16. collision=[]
  17.  
  18. class Ball:
  19.     def __init__(self,t,m,R,r=[0, 0],v=[0, 0],clr='r'):
  20.         self.__r=np.array(r,dtype='float')
  21.         self.m=m
  22.         self.__R=float(R)
  23.         self.__v=np.array(v,dtype = 'float')
  24.         self.t=t
  25.        # self.__dpos=np.array([r(0),r(1)],[v(0),v(1)])
  26.         self.__patch=plt.Circle(self.__r,self.__R, ec=clr,fill=False,fc=clr)
  27.         if len(self.__r)!=2:
  28.             raise Exception("Vector parameter size")  
  29.    
  30.     def pos(self):
  31.         return self.__r
  32.    
  33.     def vel(self):
  34.         return self.__v
  35.    
  36.     def move(self,dt):
  37.         self.__r=self.__r+np.multiply(self.__v,dt)
  38.         self.__patch.center=self.__r
  39.            
  40.     def rad(self):
  41.         return self.__R
  42.     def ty(self):
  43.         return self.t
  44.     def get_patch(self):
  45.         return self.__patch
  46.    
  47.     def time_to_collision(self,other):
  48.         a=np.dot(self.__v,self.__v)+np.dot(other.__v,other.__v)-2*np.dot(self.__v,other.__v)
  49.         b=2*np.dot(self.__r,self.__v)-2*np.dot(self.__r,other.__v)-2*np.dot(self.__v,other.__r)+2*np.dot(other.__r,other.__v)
  50.         if self.t==1 and other.t==1:
  51.             c=np.dot(self.__r,self.__r)+np.dot(other.__r,other.__r)-2*np.dot(self.__r,other.__r)-(self.__R+other.__R)*(self.__R+other.__R)
  52.        
  53.             delta=b*b-4*a*c
  54.             if delta<0:
  55.                 return 100000
  56.        
  57.             if delta==0:
  58.                 return 100000
  59.             dt1=(-b+m.sqrt(delta))/(2*a)
  60.             dt2=(-b-m.sqrt(delta))/(2*a)
  61.        
  62.             if dt1<0 and dt2<0:
  63.                 return 100000
  64.                
  65.             if dt1>0 and dt2>0:
  66.                 return min([dt1,dt2])
  67.                
  68.             if dt1==0 and dt2>0:
  69.                 return 0
  70.            
  71.             if dt2==0 and dt1>0:
  72.                 return 0
  73.            
  74.             if dt1==0 and dt2<0:
  75.                 return 100000
  76.        
  77.             if dt2==0 and dt1<0:
  78.                 return 100000
  79.            
  80.            
  81.                
  82.         if (self.t==-1 and other.t==1) or (self.t==1 and other.t==-1):
  83.             c=np.dot(self.__r,self.__r)+np.dot(other.__r,other.__r)-2*np.dot(self.__r,other.__r)-(self.__R-other.__R)*(self.__R-other.__R)
  84.             delta=b*b-4*a*c
  85.             if delta<0:
  86.                 return 100000
  87.        
  88.             if delta==0:
  89.                 return 100000
  90.             dt1=(-b+m.sqrt(delta))/(2*a)
  91.             dt2=(-b-m.sqrt(delta))/(2*a)
  92.             if dt1<0 and dt2>0:
  93.                 return dt2
  94.                
  95.             if dt1>0 and dt2<0:
  96.                 return dt1
  97.            
  98.             if dt1==0 or dt2==0:
  99.                 return 0
  100.        
  101.                
  102.                
  103.        
  104.        
  105.     def collide(self, other):
  106.         other.__v=np.array(other.__v,dtype='float')
  107.        
  108.         self.__v=-(self.__v)
  109.         other.__v=-(other.__v)
  110.         #other.__v[1]=(np.linalg.norm(other.__v)*m.cos(theta2-phi)(other.m-self.m)+2*self.m*np.linalg.norm(self.__v)*m.cos(theta1-phi))*m.sin(phi)/(self.m+other.m)+np.linalg.norm(other.__v)*m.sin(theta2-phi)*m.cos(phi)
  111. class Gas:
  112.     def __init__(self,list=[Ball(1,1.0,1.0,[2,1], [0.2,0.3],'r'), Ball(1,1.0,1.0,[0,0], [-0.2,0.3],'r'),Ball(1,1.0,1.0,[0,5], [0.2,-0.3],'r'),Ball(1,1.0,1.0,[2,4], [-0.5,0.3],'r'),Ball(-1,500000.0,10.0,[0,0], [0,0],'g')]):
  113.         """
  114.        Initialise the Orbit class for 2 balls and some trivial text
  115.        """
  116.         #self.__ball1 = Ball(0,1.0,1.0,[2,1], [0.2,0.3],'r')
  117.         #self.__ball2=Ball(0,1.0,1.0,[0,0], [-0.2,0.3],'r')
  118.         #self.__ball3=Ball(0,1.0,1.0,[0,5], [0.2,-0.3],'r')
  119.         #self.__ball4=Ball(0,1.0,1.0,[2,3], [-0.5,0.3],'r')
  120.         #self.__container = Ball(1,500000.0,10.0,[0,0], [0,0],'g')
  121.         #self.__list=[self.__ball1,self.__ball2,self.__ball3,self.__ball4,self.__container]
  122.         self.list=[Ball(1,1.0,1.0,[2,1], [0.2,0.3],'r'), Ball(1,1.0,1.0,[0,0], [-0.2,0.3],'r'),Ball(1,1.0,1.0,[0,5], [0.2,-0.3],'r'),Ball(1,1.0,1.0,[2,-7], [-0.5,0.3],'r'),Ball(-1,500000.0,10.0,[0,0], [0,0],'g')]
  123.         self.__text0 = None
  124.  
  125.     def init_figure(self):
  126.         """
  127.        Initialise the container diagram and add it to
  128.        the plot.
  129.        This method is called once by FuncAnimation with no arguments.
  130.        Returns a list or tuple of the 'patches' to be animated.
  131.        """
  132.         # add the big circle to represent the container
  133.         BigCirc = plt.Circle((0,0), 10, ec = 'b', fill = False, ls = 'solid')
  134.         ax.add_artist(BigCirc)
  135.         # initialise the text to be animated and add it to the plot
  136.         self.__text0 = ax.text(-9.9,9,"f={:4d}".format(0,fontsize=12))
  137.         patches = [self.__text0]
  138.        
  139.         # add the patches for the balls to the plot
  140.         for b in self.list:
  141.             pch = b.get_patch()
  142.             ax.add_patch(pch)
  143.             patches.append(pch)
  144.            
  145.         return patches
  146.    
  147.     def change_mom(self):
  148.         change_mom=self.m*self.__v
  149.         return change_mom
  150.        
  151.  
  152.    
  153.  
  154.     def next_frame(self,framenumber):
  155.         self.__text0.set_text("f={:4d}".format(framenumber))
  156.         patches = [self.__text0]
  157.         collision=[]
  158.         for b in self.list:
  159.             for i in range(len(self.list)):
  160.                 for j in range(i+1,len(self.list)):
  161.                
  162.                     collision.append(self.list[i].time_to_collision(self.list[j]))
  163.                     #print ("a")
  164.                     #print (abs(np.linalg.norm(self.list[i].pos()-self.list[j].pos())-(self.list[i].ty()*self.list[i].rad()+self.list[j].ty()*self.list[j].rad())))
  165.                 #print (self.list[i].time_to_collision(self.list[j]))
  166.             #print(collision)                        
  167.             # print("c")
  168.            
  169.             #print("j")
  170.            
  171.            
  172.             if min(collision)==0:
  173.                 for i in range(len(self.list)):
  174.                     for j in range(i+1,len(self.list)):
  175.                         if abs(np.linalg.norm(self.list[i].pos()-self.list[j].pos())-(self.list[i].ty()*self.list[i].rad()+self.list[j].ty()*self.list[j].rad()))<0.1:
  176.                             print ("d")
  177.                             self.list[i].collide(self.list[j])
  178.             else:
  179.                 #print(b.pos())    
  180.                 b.move(min(collision))
  181.                 #print(b.pos())
  182.                 patches.append(b.get_patch())
  183.                 #print (collision)
  184.             #print(b.vel())
  185.            # print ("b")
  186.        
  187.         return patches
  188.    
  189.  
  190. if __name__ == "__main__":
  191.    
  192.     fig=plt.figure()
  193.     ax=plt.axes(xlim=(-10,10),ylim=(-10,10))
  194.     ax.axes.set_aspect('equal')
  195.  
  196.     movie=Gas()
  197.    
  198.     anim=animation.FuncAnimation(fig,
  199.                                  movie.next_frame,
  200.                                  init_func = movie.init_figure,
  201.                                  frames=1000,
  202.                                  interval=20,
  203.                                  blit=True)
  204.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement