Skip to content

Commit e1f6b0d

Browse files
committed
doc: update README with architecture diagram
[skip-ci]
1 parent ef5b29d commit e1f6b0d

1 file changed

Lines changed: 237 additions & 0 deletions

File tree

docs/architecture.md

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
## Architecture overview
2+
3+
Simphony is a Geant4 plus OptiX optical photon transport stack. Geant4 owns the
4+
detector construction, material definitions, event loop, and optional reference
5+
tracking. `G4CXOpticks` is the bridge into the GPU stack: `SetGeometry(world)`
6+
translates a Geant4 world into Opticks/CSG data once, and `simulate(eventID,
7+
reset)` launches GPU photon generation and propagation for each event.
8+
9+
Application-facing code lives mainly in `src/`, `config/`, `tests/`, and
10+
`optiphy/`. The lower-level packages are Opticks-derived framework libraries:
11+
`sysrap`, `CSG`, `qudarap`, `CSGOptiX`, `u4`, and `g4cx`. Optional DD4hep
12+
integration plugins live in `dd4hepplugins/` and are built only when DD4hep is
13+
available.
14+
15+
### Layered view
16+
17+
```mermaid
18+
19+
flowchart TB
20+
subgraph Inputs["Inputs"]
21+
direction TB
22+
userinputs["GDML<br/>Geant4 macro<br/>config/*.json"]
23+
end
24+
25+
subgraph Apps["Application layer"]
26+
direction TB
27+
appcode["installed targets<br/>simg4ox<br/>GPUCerenkov<br/>GPURaytrace"]
28+
end
29+
30+
subgraph Bridge["Geant4 bridge"]
31+
direction TB
32+
u4["u4<br/>Geant4 translation<br/>materials<br/>surfaces<br/>recorder utilities"]
33+
g4cx["g4cx<br/>G4CXOpticks"]
34+
end
35+
36+
subgraph Runtime["GPU runtime"]
37+
direction TB
38+
sysrap["sysrap<br/>NP arrays<br/>SEvt/SSim<br/>config<br/>photon types"]
39+
csg["CSG<br/>CSGFoundry<br/>primitives<br/>geometry model"]
40+
qudarap["qudarap<br/>QSim/QEvt<br/>RNG<br/>optical physics"]
41+
csgoptix["CSGOptiX<br/>OptiX module<br/>GAS/IAS/SBT<br/>launch control"]
42+
end
43+
44+
subgraph Outputs["Outputs"]
45+
direction TB
46+
persisted["NumPy arrays<br/>hit.npy<br/>photon.npy<br/>record.npy<br/>seq.npy"]
47+
analysis["analysis with optiphy tools"]
48+
end
49+
50+
userinputs --> appcode
51+
appcode --> u4
52+
u4 --> g4cx
53+
u4 --> sysrap
54+
u4 --> csg
55+
sysrap --> csg
56+
sysrap --> qudarap
57+
csg --> csgoptix
58+
qudarap --> csgoptix
59+
g4cx --> csgoptix
60+
61+
csgoptix --> persisted
62+
persisted --> analysis
63+
```
64+
65+
### Build targets and packages
66+
67+
The top-level CMake project is `simphony` and builds these core libraries in
68+
dependency order:
69+
70+
| Layer | CMake target | Directory | Main role |
71+
|---|---|---|---|
72+
| System utilities | `SysRap` | `sysrap/` | `NP`/`NPFold`, `SEvt`, `SSim`, logging, config, photon and genstep types |
73+
| CSG geometry | `CSG` | `CSG/` | `CSGFoundry` and CSG primitive/intersection data |
74+
| CUDA simulation support | `QUDARap` | `qudarap/` | `QSim`, `QEvt`, RNG, and optical physics tables |
75+
| OptiX runtime | `CSGOptiX` | `CSGOptiX/` | OptiX PTX/module setup, acceleration structures, SBT, and launches |
76+
| Geant4 utilities | `U4` | `u4/` | Geant4 material/surface/geometry translation and recorder support |
77+
| G4-to-GPU bridge | `G4CX` | `g4cx/` | `G4CXOpticks` geometry setup, GPU simulation, and reset/finalization |
78+
79+
The installed application targets from `src/` are:
80+
81+
| Target | Purpose |
82+
|---|---|
83+
| `simg4ox` | General Geant4 application that loads GDML, reads a torch JSON config, runs Geant4, and launches GPU simulation at event end |
84+
| `GPUCerenkov` | Cerenkov-focused example using charged-particle genstep collection |
85+
| `GPURaytrace` | Cerenkov plus scintillation example with GPU ray tracing |
86+
| `GPUPhotonSource` | Torch-generated optical photons, with Geant4 and GPU tracking side by side |
87+
| `GPUPhotonSourceMinimal` | Torch-generated optical photons with GPU-only photon transport |
88+
| `GPUPhotonFileSource` | User-provided photon text file with GPU-only photon transport |
89+
| `consgeo` | GDML-to-CSG conversion helper |
90+
| `simtox` | Torch photon generator that writes `out/photons.npy` |
91+
92+
`simphony_argparse` and `simphony_g4_deps` are interface targets used to share
93+
third-party argparse headers and common Geant4/G4CX link dependencies. The
94+
standalone `examples/` tree contains small consumer examples that use an
95+
installed `simphony` package; it is not added by the top-level `CMakeLists.txt`.
96+
97+
### Runtime data flow
98+
99+
There are two main runtime input modes:
100+
101+
- **Charged-particle driven:** Geant4 tracks charged particles, Cerenkov and/or
102+
scintillation gensteps are collected into `SEvt`, and the GPU generates and
103+
propagates optical photons from those gensteps.
104+
- **Direct photon input:** the application injects an `NP` array of photons into
105+
`SEvt` with `SetInputPhoton`, using photons generated from a torch JSON config
106+
or parsed from a user-provided photon file.
107+
108+
```mermaid
109+
%%{init: {
110+
"flowchart": {
111+
"nodeSpacing": 12,
112+
"rankSpacing": 34,
113+
"htmlLabels": true
114+
}
115+
}}%%
116+
117+
flowchart TB
118+
subgraph Geometry["Geometry setup, once"]
119+
direction TB
120+
gdml2["GDML file"]
121+
det["DetectorConstruction<br/>G4GDMLParser"]
122+
sens["ConstructSDandField<br/>attach SensDet<br/>logical volumes"]
123+
setgeo["G4CXOpticks::SetGeometry(world)"]
124+
u4tree["U4Tree<br/>builds stree in SSim"]
125+
foundry["CSGFoundry<br/>CreateFromSim"]
126+
create["CSGOptiX::Create<br/>upload geometry<br/>build GAS/IAS/SBT<br/>load PTX"]
127+
end
128+
129+
subgraph Event["Per-event work"]
130+
direction TB
131+
begin["SEvt::beginOfEvent"]
132+
eventinput["event input mode<br/>Geant4 tracking to gensteps<br/>or direct photons to SetInputPhoton"]
133+
evtend["SEvt gather<br/>endOfEvent"]
134+
simulate["G4CXOpticks::simulate(eventID, false)"]
135+
qsim["QSim<br/>generate and propagate"]
136+
launch["OptiX launch<br/>CSG intersections"]
137+
hits["GPU hits<br/>and event arrays"]
138+
reset["G4CXOpticks::reset(eventID)"]
139+
end
140+
141+
subgraph Persist["Persist and inspect"]
142+
direction TB
143+
eventout["opticks_hits_output.txt<br/>g4_hits_output.txt<br/>or o_hits.npy/g_hits.npy<br/><br/>/tmp/$USER/opticks/GEOM/...<br/>hit.npy<br/>photon.npy<br/>record.npy<br/>seq.npy"]
144+
py["optiphy/ana<br/>optiphy/tools<br/>tests"]
145+
end
146+
147+
gdml2 --> det --> sens --> setgeo --> u4tree --> foundry --> create
148+
create --> simulate
149+
150+
begin --> eventinput
151+
eventinput --> evtend
152+
evtend --> simulate
153+
simulate --> qsim --> launch --> hits --> reset
154+
155+
hits --> eventout
156+
eventout --> py
157+
```
158+
159+
### OptiX pipeline and extension points
160+
161+
The fixed OptiX programs are built from `CSGOptiX/CSGOptiX7.cu` into a PTX
162+
object during the CMake build and installed with the libraries. User code
163+
usually changes scene content, photon/genstep inputs, or Geant4 actions rather
164+
than editing the OptiX programs directly.
165+
166+
```mermaid
167+
%%{init: {
168+
"flowchart": {
169+
"nodeSpacing": 12,
170+
"rankSpacing": 34,
171+
"htmlLabels": true
172+
}
173+
}}%%
174+
175+
flowchart TB
176+
subgraph User["User-controlled inputs"]
177+
direction TB
178+
usercontrolled["GDML scene<br/>optical properties<br/>sensor tags<br/><br/>gensteps<br/>or input photons<br/><br/>Geant4 app logic<br/>primary generator<br/>event/stepping/tracking actions"]
179+
end
180+
181+
subgraph Launch["Launch preparation"]
182+
direction TB
183+
launchdata["SEvt/QEvt<br/>event buffers<br/><br/>CSGFoundry<br/>geometry buffers"]
184+
sbt["CSGOptiX<br/>module<br/>pipeline<br/>SBT"]
185+
end
186+
187+
subgraph Programs["CSGOptiX7.cu programs"]
188+
direction TB
189+
rg["__raygen__rg<br/>render<br/>simtrace<br/>simulate"]
190+
simrg["simulate path<br/>qsim generate_photon<br/>qsim propagate"]
191+
trace["optixTrace"]
192+
shaderprograms["__intersection__is<br/>CSG primitive tests<br/><br/>__closesthit__ch<br/>boundary payload<br/><br/>__miss__ms<br/>miss payload"]
193+
end
194+
195+
subgraph Extend["Lower-level extension points"]
196+
direction TB
197+
csgext["CSG/<br/>new primitives<br/>import rules"]
198+
physext["qudarap/<br/>transport physics<br/>optical models"]
199+
u4ext["u4/<br/>Geant4 mapping<br/>materials<br/>surfaces<br/>sensors"]
200+
end
201+
202+
usercontrolled --> launchdata
203+
launchdata --> sbt
204+
sbt --> rg --> simrg --> trace
205+
trace --> shaderprograms
206+
csgext -.-> shaderprograms
207+
physext -.-> simrg
208+
u4ext -.-> launchdata
209+
```
210+
211+
### What users typically provide
212+
213+
| You provide | Where it enters | Purpose |
214+
|---|---|---|
215+
| GDML geometry with material and surface properties, plus `SensDet` aux tags when needed | `DetectorConstruction` -> `G4CXOpticks::SetGeometry` | Defines the world, optical media, and sensitive surfaces |
216+
| Geant4 macro | Geant4 run manager | Controls threads, `/run/beamOn`, visualization, and G4 optical-photon tracking settings |
217+
| Geant4 user actions in `src/` | Executable-specific app headers | Defines primary generation, event flow, optional G4 reference tracking, and output writing |
218+
| Torch JSON config in `config/` | `generate_photons` -> `SEvt::SetInputPhoton` | Direct optical photon injection without charged primaries |
219+
| Photon text file | `GPUPhotonFileSource` -> `SEvt::SetInputPhoton` | Replay or externally generated photon distributions |
220+
| DD4hep detector/application configuration, when enabled | `dd4hepplugins/` actions | Integrates Simphony GPU optical transport into DD4hep/DDG4 workflows |
221+
222+
### Where to start in the tree
223+
224+
- Start in `src/` if you are adding or changing an installed executable, input
225+
mode, Geant4 primary generator, or Geant4 action.
226+
- Use `config/` for torch-source JSON presets and `tests/geom/` for GDML test
227+
geometries.
228+
- Move to `g4cx/` and `u4/` when changing Geant4 integration, geometry
229+
translation, materials, surfaces, sensitive-detector mapping, or event
230+
recording.
231+
- Move to `CSG/` when adding a CSG primitive, changing CSG import, or debugging
232+
geometry intersections.
233+
- Move to `qudarap/` and `CSGOptiX/` only when extending GPU transport physics,
234+
event buffers, OptiX launches, or OptiX device programs.
235+
- Use `dd4hepplugins/` for DD4hep/DDG4 action plugin work.
236+
- Use `optiphy/ana/`, `optiphy/tools/`, and `tests/` to inspect outputs and
237+
validate behavior.

0 commit comments

Comments
 (0)