Skip to main content

Idealized Korteweg-de Vries soliton solver with pseudo-spectral methods

Project description

sangkuriang: An Open-Source Pseudo-Spectral Solver for Idealized Korteweg–de Vries Soliton Simulations

DOI Python PyPI License: WTFPL Numba

Overview

sangkuriang is a high-performance Python solver for the idealized Korteweg-de Vries (KdV) equation, a fundamental nonlinear partial differential equation that describes the propagation of weakly nonlinear and weakly dispersive waves. The KdV equation is renowned for supporting solitons—localized wave packets that maintain their shape while traveling at constant velocity and exhibit remarkable stability during collisions.

This solver implements pseudo-spectral methods combined with adaptive high-order time integration to achieve spectral accuracy in space and exceptional temporal precision. The implementation leverages Numba's JIT compilation for significant performance gains (10-100× speedup) and supports multi-core parallel processing for large-scale simulations. The package includes comprehensive visualization tools for creating publication-quality 3D animations and NetCDF4 output for interoperability with scientific data analysis workflows.

soliton_collides

Key Applications:

  • Nonlinear wave dynamics research
  • Soliton interaction studies
  • Numerical methods benchmarking
  • Educational demonstrations of wave phenomena
  • Validation of theoretical predictions in nonlinear physics

Physics

The solver addresses the canonical form of the Korteweg-de Vries equation:

$$\frac{\partial u}{\partial t} + \varepsilon \cdot u \cdot \frac{\partial u}{\partial x} + \mu \cdot \frac{\partial^3 u}{\partial x^3} = 0$$

where:

  • $u(x,t)$ is the wave amplitude [m]
  • $\varepsilon$ is the nonlinearity parameter [m⁻¹]
  • $\mu$ is the dispersion coefficient [m³/s]
  • $x$ is the spatial coordinate [m]
  • $t$ is time [s]

Physical Interpretation:

  • Nonlinear term ($\varepsilon u \partial_x u$): Steepens wave profiles, causing wave breaking in the absence of dispersion
  • Dispersion term ($\mu \partial_x^3 u$): Spreads wave energy, with shorter wavelengths traveling faster
  • Soliton balance: Exact equilibrium between nonlinearity and dispersion creates stable traveling waves

Conservation Laws:

The KdV equation preserves three fundamental invariants:

  1. Mass: $M = \int_{-\infty}^{\infty} u , dx$

  2. Momentum: $P = \int_{-\infty}^{\infty} u^2 , dx$

  3. Energy: $E = \int_{-\infty}^{\infty} \left[\frac{\varepsilon}{2} u^3 - \frac{3\mu}{2} \left(\frac{\partial u}{\partial x}\right)^2\right] dx$

These conservation laws serve as critical diagnostic tools for assessing numerical accuracy.

Numerical Methods

Spatial Discretization: Pseudo-Spectral Method

The solver employs the Fourier pseudo-spectral method for spatial derivatives, achieving exponential convergence for smooth, periodic solutions.

Discrete Fourier Transform:

For a periodic function $u(x)$ discretized on $N$ points $x_j = x_{\text{min}} + j \Delta x$ where $j = 0, 1, \ldots, N-1$ and $\Delta x = (x_{\text{max}} - x_{\text{min}})/N$:

$$\hat{u}k = \frac{1}{N} \sum{j=0}^{N-1} u_j e^{-2\pi i k j / N}$$

Spectral Derivative Computation:

First derivative: $$\frac{\partial u}{\partial x}(x_j) = \mathcal{F}^{-1}\left[i k \hat{u}_k\right]$$

Third derivative: $$\frac{\partial^3 u}{\partial x^3}(x_j) = \mathcal{F}^{-1}\left[(i k)^3 \hat{u}_k\right]$$

where $k$ are the wavenumbers:

$$k_n = \begin{cases} \frac{2\pi n}{L} & \text{for } n = 0, 1, \ldots, N/2-1 \ \frac{2\pi (n-N)}{L} & \text{for } n = N/2, \ldots, N-1 \end{cases}$$

with $L = x_{\text{max}} - x_{\text{min}}$ being the domain length.

