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));