Skip to content

CP2K 编译及使用

CP2K 是一个量子化学和固态物理软件包,可以对固态、液体、分子、周期性、材料、晶体和生物系统进行原子模拟。CP2K 为各种建模方法提供了一个通用框架,例如使用混合高斯和平面波方法(GPW 和 GAPW)的密度泛函理论(DFT)。支持的理论级别包括 DFTB、LDA、GGA、MP2、RPA、半经验方法(AM1、PM3、PM6、RM1、MNDO 等)以及经典力场(AMBER、CHARMM 等)。CP2K 可以进行分子动力学、元动力学、蒙特卡罗方法、埃伦费斯特动力学、振动分析、核心能级光谱、能量最小化和使用 NEB 或二聚体方法的过渡态优化模拟(详细功能概述)。

CP2K 用 Fortran 2008 编写,并且可以通过多线程、MPI 和 CUDA 结合高效并行运行。它在 GPL 许可下免费提供。因此,尝试使用该代码并根据需要进行修改非常容易。

本教程将介绍在SonmiHPC集群系统上使用 Intel oneAPI 进行编译安装 CP2K,并提供给集群内用户进行使用。

源码下载

可以直接从 CP2K 的 Github 仓库中下载各个版本的源码。本教程直接从Github 中下载最新版本的源码压缩包并上传到集群上用户目录下。

使用下面的命令进行解压,并进去源码文件夹中。

shell
tar xvf cp2k-2024.1.tar.bz2
cd cp2k-2024.1
tar xvf cp2k-2024.1.tar.bz2
cd cp2k-2024.1

安装依赖组件

由于大部分依赖组件需要下载安装包到本地,并且服务器均在国外,如果有条件可以提前设置好wget的局域网下的代理进行加速。

先激活 Intel oneAPI 环境:

shell
source /opt/intel/oneapi/setvars.sh
source /opt/intel/oneapi/setvars.sh

进入toolchain文件夹中安装相关组件:

shell
cd tools/toolchain/
./install_cp2k_toolchain.sh --no-check-certificate --mpi-mode=intelmpi --math-mode=mkl --with-intel --with-gcc --with-cmake=system --with-libxc=install --with-libgrpp=install --with-fftw=install --with-scalapack=install --with-libxsmm=install --with-elpa=install --with-ptscotch=install --with-superlu=install --with-pexsi=install --with-plumed=install --with-sirius=install --with-gsl=install --with-libvdwxc=install --with-spglib=install --with-hdf5=install --with-spfft=install --with-spla=install --with-cosma=install --with-libvori=install
cd tools/toolchain/
./install_cp2k_toolchain.sh --no-check-certificate --mpi-mode=intelmpi --math-mode=mkl --with-intel --with-gcc --with-cmake=system --with-libxc=install --with-libgrpp=install --with-fftw=install --with-scalapack=install --with-libxsmm=install --with-elpa=install --with-ptscotch=install --with-superlu=install --with-pexsi=install --with-plumed=install --with-sirius=install --with-gsl=install --with-libvdwxc=install --with-spglib=install --with-hdf5=install --with-spfft=install --with-spla=install --with-cosma=install --with-libvori=install

需要GPU支持可以自行添加其他模块,可以使用下面命令查看所有可选项:

shell
./install_cp2k_toolchain.sh --help
./install_cp2k_toolchain.sh --help

等待组件安装完成后会提示下面的信息:

