Skip to content

Hénon-Heiles

The Hénon-Heiles system of a star in a galactic potential.

  • Class: HenonHeilesSystem
  • Import: from skcomplex.systems.physics.nonlinear_oscillators import HenonHeilesSystem
  • Field: physics/nonlinear_oscillators
  • Topics: chaos, ode, hamiltonian
  • References:
    • Hénon and Heiles 1964
    • Datseris and Parlitz 2022

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.nonlinear_oscillators import HenonHeilesSystem

trajectory = HenonHeilesSystem().prepare().simulate()

Parameters

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

Parameter Default
x 0.0
y 0.1
px 0.35
py 0.0
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 HenonHeilesSystem(BaseSystem):
    """The Hénon-Heiles system of a star in a galactic potential.

    The motion is chaotic at higher energies.

    The star moves under the Hamiltonian `H = (px² + py²) / 2 + (x² + y²) / 2 + x² y - y³ / 3`, which is conserved. The
    system has the coordinates `star.x` and `star.y` and their momenta `star.px` and `star.py`.

    Args:
        x:
            The initial first coordinate.

        y:
            The initial second coordinate.

        px:
            The initial first momentum.

        py:
            The initial second momentum.

        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.nonlinear_oscillators import HenonHeilesSystem
        >>> HenonHeilesSystem(px=0.3).prepare().system_.elements[0].px
        0.3
    """

    field = 'physics/nonlinear_oscillators'
    topics = ('chaos', 'ode', 'hamiltonian')
    references = ('Hénon and Heiles 1964', 'Datseris and Parlitz 2022')
    until = 200.0
    dt = 0.05
    invariants: ClassVar[Mapping[str, float]] = {'star.energy': 1e-6}
    observables = (Observable('energy', _get_henon_heiles_energy, element='star'),)

    def __init__(
        self,
        x: float = 0.0,
        y: float = 0.1,
        px: float = 0.35,
        py: float = 0.0,
        *,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.x = x
        self.y = y
        self.px = px
        self.py = py

    @staticmethod
    def _get_henon_heiles_hamiltonian(s: SimpleNamespace) -> Expression:
        """Return the Hénon-Heiles Hamiltonian of the target."""
        return _get_henon_heiles_energy(s.target)

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of a star in a galactic potential, integrated by the symplectic Yoshida method.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        self.system_ = System(
            self.label,
            elements=[HenonHeilesElement('star', x=self.x, y=self.y, px=self.px, py=self.py)],
            interactions=[
                HamiltonianInteraction(
                    'motion',
                    target='star',
                    coordinates=('x', 'y'),
                    momenta=('px', 'py'),
                    hamiltonian=self._get_henon_heiles_hamiltonian,
                ),
            ],
            solver=Yoshida4Solver(dt=0.01),
        )

Components

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

Elements:

Interactions:

  • HamiltonianInteraction, from Interaction
class HenonHeilesElement(Element):
    """Star in the Hénon-Heiles potential `V = (x^2 + y^2) / 2 + x^2 y - y^3 / 3`.

    The star has the canonical momenta `px, py`.

    The coordinates and the momenta are states of order 1, which a Hamiltonian interaction moves. Below the escape
    energy `1 / 6` the motion is regular for small energies and mostly chaotic near the escape energy.

    Args:
        label:
            The label of the star.

        x:
            The initial first coordinate.

        y:
            The initial second coordinate.

        px:
            The initial first momentum.

        py:
            The initial second momentum.

    Examples:
        >>> from skcomplex.systems.physics.nonlinear_oscillators import HenonHeilesElement
        >>> HenonHeilesElement('star').px
        0.35
    """

    visual = VisualSpec('bodies', {'positions': ('x', 'y'), 'trail': None})

    def __init__(self, label: str, *, x: float = 0.0, y: float = 0.1, px: float = 0.35, py: float = 0.0) -> None:
        super().__init__(label)
        self.x = x
        self.y = y
        self.px = px
        self.py = py

class HamiltonianInteraction(Interaction):
    """An interaction that moves canonical pairs by Hamilton's equations.

    A Hamiltonian `H` is the total energy of a system as a function of its coordinates `q` and momenta `p`. The
    interaction gives Hamilton's equations `q' = dH/dp` and `p' = -dH/dq`, one rate per coordinate and per momentum. The
    coordinates and momenta are states of every role that binds one element or group, `target` by default, and they
    pair in order. The rates of one state add, so a coupling between two oscillators is an interaction of its own whose
    Hamiltonian holds only the coupling energy. Dissipation or forcing is a `RateLaw` on the momenta in an interaction
    of its own.

    Unlike `EulerLagrangeInteraction`, which takes coordinates and velocities, it takes coordinates and momenta and
    gives first order rates, which symplectic solvers keep on the surface of constant energy. Choose it when the energy
    is known in canonical form and long runs must conserve it.

    It appears in many fields. Planets orbit a star, atoms vibrate in a molecule, and charged particles move in a
    magnetic trap by Hamilton's equations.

    Args:
        label:
            The name of the interaction, unique among the components of its system, such as `'orbit'`.

        target:
            The path of the element or group whose states are the canonical pairs.

        coordinates:
            The name of the state of order 1 that is a coordinate, or the names of several states.

        momenta:
            The name of the state of order 1 that is the momentum of the coordinate, or the names of several states in
            the order of the coordinates.

        hamiltonian:
            A function of the namespace that returns the Hamiltonian.

    Raises:
        ValidationError:
            A state is not a name, the coordinates and the momenta differ in count, the Hamiltonian is not callable,
            or a coordinate and its momentum differ in size. Preparing a system raises it too when another law gives
            a coordinate a rate of a higher derivative.

    Examples:
        >>> from skcomplex.core.components.elements import Element
        >>> from skcomplex.core.systems import System
        >>> from skcomplex.systems.physics.common.mechanics import HamiltonianInteraction
        >>> energy = lambda s: (s.target.p**2 + s.target.q**2) / 2
        >>> spring = HamiltonianInteraction('spring', target='mass', hamiltonian=energy)
        >>> system = System(elements=[Element('mass', values={'q': 1.0, 'p': 0.0})], interactions=[spring]).prepare()
        >>> trajectory = system.simulate(until=3.14159, dt=0.01)
        >>> round(float(trajectory.get_values('mass.q')[-1]), 3)
        -1.0
    """

    law_classes: ClassVar[tuple[type[BaseLaw], ...]] = (RateLaw,)

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        coordinates: str | Sequence[str] = 'q',
        momenta: str | Sequence[str] = 'p',
        hamiltonian: Callable[[SimpleNamespace], object] | None = None,
    ) -> None:
        super().__init__(label)
        self.target = target
        self.coordinates = coordinates
        self.momenta = momenta
        self.hamiltonian = hamiltonian

    def _get_names(self, names: str | Sequence[str]) -> tuple[str, ...]:
        """Return the names of the states of one side of the canonical pairs."""
        return (names,) if isinstance(names, str) else tuple(names)

    def _check_arguments(self) -> None:
        """Raise an error for an argument the interaction cannot take.

        Raises:
            ValidationError:
                The type declares states, a state is not a name, the coordinates and the momenta differ in count, or
                the Hamiltonian is not callable.
        """
        super()._check_arguments()
        for side in ('coordinates', 'momenta'):
            names = self._get_names(getattr(self, side))
            if not names or not all(isinstance(name, str) and name.isidentifier() for name in names):
                error_msg = f'The {side} of {self.label!r} are {getattr(self, side)!r}. Give names of states.'
                raise ValidationError(error_msg)
        coordinates, momenta = self._get_names(self.coordinates), self._get_names(self.momenta)
        if len(coordinates) != len(momenta):
            unpaired = coordinates[len(momenta) :] or momenta[len(coordinates) :]
            error_msg = (
                f'{type(self).__name__} {self.label!r} pairs the coordinates {coordinates} with the momenta {momenta}, '
                f'so {", ".join(unpaired)} has no partner. Give one momentum per coordinate.'
            )
            raise ValidationError(error_msg)
        if self.hamiltonian is not None and not callable(self.hamiltonian):
            error_msg = f'The hamiltonian of {self.label!r} is {self.hamiltonian!r}. Give a function of the namespace.'
            raise ValidationError(error_msg)

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the rates of the canonical pairs, after checking the states and the Hamiltonian.

        Args:
            context:
                The namespace of its roles, the time `t`, and the parameters.
        """
        super()._prepare(context)
        self.laws_ = self._get_canonical_laws(context)

    def _get_hamiltonian(self, namespace: SimpleNamespace) -> object:
        """Return the Hamiltonian.

        This is the hook a subclass overrides to derive it from its parameters.
        """
        if self.hamiltonian is None:
            error_msg = f'{type(self).__name__} {self.label!r} has no Hamiltonian. Give a function of the namespace.'
            raise ValidationError(error_msg)
        return self.hamiltonian(namespace)

    def _get_members(self, namespace: SimpleNamespace) -> list[SimpleNamespace]:
        """Return the namespaces of the members whose states are canonical pairs.

        These are the members of every role of one path.
        """
        return [getattr(namespace, name) for name in self._get_roles(single=True)]

    def _get_pairs(self, namespace: SimpleNamespace) -> list[tuple[Target, Target]]:
        """Return the canonical pairs of every member.

        It checks that each coordinate matches its momentum.
        """
        pairs = []
        names = list(zip(self._get_names(self.coordinates), self._get_names(self.momenta), strict=True))
        for member in self._get_members(namespace):
            for coordinate, momentum in names:
                q, p = getattr(member, coordinate), getattr(member, momentum)
                if len(self._get_components(q)) != len(self._get_components(p)):
                    error_msg = (
                        f'The coordinate {coordinate} and the momentum {momentum} of {self.label!r} differ in size. '
                        'Give a momentum of the size of its coordinate.'
                    )
                    raise ValidationError(error_msg)
                pairs.append((q, p))
        return pairs

    def _get_components(self, state: Target) -> list[Expression]:
        """Return the components of a state, the state itself for a scalar."""
        return list(state) if isinstance(state, FreeModuleElement) else [state]

    def _get_derivative(self, hamiltonian: Expression, state: Target) -> Target:
        """Return the derivative of the Hamiltonian with respect to a state.

        For a vector state, it returns that of every component.
        """
        if isinstance(state, FreeModuleElement):
            return vector(SR, [self._get_partial(hamiltonian, component) for component in state])
        return self._get_partial(hamiltonian, state)

    @staticmethod
    def _get_partial(hamiltonian: Expression, state: Expression) -> Expression:
        """Return the partial derivative of the Hamiltonian with respect to a scalar state.

        A state that is a function of the time, such as `q(t)`, stands in for a symbol while the derivative is taken.
        """
        if state.is_symbol():
            return hamiltonian.diff(state)
        symbol = SR.symbol()
        return hamiltonian.subs({state: symbol}).diff(symbol).subs({symbol: state})

    def _get_canonical_laws(self, namespace: SimpleNamespace) -> list[BaseLaw]:
        """Return the rate of every coordinate, `dH/dp`, and of every momentum, `-dH/dq`.

        Args:
            namespace:
                The namespace of the symbols the laws can use, with the namespaces of the roles.

        Returns:
            laws:
                The rate laws of the coordinates and the momenta.
        """
        pairs = self._get_pairs(namespace)
        hamiltonian = SR(self._get_hamiltonian(namespace))
        laws: list[BaseLaw] = []
        for q, p in pairs:
            laws.append(RateLaw(q, self._get_derivative(hamiltonian, p)))
            laws.append(RateLaw(p, -self._get_derivative(hamiltonian, q)))
        return laws

Equations

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

star.x: diff(x_star(t), t) == px_star(t)
star.y: diff(y_star(t), t) == py_star(t)
star.px: diff(px_star(t), t) == -2*x_star(t)*y_star(t) - x_star(t)
star.py: diff(py_star(t), t) == -x_star(t)^2 + y_star(t)^2 - y_star(t)