test_jointsmethod.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 (
  7. PinJoint, JointsMethod, RigidBody, Particle, Body, KanesMethod,
  8. PrismaticJoint, LagrangesMethod, inertia)
  9. from sympy.physics.vector import dynamicsymbols, ReferenceFrame
  10. from sympy.testing.pytest import raises, warns_deprecated_sympy
  11. from sympy import zeros
  12. from sympy.utilities.lambdify import lambdify
  13. from sympy.solvers.solvers import solve
  14. t = dynamicsymbols._t # type: ignore
  15. def test_jointsmethod():
  16. with warns_deprecated_sympy():
  17. P = Body('P')
  18. C = Body('C')
  19. Pin = PinJoint('P1', P, C)
  20. C_ixx, g = symbols('C_ixx g')
  21. q, u = dynamicsymbols('q_P1, u_P1')
  22. P.apply_force(g*P.y)
  23. with warns_deprecated_sympy():
  24. method = JointsMethod(P, Pin)
  25. assert method.frame == P.frame
  26. assert method.bodies == [C, P]
  27. assert method.loads == [(P.masscenter, g*P.frame.y)]
  28. assert method.q == Matrix([q])
  29. assert method.u == Matrix([u])
  30. assert method.kdes == Matrix([u - q.diff()])
  31. soln = method.form_eoms()
  32. assert soln == Matrix([[-C_ixx*u.diff()]])
  33. assert method.forcing_full == Matrix([[u], [0]])
  34. assert method.mass_matrix_full == Matrix([[1, 0], [0, C_ixx]])
  35. assert isinstance(method.method, KanesMethod)
  36. def test_rigid_body_particle_compatibility():
  37. l, m, g = symbols('l m g')
  38. C = RigidBody('C')
  39. b = Particle('b', mass=m)
  40. b_frame = ReferenceFrame('b_frame')
  41. q, u = dynamicsymbols('q u')
  42. P = PinJoint('P', C, b, coordinates=q, speeds=u, child_interframe=b_frame,
  43. child_point=-l * b_frame.x, joint_axis=C.z)
  44. with warns_deprecated_sympy():
  45. method = JointsMethod(C, P)
  46. method.loads.append((b.masscenter, m * g * C.x))
  47. method.form_eoms()
  48. rhs = method.rhs()
  49. assert rhs[1] == -g*sin(q)/l
  50. def test_jointmethod_duplicate_coordinates_speeds():
  51. with warns_deprecated_sympy():
  52. P = Body('P')
  53. C = Body('C')
  54. T = Body('T')
  55. q, u = dynamicsymbols('q u')
  56. P1 = PinJoint('P1', P, C, q)
  57. P2 = PrismaticJoint('P2', C, T, q)
  58. with warns_deprecated_sympy():
  59. raises(ValueError, lambda: JointsMethod(P, P1, P2))
  60. P1 = PinJoint('P1', P, C, speeds=u)
  61. P2 = PrismaticJoint('P2', C, T, speeds=u)
  62. with warns_deprecated_sympy():
  63. raises(ValueError, lambda: JointsMethod(P, P1, P2))
  64. P1 = PinJoint('P1', P, C, q, u)
  65. P2 = PrismaticJoint('P2', C, T, q, u)
  66. with warns_deprecated_sympy():
  67. raises(ValueError, lambda: JointsMethod(P, P1, P2))
  68. def test_complete_simple_double_pendulum():
  69. q1, q2 = dynamicsymbols('q1 q2')
  70. u1, u2 = dynamicsymbols('u1 u2')
  71. m, l, g = symbols('m l g')
  72. with warns_deprecated_sympy():
  73. C = Body('C') # ceiling
  74. PartP = Body('P', mass=m)
  75. PartR = Body('R', mass=m)
  76. J1 = PinJoint('J1', C, PartP, speeds=u1, coordinates=q1,
  77. child_point=-l*PartP.x, joint_axis=C.z)
  78. J2 = PinJoint('J2', PartP, PartR, speeds=u2, coordinates=q2,
  79. child_point=-l*PartR.x, joint_axis=PartP.z)
  80. PartP.apply_force(m*g*C.x)
  81. PartR.apply_force(m*g*C.x)
  82. with warns_deprecated_sympy():
  83. method = JointsMethod(C, J1, J2)
  84. method.form_eoms()
  85. assert expand(method.mass_matrix_full) == Matrix([[1, 0, 0, 0],
  86. [0, 1, 0, 0],
  87. [0, 0, 2*l**2*m*cos(q2) + 3*l**2*m, l**2*m*cos(q2) + l**2*m],
  88. [0, 0, l**2*m*cos(q2) + l**2*m, l**2*m]])
  89. assert trigsimp(method.forcing_full) == trigsimp(Matrix([[u1], [u2], [-g*l*m*(sin(q1 + q2) + sin(q1)) -
  90. g*l*m*sin(q1) + l**2*m*(2*u1 + u2)*u2*sin(q2)],
  91. [-g*l*m*sin(q1 + q2) - l**2*m*u1**2*sin(q2)]]))
  92. def test_two_dof_joints():
  93. q1, q2, u1, u2 = dynamicsymbols('q1 q2 u1 u2')
  94. m, c1, c2, k1, k2 = symbols('m c1 c2 k1 k2')
  95. with warns_deprecated_sympy():
  96. W = Body('W')
  97. B1 = Body('B1', mass=m)
  98. B2 = Body('B2', mass=m)
  99. J1 = PrismaticJoint('J1', W, B1, coordinates=q1, speeds=u1)
  100. J2 = PrismaticJoint('J2', B1, B2, coordinates=q2, speeds=u2)
  101. W.apply_force(k1*q1*W.x, reaction_body=B1)
  102. W.apply_force(c1*u1*W.x, reaction_body=B1)
  103. B1.apply_force(k2*q2*W.x, reaction_body=B2)
  104. B1.apply_force(c2*u2*W.x, reaction_body=B2)
  105. with warns_deprecated_sympy():
  106. method = JointsMethod(W, J1, J2)
  107. method.form_eoms()
  108. MM = method.mass_matrix
  109. forcing = method.forcing
  110. rhs = MM.LUsolve(forcing)
  111. assert expand(rhs[0]) == expand((-k1 * q1 - c1 * u1 + k2 * q2 + c2 * u2)/m)
  112. assert expand(rhs[1]) == expand((k1 * q1 + c1 * u1 - 2 * k2 * q2 - 2 *
  113. c2 * u2) / m)
  114. def test_simple_pedulum():
  115. l, m, g = symbols('l m g')
  116. with warns_deprecated_sympy():
  117. C = Body('C')
  118. b = Body('b', mass=m)
  119. q = dynamicsymbols('q')
  120. P = PinJoint('P', C, b, speeds=q.diff(t), coordinates=q,
  121. child_point=-l * b.x, joint_axis=C.z)
  122. b.potential_energy = - m * g * l * cos(q)
  123. with warns_deprecated_sympy():
  124. method = JointsMethod(C, P)
  125. method.form_eoms(LagrangesMethod)
  126. rhs = method.rhs()
  127. assert rhs[1] == -g*sin(q)/l
  128. def test_chaos_pendulum():
  129. #https://www.pydy.org/examples/chaos_pendulum.html
  130. mA, mB, lA, lB, IAxx, IBxx, IByy, IBzz, g = symbols('mA, mB, lA, lB, IAxx, IBxx, IByy, IBzz, g')
  131. theta, phi, omega, alpha = dynamicsymbols('theta phi omega alpha')
  132. A = ReferenceFrame('A')
  133. B = ReferenceFrame('B')
  134. with warns_deprecated_sympy():
  135. rod = Body('rod', mass=mA, frame=A,
  136. central_inertia=inertia(A, IAxx, IAxx, 0))
  137. plate = Body('plate', mass=mB, frame=B,
  138. central_inertia=inertia(B, IBxx, IByy, IBzz))
  139. C = Body('C')
  140. J1 = PinJoint('J1', C, rod, coordinates=theta, speeds=omega,
  141. child_point=-lA * rod.z, joint_axis=C.y)
  142. J2 = PinJoint('J2', rod, plate, coordinates=phi, speeds=alpha,
  143. parent_point=(lB - lA) * rod.z, joint_axis=rod.z)
  144. rod.apply_force(mA*g*C.z)
  145. plate.apply_force(mB*g*C.z)
  146. with warns_deprecated_sympy():
  147. method = JointsMethod(C, J1, J2)
  148. method.form_eoms()
  149. MM = method.mass_matrix
  150. forcing = method.forcing
  151. rhs = MM.LUsolve(forcing)
  152. xd = (-2 * IBxx * alpha * omega * sin(phi) * cos(phi) + 2 * IByy * alpha * omega * sin(phi) *
  153. cos(phi) - g * lA * mA * sin(theta) - g * lB * mB * sin(theta)) / (IAxx + IBxx *
  154. sin(phi)**2 + IByy * cos(phi)**2 + lA**2 * mA + lB**2 * mB)
  155. assert (rhs[0] - xd).simplify() == 0
  156. xd = (IBxx - IByy) * omega**2 * sin(phi) * cos(phi) / IBzz
  157. assert (rhs[1] - xd).simplify() == 0
  158. def test_four_bar_linkage_with_manual_constraints():
  159. q1, q2, q3, u1, u2, u3 = dynamicsymbols('q1:4, u1:4')
  160. l1, l2, l3, l4, rho = symbols('l1:5, rho')
  161. N = ReferenceFrame('N')
  162. inertias = [inertia(N, 0, 0, rho * l ** 3 / 12) for l in (l1, l2, l3, l4)]
  163. with warns_deprecated_sympy():
  164. link1 = Body('Link1', frame=N, mass=rho * l1,
  165. central_inertia=inertias[0])
  166. link2 = Body('Link2', mass=rho * l2, central_inertia=inertias[1])
  167. link3 = Body('Link3', mass=rho * l3, central_inertia=inertias[2])
  168. link4 = Body('Link4', mass=rho * l4, central_inertia=inertias[3])
  169. joint1 = PinJoint(
  170. 'J1', link1, link2, coordinates=q1, speeds=u1, joint_axis=link1.z,
  171. parent_point=l1 / 2 * link1.x, child_point=-l2 / 2 * link2.x)
  172. joint2 = PinJoint(
  173. 'J2', link2, link3, coordinates=q2, speeds=u2, joint_axis=link2.z,
  174. parent_point=l2 / 2 * link2.x, child_point=-l3 / 2 * link3.x)
  175. joint3 = PinJoint(
  176. 'J3', link3, link4, coordinates=q3, speeds=u3, joint_axis=link3.z,
  177. parent_point=l3 / 2 * link3.x, child_point=-l4 / 2 * link4.x)
  178. loop = link4.masscenter.pos_from(link1.masscenter) \
  179. + l1 / 2 * link1.x + l4 / 2 * link4.x
  180. fh = Matrix([loop.dot(link1.x), loop.dot(link1.y)])
  181. with warns_deprecated_sympy():
  182. method = JointsMethod(link1, joint1, joint2, joint3)
  183. t = dynamicsymbols._t
  184. qdots = solve(method.kdes, [q1.diff(t), q2.diff(t), q3.diff(t)])
  185. fhd = fh.diff(t).subs(qdots)
  186. kane = KanesMethod(method.frame, q_ind=[q1], u_ind=[u1],
  187. q_dependent=[q2, q3], u_dependent=[u2, u3],
  188. kd_eqs=method.kdes, configuration_constraints=fh,
  189. velocity_constraints=fhd, forcelist=method.loads,
  190. bodies=method.bodies)
  191. fr, frs = kane.kanes_equations()
  192. assert fr == zeros(1)
  193. # Numerically check the mass- and forcing-matrix
  194. p = Matrix([l1, l2, l3, l4, rho])
  195. q = Matrix([q1, q2, q3])
  196. u = Matrix([u1, u2, u3])
  197. eval_m = lambdify((q, p), kane.mass_matrix)
  198. eval_f = lambdify((q, u, p), kane.forcing)
  199. eval_fhd = lambdify((q, u, p), fhd)
  200. p_vals = [0.13, 0.24, 0.21, 0.34, 997]
  201. q_vals = [2.1, 0.6655470375077588, 2.527408138024188] # Satisfies fh
  202. u_vals = [0.2, -0.17963733938852067, 0.1309060540601612] # Satisfies fhd
  203. mass_check = Matrix([[3.452709815256506e+01, 7.003948798374735e+00,
  204. -4.939690970641498e+00],
  205. [-2.203792703880936e-14, 2.071702479957077e-01,
  206. 2.842917573033711e-01],
  207. [-1.300000000000123e-01, -8.836934896046506e-03,
  208. 1.864891330060847e-01]])
  209. forcing_check = Matrix([[-0.031211821321648],
  210. [-0.00066022608181],
  211. [0.001813559741243]])
  212. eps = 1e-10
  213. assert all(abs(x) < eps for x in eval_fhd(q_vals, u_vals, p_vals))
  214. assert all(abs(x) < eps for x in
  215. (Matrix(eval_m(q_vals, p_vals)) - mass_check))
  216. assert all(abs(x) < eps for x in
  217. (Matrix(eval_f(q_vals, u_vals, p_vals)) - forcing_check))