Good afternoon,
I am working on a simulation setup inspired by example B1. It consists of a 3x3 matrix of cells.
Each cell is made of two materials: epoxy and LYSO crystals. The LYSO crystals are inserted into a single cell in the form of spheres, with random positions generated inside the cell.
After generating the spheres, this “main cell with spheres” is replicated to form the full 3x3 matrix of identical cells.
This setup is working, and I attach part of the DetectorConstruction.cc code below:
// --- DIANADetectorConstruction snippet: full sphere generation and cell replicas ---
G4double cell_sizeXY = 2.*cm;
G4double cell_sizeZ = 20.*cm;
G4Material* dianaPassiveMaterial = epoxy; // matrix material
G4Material* sphereMat = LYSO; // sphere material
G4double sphereRadius = 0.5*mm;
G4double DMIN = 2*sphereRadius;
G4int NX = std::max(1, int(cell_sizeXY / (2.0 * sphereRadius)));
G4int NY = std::max(1, int(cell_sizeXY / (2.0 * sphereRadius)));
G4int NZ = std::max(1, int(cell_sizeZ / (2.0 * sphereRadius)));
// compute number of spheres based on concentration and volumes
size_t numSpheres = size_t((((fragmentsConcentration*dianaPassiveMaterial->GetDensity())
/(sphereMat->GetDensity()*(1-fragmentsConcentration)
+ dianaPassiveMaterial->GetDensity()*fragmentsConcentration))
*(cell_sizeXY*cell_sizeXY*cell_sizeZ))
/((4./3.)*CLHEP::pi*std::pow(sphereRadius,3)));
// --- Create the cell ---
G4Box* box = new G4Box("Cell", 0.5*cell_sizeXY, 0.5*cell_sizeXY, 0.5*cell_sizeZ);
G4LogicalVolume* box_logical = new G4LogicalVolume(box, dianaPassiveMaterial, "DIANACell");
// --- Prepare sphere generation ---
auto sphere = new G4Orb("Sphere", sphereRadius);
std::vector<G4LogicalVolume*> sensitiveVolumes;
std::vector<G4ThreeVector> placedSpheres;
std::random_device rd;
std::mt19937 gen(rd());
// helper to map 3D indices to 1D
auto index = [&](int ix,int iy,int iz){ return ix + NX*(iy + NY*iz); };
G4int nspheres = 0;
G4ThreeVector bmin, bmax;
box->BoundingLimits(bmin, bmax); // get bounding box
G4double delx = (bmax.x() - bmin.x()) / NX;
G4double dely = (bmax.y() - bmin.y()) / NY;
G4double delz = (bmax.z() - bmin.z()) / NZ;
while (nspheres < numSpheres) {
// generate random point inside the box
G4double x = bmin.x() + (bmax.x() - bmin.x())*G4UniformRand();
G4double y = bmin.y() + (bmax.y() - bmin.y())*G4UniformRand();
G4double z = bmin.z() + (bmax.z() - bmin.z())*G4UniformRand();
G4ThreeVector p(x,y,z);
if (box->Inside(p) != kInside) continue; // skip if outside
if (box->DistanceToOut(p) < 0.5*DMIN) continue; // too close to edge
// determine which spatial cell the point falls in
G4int ix = std::min(NX-1, std::max(0,int((x-bmin.x())/delx)));
G4int iy = std::min(NY-1, std::max(0,int((y-bmin.y())/dely)));
G4int iz = std::min(NZ-1, std::max(0,int((z-bmin.z())/delz)));
// neighbor cells for overlap checking
G4int iprev_x = (ix==0)? ix : ix-1;
G4int inext_x = (ix==NX-1)? ix : ix+1;
G4int iprev_y = (iy==0)? iy : iy-1;
G4int inext_y = (iy==NY-1)? iy : iy+1;
G4int iprev_z = (iz==0)? iz : iz-1;
G4int inext_z = (iz==NZ-1)? iz : iz+1;
G4bool accept = true;
for (G4int i=iprev_x; i<=inext_x && accept; ++i)
for (G4int j=iprev_y; j<=inext_y && accept; ++j)
for (G4int k=iprev_z; k<=inext_z && accept; ++k)
for (const auto& pos : positions[index(i,j,k)])
if ((pos-p).mag() < DMIN) { accept=false; break; }
if (!accept) continue;
// accept the point and place the sphere
nspheres++;
if (nspheres % 5000 == 0) G4cout << "nspheres = " << nspheres << G4endl;
positions[index(ix,iy,iz)].push_back(p);
auto sph_logical = new G4LogicalVolume(sphere, sphereMat, "Sphere");
new G4PVPlacement(0, p, sph_logical, "Sphere", box_logical, false, 0, true);
// optionally add to sensitive volumes
if (!doIswapMaterials) sensitiveVolumes.push_back(sph_logical);
// --- check overlaps explicitly for warning ---
bool overlap = false;
const auto& posList = positions[index(ix,iy,iz)];
if (posList.size()>1) {
const auto& p1 = posList.back();
for (size_t i=0; i<posList.size()-1; ++i) {
if ((p1-posList[i]).mag() < 2*sphereRadius) {
G4cout << Overlap detected in cell (" << ix << "," << iy << "," << iz << ")" << G4endl;
overlap = true;
break;
}
}
}
if (overlap) {
G4cout << "Stopping generation after " << nspheres << " spheres due to overlap." << G4endl;
break;
}
}
// --- Replica cells in X and Y ---
G4int nRep = 3;
auto logicReplicaX = new G4LogicalVolume(
new G4Box("ReplicaX", 0.5*cell_sizeXY*nRep, 0.5*cell_sizeXY, 0.5*cell_sizeZ),
world_mat, "ReplicaX"
);
new G4PVReplica("Cell_X", box_logical, logicReplicaX, kXAxis, nRep, cell_sizeXY);
auto logicReplicaY = new G4LogicalVolume(
new G4Box("ReplicaY", 0.5*cell_sizeXY*nRep, 0.5*cell_sizeXY*nRep, 0.5*cell_sizeZ),
world_mat, "ReplicaY"
);
new G4PVReplica("Cell_Y", logicReplicaX, logicReplicaY, kYAxis, nRep, cell_sizeXY);
// place the whole matrix in the world
new G4PVPlacement(0, G4ThreeVector(), logicReplicaY, "DIANA_matrix", logicWorld, false, 0, true);
Now, in addition to what written above, I would like to add to my setup a lead brick
// --------------------------------------------
// Aggiunta blocco di piombo davanti alla matrice
// --------------------------------------------
// Parametri del blocco
G4double leadX = 3 * cell_sizeXY; // stessa larghezza della matrice
G4double leadY = 3 * cell_sizeXY;
G4double leadZ = 1.0 * cm; // spessore 1 cm (modifica tu)
// Gap per evitare overlap
G4double gap = 1*mm;
// Materiale
G4Material* lead = nist->FindOrBuildMaterial("G4_Pb");
// Solido
auto solidLead = new G4Box("LeadBlock",
0.5 * leadX,
0.5 * leadY,
0.5 * leadZ);
// Volume logico
auto logicLead = new G4LogicalVolume(solidLead, lead, "LeadBlock");
// Posizione: davanti alla matrice 3×3
G4double zPosLead = 0.5 * cell_sizeZ + 0.5 * leadZ + gap;
// Posizionamento
new G4PVPlacement(
0,
G4ThreeVector(0, 0, zPosLead),
logicLead,
"LeadBlock",
logicWorld,
false, 0, true
);
// Visualizzazione
auto visLead = new G4VisAttributes(G4Colour(0.3, 0.3, 0.3, 0.8));
visLead->SetForceSolid(true);
logicLead->SetVisAttributes(visLead);
But, when I do that, I obtain this error:
*** Break *** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4geometry.dylib] G4VPhysicalVolume::GetRotation() (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4geometry.dylib] G4VoxelNavigation::ComputeStep(CLHEP::Hep3Vector const&, CLHEP::Hep3Vector const&, double, double&, G4NavigationHistory&, bool&, CLHEP::Hep3Vector&, bool&, bool&, G4VPhysicalVolume**, int&) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4geometry.dylib] G4Navigator::ComputeStep(CLHEP::Hep3Vector const&, CLHEP::Hep3Vector const&, double, double&) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4processes.dylib] G4Transportation::AlongStepGetPhysicalInteractionLength(G4Track const&, double, double, double&, G4GPILSelection*) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4tracking.dylib] G4SteppingManager::DefinePhysicalStepLength() (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4tracking.dylib] G4SteppingManager::Stepping() (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4tracking.dylib] G4TrackingManager::ProcessOneTrack(G4Track*) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4event.dylib] G4EventManager::DoProcessing(G4Event*, std::__1::vector<G4Track*, std::__1::allocator<G4Track*>>*, bool) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4run.dylib] G4RunManager::ProcessOneEvent(int) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4run.dylib] G4RunManager::DoEventLoop(int, char const*, int) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4run.dylib] G4RunManager::BeamOn(int, char const*, int) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4run.dylib] G4RunMessenger::SetNewValue(G4UIcommand*, G4String) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4intercoms.dylib] G4UIcommand::DoIt(G4String const&) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4intercoms.dylib] G4UImanager::ApplyCommand(char const*) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4intercoms.dylib] G4UIbatch::ExecCommand(G4String const&) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4intercoms.dylib] G4UIbatch::SessionStart() (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4intercoms.dylib] G4UImanager::ExecuteMacroFile(char const*) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4intercoms.dylib] G4UIcontrolMessenger::SetNewValue(G4UIcommand*, G4String) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4intercoms.dylib] G4UIcommand::DoIt(G4String const&) (no debug info)
[/Applications/Geant4/geant4-v11.3.0-install/lib/libG4intercoms.dylib] G4UImanager::ApplyCommand(char const*) (no debug info)
[/Users/annamarini/Desktop/Work/INFN/DIANA/Simulazione-coordinate/diana_simulation/build/exampleDIANA] main /Users/annamarini/Desktop/Work/INFN/DIANA/Simulazione-coordinate/diana_simulation/diana-simu/exampleDIANA.cc:120
[/usr/lib/dyld] start (no debug info)
WARNING - Attempt to delete the physical volume store while geometry closed !
WARNING - Attempt to delete the logical volume store while geometry closed !
WARNING - Attempt to delete the solid store while geometry closed !
WARNING - Attempt to delete the region store while geometry closed !
If I comment the positioning of the spheres, it works again (brick + matrix). I cannot understand what the issue is. Any help is more than welcome!
Geant4 Version: geant4-v11.3.0
Operating System: MacOS 26.1
Compiler/Version: Apple clang version 17.0.0
CMake Version: 4.2.0