Debian Codegraph @

examples/tvtk/visual/gyro2.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 """A simple example demonstrating the creation of actors and animating
4 the in a scene using visual modeule."""
5
6 # Author: Raashid Baig <[email protected]>
7 # License: BSD Style.
8 from math import pi, cos, sin
9
10 from tvtk.tools.visual import vector, box, cylinder, frame, curve, \
11 iterate, show
12
13 # Gyroscope sitting on a pedestal
14
15 # The analysis is in terms of Lagrangian mechanics.
16 # The Lagrangian variables are polar angle theta,
17 # azimuthal angle phi, and spin angle alpha.
18
19 Lshaft = 1. # length of gyroscope shaft
20 Rshaft = 0.03 # radius of gyroscope shaft
21 M = 1. # mass of gyroscope (massless shaft)
22 Rrotor = 0.4 # radius of gyroscope rotor
23 Drotor = 0.1 # thickness of gyroscope rotor
24 Dsquare = 1.4*Drotor # thickness of square that turns with rotor
25 I = 0.5*M*Rrotor**2. # moment of inertia of gyroscope
26 hpedestal = Lshaft # height of pedestal
27 wpedestal = 0.1 # width of pedestal
28 tbase = 0.05 # thickness of base
29 wbase = 3.*wpedestal # width of base
30 g = 9.8
31 Fgrav = vector(0,-M*g,0)
32 top = vector(0,0,0) # top of pedestal
33
34 theta = pi/3. # initial polar angle of shaft (from vertical)
35 thetadot = 0 # initial rate of change of polar angle
36 alpha = 0 # initial spin angle
37 alphadot = 15 # initial rate of change of spin angle (spin ang. velocity)
38 phi = -pi/2. # initial azimuthal angle
39 phidot = 0 # initial rate of change of azimuthal angle
40 # Comment in following line to get pure precession
41 ##phidot = (-alphadot+sqrt(alphadot**2+2*M*g*r*cos(theta)/I))/cos(theta)
42
43 pedestal = box(pos=top-vector(0,hpedestal/2.,0),
44 height=hpedestal, length=wpedestal, width=wpedestal,
45 color=(0.4,0.4,0.5))
46 base = box(pos=top-vector(0,hpedestal+tbase/2.,0),
47 height=tbase, length=wbase, width=wbase,
48 color=pedestal.color)
49
50 shaft = cylinder(axis=(Lshaft,0,0), length = Lshaft,
51 radius=Rshaft, color=(0,1,0))
52 rotor = cylinder(pos=(Lshaft/2 - Drotor/2, 0, 0),
53 axis=(Drotor, 0, 0), length = Drotor,
54 radius=Rrotor, color=(1,0,0))
55
56 gyro = frame(shaft, rotor)
57 gyro.axis = (sin(theta)*sin(phi),cos(theta),sin(theta)*cos(phi))
58
59 trail = curve(radius=Rshaft/8., color=(1,1,0))
60
61 r = Lshaft/2.
62 dt = 0.0001
63 t = 0.
64 Nsteps = 20 # number of calculational steps between graphics updates
65
66 def anim():
67 global theta, phidot, alphadot, M, g, r, thetadot, phi, alpha, t
68 for step in range(Nsteps): # multiple calculation steps for accuracy
69 # Calculate accelerations of the Lagrangian coordinates:
70 atheta = (phidot**2*sin(theta)*cos(theta)
71 -2.*(alphadot+phidot*cos(theta))*phidot*sin(theta)
72 +2.*M*g*r*sin(theta)/I)
73 aphi = 2.*thetadot*(alphadot-phidot*cos(theta))/sin(theta)
74 aalpha = phidot*thetadot*sin(theta)-aphi*cos(theta)
75 # Update velocities of the Lagrangian coordinates:
76 thetadot = thetadot+atheta*dt
77 phidot = phidot+aphi*dt
78 alphadot = alphadot+aalpha*dt
79 # Update Lagrangian coordinates:
80 theta = theta+thetadot*dt
81 phi = phi+phidot*dt
82 alpha = alpha+alphadot*dt
83
84 gyro.axis = vector(sin(theta)*sin(phi),cos(theta),sin(theta)*cos(phi))
85 # Display approximate rotation of rotor and shaft:
86 gyro.rotate(axis = gyro.axis, angle=alphadot*dt*Nsteps, origin = gyro.pos)
87 trail.append(gyro.pos + gyro.axis * Lshaft)
88 t = t+dt*Nsteps
89
90 a = iterate(40, anim)
91 a.edit_traits()
92 show()