Hans Petter Langtangen 10 سال پیش
والد
کامیت
37cb569901
6فایلهای تغییر یافته به همراه121 افزوده شده و 13 حذف شده
  1. 10 5
      README.do.txt
  2. BIN
      examples/FE_comic_strip.pdf
  3. BIN
      examples/FE_comic_strip.png
  4. 16 4
      examples/osc1.py
  5. 90 2
      examples/pendulum.py
  6. 5 2
      pysketcher/shapes.py

+ 10 - 5
README.do.txt

@@ -11,21 +11,26 @@ FIGURE: [doc/src/tut/fig-tut/wheel_on_inclined_plane, width=600 frac=0.6]
 Such figures can easily be *interactively* made using a lot of drawing programs.
 Such figures can easily be *interactively* made using a lot of drawing programs.
 A Pysketcher figure, however, is defined in terms of computer code. This gives
 A Pysketcher figure, however, is defined in terms of computer code. This gives
 a great advantage: geometric features can be parameterized in term
 a great advantage: geometric features can be parameterized in term
-of variables, as here:
+of variables. Geometric variations are then trivially generated, and
+complicated figures can be built as a hierarchy of simpler elements.
+
+Here is a very simple figure that illustrates how geometric features are
+parameterized by variables (H, R, L, etc.):
 
 
 FIGURE: [doc/src/tut/fig-tut/vehicle0_dim, width=600 frac=0.6]
 FIGURE: [doc/src/tut/fig-tut/vehicle0_dim, width=600 frac=0.6]
 
 
-One can then quickly change parameters, here to
+One can then quickly change parameters, below to
 `R=0.5; L=5; H=2` and `R=2; L=7; H=1`, and get new figures that would be
 `R=0.5; L=5; H=2` and `R=2; L=7; H=1`, and get new figures that would be
 tedious to draw manually in an interactive tool.
 tedious to draw manually in an interactive tool.
 
 
 FIGURE: [doc/src/tut/fig-tut/vehicle_v23, width=800]
 FIGURE: [doc/src/tut/fig-tut/vehicle_v23, width=800]
 
 
-Another major feature of Pysketcher is the ability to let animate the
-sketch. Here is an example of a very simple vehicle on a bumpy road,
+Another major feature of Pysketcher is the ability to let the
+sketch be dynamic and make an animation of the time evolution.
+Here is an example of a very simple vehicle on a bumpy road,
 where the solution of a differential equation (upper blue line) is fed
 where the solution of a differential equation (upper blue line) is fed
 back to the sketch to make a vertical displacement of the spring and
 back to the sketch to make a vertical displacement of the spring and
-other objects in the vehicle, "view animation": "http://hplgit.github.io/bumpy/doc/src/mov-bumpy/m2_k1_5_b0_2/index.html" (the animation was created by
+other objects in the vehicle. "View animation": "http://hplgit.github.io/bumpy/doc/src/mov-bumpy/m2_k1_5_b0_2/index.html" (the animation was created by
 "this Pysketcher script": "https://github.com/hplgit/bumpy/blob/master/doc/src/fig-bumpy/bumpy_road_fig.py").
 "this Pysketcher script": "https://github.com/hplgit/bumpy/blob/master/doc/src/fig-bumpy/bumpy_road_fig.py").
 
 
 FIGURE: [http://hplgit.github.io/bumpy/doc/src/mov-bumpy/m2_k1_5_b0_2/tmp_frame_0030.png, width=600]
 FIGURE: [http://hplgit.github.io/bumpy/doc/src/mov-bumpy/m2_k1_5_b0_2/tmp_frame_0030.png, width=600]

BIN
examples/FE_comic_strip.pdf


BIN
examples/FE_comic_strip.png


+ 16 - 4
examples/osc1.py

@@ -6,10 +6,11 @@ W = L/6
 
 
 xmax = L
 xmax = L
 drawing_tool.set_coordinate_system(xmin=-L, xmax=xmax,
 drawing_tool.set_coordinate_system(xmin=-L, xmax=xmax,
-                                   ymin=-1, ymax=L,
-                                   axis=True,
+                                   ymin=-1, ymax=L+H,
+                                   axis=False,
                                    instruction_file='tmp_mpl.py')
                                    instruction_file='tmp_mpl.py')
 x = 0
 x = 0
+drawing_tool.set_linecolor('black')
 
 
 def make_dashpot(x):
 def make_dashpot(x):
     d_start = (-L,2*H)
     d_start = (-L,2*H)
@@ -27,15 +28,26 @@ def make_spring(x):
 d = make_dashpot(0)
 d = make_dashpot(0)
 s = make_spring(0)
 s = make_spring(0)
 
 
-M = Rectangle((0,H), 4*H, 4*H)
+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,4*H).set_filled_curves(pattern='/')
 ground = Wall(x=[-L/2,L], y=[0,0], thickness=-H/10)
 ground = Wall(x=[-L/2,L], y=[0,0], thickness=-H/10)
 wheel1 = Circle((H,H/2), H/2)
 wheel1 = Circle((H,H/2), H/2)
 wheel2 = wheel1.copy()
 wheel2 = wheel1.copy()
 wheel2.translate(point(2*H, 0))
 wheel2.translate(point(2*H, 0))
+
+fontsize = 18
+text_m = Text('$m$', (2*H, H+2*H), fontsize=fontsize)
+text_kx = Text('$kx$', (-L/2, H+4*H), fontsize=fontsize)
+text_bv = Text('$b\dot x$', (-L/2, H), fontsize=fontsize)
+x_axis = Axis((2*H, L), H, '$x(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({
 fig = Composition({
     'dashpot': d, 'spring': s, 'mass': M, 'left wall': left_wall,
     'dashpot': d, 'spring': s, 'mass': M, 'left wall': left_wall,
-    'ground': ground, 'wheel1': wheel1, 'wheel2': wheel2})
+    'ground': ground, 'wheel1': wheel1, 'wheel2': wheel2,
+    'text_m': text_m, 'text_kx': text_kx, 'text_bv': text_bv,
+    'x_axis': x_axis, 'x_axis_start': x_axis_start})
 
 
 fig.draw()
 fig.draw()
 #s.draw()
 #s.draw()

