Ballistic pendulum
A bullet that sticks in the block of a pendulum at rest.
- Class:
BallisticPendulumSystem - Import:
from skcomplex.systems.physics.mechanics import BallisticPendulumSystem - Field:
physics/mechanics - Topics:
mechanics,events,impacts,momentum,hybrid - References:
- Ryzhov et al. 2021
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.physics.mechanics import BallisticPendulumSystem
trajectory = BallisticPendulumSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
speed |
200.0 |
distance |
1.0 |
m |
0.01 |
M |
1.0 |
l |
1.0 |
g |
9.81 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | continuous |
| Space | none |
| Laws | rate, update |
| Randomness | deterministic |
| Domains | real |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class BallisticPendulumSystem(systems.BaseSystem):
"""A bullet that sticks in the block of a pendulum at rest.
The pendulum then swings up.
The bullet flies horizontally towards the block, and the impact is an event. Momentum is conserved in the perfectly
inelastic collision, so the pendulum starts with the speed `m v / (m + M)` and rises to the height `(m v / (m + M))²
/ (2 g)`. The bullet position stops at the block after the impact. The system has the bullet position `pendulum.x`
and the angle `pendulum.theta`, and their velocities `pendulum.x'` and `pendulum.theta'`.
Args:
speed:
The speed of the bullet.
distance:
The initial distance of the bullet from the block.
m:
The mass of the bullet.
M:
The mass of the block.
l:
The length of the rod.
g:
The gravitational acceleration.
label:
The label of the system, which names it when it is nested in another one.
solver:
The solver of the problem, or `'auto'` for the one the system chooses.
backend:
The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.
Examples:
>>> from skcomplex.systems.physics.mechanics import BallisticPendulumSystem
>>> BallisticPendulumSystem(distance=2.0).prepare().system_.elements[0].x
-2.0
"""
field = 'physics/mechanics'
topics = ('mechanics', 'events', 'impacts', 'momentum', 'hybrid')
references = ('Ryzhov et al. 2021',)
until = 3.0
dt = 0.01
def __init__(
self,
speed: float = 200.0,
distance: float = 1.0,
*,
m: float = 0.01,
M: float = 1.0,
l: float = 1.0,
g: float = 9.81,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.speed = speed
self.distance = distance
self.m = m
self.M = M
self.l = l
self.g = g
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of a bullet that sticks in the block of a pendulum at rest.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
pendulum = BallisticPendulumElement(
'pendulum',
x=-self.distance,
m=self.m,
M=self.M,
l=self.l,
g=self.g,
values={"x'": self.speed},
)
self.system_ = systems.System(
self.label,
elements=[pendulum],
interactions=[
BlockSwingInteraction('swing', target='pendulum'),
BulletImpactInteraction('impact', target='pendulum'),
],
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
BallisticPendulumElement, fromElement
Interactions:
BlockSwingInteraction, fromInteractionBulletImpactInteraction, fromInteraction
class BallisticPendulumElement(Element):
"""Bullet of mass `m` at the position `x` flying towards the block of a pendulum.
The block has the mass `M`, and the pendulum the angle `theta`.
The bullet flies freely, the pendulum swings, and the impact transfers the momentum of the bullet to the pendulum.
The velocities are `x'` and `theta'`.
Args:
label:
The label of the bullet and pendulum.
x:
The initial position of the bullet, negative before the block.
theta:
The initial angle of the pendulum.
m:
The mass of the bullet.
M:
The mass of the block.
l:
The length of the rod.
g:
The gravitational acceleration.
values:
Further values by name, such as an initial velocity named with a prime, or `None`.
Examples:
>>> from skcomplex.systems.physics.mechanics import BallisticPendulumElement
>>> BallisticPendulumElement('pendulum', M=2.0).M
2.0
"""
def __init__(
self,
label: str,
*,
x: float = -1.0,
theta: Annotated[float, Period(2 * math.pi)] = 0.0,
m: Annotated[float, RealSet.open(0, oo)] = 0.01,
M: Annotated[float, RealSet.open(0, oo)] = 1.0,
l: Annotated[float, RealSet.open(0, oo)] = 1.0,
g: Annotated[float, RealSet.open(0, oo)] = 9.81,
values: Mapping[str, object] | None = None,
) -> None:
super().__init__(label, values=values)
self.x = x
self.theta = theta
self.m = m
self.M = M
self.l = l
self.g = g
class BlockSwingInteraction(LagrangianInteraction):
"""Swing of the block of a ballistic pendulum, the Euler-Lagrange equation of `L = l θ'² / 2 + g cos θ`.
The angle `theta` of the target follows `l θ'' = -g sin θ`, with the length `l` and the gravity `g` of the target.
Args:
label:
The label of the interaction.
target:
The path of the bullet and pendulum.
Examples:
>>> from skcomplex.systems.physics.mechanics import BlockSwingInteraction
>>> BlockSwingInteraction('swing', target='pendulum').target
'pendulum'
"""
def __init__(self, label: str, *, target: Role) -> None:
super().__init__(label)
self.target = target
def _get_coordinates(self, context: SimpleNamespace) -> list[Expression]:
"""Return the angle of the pendulum."""
return [context.target.theta]
def _get_lagrangian(
self,
context: SimpleNamespace,
positions: list[Expression],
velocities: list[Expression],
) -> Expression:
"""Return the Lagrangian `l θ'² / 2 + g cos θ` of the block.
Args:
context:
The namespace of the pendulum and the time `t`.
positions:
The symbol of the angle.
velocities:
The symbol of the angular velocity.
Returns:
lagrangian:
The Lagrangian.
"""
return context.target.l * velocities[0] ** 2 / 2 + context.target.g * cos(positions[0])
class BulletImpactInteraction(Interaction):
"""Free flight of a bullet that sticks in the block of a pendulum.
The position `x` of the bullet follows `x'' = 0` until it reaches the block at `x = 0`. The impact stops the bullet
and gives the pendulum the angular velocity `m x' / ((m + M) l)`, which conserves momentum, with the masses `m` of
the bullet and `M` of the block and the length `l` of the target.
Args:
label:
The label of the interaction.
target:
The path of the bullet and pendulum.
Examples:
>>> from skcomplex.systems.physics.mechanics import BulletImpactInteraction
>>> BulletImpactInteraction('impact', target='pendulum').target
'pendulum'
"""
law_classes = (RateLaw, EventLaw)
def __init__(self, label: str, *, target: Role) -> None:
super().__init__(label)
self.target = target
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the free flight of the bullet and the event of the impact.
Args:
context:
The namespace of the bullet and pendulum and the time `t`.
"""
super()._prepare(context)
t, p = context.t, context.target
velocity = p.x.diff(t)
reset = {velocity: 0, p.theta.diff(t): p.m * velocity / ((p.m + p.M) * p.l)}
self.laws_ = [RateLaw(p.x.diff(t, 2), 0), EventLaw(p.x, reset, direction=1)]
Equations
The equations are those that symbolic returns for the system with its default parameters.
pendulum.x: diff(x_pendulum(t), t, t) == 0
pendulum.theta: diff(theta_pendulum(t), t, t) == -g_pendulum*sin(theta_pendulum(t))/l_pendulum