particle.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. from sympy import S
  2. from sympy.physics.vector import cross, dot
  3. from sympy.physics.mechanics.body_base import BodyBase
  4. from sympy.physics.mechanics.inertia import inertia_of_point_mass
  5. from sympy.utilities.exceptions import sympy_deprecation_warning
  6. __all__ = ['Particle']
  7. class Particle(BodyBase):
  8. """A particle.
  9. Explanation
  10. ===========
  11. Particles have a non-zero mass and lack spatial extension; they take up no
  12. space.
  13. Values need to be supplied on initialization, but can be changed later.
  14. Parameters
  15. ==========
  16. name : str
  17. Name of particle
  18. point : Point
  19. A physics/mechanics Point which represents the position, velocity, and
  20. acceleration of this Particle
  21. mass : Sympifyable
  22. A SymPy expression representing the Particle's mass
  23. potential_energy : Sympifyable
  24. The potential energy of the Particle.
  25. Examples
  26. ========
  27. >>> from sympy.physics.mechanics import Particle, Point
  28. >>> from sympy import Symbol
  29. >>> po = Point('po')
  30. >>> m = Symbol('m')
  31. >>> pa = Particle('pa', po, m)
  32. >>> # Or you could change these later
  33. >>> pa.mass = m
  34. >>> pa.point = po
  35. """
  36. point = BodyBase.masscenter
  37. def __init__(self, name, point=None, mass=None):
  38. super().__init__(name, point, mass)
  39. def linear_momentum(self, frame):
  40. """Linear momentum of the particle.
  41. Explanation
  42. ===========
  43. The linear momentum L, of a particle P, with respect to frame N is
  44. given by:
  45. L = m * v
  46. where m is the mass of the particle, and v is the velocity of the
  47. particle in the frame N.
  48. Parameters
  49. ==========
  50. frame : ReferenceFrame
  51. The frame in which linear momentum is desired.
  52. Examples
  53. ========
  54. >>> from sympy.physics.mechanics import Particle, Point, ReferenceFrame
  55. >>> from sympy.physics.mechanics import dynamicsymbols
  56. >>> from sympy.physics.vector import init_vprinting
  57. >>> init_vprinting(pretty_print=False)
  58. >>> m, v = dynamicsymbols('m v')
  59. >>> N = ReferenceFrame('N')
  60. >>> P = Point('P')
  61. >>> A = Particle('A', P, m)
  62. >>> P.set_vel(N, v * N.x)
  63. >>> A.linear_momentum(N)
  64. m*v*N.x
  65. """
  66. return self.mass * self.point.vel(frame)
  67. def angular_momentum(self, point, frame):
  68. """Angular momentum of the particle about the point.
  69. Explanation
  70. ===========
  71. The angular momentum H, about some point O of a particle, P, is given
  72. by:
  73. ``H = cross(r, m * v)``
  74. where r is the position vector from point O to the particle P, m is
  75. the mass of the particle, and v is the velocity of the particle in
  76. the inertial frame, N.
  77. Parameters
  78. ==========
  79. point : Point
  80. The point about which angular momentum of the particle is desired.
  81. frame : ReferenceFrame
  82. The frame in which angular momentum is desired.
  83. Examples
  84. ========
  85. >>> from sympy.physics.mechanics import Particle, Point, ReferenceFrame
  86. >>> from sympy.physics.mechanics import dynamicsymbols
  87. >>> from sympy.physics.vector import init_vprinting
  88. >>> init_vprinting(pretty_print=False)
  89. >>> m, v, r = dynamicsymbols('m v r')
  90. >>> N = ReferenceFrame('N')
  91. >>> O = Point('O')
  92. >>> A = O.locatenew('A', r * N.x)
  93. >>> P = Particle('P', A, m)
  94. >>> P.point.set_vel(N, v * N.y)
  95. >>> P.angular_momentum(O, N)
  96. m*r*v*N.z
  97. """
  98. return cross(self.point.pos_from(point),
  99. self.mass * self.point.vel(frame))
  100. def kinetic_energy(self, frame):
  101. """Kinetic energy of the particle.
  102. Explanation
  103. ===========
  104. The kinetic energy, T, of a particle, P, is given by:
  105. ``T = 1/2 (dot(m * v, v))``
  106. where m is the mass of particle P, and v is the velocity of the
  107. particle in the supplied ReferenceFrame.
  108. Parameters
  109. ==========
  110. frame : ReferenceFrame
  111. The Particle's velocity is typically defined with respect to
  112. an inertial frame but any relevant frame in which the velocity is
  113. known can be supplied.
  114. Examples
  115. ========
  116. >>> from sympy.physics.mechanics import Particle, Point, ReferenceFrame
  117. >>> from sympy import symbols
  118. >>> m, v, r = symbols('m v r')
  119. >>> N = ReferenceFrame('N')
  120. >>> O = Point('O')
  121. >>> P = Particle('P', O, m)
  122. >>> P.point.set_vel(N, v * N.y)
  123. >>> P.kinetic_energy(N)
  124. m*v**2/2
  125. """
  126. return S.Half * self.mass * dot(self.point.vel(frame),
  127. self.point.vel(frame))
  128. def set_potential_energy(self, scalar):
  129. sympy_deprecation_warning(
  130. """
  131. The sympy.physics.mechanics.Particle.set_potential_energy()
  132. method is deprecated. Instead use
  133. P.potential_energy = scalar
  134. """,
  135. deprecated_since_version="1.5",
  136. active_deprecations_target="deprecated-set-potential-energy",
  137. )
  138. self.potential_energy = scalar
  139. def parallel_axis(self, point, frame):
  140. """Returns an inertia dyadic of the particle with respect to another
  141. point and frame.
  142. Parameters
  143. ==========
  144. point : sympy.physics.vector.Point
  145. The point to express the inertia dyadic about.
  146. frame : sympy.physics.vector.ReferenceFrame
  147. The reference frame used to construct the dyadic.
  148. Returns
  149. =======
  150. inertia : sympy.physics.vector.Dyadic
  151. The inertia dyadic of the particle expressed about the provided
  152. point and frame.
  153. """
  154. return inertia_of_point_mass(self.mass, self.point.pos_from(point),
  155. frame)