This repository builds a Geant4 simulation runtime into a WebAssembly (WASM) module intended to run in a Web Worker (browser-side) and be distributed together with the required Geant4 datasets.
Besides the C++ sources, the repository includes scripts that:
- bootstrap a reproducible Emscripten toolchain + dependencies (Expat, Xerces-C, Geant4)
- package Geant4 datasets for Emscripten’s virtual filesystem
- publish build artifacts and datasets to Cyfronet S3 / Ceph
- update a separate “stubs” repository with the generated JS glue code and TypeScript typings
Audience: This repo is primarily a build/artifact repository (WASM module + datasets), not a full web UI.
When built for Emscripten, the output includes:
geant4_wasm.wasm– the WebAssembly modulegeant4_wasm.js– Emscripten JS loader (ES module export name:createWasmModule)geant4_wasm.d.ts– generated TypeScript typings- dataset payloads and preload scripts under
build/data/andbuild/js/ - optional “lazy loading” metadata JSON files under
build/lazy_files/
geant4.cpp / geant4.hpp– WASM-facing API surface (Emscripten embind)include/+src/– Geant4 user classes (physics list, generator, actions)scripts/setup_env.js– injected with--pre-jsto set Geant4 dataset env vars in the WASM runtimeutils/lazy_json_generator.py– generates JSON manifests used for lazy dataset loadingprepare.sh– downloads & builds the toolchain/deps and stages datasetscompile_application.sh– builds the WASM module and stages artifactspublish.sh– uploads staged artifacts to S3 and configures CORS/policypublish_repo.sh– copies generated stubs (JS + d.ts + preload scripts) intogeant-web-stubs
The scripts assume an environment with:
- Environment Modules (
module add ...) providing at leastcmakeandrclone wget,tar,git,npm,python- outbound network access to download Geant4 datasets and sources
- a writable staging area referenced by
MEMFS
If you’re not on an HPC environment that provides
module, you can still build locally, but you’ll need to adapt the scripts (seedocs/DEVELOPMENT.md).
- Emscripten SDK: 4.0.13
- Geant4: 11.3.2
- Expat: 2.6.4
- Xerces-C: 3.3.0
- Geant4 datasets (subset staged by default):
G4NDL.4.7.1→G4NDL4.7.1G4EMLOW.8.6.1→G4EMLOW8.6.1G4PhotonEvaporation.6.1→PhotonEvaporation6.1G4ENSDFSTATE.3.0→G4ENSDFSTATE3.0G4SAIDDATA.2.0→G4SAIDDATA2.0G4PARTICLEXS.4.1→G4PARTICLEXS4.1
The happy path (as intended by this repo) is:
./prepare.sh
./compile_application.sh
./publish.sh
./publish_repo.shThe scripts rely on MEMFS pointing to a writable working directory used for downloads, builds, and staging.
Example:
export MEMFS=/path/to/fast/scratchpublish.sh reads S3 credentials through setup_env.sh which expects a .env file containing RCLONE_CONFIG_* variables.
Template:
RCLONE_CONFIG_GEANT4_WEB_TYPE=s3
RCLONE_CONFIG_GEANT4_WEB_PROVIDER=Ceph
RCLONE_CONFIG_GEANT4_WEB_ACCESS_KEY_ID=<access_key_id>
RCLONE_CONFIG_GEANT4_WEB_SECRET_ACCESS_KEY=<secret_key>
RCLONE_CONFIG_GEANT4_WEB_ENDPOINT=https://s3p.cloud.cyfronet.pl
RCLONE_CONFIG_GEANT4_WEB_REGION=
RCLONE_CONFIG_GEANT4_WEB_LOCATION_CONSTRAINT=
RCLONE_CONFIG_GEANT4_WEB_ACL=private
setup_env.shalso generates a local.s3cfgused bys3cmdfor setting bucket CORS and policy.
The module is compiled with:
- ES module export name:
createWasmModule - environment:
worker - runtime methods exported:
FSandaddFunction
High-level usage pattern:
- Instantiate the module (
await createWasmModule(...)). - Write your input files (GDML + macro) into Emscripten FS.
- Optionally register a progress callback.
- Call
Geant4GDMRun(gdmlPath, macroPath). - Read the resulting
output.rootfrom FS.
Example (simplified):
import createWasmModule from "./geant4_wasm.js";
const mod = await createWasmModule({
// Adjust if you host .wasm/.data files on a CDN
locateFile: (p) => new URL(p, self.location.href).toString(),
});
// Progress callback (called every 100 events)
const cbPtr = mod.addFunction((eventId) => {
console.log("progress event", eventId);
}, "vi");
mod.Geant4SetProgressFunction(cbPtr);
// Provide inputs
mod.FS.writeFile("/geometry.gdml", gdmlText);
mod.FS.writeFile("/run.mac", macroText);
// Run
mod.Geant4GDMRun("/geometry.gdml", "/run.mac");
// Retrieve output produced by G4AnalysisManager
const rootBytes = mod.FS.readFile("output.root");Geant4 dataset environment variables (e.g.
G4LEDATA,G4NEUTRONHPDATA, …) are set byscripts/setup_env.jsat startup.
docs/DEVELOPMENT.md– building locally vs in an HPC/modules environmentdocs/ARCHITECTURE.md– how the WASM build is wired (CMake + Emscripten + datasets)docs/API.md– runtime API exposed to JS (inputs/outputs)docs/DEPLOYMENT.md– publishing to S3 and updating stub artifactsdocs/TROUBLESHOOTING.md– common build/runtime issues
See docs/DEVELOPMENT.md for build conventions and tips.