examples.systems.duffing_oscillator
Stochastic Duffing Oscillator
This module implements the stochastic Duffing oscillator as a system of first-order stochastic differential equations (SDEs) with both white and coloured noise.
The dimensionless SDE system is:
dq(t) = v(t) dt dv(t) = (-alpha q(t) - beta q(t)^3 - mu v(t) + gamma cos(Omega t) + xi(t)) dt + sqrt(2 mu Theta) \circ dW(t) dxi(t) = -1/t_correl_tilde * xi(t) dt + sqrt(2 D_tilde / t_correl_tilde) \circ dW(t)
where the following are dimensionless parameters: q(t): position v(t): velocity xi(t): auxiliary variable representing Ornstein-Uhlenbeck coloured noise alpha, beta: linear and nonlinear stiffness parameters mu: damping coefficient gamma: forcing amplitude Omega: forcing frequency Theta: temperature (white noise) D_tilde: coloured noise intensity t_correl_tilde: correlation time
Stochastic Duffing oscillator implementation using PyTorch.
Implements the dimensionless SDE system with state vector [q, v, xi] where:
- q(t) is dimensionless position
- v(t) is dimensionless velocity
- xi(t) is the Ornstein-Uhlenbeck process for coloured noise
Initialise the stochastic Duffing oscillator.
Arguments:
- params: Dimensionless system parameters
Get the dimension of the state vector.
Returns:
State dimension (always 3 for [q, v, xi])
Get the initial state vector [q_0, v_0, xi_0], batched for efficiency.
Arguments:
- batch_size: The number of initial states to generate.
Returns:
A batch of initial state tensors of shape [batch_size, 3]
External periodic forcing function gamma cos(Omega t).
Arguments:
- t: Dimensionless time (scalar)
Returns:
Forcing value at time t
Compute the drift term for the SDE system. This function is vectorized to handle batches of states efficiently.
Implements the drift vector field:
[dq/dt, dv/dt, dxi/dt] = [v, -alpha q - beta q^3 - mu v + gamma cos(Omega t) + xi, -xi/t_correl_tilde]
Arguments:
- t: Current dimensionless time
- state: Current state [q, v, xi] of shape [batch_size, 3] or [3]
Returns:
Drift term vector of the same shape as state.
Compute the diffusion term for the SDE system. This function is vectorized to handle batches of states efficiently.
Implements the diffusion matrix:
[0, sqrt(2 mu Theta), sqrt(2 D_tilde / t_correl_tilde)]
Arguments:
- t: Current dimensionless time
- state: Current state [q, v, xi] of shape [batch_size, 3] or [3]
Returns:
Diffusion term vector of shape [batch_size, 3] or [3]
Integrate the stochastic Duffing oscillator SDE system for a batch of trajectories.
This method was refactored to generate multiple trajectories in a batched manner for significantly improved performance, as the underlying drift and diffusion functions are vectorized. Generating trajectories one-by-one in a Python loop is inefficient.
Arguments:
- initial_state: Initial conditions of shape [batch_size, 3]. If None, uses default parameters.
- batch_size: The number of trajectories to generate. Ignored if initial_state is provided.
Returns:
Tuple of (time_grid, state_trajectory) state_trajectory has shape [batch_size, num_steps+1, 3]
Extract position and velocity from state trajectory.
Arguments:
- trajectory: State trajectory from integrate_sde of shape [batch_size, num_steps+1, 3]
Returns:
Tuple of (position, velocity) tensors
Extract coloured noise component from state trajectory.
Arguments:
- trajectory: State trajectory from integrate_sde
Returns:
Coloured noise time series xi(t)
Compute the Duffing potential energy.
V(q) = alpha q^2/2 + beta q^4/4
Arguments:
- q: Dimensionless position
Returns:
Potential energy V(q)
Compute kinetic energy.
T = v^2/2
Arguments:
- v: Dimensionless velocity
Returns:
Kinetic energy T
Compute total mechanical energy.
E = T + V = v^2/2 + alpha q^2/2 + beta q^4/4
Arguments:
- q: Dimensionless position
- v: Dimensionless velocity
Returns:
Total energy E = T + V
Compute effective potential including coloured noise contribution.
This is useful for visualising how coloured noise modifies the potential landscape.
Arguments:
- q: Dimensionless position
- xi: Coloured noise value (Ornstein-Uhlenbeck process)
Returns:
Effective potential V_eff(q, xi) = V(q) - xi·q
Configuration for generating Duffing training data.
Return cache path with key parameters directly in filename.
Generate batched Duffing trajectories for training.
Load cached Duffing trajectories from disk.
Persist Duffing training data to a torch archive.
Generate or load Duffing training data and return associated artefacts.