Unable to Emit All Particles; Program Freezes During Execution

Please fill out the following information to help in answering your question, and also see tips for posting code snippets. If you don’t provide this information it will take more time to help with your problem!

_Geant4 Version:_10.7.3
_Operating System:_Ubuntu 18
Compiler/Version:
CMake Version:
I am trying to create a scintillator detector. I have constructed a hexagonal prism cavity and placed triangular prism plastic scintillators inside it to form an array. Between the scintillators is water. I used Boolean operations with the hexagonal prism and the array of triangular prisms to create the physical volume of water.

I want to randomly generate particles within a cylinder with a radius equal to the side length of the hexagonal prism, then check if the particle is in the water. If it is, I emit it.

I tried increasing the thickness of the water layer and decreasing the size of the triangular prisms—which should make it easier for particles to be successfully generated—but this only delays the freezing of the program. For example, with a thinner water layer, the program freezes after emitting 34 particles; with a thicker water layer, it freezes after 63 particles.

I also tried removing all file streams and not outputting any files, so it doesn’t seem to be an issue with file I/O.

I found that if I remove the code that checks whether the particle is in the water body, the program runs smoothly. Additionally, even if I don’t perform the Boolean operations (the water body is just a hexagonal prism), and I generate particles within the cylinder and check whether they are in the water, the program still freezes after several emits.

Here is my code:

bool L1PrimaryGeneratorAction::IsInsideWater(G4VPhysicalVolume* volume) {
    
    if (volume == nullptr) {
        G4cerr << "Error: Volume is null!" << G4endl;
        return false;
    }
    if (fpWater == nullptr) {
        G4cerr << "Error: fpwater is null!" << G4endl;
       return false;
    }
void L1PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent) {
     G4ThreeVector pos;
    G4double x, y, z;
    fpWater = fDetConstruction->Getpwater(); 
    fTime = event * (fRunAction->GetTimeInterval());
    event++;
    int i = 0;
  G4cout << "Thread ID: " << G4Threading::G4GetThreadId() 
           << ", Event: " << event << G4endl;

G4VPhysicalVolume* volume = nullptr;  
G4Navigator* theNavigator = G4TransportationManager::GetTransportationManager()
                             ->GetNavigatorForTracking();
do {
    G4double r = (fDetConstruction->Getroutsize()) * std::sqrt(G4UniformRand());
    G4double theta = 2 * M_PI * G4UniformRand();
    x = r * std::cos(theta);
    y = r * std::sin(theta);
    z = (G4UniformRand() - 0.5) * 100 * CLHEP::mm;
    pos = G4ThreeVector(x, y, z);
    i++;

     if (i % 100 == 0) {
        G4cout << "Attempt #" << i << ": Position = (" 
               << x << ", " << y << ", " << z << ")" << G4endl;
    }
    if (i >= maxAttempts) {
        G4cerr << "Warning: Max attempts reached for event " << event 
               << ". Exiting loop." << G4endl;
        break;
    }

volume = theNavigator->LocateGlobalPointAndSetup(pos);

    if (volume == nullptr) {
        G4cerr << "Error: Could not locate volume for position (" 
               << x << ", " << y << ", " << z << ")." << G4endl;
        continue; 
    }

} while (!IsInsideWater(volume));  

I suspect there might be some slight overlap related to rounding/numerical errors at the edges. Particles can get lost or “stuck” or enter undefined behavior if they encounter voids or overlaps.

First thing to do is to run geometry checking to make sure there are no overlaps. In UI mode or in a macro, you can do this with geometry/test/run and sample more points with geometry/test/resolution.

Second, you should be using mother-daughter volume placement to avoid such issues. Define your big hexagonal volume as water then place each of the triangular prisms (with whatever materials) as daughters of that mother volume.

2 Likes

That’s Right!I have checked the overlaps and there are no overlaps.
The second suggestion works! I ‘define big hexagonal volume as water(named lwater) then place each of the triangular prisms (with whatever materials) as daughters of that mother volume.’,then I created a new logical volume (only after Boolean operation,can I define the new volume,named l1water) and set it as the true logical volume of water.The freeze disappers.
You solved the problem that had been troubling me for two days. Thank you very much!

Glad that worked. I don’t think you need the boolean at all. When you place a daughter volume in a mother volume it already takes care of making the two volumes mutually exclusive in regards to materials placed in the world. But good luck!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.