Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.mso.anu.edu.au/pfrancis/phys1101/Lectures/L11/traincrash.py
Дата изменения: Wed Mar 17 01:16:09 2010
Дата индексирования: Tue Oct 2 14:28:25 2012
Кодировка:

Поисковые слова: http astrokuban.info astrokuban
from __future__ import division
from visual import *

carpos = vector(-3.0,0.0,0.0)
carvel = vector(0.0,0.0,0.0)
Fcar = vector(0.0,0.0,0.0)
carmass = 1.0
trainpos = vector(7.0,0.0,1.0)
trainvel = vector(-1.0,0.0,0.0)
Ftrain = -Fcar
trainmass = 10.0

k1 = 5.0
k2 = 2.0

arrowscale1 = 3.
arrowscale2 = 3.0
stop = 1

showvel = 0
showcaracc = 1
showtrainacc = 1
elastic = 0

scene1 = display(width=800,height=400,background=color.white,
center=(0.,0.0,0.), autoscale=0)

road = box(pos=(0.0,0.0,-1.0),length=23,height=8,width=1,color=color.gray(0.5))

car = box(pos = carpos, length=2, width=2, height=4,
axis = vector(1.0,0.0,0.0), color=color.blue,
opacity=0.5)

train = box(pos = trainpos, length=6, width=3, height=3,
axis = vector(1.0,0.0,0.0), color=color.yellow,
opacity=0.5)


if showvel == 1:
cmomentum = arrow(pos=car.pos,
axis=carvel*arrowscale1, color=color.red,
shaftwidth=0.4, fixedwidth=1)
tmomentum = arrow(pos=train.pos,
axis=trainvel*arrowscale1, color=color.red,
shaftwidth=0.4, fixedwidth=1)

if showcaracc == 1:
caracc = arrow(pos=(car.pos+0.5*car.axis), axis=vector(0.,0.,0.),
color=color.green, shaftwidth=0.4, fixedwidth=1)

if showtrainacc == 1:
trainacc = arrow(pos=(car.pos+0.5*car.axis), axis=vector(0.,0.,0.),
color=color.green, shaftwidth=0.4, fixedwidth=1)


t = 0.
tstep = 0.01
tmax = 1000.0

while t < tmax:
if scene1.mouse.clicked:
m = scene1.mouse.getclick()
if stop == 0:
stop = 1
else:
stop = 0
if stop == 0:
rate(300)
sep = (train.pos[0]-car.pos[0])-0.5*(car.length+train.length)
if sep < 0.0:
if elastic == 1:
Fcar = vector(k1*sep,0.0,0.0) #Elastic
else:
Fcar = -k2*(carvel-trainvel) # Crumple
Ftrain = -Fcar
else:
Fcar = vector(0.0,0.0,0.0)
Ftrain = vector(0.0,0.0,0.0)
trainvel += tstep*Ftrain/trainmass
carvel += tstep*Fcar/carmass
car.pos += tstep*carvel
train.pos += tstep*trainvel
if showvel == 1:
cmomentum.pos = car.pos
cmomentum.axis = carvel*arrowscale1
tmomentum.pos = train.pos
tmomentum.axis = trainvel*arrowscale1
if showcaracc == 1:
caracc.pos = (car.pos+0.5*car.axis)
caracc.axis = Fcar*arrowscale2
if showtrainacc == 1:
trainacc.pos = (car.pos+0.5*car.axis)
trainacc.axis = Ftrain*arrowscale2