[关闭]
@Guozhongzhi 2016-11-20T14:46:24.000000Z 字数 1209 阅读 706

Problem3.31代码

计算物理


  1. #Sinai billiard
  2. from __future__ import division, print_function
  3. from visual import *
  4. r=0.6
  5. rb2=(2.+0.5*r)**2
  6. L=8
  7. side = 8.0
  8. thk = 0.3
  9. s2 = 2*side - thk
  10. s3 = 2*side + thk
  11. wallR = box (pos=( L, 0, 0), size=(thk, s2, s3), color = (0.7, 0.7, 0.7))
  12. wallL = box (pos=(-L, 0, 0), size=(thk, s2, s3), color = (0.7, 0.7, 0.7))
  13. wallB = box (pos=(0, -L, 0), size=(s3, thk, s3), color = (0.7, 0.7, 0.7))
  14. wallT = box (pos=(0, L, 0), size=(s3, thk, s3), color = (0.7, 0.7, 0.7))
  15. wallBK = box(pos=(0, 0, -0.5), size=(s2, s2, thk), color = color.green)
  16. inner=sphere(pos=(0,0,0), radius=2, color=color.cyan) #Fixed obstacle
  17. ball=sphere(pos=(2,3,0),radius=r,color=color.red,make_trail=True,retain=200) # Moving ball
  18. ball.trail_object.radius = 0.05
  19. v0=vector(1,-1.01,0)
  20. ball.v=v0
  21. trail=curve()
  22. vel=sqrt(ball.v.x**2+ball.v.y**2)
  23. dt=0.05
  24. for i in arange(15000):
  25. rate(1000)
  26. ball.pos=ball.pos+ball.v*dt # update position
  27. if mod(i,20)==0:
  28. trail.append(pos=ball.pos) #sample position, sample time= 20*dt
  29. if not L>ball.x > -L: #bounce off walls
  30. ball.v.x=-ball.v.x
  31. if not L>ball.y >- L:
  32. ball.v.y=-ball.v.y
  33. rball2=ball.x**2+ball.y**2
  34. phinew=atan2(ball.v.y,ball.v.x)
  35. if rball2 < rb2: #bounce off ball in middle
  36. theta=atan2(ball.y,ball.x)
  37. phi=atan2(ball.v.y,ball.v.x)
  38. alpha=phi-theta
  39. phinew=theta-alpha
  40. ball.v=vector(-vel*cos(phinew),-vel*sin(phinew),0)
  41. phinew=atan2(ball.v.y,ball.v.x)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注