__init__.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from sympy.external import import_module
  2. from sympy.utilities.decorator import doctest_depends_on
  3. @doctest_depends_on(modules=('antlr4',))
  4. def parse_autolev(autolev_code, include_numeric=False):
  5. """Parses Autolev code (version 4.1) to SymPy code.
  6. Parameters
  7. =========
  8. autolev_code : Can be an str or any object with a readlines() method (such as a file handle or StringIO).
  9. include_numeric : boolean, optional
  10. If True NumPy, PyDy, or other numeric code is included for numeric evaluation lines in the Autolev code.
  11. Returns
  12. =======
  13. sympy_code : str
  14. Equivalent SymPy and/or numpy/pydy code as the input code.
  15. Example (Double Pendulum)
  16. =========================
  17. >>> my_al_text = ("MOTIONVARIABLES' Q{2}', U{2}'",
  18. ... "CONSTANTS L,M,G",
  19. ... "NEWTONIAN N",
  20. ... "FRAMES A,B",
  21. ... "SIMPROT(N, A, 3, Q1)",
  22. ... "SIMPROT(N, B, 3, Q2)",
  23. ... "W_A_N>=U1*N3>",
  24. ... "W_B_N>=U2*N3>",
  25. ... "POINT O",
  26. ... "PARTICLES P,R",
  27. ... "P_O_P> = L*A1>",
  28. ... "P_P_R> = L*B1>",
  29. ... "V_O_N> = 0>",
  30. ... "V2PTS(N, A, O, P)",
  31. ... "V2PTS(N, B, P, R)",
  32. ... "MASS P=M, R=M",
  33. ... "Q1' = U1",
  34. ... "Q2' = U2",
  35. ... "GRAVITY(G*N1>)",
  36. ... "ZERO = FR() + FRSTAR()",
  37. ... "KANE()",
  38. ... "INPUT M=1,G=9.81,L=1",
  39. ... "INPUT Q1=.1,Q2=.2,U1=0,U2=0",
  40. ... "INPUT TFINAL=10, INTEGSTP=.01",
  41. ... "CODE DYNAMICS() some_filename.c")
  42. >>> my_al_text = '\\n'.join(my_al_text)
  43. >>> from sympy.parsing.autolev import parse_autolev
  44. >>> print(parse_autolev(my_al_text, include_numeric=True))
  45. import sympy.physics.mechanics as _me
  46. import sympy as _sm
  47. import math as m
  48. import numpy as _np
  49. <BLANKLINE>
  50. q1, q2, u1, u2 = _me.dynamicsymbols('q1 q2 u1 u2')
  51. q1_d, q2_d, u1_d, u2_d = _me.dynamicsymbols('q1_ q2_ u1_ u2_', 1)
  52. l, m, g = _sm.symbols('l m g', real=True)
  53. frame_n = _me.ReferenceFrame('n')
  54. frame_a = _me.ReferenceFrame('a')
  55. frame_b = _me.ReferenceFrame('b')
  56. frame_a.orient(frame_n, 'Axis', [q1, frame_n.z])
  57. frame_b.orient(frame_n, 'Axis', [q2, frame_n.z])
  58. frame_a.set_ang_vel(frame_n, u1*frame_n.z)
  59. frame_b.set_ang_vel(frame_n, u2*frame_n.z)
  60. point_o = _me.Point('o')
  61. particle_p = _me.Particle('p', _me.Point('p_pt'), _sm.Symbol('m'))
  62. particle_r = _me.Particle('r', _me.Point('r_pt'), _sm.Symbol('m'))
  63. particle_p.point.set_pos(point_o, l*frame_a.x)
  64. particle_r.point.set_pos(particle_p.point, l*frame_b.x)
  65. point_o.set_vel(frame_n, 0)
  66. particle_p.point.v2pt_theory(point_o,frame_n,frame_a)
  67. particle_r.point.v2pt_theory(particle_p.point,frame_n,frame_b)
  68. particle_p.mass = m
  69. particle_r.mass = m
  70. force_p = particle_p.mass*(g*frame_n.x)
  71. force_r = particle_r.mass*(g*frame_n.x)
  72. kd_eqs = [q1_d - u1, q2_d - u2]
  73. forceList = [(particle_p.point,particle_p.mass*(g*frame_n.x)), (particle_r.point,particle_r.mass*(g*frame_n.x))]
  74. kane = _me.KanesMethod(frame_n, q_ind=[q1,q2], u_ind=[u1, u2], kd_eqs = kd_eqs)
  75. fr, frstar = kane.kanes_equations([particle_p, particle_r], forceList)
  76. zero = fr+frstar
  77. from pydy.system import System
  78. sys = System(kane, constants = {l:1, m:1, g:9.81},
  79. specifieds={},
  80. initial_conditions={q1:.1, q2:.2, u1:0, u2:0},
  81. times = _np.linspace(0.0, 10, 10/.01))
  82. <BLANKLINE>
  83. y=sys.integrate()
  84. <BLANKLINE>
  85. """
  86. _autolev = import_module(
  87. 'sympy.parsing.autolev._parse_autolev_antlr',
  88. import_kwargs={'fromlist': ['X']})
  89. if _autolev is not None:
  90. return _autolev.parse_autolev(autolev_code, include_numeric)