Debian Codegraph @

texmf-dist/doc/latex/mandi/vdemo.py

raw
1 from vpython import *
2
3 scene.width = 400
4 scene.height = 760
5 # constants and data
6 g = 9.8 # m/s^2
7 mball = 0.03 # kg
8 Lo = 0.26 # m
9 ks = 1.8 # N/m
10 deltat = 0.01 # s
11
12 # objects (origin is at ceiling)
13 ceiling = box(pos=vector(0,0,0), length=0.2, height=0.01,
14 width=0.2)
15 ball = sphere(pos=vector(0,-0.3,0),radius=0.025,
16 color=color.orange)
17 spring = helix(pos=ceiling.pos, axis=ball.pos-ceiling.pos,
18 color=color.cyan,thickness=0.003,coils=40,
19 radius=0.010)
20
21 # initial values
22 pball = mball * vector(0,0,0) # kg m/s
23 Fgrav = mball * g * vector(0,-1,0) # N
24 t = 0
25
26 # improve the display
27 scene.autoscale = False # turn off automatic camera zoom
28 scene.center = vector(0,-Lo,0) # move camera down
29 scene.waitfor('click') # wait for a mouse click
30
31 # initial calculation loop
32 # calculation loop
33 while t < 10:
34 rate(100)
35 # we need the stretch
36 s = mag(ball.pos) - Lo
37 # we need the spring force
38 Fspring = ks * s * -norm(spring.axis)
39 Fnet = Fgrav + Fspring
40 pball = pball + Fnet * deltat
41 ball.pos = ball.pos + (pball / mball) * deltat
42 spring.axis = ball.pos - ceiling.pos
43 t = t + deltat