Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow using mapping from LAMMPS #38

Merged
merged 9 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,34 @@ dp --pt freeze
A frozen model file named `frozen_model.pth` will be generated. You can use it in the MD packages or other interfaces.
For details, follow [DeePMD-kit documentation](https://docs.deepmodeling.com/projects/deepmd/en/latest/).

### Running LAMMPS + MACE with period boundary conditions

GNN models use message passing neural networks,
so the neighbor list built with traditional cutoff radius will not work,
since the ghost atoms also need to build neighbor list.
By default, the model requests the neighbor list with a cutoff radius of $r_c \times N_{L}$,
where $r_c$ is set by `r_max` and $N_L$ is set by `num_interactions` (MACE) / `num_layers` (NequIP),
and rebuilds the neighbor list for ghost atoms.
However, this approach is very inefficient.

The alternative approach for the MACE model (note: NequIP doesn't support such approach) is to use the mapping passed from LAMMPS, which does not support MPI.
One needs to set `DP_GNN_USE_MAPPING` when freezing the models,

```sh
DP_GNN_USE_MAPPING=1 dp --pt freeze
```

and request the mapping when using LAMMPS (also requires DeePMD-kit v3.0.0rc0 or above).
By using the mapping, the ghost atoms will be mapped to the real atoms,
so the regular neighbor list with a cutoff radius of $r_c$ can be used.

```lammps
atom_modify map array
```

In the future, we will explore utilizing the MPI to communicate the neighbor list,
while this approach requires a deep hack for external packages.

## Parameters

### MACE
Expand Down
5 changes: 5 additions & 0 deletions deepmd_gnn/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Configurations read from environment variables."""

import os

DP_GNN_USE_MAPPING = os.environ.get("DP_GNN_USE_MAPPING", "0") == "1"
11 changes: 11 additions & 0 deletions deepmd_gnn/mace.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
)

import deepmd_gnn.op # noqa: F401
from deepmd_gnn import env as deepmd_gnn_env

ELEMENTS = [
"H",
Expand Down Expand Up @@ -368,6 +369,8 @@
@torch.jit.export
def get_rcut(self) -> float:
"""Get the cut-off radius."""
if deepmd_gnn_env.DP_GNN_USE_MAPPING:
return self.rcut

Check warning on line 373 in deepmd_gnn/mace.py

View check run for this annotation

Codecov / codecov/patch

deepmd_gnn/mace.py#L373

Added line #L373 was not covered by tests
return self.rcut * self.num_interactions

@torch.jit.export
Expand Down Expand Up @@ -527,6 +530,14 @@
nf, nall = extended_atype.shape
# calculate nlist for ghost atoms, as LAMMPS does not calculate it
if mapping is None and self.num_interactions > 1 and nloc < nall:
if deepmd_gnn_env.DP_GNN_USE_MAPPING:
# when setting DP_GNN_USE_MAPPING, ghost atoms are only built
# for one message-passing layer
msg = (

Check warning on line 536 in deepmd_gnn/mace.py

View check run for this annotation

Codecov / codecov/patch

deepmd_gnn/mace.py#L536

Added line #L536 was not covered by tests
"When setting DP_GNN_USE_MAPPING, mapping is required. "
"If you are using LAMMPS, set `atom_modify map yes`."
)
raise ValueError(msg)

Check warning on line 540 in deepmd_gnn/mace.py

View check run for this annotation

Codecov / codecov/patch

deepmd_gnn/mace.py#L540

Added line #L540 was not covered by tests
nlist = build_neighbor_list(
extended_coord.view(nf, -1),
extended_atype,
Expand Down