|
bi-kappa 3.0.0
Monte Carlo particle samplers for plasma velocity and position distributions
|
Header-only C++11 samplers for loading particle velocities and positions in kinetic plasma simulations (particle-in-cell and hybrid codes). The package samples bi-Kappa and bi-Maxwellian velocity distributions about an arbitrary magnetic-field direction, and draws velocities or positions from densities you supply. A Python script tests a bi-Kappa velocity sample from any loader against the target distribution.
Copy the headers you need from cpp/ into your project, or add cpp/ to the include path. The headers need C++11 and nothing outside the standard library. Continuous integration builds and runs the regression suite with GCC and libstdc++ and with Clang and libc++ on Linux x86-64, and with Apple Clang and libc++ on macOS arm64, and compiles each header on its own with -std=c++11 -pedantic-errors.
To build and run the code shipped in cpp/:
The regression suite takes about 10 s. main.exe writes five sample files of 50,000 lines each to the current directory; samples_bikappa.txt holds bi-Kappa velocities drawn with no_cap(), kappa = 2, theta_perp = 1, theta_par = 2 and ub = {0, 0, 1}.
The usage examples include complete programs for an oblique field direction, for loading particles with a bulk drift and a temperature in a particle-in-cell code, and for bi_kappa_distribution<float>.
bi_kappa_distribution and bi_maxwellian_distribution take a cap max_normalized_velocity, in thermal speeds. It is applied to each component in field-aligned coordinates: a draw with |v_i| / theta_i > cap for any component is redrawn. The samples therefore follow the distribution conditioned on that box, not the distribution itself.
Details, including the frame in which the box is applied, are on the parameters page.
| Header / class | Samples | define(...) arguments |
|---|---|---|
| bi_kappa_distribution | bi-Kappa velocity | kappa, theta_perp, theta_par, ub[, cap[, seed]] |
| bi_maxwellian_distribution | bi-Maxwellian velocity | theta_perp, theta_par, ub[, cap[, seed]] |
| general_velocity_generator | speed from your g(w), with w the speed squared; isotropic direction | g, w_min, w_max[, probe_points, max_reject_tries] |
| field_aligned_velocity_generator | parallel speed from your g(w), w = v_par^2, with a fixed sign along ub; Maxwellian perpendicular components | g, w_min, w_max, theta_perp, ub, sign[, probe_points, max_reject_tries, cap] |
| general_position_generator | position in 1-3 dimensions from your density rho(x) | dimension, lower, upper, rho[, probe_points, max_reject_tries] |
Common behavior:
The Python code needs Python 3.10 or later.
bikappa_validate.py tests an (N, 3) velocity sample, with N >= 1000, from any bi-Kappa loader against the bi-Kappa distribution. It uses no moments, so it also applies at kappa <= 3/2. It tests the radius, the direction, their independence, the counts in cells of equal probability (with the radius resolved into the tail as far as the sample size allows), the anisotropy, and the number of non-finite values.
To test the sample written by the demo program (cd cpp && make && ./main.exe), run from python/:
A text sample has three columns, vx vy vz in the global frame, one draw per line, separated by whitespace (by commas for a .csv file). Blank lines and lines starting with # are ignored, and a non-numeric first line such as vx vy vz is skipped as a header.
| Option | Meaning |
|---|---|
| sample | .npy array of shape (N, 3); raw float64 (.bin, .f64), three values per draw; any other extension is read as text |
| --kappa, --theta-perp, --theta-par | parameters of the target distribution (required) |
| --bhat BX BY BZ | field direction; omit if the third column is already the parallel component |
| --attempts N | total draws attempted, if the loader redrew samples outside a cap; adds the rejected fraction to the report |
| --alpha A | family-wise significance level, default 0.01; the tests are corrected for multiple testing (Holm) |
| --binary | read the file as raw float64 whatever its extension |
| --json | print the full report as JSON |
The exit status is 0 if every test passes, 1 if at least one test fails, and 2 if the input or the options are invalid (with a message on standard error). The tests compare the sample with the uncapped bi-Kappa distribution, so a sample from bi_kappa_distribution should be drawn with no_cap(); the default cap of 20 changes the distribution.
From Python:
Other Python files:
Each experiment directory has a README with its commands and outputs. Run the commands from that directory.
| Directory | In the paper | Command |
|---|---|---|
| experiments/exp1_radial_directional | Tables III and IV, Figs. 4 and 5, Sec. VI C | make cells tail moments frame marginals (cells first) |
| experiments/exp2_cap_characterization | Table II, Fig. 3 | make run, make verify, then uv run --project ../../python python exp2_analyze.py |
| experiments/exp4_finite_precision | Fig. 2 | make figures redraws it from the committed results; the full study (protocol, sampling, analysis) is in the GitHub repository, see its README |
| experiments/exp3_benchmark | not shown (speed comparison with two other samplers) | make run, then uv run --project ../../python python exp3_analyze.py |
paper/figures/make_manuscript_assets.py regenerates the paper's figures and tables from the committed experiment results. It writes them into paper/overleaf/figures/ and paper/overleaf/tables/, which it creates; the manuscript source itself is not part of this repository.
Please cite the paper and the software:
GitHub's "Cite this repository" button reads CITATION.cff.
MIT. See LICENSE.