spectral.KdVSolver#
- class spectral.KdVSolver(N: int, L: float, dealias: bool = False)[source]#
Bases:
objectKorteweg-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 for KdV equation.
Compute eigenvalues of frozen-coefficient linearized KdV operator.
Compute Fourier spectrum for spectral analysis.
Compute right-hand side of semi-discrete KdV equation.
Solve KdV equation from t=0 to t=t_final.
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
Spatial and Temporal Convergence for Fourier KdV Solver
Spatial and Temporal Convergence for Fourier KdV Solver
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:
- u
np.ndarray Solution field
- dx
float Grid spacing
- u
- Returns:
- 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_max
float Maximum amplitude for frozen-coefficient approximation
- u_max
- Returns:
np.ndarrayComplex eigenvalues of the linearized operator
- get_spectrum(u: ndarray) tuple[ndarray, ndarray, ndarray][source]#
Compute Fourier spectrum for spectral analysis.
- Parameters:
- u
np.ndarray Solution field
- u
- Returns:
- k
np.ndarray Wave numbers
- magnitude
np.ndarray Magnitude \(|\hat{u}_k|\) of Fourier coefficients
- phase
np.ndarray Phase angle of Fourier coefficients
- k
- 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:
- u
np.ndarray Solution at current time
- t
float Current time
- source_term
Callable[[np.ndarray,float],np.ndarray] |None, optional Optional source term function f(x, t) for manufactured solutions
- u
- Returns:
np.ndarrayTime 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:
- u0
np.ndarray Initial condition
- t_final
float Final time
- dt
float Time step
- save_every
int, optional Save solution every N steps, by default 1
- integrator
TimeIntegrator, optional Time integration method, by default RK4()
- measure_performancebool, optional
Measure and return performance metrics, by default False
- u0
- Returns:
- t_saved
np.ndarray Times at which solution was saved
- u_saved
np.ndarray Saved solutions (shape: [n_saves, N])
- performance
dict, 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
- t_saved
- 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, L
gridsizeandhalf-domainfor[-L,L] - u_max
max\(|u|\)expected(for1-solitonwithparameterc,useu_max= c/2) - integrator_name
oneof {“rk4”, “rk3”} - dealiased
retainedforAPIcompatibility;thereturnedΔtisconservative across both aliased and de-aliased configurations.
- N, L
- Returns:
floatSuggested stable time step.