Skip to content

Repository files navigation

favicon Ballistic Solver Library

LGPL library for small arms ballistic calculations based on point-mass (3 DoF) plus spin drift.

license pypi pypi-pre downloads downloads/month coverage py-versions Made in Ukraine

powered by bclibc powered by pyodide

Pre-commit

Contents

Installation

uv

uv add py-ballisticcalc

# Using precompiled backend (improves performance)
uv add py-ballisticcalc[exts]

# Using matplotlib and pandas uses additional dependencies
uv add py-ballisticcalc[charts]

# Get everything, including the SciPy engine
uv add py-ballisticcalc[exts,charts,scipy]

pip

pip install py-ballisticcalc

# Using precompiled backend (improves performance)
pip install py-ballisticcalc[exts]

# Using matplotlib and pandas uses additional dependencies
pip install py-ballisticcalc[charts]

# Get everything, including the SciPy engine
pip install py-ballisticcalc[exts,charts,scipy]

Quick Start - click here to open Quick Start guide

Prefer to try it before installing anything? Open the Interactive Web REPL — runs entirely in your browser via Pyodide.

Work in your preferred terms with easy conversions for the following dimensions and units:

  • Angular: radian, degree, MOA, mil, mrad, thousandth, inch/100yd, cm/100m, o'clock
  • Distance: inch, foot, yard, mile, nautical mile, mm, cm, m, km, line
  • Energy: foot-pound, joule
  • Pressure: mmHg, inHg, bar, hPa, PSI
  • Temperature: Fahrenheit, Celsius, Kelvin, Rankine
  • Time: second, minute, millisecond, microsecond, nanosecond, picosecond
  • Velocity: m/s, km/h, ft/s, mph, knots
  • Weight: grain, ounce, gram, pound, kilogram, newton

Choose between different calculation engines, or build your own. Included engines:

Mean time

Mean time per call by engine

Speedup vs python.rk4

Speedup vs python.rk4

Engine Name Speed (Find Zero / Trajectory) Dependencies Description Tests
python+rk4 Baseline (1x) None; default Runge-Kutta 4th-order integration tests
python+euler ⬇️ 0.6x / 0.6x (slower) None Euler 1st-order integration tests
python+verlet ⬇️ 0.8x / 0.8x (slower) None Verlet 2nd-order symplectic integration tests
cython+rk4 ⬆️ 205x / 129x (faster) [exts] Compiled Runge-Kutta 4th-order tests
cython+euler ⬆️ 54x / 44x (faster) [exts] Compiled Euler integration tests
cython+verlet ⬆️ 130x / 99x (faster) [exts] Compiled Verlet 2nd-order symplectic tests
cython+rkck1 ⬆️ 2567x / 326x (faster) [exts] Compiled Cash-Karp adaptive RK45 tests
cython+dopri1 ⬆️ 2567x / 326x (faster) [exts] Dormand--Prince 5(4), SciPy RK45-style controller tests
cython+tsitouras1 ⬆️ 2567x / 326x (faster) [exts] Tsitouras 5(4), SciPy RK45-style controller tests
scipy+rk23 ⬆️ 3.4x / 3.2x (faster) [scipy] SciPy solve_ivp RK23 — Bogacki–Shampine 3(2), lowest order: cheap steps, loose accuracy —
scipy+rk45 ⬆️ 5.4x / 9.8x (faster) [scipy] SciPy solve_ivp RK45 — Dormand–Prince 5(4), SciPy default: general-purpose tests
scipy+dop853 ⬆️ 1.8x / 4.8x (faster) [scipy] SciPy solve_ivp DOP853 — Dormand–Prince 8(5,3), high order: for tight tolerances —
scipy+radau ⬆️ 1.5x / 1.5x (faster) [scipy] SciPy solve_ivp Radau — implicit Radau IIA 5th-order, for stiff problems —
scipy+bdf ↔️ 0.8x / 1.5x (mixed) [scipy] SciPy solve_ivp BDF — implicit variable-order (1–5) multistep, for stiff problems —
scipy+lsoda ⬆️ 2.9x / 4.3x (faster) [scipy] SciPy solve_ivp LSODA — Adams/BDF (ODEPACK), switches automatically between non-stiff and stiff —

Tip

No C extensions on your platform? tiny-bclibc-wasm runs bclibc's tiny_bclibc solver as WebAssembly and plugs into py-ballisticcalc as the tiny_bclibc_wasm+tsitouras-dp engine. It is meant for runtimes that can run WebAssembly but can't load CPython extension modules, such as Pythonista on iOS, where the [exts] engines can't be installed. Install it with pip install tiny-bclibc-wasm[pybc].

About project

The library provides trajectory calculation for ballistic projectiles launched by airguns, bows, firearms, artillery, etc.

The core point-mass (3DoF) ballistic model underlying this project was used on the earliest digital computers. Robert McCoy (author of Modern Exterior Ballistics) implemented one in BASIC. JBM published code in C. Nikolay Gekht ported that to C#, extended it with formulas from Bryan Litz's Applied Ballistics, and ported it to Go, while Alexandre Trofimov implemented a calculator in JavaScript.

This Python3 implementation has been expanded to support multiple ballistic coefficients and custom drag functions, such as those derived from Doppler radar data.

Contributors

This project exists thanks to all the people who contribute.

Special thanks to:

  • David Bookstaber - Ballistics Expert
    For help understanding and improving the functionality
  • Serhiy Yevtushenko - Applied Mathematician
    For helping in consultations, testing, and improving edge case compatibility
  • Nikolay Gekht
    For the source code in C# and GO-lang from which this project firstly was forked

Sister projects

  • bclibc - High performance C++/C99 Ballistic solver engine
  • micropython-bclibc - Pure C99 Ballistic Solver Engine for MicroPython (bclibc subset)
  • tiny-bclibc-wasm - bclibc's tiny_bclibc as WebAssembly for CPython, PyPy and Pythonista, usable as a py-ballisticcalc engine
  • ebalistyka - Ballistic Calculator built with Flutter and high performance C++ engine
  • js-ballistics - ISC library for small arms ballistic calculations (JavaScript ES6+)

License

Copyright (C) 2023 Yaroshenko Dmytro (o-murphy)

This library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License v3.0 as published by the Free Software Foundation.

See LICENSE for the full text. See CHANGELOG for release history.

Note

bclibc (the ballistic solver engine, located in py_ballisticcalc_exts/external/bclibc) is licensed separately under the GNU Lesser General Public License v3.0. See py_ballisticcalc_exts/external/bclibc/LICENSE.

Warning

RISK NOTICE

This library performs approximate simulations of complex physical processes. Therefore, the calculation results MUST NOT be considered as completely and reliably > reflecting actual behavior of projectiles. While these results may be used for educational purpose, they must NOT be considered as reliable for the areas where incorrect calculation may cause making a wrong decision, financial harm, or can put a human life at risk.

THE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.

Footnotes

  1. Adaptive RK45; actual speed depends on the configured tolerances. Cash-Karp, Dormand-Prince, and Tsitouras measure in the same performance class as each other on typical trajectories -- see benchmarks for the measurement, not a ranking. ↩ ↩2 ↩3

About

LGPL library for small arms ballistic calculations based on point-mass (3 DoF) plus spin drift.

Topics

Resources

Contributing

Stars

65 stars

Watchers

4 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages