staggered_mesh_function.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from pysketcher import *
  2. Nt = 5
  3. #u = SketchyFunc1()
  4. #t_mesh = linspace(0, 8, Nt+1)
  5. u = SketchyFunc3()
  6. t_mesh = linspace(0, 6, Nt+1)
  7. t_mesh_staggered = linspace(0.5*(t_mesh[0]+t_mesh[1]),
  8. 0.5*(t_mesh[-2] + t_mesh[-1]), Nt)
  9. # Add 20% space to the left and 30% to the right of the coordinate system
  10. t_axis_extent = t_mesh[-1] - t_mesh[0]
  11. t_min = t_mesh[0] - 0.2*t_axis_extent
  12. t_max = t_mesh[-1] + 0.3*t_axis_extent
  13. u_max = 1.3*max([u(t) for t in t_mesh])
  14. u_min = -0.2*u_max
  15. drawing_tool.set_coordinate_system(t_min, t_max, u_min, u_max, axis=False)
  16. drawing_tool.set_linecolor('black')
  17. r = 0.005*(t_max-t_min) # radius of circles placed at mesh points
  18. u_discrete = Composition({i: Composition(dict(
  19. circle=Circle(point(t, u(t)), r).set_filled_curves('black'),
  20. u_point=Text('$u_%d$' % i,
  21. point(t, u(t)) + (point(0,5*r)
  22. if i > 0 else point(-5*r,0))),
  23. )) for i, t in enumerate(t_mesh)})
  24. # u' = v
  25. #v = u.smooth.derivative(n=1)
  26. v = SketchyFunc4()
  27. v_discrete = Composition({i: Composition(dict(
  28. circle=Circle(point(t, v(t)), r).set_filled_curves('red'),
  29. v_point=Text(r'$v_{%d/2}$' % (2*i+1),
  30. point(t, v(t)) + (point(0,5*r))),
  31. )) for i, t in enumerate(t_mesh_staggered)})
  32. axes = Composition(dict(
  33. x=Axis(point(0,0), t_mesh[-1] + 0.2*t_axis_extent, '$t$',
  34. label_spacing=(1/45.,-1/30.)),
  35. y=Axis(point(0,0), 0.8*u_max, '$u,v$',
  36. rotation_angle=90)))
  37. h = 0.03*u_max # tickmarks height
  38. u_nodes = Composition({i: Composition(dict(
  39. node=Line(point(t,h), point(t,-h)),
  40. name=Text('$t_%d$' % i, point(t,-3.5*h))))
  41. for i, t in enumerate(t_mesh)})
  42. v_nodes = Composition({i: Composition(dict(
  43. node=Line(point(t,h/1.5), point(t,-h/1.5)).set_linecolor('red'),
  44. name=Text(r'$t_{%d/2}$' % (2*i+1), point(t,-3.5*h))))
  45. for i, t in enumerate(t_mesh_staggered)})
  46. illustration = Composition(dict(u=u_discrete,
  47. v=v_discrete,
  48. u_mesh=u_nodes,
  49. v_mesh=v_nodes,
  50. axes=axes)).set_name('fdm_uv')
  51. drawing_tool.erase()
  52. # Staggered t mesh and u and v points
  53. illustration.draw()
  54. drawing_tool.display()
  55. drawing_tool.savefig(illustration.get_name())
  56. # Exact u line (u is a Spline Shape that applies 500 intervals by default
  57. # for drawing the curve)
  58. u_exact = u.set_linestyle('dashed').set_linewidth(1)
  59. u_exact.draw()
  60. #v = Curve(u.xcoor, v(u.xcoor))
  61. t_mesh_staggered_fine = linspace(t_mesh_staggered[0],
  62. t_mesh_staggered[-1],
  63. 501)
  64. v_exact = Curve(t_mesh_staggered_fine, v(t_mesh_staggered_fine)).\
  65. set_linestyle('dashed').set_linewidth(1)
  66. v_exact.draw()
  67. drawing_tool.display()
  68. drawing_tool.savefig('%s_uve' % illustration.get_name())
  69. raw_input()