+ 90 - 2
examples/pendulum.py

@@ -1,2 +1,90 @@
-# See pysketcher/examples/pendulum.py for code that produced the
-# pendulum figures.
+from pysketcher import *
+
+H = 7.
+W = 6.
+
+drawing_tool.set_coordinate_system(xmin=0, xmax=W,
+                                   ymin=0, ymax=H,
+                                   axis=False)
+drawing_tool.set_linecolor('blue')
+#drawing_tool.set_grid(True)
+
+def set_dashed_thin_blackline(*objects):
+    """Set linestyle of an object to dashed, black, width=1."""
+    for obj in objects:
+        obj.set_linestyle('dashed')
+        obj.set_linecolor('black')
+        obj.set_linewidth(1)
+
+
+L = 5*H/7          # length
+P = (W/6, 0.85*H)  # rotation point
+a = 40             # angle
+
+path = Arc(P, L, -90, a)
+angle = Arc_wText(r'$\theta$', P, L/4, -90, a, text_spacing=1/30.)
+vertical = Line(P, P-point(0,L))
+
+rod = Line(P, P + L*point(sin(radians(a)), -L*cos(radians(a))))
+# or shorter (and more reliable)
+mass_pt = path.geometric_features()['end']
+rod = Line(P, mass_pt)
+
+mass = Circle(center=mass_pt, radius=L/20.)
+mass.set_filled_curves(color='blue')
+rod_vec = rod.geometric_features()['end'] - rod.geometric_features()['start']
+mass_symbol = Text('$m$', mass_pt + L/10*unit_vec(rod_vec))
+
+length = Distance_wText(P, mass_pt, '$L$')
+# Displace length indication
+length.translate(L/15*point(cos(radians(a)), sin(radians(a))))
+gravity = Gravity(start=P+point(0.8*L,0), length=L/3)
+
+set_dashed_thin_blackline(vertical, path)
+
+dims = Composition(
+    {'vertical': vertical, 'theta': angle, 'path': path,
+     'g': gravity, 'L': length, 'm': mass_symbol})
+
+fig = Composition({'body': mass, 'rod': rod, 'dims': dims})
+
+fig.draw()
+drawing_tool.display()
+drawing_tool.savefig('tmp_pendulum1')
+
+# Draw body diagram
+raw_input('Press Return to make body diagram: ')
+#import time; time.sleep(3)
+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')
+
+rod_start = rod.geometric_features()['start']
+vertical2 = Line(rod_start, rod_start + point(0,-L/3))
+set_dashed_thin_blackline(vertical2)
+set_dashed_thin_blackline(rod)
+angle2 = Arc_wText(r'$\theta$', rod_start, L/6, -90, a, text_spacing=1/30.)
+
+# Cannot understand this one:
+#path2 = Arc(P, L, -90+a-a/2., a)
+#path2.set_arrow('<-')
+#path2.set_linestyle('dashed')
+
+body_diagram = Composition(
+    {'mg': mg_force, 'S': rod_force, 'rod': rod,
+     'vertical': vertical2, 'theta': angle2,
+     #'path': path2,
+     'body': mass, 'm': mass_symbol})
+
+body_diagram.draw()
+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.savefig('tmp_pendulum3')
+
+raw_input()

+ 5 - 2
pysketcher/shapes.py

@@ -1348,10 +1348,13 @@ class Axis(Shape):
         Then return `rotation_angle` (in degrees).
         Then return `rotation_angle` (in degrees).
         The `label_spacing` denotes the space between the label
         The `label_spacing` denotes the space between the label
         and the arrow tip as a fraction of the length of the plot
         and the arrow tip as a fraction of the length of the plot
-        in x direction. With `label_alignment` one can place
+        in x direction. A tuple can be given to adjust the position
+        in both the x and y directions (with one parameter, the
+        x position is adjusted).
+        With `label_alignment` one can place
         the axis label text such that the arrow tip is to the 'left',
         the axis label text such that the arrow tip is to the 'left',
         'right', or 'center' with respect to the text field.
         'right', or 'center' with respect to the text field.
-        The `label_spacing` and `label_alignment` parameters can
+        The `label_spacing` and `label_alignment`parameters can
         be used to fine-tune the location of the label.
         be used to fine-tune the location of the label.
         """
         """
         # Arrow is vertical arrow, make it horizontal
         # Arrow is vertical arrow, make it horizontal