Skip to content

Pendulum with a vertically or horizontally oscillating pivot

A pendulum whose suspension point oscillates vertically or horizontally.

  • Class: ParametricPendulumSystem
  • Import: from skcomplex.systems.physics.mechanics import ParametricPendulumSystem
  • Field: physics/mechanics
  • Topics: mechanics, ode, second order, forcing, parametric resonance
  • References:
    • Dilão 2023

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 ParametricPendulumSystem

trajectory = ParametricPendulumSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
theta 0.1
omega 0.0
g 9.81
l 1.0
epsilon 0.2
Omega 6.26418390534633
c 0.0
direction 'vertical'
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 ParametricPendulumSystem(systems.BaseSystem):
    """A pendulum whose suspension point oscillates vertically or horizontally.

    The suspension point has the acceleration `ε g cos Ω t`.

    Vertically, the acceleration of the suspension point modulates gravity, `θ'' + c θ' + (g / l)(1 + ε cos Ω t) sin θ =
    0`. Small oscillations grow when `Ω` is close to twice the natural frequency `sqrt(g / l)`, the parametric resonance
    of a swing, while a pendulum at rest stays at rest. Horizontally, the acceleration pushes the bob sideways, `θ'' + c
    θ' + (g / l)(sin θ + ε cos Ω t cos θ) = 0`, which for small angles is a harmonic oscillator under an external force.
    Its amplitude grows linearly in time, as `ε Ω t / 2`, when `Ω` equals the natural frequency, the linear resonance,
    and the pendulum starts to swing even from rest. The system has the angle `swing.theta` and the angular velocity
    `swing.theta'`.

    Args:
        theta:
            The initial angle.

        omega:
            The initial angular velocity.

        g:
            The gravitational acceleration.

        l:
            The length of the rod.

        epsilon:
            The amplitude of the acceleration of the suspension point, relative to `g`.

        Omega:
            The angular frequency of the suspension point.

        c:
            The friction coefficient.

        direction:
            The direction in which the suspension point oscillates, `'vertical'` or `'horizontal'`.

        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 ParametricPendulumSystem
        >>> ParametricPendulumSystem(epsilon=0.5).prepare().system_.interactions[0].epsilon
        0.5
    """

    field = 'physics/mechanics'
    topics = ('mechanics', 'ode', 'second order', 'forcing', 'parametric resonance')
    references = ('Dilão 2023',)
    until = 30.0
    dt = 0.02

    def __init__(
        self,
        theta: float = 0.1,
        omega: float = 0.0,
        *,
        g: float = 9.81,
        l: float = 1.0,
        epsilon: float = 0.2,
        Omega: float = 2 * math.sqrt(9.81),
        c: float = 0.0,
        direction: str = 'vertical',
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.theta = theta
        self.omega = omega
        self.g = g
        self.l = l
        self.epsilon = epsilon
        self.Omega = Omega
        self.c = c
        self.direction = direction

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of a pendulum whose suspension point oscillates.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        motion = ParametricPendulumInteraction(
            'motion',
            target='swing',
            epsilon=self.epsilon,
            Omega=self.Omega,
            c=self.c,
            direction=self.direction,
        )
        swing = ParametricPendulumElement('swing', theta=self.theta, g=self.g, l=self.l, values={"theta'": self.omega})
        self.system_ = systems.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:

  • ParametricPendulumElement, from Element

Interactions:

  • ParametricPendulumInteraction, from Interaction
class ParametricPendulumElement(Element):
    """Pendulum whose suspension point oscillates.

    The angle `theta` is the coordinate of `ParametricPendulumInteraction`, and `theta'` is its angular velocity.

    Args:
        label:
            The label of the pendulum.

        theta:
            The initial angle.

        g:
            The gravitational acceleration.

        l:
            The length of the rod.

        values:
            Further values by name, such as an initial velocity named with a prime, or `None`.

    Examples:
        >>> from skcomplex.systems.physics.mechanics import ParametricPendulumElement
        >>> ParametricPendulumElement('swing', l=0.5).l
        0.5
    """

    def __init__(
        self,
        label: str,
        *,
        theta: Annotated[float, Period(2 * math.pi)] = 0.1,
        g: Annotated[float, RealSet.open(0, oo)] = 9.81,
        l: Annotated[float, RealSet.open(0, oo)] = 1.0,
        values: Mapping[str, object] | None = None,
    ) -> None:
        super().__init__(label, values=values)
        self.theta = theta
        self.g = g
        self.l = l

class ParametricPendulumInteraction(LagrangianInteraction):
    """Motion of a pendulum whose suspension point oscillates.

    The point oscillates vertically or horizontally, and the motion is an Euler-Lagrange equation.

    The suspension point accelerates by `ε g cos Ω t`. Vertically, the Lagrangian per moment of inertia is
    `θ'² / 2 + (g / l)(1 + ε cos Ω t) cos θ`, and the acceleration modulates gravity, a parametric forcing.
    Horizontally, it is `θ'² / 2 + (g / l)(cos θ - ε cos Ω t sin θ)`, and the acceleration pushes the bob sideways, an
    external forcing. The Rayleigh dissipation `c θ'² / 2` adds the friction, so the angle follows
    `θ'' + c θ' + (g / l)(1 + ε cos Ω t) sin θ = 0` or `θ'' + c θ' + (g / l)(sin θ + ε cos Ω t cos θ) = 0`, with the
    gravity `g` and the length `l` of the target.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the pendulum.

        epsilon:
            The amplitude of the acceleration of the suspension point, relative to `g`.

        Omega:
            The angular frequency of the suspension point.

        c:
            The friction coefficient.

        direction:
            The direction in which the suspension point oscillates, `'vertical'` or `'horizontal'`.

    Raises:
        ValidationError:
            The direction is unknown.

    Examples:
        >>> from skcomplex.systems.physics.mechanics import ParametricPendulumInteraction
        >>> ParametricPendulumInteraction('swing', target='pendulum', epsilon=0.5).epsilon
        0.5
    """

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        epsilon: float = 0.2,
        Omega: Annotated[float, RealSet.closed_open(0, oo)] = 2 * math.sqrt(9.81),
        c: Annotated[float, RealSet.closed_open(0, oo)] = 0.0,
        direction: str = 'vertical',
    ) -> None:
        super().__init__(label)
        self.target = target
        self.epsilon = epsilon
        self.Omega = Omega
        self.c = c
        self.direction = direction

    def _check_arguments(self) -> None:
        """Raise an error for an unknown direction of the suspension point.

        Raises:
            ValidationError:
                The direction is unknown.
        """
        super()._check_arguments()
        if self.direction not in DIRECTIONS:
            error_msg = f'Unknown direction {self.direction!r}. Use one of {", ".join(DIRECTIONS)}.'
            raise ValidationError(error_msg)

    def _get_coordinates(self, context: SimpleNamespace) -> list[Expression]:
        """Return the angle of the pendulum."""
        return [context.target.theta]

    def _get_dissipation(
        self,
        context: SimpleNamespace,
        positions: list[Expression],
        velocities: list[Expression],
    ) -> Expression:
        """Return the Rayleigh dissipation `c θ'² / 2` of the friction."""
        return context.c * velocities[0] ** 2 / 2

    def _get_lagrangian(
        self,
        context: SimpleNamespace,
        positions: list[Expression],
        velocities: list[Expression],
    ) -> Expression:
        """Return the Lagrangian of the parametric pendulum per moment of inertia.

        Args:
            context:
                The namespace of the pendulum, the time `t`, and the parameters.

            positions:
                The symbol of the angle.

            velocities:
                The symbol of the angular velocity.

        Returns:
            lagrangian:
                The Lagrangian.
        """
        p = context.target
        theta = positions[0]
        drive = context.epsilon * cos(context.Omega * context.t)
        if self.direction == 'horizontal':
            return velocities[0] ** 2 / 2 + p.g / p.l * (cos(theta) - drive * sin(theta))
        return velocities[0] ** 2 / 2 + p.g / p.l * (1 + drive) * cos(theta)

Equations

The equations are those that symbolic returns for the system with its default parameters.

swing.theta: diff(theta_swing(t), t, t) == -(epsilon_motion*cos(Omega_motion*t) + 1)*g_swing*sin(theta_swing(t))/l_swing - c_motion*diff(theta_swing(t), t)