Skip to content

Standing waves on a string

A string with fixed ends on [-length / 2, length / 2].

  • Class: StandingWavesSystem
  • Import: from skcomplex.systems.physics.waves import StandingWavesSystem
  • Field: physics/waves
  • Topics: waves, field, pde, normal modes
  • References:
    • Starkovich 2021

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.waves import StandingWavesSystem

trajectory = StandingWavesSystem().prepare().simulate()

Parameters

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

Parameter Default
n_sites 99
length 2.0
c 1.0
mode 1
amplitude 1.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 lattice
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 StandingWavesSystem(BaseSystem):
    """A string with fixed ends on `[-length / 2, length / 2]`.

    The string vibrates as a standing wave.

    The displacement follows the wave equation `u'' = c² laplacian(u)` on a lattice whose edges hold the string at
    zero. The string starts at rest in the mode `amplitude sin(mode pi (x +
    length / 2) / length)`, which oscillates in place with the period `2 length / (mode c)`, up to the discretization.
    The system has the displacement `string.u` and the velocity `string.u'`.

    Args:
        n_sites:
            The number of sites inside the string.

        length:
            The length of the string.

        c:
            The speed of the waves.

        mode:
            The number of half wavelengths along the string.

        amplitude:
            The initial amplitude.

        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.waves import StandingWavesSystem
        >>> trajectory = StandingWavesSystem(9).prepare().simulate(until=0.1)
        >>> trajectory.get_lattice('string.u').shape
        (3, 9)
    """

    field = 'physics/waves'
    topics = ('waves', 'field', 'pde', 'normal modes')
    references = ('Starkovich 2021',)
    until = 4.0
    dt = 0.05

    def __init__(
        self,
        n_sites: int = 99,
        *,
        length: float = 2.0,
        c: float = 1.0,
        mode: int = 1,
        amplitude: float = 1.0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.n_sites = n_sites
        self.length = length
        self.c = c
        self.mode = mode
        self.amplitude = amplitude

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of a string with fixed ends that vibrates as a standing wave.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        spacing = self.length / (self.n_sites + 1)
        positions = spacing * np.arange(1, self.n_sites + 1)
        shape = self.amplitude * np.sin(self.mode * math.pi * positions / self.length)
        space = LatticeSpace(self.n_sites, spacing=spacing, boundary='fixed', fill=0.0)
        self.system_ = System(
            self.label,
            elements=[WaveStringElement('string', u=shape, space=space)],
            interactions=[WaveTensionInteraction('tension', target='string', c=self.c)],
        )

Components

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

Elements:

Interactions:

  • WaveTensionInteraction, from Interaction
class WaveStringElement(Element):
    """Sites of a string, with the transverse displacement `u`.

    Args:
        label:
            The label of the string.

        u:
            The initial displacement, one value or one per site.

        space:
            The lattice of the sites, or `None` for one site.

    Examples:
        >>> from skcomplex.systems.physics.waves import WaveStringElement
        >>> WaveStringElement('site', u=0.5).u
        0.5
    """

    def __init__(self, label: str, *, u: object = 0.0, space: BaseSpace | None = None) -> None:
        super().__init__(label, space=space)
        self.u = u

class WaveTensionInteraction(Interaction):
    """Tension of a string, which accelerates its displacement as the wave equation `u'' = c^2 laplacian(u)`.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the string.

        c:
            The wave speed.

    Examples:
        >>> from skcomplex.systems.physics.waves import WaveTensionInteraction
        >>> WaveTensionInteraction('tension', target='string', c=2.0).c
        2.0
    """

    law_classes = (RateLaw,)

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

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

        Args:
            context:
                The namespace of the target, the time `t`, and the parameters.
        """
        super()._prepare(context)
        u = context.target.u
        self.laws_ = [RateLaw(u.diff(context.t, 2), context.c**2 * laplacian(u))]