Skip to main content

Alternative python bindings for Geant4 via pybind11

Project description

Python bindings for Geant4

Tests Wheel PyPI

Alternative Python bindings for Geant4 via pybind11. It is loosely based on g4py, but retains an API closer to the standard C++ API and does not depend on Boost.Python.

It currently includes all g4py bindings plus a large portion of very commonly used classes and functions that aren't currently present in g4py. However, it is still off from replicating the full Geant4 API.

Installation

It's available on PyPI for all major platforms as a fully self-contained (i.e. statically linked) wheel.

pip3 install geant4-pybind

Building

Unix (Linux, OS X) prerequisites

  • A Geant4 installation version 11.0+, compiled with CMake option GEANT4_BUILD_TLS_MODEL=global-dynamic (make sure it is also added to the path)
  • A C++17 compiler (preferably the one Geant4 was compiled with)
  • Python3
  • pip
  • git

Windows prerequisites

Note: You can let geant4_pybind manage and download the datasets, if the necessary environment variables haven't been set yet.

Installation

git clone --recursive https://github.com/HaarigerHarald/geant4_pybind
pip3 install ./geant4_pybind

Windows notes

The above commands have to be executed in a "Developer Command Prompt for VS 2022" and require that pip and git are added to the PATH environment variable.

General notes

If the repository was cloned non-recursively you can check out the submodule with:

git -C ./geant4_pybind submodule update --init

Usage

Minimal

A minimal example that just starts a Geant4 shell:

from geant4_pybind import *
import sys

ui = G4UIExecutive(len(sys.argv), sys.argv)
ui.SessionStart()

Simulation examples

Check out the examples directory, which contains the Geant4 basic examples ported to Python. However, writing simulations in Python should be pretty straightforward as the API is almost identical to the C++ one. One notable exception is template classes, which have been renamed such that their type replaces the T (i.e. G4TScoreNtupleWriter<G4GenericAnalysisManager> becomes G4GenericScoreNtupleWriter).

Parallelism

One main caveat of using Python for Geant4 simulations is the global interpreter lock (GIL) that prevents Python code from being executed on multiple threads at the same time. Thus, using any of the multithreaded RunManagers in Python just increases complexity without the benefit of a better runtime. In order to circumvent this limitation and achieve some parallelism, one can facilitate Python's multiprocessing module. An example of how one might do this is exampleB1MT.py.

Design

There is a small overview, outlining the design choices made. It should be helpful to anyone seeking to extend the bindings.

License

This builds upon Geant4 and g4py and thus the Geant4 license applies. The binding code uses pybind11. All my contributions are public domain.

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

geant4_pybind-0.1.0b17.tar.gz (439.5 kB view hashes)

Uploaded Source

Built Distributions

geant4_pybind-0.1.0b17-pp310-pypy310_pp73-win_amd64.whl (15.0 MB view hashes)

Uploaded PyPy Windows x86-64

geant4_pybind-0.1.0b17-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

geant4_pybind-0.1.0b17-pp310-pypy310_pp73-macosx_10_15_x86_64.whl (26.6 MB view hashes)

Uploaded PyPy macOS 10.15+ x86-64

geant4_pybind-0.1.0b17-pp39-pypy39_pp73-win_amd64.whl (15.0 MB view hashes)

Uploaded PyPy Windows x86-64

geant4_pybind-0.1.0b17-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

geant4_pybind-0.1.0b17-pp39-pypy39_pp73-macosx_10_15_x86_64.whl (26.5 MB view hashes)

Uploaded PyPy macOS 10.15+ x86-64

geant4_pybind-0.1.0b17-pp38-pypy38_pp73-win_amd64.whl (15.0 MB view hashes)

Uploaded PyPy Windows x86-64

geant4_pybind-0.1.0b17-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

geant4_pybind-0.1.0b17-pp38-pypy38_pp73-macosx_10_15_x86_64.whl (26.5 MB view hashes)

