Skip to content

Bouncing ball

A ball that falls under gravity and bounces off the floor.

  • Class: BouncingBallSystem
  • Import: from skcomplex.systems.physics.mechanics import BouncingBallSystem
  • Field: physics/mechanics
  • Topics: mechanics, events, impacts
  • References:
    • Strogatz 2015
    • Hairer, Nørsett, and Wanner 1993

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 BouncingBallSystem

trajectory = BouncingBallSystem().prepare().simulate()

Parameters

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

Parameter Default
h 1.0
v 0.0
g 9.81
e 0.8
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 BouncingBallSystem(systems.BaseSystem):
    """A ball that falls under gravity and bounces off the floor.

    The ball loses speed by a coefficient of restitution.

    Every impact is an event that reverses the velocity and scales it by `e`. The impacts accumulate at the Zeno time
    `sqrt(2 h / g) (1 + e) / (1 - e)` for a ball dropped from rest, so runs should end before it. The system has the
    height `ball.h` and its velocity `ball.h'`.

    Args:
        h:
            The initial height.

        v:
            The initial vertical velocity.

        g:
            The gravitational acceleration.

        e:
            The coefficient of restitution.

        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 BouncingBallSystem
        >>> trajectory = BouncingBallSystem().prepare().simulate(until=1.0, dt=0.01)
        >>> round(trajectory.events[0].time, 6)
        0.451524
    """

    field = 'physics/mechanics'
    topics = ('mechanics', 'events', 'impacts')
    references = ('Strogatz 2015', 'Hairer, Nørsett, and Wanner 1993')
    until = 3.0
    dt = 0.01

    def __init__(
        self,
        h: float = 1.0,
        v: float = 0.0,
        *,
        g: float = 9.81,
        e: float = 0.8,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.h = h
        self.v = v
        self.g = g
        self.e = e

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of a ball that falls under gravity and bounces off the floor.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        self.system_ = systems.System(
            self.label,
            elements=[BouncingBallElement('ball', h=self.h, values={"h'": self.v})],
            interactions=[
                UniformGravityInteraction('gravity', target='ball', g=self.g, coordinate='h'),
                FloorImpactInteraction('impact', target='ball', e=self.e),
            ],
        )

Components

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

Elements:

  • BouncingBallElement, from Element

Interactions:

  • UniformGravityInteraction, from Interaction
  • FloorImpactInteraction, from Interaction
class BouncingBallElement(Element):
    """Ball at the height `h` above a floor.

    The height `h` and its derivative `h'`, the vertical velocity, change under the laws of gravity and of the
    impacts.

    Args:
        label:
            The label of the ball.

        h:
            The initial height.

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

    Examples:
        >>> from skcomplex.systems.physics.mechanics import BouncingBallElement
        >>> BouncingBallElement('ball', h=2.0).h
        2.0
    """

    def __init__(self, label: str, *, h: float = 1.0, values: Mapping[str, object] | None = None) -> None:
        super().__init__(label, values=values)
        self.h = h

class UniformGravityInteraction(Interaction):
    """Uniform gravity, the constant acceleration `-g` of the height of a target.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the body.

        g:
            The gravitational acceleration.

        coordinate:
            The name of the height of the body, such as `'y'` in a plane or `'h'` above a floor.

    Examples:
        >>> from skcomplex.systems.physics.mechanics import UniformGravityInteraction
        >>> UniformGravityInteraction('gravity', target='ball', g=1.62).g
        1.62
    """

    law_classes = (RateLaw,)

    def __init__(self, label: str, *, target: Role, g: float = GRAVITY, coordinate: str = 'y') -> None:
        super().__init__(label)
        self.target = target
        self.g = g
        self.coordinate = coordinate

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

        Args:
            context:
                The namespace of the body, the time `t`, and the gravity.
        """
        super()._prepare(context)
        height = getattr(context.target, self.coordinate)
        self.laws_ = [RateLaw(height.diff(context.t, 2), -context.g)]

class FloorImpactInteraction(Interaction):
    """Impact of a ball on the floor `h = 0`, which reverses its vertical velocity and scales it by `e`.

    When the height `h` of the target falls to zero, the velocity `h'` becomes `-e h'`, with the coefficient of
    restitution `e`. It is a bouncing ball, a dropped phone, or a hopping robot.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the ball, whose height is `h`.

        e:
            The coefficient of restitution, the ratio of the speeds after and before the impact.

    Examples:
        >>> from skcomplex.systems.physics.mechanics import FloorImpactInteraction
        >>> FloorImpactInteraction('impact', target='ball', e=0.5).e
        0.5
    """

    law_classes = (EventLaw,)

    def __init__(self, label: str, *, target: Role, e: Annotated[float, RealSet.closed(0, 1)] = 1.0) -> None:
        super().__init__(label)
        self.target = target
        self.e = e

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the event of the impact, which reverses the vertical velocity.

        Args:
            context:
                The namespace of the ball, the time `t`, and the restitution.
        """
        super()._prepare(context)
        height = context.target.h
        velocity = height.diff(context.t)
        reset = {velocity: velocity - (1 + context.e) * velocity}
        self.laws_ = [EventLaw(height, reset, direction=-1, label=self.label)]

Equations

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

ball.h: diff(h_ball(t), t, t) == -g_gravity