G4Subtraction of two solid box - where is the problem in my code below?

Hello,

I have been trying to subtract two solid boxes, one is larger and one is smaller, I want to subtract the smaller box from the larger one. I tried to G4SubtractionSolid class to do this task but even after subtraction, I am seeing both the boxes! Not sure where did I make a mistake. Could anyone point me to where did I make a mistake by looking at the code provided below? Below is a copy-paste of my detectorconstruction.cc file. Thank you for your help in advance.

#include "DetectorConstruction.hh"

DetectorConstruction::DetectorConstruction()  
{;}

DetectorConstruction::~DetectorConstruction() 
{;}

G4VPhysicalVolume* DetectorConstruction::Construct()
{
  //  Material Information imported from NIST database
  G4NistManager* nist = G4NistManager::Instance();
  G4Material* Water = nist->FindOrBuildMaterial("G4_WATER");
  //G4Material* Air   = nist->FindOrBuildMaterial("G4_AIR");

  //Polystyrene (C8H8)n (This is a material used in Cells Wells plate)
  G4Material* Pstyrene = new G4Material("Polystyrene", 1.03*g/cm3, 2);
  Pstyrene->AddElement(nist->FindOrBuildElement("C"), 8);
  Pstyrene->AddElement(nist->FindOrBuildElement("H"), 8);

  // Define a World Volume
  G4VSolid* solidWorld = new G4Box("solidbox", 120*mm,120*mm,120*mm); // half length of sides

  G4LogicalVolume* logicWorld = new G4LogicalVolume(solidWorld, Water, "logicWorld");
  
  G4VPhysicalVolume* physWorld = new G4PVPlacement(0,                     // no rotation of daughter frame wrt mother frame
                                                  G4ThreeVector(),        // position in mother volume at (0,0,0)
                                                  logicWorld,             // logical volume
                                                  "physWorld",            // name
                                                  0,                      // mother volume
                                                  false,                  // no boolean operation, set it to false
                                                  0,                      // copy number, unique arbitary number
                                                  true);                  // optional overlap check                     

  // Define Cell well outer box
  G4VSolid* outerbox = new G4Box("phantombox", 63.88*mm, 7.3*mm, 42.74*mm); //half length of cell well box
  
  G4LogicalVolume* logicCell_well = new G4LogicalVolume(outerbox, Pstyrene, "logicCell_well"); // logical volume - assign properties to objexct material
  
  G4VPhysicalVolume* fPhantomPhysOuter = new G4PVPlacement(0, 
                                        G4ThreeVector(),
                                        logicCell_well, "fPhantomPhysOuter",   // physical voloume - place the created object in the coordinate system of mother volume
                                        logicWorld, false, 0);

 
  // Visualization attributes -- outer box
  //logicWorld->SetVisAttributes(G4VisAttributes::GetInvisible());            // world volume box will dissappear
  // G4VisAttributes* outerBoxVisAtt= new G4VisAttributes(G4Colour(1, 1, 1)); // white volume
  // outerBoxVisAtt-> SetVisibility(true);
  // outerBoxVisAtt-> SetForceSolid(true);
  // logicCell_well-> SetVisAttributes(outerBoxVisAtt);

  // Define inner box
  G4VSolid* innerbox = new G4Box("innerBox", 48.88*mm, 5.09*mm, 32.74*mm);  // 97.76/2, 10.18/2, 65.47/2
  G4LogicalVolume* logicCell_well_inner = new G4LogicalVolume(innerbox, Pstyrene, "logicCell_well_inner");
  G4VPhysicalVolume* fPhantomPhysInner = new G4PVPlacement(0, 
                                                           G4ThreeVector(0., 3.24*mm, 0.),   //14.6 minus 10.18/2
                                                           logicCell_well_inner,
                                                           "fPhantomPhysInner", 
                                                           logicWorld, 
                                                           false, 0);

  // Visualization attributes -- inner box
  // G4VisAttributes* innerBoxVisAtt= new G4VisAttributes(G4Colour(1, 1, 0));
  // innerBoxVisAtt-> SetVisibility(true);
  // innerBoxVisAtt-> SetForceSolid(true);
  // logicCell_well_inner-> SetVisAttributes(innerBoxVisAtt);

  // Subtract inner box from outer box
  G4SubtractionSolid* solidShape = new G4SubtractionSolid("outer-inner",                 
                                                           outerbox, 
                                                           innerbox,
                                                           0,                           // no rotation
                                                           G4ThreeVector(0., 0., 0.));  // no translation

  G4LogicalVolume* logicCellWell = new G4LogicalVolume(solidShape, Pstyrene, "logicCellWell", 0, 0, 0);

  G4VPhysicalVolume* physCellWell = new G4PVPlacement(0, 
                                                      G4ThreeVector(), 
                                                      logicCellWell, 
                                                      "physCellWell",
                                                      logicWorld,   // its mother volume
                                                      false,        // Boolean operation
                                                      0, true);		  // check overlaps	


  // Visualization attributes -- subtracted volume
  G4VisAttributes* subtractedBoxVisAtt= new G4VisAttributes(G4Colour(0.4, 0.4, 0.4));
  subtractedBoxVisAtt-> SetVisibility(true);
  subtractedBoxVisAtt-> SetForceSolid(true);
  logicCellWell-> SetVisAttributes(subtractedBoxVisAtt);



 return physWorld;
}

