Skip to main content

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.

Release files for sangkuriang-ideal-solver 0.0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sangkuriang-ideal-solver 0.0.11
File Size Uploaded
sangkuriang_ideal_solver-0.0.11.tar.gz 22.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sangkuriang-ideal-solver 0.0.11
File Interpreter ABI Platform
sangkuriang_ideal_solver-0.0.11-py3-none-any.whl Python 3 none any Details

Total release size:47.3 kB

Release files / sangkuriang_ideal_solver-0.0.11.tar.gz

Download URL sangkuriang_ideal_solver-0.0.11.tar.gz
Size 22.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1bbb482331f0a2523eb6d976d876bd962e0d93152db40aeaf94245c5a52cda64
BLAKE2b-256 checksum
How to use checksums
049adb908c81d47ba46bd686489a0929fa0ee95d7e8aaad5a0c11ca352d8e8a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.11.7 Linux/5.15.0-173-generic

Release files / sangkuriang_ideal_solver-0.0.11-py3-none-any.whl

Download URL sangkuriang_ideal_solver-0.0.11-py3-none-any.whl
Size 24.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b99c8c88e1bf7372d700662af4702c76d4d8074af5eb06c7c099c9b31dcdbf30
BLAKE2b-256 checksum
How to use checksums
f254a0e55406e278afcf1fea721ba6922f8195dc608dd8c987e70d9d92a26943
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.11.7 Linux/5.15.0-173-generic

Release history Release notifications | RSS feed

This release

0.0.11 This release

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page