in our Geant4 application we have a rather complex geometry for the SNOLAB cavern. It is composed of several G4Box and G4Tubs combined via G4Union. Since the documentation notes that the tracking time is proportional to the number of constituent solids, I was looking into switching our implementation to use G4MultiUnion instead since the documentation says “Fast detection of intersections in tracking is assured…”. However, I noticed that the tracking time improvement is almost negligible. In fact, when tracking particles in the mother volume close to the intersection of G4Tubs in the G4MultiUnion, the tracking time is much slower.
Did others encounter similar behaviour? Is this a known effect? I was not able to find anything related in the documentation.
I have put together a minimal example based on example B1. Attached is the modified B1DetectorConstruction.cc where you can switch between G4Union and G4MultiUnion. Additionally, I provide a macro run_union.txt with different gun positions. You need to remove the line fParticleGun->SetParticlePosition(G4ThreeVector(x0,y0,z0)); in B1PrimaryGeneratorAction.cc to be able to sample at these positions.
Tracking 1 million 1 MeV gammas does take the following time:
sampling close to intersection of G4Tubs
Cavern G4MultiUnion: 65 s
Cavern G4Union: 60 s
Envelope G4MultiUnion: 345 s
Envelope G4Union: 64 s
sampling far away
Cavern G4MultiUnion: 68 s
Cavern G4Union: 66 s
Envelope G4MultiUnion: 63 s
Envelope G4Union: 66 s
The efficiency of G4MultiUnion becomes relevant only when many components are involved. In your case, three components, the use of G4UnionSolid is just fine. Also note that G4MultiUnion applies to components which are disjoint in space (can be normal solids or Boolean compositions), with same material.
You have defined the material for Envelope and Cavern as Al. The path length of 1 MeV electrons in Al is very short, they stop almost in the place where they were generated (please note a yellow point at the position (8,8,19) which contains 1000 tracks). This explains why the difference in tracking time is almost negligible in most of cases.
The reason of the slow down in the case where the particle is close to the boundary of G4MultiUnion is probably related with the calculation of so called safety distance. It looks that the current algorithm implemented in G4MultiUnion is not very optimal.
In any case, as Gabriele explained, the G4MultiUnion is not intended for use with a small number of components.
Yes, I intentionally designed the minimal example like this to ensure that all particles stay in the volume in which they were generated, otherwise the benchmark might have been unclear.
You both mention that “the G4MultiUnion is not intended for use with a small number of components” and that “the efficiency of G4MultiUnion becomes relevant only when many components are involved”. Do you have a rough number how many components a solid needs to be composed of, so it makes sense to use G4MultiUnion? Or asked the other way around - for how many components would you still recommend the use of boolean solids such as G4Union?
There is no number provided in the documentation, so it’s up to the user’s interpretation to determine how many is “many”. But maybe this also depends on the type and combination of solids used?
Let me explain how everything worked in your example. First, the Inside(p) function of the volumes was called to determine the initial position of the particle. Then, there was one or two calls to the DistanceToIn(p) or DistanceToOut(p) function to estimate the safety distance to the nearest surface. After that, there was no any call to any of the volumes, since the safety distance was much larger than tracking steps of the 1 MeV electron in Al. The tracker knew that the electron was always within this distance. As a result, tracking time was independent of the Cavern construction method and even the number of components in it.
This may depend on the size of the components, their order, and even the material, as in your example. In general, I would say that up to eight-ten components the use of G4UnionSolid is preferable, but the best way is to make a comparison on real geometry.