Uploaded PyPy macOS 10.15+ x86-64

geant4_pybind-0.1.0b17-pp37-pypy37_pp73-win_amd64.whl (15.0 MB view hashes)

Uploaded PyPy Windows x86-64

geant4_pybind-0.1.0b17-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

geant4_pybind-0.1.0b17-pp37-pypy37_pp73-macosx_10_15_x86_64.whl (26.5 MB view hashes)

Uploaded PyPy macOS 10.15+ x86-64

geant4_pybind-0.1.0b17-cp312-cp312-win_amd64.whl (15.0 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

geant4_pybind-0.1.0b17-cp312-cp312-musllinux_1_1_x86_64.whl (33.7 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

geant4_pybind-0.1.0b17-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.5 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

geant4_pybind-0.1.0b17-cp312-cp312-macosx_11_0_arm64.whl (24.4 MB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

geant4_pybind-0.1.0b17-cp312-cp312-macosx_10_15_x86_64.whl (26.8 MB view hashes)

Uploaded CPython 3.12 macOS 10.15+ x86-64

geant4_pybind-0.1.0b17-cp311-cp311-win_amd64.whl (15.0 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

geant4_pybind-0.1.0b17-cp311-cp311-musllinux_1_1_x86_64.whl (33.8 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

geant4_pybind-0.1.0b17-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.5 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

geant4_pybind-0.1.0b17-cp311-cp311-macosx_11_0_arm64.whl (24.3 MB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

geant4_pybind-0.1.0b17-cp311-cp311-macosx_10_15_x86_64.whl (26.5 MB view hashes)

Uploaded CPython 3.11 macOS 10.15+ x86-64

geant4_pybind-0.1.0b17-cp310-cp310-win_amd64.whl (15.0 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

geant4_pybind-0.1.0b17-cp310-cp310-musllinux_1_1_x86_64.whl (33.8 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

geant4_pybind-0.1.0b17-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.5 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

geant4_pybind-0.1.0b17-cp310-cp310-macosx_11_0_arm64.whl (24.3 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

geant4_pybind-0.1.0b17-cp310-cp310-macosx_10_15_x86_64.whl (26.5 MB view hashes)

Uploaded CPython 3.10 macOS 10.15+ x86-64

geant4_pybind-0.1.0b17-cp39-cp39-win_amd64.whl (15.6 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

geant4_pybind-0.1.0b17-cp39-cp39-musllinux_1_1_x86_64.whl (33.8 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

geant4_pybind-0.1.0b17-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.5 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

geant4_pybind-0.1.0b17-cp39-cp39-macosx_11_0_arm64.whl (24.3 MB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

geant4_pybind-0.1.0b17-cp39-cp39-macosx_10_15_x86_64.whl (26.5 MB view hashes)

Uploaded CPython 3.9 macOS 10.15+ x86-64

geant4_pybind-0.1.0b17-cp38-cp38-win_amd64.whl (15.0 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

geant4_pybind-0.1.0b17-cp38-cp38-musllinux_1_1_x86_64.whl (33.8 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

geant4_pybind-0.1.0b17-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.5 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

geant4_pybind-0.1.0b17-cp38-cp38-macosx_11_0_arm64.whl (24.3 MB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

geant4_pybind-0.1.0b17-cp38-cp38-macosx_10_15_x86_64.whl (26.5 MB view hashes)

Uploaded CPython 3.8 macOS 10.15+ x86-64

geant4_pybind-0.1.0b17-cp37-cp37m-win_amd64.whl (14.7 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

geant4_pybind-0.1.0b17-cp37-cp37m-musllinux_1_1_x86_64.whl (33.9 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

geant4_pybind-0.1.0b17-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.8 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

geant4_pybind-0.1.0b17-cp37-cp37m-macosx_10_15_x86_64.whl (26.3 MB view hashes)

Uploaded CPython 3.7m macOS 10.15+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page