mesh_function.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from pysketcher import *
  2. """
  3. u = SketchyFunc2('$u(t)$', name_pos='end')
  4. n = 7
  5. t_mesh = [i*2.25/(n-1) for i in range(n)]
  6. u = SketchyFunc2('$u(t)$', name_pos='end')
  7. t_mesh = [0, 2, 4, 6, 8]
  8. """
  9. u = SketchyFunc3()
  10. Nt = 5
  11. t_mesh = linspace(0, 6, Nt+1)
  12. # Add 20% space to the left and 30% to the right of the coordinate system
  13. t_axis_extent = t_mesh[-1] - t_mesh[0]
  14. t_min = t_mesh[0] - 0.2*t_axis_extent
  15. t_max = t_mesh[-1] + 0.3*t_axis_extent
  16. u_max = 1.3*max([u(t) for t in t_mesh])
  17. u_min = -0.2*u_max
  18. drawing_tool.set_coordinate_system(t_min, t_max, u_min, u_max, axis=False)
  19. drawing_tool.set_linecolor('black')
  20. r = 0.005*(t_max-t_min) # radius of circles placed at mesh points
  21. u_discrete = Composition({i: Composition(dict(
  22. circle=Circle(point(t, u(t)), r).set_filled_curves('black'),
  23. u_point=Text('$u_%d$' % i,
  24. point(t, u(t)) + (point(0,5*r)
  25. if i > 0 else point(-5*r,0))),
  26. )) for i, t in enumerate(t_mesh)})
  27. interpolant = Composition({
  28. i: Line(point(t_mesh[i-1], u(t_mesh[i-1])),
  29. point(t_mesh[i], u(t_mesh[i]))).set_linewidth(1)
  30. for i in range(1, len(t_mesh))})
  31. axes = Composition(dict(
  32. x=Axis(point(0,0), t_mesh[-1] + 0.2*t_axis_extent, '$t$',
  33. label_spacing=(1/45.,-1/30.)),
  34. y=Axis(point(0,0), 0.8*u_max, '$u$',
  35. rotation_angle=90)))
  36. h = 0.03*u_max # tickmarks height
  37. nodes = Composition({i: Composition(dict(
  38. node=Line(point(t,h), point(t,-h)),
  39. name=Text('$t_%d$' % i, point(t,-3.5*h))))
  40. for i, t in enumerate(t_mesh)})
  41. illustration = Composition(dict(u=u_discrete,
  42. mesh=nodes,
  43. axes=axes)).set_name('fdm_u')
  44. drawing_tool.erase()
  45. # Draw t_mesh with discrete u points
  46. illustration.draw()
  47. drawing_tool.display()
  48. drawing_tool.savefig(illustration.get_name())
  49. # Exact u line (u is a Spline Shape that applies 500 intervals by default
  50. # for drawing the curve)
  51. exact = u.set_linestyle('dashed').set_linewidth(1)
  52. exact.draw()
  53. drawing_tool.display()
  54. drawing_tool.savefig('%s_ue' % illustration.get_name())
  55. interpolant.draw()
  56. drawing_tool.display()
  57. drawing_tool.savefig('%s_uei' % illustration.get_name())
  58. raw_input()