Skip to content

Commit

Permalink
Merge pull request #6 from mir-group/develop
Browse files Browse the repository at this point in the history
v0.5.1
  • Loading branch information
Linux-cpp-lisp authored Mar 8, 2022
2 parents 80cd63e + bbae0c5 commit 86d44d2
Show file tree
Hide file tree
Showing 19 changed files with 5,193 additions and 32 deletions.
69 changes: 69 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Run LAMMPS-Python tests

on:
push:
branches:
- main
- develop

pull_request:
branches:
- main

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.9]
torch-version: [1.10.0]
nequip-branch: ["develop"]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
env:
TORCH: "${{ matrix.torch-version }}"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
python -m pip install --upgrade pip
pip install torch==${TORCH} -f https://download.pytorch.org/whl/cpu/torch_stable.html
pip install pytest
pip install pytest-xdist[psutil]
pip install mkl-include
- name: Install NequIP
env:
NEQUIP: "${{ matrix.nequip-branch }}"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
pip install "https://github.com/mir-group/nequip/archive/${NEQUIP}.zip"
- name: Fetch and build LAMMPS
run: |
mkdir lammps_dir/
cd lammps_dir/
wget -nv "https://github.com/lammps/lammps/archive/release.zip"
unzip -q release.zip
mv lammps-release/ lammps/
rm release.zip
cd ..
./patch_lammps.sh lammps_dir/lammps/
cd lammps_dir/lammps/
mkdir build/
cd build/
cmake ../cmake -DCMAKE_PREFIX_PATH=`python -c 'import torch;print(torch.utils.cmake_prefix_path)'` -DMKL_INCLUDE_DIR=`python -c "import sysconfig;from pathlib import Path;print(Path(sysconfig.get_paths()[\"include\"]).parent)"`
make -j$(nproc)
echo "LAMMPS in dir"
pwd
ls lmp
cd ../../..
echo "Now in dir"
pwd
- name: Run tests with pytest
run: |
# See https://github.com/pytest-dev/pytest/issues/1075
PYTHONHASHSEED=0 LAMMPS=`pwd`/lammps_dir/lammps/build/lmp pytest -n auto tests/
60 changes: 59 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,62 @@
*.out
*.app

