Geant4-怎样设置你的粒子源--精简注释+收藏版

时间:2022-07-24
本文章向大家介绍Geant4-怎样设置你的粒子源--精简注释+收藏版,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一个通用的粒子发生函数源文件,即MYPrimaryGeneratorAction.cc大致就是本文的全部了。同现实场景相符,你需要知道每个发射的模拟粒子的特点,即出射点在哪、发射方向、粒子类型、能量大小等参数。

给大家上个G4论坛帖子,我们G4模拟中的很多问题,该论坛几乎都会有人提问回答的。

http://hypernews.slac.stanford.edu/HyperNews/geant4/get/eventtrackmanage/13.html?inline=-1

#include "MYPrimaryGeneratorAction.hh"
#include "Randomize.hh"
#include "G4Event.hh"
#include "G4ParticleGun.hh"
#include "G4GeneralParticleSource.hh"
#include "G4ParticleTable.hh"
#include "G4ParticleDefinition.hh"
#include "G4SystemOfUnits.hh"
#include "G4RunManager.hh"
#include "G4Run.hh"
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 
MYPrimaryGeneratorAction::MYPrimaryGeneratorAction()
{G4int n_particle = 1;
//particleGun = new G4GeneralParticleSource();
particleGun = new G4ParticleGun(n_particle); 
G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
G4ParticleDefinition* particle = particleTable->FindParticle("opticalphoton");
//G4ParticleDefinition* particle = particleTable->FindParticle("gamma");
//我们找G4中现成的粒子源
particleGun->SetParticleDefinition(particle);
particleGun->SetParticleTime(0.0*ns);
//particleGun->SetParticlePosition(G4ThreeVector(-1*mm,1*mm,-1*mm));
// particleGun->SetParticleMomentumDirection(G4ThreeVector(1.,0.,0.));
particleGun->SetParticleEnergy(3.*eV);
//particleGun->SetParticlePolarization(G4ThreeVector(1,0,0));
//particleGun->SetParticleEnergy(60*keV);
//我们可以在初始化这个源文件时,就设置好particleGun的默认值。
}
MYPrimaryGeneratorAction::~MYPrimaryGeneratorAction()
{
  delete particleGun;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void MYPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
{
const G4Run *nowrun=G4RunManager::GetRunManager()->GetCurrentRun();
G4int runid=nowrun->GetRunID();
// 在程序的任意cc源文件中,均可调用G4RunManager,在这个例子中,我们想得到当前RUN的RunID,即如果我在mac执行文件中,/run/beamOn 100 重复写了3次,表示有3个RUN,RunID分别是0,1,2。
G4int posx,posy,posz;
posx=int(floor(runid/64));
posy=int(floor((runid%64)/8));
posz=int(floor((runid%64)%8));
particleGun->SetParticlePosition(G4ThreeVector((posx-3.5)*3.2*mm,(posy-3.5)*3.2*mm,(posz-3.5)*3.2*mm));
//我们想依据不同的RUN依次设置放射源的不同位置。这样子处理,我就不必每跑一次RUN改一次放射源位置了,比较灵活。这其中很多的Set***成员函数,GPS是没有的(或许有但是还没找到),ParticleGun更灵活,而GPS在mac文件中更机智,看个人选择。
G4double rndmCosTheta,rndmTheta,rndmPhi,rndmLambda,px,py,pz,pol_x,pol_y,pol_z;
      rndmCosTheta = G4UniformRand()*2-1;
      rndmTheta = acos(rndmCosTheta);
      rndmPhi = G4UniformRand()*(2*M_PI);
      rndmLambda = G4UniformRand()*(2*M_PI);
      // Momentum direction (random)
      px=sin(rndmTheta)*cos(rndmPhi);
      py=sin(rndmTheta)*sin(rndmPhi);
      pz=cos(rndmTheta); particleGun->SetParticleMomentumDirection(G4ThreeVector(px,py,pz));
//我们定义一个动量方向矢量,随机为一个球面源,在此特别感谢唐光毅博后的数学指导。
      // Polarization direction (random)
      pol_x=cos(rndmLambda)*cos(rndmTheta)*cos(rndmPhi)-sin(rndmLambda)*sin(rndmPhi);
      pol_y=cos(rndmLambda)*cos(rndmTheta)*sin(rndmPhi)+ sin(rndmLambda)*cos(rndmPhi);
      pol_z= - cos(rndmLambda)*sin(rndmTheta);
      particleGun->SetParticlePolarization(G4ThreeVector(pol_x,pol_y,pol_z));
//定义光学光子的极化角,这个我现在还不懂,不知道到底有啥用,可能是考虑光子的偏振吧。
particleGun->GeneratePrimaryVertex(anEvent);
// GeneratePrimaryVertex(anEvent) 这函数运行的瞬间,就会产生一个模拟粒子,写两遍就可以发射两次,你也可以借此机智的设置为一个双源或者多源。
//G4cout<<"run ID is "<<runid<<G4endl;
//用好这个简单的G4cout,你的G4程序代入感和自主学习能力将会增强很多很多倍。哪里不懂就试试万能的G4cout吧。
}

喜欢的话,分享一下吧~^o^~