其他分享
首页 > 其他分享> > Geant4/G4/蒙特卡罗模拟笔记【持续更新】

Geant4/G4/蒙特卡罗模拟笔记【持续更新】

作者:互联网

记录一下自己学G4的过程,供初学的伙伴参考。可能里面有些问题,可留言讨论。
系统:ubuntu 16.04 LTS
G4版本:4.9.5;4.10.4;4.10.6

2 Geant4学习笔记

2.1 安装

1.按照<g4安装步骤.txt>文档进行操作就行,

保持系统更新
sudo apt-get update` 

#安装gcc等编译程序
sudo apt-get install build-essential` 

#安装cmake 强烈建议使用别的方案安装新版本cmake
sudo apt-get install cmake` 

#一般使用源码安装新版本cmake
./bootstrap`
make -j4(根据电脑性能强弱选择数字)`
sudo make install`

#安装x11
sudo apt-get install cmake libx11-dev libxext-dev libxtst-dev libxrender-dev libxmu-dev libxmuu-dev` 

#安装必需的库文件
sudo apt-get install gfortran libssl-dev libpcre3-dev libglu1-mesa-dev libglew-dev libftgl-dev libmysqlclient-dev libfftw3-dev libcfitsio-dev libgraphviz-dev libavahi-compat-libdnssd-dev libldap2-dev python-dev libxml2-dev libkrb5-dev libgsl-dev libqt4-dev`  
sudo apt-get install git dpkg-dev cmake g++ gcc binutils libx11-dev libxpm-dev libxft-dev libxext-dev gfortran libssl-dev libpcre3-dev xlibmesa-glu-dev libglew1.5-dev libftgl-dev libmysqlclient-dev libfftw3-dev libcfitsio-dev graphviz-dev libavahi-compat-libdnssd-dev libldap2-dev python-dev libxml2-dev libkrb5-dev libgsl0-dev libqt4-dev`

在你想要安装g4的文件夹下新建g4安装文件夹,将g4压缩包解压到此处,同时在主文件夹建两个文件夹分别为huild和install,进入build(cmake三选一即可)

无在线下载数据包安装(无qt)
cmake -DCMAKE_INSTALL_PREFIX=~/liuchangqi/geant4.9/install/ -DGEANT4_USE_OPENGL_X11=ON -DGEANT4_USE_RAYTRACER_X11=ON GEANT4_BUILD_MULTITHREADED=ON ~/liuchangqi/geant4.9/geant4.9.5`

无在线下载数据包安装(无qt)
cmake -DCMAKE_INSTALL_PREFIX=~/liuchangqi/geant4.9/install/ -DGEANT4_USE_OPENGL_X11=ON -DGEANT4_USE_RAYTRACER_X11=ON -DGEANT4_USE_GDML=ON GEANT4_BUILD_MULTITHREADED=ON ~/liuchangqi/geant4.9/geant4.9.5`

无线下载数据包安装(带qt --by wuk)**建议使用**
cmake -DCMAKE_INSTALL_PREFIX=/home/kwu/geant4.10.6/install -DGEANT4_USE_OPENGL_X11=ON -DGEANT4_USE_RAYTRACER_X11=ON -DGEANT4_USE_GDML=ON GEANT4_BUILD_MULTITHREADED=ON -DGEANT4_INSTALL_DATA=OFF -DGEANT4_USE_QT=ON(on表示带有qt,安装时去掉括号中的内容) /home/kwu/geant4.10.6/geant4.10.06.p02`

下一步
make -j4
make install

将数据文件拷入build中的data文件夹路径下

