Advertisement
Ebo1984

Untitled

Mar 29th, 2017
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. # chaotic system of equations visualizer
  2. # code by : nando takeshiro
  3. # email: nandotakeshiro@hotmail.com
  4. # please keep the above as part of the code
  5.  
  6.  
  7.  
  8. import matplotlib.pyplot as plt
  9. from mpl_toolkits.mplot3d import Axes3D
  10. import colorsys
  11. import numpy as np
  12. from random import random
  13. import time
  14. import winsound
  15.  
  16.  
  17. # main function for saving multiple runs of frames
  18. def main():
  19.  
  20.     # set up figures and axes
  21.     fig = plt.figure(figsize=(12,12))
  22.     ax = fig.add_subplot(111, projection = '3d', aspect='equal')
  23.     ax.axis('off')
  24.     ax.set_axis_bgcolor((0,  0,  0))
  25.  
  26.     # lists of x, y and z coordinates
  27.     xl, yl, zl = [], [], []
  28.  
  29.     # b sets up axis limits
  30.     b = 3.5
  31.  
  32.     # time step
  33.     dt = np.pi/32.
  34.  
  35.     # number of lines to draw
  36.     sc = 250.
  37.     sci = int(sc)
  38.  
  39.     # scalar size
  40.     u = random() * 5
  41.  
  42.  
  43.     # random scalars
  44.     p = (-(u/2) + (u * random())) * np.pi
  45.     q = -(u/2) + (u * random())
  46.     r = -(u/2) + (u * random())
  47.     s = -(u/2) + (u * random())
  48.     v = -(u/2) + (u * random())
  49.     w = -(u/2) + (u * random())
  50.    
  51.  
  52.     # initial radius
  53.     f = 4 * random()
  54.  
  55.     # index to choose x, y or z
  56.     def rnd():
  57.         i = int(3 * random())
  58.         return i
  59.  
  60.     # indices
  61.     a1, a2, a3 = rnd(), rnd(), rnd()
  62.  
  63.     # vectorfield - main formula
  64.     def vectorfield(x,y,z):
  65.         xyz = [x,y,z]
  66.         i = q * np.cos(xyz[a3] / (xyz[a2] + p * (xyz[a1] + q) * np.pi + z) * np.pi)
  67.         j = r * np.sin(xyz[a2] * np.cos(np.pi * x) / (xyz[a2] + r * -(x + s)) * np.pi + x)
  68.         k = w * np.sin(xyz[a1] / (xyz[a2] + v / (xyz[a3] - xyz[a2] + w)) * np.pi + xyz[a1])
  69.  
  70.         return i, j, k
  71.  
  72.     # viewing angle
  73.     ax.view_init(-90, 30)
  74.  
  75.     print "Calculating lines..."
  76.     for pts in range(0,(sci*2)):
  77.  
  78.         hue = random()
  79.         # set up initial coordinates
  80.         x = f * np.cos((pts/sc)*np.pi)
  81.         y = f * np.sin((pts/sc)*np.pi)
  82.         z = f * np.sin((pts/sc)*np.pi)
  83.  
  84.         xl, yl, zl = [], [], []
  85.  
  86.         # length of curves
  87.         for t in np.arange(0, 2000, dt):
  88.             i, j, k = vectorfield(x, y, z)
  89.             x = x + (i * dt)
  90.             y = y + (j * dt)
  91.             z = z + (k * dt)
  92.             xl.append(x)
  93.             yl.append(y)
  94.             zl.append(z)
  95.            
  96.         # show curves
  97.         ax.plot(xl,yl,zl, '-', zdir='z',alpha=0.4,
  98.             color = colorsys.hsv_to_rgb(hue,1.0,1.0))
  99.  
  100.  
  101.  
  102.     ax.set_xlim((-b,b))
  103.     ax.set_ylim((-b,b))
  104.     ax.set_zlim((-b,b))
  105.  
  106.  
  107.  
  108.  
  109.  
  110.     save_set = 1
  111.     if save_set == 1:
  112.  
  113.        
  114.         # save individual frames
  115.         # comment this out if you only want to see the plot
  116.         unique_id = str(random()*3) + '_'
  117.         n_frames = 1
  118.         zoom_depth = 3.
  119.  
  120.         # create log file of all parameters
  121.         set_wr = open('FS%s log.txt' % unique_id, 'w+')
  122.         set_wr.write('ID: FS %s' % unique_id + '\n')
  123.         set_wr.write('b = %s (initial x,y,z lim)' % str(b) + '\n')
  124.         set_wr.write('u = %s' % str(u) + '\n')
  125.         set_wr.write('p = %s' % str(p) + '\n')
  126.         set_wr.write('q = %s' % str(q) + '\n')
  127.         set_wr.write('r = %s' % str(r) + '\n')
  128.         set_wr.write('s = %s' % str(s) + '\n')
  129.         set_wr.write('v = %s' % str(v) + '\n')
  130.         set_wr.write('w = %s' % str(w) + '\n')
  131.         set_wr.write('f = %s' % str(f) + '\n')
  132.         set_wr.write('for a1,a2,a3: 0 = x, 1 = y, 2 = z' + '\n')
  133.         set_wr.write('a1 = %s' % str(a1) + '\n')
  134.         set_wr.write('a1 = %s' % str(a2) + '\n')
  135.         set_wr.write('a1 = %s' % str(a3) + '\n')
  136.         set_wr.write('number of drawn lines: %.0f' % sc)
  137.         set_wr.close()
  138.  
  139.  
  140.        
  141.         for frames in range(0,n_frames):
  142.  
  143.             # zooms from b to b-zoom_depth and back
  144.  
  145.             fstep = (frames/(1.*n_frames)) * np.pi
  146.             fsin = zoom_depth * (np.sin(fstep))**2
  147.             lim = b - fsin
  148.  
  149.            
  150.            
  151.             ax.set_xlim((-lim,lim))
  152.             ax.set_ylim((-lim,lim))
  153.             ax.set_zlim((-lim,lim))
  154.  
  155.  
  156.             ax.view_init(-90+frames,30+frames)
  157.             remaining_time = (2880 - (8 * frames))/60.
  158.            
  159.            
  160.             fname = 'FS' + unique_id + str(frames) + '.png'
  161.             print "Saving: %s" % fname
  162.             plt.savefig(fname, format='png',dpi=300,
  163.                         bbox_inches='tight', pad_inches=0)
  164.  
  165.         winsound.Beep(2000,500)
  166.     else:
  167.         plt.show()
  168.  
  169.  
  170.    
  171. #main function, generates 50 images
  172. for images in range(0,50):
  173.     print "Image: %d" % (images + 1)
  174.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement