Navigation
This example closes a geometry into a flat GeometryModel, follows rays through it with the navigator, and makes X-ray images of it. The same navigator runs on GPUs.
You can also download this example as a Jupyter notebook and a plain Julia source file.
Table of contents
- Loading the modules
- A geometry
- Closing the geometry
- Locating points
- Following a ray
- X-ray images
- Material budget
- Single precision and GPUs
Loading the modules
using Geom4hep, Geom4hep.Units
using CairoMakie: CairoMakie, Figure, Axis, heatmap!, lines!, ColorbarA geometry
A box of water with a lead ball inside, a steel shell around it, and a hole along z. The materials are taken from the NIST data of Geant4.
function build(T)
world = Volume{T}("world", Box{T}(50cm, 50cm, 50cm), nist_material(T, "G4_AIR"))
shell = Volume{T}("shell", Sphere{T}(36cm, 38cm), nist_material(T, "G4_STAINLESS-STEEL"))
tank = Volume{T}("tank", BooleanSubtraction(Box{T}(20cm, 20cm, 20cm), Tube{T}(0, 3cm, 25cm, 0, 2π)),
nist_material(T, "G4_WATER"))
ball = Volume{T}("ball", Sphere{T}(0, 8cm), nist_material(T, "G4_Pb"))
place_daughter!(tank, Transformation3D{T}(12cm, 0, 0), ball)
place_daughter!(world, one(Transformation3D{T}), shell)
place_daughter!(world, one(Transformation3D{T}), tank)
return world
end
world = build(Float64);Closing the geometry
close_geometry converts the hierarchy into a GeometryModel: flat arrays of plain structures, shapes called without dynamic dispatch and boolean shapes as CSG programs. With with_index=true it also returns the Volume and Material of each index of the model, to get their names.
model, index = close_geometry(world; with_index=true)
modelGeometryModel{Float64}(volumes=4, placements=3, materials=4, shape kinds=4, shapes=6, bvh nodes=0, csg components=2)Locating points
locate gives the navigation state of a point: the deepest volume that contains it, and the path of placements to it.
name(state) = in_world(state) ? index.volumes[state.volume].label : "outside"
[name(locate(model, Point3(x, 0.0, 0.0))) for x in (0.0, 12cm, 25cm, 37cm, 45cm, 60cm)]6-element Vector{String}:
"world"
"ball"
"world"
"shell"
"world"
"outside"Following a ray
compute_step gives the distance to the next boundary along a direction, and the state after crossing it. Repeating it until the ray leaves the world gives the volumes it crosses.
function follow(model, point, dir)
state = locate(model, point)
crossings = []
while in_world(state)
step, next = compute_step(model, state, point, dir, Inf)
push!(crossings, (volume=name(state), step=step))
point += step * dir
state = next
end
return crossings
end
follow(model, Point3(-49cm, 1cm, 0.0), Vector3(1.0, 0.0, 0.0))11-element Vector{Any}:
(volume = "world", step = 110.13160173554844)
(volume = "shell", step = 20.007313955519948)
(volume = "world", step = 159.8610843089316)
(volume = "tank", step = 171.7157287525381)
(volume = "world", step = 56.56854249492381)
(volume = "tank", step = 12.343189420600378)
(volume = "ball", step = 158.74507866387543)
(volume = "tank", step = 0.6274606680622696)
(volume = "world", step = 159.86108430893162)
(volume = "shell", step = 20.007313955520004)
(volume = "world", step = 120.13160173554843)Along z the ray goes through the hole of the tank:
follow(model, Point3(0.0, 0.0, -49cm), Vector3(0.0, 0.0, 1.0))5-element Vector{Any}:
(volume = "world", step = 110.0)
(volume = "shell", step = 20.0)
(volume = "world", step = 720.0)
(volume = "shell", step = 20.0)
(volume = "world", step = 120.0)X-ray images
xray integrates the density along parallel rays (the mass thickness), on the CPU with Julia threads or on a GPU. Here the rays are parallel to the z axis (axis 3) and to the x axis (axis 1).
u, v, image, nsteps = xray(model, 3, 300)
fig = Figure(size=(900, 400))
ax = Axis(fig[1, 1], aspect=1, title="along z", xlabel="x (mm)", ylabel="y (mm)")
hm = heatmap!(ax, u, v, image, colormap=:grays)
u, v, image, nsteps = xray(model, 1, 300)
ax = Axis(fig[1, 2], aspect=1, title="along x", xlabel="y (mm)", ylabel="z (mm)")
heatmap!(ax, u, v, image, colormap=:grays)
Colorbar(fig[1, 3], hm, label="mass thickness (g/cm³ × mm)")
figMaterial budget
With quantity=:radlen or :nuclen, xray integrates the length in units of the radiation length X0 or of the nuclear interaction length λ of the materials: the material budget seen by particles crossing the detector. The lead ball is many radiation lengths thick, but less than one interaction length.
fig = Figure(size=(900, 400))
for (k, (q, label)) in enumerate(((:radlen, "x/X0"), (:nuclen, "x/λ")))
u, v, budget, _ = xray(model, 3, 300; quantity=q)
ax = Axis(fig[1, 2k-1], aspect=1, title="$label along z", xlabel="x (mm)", ylabel="y (mm)")
hm = heatmap!(ax, u, v, budget, colormap=:viridis)
Colorbar(fig[1, 2k], hm, label=label)
end
figSingle precision and GPUs
The same geometry in single precision gives the same results within the precision of Float32.
model32 = close_geometry(build(Float32))
u, v, image32, _ = xray(model32, 1, 300)
maximum(abs, image32 - image) / maximum(image)1.8529423483488302e-5To run on a GPU, load its KernelAbstractions backend and give it to xray; the model is copied to the GPU (Apple GPUs need Float32):
using Metal
u, v, image, nsteps = xray(model32, 1, 1000; backend=MetalBackend())The navigator functions (locate, compute_step) can also be called from your own kernels: the model is a plain isbits structure once adapted to the GPU with Adapt.adapt(backend, model).
This page was generated using Literate.jl.