A high-performance solver for 2D Kelvin-Helmholtz instability with Numba acceleration
Project description
kh2d: Python-based incompressible Kelvin-Helmholtz 2D Instability Solver
A high-performance solver for 2D Kelvin-Helmholtz instability for incompressible flows using finite difference methods with Numba acceleration.
Features
- Numba JIT compilation for 10-50x speedup
- Multi-core parallelization support
- High-order finite difference schemes
- FFT-based Poisson solver
- Multiple predefined scenarios (shear layers, rotating flows, forced turbulence)
- NetCDF output format
- Animated GIF generation
- Conservation monitoring (mass, momentum, energy)
Installation
Install from PyPI (Recommended)
pip install kh2d-solver
Install from Source
git clone https://github.com/sandyherho/kelvin-helmholtz-2d-solver.git
cd kelvin-helmholtz-2d-solver
pip install .
Development Installation
For development with editable installation:
git clone https://github.com/sandyherho/kelvin-helmholtz-2d-solver.git
cd kelvin-helmholtz-2d-solver
pip install -e .
Quick Start
Run simulations directly after installation:
# Run a basic shear layer simulation
kh2d-simulate basic_shear
# Run with multiple cores (e.g., 8 cores)
kh2d-simulate basic_shear --cores 8
# Run all predefined scenarios
kh2d-simulate --all
# Use a custom configuration
kh2d-simulate --config my_config.txt
Python API Usage
import numpy as np
from kh2d_solver import KH2DSolver, ShearLayer
# Create solver
solver = KH2DSolver(nx=256, nz=128, lx=2.0, lz=1.0)
# Set initial conditions
ic = ShearLayer(shear_thickness=0.05, u_top=1.0, u_bot=-1.0)
u0, w0, rho0 = ic(solver.x, solver.z)
# Run simulation
result = solver.solve(
u0=u0, w0=w0, rho0=rho0,
t_final=10.0,
reynolds=1000,
richardson=0.25
)
# Access results
vorticity = result['vorticity']
density = result['rho']
Performance Options
The solver automatically uses Numba JIT compilation for critical loops. You can control parallelization:
# Use all available cores (default)
kh2d-simulate basic_shear
# Specify number of cores
kh2d-simulate basic_shear --cores 4
# Disable parallelization (single core)
kh2d-simulate basic_shear --cores 1
Configuration
Configuration files are simple text files with key-value pairs:
# Example configuration
scenario_name = My Simulation
nx = 256
nz = 128
t_final = 10.0
reynolds = 1000
richardson = 0.25
n_cores = 8 # Optional: specify cores (default: all available)
Predefined Scenarios
- basic_shear - Classical Kelvin-Helmholtz instability
- double_shear - Double shear layer configuration
- rotating - KH instability with system rotation
- forced - Forced turbulence with energy injection
All scenarios use a unified 2.0 × 1.0 domain for direct comparison.
Output
The solver generates:
- NetCDF files with complete flow field data
- Animated GIFs showing vorticity (ω_z) and density evolution
- Log files with simulation parameters and performance metrics
Physics
The solver solves the 2D incompressible Navier-Stokes equations with density stratification:
- Continuity: ∇·u = 0
- Momentum: ∂u/∂t + (u·∇)u = -(1/ρ₀)∇p + ν∇²u - (ρ/ρ₀)gk
- Density: ∂ρ/∂t + u·∇ρ = κ∇²ρ
The vorticity shown is the z-component: ω_z = ∂w/∂x - ∂u/∂z
Publishing to PyPI (For Maintainers)
First-time Setup
- Create PyPI Account: Register at pypi.org
- Get API Token: Account Settings → API tokens → Create token
- Configure Poetry:
poetry config pypi-token.pypi pypi-XXXXXXXX
Publishing Process
# Update version in pyproject.toml
poetry version patch # or minor/major
# Build the package
poetry build
# Publish to PyPI
poetry publish
Version Management
Follow semantic versioning (MAJOR.MINOR.PATCH):
- MAJOR: Breaking changes
- MINOR: New features (backwards compatible)
- PATCH: Bug fixes
Requirements
- Python 3.8+
- NumPy
- SciPy
- Matplotlib
- netCDF4
- tqdm
- Numba
Authors
- Sandy H. S. Herho (sandy.herho@email.ucr.edu)
- Faiz R. Fajary
- Iwan P. Anwar
- Faruq Khadami
- Gandhi Napitupulu
- Nurjanna J. Trilaksono
License
This project is licensed under the WTFPL - Do What The F*ck You Want To Public License. See the LICENSE file for details.
Citation
If you use this software in your research, please cite:
@software{kh2d_solver_2025,
title = {Kelvin-Helmholtz 2D Instability Solver},
author = {Herho, Sandy H. S. and Fajary, Faiz R. and Anwar, Iwan P. and
Khadami, Faruq and Napitupulu, Gandhi and Trilaksono, Nurjanna J.},
year = {2025},
version = {0.1.0},
url = {https://github.com/sandyherho/kelvin-helmholtz-2d-solver}
}
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For issues, questions, or suggestions, please open an issue on GitHub.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kh2d_solver-0.1.0.tar.gz.
File metadata
- Download URL: kh2d_solver-0.1.0.tar.gz
- Upload date:
- Size: 17.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.2 Linux/6.11.9-100.fc39.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe2a30e52727de9f9d67e67c3d5a5eeec0da3e5db2db1ceb097e7ce91602195d
|
|
| MD5 |
0c3ff220d58602c4425eebbe91f310b8
|
|
| BLAKE2b-256 |
469500fcd486063903a6b107f50782350f195556a969ff395ca30cc35b1b2a40
|
File details
Details for the file kh2d_solver-0.1.0-py3-none-any.whl.
File metadata
- Download URL: kh2d_solver-0.1.0-py3-none-any.whl
- Upload date:
- Size: 18.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.2 Linux/6.11.9-100.fc39.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e1efb4330b445ab9d72322c2a1fec65e61ec70e6d0e7a56e36eddc73df1ca7e
|
|
| MD5 |
0f60ee3ff6d2ba4d4821e9b145cc5f2f
|
|
| BLAKE2b-256 |
68b5b91bb6950d7ba0eae795f089aacc13b0196f160379eae90f336b355e5371
|