ETikhonov

31

Sep 8th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.58 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #Copyright E.V.Tikhonov 2013
  3. #Distributed under the terms of the GNU General Public License v2
  4.  
  5. import numpy as np
  6. import math
  7. from math import pi
  8. from math import e
  9. from matplotlib import pyplot as pl
  10. from matplotlib import animation
  11.  
  12. g=9.81 #gravity constant
  13. R=2    #vessel radius
  14.  
  15. class Solution(object):
  16.     """
  17.     Class which implements analytical solution of differential equation
  18.    m\frac{dV}{dt}=\frac{4}{3}\pi r^3g(\rho_p-\rho_m)-6\pi\eta rV
  19.    for particle with density rho_p moving through medium with density
  20.    rho_m under gravity force.
  21.  
  22.     """
  23.     def __init__(self,x,v,a,t,r=0.3,rho_m=1.261,rho_p=11.34,eta=14.9):
  24.         """
  25.                 Parameters
  26.        ----------
  27.        x : array, float
  28.            length-N array of spatial coordinate
  29.        v : array, float
  30.            length-N array of particle instantaneous velocity    
  31.        a : array, float
  32.            length-N array of particle acceleration                        
  33.        t : array, float
  34.            length-N array of evenly distributed time coordinate
  35.        r : float
  36.            particle radius
  37.  
  38.        rho_p: float
  39.            particle density, g*cm**-3
  40.            aluminium:    2.70
  41.            iron:        7.874
  42.            copper:     8.96
  43.            lead:       11.34
  44.            gold:        11.6
  45.            uranium:        19.1
  46.        rho_m: float
  47.            medium density, g*cm**-3
  48.            mercury:        13.534
  49.            glycerol:        1.261
  50.            sulphuric acid:    1.8356
  51.            ethylene glycol:    1.1132
  52.            water:            0.9982
  53.        eta: float
  54.            medium viscosity, P
  55.            glycerol:         14.9
  56.            supluric acid:    0.242
  57.            ethylene glycol:    0.161
  58.            mercury:        1.526e-2
  59.            water:            8.94e-3
  60.         """
  61.         self.x,self.v,self.a,self.t = map (np.asarray,(x,v,a,t))
  62.         N = self.x.size
  63.         N = self.v.size
  64.         N = self.a.size
  65.         N = self.t.size
  66.         self.r=r
  67.         assert self.x.shape == (N,)
  68.         assert self.v.shape == (N,)
  69.         assert self.a.shape == (N,)
  70.         assert self.t.shape == (N,)
  71.         self.N = len(t)
  72.         dt = t[1]-t[0]
  73. #       A = (4/3.)*pi*r**3*g*(rho_p-rho_m)
  74.         m = rho_p*(4/3.)*pi*r**3
  75.         def set_v_from_t(_t):
  76.             result=(2/9.)*(r**2*g*(rho_p-rho_m)/eta)*(1-e**-((6*pi*eta*r*_t)/m))
  77.             return result;
  78.         def set_a_from_t(_t):
  79.             result=(4/3.)*(pi*r**3/m)*g*(rho_p-rho_m)*e**-((6*pi*eta*r*_t)/m)
  80.             return result;
  81.         _x=0
  82.         for i in range(N):
  83.             if _x >= 60:
  84.                 x[i]=60
  85.                 v[i]=0
  86.                 a[i]=0
  87.             else:
  88.                 v[i] = float(set_v_from_t(t[i]))
  89.                 x[i] = _x+v[i]*dt
  90.                 a[i] = set_a_from_t(t[i])
  91.                 _x=x[i]
  92.                 print t[i],x[i],v[i],a[i]
  93.  
  94. N = 2**13
  95. dt = 0.01
  96. x = 0.0 * np.arange(N)
  97. v = 0.0 * np.arange(N)
  98. a = 0.0 * np.arange(N)
  99. t = dt * (np.arange(N))
  100. #t_max = 120
  101. x_max=60
  102. #N_steps = 50
  103. frames = N
  104. S = Solution(x=x,v=v,a=a,t=t)
  105.  
  106. fig=pl.figure()
  107. hlim=(-R,R)
  108. xmin=0
  109. #xmax=S.x[S.N-1]
  110. xmax=60
  111. ax=fig.add_subplot(111,xlim=(-R*5,R*5),ylim=(xmax+xmax/10,xmin-xmax/10))
  112. #ax.set_xticks((-R,0,R))
  113. #ax.set_yticks((0,5,10,15))
  114. pl.axhspan(0,60,xmin=0.5-0.05*R,xmax=0.5+0.05*R,facecolor='#8888ff',alpha=0.5)
  115. title = ax.set_title("")
  116. part, = ax.plot([],[],'o',ms=45*S.r)
  117. time_text = ax.text(0, 0.9, '', transform=ax.transAxes)
  118. time_template = 'time = %.2fs'
  119. def init():
  120.     part.set_data([],[])
  121.     time_text.set_text('')
  122.     return part, time_text
  123.    
  124. def animate(i):
  125.     part.set_data(0.0,S.x[i])
  126.     time_text.set_text(time_template%(S.t[i]))
  127.     return part,time_text
  128.  
  129.  
  130. anim=animation.FuncAnimation(fig, animate, init_func=init,frames=frames, interval=10, blit=True)
  131.  
  132. pl.show()
Advertisement
Add Comment
Please, Sign In to add comment