implementations.problem_classes.HarmonicOscillator module

class harmonic_oscillator(k=1.0, mu=0.0, u0=(1, 0), phase=0.0, amp=1.0)[source]

Bases: ptype

Example implementing the harmonic oscillator with mass \(1\)

\[\frac{d^2 x}{dt^2} = -kx - \mu \frac{d x}{dt},\]

which is a second-order problem. The unknown function \(x\) denotes the position of the mass, and the derivative is the velocity. \(\mu\) defines the damping and \(k\) is the spring constant.

Parameters:
  • k (float, optional) – Spring constant \(k\).

  • mu (float, optional) – Damping parameter \(\mu\).

  • u0 (tuple, optional) – Initial condition for the position, and the velocity. Should be a tuple, e.g. u0=(1, 0).

  • phase (float, optional) – Phase of the oscillation.

  • amp (float, optional) – Amplitude of the oscillation.

  • Source (https://beltoforion.de/en/harmonic_oscillator/)

dtype_f

alias of acceleration

dtype_u

alias of particles

eval_f(u, t)[source]

Routine to compute the right-hand side of the problem.

Parameters:
  • u (dtype_u) – Current values of the particles.

  • t (float) – Current time of the numerical solution is computed (not used here).

Returns:

me – The right-hand side of the problem.

Return type:

dtype_f

eval_hamiltonian(u)[source]

Routine to compute the Hamiltonian.

Parameters:

u (dtype_u) – Current values of the particles.

Returns:

ham – The Hamiltonian.

Return type:

float

u_exact(t)[source]

Routine to compute the exact trajectory at time \(t\).

Parameters:

t (float) – Time of the exact trajectory.

Returns:

me – Exact position and velocity.

Return type:

dtype_u

u_init()[source]

Helper function to compute the initial condition for u.