spectral.KdVSolver#

class spectral.KdVSolver(N: int, L: float, dealias: bool = False)[source]#

Bases: object

Korteweg-de Vries equation solver using Fourier spectral methods.

Solves the KdV equation u_t + 6u*u_x + u_xxx = 0 on a periodic domain using Fourier collocation for spatial discretization. The nonlinear term can optionally be dealiased using the 3/2-rule.

Notes

The Fourier spectral method provides exponential convergence for smooth periodic solutions. Spatial derivatives are computed in Fourier space via multiplication by ik for first derivatives and (ik)^3 for third derivatives, where k is the wavenumber.

The 3/2-rule dealiasing prevents aliasing errors in the nonlinear convolution product u*u_x by padding the Fourier coefficients to 3/2 times the original resolution.

Methods

__init__

Initialize the KdV solver.

compute_conserved_quantities

Compute conserved quantities for KdV equation.

compute_eigenvalues

Compute eigenvalues of frozen-coefficient linearized KdV operator.

get_spectrum

Compute Fourier spectrum for spectral analysis.

rhs

Compute right-hand side of semi-discrete KdV equation.

solve

Solve KdV equation from t=0 to t=t_final.

stable_dt

Compute stable time step via absolute-stability & frozen coefficients.

Examples using spectral.KdVSolver#

KdV Eigenvalue Stability Analysis - Data Generation

KdV Eigenvalue Stability Analysis - Data Generation

Generate KdV Soliton Samples (RK4, RK3)

Generate KdV Soliton Samples (RK4, RK3)

Spatial and Temporal Convergence for Fourier KdV Solver

Spatial and Temporal Convergence for Fourier KdV Solver

Single Soliton Error and Conservation Analysis

Single Soliton Error and Conservation Analysis

Aliasing Diagnostics for Fourier KdV Solver

Aliasing Diagnostics for Fourier KdV Solver

Two-Soliton KdV Collision Dataset Generation

Two-Soliton KdV Collision Dataset Generation

Profiling Script: Single Soliton KdV Solver

Profiling Script: Single Soliton KdV Solver

Scalability Analysis: Time per Step vs Grid Size

Scalability Analysis: Time per Step vs Grid Size

Work-Precision Analysis: RK3 vs RK4 with Varying Grid Resolution

Work-Precision Analysis: RK3 vs RK4 with Varying Grid Resolution
static compute_conserved_quantities(u: ndarray, dx: float) tuple[float, float, float][source]#

Compute conserved quantities for KdV equation.

Mass: M = ∫ u dx Momentum: V = ∫ u² dx Energy: E = ∫ (½u_x² - u³) dx

Parameters:
unp.ndarray

Solution field

dxfloat

Grid spacing

Returns:
Mfloat

Mass

Vfloat

Momentum

Efloat

Energy

compute_eigenvalues(u_max: float) ndarray[source]#

Compute eigenvalues of frozen-coefficient linearized KdV operator.

For the KdV equation u_t = -6u*u_x - u_xxx, the linearization around a frozen coefficient u_max gives: L = -6*u_max*D1 - D3

where D1 and D3 are the first and third derivative operators. This is useful for stability analysis.

Parameters:
u_maxfloat

Maximum amplitude for frozen-coefficient approximation

Returns:
np.ndarray

Complex eigenvalues of the linearized operator

get_spectrum(u: ndarray) tuple[ndarray, ndarray, ndarray][source]#

Compute Fourier spectrum for spectral analysis.

Parameters:
unp.ndarray

Solution field

Returns:
knp.ndarray

Wave numbers

magnitudenp.ndarray

Magnitude \(|\hat{u}_k|\) of Fourier coefficients

phasenp.ndarray

Phase angle of Fourier coefficients

rhs(u: ndarray, t: float, source_term: Callable | None = None) ndarray[source]#

Compute right-hand side of semi-discrete KdV equation.

RHS = \(-6u u_x - u_{xxx} + f(x,t)\)

Parameters:
unp.ndarray

Solution at current time

tfloat

Current time

source_termCallable[[np.ndarray, float], np.ndarray] | None, optional

Optional source term function f(x, t) for manufactured solutions

Returns:
np.ndarray

Time derivative du/dt

solve(u0: ndarray, t_final: float, dt: float, save_every: int = 1, integrator: TimeIntegrator = None, measure_performance: bool = False) tuple[ndarray, ndarray] | tuple[ndarray, ndarray, dict][source]#

Solve KdV equation from t=0 to t=t_final.

Parameters:
u0np.ndarray

Initial condition

t_finalfloat

Final time

dtfloat

Time step

save_everyint, optional

Save solution every N steps, by default 1

integratorTimeIntegrator, optional

Time integration method, by default RK4()

measure_performancebool, optional

Measure and return performance metrics, by default False

Returns:
t_savednp.ndarray

Times at which solution was saved

u_savednp.ndarray

Saved solutions (shape: [n_saves, N])

performancedict, optional

Performance metrics (returned if measure_performance=True): - ‘wall_time_s’: Total wall time in seconds - ‘mean_step_time_ms’: Mean time per step in milliseconds - ‘std_step_time_ms’: Standard deviation of step times - ‘nsteps’: Total number of time steps

static stable_dt(N: int, L: float, u_max: float, *, integrator_name: str = 'rk4', dealiased: bool = False) float[source]#

Compute stable time step via absolute-stability & frozen coefficients.

Semi-discrete KdV eigenvalues (frozen u): \(\lambda_k = ik(k^2 - 6u_{max})\), so \(|\lambda_k| = |k| \cdot |k^2 - 6u_{max}|\). Choose \(\Delta t\) so that \(\Delta t |\lambda_{max}| \leq s(method)\) on the imaginary axis.

Parameters:
N, Lgrid size and half-domain for [-L, L]
u_maxmax \(|u|\) expected (for 1-soliton with parameter c, use u_max = c/2)
integrator_nameone of {“rk4”, “rk3”}
dealiasedretained for API compatibility; the returned Δt is conservative

across both aliased and de-aliased configurations.

Returns:
float

Suggested stable time step.