Advantages:

  • Exponential accuracy for smooth solutions: error $\sim \mathcal{O}(e^{-cN})$
  • Exact derivative computation in Fourier space
  • No numerical dissipation or dispersion
  • Efficient $\mathcal{O}(N \log N)$ complexity via FFT

Temporal Integration: DOP853 Method

Time integration uses the Dormand-Prince 8(5,3) method—an explicit Runge-Kutta scheme with adaptive step size control.

Method Properties:

  • Order: 8th-order accurate with 5th and 3rd order embedded error estimates
  • Stages: 12 function evaluations per step
  • Adaptivity: Automatic step size adjustment based on local error tolerance

Semi-Discrete Form:

After spatial discretization, the KdV equation becomes an ODE system:

$$\frac{d\mathbf{u}}{dt} = \mathbf{F}(\mathbf{u}, t)$$

where:

$$\mathbf{F}(\mathbf{u}, t) = -\varepsilon \mathbf{u} \odot \mathcal{F}^{-1}[i k \hat{\mathbf{u}}] - \mu \mathcal{F}^{-1}[(ik)^3 \hat{\mathbf{u}}]$$

Error Control:

The local error at each time step is estimated as:

$$\text{err} = |\mathbf{u}_8 - \mathbf{u}_5|$$

where $\mathbf{u}_8$ and $\mathbf{u}_5$ are the 8th and 5th order solutions, respectively.

Step size adjustment: $$\Delta t_{\text{new}} = 0.9 \Delta t_{\text{old}} \left(\frac{\text{tol}}{\text{err}}\right)^{1/8}$$

Tolerance Specification:

  • Relative tolerance (rtol): typically $10^{-10}$
  • Absolute tolerance (atol): typically $10^{-12}$

Combined error criterion: $$\text{err} < \text{rtol} \cdot |\mathbf{u}| + \text{atol}$$

Parallel Acceleration

Numba JIT Compilation:

Critical computational kernels are compiled using Numba's @njit(parallel=True) decorator:

@njit(parallel=True, cache=True)
def compute_nonlinear_term(u, u_x, eps):
    result = np.empty_like(u)
    for i in prange(len(u)):
        result[i] = -eps * u[i] * u_x[i]
    return result

Performance Gains:

  • 10-100× speedup over pure Python/NumPy
  • Multi-core parallelization via prange
  • Automatic vectorization and loop optimization

Features

  • Spectral accuracy: Exponential convergence for smooth solutions
  • Adaptive time stepping: DOP853 (8th-order Runge-Kutta)
  • High-performance computing: Numba JIT compilation (10-100× speedup)
  • Parallel processing: Multi-core CPU utilization
  • Professional visualization: 3D animated GIFs with conservation diagnostics
  • Scientific data format: NetCDF4 output with CF-1.8 conventions
  • Conservation monitoring: Real-time tracking of mass, momentum, and energy

Directory Structure

sangkuriang-ideal-solver/
├── configs/                          # Simulation configuration files
│   ├── case1_single_soliton.txt     # Single soliton baseline
│   ├── case2_two_solitons.txt       # Two equal solitons
│   ├── case3_collision.txt          # Soliton collision
│   └── case4_three_solitons.txt     # Three-body system
│
├── src/sangkuriang_ideal/           # Main package source
│   ├── __init__.py                  # Package initialization
│   ├── cli.py                       # Command-line interface
│   │
│   ├── core/                        # Core numerical algorithms
│   │   ├── __init__.py
│   │   ├── solver.py                # KdV solver with spectral methods
│   │   └── initial_conditions.py   # Soliton profile generators
│   │
│   ├── io/                          # Input/output handlers
│   │   ├── __init__.py
│   │   ├── config_manager.py       # Configuration file parser
│   │   └── data_handler.py         # NetCDF writer
│   │
│   ├── visualization/               # Animation and plotting
│   │   ├── __init__.py
│   │   └── animator.py             # 3D GIF generator
│   │
│   └── utils/                       # Utilities
│       ├── __init__.py
│       ├── logger.py               # Simulation logging
│       └── timer.py                # Performance profiling
│
├── outputs/                         # Generated results (created at runtime)
│   ├── *.nc                        # NetCDF data files
│   └── *.gif                       # Animation files
│
├── logs/                           # Simulation logs (created at runtime)
│   └── *.log
│
├── pyproject.toml                  # Poetry build configuration
├── README.md                       # This file
├── LICENSE                         # WTFPL license
└── .gitignore                     # Git ignore patterns