将以下几行加入~/.bashrc(安装4.10.6版本时不需要export步骤,将data数据放在/home/kwu/geant4.10.6/install/share/Geant4-10.6.2,直接加 source /home/kwu/install/geant4.10.6/install/share/Geant4-10.6.2/geant4make/geant4make.sh

只有在安装较就版本是需要export(目前已知4.9.5) 【也不需要】(也就是所有版本安装的时候都不需要在~/.bashrc里面加入export)

#export G4SAIDXSDATA=~/geant4.9.5/G4/data/G4SAIDDATA1.1
export G4LEDATA=~/geant4.9.5/G4/data/G4EMLOW6.23
export G4LEVELGAMMADATA=~/geant4.9.5/G4/data/PhotonEvaporation2.2
export G4NEUTRONXSDATA=~/geant4.9.5/G4/data/G4NEUTRONXS1.1
export G4RADIOACTIVEDATA=~/geant4.9.5/G4/data/RadioactiveDecay3.4
export G4REALSURFACEDATA=~/geant4.9.5/G4/data/RealSurface1.0
export G4ABLADATA=~/geant4.9.5/G4/data/G4ABLA3.0
export G4NEUTRONHPDATA=~/geant4.9.5/G4/data/G4NDL4.0
#export G4ENSDFSTATEDATA=~/geant4.9.5/G4/data/G4ENSDFSTATE2.2
#export G4TENDLDATA=~/geant4.9.5/G4/data/G4TENDL1.3.2
export G4PIIDATA=~/geant4.9.5/G4/data/G4PII1.3
source /home/kwu/geant4.9.5/install/share/Geant4-9.5.1/geant4make/geant4make.sh

source /home/kwu/install/geant4.9.5/install/bin/geant4.sh

遇到cmake时error:could not find XercesC的解决办法:

sudo apt-get install libxerces-c-dev

https://github.com/BelledonneCommunications/linphone-desktop/issues/216

3.在4.9.5安装的时候

Source /install/share/geant4-9.5.1/geantmake/geantmake.sh会报错

解决办法是source /install/bin/geant4.sh

2.2建几何时实现布尔运算

有时需要将多个几何体结合起来,如在某个体积里面挖孔,这里需要借用Boolean,见说明书中的 Solids made by Boolean operations

2.3关于.mac文件的一些操作

2.3.1 在qt里面查看由mac(eg:run1.mac)定义的源的性质:

在vis.mac中加入以下内容

/vis/scene/add/trajectories smooth
/vis/modeling/trajectories/create/drawByCharge
/vis/modeling/trajectories/drawByCharge-0/default/setDrawStepPts true
/vis/modeling/trajectories/drawByCharge-0/default/setStepPtsSize 2
# To superimpose all of the events from a given run:
/vis/scene/endOfEventAction accumulate
# Re-establish auto refreshing and verbosity:
/vis/viewer/set/autoRefresh true
/vis/verbose warnings

将run1.mac中的//run/numberOfThreads 6 注释掉

在qt里面运行**/control/execute run1.mac**

2.3.2 在run.mac中设置粒子发射的方向

/gps/ang/mintheta 0 deg
/gps/ang/maxtheta 180 deg
/gps/ang/minphi 90 deg
/gps/ang/maxphi 270 deg

2.3.3 建立一个从圆柱体表面发射的源

extended/eventgenerator/exgps/macros/test07.mac

/gps/pos/type Surface
/gps/pos/shape Cylinder
/gps/pos/centre 2. 2. 2. cm
/gps/pos/radius 2.5 cm
/gps/pos/halfz 5. cm

2.7.3 Macro Commands中也有描述

2.4 Make colors

 G4Colour white  (1.0, 1.0, 1.0) ;
 G4Colour grey  (0.5, 0.5, 0.5) ;
 G4Colour lgrey  (.85, .85, .85) ;
 G4Colour red   (1.0, 0.0, 0.0) ;
 G4Colour blue  (0.0, 0.0, 1.0) ;
 G4Colour cyan  (0.0, 1.0, 1.0) ;
 G4Colour magenta (1.0, 0.0, 1.0) ;
 G4Colour yellow (1.0, 1.0, 0.0) ;
 G4Colour orange (.75, .55, 0.0) ;
 G4Colour lblue  (0.0, 0.0, .75) ;
 G4Colour lgreen (0.0, .75, 0.0) ;
 G4Colour green  (0.0, 1.0, 0.0) ;
 G4Colour brown  (0.7, 0.4, 0.1) ;

From /examples/advanced/underground_physics/src/DMXDetectorConstruction.cc

 G4Colour (G4double r = 1., G4double g = 1., G4double b = 1., G4double a = 1.);//前三个表示三种颜色,最后一个表示透明度
//越大颜色越深

将几何显示设置为框架或者显示表面:在vis.mac中加入

/vis/viewer/set/style surface(wireframe)

2.5 将mac中源的设置画成图

Exgps中,运行./exgps xxx.mac,会生成相应的root文件,root文件里面就有对应源的空间分布、发射角度、能谱等信息(如图所示)。
可以获取能量、角度、位置信息,这里以位置信息为例

2.6 root提取数据merge

在用多线程且是Ntuple的时候要用merge

analysisManeger->SetNtupleMerging(ture);//加入在HistoManager.cc或者RunAction.cc中生成rootfile文件的地方

不然会产生很多个root文件

最好什么时候都用merge

2.7 DetectorConstrution之G4PVReplica

用来建立沿轴向重复的没有间隙的volume

例子在 examples4.10.6/extended/analysis/AnaEx01,李bamboo视频Geant4入门基础教程视频系列3之利用root统计物理数据

2.8 Geant4随机数种子更改

在主程序example.cc中加入下面几行

#include "Randomize.hh"

#include "time.h"

//下面的加在main()函数中

  CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine());//选择随机数引擎RanecuEngine,默认为HepJamesRandom

