Rotating swing
A rotating swing.
- Class:
RotatingSwingSystem - Import:
from skcomplex.systems.physics.mechanics import RotatingSwingSystem - Field:
physics/mechanics - Topics:
mechanics,ode,second order,rotation,lagrangian - 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 RotatingSwingSystem
trajectory = RotatingSwingSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
omega |
2.0 |
theta |
0.1 |
n_chairs |
8 |
J |
10.0 |
m |
1.0 |
a |
1.0 |
l |
1.0 |
g |
9.81 |
torque |
0.0 |
friction |
0.0 |
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 |
| Randomness | deterministic |
| Domains | real |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class RotatingSwingSystem(BaseSystem):
"""A rotating swing.
Chairs on chains hang from the rods of a stand that turns about a vertical axis.
The stand of inertia `J` turns by the angle `φ`, and the chairs, all deflected by the same angle `θ`, follow the
Lagrange equations `[J + n m (a + l sin θ)²] φ'' + 2 n m l φ' θ' (a + l sin θ) cos θ = M - c φ'` and `l θ'' - φ'² (a
+ l sin θ) cos θ + g sin θ = 0`. At a steady speed `ω` the chairs settle at the angle where `g tan θ = ω² (a + l sin
θ)`. Without torque and friction the angular momentum of the swing is conserved. The system has the angle of the
stand `swing.phi` and of the chains `swing.theta`, and their angular velocities `swing.phi'` and `swing.theta'`.
Args:
omega:
The initial angular velocity of the stand.
theta:
The initial angle of the chains from the vertical.
n_chairs:
The number of chairs.
J:
The moment of inertia of the stand.
m:
The mass of every chair.
a:
The length of the rods.
l:
The length of the chains.
g:
The gravitational acceleration.
torque:
The torque that drives the stand.
friction:
The friction coefficient of the stand's axis.
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 RotatingSwingSystem
>>> trajectory = RotatingSwingSystem(omega=1.0).prepare().simulate(until=1.0)
>>> float(trajectory.get_values("swing.phi'")[0])
1.0
"""
field = 'physics/mechanics'
topics = ('mechanics', 'ode', 'second order', 'rotation', 'lagrangian')
references = ('Ryzhov et al. 2021',)
until = 20.0
dt = 0.05
def __init__(
self,
omega: float = 2.0,
theta: float = 0.1,
*,
n_chairs: int = 8,
J: float = 10.0,
m: float = 1.0,
a: float = 1.0,
l: float = 1.0,
g: float = 9.81,
torque: float = 0.0,
friction: float = 0.0,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.omega = omega
self.theta = theta
self.n_chairs = n_chairs
self.J = J
self.m = m
self.a = a
self.l = l
self.g = g
self.torque = torque
self.friction = friction
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of a stand that turns with chairs on chains.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
swing = RotatingSwingElement(
'swing',
theta=self.theta,
n_chairs=self.n_chairs,
J=self.J,
m=self.m,
a=self.a,
l=self.l,
g=self.g,
values={"phi'": self.omega},
)
motion = RotatingSwingInteraction('motion', target='swing', torque=self.torque, friction=self.friction)
self.system_ = System(self.label, elements=[swing], interactions=[motion])
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
RotatingSwingElement, fromElement
Interactions:
RotatingSwingInteraction, fromInteraction
class RotatingSwingElement(Element):
"""Rotating swing.
Chairs on chains hang from the rods of a stand that turns about a vertical axis.
The angle `phi` of the stand and the angle `theta` of the chains from the vertical are the coordinates of
`RotatingSwingInteraction`, and their angular velocities are `phi'` and `theta'`.
Args:
label:
The label of the swing.
phi:
The initial angle of the stand.
theta:
The initial angle of the chains.
J:
The moment of inertia of the stand.
m:
The mass of every chair.
n_chairs:
The number of chairs.
a:
The length of the rods.
l:
The length of the chains.
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 RotatingSwingElement
>>> RotatingSwingElement('swing', n_chairs=4.0).n_chairs
4.0
"""
def __init__(
self,
label: str,
*,
phi: Annotated[float, Period(2 * math.pi)] = 0.0,
theta: float = 0.1,
J: Annotated[float, RealSet.open(0, oo)] = 10.0,
m: Annotated[float, RealSet.open(0, oo)] = 1.0,
n_chairs: Annotated[float, RealSet.open(0, oo)] = 8.0,
a: Annotated[float, RealSet.closed_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.phi = phi
self.theta = theta
self.J = J
self.m = m
self.n_chairs = n_chairs
self.a = a
self.l = l
self.g = g
class RotatingSwingInteraction(LagrangianInteraction):
"""Motion of a rotating swing.
The motion is the Euler-Lagrange equations with a torque and friction on the stand.
The Lagrangian is `[J + n m (a + l sin θ)²] φ'² / 2 + n m l² θ'² / 2 + n m g l cos θ`, the torque drives the angle
`φ` of the stand, and the Rayleigh dissipation `c φ'² / 2` adds the friction of its axis.
Args:
label:
The label of the interaction.
target:
The path of the swing.
torque:
The torque that drives the stand.
friction:
The friction coefficient of the stand's axis.
Examples:
>>> from skcomplex.systems.physics.mechanics import RotatingSwingInteraction
>>> RotatingSwingInteraction('motion', target='swing', friction=0.5).friction
0.5
"""
def __init__(
self,
label: str,
*,
target: Role,
torque: float = 0.0,
friction: Annotated[float, RealSet.closed_open(0, oo)] = 0.0,
) -> None:
super().__init__(label)
self.target = target
self.torque = torque
self.friction = friction
def _get_coordinates(self, context: SimpleNamespace) -> list[Expression]:
"""Return the angles of the stand and the chains."""
return [context.target.phi, context.target.theta]
def _get_dissipation(
self,
context: SimpleNamespace,
positions: list[Expression],
velocities: list[Expression],
) -> Expression:
"""Return the Rayleigh dissipation `c φ'² / 2` of the stand's axis."""
return context.friction * velocities[0] ** 2 / 2
def _get_forces(
self,
context: SimpleNamespace,
positions: list[Expression],
velocities: list[Expression],
) -> list[Expression]:
"""Return the generalized forces, the torque on the stand and none on the chains."""
return [context.torque, 0]
def _get_lagrangian(
self,
context: SimpleNamespace,
positions: list[Expression],
velocities: list[Expression],
) -> Expression:
"""Return the Lagrangian of the stand and the chairs.
Args:
context:
The namespace of the swing, the time `t`, and the parameters.
positions:
The symbols of the angles.
velocities:
The symbols of the angular velocities.
Returns:
lagrangian:
The Lagrangian.
"""
p = context.target
theta = positions[1]
chairs = p.n_chairs * p.m
radius = p.a + p.l * sin(theta)
kinetic = (p.J + chairs * radius**2) * velocities[0] ** 2 / 2
kinetic += chairs * p.l**2 * velocities[1] ** 2 / 2
return kinetic + chairs * p.g * p.l * cos(theta)
Equations
The equations are those that symbolic returns for the system with its default parameters.
swing.phi: diff(phi_swing(t), t, t) == -(2*(l_swing*sin(theta_swing(t)) + a_swing)*l_swing*m_swing*n_chairs_swing*cos(theta_swing(t))*diff(phi_swing(t), t)*diff(theta_swing(t), t) + friction_motion*diff(phi_swing(t), t) - torque_motion)/((l_swing*sin(theta_swing(t)) + a_swing)^2*m_swing*n_chairs_swing + J_swing)
swing.theta: diff(theta_swing(t), t, t) == ((l_swing*sin(theta_swing(t)) + a_swing)*l_swing*m_swing*n_chairs_swing*cos(theta_swing(t))*diff(phi_swing(t), t)^2 - g_swing*l_swing*m_swing*n_chairs_swing*sin(theta_swing(t)))/(l_swing^2*m_swing*n_chairs_swing)