You have constructed three physical volumes fPhantomPhysOuter, fPhantomPhysInner and physCellWell. The image shows the first and second volumes, the third volumes is not visible, as it is inside the first one.

1 Like

I have commented on the part of the code that was used to visualize the first and second physical volumes but not the third one. So, I am not supposed to visualize the final volume (the third one)? Why am I still seeing the first and second? What is the way to visualize only the final volume (subtracted part) in this code? Apologies if this is a naive question!

It is difficult to say why visualisation does not work as expected. Try to comment first two G4PVPlacement statements and see what happens.

I see only an outer box (first volume) even after commenting out G4PVPlacement statements for the outer box and inner box! Could it be due to the volume overlap? Geant4 also threw the overlap warning!

          ################################
          !!! G4Backtrace is activated !!!
          ################################


**************************************************************
 Geant4 version Name: geant4-11-00-patch-02 [MT]   (25-May-2022)
  << in Multi-threaded mode >> 
                       Copyright : Geant4 Collaboration
                      References : NIM A 506 (2003), 250-303
                                 : IEEE-TNS 53 (2006), 270-278
                                 : NIM A 835 (2016), 186-225
                             WWW : http://geant4.org/
**************************************************************

Checking overlaps for volume physCellWell:0 (G4SubtractionSolid) ... 
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : GeomVol1002
      issued by : G4PVPlacement::CheckOverlaps()
Overlap with volume already placed !
          Overlap is detected for volume physCellWell:0 (G4SubtractionSolid) with fPhantomPhysOuter:0 (G4Box)
          overlap at local point (48.88,0.021463,-6.91598) by 7.27854 mm  (max of 368 cases)
NOTE: Reached maximum fixed number -1- of overlaps reports for this volume !
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

Please check that you really commented fPhantomPhysOuter, it looks that it exists.

Oops, I sent you the wrong info! Apologies but after I commented on the first and second volumes there is no overlap, but still I am seeing the first volume.

Available UI session types: [ Qt, tcsh, csh ]


          ################################
          !!! G4Backtrace is activated !!!
          ################################


**************************************************************
 Geant4 version Name: geant4-11-00-patch-02 [MT]   (25-May-2022)
  << in Multi-threaded mode >> 
                       Copyright : Geant4 Collaboration
                      References : NIM A 506 (2003), 250-303
                                 : IEEE-TNS 53 (2006), 270-278
                                 : NIM A 835 (2016), 186-225
                             WWW : http://geant4.org/
**************************************************************

Checking overlaps for volume physCellWell:0 (G4SubtractionSolid) ... OK! 

It is correct, you see the external surface of the volume. To see the internal surface you should set transparency for the logical volume. Alternatively you can visualise the volume as a cloud of dots:

vis/viewer/set/style cloud

Just looking at your code snippet quickly (I may have misinterpreted it) you are making a subtraction of a smaller box from a larger box with no relative displacement, so the smaller is entirely inside the larger. So in surface mode you simply see the outer. Try

/vis/viewer/set/style wireframe

But why not make the smaller one of air and place it inside the larger using a mother-daughter relationship? That is equivalent to subtraction and more efficient for tracking.

John

1 Like

Dear tiwarias

(1) You don’t have to define 3 separate physical volumes just take two boxes, let boxOUT and boxIN , use G4Substraction to define a subtraction solid, and then define its Logical and Physical Volume.

(2) please correct the boolean operation to ‘true’ while defining physCellWell as you defined a Subtraction solid having a boolean operation.
An example is attached for you.
example.txt (1.8 KB)

1 Like