//将系统时间设置为随机数种子
  G4long seed = time(NULL);
  CLHEP::HepRandom::setTheSeed(seed);

2.9 PhysicsList中定义粒子的两种方法

/home/kwu/g4_file/examples4.10.6/extended/electromagnetic/TestEm1

//定义全部波色子
G4BosonConstructor  pBosonConstructor;
pBosonConstructor.ConstructParticle();

/home/kwu/g4_file/examples4.10.6/extended/electromagnetic/TestEm4

//可以只定义某一类粒子中的几种粒子
//比如只考虑波色子中gamma粒子
void PhysicsList::ConstructParticle()
{
  ConstructBosons();//波色子
  ConstructLeptons();//轻子
  G4GenericIon::GenericIonDefinition();//一般离子
}

void PhysicsList::ConstructBosons()
{
 G4Gamma::GammaDefinition();//只考虑了gamma
}

2.10 获取初始粒子(源)的相关信息

在EventAction中,

// 借鉴/home/kwu/g4_file/examples4.10.6/advanced/amsEcal

//PrimaryGeneratorAction.cc
EventAction::EventAction(DetectorConstruction* det,PrimaryGeneratorAction* prim)
:detector(det), primary(prim)
{}

//可以加在BeginOfEventAction或者EndOfEventAction中
//获取初级粒子的能量、方向、位置
G4double Ebeam = primary->GetParticleGun()->GetParticleEnergy();
G4ThreeVector direction    =  primary->GetParticleGun()->GetParticleMomentumDirection();
G4ThreeVector vertex =  primary->GetParticleGun()->GetParticlePosition();
//位置(x,y,z),以及方向的theta,phi角度
G4double x = vertex.x(), y = vertex.y(), z = vertex.z();
G4double theta = direction.theta(), phi = direction.phi();

//PrimaryGeneratorAction.hh
public:
	G4ParticleGun* GetParticleGun() {return particleGun;};

2.11 G4VProcess 相关的点

AlongStepDoIt 单步时调用

PostStepDoIt 单步终点、并且在产生最小步长(或者强制发生相互碰撞)时调用

AtRestDoIt 粒子停止、并且在产生最小步长(或者强制发生相互碰撞)时调用

所有的AlongStepDoIt调用完之后,调用PostStepDoIt,然后更新G4Track保存的信息

2.12 PhysicsList 命名规则

QGS Quark-gluon model(>~20GeV) 夸克胶子模型

FTF Fritiof parton Model (>~10GeV)

LHEP Low and High energy parameterization model

BIC Binary Light Ion Cascade (<~10 GeV) 二元级联模型

BERT Bertini Cascade Model (<~10 GeV)

HP high precision neutron, proton, d, t, 3He, alpha interaction model (< 20 MeV)

PRECO Pre compound Model (<~150MeV)

EMV(X) Variation of Standard EM package

P Precompound model When the FTF and QGS models are used, the Precompound model (P) is also invoked to de-excite the remnant nucleus after the initial high energy interaction.

2.13 PhysicsList 选用

入射粒子能量<5GeV,用包含“BERT”“BIC”,如QGSP_BERT, QGSP_BIC, FTFP_BERT,

需要详细的中子输运过程,用包含"HP",QGSP_BERT_HP, QGSP_BIC_HP,

需要考虑Bragg,用“EMX”“EMV”,QGSP_BERT_EMV, QGSP_BERT_EMX

核之间的相互作用,自定义

可见光的输运过程、放射性衰变。仅“LEB

2.14 构建几何时判断几何体是否有重复(Overlap)

