Advertisement
larsupilami73

Lorenz attractor divergence animation

Jul 10th, 2019
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. #!/usr/bin/python3
  2. #lorenz system demo
  3. #larsupilami73
  4.  
  5. import os,sys
  6. from scipy import *
  7. from scipy import integrate
  8. import matplotlib.pyplot as plt
  9. from mpl_toolkits.mplot3d import Axes3D
  10.  
  11. #parameters
  12. sigma = 10.
  13. rho = 28.
  14. beta = 8/3.
  15.  
  16. tbegin = 0.
  17. tend   = 200.
  18. tstep = 0.01
  19. T = linspace(tbegin,tend, int((tend-tbegin)/tstep))
  20.  
  21. #system
  22. def dxdt(X, t=0):
  23.     x = X[0]
  24.     y = X[1]
  25.     z = X[2]
  26.     return array([sigma * (y - x),x * (rho - z ) - y, x*y - beta*z])
  27.  
  28.  
  29. #initial run to find consistent initial values
  30. X0=[0.5,0.5,0.5]
  31. X, infodict = integrate.odeint(dxdt, X0, T, full_output=1)
  32. #infodict['message'] # >>> 'Integration successful.' if argument full_output=True
  33. x,y,z = X.T
  34. #select closeby initial values to step over the attractor
  35. numpoints = 20
  36. initial_distance = 0.2
  37. xp=[x[-1]+random.normal(scale=initial_distance) for dummy in range(numpoints)]
  38. yp=[y[-1]+random.normal(scale=initial_distance) for dummy in range(numpoints)]
  39. zp=[z[-1]+random.normal(scale=initial_distance) for dummy in range(numpoints)]
  40.  
  41. savetxt('out.dat',vstack((x,y,z,T)).T)
  42. #sys.exit(0)
  43.  
  44. numframes = 200
  45. try:
  46.     os.mkdir('frames')
  47. except:
  48.     pass
  49. try:
  50.     os.system('rm -f ./frames/*.png')   #remove previous run frames
  51. except:
  52.     pass
  53. fig = plt.figure(1, figsize=(8, 6)) #make the figure once
  54. for k in range(numframes):
  55.     for l in range(numpoints):
  56.         T = [0,0.01,0.02,0.03,0.04] #range(10)* tstep
  57.         X0=[xp[l],yp[l],zp[l]]
  58.         X, infodict = integrate.odeint(dxdt, X0, T, full_output=1)
  59.         #infodict['message'] # >>> 'Integration successful.' if argument full_output=True
  60.         x1,y1,z1 = X.T
  61.    
  62.         #new initial values
  63.         xp[l] = x1[-1]
  64.         yp[l] = y1[-1]
  65.         zp[l] = z1[-1]
  66.        
  67.     #plot it
  68.     plt.cla()
  69.     plt.clf()
  70.    
  71.     #plt.margins((0,0.02))
  72.     #plt.rc('mathtext', fontset = 'cm') #make sure mathtext uses nice font.
  73.     #plt.rc('mathtext', rm='serif')
  74.    
  75.     ax = Axes3D(fig, elev=-156, azim=110+k/2.)
  76.    
  77.     fig.set_facecolor('black')
  78.     ax.set_facecolor('black')
  79.     ax.grid(False)
  80.     ax.w_xaxis.pane.fill = False
  81.     ax.w_yaxis.pane.fill = False
  82.     ax.w_zaxis.pane.fill = False
  83.    
  84.     #ax.set_title('rho=%f'%rho)
  85.     no_points = len(x)
  86.     cm = plt.get_cmap('binary_r')
  87.     #this worked for previous matplotlib versions
  88.     #then no c=c needed in ax.plot below
  89.     #ax.set_color_cycle([cm(1.*i/(no_points-1)) for i in range(no_points-1)])
  90.     for i in range(no_points-1):
  91.         c = cm(int(255*i/(no_points-1)))
  92.         bar = ax.plot(x[i:i+2],y[i:i+2],z[i:i+2], c=c,linewidth=0.6, alpha=0.5)
  93.    
  94.     ax.scatter(xp,yp,zp, c=range(numpoints), cmap=plt.cm.tab20, edgecolor='white', s=30.0,alpha=1.0,marker='o')
  95.    
  96.     ax.set_xlim(-26,26)
  97.     ax.set_ylim(-20,20)
  98.     ax.set_zlim(5,45)
  99.     ax.set_xlabel('x', color='white')
  100.     ax.set_ylabel('y',color='white')   
  101.     ax.set_zlabel('z',color='white')
  102.    
  103.     plt.savefig('./frames/frame%05d.png' %k)
  104.     ax.clear()
  105.     print('Frame number: %d' %k)
  106.    
  107.    
  108. #now assemble all the pictures in a movie
  109. #print('converting to gif!')
  110. #os.system('convert -delay 40 -loop 0 ./frames/*.png fb_tot.gif')
  111. #or to mp4
  112. print('converting to mp4!')
  113. os.system("ffmpeg -y -r 40 -i ./frames/frame%05d.png -c:v libx264 -vf fps=20 lorenz.mp4")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement