Geant4 crashes when loading gdml file (again)

(Accidentally deleted previous identical topic)

Geant4 Version: 11.3.0
Operating System: MacOS 14.0 (ARM64 M2 chip)
Compiler/Version: Apple clang version 15.0.0 (clang-1500.0.40.1)
CMake Version: 3.27.4


I’m having issues loading a gdml file into geant4. I am using a modified version of the ExampleB4a, where I edit the B4DetectorConstruction.cc file to add a gdml geometry with the following code snippet:

// Load GDML file
    G4GDMLParser parser;
    parser.Read("{my_sim_directory}/src/block.gdml", true); // false disables validation

    const G4LogicalVolumeStore* volumeStore = G4LogicalVolumeStore::GetInstance();
    for (const auto& volume : *volumeStore) {
            G4cout << "Found GDML volume: " << volume->GetName() << G4endl;
    }

Where I have the build, include, etc. directories in {my_sim_directory}. I want to load a more complicated gdml file terrain.gdml, but it gives a segmentation fault even with block.gdml, which I copied from the G01 example on loading .gdml files. The G01 example works for me, visualizing both the block.gdml and terrain.gdml files with Qt.

After making and running the exampleB4a executable, I get this output (the first lines are from the end of printing the material table)

   --->  Element: Magnesium (Mg)   Z = 12.0   N =    24   A = 24.305 g/mole
         --->  Isotope:  Mg24   Z = 12   N =  24   A =  23.99 g/mole   abundance: 78.990 %
         --->  Isotope:  Mg25   Z = 12   N =  25   A =  24.99 g/mole   abundance: 10.000 %
         --->  Isotope:  Mg26   Z = 12   N =  26   A =  25.98 g/mole   abundance: 11.010 %
          ElmMassFraction:   2.09 %  ElmAbundance   1.81 %



G4GDML: Reading '{my_sim_directory}/src/block.gdml'...

 *** Break *** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/Users/odinschneider/osdir/geant4/geant4-v11.3.0/geant4-install/lib/libG4gdml.dylib] G4GDMLRead::Read(G4String const&, bool, bool, bool) (no debug info)
[{my_sim_directory}/build02/exampleB4a] B4DetectorConstruction::DefineVolumes() (no debug info)
[/Users/odinschneider/osdir/geant4/geant4-v11.3.0/geant4-install/lib/libG4run.dylib] G4RunManager::InitializeGeometry() (no debug info)
[/Users/odinschneider/osdir/geant4/geant4-v11.3.0/geant4-install/lib/libG4run.dylib] G4RunManager::Initialize() (no debug info)
[{my_sim_directory}/build02/exampleB4a] main (no debug info)
[/usr/lib/dyld] start (no debug info)

Since I used block.gdml, I don’t think this is an issue with the gdml file. Here is my B4DetectorConstruction.cc file. It is largely supposed to describe a burial site, where terrain.gdml would import the burial site and then a chamber is placed inside and a detector (camera) is put on the hill. I can’t attach the gdml files here due to their file format. If you could help me with this segmentation violation, that would be great. If more information is needed, please let me know :slight_smile:
B4DetectorConstruction.cc (21.3 KB)

update: I retried this with both geant4 versions 11.2.2 and 11.1.3 (instead of 11.3.0) and this lead to the same issue.

Have you looked at examples/extended/persistency/gdml? There might be some ways of doing things to be learnt there?

Sorry, see you have already looked.

Workaround:

I created a header file TessellatedVertices.hh that defines all the vertices from the gdml file in “Geant4” syntax. Then TessellatedGeometry.hh describes how these are arranged and TessellatedGeometry.cc can be used to implement the geometry. This is probably a lot slower than using gdml, but works without any bugs.
TessellatedGeometry.cc (2.2 KB)

First few lines of TessellatedVertices.hh:

#ifndef TESSELLATED_VERTICES_HH
#define TESSELLATED_VERTICES_HH

#include "G4ThreeVector.hh"
#include "G4SystemOfUnits.hh"

// Vertex definitions extracted from GDML
const G4ThreeVector terrain_no_mound_v1_0(-61659.490726*mm, 42702.001352*mm, -6962.132226*mm);
const G4ThreeVector terrain_no_mound_v1_1(-61692.289619*mm, 42654.604871*mm, -6964.485428*mm);
const G4ThreeVector terrain_no_mound_v1_2(-61626.655418*mm, 42654.604871*mm, -6951.477603*mm);
const G4ThreeVector terrain_no_mound_v1_3(-61643.077623*mm, 42725.690138*mm, -6960.952091*mm);
const G4ThreeVector terrain_no_mound_v1_4(-61626.655418*mm, 42749.372614*mm, -6959.769600*mm);
const G4ThreeVector terrain_no_mound_v1_5(-61728.695890*mm, 42601.904180*mm, -6966.390527*mm);

TessellatedGeometry.hh:


#ifndef TESSELLATED_GEOMETRY_HH
#define TESSELLATED_GEOMETRY_HH

#include "G4TessellatedSolid.hh"
#include "G4TriangularFacet.hh"
#include "G4SystemOfUnits.hh"
#include "TessellatedVertices.hh"

/*
  This header declares an inline function that creates tessellated solids
  from the GDML facet definitions. It creates the following solids:
    - terrain_no_mound_v1
    - mound_v0
*/

inline void DefineTessellatedSolids(G4TessellatedSolid*& terrainSolid, G4TessellatedSolid*& moundSolid) {
    terrainSolid = new G4TessellatedSolid("terrain_no_mound_v1");
    terrainSolid->AddFacet(new G4TriangularFacet(terrain_no_mound_v1_0, terrain_no_mound_v1_1, terrain_no_mound_v1_2, ABSOLUTE));
    terrainSolid->AddFacet(new G4TriangularFacet(terrain_no_mound_v1_3, terrain_no_mound_v1_0, terrain_no_mound_v1_2, ABSOLUTE));
    terrainSolid->AddFacet(new G4TriangularFacet(terrain_no_mound_v1_4, terrain_no_mound_v1_3, terrain_no_mound_v1_2, ABSOLUTE));
    terrainSolid->AddFacet(new G4TriangularFacet(terrain_no_mound_v1_2, terrain_no_mound_v1_1, terrain_no_mound_v1_5, ABSOLUTE));
    terrainSolid->AddFacet(new G4TriangularFacet(terrain_no_mound_v1_2, terrain_no_mound_v1_5, terrain_no_mound_v1_6, ABSOLUTE));
    terrainSolid->AddFacet(new G4TriangularFacet(terrain_no_mound_v1_7, terrain_no_mound_v1_2, terrain_no_mound_v1_8, ABSOLUTE));
    terrainSolid->AddFacet(new G4TriangularFacet(terrain_no_mound_v1_8, terrain_no_mound_v1_2, terrain_no_mound_v1_6, ABSOLUTE));
    terrainSolid->AddFacet(new G4TriangularFacet(terrain_no_mound_v1_9, terrain_no_mound_v1_4, terrain_no_mound_v1_2, ABSOLUTE));
    terrainSolid->AddFacet(new G4TriangularFacet(terrain_no_mound_v1_10, terrain_no_mound_v1_11, terrain_no_mound_v1_12, ABSOLUTE));

and then implemented with TessellatedGeometry.cc:

#include "TessellatedVertices.hh"   // Contains all vertex definitions
#include "TessellatedGeometry.hh"   // Declares DefineTessellatedSolids()
#include "G4TessellatedSolid.hh"
#include "G4TriangularFacet.hh"
#include "G4LogicalVolume.hh"
#include "G4NistManager.hh"
#include "G4PVPlacement.hh"
#include "G4Box.hh"
#include "G4VPhysicalVolume.hh"
#include "G4SystemOfUnits.hh"

//
// CreateTessellatedGeometry() builds the tessellated solids (terrain and mound),
// creates logical volumes for them using the materials (Rock1 and Rock2),
// places them into a world volume (using G4_AIR) and returns the physical world.
//
G4VPhysicalVolume* CreateTessellatedGeometry() {
    // Get materials from the NIST manager
    G4NistManager* nist = G4NistManager::Instance();
    G4Material* rock1 = nist->FindOrBuildMaterial("Rock1");
    G4Material* rock2 = nist->FindOrBuildMaterial("Rock2");
    G4Material* air   = nist->FindOrBuildMaterial("G4_AIR");

    // Create the tessellated solids using the inline function declared in TessellatedGeometry.hh
    G4TessellatedSolid* terrainSolid = nullptr;
    G4TessellatedSolid* moundSolid   = nullptr;
    DefineTessellatedSolids(terrainSolid, moundSolid);

    // Create logical volumes for the tessellated solids.
    // (Assumes that the GDML assigned terrain to Rock1 and mound to Rock2)
    G4LogicalVolume* terrainLV = new G4LogicalVolume(terrainSolid, rock1, "TerrainLV");
    G4LogicalVolume* moundLV   = new G4LogicalVolume(moundSolid, rock2, "MoundLV");

    // Create a world volume.
    // Here we use a G4Box with a large size (adjust as necessary).
    G4Box* worldSolid = new G4Box("WorldBox", 150 * m, 150 * m, 150 * m);
    G4LogicalVolume* worldLV = new G4LogicalVolume(worldSolid, air, "WorldLV");

    // Place the terrain and mound logical volumes in the world volume.
    // Since the vertex positions are defined absolutely, we place both at (0,0,0)
    new G4PVPlacement(nullptr, G4ThreeVector(0, 0, 0), terrainLV, "Terrain", worldLV, false, 0, true);
    new G4PVPlacement(nullptr, G4ThreeVector(0, 0, 0), moundLV, "Mound", worldLV, false, 0, true);

    // Return the physical world volume.
    return new G4PVPlacement(nullptr, G4ThreeVector(0, 0, 0), worldLV, "World", nullptr, false, 0, true);
}

Hope this helps for anyone else facing troubles with gdml and implementing geometries. If there are updates on the gdml crashing, I’d be happy to hear about it for the future.

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