这是一个脚本文件,你可以复制过去并且尝试着在自己本机上运行。
本文共分成三个部分:第一部分是安装g4、第二部分是尝试运行G4、第三部分是可视化功能的实现。
如果你在完成安装后,尝试运行G4已经成功,则可以忽略第三部分。
在该脚本文件中共有四个变量
url:你选择的geant4的版本下载地址
path:安装路径
GEANT4_INSTALL_DIR:G4安装的文件夹
i:好像是线程数,解释看下面,用在57行
在57
行make -j8
该处的数字8可更改,数字越大越快,但是电脑配置差会得到相反的结果,恰到好处正好。
#!/bin/bash
# echo 'passwd' | sudo -S apt install -y package_name 免自己输入密码设置,passwd是自己的密码
touch ~/geant4Output.txt
# 更新软件源
sudo apt-get update >> ~/geant4Output.txt
sudo apt-get upgrade -y >> ~/geant4Output.txt
# 设置变量
url=https://gitlab.cern.ch/geant4/geant4/-/archive/v11.0.4/geant4-v11.0.4.tar.gz
path=~/g4/geant4/
GEANT4_INSTALL_DIR=${path}g4install
i=8
echo "变量设置成功"
# 下载Geant4安装包
mkdir -p $path && cd $path
wget -c $url #下载g4压缩包,若是重装G4:注释此行,取消注释下行(mv)
# mv ~/geant4*.tar.gz $path # 将g4.tar.gz 移动至${path}路径
echo "下载完成..."
# 解压安装包
tar -xzf geant4-*.tar.gz
mv geant4-*.tar.gz ~/ # 预防未安装成功,将压缩包移入~/
echo "解压完成"
# 创建安装and构建目录
mkdir -p ${path}g4install ${path}g4build && cd ${path}g4build
echo "创建并进入${path}g4build"
# 安装Geant4
echo "开始安装..."
echo "开始安装..." >> ~/geant4Output.txt
# 预备工作
echo "预备工作..."
echo "预备工作..." >> ~/geant4Output.txt
#一些依赖项
#build-essential cmake cmake-qt-gui cmake-curses-gui libexpat-dev libgl1-mesa-dev libglu1-mesa-dev libxt-dev libxmu-dev libxerces-c-dev libxi-dev zlib1g-dev libgl2ps-dev libexpat1-dev libmotif-dev qt5*
sudo apt-get install -y cmake >> ~/geant4Output.txt
sudo apt-get install -y cmake-qt-gui >> ~/geant4Output.txt
sudo apt-get install -y cmake-curses-gui >> ~/geant4Output.txt
sudo apt-get install -y build-essential >> ~/geant4Output.txt
sudo apt-get install -y libexpat-dev libgl1-mesa-dev libglu1-mesa-dev libxt-dev libxmu-dev libxerces-c-dev >> ~/geant4Output.txt
sudo apt-get install -y libxi-dev zlib1g-dev libgl2ps-dev libexpat1 libexpat1-dev libmotif-dev qt5* >> ~/geant4Output.txt
echo "预备工作完成..."
echo "预备工作完成..." >> ~/geant4Output.txt
# 安装
echo "开始安装g4"
# cmake 备选项,一般没用
# cmake -DGEANT4_USE_OPENGL_X11=ON -DGEANT4_USE_RAYTRACER_X11=ON -DGEANT4_USE_QT=ON -DGEANT4_BUILD_MULTITHREADED=ON
# cmake -DBUILD_SHARED_LIBS=ON -DGEANT4_USE_SYSTEM_CLHEP=OFF -DGEANT4_USE_GDML=ON -DGEANT4_USE_USOLIDS=OFF -DGEANT4_USE_XM=OFF -DGEANT4_USE_PYTHON=OFF -DGEANT4_USE_NETWORKDAWN=OFF -DCMAKE_BUILD_TYPE=RelWithDebInfo -DGEANT4_INSTALL_EXAMPLES=ON -DGEANT4_INSTALL_TESTDATA=ON
echo "g4第一步:cmake 选项 安装路径/解压文件"
sudo cmake -DGEANT4_INSTALL_DATA=ON -DGEANT4_USE_OPENGL_X11=ON -DGEANT4_USE_RAYTRACER_X11=ON -DGEANT4_USE_QT=ON -DGEANT4_BUILD_MULTITHREADED=ON -DGEANT4_INSTALL_EXAMPLES=ON -DGEANT4_INSTALL_TESTDATA=ON -DCMAKE_INSTALL_PREFIX=${GEANT4_INSTALL_DIR} ${path}geant4-v11.0.4 >> ~/geant4Output.txt
echo "g4第二步:make -jn"
sudo make -j${i} >> ~/geant4Output.txt
echo "g4第三步:make"
sudo make install >> ~/geant4Output.txt
echo "安装g4完成,正在检测是否安装成功..."
# 设置Geant4环境变量
echo -e "开始配置环境变量... \n 建议自己寻找路径手动配置 "
############## 配置环境->建议手动配置 #################
# vi ~/.bashrc
# 文件末尾写入下面两行命令:
# source ~/g4/geant4/g4install/bin/geant4.sh
# source ~/g4/geant4/g4install/share/Geant4-11.0.4/geant4make/geat4make.sh
# 保存退出
# source ~/.bashrc
############## 配置环境->建议手动配置 #################
############## 配置环境 #################
read -t 60 -p "开始配置环境强烈建议手动配置,需要脚本设置变量吗?(y/n)" Reply
if [[ "$Reply" == "y" ]]
then
if [ -e ${GEANT4_INSTALL_DIR}/bin/geant4.sh ]
then
echo "source ${GEANT4_INSTALL_DIR}/bin/geant4.sh" >> ~/.bashrc
if [ $? -eq 0 ]
then
echo ".../bin:./bashrc成功"
echo ".../bin:./bashrc成功" >> ~/geant4Output.txt
else
echo ".../bin:./bashrc失败"
echo ".../bin:./bashrc失败" >> ~/geant4Output.txt
fi
fi
if [ -e ${GEANT4_INSTALL_DIR}/share/Geant4-11.04/geant4make/geant4make.sh ]
then
echo "source ${GEANT4_INSTALL_DIR}/share/Geant4-11.0.4/geant4make/geant4make.sh" >> ~/.bashrc
if [ $? -eq 0 ]
then
echo ".../geant4make.sh:./bashrc成功"
echo ".../geant4make.sh:./bashrc成功" >> ~/geant4Output.txt
else
echo ".../geant4make.sh:./bashrc失败"
echo ".../geant4make.sh:./bashrc失败" >> ~/geant4Output.txt
fi
fi
source ~/.bashrc
echo "环境变量配置完成,建议亲自查看"
fi
############## 配置环境 #################
# 确认安装完成?
if [[ -n "$(geant4-config --version)" ]]
then
echo "Geant4 has been installed successfully!"
else
echo "OH!!!!!!! fuck"
fi
echo "请检查环境配置!"
请确保已经安装成功,添加环境和 source ~/.bashrc
cd ~/g4/geant4/geant4-v11.0.4/examples/basic/B1
mkdir test && cd test
cmake ../
make
./exampleB1
成功出现可视化界面了吗? 如果出现,恭喜你已经安装成功啦,太棒了捏~
!
如果没有出现可以试试第三部分
######################### 出现可视化问题 ##########################
# cmake ../ 和 make 均未出现问题,但是 ./exmple 无法显示图形化界面
# 类似与这样
# *** G4Exception : UIMAN0123
# issued by : G4UImanager::ApplyCommand
# Command aborted (500)
# Error code : 500
# *** This is just a warning message. ***
# 以及
# Got keys from plugin meta data ("ibus")
# QFactoryLoader::QFactoryLoader() checking directory path "/home/ys/g4/geant4/geant4-v11.0.4/examples/basic/B1/t/platforminputcontexts" ...
# loaded library "/usr/lib/x86_64-linux-gnu/qt5/plugins/platforminputcontexts/libcomposeplatforminputcontextplugin.so"
# Segmentation fault
# 以及这样
# Warning: "" failed to compile!
# Warning: "" failed to compile!
# Warning: "" failed to compile!
# Warning: "" failed to compile!
# Warning: "" failed to compile!
# Warning: "" failed to compile!
# Warning: "" failed to compile!
# Warning: "" failed to compile!
# Warning: "" failed to compile!
# QGLShaderProgram::uniformLocation( imageTexture ): shader program is not linked
# Warning: "" failed to compile!
# The X11 connection broke: I/O error (code 1)
# XIO: fatal IO error 2 (No such file or directory) on X server "localhost:12.0"
# after 1065 requests (1065 known processed) with 0 events remaining.
# WARNING - Attempt to delete the physical volume store while geometry closed !
# WARNING - Attempt to delete the logical volume store while geometry closed !
# WARNING - Attempt to delete the solid store while geometry closed !
# WARNING - Attempt to delete the region store while geometry closed !
# 终端输入:geant4-config --version查看是否真的安装成功了。
# if 出现了版本号,则成功->继续尝试下面的解决方案;
# else 则失败->重装(可怜巴巴)
一. 参考wsl指南[Linux GUI](https://learn.microsoft.com/zh-cn/windows/wsl/tutorials/gui-apps)
下载相关的GPU驱动
wsl --update
wls --shutdown
sudo apt update
sudo apt install gedit -y
sudo apt install gimp -y
sudo apt install nautilus -y
sudo apt install vlc -y
sudo apt install x11-apps -y
# 终端尝试 xeyes ,若是出现小眼睛,则说明已经安装成功,再次尝试运行G4。
# 若是无法出现小眼睛,继续下面的二
若出现报错:MoTTY X11 proxy: Unsupported authorisation protocol
可以尝试
cp /root/.Xauthority ~/.Xauthority
或者
cp ~/.Xauthority /root/.Xauthority
# 二. 无法进行继续尝试
sudo apt-get remove openssh-server
sudo apt-get install -y xfonts-base xfonts-75dpi xfonts-100dpi xfonts-scalable xfonts-cyrillic
sudo apt-get install -y xfce4 xfce4-session x11-apps
sudo apt-get install -y openssh-server
## 注意此处:连接ip ??存疑
# echo "export DISPLAY=:0" >> ~./bashrc && source ~/.bashrc
##
# 第二步完成
# 再次尝试xeyes,若是出现小眼睛,继续尝试运行geant4
或者
# 三. 可以调试出g4窗口,但是无法显示几何体(黑屏)
# 可以尝试下行代码
export LIBGL_ALWAYS_INDIRECT=0
# 如果g4窗口恢复正常,运行下行代码,写入执行文件
echo "export LIBGL_ALWAYS_INDIRECT=0" >> ~/.bashrc
感谢参考网页的作者!感谢采用此篇文章安装g4的用户以及给出的评论和意见!特别感谢张桓瑜858指出文章中的相关错误以及给予的宝贵建议!