---
# System prepended metadata

title: LAMMPS整理
tags: [lammps]

---

# LAMMPS整理
###### tags:`lammps`
## 簡介
LAMMPS為大型分子動力學模擬軟體，由經驗公式(力場/勢能)加上一些系統資訊([系綜](https://zh.wikipedia.org/wiki/%E7%B3%BB%E7%BB%BC)、邊界條件)、結構藉由牛頓運動方程去計算原子位置、速度及加速度(因計算量比第一原理少，所以可計算較大尺度的分子模擬)

* 分子動力學模擬基本流程
![](https://i.imgur.com/16MrZUF.png)
![](https://i.imgur.com/Fr5gOID.png)
![](https://i.imgur.com/uKIMQa0.png)

->分子模擬有分為兩種
* 蒙特卡羅模擬 Monte Carlo->藉由整體勢能方程對box裡的原子作隨機移動，在判斷是否能接受，以機率的方式模擬，與波茲曼分配有關，只能模擬平衡態體系
[蒙地卡羅方法-維基](https://zh.wikipedia.org/wiki/%E8%92%99%E5%9C%B0%E5%8D%A1%E7%BE%85%E6%96%B9%E6%B3%95)
* 分子動力學模擬 -> 基於牛頓運動方程式計算原子間的力，產生加速度並位移，然後再計算力，重複此步驟

## lammps流程
三大input檔:
* in.(主要input檔)
* .data(複雜模型構型->包括原子位置、速度等初始條件)
* POT file/force field file(除lammps本身之外的勢能檔案->可自行編寫)

### main input file script
* initialization=初始系統模擬設置
* system definition=初始模型建構/讀取模型檔案
    * 初始模型建構->建立cell，再建立內部原子資訊模型建構
    * 讀取模型檔案->會有模型的topo檔(pdb檔)
* simulation settings
    * 定義原子間相互作用或設置力場
    * 特定計算設定
    * 輸出設定
* run the simulation=模擬環境設置並運行

#### initialization
根據官網有下列參數設置
units、dimension、newton、processors、boundary、atom_style、atom_modify
##### units
`units style`
style = lj / real / metal / si / cgs / electron / micro / nano
***->主要為單位制定，預設為lj***

##### pair_style
力場/勢函數
[LAMMPS中data文件、pair_style文件和pair_coeff文件 ](http://blog.sciencenet.cn/home.php?mod=space&uid=3437453&do=blog&id=1263434)
[Force Field選擇 ](http://chiustin.blogspot.com/2018/12/force-field.html)

## lammps dockerfile=下載方法

### CPU
正確版本 需要mpi fftw openssh cmake
```shell=
#centos7baseimage
FROM centos:7
#download necessary document
RUN yum -y update &&    yum -y install wget     vim       fftw    mpich   GNUmake make which locate&& yum groupinstall -y 'Development Tools' && yum clean all
#openssl
ENV PATH=$PATH:/usr/lib64/mpich/bin
RUN mkdir /usr/local/openssl && \
    cd /usr/local/openssl && \
    wget https://www.openssl.org/source/openssl-1.1.1i.tar.gz && \
    tar xvf openssl-1.1.1i.tar.gz && \
    cd /usr/local/openssl/openssl-1.1.1i && \
    ./config --prefix=/usr/local/openssl && \
    make -j16 && \
    make install && \
    rm -rf /usr/bin/openssl && \
    ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl && \
    ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1 && \
    ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1 && \
    yum install -y openssl-devel && \
#cmake
    mkdir /usr/local/cmake && \
    cd /usr/local/cmake/ && \
    wget https://github.com/Kitware/CMake/releases/download/v3.20.0-rc2/cmake-3.20.0-rc2.tar.gz && \
    tar xvf cmake-3.20.0-rc2.tar.gz && \
    cd cmake-3.20.0-rc2/ && \
    ./bootstrap --prefix=/usr/local/cmake && \
    gmake -j16 && \
    gmake install && \
    ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake && \
    ln -s /usr/local/cmake/bin/cpack /usr/bin/cpack && \
    ln -s /usr/local/cmake/bin/ctest /usr/bin/ctest && \
    cmake --version && \
#lammps
    cd /opt &&\
    git clone -b stable https://github.com/lammps/lammps.git lammps && \
    cd /opt/lammps && \
    mkdir /opt/lammps/build && \
    cd /opt/lammps/build && \
    cmake /opt/lammps/cmake && \
    cmake --build . && \
    ENV PATH=$PATH:/opt/lammps/build 
CMD ["/bin/bash"]

```
singularity def(mpich、openssl、cmake、lammps)
```shell=
Bootstrap: docker
From: centos:7

%post
        yum -y update &&    yum -y install wget git vim       fftw    mpich   GNUmake make which locate gcc gcc-c++ && yum clean all
        #openssl
        PATH=$PATH:/usr/lib64/mpich/bin
        mkdir /usr/local/openssl
        cd /usr/local/openssl
        wget https://www.openssl.org/source/openssl-1.1.1i.tar.gz
        tar xvf openssl-1.1.1i.tar.gz
        cd /usr/local/openssl/openssl-1.1.1i
        ./config --prefix=/usr/local/openssl
        make -j16
        make install
        rm -rf /usr/bin/openssl
        ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
        ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
        ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
        yum install -y openssl-devel
        #cmake
        mkdir /usr/local/cmake
        cd /usr/local/cmake/
        wget https://github.com/Kitware/CMake/releases/download/v3.20.0-rc2/cmake-3.20.0-rc2.tar.gz
        tar xvf cmake-3.20.0-rc2.tar.gz
        cd cmake-3.20.0-rc2/
        ./bootstrap --prefix=/usr/local/cmake
        gmake -j16
        gmake install
        ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake
        ln -s /usr/local/cmake/bin/cpack /usr/bin/cpack
        ln -s /usr/local/cmake/bin/ctest /usr/bin/ctest
        cmake --version
        #lammps
        cd /opt
        git clone -b stable https://github.com/lammps/lammps.git lammps
        cd /opt/lammps
        mkdir /opt/lammps/build
        cd /opt/lammps/build
        cmake /opt/lammps/cmake
        cmake --build .
%environment
        export PATH=$PATH:/opt/lammps/build
        export PATH=$PATH:/usr/lib64/mpich/bin
%runscript
        exec /bin/bash /bin/bash "$@"
%startscript
        exec /bin/bash /bin/bash "$@"


```
singularity def(openmpi3.1.4)
```
Bootstrap: docker
From: centos:7

%post
        yum -y update &&    yum -y install wget git vim       fftw    mpich   GNUmake make which locate gcc gcc-c++ && yum clean all
        cd /
        wget https://download.open-mpi.org/release/open-mpi/v3.1/openmpi-3.1.4.tar.gz
        tar xvf openmpi-3.1.4.tar.gz
        cd /openmpi-3.1.4
        ./configure --prefix=/openmpi
        make
        make install
        PATH=$PATH:/openmpi/bin
        LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/openmpi/lib
        mkdir /usr/local/openssl
        cd /usr/local/openssl
        wget https://www.openssl.org/source/openssl-1.1.1i.tar.gz
        tar xvf openssl-1.1.1i.tar.gz
        cd /usr/local/openssl/openssl-1.1.1i
        ./config --prefix=/usr/local/openssl
        make -j16
        make install
        rm -rf /usr/bin/openssl
        ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
        ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
        ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
        yum install -y openssl-devel
        #cmake
        mkdir /usr/local/cmake
        cd /usr/local/cmake/
        wget https://github.com/Kitware/CMake/releases/download/v3.20.0-rc2/cmake-3.20.0-rc2.tar.gz
        tar xvf cmake-3.20.0-rc2.tar.gz
        cd cmake-3.20.0-rc2/
        ./bootstrap --prefix=/usr/local/cmake
        gmake -j16
        gmake install
        ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake
        ln -s /usr/local/cmake/bin/cpack /usr/bin/cpack
        ln -s /usr/local/cmake/bin/ctest /usr/bin/ctest
        cmake --version
        #lammps
        cd /opt
        git clone -b stable https://github.com/lammps/lammps.git lammps
        cd /opt/lammps
        mkdir /opt/lammps/build
        cd /opt/lammps/build
        cmake /opt/lammps/cmake
        cmake --build .
%environment
        export PATH=$PATH:/opt/lammps/build
        export PATH=$PATH:/openmpi/bin
        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/openmpi/lib
%runscript
        exec /bin/bash /bin/bash "$@"
%startscript
        exec /bin/bash /bin/bash "$@"

```
### GPU編譯
[Install of GPU package for LAMMPS](https://zhuanlan.zhihu.com/p/32648883)
[LAMMPS | NVIDIA NGC](https://ngc.nvidia.com/catalog/containers/hpc:lammps)
[GPU package](https://lammps.sandia.gov/doc/Speed_gpu.html)
[Centos 安裝GPU并行lammps](https://zh-hant.hotbak.net/key/gpulammpscentos%E5%AE%89%E8%A3%9D%E5%B9%B6%E8%A1%8Cl.html)
[Centos 安装GPU并行lammps](https://blog.csdn.net/weixin_43205202/article/details/88261607)
[Centos install GPU parallel lammps](https://programmersought.com/article/3305913470/)
[CMake build](https://lammps.sandia.gov/doc/Build_extras.html#cmake-build)
下載
需先下載cuda、 cmake
nvcc是cuda的編譯器
[cuda](https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&=CentOS&target_version=7&target_type=rpm_network)
```shell=
sudo yum-config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/rhel7/x86_64/cuda-rhel7.repo
sudo yum clean all
sudo yum -y install nvidia-driver-latest-dkms cuda
sudo yum -y install cuda-drivers
```
主要參考
https://lammps.sandia.gov/doc/Build_package.html
:::info
流程
裝CUDA(會幫你裝GPU driver)然後CMAKE
:::
[Lammps的CUDA加速](https://zhuanlan.zhihu.com/p/56353024)
下載的時候大量出現以下訊息(cuda11)
cmake指令為
```
cmake -C ../cmake/presets/most.cmake -C ../cmake/presets/nolib.cmake -D PKG_GPU=on -D GPU_API=cuda -D GPU_ARCH=sm_61 ../cmake
cmake --build .
```
![](https://i.imgur.com/e9AJYNK.png)
參考[[Feature Request] Modernize CUDA support in GPU package](https://github.com/lammps/lammps/issues/2355)
推測可能為cuda版本關係
完成後測試
```
mpirun -np 8 lmp -sf gpu -pk gpu 1 -in in.crack
```
出現錯誤
![](https://i.imgur.com/vB15b1K.png)
![](https://i.imgur.com/FLnQR0G.png)
重複出現
主要是
`WARNING: Temperature for thermo pressure is not for group all (src/thermo.cpp:472)`
->看起來是input的關係(待查明)
`ERROR: Cannot use neigh_modify exclude with GPU neighbor builds (src/GPU/fix_gpu.cpp:254)`
->lammps官方給出的原因是
```
Cannot use neigh_modify exclude with GPU neighbor builds
    This is a current limitation of the GPU implementation in LAMMPS.
```
[[lammps-users] Cannot use neigh_modify exclude with GPU neighbor builds](https://lammps.sandia.gov/threads/msg70729.html)
在simulation box define(`region`) 前加一個指令 
```
package gpu 2 neigh no
```
[lammps可能error一覽](https://www.afs.enea.it/software/lammps/doc17/html/Section_errors.html)