rigidbody.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. from sympy.core.backend import sympify
  2. from sympy.physics.vector import Point, ReferenceFrame, Dyadic
  3. from sympy.utilities.exceptions import sympy_deprecation_warning
  4. __all__ = ['RigidBody']
  5. class RigidBody:
  6. """An idealized rigid body.
  7. Explanation
  8. ===========
  9. This is essentially a container which holds the various components which
  10. describe a rigid body: a name, mass, center of mass, reference frame, and
  11. inertia.
  12. All of these need to be supplied on creation, but can be changed
  13. afterwards.
  14. Attributes
  15. ==========
  16. name : string
  17. The body's name.
  18. masscenter : Point
  19. The point which represents the center of mass of the rigid body.
  20. frame : ReferenceFrame
  21. The ReferenceFrame which the rigid body is fixed in.
  22. mass : Sympifyable
  23. The body's mass.
  24. inertia : (Dyadic, Point)
  25. The body's inertia about a point; stored in a tuple as shown above.
  26. Examples
  27. ========
  28. >>> from sympy import Symbol
  29. >>> from sympy.physics.mechanics import ReferenceFrame, Point, RigidBody
  30. >>> from sympy.physics.mechanics import outer
  31. >>> m = Symbol('m')
  32. >>> A = ReferenceFrame('A')
  33. >>> P = Point('P')
  34. >>> I = outer (A.x, A.x)
  35. >>> inertia_tuple = (I, P)
  36. >>> B = RigidBody('B', P, A, m, inertia_tuple)
  37. >>> # Or you could change them afterwards
  38. >>> m2 = Symbol('m2')
  39. >>> B.mass = m2
  40. """
  41. def __init__(self, name, masscenter, frame, mass, inertia):
  42. if not isinstance(name, str):
  43. raise TypeError('Supply a valid name.')
  44. self._name = name
  45. self.masscenter = masscenter
  46. self.mass = mass
  47. self.frame = frame
  48. self.inertia = inertia
  49. self.potential_energy = 0
  50. def __str__(self):
  51. return self._name
  52. def __repr__(self):
  53. return self.__str__()
  54. @property
  55. def frame(self):
  56. return self._frame
  57. @frame.setter
  58. def frame(self, F):
  59. if not isinstance(F, ReferenceFrame):
  60. raise TypeError("RigdBody frame must be a ReferenceFrame object.")
  61. self._frame = F
  62. @property
  63. def masscenter(self):
  64. return self._masscenter
  65. @masscenter.setter
  66. def masscenter(self, p):
  67. if not isinstance(p, Point):
  68. raise TypeError("RigidBody center of mass must be a Point object.")
  69. self._masscenter = p
  70. @property
  71. def mass(self):
  72. return self._mass
  73. @mass.setter
  74. def mass(self, m):
  75. self._mass = sympify(m)
  76. @property
  77. def inertia(self):
  78. return (self._inertia, self._inertia_point)
  79. @inertia.setter
  80. def inertia(self, I):
  81. if not isinstance(I[0], Dyadic):
  82. raise TypeError("RigidBody inertia must be a Dyadic object.")
  83. if not isinstance(I[1], Point):
  84. raise TypeError("RigidBody inertia must be about a Point.")
  85. self._inertia = I[0]
  86. self._inertia_point = I[1]
  87. # have I S/O, want I S/S*
  88. # I S/O = I S/S* + I S*/O; I S/S* = I S/O - I S*/O
  89. # I_S/S* = I_S/O - I_S*/O
  90. from sympy.physics.mechanics.functions import inertia_of_point_mass
  91. I_Ss_O = inertia_of_point_mass(self.mass,
  92. self.masscenter.pos_from(I[1]),
  93. self.frame)
  94. self._central_inertia = I[0] - I_Ss_O
  95. @property
  96. def central_inertia(self):
  97. """The body's central inertia dyadic."""
  98. return self._central_inertia
  99. def linear_momentum(self, frame):
  100. """ Linear momentum of the rigid body.
  101. Explanation
  102. ===========
  103. The linear momentum L, of a rigid body B, with respect to frame N is
  104. given by
  105. L = M * v*
  106. where M is the mass of the rigid body and v* is the velocity of
  107. the mass center of B in the frame, N.
  108. Parameters
  109. ==========
  110. frame : ReferenceFrame
  111. The frame in which linear momentum is desired.
  112. Examples
  113. ========
  114. >>> from sympy.physics.mechanics import Point, ReferenceFrame, outer
  115. >>> from sympy.physics.mechanics import RigidBody, dynamicsymbols
  116. >>> from sympy.physics.vector import init_vprinting
  117. >>> init_vprinting(pretty_print=False)
  118. >>> M, v = dynamicsymbols('M v')
  119. >>> N = ReferenceFrame('N')
  120. >>> P = Point('P')
  121. >>> P.set_vel(N, v * N.x)
  122. >>> I = outer (N.x, N.x)
  123. >>> Inertia_tuple = (I, P)
  124. >>> B = RigidBody('B', P, N, M, Inertia_tuple)
  125. >>> B.linear_momentum(N)
  126. M*v*N.x
  127. """
  128. return self.mass * self.masscenter.vel(frame)
  129. def angular_momentum(self, point, frame):
  130. """Returns the angular momentum of the rigid body about a point in the
  131. given frame.
  132. Explanation
  133. ===========
  134. The angular momentum H of a rigid body B about some point O in a frame
  135. N is given by:
  136. H = I . w + r x Mv
  137. where I is the central inertia dyadic of B, w is the angular velocity
  138. of body B in the frame, N, r is the position vector from point O to the
  139. mass center of B, and v is the velocity of the mass center in the
  140. frame, N.
  141. Parameters
  142. ==========
  143. point : Point
  144. The point about which angular momentum is desired.
  145. frame : ReferenceFrame
  146. The frame in which angular momentum is desired.
  147. Examples
  148. ========
  149. >>> from sympy.physics.mechanics import Point, ReferenceFrame, outer
  150. >>> from sympy.physics.mechanics import RigidBody, dynamicsymbols
  151. >>> from sympy.physics.vector import init_vprinting
  152. >>> init_vprinting(pretty_print=False)
  153. >>> M, v, r, omega = dynamicsymbols('M v r omega')
  154. >>> N = ReferenceFrame('N')
  155. >>> b = ReferenceFrame('b')
  156. >>> b.set_ang_vel(N, omega * b.x)
  157. >>> P = Point('P')
  158. >>> P.set_vel(N, 1 * N.x)
  159. >>> I = outer(b.x, b.x)
  160. >>> B = RigidBody('B', P, b, M, (I, P))
  161. >>> B.angular_momentum(P, N)
  162. omega*b.x
  163. """
  164. I = self.central_inertia
  165. w = self.frame.ang_vel_in(frame)
  166. m = self.mass
  167. r = self.masscenter.pos_from(point)
  168. v = self.masscenter.vel(frame)
  169. return I.dot(w) + r.cross(m * v)
  170. def kinetic_energy(self, frame):
  171. """Kinetic energy of the rigid body.
  172. Explanation
  173. ===========
  174. The kinetic energy, T, of a rigid body, B, is given by
  175. 'T = 1/2 (I omega^2 + m v^2)'
  176. where I and m are the central inertia dyadic and mass of rigid body B,
  177. respectively, omega is the body's angular velocity and v is the
  178. velocity of the body's mass center in the supplied ReferenceFrame.
  179. Parameters
  180. ==========
  181. frame : ReferenceFrame
  182. The RigidBody's angular velocity and the velocity of it's mass
  183. center are typically defined with respect to an inertial frame but
  184. any relevant frame in which the velocities are known can be supplied.
  185. Examples
  186. ========
  187. >>> from sympy.physics.mechanics import Point, ReferenceFrame, outer
  188. >>> from sympy.physics.mechanics import RigidBody
  189. >>> from sympy import symbols
  190. >>> M, v, r, omega = symbols('M v r omega')
  191. >>> N = ReferenceFrame('N')
  192. >>> b = ReferenceFrame('b')
  193. >>> b.set_ang_vel(N, omega * b.x)
  194. >>> P = Point('P')
  195. >>> P.set_vel(N, v * N.x)
  196. >>> I = outer (b.x, b.x)
  197. >>> inertia_tuple = (I, P)
  198. >>> B = RigidBody('B', P, b, M, inertia_tuple)
  199. >>> B.kinetic_energy(N)
  200. M*v**2/2 + omega**2/2
  201. """
  202. rotational_KE = (self.frame.ang_vel_in(frame) & (self.central_inertia &
  203. self.frame.ang_vel_in(frame)) / sympify(2))
  204. translational_KE = (self.mass * (self.masscenter.vel(frame) &
  205. self.masscenter.vel(frame)) / sympify(2))
  206. return rotational_KE + translational_KE
  207. @property
  208. def potential_energy(self):
  209. """The potential energy of the RigidBody.
  210. Examples
  211. ========
  212. >>> from sympy.physics.mechanics import RigidBody, Point, outer, ReferenceFrame
  213. >>> from sympy import symbols
  214. >>> M, g, h = symbols('M g h')
  215. >>> b = ReferenceFrame('b')
  216. >>> P = Point('P')
  217. >>> I = outer (b.x, b.x)
  218. >>> Inertia_tuple = (I, P)
  219. >>> B = RigidBody('B', P, b, M, Inertia_tuple)
  220. >>> B.potential_energy = M * g * h
  221. >>> B.potential_energy
  222. M*g*h
  223. """
  224. return self._pe
  225. @potential_energy.setter
  226. def potential_energy(self, scalar):
  227. """Used to set the potential energy of this RigidBody.
  228. Parameters
  229. ==========
  230. scalar: Sympifyable
  231. The potential energy (a scalar) of the RigidBody.
  232. Examples
  233. ========
  234. >>> from sympy.physics.mechanics import Point, outer
  235. >>> from sympy.physics.mechanics import RigidBody, ReferenceFrame
  236. >>> from sympy import symbols
  237. >>> b = ReferenceFrame('b')
  238. >>> M, g, h = symbols('M g h')
  239. >>> P = Point('P')
  240. >>> I = outer (b.x, b.x)
  241. >>> Inertia_tuple = (I, P)
  242. >>> B = RigidBody('B', P, b, M, Inertia_tuple)
  243. >>> B.potential_energy = M * g * h
  244. """
  245. self._pe = sympify(scalar)
  246. def set_potential_energy(self, scalar):
  247. sympy_deprecation_warning(
  248. """
  249. The sympy.physics.mechanics.RigidBody.set_potential_energy()
  250. method is deprecated. Instead use
  251. B.potential_energy = scalar
  252. """,
  253. deprecated_since_version="1.5",
  254. active_deprecations_target="deprecated-set-potential-energy",
  255. )
  256. self.potential_energy = scalar
  257. # XXX: To be consistent with the parallel_axis method in Particle this
  258. # should have a frame argument...
  259. def parallel_axis(self, point):
  260. """Returns the inertia dyadic of the body with respect to another
  261. point.
  262. Parameters
  263. ==========
  264. point : sympy.physics.vector.Point
  265. The point to express the inertia dyadic about.
  266. Returns
  267. =======
  268. inertia : sympy.physics.vector.Dyadic
  269. The inertia dyadic of the rigid body expressed about the provided
  270. point.
  271. """
  272. # circular import issue
  273. from sympy.physics.mechanics.functions import inertia
  274. a, b, c = self.masscenter.pos_from(point).to_matrix(self.frame)
  275. I = self.mass * inertia(self.frame, b**2 + c**2, c**2 + a**2, a**2 +
  276. b**2, -a * b, -b * c, -a * c)
  277. return self.central_inertia + I