본문 바로가기

LAMMPS

Python Shared Library 설치

LAMMPS에서 Python을 사용하려면, 라이브러리를 설치해야 한다.

 

먼저 만들어진 build의 찌꺼기들이 있으면 삭제,

ninja uninstall
xargs sudo rm < install_manifest.txt
rm -rf *

 

AOCC, AOCL사용을 위한 모듈로드

:~  module load aocl-linux-aocc-4.2.0/aocc/aocl-linux-aocc-4.2.0_module
:~  module load aocc-compiler-5.0.0_module
:~  module list
Currently Loaded Modulefiles:
 1) aocl-linux-aocc-4.2.0/aocc/aocl-linux-aocc-4.2.0_module   2) aocc-compiler-5.0.0_module
:~  which clang
/opt/AMD/aocc-compiler-5.0.0/bin/clang
:~  which clang++
/opt/AMD/aocc-compiler-5.0.0/bin/clang++
:~  which flang
/opt/AMD/aocc-compiler-5.0.0/bin/flang

 

 

우선 cmake build 옵션부터 설정

lammps의 실행화일명은 세팅하지 않도록 한다. 실행화일명을 바꾸면, 파이썬 모듈명까지 default값에서 바뀌므로 패스 잡는게 피곤해 진다.

cmake ../cmake \
  -G Ninja \
  -D BUILD_MPI=ON \
  -D BUILD_OMP=ON \
  -D CMAKE_C_COMPILER=/opt/openmpi/aocc/5.0.6/bin/mpicc \
  -D CMAKE_CXX_COMPILER=/opt/openmpi/aocc/5.0.6/bin/mpicxx \
  -D CMAKE_Fortran_COMPILER=/opt/openmpi/aocc/5.0.6/bin/mpifort \
  -D CMAKE_C_FLAGS="-Ofast -march=native" \
  -D CMAKE_CXX_FLAGS="-Ofast -march=native" \
  -D CMAKE_Fortran_FLAGS="-Ofast -march=native" \
  -D PKG_OPENMP=yes \
  -D PKG_GRANULAR=yes \
  -D PKG_VTK=yes \
  -D PKG_RIGID=yes \
  -D PKG_MOLECULE=yes \
  -D PKG_PYTHON=yes \
  -D Python_EXECUTABLE=/usr/bin/python3 \
  -D BUILD_SHARED_LIBS=yes \
  -D VTK_DIR=$HOME/VTK_gcc_install/lib/cmake/vtk-9.4 \
  -D CMAKE_INSTALL_PREFIX=~/LAMMPS_AOCCP \
  -D LAMMPS_BLAS_LIB="-L/opt/AMD/aocl/aocl-linux-aocc-4.2.0/aocc/lib_LP64 -lblis-mt -lflame-mt -lpthread -lm" \
  -D LAMMPS_LAPACK_LIB="-L/opt/AMD/aocl/aocl-linux-aocc-4.2.0/aocc/lib_LP64 -lblis-mt -lflame-mt -lpthread -lm"  
  
  # -D LAMMPS_MACHINE=AOCCP \
  
  ninja -j 4
  ninja install
  ninja install-python

 

파이썬 라이브러리 인스톨 과정에서 에러가 뜬다. 이는 우분투 24 버젼부터는 시스템 파이썬을 건드리는 것을 불허하기 때문이다.

Successfully built lammps-2024.11.19-cp312-cp312-linux_x86_64.whl
Building new binary wheel
Purging existing wheels...

Installing wheel into system site-packages folder
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

    See /usr/share/doc/python3.12/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
You need to uninstall the LAMMPS python module manually first.

ninja: build stopped: subcommand failed.

 

다음과 같은 작업을 해보자

Build 디렉토리에서 whl파일을 우선 찾자

:~/Downloads/lammps/build_AOCC_Python  find . -name '*.whl'
./lammps-2024.11.19-cp312-cp312-linux_x86_64.whl

 

 

파이썬 가상환경 venv하 에서 LAMMPS 파이썬 라이브러리를 설치하기 위해서 모듈을 세팅해 보자

python3 -m venv ~/my_lammps_venv
source ~/my_lammps_venv/bin/activate

ninja install-python

removing build/bdist.linux-x86_64/wheel
Successfully built lammps-2024.11.19-cp312-cp312-linux_x86_64.whl
Building new binary wheel
Purging existing wheels...

Installing wheel into virtual environment
Processing ./lammps-2024.11.19-cp312-cp312-linux_x86_64.whl
Installing collected packages: lammps
  Attempting uninstall: lammps
    Found existing installation: lammps 2024.11.19
    Uninstalling lammps-2024.11.19:
      Successfully uninstalled lammps-2024.11.19
Successfully installed lammps-2024.11.19

deactivate

 

deactivate하면 가상환경이 종료된다. 다시 가상환경을 실행하려면,

source ~/my_lammps_venv/bin/activate

 

모듈로 실행해 보려 했는데 복잡하니 그냥 source 쓰자

 

테스트 방법은 가상환경하에서

(my_lammps_venv) 2025-01-28 21:52:04 : python -c "import lammps; lmp = lammps.lammps(); print(lmp.version())"
LAMMPS (19 Nov 2024 - Development - patch_19Nov2024-439-g0767de7eeb)
OMP_NUM_THREADS environment is not set. Defaulting to 1 thread. (src/comm.cpp:99)
  using 1 OpenMP thread(s) per MPI task
20241119
Total wall time: 0:00:00

 

더 심오한 스크립트

# 1. Setup simulation environments
units            si
dimension        3
atom_style       hybrid sphere molecular

boundary         f f f
newton           off
processors       * * * # grid numa

variable         dimB equal 0.01                                  # input value #
variable         dimL equal 0.01                                  # input value #
variable         dimH equal 0.01                                  # input value #

variable         boxScale equal v_dimB/0.05

# 5. Setup Domain
variable         domainXlo equal -0.033*(v_boxScale)
variable         domainXhi equal  0.033*(v_boxScale)
variable         domainYlo equal -0.033*(v_boxScale)
variable         domainYhi equal  0.033*(v_boxScale)
variable         domainZlo equal -0.033*(v_boxScale)
variable         domainZhi equal  0.20 *(v_boxScale)

variable x1 equal -(v_dimB/2)
variable x2 equal (v_dimB/2)
variable y1 equal -(v_dimL/2)
variable y2 equal (v_dimL/2)
variable z equal -(v_dimH/2)

##

python loop input 1 SELF format p here """
def loop(lmpptr):
  from lammps import lammps
  lmp = lammps(ptr=lmpptr)

  for i in range(5) :
    print ("ak")
"""

python loop invoke