Set gps position

Hello,I would like to set the position by setting the name of the volume…I mean that the simulation must get the l volume position then set the particle source in that place.

Searching on the forum, I found this @weller’s post

then I’m trying in the detectoconstruction:

  G4VPhysicalVolume* PhysSample;
PhysSample=new G4PVPlacement(0, G4ThreeVector(0, 0, (-4.55*cm+sampleheight)), sample_log, "sample", Nfilledchamber_log, false, 0);

in the macro

/gps/pos/confine PhysSample


/gps/pos/confine sample

but, by the visualization, it looks like to me that particle source is always at the center

moreover, I also tried to write a fake physical volume name and I didn’t get any error (62.1 KB)

macro.txt (1.4 KB)

Thank you

keep in mind that “confining” literally means "throw away every event that would have been created outside the confining volume. so you are responsible yourself to create an initial distribution of your gps (e.g., a sphere or a cube) that fully contains the confining volume.

in the macro you uploaded, it is a sphere of radius 1 mm, is that bigger than your confinement volume?

Helpo @weller

This is the volume

sample_shape = new G4Cons("sample_shape", 5.05*cm, 7.25*cm, 5.05*cm-(0.15/10.15*fillingheight), 7.25*cm, fillingheight/2., 0, twopi);
	    G4LogicalVolume* sample_log = new G4LogicalVolume(sample_shape, sample_mat, "sample_log", 0, 0, 0);
	    sample_log -> SetVisAttributes(Sample_Colour);
		PhysSample=new G4PVPlacement(0, G4ThreeVector(0, 0, (-4.55*cm+sampleheight)), sample_log, "sample", Nfilledchamber_log, false, 0);

Then the volume has size larger then the particle distribution

so indeed the gps gives you a tiny 1mm sphere at the origin, compared to the 50mm of your G4cons… would you try to change the gps radius it to 1m just for debugging? (for performance, I think its best to have the gps size as small as possible and still have the confining volume fully enclosed)

Thank you @weller.
Firstly I explain my goal. I’ve a semple in a detector then I’ve to simulate the decay of the sample, then the photon source must be centered in the sample volume.

This is the visualization using a 1mm sphere particle source

and this is using a 1m sphere particle source

it looks like to me that the simulation doesn’t center the particle source in the sample volume, but it always center in (0,0,0).
Then I don’t know hot to center the particle source in the sample volume.

and /gps/position x y z to the desired coordinates does not work?

or are you looking for a programmatic way to retrieve the center of a specific volume and use this as a position for the gps? don’t think that is trivial, as the volume could be inside another volume with different offsets and rotations etc.
also, the volume-name could have ambiguous results, if the mother volume has multiple instances…

Hello @weller yes I can use

/gps/pos/centre .0 .0 0. cm

but in this way it is needed to set the position…therefore my boss wants an automatic way…I found your message then I tried the “confine”.

Do you know other method to set in automatic way the particle source position?

I’ve just seen that during the run I get this warning message

-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : G4GPS001
      issued by : G4SPSPosDistribution::GenerateOne()
LoopCount = 100000
Either the source distribution >> confinement
or any confining volume may not overlap with
the source distribution or any confining volumes
may not exist

If you have set confine then this will be ignored
for this event.

*** This is just a warning message. ***

can it help to understand the problem?

Searching on the forum I found this topic Hemispherical particle source - #3 by fedet93 I read the @guatelli speaks about ConfineToVolume. I is other command to confine the source in a specific volume?

Moreover, @xiuzuo11 said that it happens when the volume to be confined is overlapped with others…he posted a solution, but the file can’t be find

i don’t think so. the real problem seems to be that “confining” does not move your position distribution. it only selects those random positions that were by change generated in the confining volume. from how I understand what you want to achieve, using “confine” is not a suitable approach.

I would suggest you calculate the positions you need for the various sample volumes, and then simply set the correct /gps/pos/centre 5 6 7 cm to have the source centered in the respective volume!?

Unfortunately, my boss wants an automatic way…he says that he uses an automatic way with other simulation (working on old G4 version). The author of this simulation wrote a personal “confine” code…but

  1. The confine code looks like similiar to the gps confine code
  2. My boss gave me these 4 files, but I think that there other files are needed to implement all the code (17.5 KB)
MGGeneratorPrimary.hh (6.3 KB) (19.6 KB)
MGGeneratorPrimaryMessenger.hh (4.4 KB)

Can you check if these calculations are correct please?

I need to set the source in the PhysSample. Here all the positions up to PhysSample

new G4PVPlacement(0, G4ThreeVector(), leadshield_log, "leadshield", logicWorld, false, 0);     
new G4PVPlacement(0, G4ThreeVector(), coppershield_log, "coppershield", leadshield_log, false, 0);
new G4PVPlacement(0, G4ThreeVector(0., 0., 11.95), Nfilledchamber_log, "Nfilledchamber",coppershield_log, false, 0);
PhysSample = new G4PVPlacement(0, G4ThreeVector(0, 0, (-4.55*cm-1.175*cm+fillingheight/2.-fsmarinelli_height/2.)), sample_log,"sample", Nfilledchamber_log, false, 0);

Then, given that fillingheight=16.4 cm and fsmarinelli_height=0 cm

  1. leadshield_log is centered in (0,0,0) in the world coordinates
  2. coppershield is centered in (0,0,0) in the leadshield_log coordinates, corresponding also to (0,0,0) in the world coordinates
  3. Nfilledchamber_log, is centered in (0,0,11.95) in the coppershield_log coordinates, corresponding also to (0,0,11.95) in the world coordinates
  4. sample_log is centered in (0,0,2.475) in the Nfilledchamber_log coordinates, corresponding to (0,0,14.425) in the world coordinates

then the command line to set the source should be /gps/pos/centre .0 .0 14.425 cm
is it the rigth way to calculate the coordinates?

yes, I would do it the same way :slight_smile:

so you do actually want to use the whole sample volume as the source, sorry I misunderstood you there. thought you wanted to center a source into the sample volume, but not use the whole volume itself…
→ how about you ask your boss for the remaining files?
if the code is exactly what you need, that would be the perfect solution, no? I think making it work with the version of geant4 you use is a lot easier than coding it from scratch yourself.

Yes, I’ve to write him asking the other remaining files…but, sincerly, I’m not sure that this code really does what my boss thinks…because it is similiar to the gps confine definition, so it’s strange that the personal code works and the gps doesn’t

why would the gps confine (/gps/pos/confine physical_volume_name) not work? just make sure the physical_volume is within the position distribution, and the volume is not too small!?

this is what the warning message literally tells you :wink:

Because my sample physical volume is on the right part of the detector, then I expect that using the confine function my source is positioned in the sample volume (i.e. my sample volume is the source). On the contrary, as you can see in the pictorial views shown in my previous message, the source is at the center

What fo you mean here?