Debian Codegraph @

examples/tvtk/visual/doublependulum.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
9 # Double pendulum
10
11 #The analysis is in terms of Lagrangian mechanics. The Lagrangian
12 #variables are angle of upper bar, angle of lower bar,measured from
13 #the vertical.
14
15 from tvtk.tools.visual import box, vector, frame, iterate, show
16 from math import pi, sin
17
18 g = 9.8
19 M1 = 2.0
20 M2 = 1.0
21 d = 0.05 # thickness of each bar
22 gap = 2.*d # distance between two parts of upper, U-shaped assembly
23 L1 = 0.5 # physical length of upper assembly; distance between axles
24 L1display = L1+d # show upper assembly a bit longer than physical, to overlap axle
25 L2 = 1.0 # physical length of lower bar
26 L2display = L2+d/2. # show lower bar a bit longer than physical, to overlap axle
27 # Coefficients used in Lagrangian calculation
28 A = (1./4.)*M1*L1**2+(1./12.)*M1*L1**2+M2*L1**2
29 B = (1./2.)*M2*L1*L2
30 C = g*L1*(M1/2.+M2)
31 D = M2*L1*L2/2.
32 E = (1./12.)*M2*L2**2+(1./4.)*M2*L2**2
33 F = g*L2*M2/2.
34
35 hpedestal = 1.3*(L1+L2) # height of pedestal
36 wpedestal = 0.1 # width of pedestal
37 tbase = 0.05 # thickness of base
38 wbase = 8.*gap # width of base
39 offset = 2.*gap # from center of pedestal to center of U-shaped upper assembly
40 top = vector(0,0,0) # top of inner bar of U-shaped upper assembly
41
42 theta1 = 1.3*pi/2. # initial upper angle (from vertical)
43 theta1dot = 0 # initial rate of change of theta1
44 theta2 = 0 # initial lower angle (from vertical)
45 theta2dot = 0 # initial rate of change of theta2
46
47 pedestal = box(pos = (top - vector(0, hpedestal/2.0, offset)),size = (wpedestal, 1.1*hpedestal, wpedestal), color = (0.4,0.4,0.5))
48
49 base = box(pos = (top - vector(0,hpedestal + tbase/2.0, offset)),size=(wbase, tbase, wbase),color = (0.4,0.4,0.5))
50
51 bar1 = box(pos=(L1display/2.0 - d/2.0, 0, -(gap+d)/2.0), size=(L1display, d, d), color=(1,0,0))
52 bar1b = box(pos=(L1display/2.0 - d/2.0, 0, (gap+d)/2.0), size=(L1display, d, d), color=(1,0,0))
53
54 frame1 = frame(bar1, bar1b)
55 frame1.pos = (0.0, 0.0, 0.0)
56 frame1.axis = (0.0, -1.0, 0.0)
57 frame1.rotate(axis=(0,0,1), angle = 180.0*theta1/pi)
58
59 bar2 = box(pos = (L2display/2.0 - d/2.0, 0, 0), size = (L2display, d, d), color = (0,1,0))
60
61 frame2 = frame(bar2)
62 frame2.pos = (0.0, -1.0*L1, 0.0)
63 frame2.axis = (0.0, -1.0, 0.0)
64 frame2.rotate(axis = (0,0,1), angle = 180.0*theta2/pi)
65
66 dt = 0.001
67
68 def anim():
69 global theta1, theta2, theta1dot, theta2dot
70 atheta1 = ((E*C/B)*sin(theta1)-F*sin(theta2))/(D-E*A/B)
71 atheta2 = -(A*atheta1+C*sin(theta1))/B
72
73 theta1dot = theta1dot + atheta1*dt
74 theta2dot = theta2dot + atheta2*dt
75
76 dtheta1 = theta1dot*dt
77 dtheta2 = theta2dot*dt
78 theta1 = theta1 + dtheta1
79 theta2 = theta2 + dtheta2
80
81 frame1.rotate(axis = (0,0,1), angle = 180.0*dtheta1/pi)
82
83 frame2.pos = top + frame1.axis*L1
84 frame2.rotate(axis = (0,0,1), angle = 180*dtheta2/pi)
85
86 a = iterate(20, anim)
87 a.edit_traits()
88 show()