Hans Petter Langtangen 10 năm trước cách đây
mục cha
commit
50943d31b5
4 tập tin đã thay đổi với 144 bổ sung13 xóa
  1. 30 7
      examples/osc.py
  2. 73 0
      examples/osc2.py
  3. 24 3
      examples/pendulum.py
  4. 17 3
      pysketcher/shapes.py

+ 30 - 7
examples/osc.py

@@ -29,7 +29,7 @@ d = make_dashpot(0)
 s = make_spring(0)
 
 M = Rectangle((0,H), 4*H, 4*H).set_linewidth(4)
-left_wall = Rectangle((-L,0),H/10,4*H).set_filled_curves(pattern='/')
+left_wall = Rectangle((-L,0),H/10,L).set_filled_curves(pattern='/')
 ground = Wall(x=[-L/2,L], y=[0,0], thickness=-H/10)
 wheel1 = Circle((H,H/2), H/2)
 wheel2 = wheel1.copy()
@@ -44,16 +44,39 @@ x_axis = Axis((2*H, L), H, '$x(t)$', fontsize=fontsize,
 x_axis_start = Line((2*H, L-H/4), (2*H, L+H/4)).set_linewidth(4)
 
 fig = Composition({
-    'dashpot': d, 'spring': s, 'mass': M, 'left wall': left_wall,
+    'spring': s, 'mass': M, 'left wall': left_wall,
     'ground': ground, 'wheel1': wheel1, 'wheel2': wheel2,
-    'text_m': text_m, 'text_kx': text_kx, 'text_bv': text_bv,
+    'text_m': text_m, 'text_kx': text_kx,
     'x_axis': x_axis, 'x_axis_start': x_axis_start})
 
 fig.draw()
-#s.draw()
-print s
-print s.shapes['bar1']['line'].x, s.shapes['bar1']['line'].y
-print s.shapes['bar2']['line'].x, s.shapes['bar2']['line'].y
+drawing_tool.display()
+drawing_tool.savefig('oscillator_spring')
+
+drawing_tool.erase()
+
+fig['dashpot'] = d
+fig['text_bv'] = text_bv
+
+# or fig = Composition(dict(fig=fig, dashpot=d, text_bv=text_bv))
+fig.draw()
+
 drawing_tool.display()
 drawing_tool.savefig('oscillator')
+
+drawing_tool.erase()
+
+text_kx = Text('$s(u)$', (-L/2, H+4*H), fontsize=fontsize)
+text_bv = Text('$f(\dot u)$', (-L/2, H), fontsize=fontsize)
+x_axis = Axis((2*H, L), H, '$u(t)$', fontsize=fontsize,
+              label_spacing=(0.04, -0.01))
+F_force = Force((4*H, H+2*H), (4*H+H, H+2*H), '$F(t)$',
+                text_spacing=(0.035, -0.01), text_pos='end', fontsize=fontsize)
+fig['text_kx'] = text_kx
+fig['text_bv'] = text_bv
+fig['x_axis'] = x_axis
+fig['F_force'] = F_force
+fig.draw()
+drawing_tool.savefig('oscillator_general')
+
 raw_input()

+ 73 - 0
examples/osc2.py

@@ -0,0 +1,73 @@
+"""As osc.py, but without wheels."""
+from pysketcher import *
+
+L = 12.
+H = L/6
+W = L/6
+
+xmax = L
+drawing_tool.set_coordinate_system(xmin=-L, xmax=xmax,
+                                   ymin=-1, ymax=L+H,
+                                   axis=False,
+                                   instruction_file='tmp_mpl.py')
+x = 0
+drawing_tool.set_linecolor('black')
+
+def make_dashpot(x):
+    d_start = (-L,2*H)
+    d = Dashpot(start=d_start, total_length=L+x, width=W,
+                bar_length=3*H/2, dashpot_length=L/2, piston_pos=H+x)
+    d.rotate(-90, d_start)
+    return d
+
+def make_spring(x):
+    s_start = (-L,4*H)
+    s = Spring(start=s_start, length=L+x, bar_length=3*H/2, teeth=True)
+    s.rotate(-90, s_start)
+    return s
+
+d = make_dashpot(0)
+s = make_spring(0)
+
+M = Rectangle((0,H), 4*H, 4*H).set_linewidth(4)
+left_wall = Rectangle((-L,H),H/10,L-H).set_filled_curves(pattern='/')
+ground = Wall(x=[-L/2,L], y=[H,H], thickness=-H/10)
+
+fontsize = 18
+text_m = Text('$m$', (2*H, H+2*H), fontsize=fontsize)
+text_kx = Text('$s(u)$', (-L/2, H+4*H), fontsize=fontsize)
+text_bv = Text('$f(u)$', (-L/2, H), fontsize=fontsize)
+x_axis = Axis((2*H, L), H, '$u(t)$', fontsize=fontsize,
+              label_spacing=(0.04, -0.01))
+x_axis_start = Line((2*H, L-H/4), (2*H, L+H/4)).set_linewidth(4)
+
+fig = Composition({
+    'spring': s, 'mass': M, 'left wall': left_wall,
+    'ground': ground,
+    'text_m': text_m, 'text_kx': text_kx,
+    'x_axis': x_axis, 'x_axis_start': x_axis_start})
+
+fig.draw()
+drawing_tool.display()
+drawing_tool.savefig('oscillator2_spring')
+
+drawing_tool.erase()
+
+fig['dashpot'] = d
+fig['text_bv'] = text_bv
+
+# or fig = Composition(dict(fig=fig, dashpot=d, text_bv=text_bv))
+fig.draw()
+
+drawing_tool.display()
+drawing_tool.savefig('oscillator2')
+
+drawing_tool.erase()
+
+F_force = Force((4*H, H+2*H), (4*H+H, H+2*H), '$F(t)$',
+                text_spacing=(0.035, -0.01), text_pos='end', fontsize=fontsize)
+fig['F_force'] = F_force
+fig.draw()
+drawing_tool.savefig('oscillator2_force')
+
+raw_input()

+ 24 - 3
examples/pendulum.py

@@ -60,7 +60,7 @@ drawing_tool.erase()
 drawing_tool.set_linecolor('black')
 mg_force = Force(mass_pt, mass_pt + L/5*point(0,-1), '$mg$', text_pos='end')
 rod_force = Force(mass_pt, mass_pt - L/3*unit_vec(rod_vec),
-                  '$S$', text_pos='end')
+                  '$S$', text_pos='end', text_spacing=(0.03, 0.01))
 
 rod_start = rod.geometric_features()['start']
 vertical2 = Line(rod_start, rod_start + point(0,-L/3))
