图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
5.8 KiB

  1. from sympy.core.function import expand
  2. from sympy.core.symbol import symbols
  3. from sympy.functions.elementary.trigonometric import (cos, sin)
  4. from sympy.matrices.dense import Matrix
  5. from sympy.simplify.trigsimp import trigsimp
  6. from sympy.physics.mechanics import (PinJoint, JointsMethod, Body, KanesMethod,
  7. PrismaticJoint, LagrangesMethod, inertia)
  8. from sympy.physics.vector import dynamicsymbols, ReferenceFrame
  9. from sympy.testing.pytest import raises
  10. t = dynamicsymbols._t # type: ignore
  11. def test_jointsmethod():
  12. P = Body('P')
  13. C = Body('C')
  14. Pin = PinJoint('P1', P, C)
  15. C_ixx, g = symbols('C_ixx g')
  16. theta, omega = dynamicsymbols('theta_P1, omega_P1')
  17. P.apply_force(g*P.y)
  18. method = JointsMethod(P, Pin)
  19. assert method.frame == P.frame
  20. assert method.bodies == [C, P]
  21. assert method.loads == [(P.masscenter, g*P.frame.y)]
  22. assert method.q == [theta]
  23. assert method.u == [omega]
  24. assert method.kdes == [omega - theta.diff()]
  25. soln = method.form_eoms()
  26. assert soln == Matrix([[-C_ixx*omega.diff()]])
  27. assert method.forcing_full == Matrix([[omega], [0]])
  28. assert method.mass_matrix_full == Matrix([[1, 0], [0, C_ixx]])
  29. assert isinstance(method.method, KanesMethod)
  30. def test_jointmethod_duplicate_coordinates_speeds():
  31. P = Body('P')
  32. C = Body('C')
  33. T = Body('T')
  34. q, u = dynamicsymbols('q u')
  35. P1 = PinJoint('P1', P, C, q)
  36. P2 = PrismaticJoint('P2', C, T, q)
  37. raises(ValueError, lambda: JointsMethod(P, P1, P2))
  38. P1 = PinJoint('P1', P, C, speeds=u)
  39. P2 = PrismaticJoint('P2', C, T, speeds=u)
  40. raises(ValueError, lambda: JointsMethod(P, P1, P2))
  41. P1 = PinJoint('P1', P, C, q, u)
  42. P2 = PrismaticJoint('P2', C, T, q, u)
  43. raises(ValueError, lambda: JointsMethod(P, P1, P2))
  44. def test_complete_simple_double_pendulum():
  45. q1, q2 = dynamicsymbols('q1 q2')
  46. u1, u2 = dynamicsymbols('u1 u2')
  47. m, l, g = symbols('m l g')
  48. C = Body('C') # ceiling
  49. PartP = Body('P', mass=m)
  50. PartR = Body('R', mass=m)
  51. J1 = PinJoint('J1', C, PartP, speeds=u1, coordinates=q1,
  52. child_joint_pos=-l*PartP.x, parent_axis=C.z,
  53. child_axis=PartP.z)
  54. J2 = PinJoint('J2', PartP, PartR, speeds=u2, coordinates=q2,
  55. child_joint_pos=-l*PartR.x, parent_axis=PartP.z,
  56. child_axis=PartR.z)
  57. PartP.apply_force(m*g*C.x)
  58. PartR.apply_force(m*g*C.x)
  59. method = JointsMethod(C, J1, J2)
  60. method.form_eoms()
  61. assert expand(method.mass_matrix_full) == Matrix([[1, 0, 0, 0],
  62. [0, 1, 0, 0],
  63. [0, 0, 2*l**2*m*cos(q2) + 3*l**2*m, l**2*m*cos(q2) + l**2*m],
  64. [0, 0, l**2*m*cos(q2) + l**2*m, l**2*m]])
  65. assert trigsimp(method.forcing_full) == trigsimp(Matrix([[u1], [u2], [-g*l*m*(sin(q1 + q2) + sin(q1)) -
  66. g*l*m*sin(q1) + l**2*m*(2*u1 + u2)*u2*sin(q2)],
  67. [-g*l*m*sin(q1 + q2) - l**2*m*u1**2*sin(q2)]]))
  68. def test_two_dof_joints():
  69. q1, q2, u1, u2 = dynamicsymbols('q1 q2 u1 u2')
  70. m, c1, c2, k1, k2 = symbols('m c1 c2 k1 k2')
  71. W = Body('W')
  72. B1 = Body('B1', mass=m)
  73. B2 = Body('B2', mass=m)
  74. J1 = PrismaticJoint('J1', W, B1, coordinates=q1, speeds=u1)
  75. J2 = PrismaticJoint('J2', B1, B2, coordinates=q2, speeds=u2)
  76. W.apply_force(k1*q1*W.x, reaction_body=B1)
  77. W.apply_force(c1*u1*W.x, reaction_body=B1)
  78. B1.apply_force(k2*q2*W.x, reaction_body=B2)
  79. B1.apply_force(c2*u2*W.x, reaction_body=B2)
  80. method = JointsMethod(W, J1, J2)
  81. method.form_eoms()
  82. MM = method.mass_matrix
  83. forcing = method.forcing
  84. rhs = MM.LUsolve(forcing)
  85. assert expand(rhs[0]) == expand((-k1 * q1 - c1 * u1 + k2 * q2 + c2 * u2)/m)
  86. assert expand(rhs[1]) == expand((k1 * q1 + c1 * u1 - 2 * k2 * q2 - 2 *
  87. c2 * u2) / m)
  88. def test_simple_pedulum():
  89. l, m, g = symbols('l m g')
  90. C = Body('C')
  91. b = Body('b', mass=m)
  92. q = dynamicsymbols('q')
  93. P = PinJoint('P', C, b, speeds=q.diff(t), coordinates=q, child_joint_pos = -l*b.x,
  94. parent_axis=C.z, child_axis=b.z)
  95. b.potential_energy = - m * g * l * cos(q)
  96. method = JointsMethod(C, P)
  97. method.form_eoms(LagrangesMethod)
  98. rhs = method.rhs()
  99. assert rhs[1] == -g*sin(q)/l
  100. def test_chaos_pendulum():
  101. #https://www.pydy.org/examples/chaos_pendulum.html
  102. mA, mB, lA, lB, IAxx, IBxx, IByy, IBzz, g = symbols('mA, mB, lA, lB, IAxx, IBxx, IByy, IBzz, g')
  103. theta, phi, omega, alpha = dynamicsymbols('theta phi omega alpha')
  104. A = ReferenceFrame('A')
  105. B = ReferenceFrame('B')
  106. rod = Body('rod', mass=mA, frame=A, central_inertia=inertia(A, IAxx, IAxx, 0))
  107. plate = Body('plate', mass=mB, frame=B, central_inertia=inertia(B, IBxx, IByy, IBzz))
  108. C = Body('C')
  109. J1 = PinJoint('J1', C, rod, coordinates=theta, speeds=omega,
  110. child_joint_pos=-lA*rod.z, parent_axis=C.y, child_axis=rod.y)
  111. J2 = PinJoint('J2', rod, plate, coordinates=phi, speeds=alpha,
  112. parent_joint_pos=(lB-lA)*rod.z, parent_axis=rod.z, child_axis=plate.z)
  113. rod.apply_force(mA*g*C.z)
  114. plate.apply_force(mB*g*C.z)
  115. method = JointsMethod(C, J1, J2)
  116. method.form_eoms()
  117. MM = method.mass_matrix
  118. forcing = method.forcing
  119. rhs = MM.LUsolve(forcing)
  120. xd = (-2 * IBxx * alpha * omega * sin(phi) * cos(phi) + 2 * IByy * alpha * omega * sin(phi) *
  121. cos(phi) - g * lA * mA * sin(theta) - g * lB * mB * sin(theta)) / (IAxx + IBxx *
  122. sin(phi)**2 + IByy * cos(phi)**2 + lA**2 * mA + lB**2 * mB)
  123. assert (rhs[0] - xd).simplify() == 0
  124. xd = (IBxx - IByy) * omega**2 * sin(phi) * cos(phi) / IBzz
  125. assert (rhs[1] - xd).simplify() == 0