模型介紹
HiPIMS(High-Performance Integrated hydrodynamic Modelling System)使用最先進的數值方案(Godunov型有限體積法)來求解二維淺水方程以進行洪水模擬。為了支持高分辨率洪水模擬,使用CUDA/C ++語言在多個GPU上實現了HiPIMS,以實現高性能計算。由于HiPIMS具有模塊化和靈活的結構,因此只要問題可以在均勻的矩形網格上解決,就可以進一步開發用于水文科學的其他應用。
HiPIMS由水力環境建模實驗室開發和維護,HiPIMS的開發由梁秋華(Qiuhua Liang)教授領導,凝聚了將近二十年的心血和眾多研究人員的努力。
Pypims 是HiPIMS的Python API,該API提供了一個用戶友好的集成工具鏈,用于準備輸入,運行HiPIMS和可視化輸出。完整的文檔在https://pypims.readthedocs.io/en/latest/上。
第一種安裝方式:編譯安裝
Pypims 是一個Python第三方庫,源代碼已經在Github開源,按照說明書 在Win 11電腦上安裝成功。步驟記錄如下。
計算機環境
我的計算機操作系統為Windows 11 專業工作站版,版本號23H2。
系統類型是64 位操作系統, 基于 x64 的處理器。
處理器為12th Gen Intel? Core? i7-12700 2.10 GHz,內存為 96 GB。
GPU為NVIDIA RTX A4000,顯存為 16 GB,驅動版本為576.02,CUDA版本為12.9。
安裝Miniconda
Miniconda是Anaconda的輕量級版本,由Continuum Analytics(現Anaconda Inc.)開發。通過Miniconda,用戶可高效管理項目依賴,平衡靈活性與資源消耗。
安裝Git
從Git官方網站下載安裝程序:
Git官網下載頁面
運行下載的.exe文件,按照安裝向導默認選項完成安裝。安裝完成后,在命令提示符或PowerShell中輸入 git --version
驗證是否安裝成功。
安裝完成后需設置用戶名和郵箱:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
使用 git config --list
可查看當前配置。
安裝Visual Studio
Visual Studio 是由微軟開發的集成開發環境(IDE),支持多種編程語言(如 C#, C++, Python, JavaScript 等)和平臺(Windows, Web, 移動端等)。
安裝免費的Visual Studio Community版本,并安裝C++負載。
安裝NVIDIA CUDA Toolkit
CUDA Toolkit可以從NVIDIA網站下載。
安裝完成后,設置環境變量。
克隆Pypims項目
進入欲安裝的文件夾,在命令提示符工具中,輸入以下命令
git clone https://github.com/pypims/pypims.git
cd pypims
如果連接不上Github,可以設置網絡代理。
修改CmakeLists
當前 CUDA 版本或顯卡驅動 不再支持 sm_35
。因此需要在Pypims項目文件./pypims/pypims/CMakeLists.txt中,去掉對sm_35
的支持,并替換為本機顯卡支持的Compute Capability(sm_86
)。
CUDA GPU Compute Capability查詢網址:https://developer.nvidia.com/cuda-gpus
RTX A4000 → Compute Capability = 8.6 → sm_86
IF(WIN32)set(PLATFORM_CXX_FLAGS /Zi)set(CUDA_SEPARABLE_COMPILATION ON)set(CUDA_NVCC_FLAGS -arch=sm_86;--expt-extended-lambda)
ENDIF(WIN32)
修改cuda_data_bank.cu
在./pypims/pypims/lib/src/multi_threading/cuda_data_bank.cu代碼中,使用了 HEMI_DEV_CALLABLE_INLINE_MEMBER 宏,它的作用是用來兼容 GPU 和 CPU 編譯環境。而 HEMI 本身不再與新版本 CUDA(>= 11)和 Thrust 完美兼容。再加上我使用的是 CUDA 12.9,在編譯時會觸發如下錯誤:
error : static assertion failed with "unimplemented for this system"
這主要是 Thrust 在執行 thrust::device_vector<GC::Vector3>
的 .resize()
操作時,無法推導出如何用你提供的構造器在 device 上初始化對象。
解決方案是手動定義 thrust::device_vector<GC::Vector3>
時不要使用 .resize(n)
。
// 不推薦
thrust::device_vector<GC::Vector3> v;
v.resize(n); // ? 這會默認構造 device 端 Vector3,如果 Thrust 不支持就會崩潰// 推薦
std::vector<GC::Vector3> host_vec(n, GC::Vector3(0, 0, 0));
thrust::device_vector<GC::Vector3> v = host_vec;
因此對cuda_data_bank.cu文件的cuDataBankBranch函數修改為
cuDataBankBranch::cuDataBankBranch(unsigned int _size_lower2receive, unsigned int _size_lower2send, unsigned int _size_upper2receive, unsigned int _size_upper2send):size_lower2receive(_size_lower2receive), size_lower2send(_size_lower2send), size_upper2receive(_size_upper2receive), size_upper2send(_size_upper2send){lower_status = 0;std::vector<GC::Vector3> host_vec1(size_lower2receive, GC::Vector3(0, 0, 0));data_lower = host_vec1;indices_lower2receive = host_vec1;std::vector<GC::Vector3> host_vec2(size_lower2send, GC::Vector3(0, 0, 0));indices_lower2send = host_vec2;upper_status = 0;std::vector<GC::Vector3> host_vec3(size_upper2receive, GC::Vector3(0, 0, 0));data_upper = host_vec3;indices_upper2receive = host_vec3;std::vector<GC::Vector3> host_vec4(size_upper2send, GC::Vector3(0, 0, 0));indices_upper2send = host_vec4;}
創建虛擬環境
虛擬環境:允許創建隔離的Python環境,避免依賴沖突。
用管理員方式打開Anaconda Prompt,創建 Conda 虛擬環境
conda create -n pypims python=3.7 -y
安裝依賴
激活環境
conda activate pypims
安裝Cmake
conda install cmake -y
安裝GDAL
conda install gdal -y
用相同的方式安裝./pypims/docs/requirements.txt
中列出的所有庫
conda install rasterio -y
conda install fiona -y
conda install numpy -y
conda install scipy -y
conda install matplotlib -y
conda install imageio -y
conda install pandas -y
conda install pyshp -y
conda install “sphinx>=4.1” -y
conda install “sphinx-rtd-theme>=1.0.0” -y
conda install nbsphinx -y
安裝ipykernel,用于在Visual Studio Code中運行Jupyter Notebook的demo。
conda install ipykernel -y
安裝Pypims
在Pypims文件夾中,打開Anaconda Prompt,進入pypims環境,輸入命令
python setup.py install
安裝完成。
第二種安裝方式:Conda直接安裝
在安裝完Miniconda、Git、Visual Studio、NVIDIA CUDA Toolkit后,創建虛擬環境,并安裝所有依賴。
然后在虛擬環境中,輸入以下命令,直接安裝Pypims。
注意:此方式未經測試,可能會遇到錯誤。
conda install pypims
或者
pip install pypims
運行洪水淹沒模擬案例
根據Pypims官方提供的教程,使用Visual Studio Code打開./pypims/docs/source/Tutorials/flood.ipynb
,選擇Python環境pypims
,運行flood.ipynb
,學習Pypims的使用。