Installation

From PyPI:

pip install sangkuriang-ideal-solver

From source:

git clone https://github.com/sandyherho/sangkuriang-ideal-solver.git
cd sangkuriang-ideal-solver
pip install -e .

Quick Start

Command line:

sangkuriang case1           # Single soliton
sangkuriang case3 --cores 8 # Collision with 8 cores
sangkuriang --all           # Run all cases

Python API:

from sangkuriang_ideal import KdVSolver, SechProfile

# Initialize solver
solver = KdVSolver(nx=512, x_min=-30.0, x_max=30.0, n_cores=8)

# Create initial condition (sech² profile)
u0 = SechProfile(amplitude=4.0, width=2.0, position=-10.0)(solver.x)

# Solve
result = solver.solve(u0=u0, mu=0.1, eps=0.2, t_final=50.0)

# Check conservation
print(f"Energy error: {result['energy_error']:.2e}")

Test Cases

Case Description Physics Initial Conditions
1 Single soliton Baseline propagation $u(x,0) = 4 \text{ sech}^2((x+10)/2)$
2 Two equal solitons Phase shift interaction Two identical solitons
3 Collision Overtaking phenomenon $A_1=6$, $A_2=2$ (different heights)
4 Three-soliton system Complex multi-body dynamics $A_1=7$, $A_2=4$, $A_3=2.5$

Soliton Velocity Formula: $v = \frac{\varepsilon A}{3}$

where $A$ is the soliton amplitude. Taller solitons travel faster, leading to overtaking collisions.

Citation

If you use this software in your study, please cite:

@article{irawan2026_sangkuriang,
  title   = {{An Open-Source Pseudo-Spectral Solver for Idealized Korteweg–de Vries Soliton Simulations}},
  author  = {Irawan, Dasapta E. and Herho, Sandy H. S. and Pamumpuni, Astyka and Kartiko, Rendy D. and Khadami, Faruq Anwar, Iwan P. and Sujatmiko, Karina A. and Handayani, Alfita P. and Fajary, Faiz R. and Suwarman, R.},
  journal = {Water},
  volume  = {18},
  number = {7},
  pages   = {779},
  year    = {2026},
  note = {\url{https://doi.org/10.3390/w18070779}}
}

Authors

  • Dasapta E. Irawan (dasaptaerwin@itb.ac.id)
  • Sandy H.S. Herho
  • Astyka Pamumpuni
  • Rendy Dwi Kartiko
  • Faruq Khadami
  • Iwan P. Anwar
  • Karina A. Sujatmiko
  • Alfita P. Handayani
  • Faiz R. Fajary
  • Rusmawan Suwarman

License

MIT © 2026 Applied Geology Research Group ITB - See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sangkuriang_ideal_solver-0.0.11.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sangkuriang_ideal_solver-0.0.11-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file sangkuriang_ideal_solver-0.0.11.tar.gz.

File metadata

  • Download URL: sangkuriang_ideal_solver-0.0.11.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.11.7 Linux/5.15.0-173-generic

File hashes

Hashes for sangkuriang_ideal_solver-0.0.11.tar.gz
Algorithm Hash digest
SHA256 1bbb482331f0a2523eb6d976d876bd962e0d93152db40aeaf94245c5a52cda64
MD5 f86bf109f945c2c38c42b0ea69ec6590
BLAKE2b-256 049adb908c81d47ba46bd686489a0929fa0ee95d7e8aaad5a0c11ca352d8e8a0

See more details on using hashes here.

File details

Details for the file sangkuriang_ideal_solver-0.0.11-py3-none-any.whl.

File metadata

File hashes

Hashes for sangkuriang_ideal_solver-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 b99c8c88e1bf7372d700662af4702c76d4d8074af5eb06c7c099c9b31dcdbf30
MD5 a1b841fa3af823b94754c0b278496c53
BLAKE2b-256 f254a0e55406e278afcf1fea721ba6922f8195dc608dd8c987e70d9d92a26943

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page