osc1.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from pysketcher import *
  2. L = 12.
  3. H = L/6
  4. W = L/6
  5. xmax = L
  6. drawing_tool.set_coordinate_system(xmin=-L, xmax=xmax,
  7. ymin=-1, ymax=L+H,
  8. axis=False,
  9. instruction_file='tmp_mpl.py')
  10. x = 0
  11. drawing_tool.set_linecolor('black')
  12. def make_dashpot(x):
  13. d_start = (-L,2*H)
  14. d = Dashpot(start=d_start, total_length=L+x, width=W,
  15. bar_length=3*H/2, dashpot_length=L/2, piston_pos=H+x)
  16. d.rotate(-90, d_start)
  17. return d
  18. def make_spring(x):
  19. s_start = (-L,4*H)
  20. s = Spring(start=s_start, length=L+x, bar_length=3*H/2, teeth=True)
  21. s.rotate(-90, s_start)
  22. return s
  23. d = make_dashpot(0)
  24. s = make_spring(0)
  25. M = Rectangle((0,H), 4*H, 4*H).set_linewidth(4)
  26. left_wall = Rectangle((-L,0),H/10,L).set_filled_curves(pattern='/')
  27. ground = Wall(x=[-L/2,L], y=[0,0], thickness=-H/10)
  28. wheel1 = Circle((H,H/2), H/2)
  29. wheel2 = wheel1.copy()
  30. wheel2.translate(point(2*H, 0))
  31. fontsize = 18
  32. text_m = Text('$m$', (2*H, H+2*H), fontsize=fontsize)
  33. text_kx = Text('$kx$', (-L/2, H+4*H), fontsize=fontsize)
  34. text_bv = Text('$b\dot x$', (-L/2, H), fontsize=fontsize)
  35. x_axis = Axis((2*H, L), H, '$x(t)$', fontsize=fontsize,
  36. label_spacing=(0.04, -0.01))
  37. x_axis_start = Line((2*H, L-H/4), (2*H, L+H/4)).set_linewidth(4)
  38. fig = Composition({
  39. 'spring': s, 'mass': M, 'left wall': left_wall,
  40. 'ground': ground, 'wheel1': wheel1, 'wheel2': wheel2,
  41. 'text_m': text_m, 'text_kx': text_kx,
  42. 'x_axis': x_axis, 'x_axis_start': x_axis_start})
  43. fig.draw()
  44. drawing_tool.display()
  45. drawing_tool.savefig('tmp_oscillator_spring')
  46. drawing_tool.erase()
  47. fig['dashpot'] = d
  48. fig['text_bv'] = text_bv
  49. # or fig = Composition(dict(fig=fig, dashpot=d, text_bv=text_bv))
  50. fig.draw()
  51. drawing_tool.display()
  52. drawing_tool.savefig('tmp_oscillator')
  53. drawing_tool.erase()
  54. text_kx = Text('$s(u)$', (-L/2, H+4*H), fontsize=fontsize)
  55. text_bv = Text('$f(\dot u)$', (-L/2, H), fontsize=fontsize)
  56. x_axis = Axis((2*H, L), H, '$u(t)$', fontsize=fontsize,
  57. label_spacing=(0.04, -0.01))
  58. F_force = Force((4*H, H+2*H), (4*H+H, H+2*H), '$F(t)$',
  59. text_spacing=(0.035, -0.01), text_pos='end', fontsize=fontsize)
  60. fig['text_kx'] = text_kx
  61. fig['text_bv'] = text_bv
  62. fig['x_axis'] = x_axis
  63. fig['F_force'] = F_force
  64. fig.draw()
  65. drawing_tool.savefig('tmp_oscillator_general')
  66. raw_input()