ZPinchSim is a lightweight, from‑scratch scaffold, research‑oriented codebase to study ans simulate the linear and weakly nonlinear evolution of the Z‑pinch in cylindrical geometry. It solves a resistive MHD model on an
Why this tool?
- Pedagogical clarity with production‑grade numerics (finite‑volume + CT).
- Fast iteration: simple YAML configs, compact binary, CSV snapshots.
- Diagnostics‑first: growth rates, energies, and mode amplitudes out of the box.
-
Pseudo‑3D awareness: projections for
$m=1$ content from$(r,z)$ fields.
A Z‑pinch confines plasma with its self‑generated azimuthal field
where
However, Z‑pinches are prone to two archetypal current‑driven instabilities:
-
Sausage (
$m=0$ ): axisymmetric modulation of column radius; segments thin and thicken, altering line density and magnetic pressure. It’s mainly a compressional mode and can disrupt via necking. -
Kink (
$m=1$ ): helical transverse displacement of the column; the entire filament “snakes.” It’s primarily a transverse instability, sensitive to current profile and line‑tying.
Sheared axial flow
We solve single‑fluid resistive MHD in cylindrical coordinates, with primitive variables
Continuity
Momentum (radial and axial)
with cylindrical source terms near
Induction (Constrained Transport for
Azimuthal field
Discretized as a conservative advection in
Energy (toy‑model internal energy handled via
Equilibrium builder: initial
-
Finite Volume (FV) + MUSCL reconstruction (2nd‑order)
Reconstructs left/right states at interfaces with TVD limiters (Minmod/MC) to reduce spurious oscillations near sharp gradients (e.g., forming necks in sausage).
Impact: keeps the solution non‑oscillatory and accurate in smooth regions—essential to measure growth rates reliably. -
Approximate Riemann solvers (HLL in
$r$ and$z$ )
Supplies physically consistent fluxes at cell faces. HLL is robust for mixed fast/Alfvénic waves and avoids odd–even decoupling.
Impact: well‑behaved shock/rarefaction handling and stable time stepping across all regimes explored. -
Constrained Transport (CT) for
$(B_r,B_z)$
Updates magnetic flux with a staggered electromotive field$E_\theta$ so that the discrete divergence of$(B_r,B_z)$ remains near machine precision.
Impact: preserves$\nabla!\cdot\mathbf{B}\approx0$ , preventing spurious forces that would fake or mask instabilities. -
Axisymmetric
$B_\theta$ operator
Dedicated advection–diffusion update consistent with cylindrical geometry and optional resistivity$\eta_\theta$ .
Impact: permits clean studies of current‑driven modes while controlling numerical dissipation separately from CT. -
Cylindrical source terms (well‑balanced)
Special handling of$1/r$ terms (e.g.,$B_\theta^2/r$ in radial momentum).
Impact: maintains near‑equilibrium cores without artificial motion, improving signal‑to‑noise of growing modes. -
Boundary Conditions
-
Axis
$r=0$ : symmetry on scalars,$v_r=0$ ,$B_r=0$ , consistent extrapolation for others. -
Outer
$r=R_\text{max}$ : reflective/solid‑wall for velocities, copying for scalars; fields handled to avoid artificial currents. -
$z$ boundaries: periodic or sponge layers (exponential damping) to absorb outgoing content.
Impact: prevents non‑physical reflections that would corrupt growth‑rate fits and spatial spectra.
-
Axis
-
Kreiss–Oliger (KO) edge filter
A gentle high‑order smoother applied only near domain edges.
Impact: tames grid‑scale noise seeded at boundaries without degrading interior dynamics. -
CFL time step and velocity cap
$dt \le \text{CFL}\cdot \min(dr,dz)/c_\text{fast}$ using a conservative fast‑magnetosonic estimate$c_f=\sqrt{c_s^2+v_A^2}$ , with an optional cap on apparent wave speed.
Impact: stable, predictable stepping and performance across parameter scans. -
Mode seeding (
$m=0$ or$m=1$ )
Controlled perturbations$,\propto \cos(k z)$ (sausage) or$m=1$ content via side fields.
Impact: reproducible linear growth rates and mode identification.
ZPinchSim/
├─ configs/ # YAML runs: kink.yaml, sausage.yaml, stable.yaml, helper.yaml
├─ include/ # Doxygen-documented headers (grid, state, physics, rsolver, io, utils, reconstruction)
├─ src/ # Implementations (physics.cpp is deeply documented)
├─ pyscripts/ # Python tools: utils.py, diagnostics.py, viz.py
├─ CMakeLists.txt
├─ LICENSE
└─ README.md
# Configure and build
mkdir build && cd build
cmake ..
cmake --build . --config Release -j
# Run (examples)
./zpinch_run ../configs/kink.yaml
./zpinch_run ../configs/sausage.yaml
./zpinch_run ../configs/stable.yaml
# or a custom file
./zpinch_run ../configs/custom.yamlOn Windows with the provided paths:
.\zpinch_run.exe .\configs\kink.yaml
.\zpinch_run.exe .\configs\sausage.yaml
.\zpinch_run.exe .\configs\stable.yaml
.\zpinch_run.exe .\configs\custom.yaml
Output is written to data/<run_name>/ as CSV snapshots plus minimal metadata.
-
Grid + Config: domain
$(0\le r\le R_\text{max}, 0\le z\le Z_\text{max})$ , guards, limiters, CFL, resistivities, BCs are read from YAML. -
Equilibrium Builder: constructs
$B_\theta(r)$ and$p(r)$ satisfying approximate radial force balance; adds uniform$B_{z0}$ if requested. -
Optional Shear Flow: injects
$v_z(r)$ (linear or localized Gaussian) to test shear‑stabilization hypotheses. -
Mode Seeding: small perturbations at wavenumber
$k$ (sausage/kink). -
Time Integration Loop (per step):
- Reconstruct MUSCL states; compute HLL fluxes (radial and axial sweeps).
- CT update for
$(B_r,B_z)$ via$E_\theta = -(v_r B_z - v_z B_r) + \eta_\text{ct}(\partial_r B_z - \partial_z B_r)$ . - Axisymmetric
$B_\theta$ advection–diffusion update. - Apply cylindrical sources, KO edge filter, BCs, and optional sponge.
- Half‑step corrector pass (SSP‑RK2‑like) with the same phases.
-
I/O: snapshots every
output_everysteps; diagnostics CSV for lightweight monitoring. - Finish: final snapshot, metrics flush, concise summary on stderr/stdout.
Before using the Python tools, run the utility installer once (creates/activates a venv and installs requirements if missing):
python pyscripts/utils.py --bootstrapDetects dominant
Recommended commands
# Sausage (m=0), 2.5D dataset
python pyscripts/diagnostics.py --run-dir .\data\sausage_m0_2p5d --field p --mode volume --demean-z --save-spectrum
# Kink (m=1), 2.5D dataset
python pyscripts\diagnostics.py --run-dir .\data\kink_m1_2p5d --field p --mode volume --demean-z --save-spectrum
# Stable reference
python pyscripts\diagnostics.py --run-dir .\data\stable_ref_2p5d --field p --mode volume --demean-z --save-spectrumOutputs:
-
diagnostics_Ak.csv,diagnostics_energy.csv - optional
diagnostics_fit_gamma.csvwith best‑fit$\gamma$ and window -
diagnostics_lnA_fit.pngwhen a fit succeeds -
diagnostics_spectrum_k.csvif$k$ is auto‑detected
Synthesizes kink/sausage/stable visualizations and optional MP4s by re‑mapping the
Recommended commands
# Sausage movie (auto logistic growth, softer clamp in radius)
python pyscripts/viz.py --run-dir ./data/sausage_m0_2p5d --field p --mode auto --amp-mode auto-logistic \
--logistic-power 1.8 --a-sat-frac 0.65 --sausage-bounds 0.60 1.60 --sausage-ksoft 6.0 --sausage-beta 0.95 \
--make-mp4 --fps 24
# Kink movies (two presets)
python pyscripts/viz.py --run-dir ./data/kink_m1_2p5d --field p --mode auto --amp-mode auto-logistic \
--logistic-power 1.6 --a-sat-frac 0.60 --make-mp4 --fps 24
python pyscripts\viz.py --run-dir .\data\kink_m1_2p5d --field p --mode auto --amp-mode auto-logistic \
--a-sat-frac 0.5 --make-mp4 --fps 24
# Stable reference (no growth)
python pyscripts\viz.py --run-dir .\data\stable_ref_2p5d --field p --mode stable --make-mp4 --fps 24Artifacts:
viz_{field}_{mode}.mp4(if--make-mp4), andviz_{field}_{mode}_last.png
-
$A_k(t)$ growth: in the linear stage,$A_k(t)\sim e^{\gamma t}$ so$\ln A_k$ is a straight line. The fitter reports$\gamma$ , window$[t_\text{min},t_\text{max}]$ , and$R^2$ . -
Energies: volume integrals of magnetic (
$E_B$ ), kinetic ($E_K$ ), and internal ($E_\mathrm{int}$ ) proxies help identify transfers during onset/saturation. -
Effect of shear: enabling a radial shear in
$v_z(r)$ should reduce$\gamma_{m=0}$ and can modify$m=1$ depending on profiles (qualitative agreement with kinetic results).
-
Sausage (
$m=0$ )

Axisymmetric necking/bulging; strong modulation of cross‑section and axial magnetic pressure. Growth rate$\gamma$ from$A_k(t)$ should be clearly exponential before saturation. -
Kink (
$m=1$ )

Helical centerline displacement; column “snakes” along$z$ . Diagnostics show transverse motion with comparatively weaker compression. -
Stable Reference

Equilibrium persists; perturbations are either absent or damped by resistivity/sponge/shear.
- Single‑fluid resistive MHD (no two‑fluid/Hall/kinetic effects).
- Simplified energy handling; radiative and viscous terms omitted.
- Axisymmetric base fields with pseudo‑3D
$m=1$ projection for visualization/diagnostics. - Intended for linear/early‑nonlinear exploration; not a reactor design tool.
-
Brio–Wu shock tube along
$z$ : verify FV+HLL+limiter performance. -
Equilibrium hold: run with no seeds, check
$L_2$ residual of radial balance and$\nabla!\cdot \mathbf{B}$ . -
Resolution study: confirm
$\gamma$ convergence with$(N_r,N_z)$ . -
Shear scans: varying
$dv_z/dr$ to reproduce trends in sausage suppression.
- K. Tummel et al., “Kinetic simulations of sheared flow stabilization in high‑temperature Z‑pinch plasmas,” Physics of Plasmas 26, 062506 (2019). https://doi.org/10.1063/1.5092241
- J. P. Freidberg, Ideal MHD (Cambridge Univ. Press, 2014).
- F. F. Chen, Introduction to Plasma Physics and Controlled Fusion (Springer, 2016).
- Author: Juan Pablo Solís Ruiz
- Email: jp.sruiz18.tec@gmail.com
- GitHub: h4xter1612