Skip to content

Mechanics

Define the Lagrangian mechanics that the systems of several fields share.

The equations of motion follow from a Lagrangian, the kinetic minus the potential energy, as for a pendulum, a planet, or the rotor of a motor.

EulerLagrangeInteraction(label, *, target, states='x', lagrangian=None, dissipation=None, forcing=None)

Bases: Interaction

An interaction that moves coordinates by the equations of a Lagrangian.

A Lagrangian L is the kinetic minus the potential energy of a mechanical system. The interaction gives the equations of motion d/dt dL/dq' - dL/dq = Q - dR/dq' for the coordinates q, with the dissipation function R and the generalized forces Q. It returns one equation per coordinate, and the compiler solves them together for the accelerations, so a Lagrangian with a mass matrix, such as that of a double pendulum, needs no inversion by hand. Equations of one coordinate from several interactions add, so a spring between two pendulums is an interaction of its own whose Lagrangian holds only the coupling energy.

A Lagrangian, dissipation, or forcing function takes a namespace with the roles, the time t, and the parameters a subclass declares, and writes the velocity of a coordinate q as q.diff(t). The coordinates are the states of every role that binds one element or group, target by default, and every component of a vector state is a coordinate. Every other value of the roles, such as a mass or an inertia, is held fixed when the momenta change in time, so it stays a parameter.

Unlike HamiltonianInteraction, which needs canonical momenta, it takes the energy in coordinates and velocities. Choose it when the energy is easier to write than the forces, as for linked bodies.

It appears in many fields. Pendulums and linkages in mechanics, electric circuits written by their energy, and the joints of a robot arm follow the equations of a Lagrangian.

Parameters:

Name Type Description Default
label str

The name of the interaction, unique among the components of its system, such as 'pendulum'.

required
target Role

The path of the element or group whose states are the coordinates.

required
states str | Sequence[str]

The name of the state that is a coordinate, or the names of several states.

'x'
lagrangian Callable[[SimpleNamespace], object] | None

A function of the namespace that returns the Lagrangian.

None
dissipation Callable[[SimpleNamespace], object] | None

A function of the namespace that returns the Rayleigh dissipation function, or None for none.

None
forcing Callable[[SimpleNamespace], object] | None

A function of the namespace that returns the generalized forces, or None for none.

None

Raises:

Type Description
ValidationError

A state is not a name, or a function is not callable.

Examples:

>>> from sage.all__sagemath_symbolics import cos
>>> from skcomplex.core.components.elements import Element
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.general.mechanics import EulerLagrangeInteraction
>>> swing = EulerLagrangeInteraction(
...     'swing',
...     target='bob',
...     states='q',
...     lagrangian=lambda s: s.target.q.diff(s.t) ** 2 / 2 + cos(s.target.q),
... )
>>> system = System(elements=[Element('bob', values={'q': 0.1})], interactions=[swing])
>>> trajectory = system.prepare().simulate(until=3.2, dt=0.01)
>>> round(float(trajectory.get_values('bob.q')[-1]), 2)
-0.1
Source code in src/skcomplex/systems/general/mechanics/_euler_lagrange.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def __init__(
    self,
    label: str,
    *,
    target: Role,
    states: str | Sequence[str] = 'x',
    lagrangian: Callable[[SimpleNamespace], object] | None = None,
    dissipation: Callable[[SimpleNamespace], object] | None = None,
    forcing: Callable[[SimpleNamespace], object] | None = None,
) -> None:
    super().__init__(label)
    self.target = target
    self.states = states
    self.lagrangian = lagrangian
    self.dissipation = dissipation
    self.forcing = forcing