Skip to content

Double pendulum

A double pendulum, a pendulum hanging from the bob of another.

  • Class: DoublePendulumSystem
  • Import: from skcomplex.systems.physics.mechanics import DoublePendulumSystem
  • Field: physics/mechanics
  • Topics: mechanics, ode, second order, chaos, lagrangian
  • References:
    • Ryzhov et al. 2021
    • Argun 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 DoublePendulumSystem

trajectory = DoublePendulumSystem().prepare().simulate()

Parameters

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

Parameter Default
theta1 1.5707963267948966
theta2 1.5707963267948966
omega1 0.0
omega2 0.0
m1 1.0
m2 1.0
l1 1.0
l2 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
Randomness deterministic
Domains real
Structure fixed

Class

The source of the class shows how it builds its components from its model parameters.

class DoublePendulumSystem(systems.BaseSystem):
    """A double pendulum, a pendulum hanging from the bob of another.

    The system is written as its two Lagrange equations.

    The equations `(m1 + m2) l1 θ1'' + m2 l2 θ2'' cos(θ1 - θ2) + m2 l2 θ2'² sin(θ1 - θ2) + (m1 + m2) g sin θ1 = 0` and
    `l2 θ2'' + l1 θ1'' cos(θ1 - θ2) - l1 θ1'² sin(θ1 - θ2) + g sin θ2 = 0` are solved jointly for the angular
    accelerations. The motion is chaotic at large amplitudes. The system has the angles `pendulum.theta1` and
    `pendulum.theta2`, which wrap around the circle, and their angular velocities `pendulum.theta1'` and
    `pendulum.theta2'`.

    Args:
        theta1:
            The initial angle of the upper rod from the vertical, in radians.

        theta2:
            The initial angle of the lower rod from the vertical, in radians.

        omega1:
            The initial angular velocity of the upper rod.

        omega2:
            The initial angular velocity of the lower rod.

        m1:
            The mass of the upper bob.

        m2:
            The mass of the lower bob.

        l1:
            The length of the upper rod.

        l2:
            The length of the lower 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 DoublePendulumSystem
        >>> DoublePendulumSystem(m2=2.0).prepare().system_.elements[0].m2
        2.0
    """

    field = 'physics/mechanics'
    topics = ('mechanics', 'ode', 'second order', 'chaos', 'lagrangian')
    references = ('Ryzhov et al. 2021', 'Argun et al. 2021')
    until = 20.0
    dt = 0.02

    def __init__(
        self,
        theta1: float = math.pi / 2,
        theta2: float = math.pi / 2,
        omega1: float = 0.0,
        omega2: float = 0.0,
        *,
        m1: float = 1.0,
        m2: float = 1.0,
        l1: float = 1.0,
        l2: 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.theta1 = theta1
        self.theta2 = theta2
        self.omega1 = omega1
        self.omega2 = omega2
        self.m1 = m1
        self.m2 = m2
        self.l1 = l1
        self.l2 = l2
        self.g = g

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of a pendulum hanging from the bob of another.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        pendulum = DoublePendulumElement(
            'pendulum',
            theta1=self.theta1,
            theta2=self.theta2,
            m1=self.m1,
            m2=self.m2,
            l1=self.l1,
            l2=self.l2,
            g=self.g,
            values={"theta1'": self.omega1, "theta2'": self.omega2},
        )
        motion = DoublePendulumInteraction('motion', target='pendulum')
        self.system_ = systems.System(self.label, elements=[pendulum], interactions=[motion])

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

  • DoublePendulumElement, from Element

Interactions:

  • DoublePendulumInteraction, from Interaction
class DoublePendulumElement(Element):
    """Double pendulum.

    A bob of mass `m2` on a rod of length `l2` hangs from a bob of mass `m1` on a rod `l1`.

    The angles `theta1` and `theta2` of the rods from the vertical wrap around the circle, and their angular velocities
    are `theta1'` and `theta2'`. The motion comes from `DoublePendulumInteraction`.

    Args:
        label:
            The label of the double pendulum.

        theta1:
            The initial angle of the upper rod.

        theta2:
            The initial angle of the lower rod.

        m1:
            The mass of the upper bob.

        m2:
            The mass of the lower bob.

        l1:
            The length of the upper rod.

        l2:
            The length of the lower rod.

        g:
            The gravitational acceleration.

        values:
            Further values by name, such as the initial angular velocities `theta1'` and `theta2'`, or `None`.

    Examples:
        >>> from skcomplex.systems.physics.mechanics import DoublePendulumElement
        >>> DoublePendulumElement('pendulum', m2=2.0).m2
        2.0
    """

    visual = VisualSpec(
        'bodies',
        {'positions': _get_double_bobs, 'pivot': (0.0, 0.0), 'rods': True, 'traced': [1]},
    )

    def __init__(
        self,
        label: str,
        *,
        theta1: Annotated[float, Period(2 * math.pi)] = math.pi / 2,
        theta2: Annotated[float, Period(2 * math.pi)] = math.pi / 2,
        m1: Annotated[float, RealSet.open(0, oo)] = 1.0,
        m2: Annotated[float, RealSet.open(0, oo)] = 1.0,
        l1: Annotated[float, RealSet.open(0, oo)] = 1.0,
        l2: 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.theta1 = theta1
        self.theta2 = theta2
        self.m1 = m1
        self.m2 = m2
        self.l1 = l1
        self.l2 = l2
        self.g = g

class DoublePendulumInteraction(LagrangianInteraction):
    """Motion of a double pendulum.

    The motion is the Euler-Lagrange equations of its kinetic energy and gravity.

    The Lagrangian is `(m1 + m2) l1² θ1'² / 2 + m2 l2² θ2'² / 2 + m2 l1 l2 θ1' θ2' cos(θ1 - θ2) + (m1 + m2) g l1 cos θ1
    + m2 g l2 cos θ2`, with the masses, lengths, and gravity of the target.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the double pendulum.

    Examples:
        >>> from skcomplex.systems.physics.mechanics import DoublePendulumInteraction
        >>> DoublePendulumInteraction('motion', 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 angles of the two rods."""
        return [context.target.theta1, context.target.theta2]

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

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

            positions:
                The symbols of the two angles.

            velocities:
                The symbols of the two angular velocities.

        Returns:
            lagrangian:
                The Lagrangian.
        """
        p = context.target
        theta1, theta2 = positions
        w1, w2 = velocities
        kinetic = (p.m1 + p.m2) * p.l1**2 * w1**2 / 2 + p.m2 * p.l2**2 * w2**2 / 2
        kinetic += p.m2 * p.l1 * p.l2 * w1 * w2 * cos(theta1 - theta2)
        return kinetic + (p.m1 + p.m2) * p.g * p.l1 * cos(theta1) + p.m2 * p.g * p.l2 * cos(theta2)

Equations

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

pendulum.theta1: diff(theta1_pendulum(t), t, t) == (l1_pendulum*l2_pendulum*m2_pendulum*sin(theta1_pendulum(t) - theta2_pendulum(t))*diff(theta1_pendulum(t), t)^2 - g_pendulum*l2_pendulum*m2_pendulum*sin(theta2_pendulum(t)) + (l1_pendulum*l2_pendulum*m2_pendulum*sin(theta1_pendulum(t) - theta2_pendulum(t))*diff(theta2_pendulum(t), t)^2 + g_pendulum*l1_pendulum*(m1_pendulum + m2_pendulum)*sin(theta1_pendulum(t)))*l2_pendulum*m2_pendulum*cos(theta1_pendulum(t) - theta2_pendulum(t))/(l1_pendulum*(m1_pendulum + m2_pendulum)))*l2_pendulum*m2_pendulum*cos(theta1_pendulum(t) - theta2_pendulum(t))/((l2_pendulum^2*m2_pendulum^2*cos(theta1_pendulum(t) - theta2_pendulum(t))^2/(m1_pendulum + m2_pendulum) - l2_pendulum^2*m2_pendulum)*l1_pendulum*(m1_pendulum + m2_pendulum)) - (l1_pendulum*l2_pendulum*m2_pendulum*sin(theta1_pendulum(t) - theta2_pendulum(t))*diff(theta2_pendulum(t), t)^2 + g_pendulum*l1_pendulum*(m1_pendulum + m2_pendulum)*sin(theta1_pendulum(t)))/(l1_pendulum^2*(m1_pendulum + m2_pendulum))
pendulum.theta2: diff(theta2_pendulum(t), t, t) == -(l1_pendulum*l2_pendulum*m2_pendulum*sin(theta1_pendulum(t) - theta2_pendulum(t))*diff(theta1_pendulum(t), t)^2 - g_pendulum*l2_pendulum*m2_pendulum*sin(theta2_pendulum(t)) + (l1_pendulum*l2_pendulum*m2_pendulum*sin(theta1_pendulum(t) - theta2_pendulum(t))*diff(theta2_pendulum(t), t)^2 + g_pendulum*l1_pendulum*(m1_pendulum + m2_pendulum)*sin(theta1_pendulum(t)))*l2_pendulum*m2_pendulum*cos(theta1_pendulum(t) - theta2_pendulum(t))/(l1_pendulum*(m1_pendulum + m2_pendulum)))/(l2_pendulum^2*m2_pendulum^2*cos(theta1_pendulum(t) - theta2_pendulum(t))^2/(m1_pendulum + m2_pendulum) - l2_pendulum^2*m2_pendulum)