Skip to content

Huygens clocks and pendulums on coupled supports

Pendulum clocks on supports joined by springs.

  • Class: HuygensClocksSystem
  • Import: from skcomplex.systems.physics.mechanics import HuygensClocksSystem
  • Field: physics/mechanics
  • Topics: mechanics, ode, second order, synchronization, limit cycle
  • References:
    • Huygens 1673
    • Dilão 2023
    • 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 HuygensClocksSystem

trajectory = HuygensClocksSystem().prepare().simulate()

Parameters

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

Parameter Default
n_clocks 2
theta 0.2
k 1.0
m 1.0
l 1.0
M 1.0
g 9.8
rho 0.1
lam 0.1
theta_c 0.1
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 HuygensClocksSystem(systems.BaseSystem):
    """Pendulum clocks on supports joined by springs.

    The clocks synchronize through the motion of the supports.

    Every pendulum hangs from a support of mass `M` that moves horizontally with damping `rho`, and neighbouring
    supports are joined by springs of stiffness `k`. A pendulum follows `m l θ'' + f(θ) θ' + m g sin θ = -m x'' cos θ`
    and its support `(M + m) x'' + 2 rho x' + m l θ'' cos θ = m l θ'² sin θ + spring forces`. The escapement `f(θ) = 2 λ
    sgn(|θ| - θ_c)` is `-2 λ` for `|θ| < θ_c`, pumping energy in, and `2 λ` beyond, so every clock has a limit cycle.
    With `λ = 0` the clocks are plain pendulums. Two identical clocks synchronize in antiphase. The first clock starts
    at `theta`, away from the switch of the escapement at `θ_c`, and the others at rest. The system has the angles
    `clock_<i>.theta` and the support positions `clock_<i>.x`, with their velocities `clock_<i>.theta'` and
    `clock_<i>.x'`.

    Args:
        n_clocks:
            The number of clocks in the row.

        theta:
            The initial angle of the first clock. The others start at rest.

        k:
            The stiffness of the springs between neighbouring supports.

        m:
            The mass of every bob.

        l:
            The length of every pendulum.

        M:
            The mass of every support.

        g:
            The gravitational acceleration.

        rho:
            The damping of the supports.

        lam:
            The strength of the escapement.

        theta_c:
            The angle below which the escapement pumps energy in.

        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 HuygensClocksSystem
        >>> len(HuygensClocksSystem(3).prepare().system_.interactions)
        5
    """

    field = 'physics/mechanics'
    topics = ('mechanics', 'ode', 'second order', 'synchronization', 'limit cycle')
    references = ('Huygens 1673', 'Dilão 2023', 'Dilão 2023')
    until = 20.0
    dt = 0.05

    def __init__(
        self,
        n_clocks: int = 2,
        *,
        theta: float = 0.2,
        k: float = 1.0,
        m: float = 1.0,
        l: float = 1.0,
        M: float = 1.0,
        g: float = 9.8,
        rho: float = 0.1,
        lam: float = 0.1,
        theta_c: float = 0.1,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.n_clocks = n_clocks
        self.theta = theta
        self.k = k
        self.m = m
        self.l = l
        self.M = M
        self.g = g
        self.rho = rho
        self.lam = lam
        self.theta_c = theta_c

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of pendulum clocks on supports joined by springs.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        clocks = [
            ClockPendulumElement(
                f'clock_{index}',
                theta=self.theta if index == 0 else 0.0,
                m=self.m,
                l=self.l,
                M=self.M,
                g=self.g,
                rho=self.rho,
                lam=self.lam,
                theta_c=self.theta_c,
            )
            for index in range(self.n_clocks)
        ]
        springs = [
            SupportSpringInteraction(f'spring_{index}', first=f'clock_{index}', second=f'clock_{index + 1}', k=self.k)
            for index in range(self.n_clocks - 1)
        ]
        motions = [
            ClockPendulumInteraction(f'motion_{index}', target=f'clock_{index}') for index in range(self.n_clocks)
        ]
        self.system_ = systems.System(self.label, elements=clocks, interactions=[*springs, *motions])

Components

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

Elements:

  • ClockPendulumElement, from Element

Interactions:

  • SupportSpringInteraction, from Interaction
  • ClockPendulumInteraction, from Interaction
class ClockPendulumElement(Element):
    """Pendulum clock, a pendulum of angle `theta` hanging from a support.

    The support is at the horizontal position `x`. The motion comes from `ClockPendulumInteraction`, and the velocities
    are `theta'` and `x'`.

    Args:
        label:
            The label of the clock.

        theta:
            The initial angle of the pendulum.

        x:
            The initial position of the support.

        m:
            The mass of the bob.

        l:
            The length of the pendulum.

        M:
            The mass of the support.

        g:
            The gravitational acceleration.

        rho:
            The damping of the support.

        lam:
            The strength of the escapement.

        theta_c:
            The angle below which the escapement pumps energy in.

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

    Examples:
        >>> from skcomplex.systems.physics.mechanics import ClockPendulumElement
        >>> ClockPendulumElement('clock', lam=0.2).lam
        0.2
    """

    def __init__(
        self,
        label: str,
        *,
        theta: float = 0.2,
        x: float = 0.0,
        m: Annotated[float, RealSet.open(0, oo)] = 1.0,
        l: Annotated[float, RealSet.open(0, oo)] = 1.0,
        M: Annotated[float, RealSet.open(0, oo)] = 1.0,
        g: Annotated[float, RealSet.open(0, oo)] = 9.8,
        rho: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
        lam: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
        theta_c: Annotated[float, RealSet.open(0, oo)] = 0.1,
        values: Mapping[str, object] | None = None,
    ) -> None:
        super().__init__(label, values=values)
        self.theta = theta
        self.x = x
        self.m = m
        self.l = l
        self.M = M
        self.g = g
        self.rho = rho
        self.lam = lam
        self.theta_c = theta_c

class SupportSpringInteraction(Interaction):
    """Spring of stiffness `k` between the supports of two pendulum clocks.

    The spring pulls the supports with the forces `±k (x2 - x1)`. Every clock answers with the accelerations of its
    support and its pendulum through its mass matrix `[[m l², m l cos θ], [m l cos θ, M + m]]`, so a force `Q` on the
    support gives `θ'' = -m l cos θ Q / D` and `x'' = m l² Q / D`, with the determinant `D = m l² (M + m) - (m l cos
    θ)²`.

    Args:
        label:
            The label of the interaction.

        first:
            The path of the first clock.

        second:
            The path of the second clock.

        k:
            The stiffness of the spring.

    Examples:
        >>> from skcomplex.systems.physics.mechanics import SupportSpringInteraction
        >>> SupportSpringInteraction('spring', first='clock_0', second='clock_1', k=2.0).k
        2.0
    """

    law_classes = (RateLaw,)

    def __init__(
        self,
        label: str,
        *,
        first: Role,
        second: Role,
        k: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
    ) -> None:
        super().__init__(label)
        self.first = first
        self.second = second
        self.k = k

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the accelerations that the spring gives the supports and the pendulums of the two clocks.

        Args:
            context:
                The namespace of the two clocks, the time `t`, and the stiffness.
        """
        super()._prepare(context)
        first, second, t = context.first, context.second, context.t
        pull = context.k * (second.x - first.x)
        laws = []
        for clock, force in ((first, pull), (second, -pull)):
            coupling = clock.m * clock.l * cos(clock.theta)
            determinant = clock.m * clock.l**2 * (clock.M + clock.m) - coupling**2
            laws += [
                RateLaw(clock.theta.diff(t, 2), -coupling * force / determinant),
                RateLaw(clock.x.diff(t, 2), clock.m * clock.l**2 * force / determinant),
            ]
        self.laws_ = laws

class ClockPendulumInteraction(LagrangianInteraction):
    """Motion of a pendulum clock on a moving support.

    The motion is the Euler-Lagrange equations with the escapement.

    The Lagrangian is `(M + m) x'² / 2 + m l x' θ' cos θ + m l² θ'² / 2 + m g l cos θ`. The Rayleigh dissipation
    `λ l sgn(|θ| - θ_c) θ'² + rho x'²` damps the support and gives the escapement `2 λ l sgn(|θ| - θ_c) θ'`, which pumps
    energy into the pendulum below the angle `θ_c` and takes it out beyond.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the clock.

    Examples:
        >>> from skcomplex.systems.physics.mechanics import ClockPendulumInteraction
        >>> ClockPendulumInteraction('motion', target='clock').target
        'clock'
    """

    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 and the position of its support."""
        return [context.target.theta, context.target.x]

    def _get_dissipation(
        self,
        context: SimpleNamespace,
        positions: list[Expression],
        velocities: list[Expression],
    ) -> Expression:
        """Return the Rayleigh dissipation of the escapement and of the support damping."""
        p = context.target
        w, v = velocities
        escapement = p.lam * p.l * sign(abs(positions[0]) - p.theta_c) * w**2
        return escapement + p.rho * v**2

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

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

            positions:
                The symbols of the angle and the position of the support.

            velocities:
                The symbols of the angular velocity and the velocity of the support.

        Returns:
            lagrangian:
                The Lagrangian.
        """
        p = context.target
        theta = positions[0]
        w, v = velocities
        kinetic = (p.M + p.m) * v**2 / 2 + p.m * p.l * v * w * cos(theta) + p.m * p.l**2 * w**2 / 2
        return kinetic + p.m * p.g * p.l * cos(theta)

Equations

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

clock_0.theta: diff(theta_clock_0(t), t, t) == -k_spring_0*l_clock_0*m_clock_0*(x_clock_0(t) - x_clock_1(t))*cos(theta_clock_0(t))/(l_clock_0^2*m_clock_0^2*cos(theta_clock_0(t))^2 - (M_clock_0 + m_clock_0)*l_clock_0^2*m_clock_0) + (l_clock_0*m_clock_0*sin(theta_clock_0(t))*diff(theta_clock_0(t), t)^2 - 2*rho_clock_0*diff(x_clock_0(t), t) + (g_clock_0*l_clock_0*m_clock_0*sin(theta_clock_0(t)) + 2*l_clock_0*lam_clock_0*sgn(-theta_c_clock_0 + abs(theta_clock_0(t)))*diff(theta_clock_0(t), t))*cos(theta_clock_0(t))/l_clock_0)*cos(theta_clock_0(t))/((m_clock_0*cos(theta_clock_0(t))^2 - M_clock_0 - m_clock_0)*l_clock_0) - (g_clock_0*l_clock_0*m_clock_0*sin(theta_clock_0(t)) + 2*l_clock_0*lam_clock_0*sgn(-theta_c_clock_0 + abs(theta_clock_0(t)))*diff(theta_clock_0(t), t))/(l_clock_0^2*m_clock_0)
clock_0.x: diff(x_clock_0(t), t, t) == k_spring_0*l_clock_0^2*m_clock_0*(x_clock_0(t) - x_clock_1(t))/(l_clock_0^2*m_clock_0^2*cos(theta_clock_0(t))^2 - (M_clock_0 + m_clock_0)*l_clock_0^2*m_clock_0) - (l_clock_0*m_clock_0*sin(theta_clock_0(t))*diff(theta_clock_0(t), t)^2 - 2*rho_clock_0*diff(x_clock_0(t), t) + (g_clock_0*l_clock_0*m_clock_0*sin(theta_clock_0(t)) + 2*l_clock_0*lam_clock_0*sgn(-theta_c_clock_0 + abs(theta_clock_0(t)))*diff(theta_clock_0(t), t))*cos(theta_clock_0(t))/l_clock_0)/(m_clock_0*cos(theta_clock_0(t))^2 - M_clock_0 - m_clock_0)
clock_1.theta: diff(theta_clock_1(t), t, t) == k_spring_0*l_clock_1*m_clock_1*(x_clock_0(t) - x_clock_1(t))*cos(theta_clock_1(t))/(l_clock_1^2*m_clock_1^2*cos(theta_clock_1(t))^2 - (M_clock_1 + m_clock_1)*l_clock_1^2*m_clock_1) + (l_clock_1*m_clock_1*sin(theta_clock_1(t))*diff(theta_clock_1(t), t)^2 - 2*rho_clock_1*diff(x_clock_1(t), t) + (g_clock_1*l_clock_1*m_clock_1*sin(theta_clock_1(t)) + 2*l_clock_1*lam_clock_1*sgn(-theta_c_clock_1 + abs(theta_clock_1(t)))*diff(theta_clock_1(t), t))*cos(theta_clock_1(t))/l_clock_1)*cos(theta_clock_1(t))/((m_clock_1*cos(theta_clock_1(t))^2 - M_clock_1 - m_clock_1)*l_clock_1) - (g_clock_1*l_clock_1*m_clock_1*sin(theta_clock_1(t)) + 2*l_clock_1*lam_clock_1*sgn(-theta_c_clock_1 + abs(theta_clock_1(t)))*diff(theta_clock_1(t), t))/(l_clock_1^2*m_clock_1)
clock_1.x: diff(x_clock_1(t), t, t) == -k_spring_0*l_clock_1^2*m_clock_1*(x_clock_0(t) - x_clock_1(t))/(l_clock_1^2*m_clock_1^2*cos(theta_clock_1(t))^2 - (M_clock_1 + m_clock_1)*l_clock_1^2*m_clock_1) - (l_clock_1*m_clock_1*sin(theta_clock_1(t))*diff(theta_clock_1(t), t)^2 - 2*rho_clock_1*diff(x_clock_1(t), t) + (g_clock_1*l_clock_1*m_clock_1*sin(theta_clock_1(t)) + 2*l_clock_1*lam_clock_1*sgn(-theta_c_clock_1 + abs(theta_clock_1(t)))*diff(theta_clock_1(t), t))*cos(theta_clock_1(t))/l_clock_1)/(m_clock_1*cos(theta_clock_1(t))^2 - M_clock_1 - m_clock_1)