Skip to content

Newton's cradle

Newton's cradle.

  • Class: NewtonsCradleSystem
  • Import: from skcomplex.systems.physics.mechanics import NewtonsCradleSystem
  • Field: physics/mechanics
  • Topics: mechanics, events, impacts, momentum
  • References:
    • Hinch and Saint-Jean 1999

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 NewtonsCradleSystem

trajectory = NewtonsCradleSystem().prepare().simulate()

Parameters

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

Parameter Default
n_balls 5
speed 0.5
diameter 0.1
gap 0.001
length 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 NewtonsCradleSystem(systems.BaseSystem):
    """Newton's cradle.

    Pendulum balls in a row pass momentum along by elastic collisions.

    The balls swing with small amplitude about rest positions that leave a small gap between neighbours, so collisions
    are separate events. The first ball starts with a speed towards the others, and equal masses exchange velocities at
    every collision. The system has the horizontal positions `ball_<i>.x` and velocities `ball_<i>.x'`.

    Args:
        n_balls:
            The number of balls.

        speed:
            The initial speed of the first ball.

        diameter:
            The diameter of the balls.

        gap:
            The gap between neighbouring balls at rest.

        length:
            The length of the pendulums.

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

    field = 'physics/mechanics'
    topics = ('mechanics', 'events', 'impacts', 'momentum')
    references = ('Hinch and Saint-Jean 1999',)
    until = 5.0
    dt = 0.01

    def __init__(
        self,
        n_balls: int = 5,
        *,
        speed: float = 0.5,
        diameter: float = 0.1,
        gap: float = 0.001,
        length: 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.n_balls = n_balls
        self.speed = speed
        self.diameter = diameter
        self.gap = gap
        self.length = length
        self.g = g

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of pendulum balls in a row that pass momentum along by elastic collisions.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        spacing = self.diameter + self.gap
        elements = [
            HangingBallElement(
                f'ball_{index}',
                x=index * spacing,
                rest=index * spacing,
                g=self.g,
                length=self.length,
                values={"x'": self.speed if index == 0 else 0.0},
            )
            for index in range(self.n_balls)
        ]
        swings: list[BaseInteraction] = [
            HangingSwingInteraction(f'swing_{index}', target=f'ball_{index}') for index in range(self.n_balls)
        ]
        collisions: list[BaseInteraction] = [
            ElasticCollisionInteraction(
                f'collision_{index}',
                left=f'ball_{index}',
                right=f'ball_{index + 1}',
                diameter=self.diameter,
            )
            for index in range(self.n_balls - 1)
        ]
        self.system_ = systems.System(self.label, elements=elements, interactions=[*swings, *collisions])

Components

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

Elements:

Interactions:

  • HangingSwingInteraction, from Interaction
  • ElasticCollisionInteraction, from Interaction
class HangingBallElement(Element):
    """Ball hanging on a string of length `length`.

    The ball swings with small amplitude about its rest position.

    The horizontal position is `x` and its derivative `x'` is the velocity. For small amplitudes the ball moves in the
    potential `g (x - rest)² / (2 length)`.

    Args:
        label:
            The label of the ball.

        x:
            The initial horizontal position.

        rest:
            The rest position.

        g:
            The gravitational acceleration.

        length:
            The length of the string.

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

    Examples:
        >>> from skcomplex.systems.physics.mechanics import HangingBallElement
        >>> HangingBallElement('ball', rest=0.5).rest
        0.5
    """

    def __init__(
        self,
        label: str,
        *,
        x: float = 0.0,
        rest: float = 0.0,
        g: float = 9.81,
        length: Annotated[float, RealSet.open(0, oo)] = 1.0,
        values: Mapping[str, object] | None = None,
    ) -> None:
        super().__init__(label, values=values)
        self.x = x
        self.rest = rest
        self.g = g
        self.length = length

class HangingSwingInteraction(Interaction):
    """Small swing of a hanging ball about its rest position.

    The position `x` of the target follows `x'' = -g (x - rest) / length`, with the gravity `g`, the rest position
    `rest`, and the length `length` of the string, all values of the target.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the ball.

    Examples:
        >>> from skcomplex.systems.physics.mechanics import HangingSwingInteraction
        >>> HangingSwingInteraction('swing', target='ball').target
        'ball'
    """

    law_classes = (RateLaw,)

    def __init__(self, label: str, *, target: Role) -> None:
        super().__init__(label)
        self.target = target

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the acceleration of the ball.

        Args:
            context:
                The namespace of the ball and the time `t`.
        """
        super()._prepare(context)
        ball = context.target
        self.laws_ = [RateLaw(ball.x.diff(context.t, 2), -ball.g * (ball.x - ball.rest) / ball.length)]

class ElasticCollisionInteraction(Interaction):
    """Elastic collision of two equal masses in a row.

    The masses exchange their velocities when they touch.

    The collision is the event at which the gap `x_right - x_left - diameter` between the balls falls to zero, and
    equal masses swap their velocities in an elastic collision.

    Args:
        label:
            The label of the interaction.

        left:
            The path of the left ball, with a position `x` of order 2.

        right:
            The path of the right ball, with a position `x` of order 2.

        diameter:
            The diameter of the balls.

    Examples:
        >>> from skcomplex.systems.physics.mechanics import ElasticCollisionInteraction
        >>> ElasticCollisionInteraction('collision', left='a', right='b', diameter=0.2).diameter
        0.2
    """

    law_classes = (EventLaw,)

    def __init__(
        self,
        label: str,
        *,
        left: Role,
        right: Role,
        diameter: Annotated[float, RealSet.open(0, oo)] = 0.1,
    ) -> None:
        super().__init__(label)
        self.left = left
        self.right = right
        self.diameter = diameter

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the event of the contact, which swaps the velocities of the balls.

        Args:
            context:
                The namespace of the two balls, the time `t`, and the diameter.
        """
        super()._prepare(context)
        t, left, right = context.t, context.left.x, context.right.x
        swap = {left.diff(t): right.diff(t), right.diff(t): left.diff(t)}
        self.laws_ = [EventLaw(right - left - context.diameter, swap, direction=-1)]

Equations

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

ball_0.x: diff(x_ball_0(t), t, t) == g_ball_0*(rest_ball_0 - x_ball_0(t))/length_ball_0
ball_1.x: diff(x_ball_1(t), t, t) == g_ball_1*(rest_ball_1 - x_ball_1(t))/length_ball_1
ball_2.x: diff(x_ball_2(t), t, t) == g_ball_2*(rest_ball_2 - x_ball_2(t))/length_ball_2
ball_3.x: diff(x_ball_3(t), t, t) == g_ball_3*(rest_ball_3 - x_ball_3(t))/length_ball_3
ball_4.x: diff(x_ball_4(t), t, t) == g_ball_4*(rest_ball_4 - x_ball_4(t))/length_ball_4