theme: orange
持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第17天,点击查看活动详情
🤞 个人主页:@青Cheng序员石头
Geant4模拟有三种运行模式,续上篇文章提到的macro文件批处理模式,这篇文章继续讲剩下的硬编码运行模式和混合运行模式。
硬编码运行模式
硬编码运行模式,就是将模拟所需的流程中的一切参数都写在代码中,这种运行缺点是相当明显的,那就是不灵活,在实际模拟过程中很多时候需要动态根据模拟结果调整能量的大小,入射角度,粒子发射的个数等,这还得修改代码重新编译,挺麻烦的。
虽然有那么不方便,但是对于设置模拟过程的初始化值还是很有用处的,那么其代码实现是怎样的呢?
```C++ int main() { //. . ….. G4runManager初始化… //. . ….. class初始化… //………G4核初始化… runManager->Initialize(); //start run G4int numberOfEvent = 3; runManager->BeamOn(numberOfEvent); delete runManager; return 0; }
比如还可以用于定义发射枪的一些默认参数。
C++ G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable(); G4ParticleDefinition* particle = particleTable->FindParticle("neutron"); particleGun->SetParticleDefinition(particle); particleGun->SetParticleTime(0.0ns); particleGun->SetParticlePosition(G4ThreeVector(0.0cm,0.0cm,-1.0cm)); particleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,2.)); particleGun->SetParticleEnergy(14.2* MeV); ```
混合运行模式
上面讲到的三种模式,单独使用都有各自的优缺点,从Geant4 官方来说,更加推崇使用三者同时使用的混合运行模式。
```C++ //Mixed mode
include "G4UImanager.hh "
include "G4RunManager.hh"
include "G4VisManager.hh"
include "G4VisExecutive.hh"
include "G4UItcsh.hh"
include "G4UIterminal.hh"
include "G4UIsession.hh"
int main(int argc,char* argv) {
G4RunManager MyRun = new G4RunManager; //this part is for the visualization G4VisManager* VisManager = new G4VisExecutive; VisManager->Initialize();
MyRun->Initialize();
//G4UIsession* session = new G4UIterminal(new G4UItcsh);
G4UImanager* UI = G4UImanager::GetUIpointer();
if(argc==1){
UI->ApplyCommand("/run/verbose 2");
G4String command="/control/execute /g4work/pphotonelectron/vis.mac";
UI->ApplyCommand(command);
//session->SessionStart();
}
else{
UI->ApplyCommand("/run/verbose 2");
G4String command="/control/execute /g4work/pphotonelectron/";
G4String fileName = argv[1];
UI->ApplyCommand(command+fileName);
//session->SessionStart();
}
//delete session; delete VisManager; delete MyRun; return 0; }
``` 当该程序编译完成以后,在运行时,通过输入的运行命令所带的参数个数决定到底使用哪种模式,可谓是灵活方便。
如果输入命令./myExecutable
,会进入if内部的逻辑,如果输入命令./myExecutable mymacro.mac
,那么将进入else内部的逻辑。强烈推荐混合运行方式。
少年,没看够?点击石头的详情介绍,随便点点看看,说不定有惊喜呢?欢迎支持点赞/关注/评论,有你们的支持是我更文最大的动力,多谢啦!