# LorenzODE.py: The Lorenz chaotic attractor in interactive 3D.
#       Note the use of the Euler integration method.

from visual import *

print """
Right button drag to rotate camera to view scene.
Middle button drag up or down to zoom in or out
"""
scene.title = "Lorenz differential equation"
scene.center = vector(0,0,25)
# If you can cross your eyes to see stereo, uncomment the next two lines.
#scene.stereo = 'crosseyed'
#scene.stereodepth = 2
scene.height = 600
scene.width = 600
scene.background = (1,1,1)
scene.forward = (0,1,-0.5)
scene.lights = [ vector(-0.25, -0.5, -1.0) ,vector(0.25, 0.5, 1.0) ]

# Draw axes
curve( pos = [ (-25,0,0), (25,0,0) ], color = (0,0,0), radius = 0.2 )
curve( pos = [ (0,-25,0), (0,25,0) ], color = (0,0,0), radius = 0.2 )
curve( pos = [ (0,0,0), (0,0,50) ], color = (0,0,0), radius = 0.2 )

lorenz = curve( color = color.black, radius=0.2 )

dt = 0.001
y = vector(-10,-7,35)
sigma = 10.0
R = 28.0
b = -8.0/3.0
theta = 3.0 * pi / 4.0

for t in arange(0,10,dt):
  dydt = vector(sigma * (-y[0] + y[1]),
                R*y[0] - y[0]*y[2] - y[1],
                b*y[2] + y[0]*y[1]);
  y = y + dydt*dt
  # Draw lines colored by speed
  c = clip( [mag(dydt) * 0.005], 0, 1 )[0]
  xt = cos(theta) * y[0] - sin(theta) * y[1]
  yt = sin(theta) * y[0] + cos(theta) * y[1]
  lorenz.append( pos=(xt,yt,y[2]), color=(c,0.1, 1-c) )
  #lorenz.append( pos=y, color=(c,0.1, 1-c) )
