File size: 7,577 Bytes
528457d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
from sympy.core.backend import sympify
from sympy.physics.vector import Point
from sympy.utilities.exceptions import sympy_deprecation_warning
__all__ = ['Particle']
class Particle:
"""A particle.
Explanation
===========
Particles have a non-zero mass and lack spatial extension; they take up no
space.
Values need to be supplied on initialization, but can be changed later.
Parameters
==========
name : str
Name of particle
point : Point
A physics/mechanics Point which represents the position, velocity, and
acceleration of this Particle
mass : sympifyable
A SymPy expression representing the Particle's mass
Examples
========
>>> from sympy.physics.mechanics import Particle, Point
>>> from sympy import Symbol
>>> po = Point('po')
>>> m = Symbol('m')
>>> pa = Particle('pa', po, m)
>>> # Or you could change these later
>>> pa.mass = m
>>> pa.point = po
"""
def __init__(self, name, point, mass):
if not isinstance(name, str):
raise TypeError('Supply a valid name.')
self._name = name
self.mass = mass
self.point = point
self.potential_energy = 0
def __str__(self):
return self._name
def __repr__(self):
return self.__str__()
@property
def mass(self):
"""Mass of the particle."""
return self._mass
@mass.setter
def mass(self, value):
self._mass = sympify(value)
@property
def point(self):
"""Point of the particle."""
return self._point
@point.setter
def point(self, p):
if not isinstance(p, Point):
raise TypeError("Particle point attribute must be a Point object.")
self._point = p
def linear_momentum(self, frame):
"""Linear momentum of the particle.
Explanation
===========
The linear momentum L, of a particle P, with respect to frame N is
given by:
L = m * v
where m is the mass of the particle, and v is the velocity of the
particle in the frame N.
Parameters
==========
frame : ReferenceFrame
The frame in which linear momentum is desired.
Examples
========
>>> from sympy.physics.mechanics import Particle, Point, ReferenceFrame
>>> from sympy.physics.mechanics import dynamicsymbols
>>> from sympy.physics.vector import init_vprinting
>>> init_vprinting(pretty_print=False)
>>> m, v = dynamicsymbols('m v')
>>> N = ReferenceFrame('N')
>>> P = Point('P')
>>> A = Particle('A', P, m)
>>> P.set_vel(N, v * N.x)
>>> A.linear_momentum(N)
m*v*N.x
"""
return self.mass * self.point.vel(frame)
def angular_momentum(self, point, frame):
"""Angular momentum of the particle about the point.
Explanation
===========
The angular momentum H, about some point O of a particle, P, is given
by:
``H = cross(r, m * v)``
where r is the position vector from point O to the particle P, m is
the mass of the particle, and v is the velocity of the particle in
the inertial frame, N.
Parameters
==========
point : Point
The point about which angular momentum of the particle is desired.
frame : ReferenceFrame
The frame in which angular momentum is desired.
Examples
========
>>> from sympy.physics.mechanics import Particle, Point, ReferenceFrame
>>> from sympy.physics.mechanics import dynamicsymbols
>>> from sympy.physics.vector import init_vprinting
>>> init_vprinting(pretty_print=False)
>>> m, v, r = dynamicsymbols('m v r')
>>> N = ReferenceFrame('N')
>>> O = Point('O')
>>> A = O.locatenew('A', r * N.x)
>>> P = Particle('P', A, m)
>>> P.point.set_vel(N, v * N.y)
>>> P.angular_momentum(O, N)
m*r*v*N.z
"""
return self.point.pos_from(point) ^ (self.mass * self.point.vel(frame))
def kinetic_energy(self, frame):
"""Kinetic energy of the particle.
Explanation
===========
The kinetic energy, T, of a particle, P, is given by:
``T = 1/2 (dot(m * v, v))``
where m is the mass of particle P, and v is the velocity of the
particle in the supplied ReferenceFrame.
Parameters
==========
frame : ReferenceFrame
The Particle's velocity is typically defined with respect to
an inertial frame but any relevant frame in which the velocity is
known can be supplied.
Examples
========
>>> from sympy.physics.mechanics import Particle, Point, ReferenceFrame
>>> from sympy import symbols
>>> m, v, r = symbols('m v r')
>>> N = ReferenceFrame('N')
>>> O = Point('O')
>>> P = Particle('P', O, m)
>>> P.point.set_vel(N, v * N.y)
>>> P.kinetic_energy(N)
m*v**2/2
"""
return (self.mass / sympify(2) * self.point.vel(frame) &
self.point.vel(frame))
@property
def potential_energy(self):
"""The potential energy of the Particle.
Examples
========
>>> from sympy.physics.mechanics import Particle, Point
>>> from sympy import symbols
>>> m, g, h = symbols('m g h')
>>> O = Point('O')
>>> P = Particle('P', O, m)
>>> P.potential_energy = m * g * h
>>> P.potential_energy
g*h*m
"""
return self._pe
@potential_energy.setter
def potential_energy(self, scalar):
"""Used to set the potential energy of the Particle.
Parameters
==========
scalar : Sympifyable
The potential energy (a scalar) of the Particle.
Examples
========
>>> from sympy.physics.mechanics import Particle, Point
>>> from sympy import symbols
>>> m, g, h = symbols('m g h')
>>> O = Point('O')
>>> P = Particle('P', O, m)
>>> P.potential_energy = m * g * h
"""
self._pe = sympify(scalar)
def set_potential_energy(self, scalar):
sympy_deprecation_warning(
"""
The sympy.physics.mechanics.Particle.set_potential_energy()
method is deprecated. Instead use
P.potential_energy = scalar
""",
deprecated_since_version="1.5",
active_deprecations_target="deprecated-set-potential-energy",
)
self.potential_energy = scalar
def parallel_axis(self, point, frame):
"""Returns an inertia dyadic of the particle with respect to another
point and frame.
Parameters
==========
point : sympy.physics.vector.Point
The point to express the inertia dyadic about.
frame : sympy.physics.vector.ReferenceFrame
The reference frame used to construct the dyadic.
Returns
=======
inertia : sympy.physics.vector.Dyadic
The inertia dyadic of the particle expressed about the provided
point and frame.
"""
# circular import issue
from sympy.physics.mechanics import inertia_of_point_mass
return inertia_of_point_mass(self.mass, self.point.pos_from(point),
frame)
|