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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

geant4_pybind-0.1.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (28.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

geant4_pybind-0.1.1-cp310-cp310-macosx_11_0_arm64.whl (17.7 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

geant4_pybind-0.1.1-cp310-cp310-macosx_10_15_x86_64.whl (18.9 MB view details)

Uploaded CPython 3.10macOS 10.15+ x86-64

File details

Details for the file geant4_pybind-0.1.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for geant4_pybind-0.1.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6e85e39414426bff18de2ffee4c1351881547c1b1df587a12832801e3f544112
MD5 d15dccc3eace47f76e8e15ab3fc3b52a
BLAKE2b-256 db61cc378b6ad17e283bffd073e209e996f38140a98e0d9fcef242b58c0a82c1

See more details on using hashes here.

File details

Details for the file geant4_pybind-0.1.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for geant4_pybind-0.1.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0db2339601e92851ec6703b6b37fa4a3cfbb61c9bf7014fa9775ccfb5cff48a3
MD5 470dc02a14276ac439d26537943594ac
BLAKE2b-256 9526e000915bf7e59f29ed9ce522a355f0cf6de53d7b2e2577483d1acbbef0d8

See more details on using hashes here.

File details

Details for the file geant4_pybind-0.1.1-cp310-cp310-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for geant4_pybind-0.1.1-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 7ae645bb9d94f4cfabb91f3afe57403ee2eeab52745310e54adaa34209f0985a
MD5 687444819938a336dcc39fe59fee814b
BLAKE2b-256 b2651f37ce6dfc1e0bb4878ab1d020d5e65e0defd6266272b271e008d7264540

See more details on using hashes here.

Supported by

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