==================== generating arch files ====================
arch files can be found in the /root/cp2k-2024.1/tools/toolchain/install/arch subdirectory
Wrote /root/cp2k-2024.1/tools/toolchain/install/arch/local.ssmp
Wrote /root/cp2k-2024.1/tools/toolchain/install/arch/local.sdbg
Wrote /root/cp2k-2024.1/tools/toolchain/install/arch/local.psmp
Wrote /root/cp2k-2024.1/tools/toolchain/install/arch/local.pdbg
========================== usage =========================
Done!
Now copy:
  cp /root/cp2k-2024.1/tools/toolchain/install/arch/* to the cp2k/arch/ directory
To use the installed tools and libraries and cp2k version
compiled with it you will first need to execute at the prompt:
  source /root/cp2k-2024.1/tools/toolchain/install/setup
To build CP2K you should change directory:
  cd cp2k/
  make -j 8 ARCH=local VERSION="ssmp sdbg psmp pdbg"

arch files for GPU enabled CUDA versions are named "local_cuda.*"
arch files for GPU enabled HIP versions are named "local_hip.*"
arch files for OpenCL (GPU) versions are named "local_opencl.*"
arch files for coverage versions are named "local_coverage.*"

Note that these pre-built arch files are for the GNU compiler, users have to adapt them for other compilers.
It is possible to use the provided CP2K arch files as guidance.
==================== generating arch files ====================
arch files can be found in the /root/cp2k-2024.1/tools/toolchain/install/arch subdirectory
Wrote /root/cp2k-2024.1/tools/toolchain/install/arch/local.ssmp
Wrote /root/cp2k-2024.1/tools/toolchain/install/arch/local.sdbg
Wrote /root/cp2k-2024.1/tools/toolchain/install/arch/local.psmp
Wrote /root/cp2k-2024.1/tools/toolchain/install/arch/local.pdbg
========================== usage =========================
Done!
Now copy:
  cp /root/cp2k-2024.1/tools/toolchain/install/arch/* to the cp2k/arch/ directory
To use the installed tools and libraries and cp2k version
compiled with it you will first need to execute at the prompt:
  source /root/cp2k-2024.1/tools/toolchain/install/setup
To build CP2K you should change directory:
  cd cp2k/
  make -j 8 ARCH=local VERSION="ssmp sdbg psmp pdbg"

arch files for GPU enabled CUDA versions are named "local_cuda.*"
arch files for GPU enabled HIP versions are named "local_hip.*"
arch files for OpenCL (GPU) versions are named "local_opencl.*"
arch files for coverage versions are named "local_coverage.*"

Note that these pre-built arch files are for the GNU compiler, users have to adapt them for other compilers.
It is possible to use the provided CP2K arch files as guidance.

按照提示,将生成的 arch 文件复制到对应的 arch文件夹中:

shell
cp -r install/arch/* ../../arch/
cp -r install/arch/* ../../arch/

编译安装

回到源码目录根路径,并激活环境变量:

cd ../../
source tools/toolchain/install/setup
cd ../../
source tools/toolchain/install/setup

执行下面的命令进行编译psmp版本,该版本支持 MPI + OpenMP 混合模式:

shell
make -j ARCH=local VERSION="psmp"
make -j ARCH=local VERSION="psmp"

等待编译完成。

创建一个文件夹用于放置编译好的二进制可执行文件和相关依赖组件:

shell
mkdir -p /share/apps/cp2k/2024.1/bin
cp -r exe/local/* /share/apps/cp2k/2024.1/bin/
cp -r tools/toolchain/install /share/apps/cp2k/2024.1/
cp -r data /share/apps/cp2k/2024.1/
mkdir -p /share/apps/cp2k/2024.1/bin
cp -r exe/local/* /share/apps/cp2k/2024.1/bin/
cp -r tools/toolchain/install /share/apps/cp2k/2024.1/
cp -r data /share/apps/cp2k/2024.1/

为方便集群内其他用户使用,修改该路径权限:

shell
chmod -R 755 /share/apps/cp2k
chmod -R 755 /share/apps/cp2k

Modulefile编写

编译好的程序可以通过Module模块的方式提供给集群上其他用户使用,可以编写如下的Modulefile文件:

shell
mkdir -p /share/apps/modulefiles/cp2k
mkdir -p /share/apps/modulefiles/cp2k

在该路径下创建一个新的文件:

shell
cd /share/apps/modulefiles/cp2k && vim 2024.1
cd /share/apps/modulefiles/cp2k && vim 2024.1

新增文件内容如下:

#%Module###########################################
set modulefilename "CP2K"
set modulefilever  "2024.1"

set min_tcl_ver 8.4
if { $tcl_version < $min_tcl_ver } {
    puts stderr " "
    puts stderr "ERROR: This modulefile requires tcl $min_tcl_ver or greater."
    puts stderr "Your system reports that tclsh version $tcl_version is installed."
    exit 1
}

if { [ module-info mode load ] } {
    puts stderr "Loading $modulefilename version $modulefilever"
}

if { [ module-info mode ] == "unload" || [ module-info mode ] == "remove" } {
    puts stderr "Removing $modulefilename version $modulefilever"
    puts stderr "Use  to view any remaining dependent modules."
}

prepend-path PATH /share/apps/cp2k/2024.1/bin/
source-sh bash /share/apps/cp2k/2024.1/install/setup
setenv CP2K_DATA_DIR /share/apps/cp2k/2024.1/data
#%Module###########################################
set modulefilename "CP2K"
set modulefilever  "2024.1"

set min_tcl_ver 8.4
if { $tcl_version < $min_tcl_ver } {
    puts stderr " "
    puts stderr "ERROR: This modulefile requires tcl $min_tcl_ver or greater."
    puts stderr "Your system reports that tclsh version $tcl_version is installed."
    exit 1
}

if { [ module-info mode load ] } {
    puts stderr "Loading $modulefilename version $modulefilever"
}

if { [ module-info mode ] == "unload" || [ module-info mode ] == "remove" } {
    puts stderr "Removing $modulefilename version $modulefilever"
    puts stderr "Use  to view any remaining dependent modules."
}

prepend-path PATH /share/apps/cp2k/2024.1/bin/
source-sh bash /share/apps/cp2k/2024.1/install/setup
setenv CP2K_DATA_DIR /share/apps/cp2k/2024.1/data

保存后,设置一下权限:

shell
chmod -R 755 /share/apps/modulefiles
chmod -R 755 /share/apps/modulefiles

集群上面的用户可以通过如下的命令来查看该模块:

[root@sonmi cp2k]# module avail 
 -------------------- /share/apps/modulefiles --------------------
cp2k/2024.1
[root@sonmi cp2k]# module avail 
 -------------------- /share/apps/modulefiles --------------------
cp2k/2024.1

通过执行下面的命令来加载该模块:

shell
module load cp2k/2024.1
module load cp2k/2024.1

使用示例

在用户目录下创建一个 cp2k-example 文件夹,从 CP2K 源码中自带的 Benchmark 文件夹中复制 QS_LiH_HFX 例子。

shell
mkdir ~/cp2k-example && cd ~/cp2k-example
cp -r ~/cp2k-2024.1/benchmarks/QS_LiH_HFX/* .
mkdir ~/cp2k-example && cd ~/cp2k-example
cp -r ~/cp2k-2024.1/benchmarks/QS_LiH_HFX/* .

并创建一个提交脚本:

shell
vim submit.sh
vim submit.sh

脚本内容如下:

#!/bin/bash
#SBATCH --job-name=cp2k
#SBATCH --partition=sonmi
#SBATCH --output=%j.out
#SBATCH --error=%j.err
#SBATCH --nodelist=compute-0-[0-1]
#SBATCH --exclusive
#SBATCH --time=02:00:00

ulimit -s unlimited
ulimit -l unlimited

module load cp2k/2024.1
source /opt/intel/oneapi/setvars.sh
cd $SLURM_SUBMIT_DIR

mpirun -np 12 cp2k.popt input_bulk_B88_3.inp
#!/bin/bash
#SBATCH --job-name=cp2k
#SBATCH --partition=sonmi
#SBATCH --output=%j.out
#SBATCH --error=%j.err
#SBATCH --nodelist=compute-0-[0-1]
#SBATCH --exclusive
#SBATCH --time=02:00:00

ulimit -s unlimited
ulimit -l unlimited

module load cp2k/2024.1
source /opt/intel/oneapi/setvars.sh
cd $SLURM_SUBMIT_DIR

mpirun -np 12 cp2k.popt input_bulk_B88_3.inp

使用下面命令提交计算任务:

shell
sbatch submit.sh
sbatch submit.sh

参考链接

  1. CP2K 官网
  2. CP2K Github 仓库

本站内容未经授权禁止转载
联系邮箱: [email protected]