G4bool checkOverlaps = true;
//
new G4PVPlacement(0,G4ThreeVector(),lv_out,"outside_box_pv",
                              logicWorld,0,false,checkOverlaps);

在Qt里面看几何的时候,如果有Overlap,在Qt界面会有警告;没有Overlap则会显示

Checking overlaps for volume outside_box_pv (G4SubtractionSolid) ... OK!
Checking overlaps for volume pb_pv (G4Box) ... OK!
Checking overlaps for volume middle_box_pv (G4SubtractionSolid) ... OK!
Checking overlaps for volume fe_pv (G4Box) ... OK!
Checking overlaps for volume inside_box_pv (G4Box) ... OK!
Checking overlaps for volume win_toughened_pv (G4Box) ... OK!
Checking overlaps for volume win_toughened_pv (G4Box) ... OK!
Checking overlaps for volume win_lead_pv (G4Box) ... OK!
Checking overlaps for volume win_lead_pv (G4Box) ... OK!
Checking overlaps for volume tub_quartz_pv (G4Tubs) ... OK!
Checking overlaps for volume tub_leb_pv (G4Tubs) ... OK!
Checking overlaps for volume tub_tank_pv (G4Box) ... OK!
Checking overlaps for volume tub_tank_pv (G4Box) ... OK!
Checking overlaps for volume tank2_pv (G4Box) ... OK!

2.15 G4阈值设定 SetCuts

F:\manual_book\G4学习资料\g4slac201908\11Physics2CRNL.pdf

SetCuts(1 mm)

如果初级粒子的能量不足以产生能至少输运1mm的次级粒子,将会发生一下两件事:

1.不产生次级粒子

2.连续能损直至能量降为零

To avoid infrared divergence, some electromagnetic processes require a threshold below which no secondary will be generated. Because of this requirement, gammas, electrons and positrons require production threshold. This threshold should be defined as a distance, or range cut-off, which is internally converted to an energy for individual materials.

This range cut value is applied to the proton as production thresholds of nuclei for hadron elastic processes. In this case, the range cut value does not means the distance of traveling.

# UI commands
/run/setCut 0.01 mm
/run/setCutForAGivenParticle e- 0.01 mm

2.16构建几何时在同一个几何体上挖多个孔的处理方法

挖单个孔时用 G4SubtractionSolid;

挖多个孔,如果用多次G4SubtractionSolid(如下),在几何上看,前后两次挖的孔会有点不一样,具体原因未知;

G4SubtractionSolid* box_mid3 = new G4SubtractionSolid ("box-tub",
                  box_mid2,tub_sub2,0,G4ThreeVector(250.*mm,yy_tub1,zz2));
G4SubtractionSolid* box_mid = new G4SubtractionSolid ("box-tub",
                  box_mid3,tub_sub2,0,G4ThreeVector(-250.*mm,yy_tub1,zz2));

所以挖多个孔处理的方法如下:

//先将多个孔的物理体合并为一个体
G4Transform3D tub2 = G4Transform3D(rotm,G4ThreeVector(250.*mm,yy_tub1,zz2));
G4Transform3D tub4 = G4Transform3D(rotm,G4ThreeVector(-250.*mm,yy_tub1,zz2));
G4Transform3D box2 = G4Transform3D(rotm,G4ThreeVector(641.*mm,yy_box_window,zz2));
G4Transform3D box4 = G4Transform3D(rotm,G4ThreeVector(-641.*mm,yy_box_window,zz2));
G4MultiUnion* munion_tub2 = new G4MultiUnion("tub_union2");
munion_tub2->AddNode(*tub_sub2,tub2);
munion_tub2->AddNode(*tub_sub4,tub4);
munion_tub2->AddNode(*box_sub2,box2);
munion_tub2->AddNode(*box_sub4,box4);
munion_tub2->Voxelize();

//然后整体一次扣去
G4SubtractionSolid* box_mid = new G4SubtractionSolid("box-tub2",
                    box_mid_old,munion_tub2,0,G4ThreeVector(0.,0.,0.));

2.17法国波尔多原子能机构提供的Geant4虚拟机

网站
下载之后直接用VMware打开虚拟机就可,里面自带Geant4\root,不用自己安装,不过是CentOS系统

标签:box,G4,pv,蒙特卡罗,dev,install,geant4.9,Geant4
来源: https://blog.csdn.net/weixin_44611621/article/details/116094805