Changing Momentum Direction Between Runs

Hi All,

I’m running a simulation where I calculate the diffusion coefficient of an electron through a material. I’ve got it all set up and working to generate my data, but now I need to do this for 1000 different initial momentum directions of the electron (I’m trying to obtain a continuous distribution of the diffusion coefficient as a function of initial momentum direction). So I’m thinking I need to change the momentum direction I give to G4ParticleGun in between each run for 1000 runs. Is there a way to do this? Or do I need to write a macro that loops 1000 times and sets it manually in the UI commands every run?

Ben Phillips

You could have your generator throw a random momentum direction each time, and collect all your data in one run. If you don’t want to write your own generator code, you could look at using G4GeneralParticleSource (GPS).

You may want to use a couple of UI commands to control a loop. You need to have two macro files , one for controlling a loop (let’s call it A.mac), and the other is for controlling an each individual run (this is a macro you already have, let’s call it B.mac).


/control/loop B.mac i 0 1000 ## variable “i” changes from 0 to 1000


/control/multiply ang {i} 0.36 ## variable “ang” = “i” * 0.36, i.e. varies from 0 to 360
/your_UI_command/set_angle {ang} deg

Please note that in this example /control/loop command execute B.mac macro file 1000 times with changing the parameter “I”.