.vscode/
.vscode/*
!.vscode/settings.json


# ---------- Python .gigignores-----------

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Sphinx documentation
docs/_build/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"editor.formatOnSave": false,
"[python]": {
"editor.formatOnSave": true
},
"python.formatting.provider": "black"
}
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ Most recent change on the bottom.

## [Unreleased]

## [0.5.1]
### Added
- Full suite of unit tests

### Fixed
- edge duplication issue

## [0.5.0] - 11/24/2021

### Changed
- The mapping from LAMMPS to NequIP types is now explicitly provided
- Don't print full config in LAMMPS log
Expand Down
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ This pair style allows you to use NequIP models in LAMMPS simulations.

## Pre-requisites

* NequIP >= 0.4.0
* Pytorch or LibTorch >= 1.9.0
* PyTorch or LibTorch >= 1.10.0

## Usage in LAMMPS

Expand All @@ -17,8 +16,8 @@ pair_coeff * * deployed.pth <type name 1> <type name 2> ...
```
where `deployed.pth` is the filename of your trained model.

The names after the model path indicate, in order, the names of the NequIP model's atom types to use for LAMMPS atom types 1, 2, and so on. The number of names given must be equal to the number of atom types in the lammps configuration (not the NequIP model!).
The element string should be consistent with the names specified in the NequIP training YAML in `chemical_symbol_to_type` or `type_names`.
The names after the model path `deployed.pth` indicate, in order, the names of the NequIP model's atom types to use for LAMMPS atom types 1, 2, and so on. The number of names given must be equal to the number of atom types in the LAMMPS configuration (not the NequIP model!).
The given names must be consistent with the names specified in the NequIP training YAML in `chemical_symbol_to_type` or `type_names`.

## Building LAMMPS with this pair style

Expand Down Expand Up @@ -73,13 +72,22 @@ cmake ../cmake -DCMAKE_PREFIX_PATH=/path/to/libtorch
```
CMake will look for MKL and, optionally, CUDA and cuDNN. You may have to explicitly provide the path for your CUDA installation (e.g. `-DCUDA_TOOLKIT_ROOT_DIR=/usr/lib/cuda/`) and your MKL installation (e.g. `-DMKL_INCLUDE_DIR=/usr/include/`).

Note that the CUDA that comes with PyTorch when installed with `conda` (the `cudatoolkit` package) may not be sufficient (see [here](https://github.com/pytorch/extension-cpp/issues/26), for example) and you may have to install full CUDA seperately.

Pay attention to warnings and error messages.

**MKL:** If `MKL_INCLUDE_DIR` is not found and you are using a Python environment, a simple solution is to run `conda install mkl-include` or `pip install mkl-include` and append:
```
-DMKL_INCLUDE_DIR="$CONDA_PREFIX/include"
```
to the `cmake` command if using a `conda` environment, or
```
-DMKL_INCLUDE_DIR=`python -c "import sysconfig;from pathlib import Path;print(Path(sysconfig.get_paths()[\"include\"]).parent)"`
```
if using plain Python and `pip`.

**CUDA:** Note that the CUDA that comes with PyTorch when installed with `conda` (the `cudatoolkit` package) is usually insufficient (see [here](https://github.com/pytorch/extension-cpp/issues/26), for example) and you may have to install full CUDA seperately. A minor version mismatch between the available full CUDA version and the version of `cudatoolkit` is usually *not* a problem, as long as the system CUDA is equal or newer. (For example, PyTorch's requested `cudatoolkit==11.3` with a system CUDA of 11.4 works, but a system CUDA 11.1 will likely fail.)

### Build LAMMPS
```bash
make -j$(nproc)
```
This gives `lammps/build/lmp`, which can be run as usual with `/path/to/lmp -in in.script`. If you specify `-DCMAKE_INSTALL_PREFIX=/somewhere/in/$PATH` (the default is `$HOME/.local`), you can do `make install` and just run `lmp -in in.script`.

13 changes: 8 additions & 5 deletions pair_nequip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
// (see https://github.com/pytorch/pytorch/blob/dfbd030854359207cb3040b864614affeace11ce/torch/csrc/jit/api/module.cpp#L479)
// is wrong, and we have ro "reimplement" the function
// to get around that...
// it's broken in 1.8 and 1.9 so the < check is correct.
// This appears to be fixed in 1.10.
#if (TORCH_VERSION_MAJOR == 1 && TORCH_VERSION_MINOR < 10)
// it's broken in 1.8 and 1.9
// BUT the internal logic in the function is wrong in 1.10
// So we only use torch::jit::freeze in >=1.11
#if (TORCH_VERSION_MAJOR == 1 && TORCH_VERSION_MINOR <= 10)
#define DO_TORCH_FREEZE_HACK
// For the hack, need more headers:
#include <torch/csrc/jit/passes/freeze_module.h>
Expand Down Expand Up @@ -194,7 +195,8 @@ void PairNEQUIP::coeff(int narg, char **arg) {
auto out_mod = freeze_module(
model, {}
);
auto graph = model.get_method("forward").graph();
// See 1.11 bugfix in https://github.com/pytorch/pytorch/pull/71436
auto graph = out_mod.get_method("forward").graph();
OptimizeFrozenGraph(graph, optimize_numerics);
model = out_mod;
#else
Expand Down Expand Up @@ -394,14 +396,15 @@ void PairNEQUIP::compute(int eflag, int vflag){
edge_counter++;

if (debug_mode){
printf("%d %d %g %g %g %g %g %g %g %g %g %g\n", itag-1, jtag-1,
printf("%d %d %.10g %.10g %.10g %.10g %.10g %.10g %.10g %.10g %.10g %.10g\n", itag-1, jtag-1,
pos[itag-1][0],pos[itag-1][1],pos[itag-1][2],pos[jtag-1][0],pos[jtag-1][1],pos[jtag-1][2],
e_vec[0],e_vec[1],e_vec[2],sqrt(rsq));
}

}
}
}
if (debug_mode) printf("end NEQUIP edges\n");

// shorten the list before sending to nequip
torch::Tensor edges_tensor = torch::zeros({2,edge_counter}, torch::TensorOptions().dtype(torch::kInt64));
Expand Down
19 changes: 0 additions & 19 deletions test/check_result.py

This file was deleted.

90 changes: 90 additions & 0 deletions tests/test_data/Cu-cubic.xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.017391652023890813 free_energy=-0.017391652023890813 pbc="T T T"
Cu 0.01764052 0.00400157 0.00978738 -0.08717844 0.04962473 -0.12179229 -0.00472427
Cu 0.02240893 1.82367558 1.79522722 -0.12285307 -0.13375551 0.17188324 -0.00362717
Cu 1.81450088 -0.00151357 1.80396781 0.08501339 0.09035482 0.10694376 -0.00469092
Cu 1.80910599 1.80644044 0.01454274 0.12501812 -0.00622404 -0.15703471 -0.00434929
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.015905094412799414 free_energy=-0.015905094412799414 pbc="T T T"
Cu 0.01624345 -0.00611756 -0.00528172 -0.06246748 0.14140106 -0.03262814 -0.00437084
Cu -0.01072969 1.81365408 1.78198461 0.13723509 -0.12500316 0.07279520 -0.00412798
Cu 1.82244812 -0.00761207 1.80819039 -0.11120998 0.15286120 -0.12108536 -0.00357991
Cu 1.80250630 1.81962108 -0.02060141 0.03644236 -0.16925910 0.08091829 -0.00382637
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.01528731937769301 free_energy=-0.01528731937769301 pbc="T T T"
Cu -0.00416758 -0.00056267 -0.02136196 0.01148580 -0.04169084 0.08236991 -0.00512480
Cu 0.01640271 1.78706564 1.79658253 -0.14132134 0.08420319 0.07401279 -0.00350296
Cu 1.81002881 -0.01245288 1.79442048 0.01247261 0.04689613 0.08984380 -0.00391347
Cu 1.79590992 1.81051454 0.02292208 0.11736294 -0.08940848 -0.24622650 -0.00274609
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.01887755101138211 free_energy=-0.01887755101138211 pbc="T T T"
Cu 0.01788628 0.00436510 0.00096497 -0.15444665 -0.09501731 -0.02555193 -0.00454935
Cu -0.01863493 1.80222612 1.80145241 0.11584357 0.01712771 0.06618692 -0.00503852
Cu 1.80417259 -0.00627001 1.80456182 0.00463158 -0.01619046 0.04319703 -0.00488369
Cu 1.80022782 1.79186135 0.00884622 0.03397150 0.09408005 -0.08383203 -0.00440599
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.020128104148323445 free_energy=-0.020128104148323445 pbc="T T T"
Cu 0.00050562 0.00499951 -0.00995909 -0.07686553 -0.03193870 0.02688282 -0.00520543
Cu 0.00693599 1.80081698 1.78915423 -0.12450754 0.07391750 0.07765346 -0.00475940
Cu 1.79852293 0.00598575 1.80832250 0.08218191 -0.03926083 -0.06416069 -0.00518652
Cu 1.79352523 1.81118670 -0.00087987 0.11919115 -0.00271797 -0.04037558 -0.00497675
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.020154675346251594 free_energy=-0.020154675346251594 pbc="T T T"
Cu 0.00441227 -0.00330870 0.02430771 -0.08239901 -0.01596948 -0.11560842 -0.00482879
Cu -0.00252092 1.80609610 1.82082481 -0.03096779 -0.04175481 -0.03348025 -0.00521007
Cu 1.79590768 -0.00591637 1.80687603 0.07820172 0.00326506 0.06975380 -0.00487497
Cu 1.80170130 1.79307235 -0.00204877 0.03516508 0.05445923 0.07933486 -0.00524084
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.01482573303385415 free_energy=-0.01482573303385415 pbc="T T T"
Cu -0.00311784 0.00729004 0.00217821 0.05397007 -0.04615605 0.08312341 -0.00388045
Cu -0.00899092 1.78013219 1.81413252 0.09728569 0.15265900 -0.04201865 -0.00425637
Cu 1.81627064 -0.01514093 1.82139291 -0.13314014 0.11989754 -0.09607516 -0.00301817
Cu 1.80070106 1.83131281 0.00601822 -0.01811562 -0.22640049 0.05497041 -0.00367074
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.021005203893530222 free_energy=-0.021005203893530222 pbc="T T T"
Cu 0.01690526 -0.00465937 0.00032820 -0.10681903 -0.01566386 0.03841762 -0.00519367
Cu 0.00407516 1.79711077 1.80502066 -0.01195269 -0.02590150 -0.00836358 -0.00557351
Cu 1.80499110 -0.01754724 1.81517658 0.08162914 0.07959041 -0.08354024 -0.00494696
Cu 1.81100499 1.79874571 -0.00171548 0.03714257 -0.03802506 0.05348620 -0.00529107
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.004108154437133749 free_energy=-0.004108154437133749 pbc="T T T"
Cu 0.00091205 0.01091283 -0.01946970 0.10455948 -0.22944333 0.27304574 -0.00116755
Cu -0.01386350 1.78203508 1.82909834 0.21498056 0.31338220 -0.18956466 0.00087501
Cu 1.82227836 0.02204556 1.81294828 -0.18865295 -0.31150667 -0.06864456 -0.00045239
Cu 1.81476421 1.79316573 0.01916364 -0.13088708 0.22756779 -0.01483652 -0.00336323
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.019378981209557722 free_energy=-0.019378981209557722 pbc="T T T"
Cu 0.00001109 -0.00289544 -0.01116066 -0.08585694 0.03365348 0.05228967 -0.00526449
Cu -0.00012883 1.80121639 1.80018865 -0.08489222 -0.00366840 0.06224814 -0.00489394
Cu 1.78982669 -0.00490872 1.80259319 0.11746446 0.04869625 0.04446378 -0.00464278
Cu 1.79852053 1.81135891 0.01740117 0.05328471 -0.07868133 -0.15900158 -0.00457777
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.02045229620204303 free_energy=-0.02045229620204303 pbc="T T T"
Cu 0.01331587 0.00715279 -0.01545400 -0.09836036 -0.01328786 0.08680352 -0.00509484
Cu -0.00008384 1.81121336 1.79779914 0.00068210 -0.01608512 0.04158748 -0.00522638
Cu 1.80765512 0.00108549 1.80504291 0.03263030 0.03155436 -0.01202871 -0.00515197
Cu 1.80325400 1.80933026 0.01203037 0.06504795 -0.00218138 -0.11636230 -0.00497910
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.018290814060376803 free_energy=-0.018290814060376803 pbc="T T T"
Cu 0.01749455 -0.00286073 -0.00484565 -0.19061190 -0.01456145 0.04251433 -0.00438217
Cu -0.02653319 1.80491715 1.80180369 0.13511600 0.00423319 -0.01469846 -0.00510239
Cu 1.79963371 0.00315403 1.80921051 0.00818829 -0.05888333 -0.06960037 -0.00441341
Cu 1.79434397 1.79613760 -0.00475733 0.04730761 0.06921159 0.04178450 -0.00439284
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.014445625580108867 free_energy=-0.014445625580108867 pbc="T T T"
Cu 0.00472986 -0.00681426 0.00242439 0.08237017 0.05878299 -0.11269540 -0.00449715
Cu -0.01700736 1.81253143 1.78965279 0.24383637 -0.08787004 0.14938555 -0.00237017
Cu 1.80505127 -0.00120228 1.79693018 -0.05672510 0.01724830 0.09417810 -0.00506256
Cu 1.83371819 1.79902177 0.00472457 -0.26948144 0.01183874 -0.13086824 -0.00251574
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.0176633526467036 free_energy=-0.0176633526467036 pbc="T T T"
Cu -0.00712391 0.00753766 -0.00044503 0.06557173 -0.03798336 0.08665333 -0.00473434
Cu 0.00451812 1.81845102 1.81032338 -0.02074291 -0.03687740 -0.09653905 -0.00501509
Cu 1.81850188 0.00861211 1.81978686 -0.11099053 -0.04625615 -0.16681003 -0.00395624
Cu 1.79454623 1.79711011 -0.01261606 0.06616171 0.12111690 0.17669574 -0.00395768
4
Lattice="3.61 0.0 0.0 0.0 3.61 0.0 0.0 0.0 3.61" Properties=species:S:1:pos:R:3:forces:R:3:energies:R:1 energy=-0.018288343723298794 free_energy=-0.018288343723298794 pbc="T T T"
Cu 0.01551339 0.00079186 0.00173977 -0.13521265 -0.09341389 -0.02995679 -0.00426431
Cu -0.00072337 1.78495671 1.80644678 -0.01536463 0.16563008 -0.00209572 -0.00447119
Cu 1.78998831 0.00211109 1.79941795 0.17102355 -0.10330832 0.04999955 -0.00401561
Cu 1.81584529 1.80313711 0.00014661 -0.02044627 0.03109214 -0.01794704 -0.00553723
Loading

0 comments on commit 86d44d2

Please sign in to comment.