@@ -80,11 +80,32 @@ body_diagram = Composition(
      'body': mass, 'm': mass_symbol})
 
 body_diagram.draw()
-drawing_tool.display('Body diagram')
+#drawing_tool.display('Body diagram')
 drawing_tool.savefig('tmp_pendulum2')
 
 drawing_tool.adjust_coordinate_system(body_diagram.minmax_coordinates(), 90)
-drawing_tool.display('Body diagram')
+#drawing_tool.display('Body diagram')
 drawing_tool.savefig('tmp_pendulum3')
 
+drawing_tool.erase()
+air_force = Force(mass_pt, mass_pt - L/6*unit_vec((rod_vec[1], -rod_vec[0])),
+                  '$\sim|v|v$', text_pos='end', text_spacing=(0.04,0.005))
+
+body_diagram['air'] = air_force
+body_diagram.draw()
+#drawing_tool.display('Body diagram')
+drawing_tool.savefig('tmp_pendulum4')
+
+drawing_tool.erase()
+ir = Force(P, P + L/10*unit_vec(rod_vec),
+           r'${\bf i}_r$', text_pos='end', text_spacing=(0.015,0))
+ith = Force(P, P + L/10*unit_vec((-rod_vec[1], rod_vec[0])),
+           r'${\bf i}_{\theta}$', text_pos='end', text_spacing=(0.02,0.005))
+
+body_diagram['ir'] = ir
+body_diagram['ith'] = ith
+body_diagram.draw()
+#drawing_tool.display('Body diagram')
+drawing_tool.savefig('tmp_pendulum5')
+
 raw_input()

+ 17 - 3
pysketcher/shapes.py

@@ -1390,7 +1390,15 @@ class Force(Arrow1):
     def __init__(self, start, end, text, text_spacing=1./60,
                  fontsize=0, text_pos='start', text_alignment='center'):
         Arrow1.__init__(self, start, end, style='->')
-        spacing = drawing_tool.xrange*text_spacing
+        if isinstance(text_spacing, (tuple,list)):
+            if len(text_spacing) == 2:
+                spacing = point(drawing_tool.xrange*text_spacing[0],
+                                drawing_tool.xrange*text_spacing[1])
+            else:
+                spacing = drawing_tool.xrange*text_spacing[0]
+        else:
+            # just a number, this is x spacing
+            spacing = drawing_tool.xrange*text_spacing
         start, end = arr2D(start), arr2D(end)
 
         # Two cases: label at bottom of line or top, need more
@@ -1403,12 +1411,18 @@ class Force(Arrow1):
                 spacing_dir = unit_vec(start - end)
                 if upward:
                     spacing *= 1.7
-                text_pos = start + spacing*spacing_dir
+                if isinstance(spacing, (int, float)):
+                    text_pos = start + spacing*spacing_dir
+                else:
+                    text_pos = start + spacing
             elif text_pos == 'end':
                 spacing_dir = unit_vec(end - start)
                 if downward:
                     spacing *= 1.7
-                text_pos = end + spacing*spacing_dir
+                if isinstance(spacing, (int, float)):
+                    text_pos = end + spacing*spacing_dir
+                else:
+                    text_pos = end + spacing
         self.shapes['text'] = Text(text, text_pos, fontsize=fontsize,
                                    alignment=text_alignment)