Skip to content

Commit

Permalink
lmp: support other units for both pair deepmd and fix dplr (#2800)
Browse files Browse the repository at this point in the history
This PR is intended to do 2 things:
1. support other unit styles like si and nano (except lj);
2. support the non-metal units for fix dplr and compute deeptensor/atom.

Unittests for both features mentioned above have also been added in this
pull request.

---------

Signed-off-by: Yifan Li李一帆 <yifanl0716@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
  • Loading branch information
3 people committed Sep 18, 2023
1 parent 0d5737f commit 19c6a68
Show file tree
Hide file tree
Showing 12 changed files with 406 additions and 176 deletions.
10 changes: 10 additions & 0 deletions doc/third-party/lammps-command.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# LAMMPS commands

## units
All units in LAMMPS except `lj` are supported. `lj` is not supported.

The most commonly used units are `metal`, since the internal units of distance, energy, force, and charge in DeePMD-kit are `\AA`, `eV`, `eV / \AA`, and `proton charge`, respectively. These units are consistent with the `metal` units in LAMMPS.

If one wants to use other units like `real` or `si`, it is welcome to do so. There is no need to do the unit conversion mannualy. The unit conversion is done automatically by LAMMPS.

The only thing that one needs to take care is the unit of the output of `compute deeptensor/atom`. Working with `metal` units for `compute deeptensor/atom` is totally fine, since there is no unit conversion. For other unit styles, we currently assume that the output of the `compute deeptensor/atom` command has the unit of distance and have applied the unit conversion factor of distance. If a user wants to infer quantities with units other than distance, the user is encouraged to open a GitHub feature request, so that the unit conversion factor can be added.

## Enable DeePMD-kit plugin (plugin mode)

If you are using the plugin mode, enable DeePMD-kit package in LAMMPS with `plugin` command:
Expand Down Expand Up @@ -119,6 +128,7 @@ dump 1 all custom 100 water.dump id type c_dipole[1] c_dipole[2] c_di

### Restrictions
- The `deeptensor/atom` compute is provided in the USER-DEEPMD package, which is compiled from the DeePMD-kit, visit the [DeePMD-kit website](https://github.com/deepmodeling/deepmd-kit) for more information.
- For the issue of using a unit style for `compute deeptensor/atom`, refer to the discussions in [units](#units) of this page.


## Long-range interaction
Expand Down
31 changes: 17 additions & 14 deletions source/lmp/compute_deeptensor_atom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ using namespace LAMMPS_NS;

ComputeDeeptensorAtom::ComputeDeeptensorAtom(LAMMPS *lmp, int narg, char **arg)
: Compute(lmp, narg, arg), dp(lmp), tensor(nullptr) {
if (!(strcmp(update->unit_style, "metal") == 0 ||
strcmp(update->unit_style, "real") == 0)) {
error->all(
FLERR,
"Compute deeptensor/atom requires metal or real unit; please set it by "
"\"units metal\" or \"units real\"");
if (strcmp(update->unit_style, "lj") == 0) {
error->all(FLERR,
"Compute deeptensor/atom does not support unit style lj. Please "
"use other "
"unit styles like metal or real unit instead. You may set it by "
"\"units metal\" or \"units real\"");
}

if (narg < 4) {
Expand All @@ -57,6 +57,8 @@ ComputeDeeptensorAtom::ComputeDeeptensorAtom(LAMMPS *lmp, int narg, char **arg)
timeflag = 1;

nmax = 0;

dist_unit_cvt_factor = force->angstrom;
}

/* ---------------------------------------------------------------------- */
Expand Down Expand Up @@ -116,16 +118,17 @@ void ComputeDeeptensorAtom::compute_peratom() {
dtype[ii] = type[ii] - 1;
}
// get box
dbox[0] = domain->h[0]; // xx
dbox[4] = domain->h[1]; // yy
dbox[8] = domain->h[2]; // zz
dbox[7] = domain->h[3]; // zy
dbox[6] = domain->h[4]; // zx
dbox[3] = domain->h[5]; // yx
dbox[0] = domain->h[0] / dist_unit_cvt_factor; // xx
dbox[4] = domain->h[1] / dist_unit_cvt_factor; // yy
dbox[8] = domain->h[2] / dist_unit_cvt_factor; // zz
dbox[7] = domain->h[3] / dist_unit_cvt_factor; // zy
dbox[6] = domain->h[4] / dist_unit_cvt_factor; // zx
dbox[3] = domain->h[5] / dist_unit_cvt_factor; // yx
// get coord
for (int ii = 0; ii < nall; ++ii) {
for (int dd = 0; dd < 3; ++dd) {
dcoord[ii * 3 + dd] = x[ii][dd] - domain->boxlo[dd];
dcoord[ii * 3 + dd] =
(x[ii][dd] - domain->boxlo[dd]) / dist_unit_cvt_factor;
}
}

Expand Down Expand Up @@ -155,7 +158,7 @@ void ComputeDeeptensorAtom::compute_peratom() {
// record when selected and in group
if (selected && ingroup) {
for (int jj = 0; jj < size_peratom_cols; ++jj) {
tensor[ii][jj] = atensor[iter_tensor + jj];
tensor[ii][jj] = atensor[iter_tensor + jj] * dist_unit_cvt_factor;
}
}
// if not selected or not in group set to 0.
Expand Down
1 change: 1 addition & 0 deletions source/lmp/compute_deeptensor_atom.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ComputeDeeptensorAtom : public Compute {
void compute_peratom() override;
double memory_usage() override;
void init_list(int, class NeighList *) override;
double dist_unit_cvt_factor;

private:
int nmax;
Expand Down
59 changes: 38 additions & 21 deletions source/lmp/fix_dplr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ FixDPLR::FixDPLR(LAMMPS *lmp, int narg, char **arg)
qe2f = force->qe2f;
xstyle = ystyle = zstyle = NONE;

if (strcmp(update->unit_style, "metal") != 0) {
error->all(
FLERR,
"Fix dplr requires metal unit, please set it by \"units metal\"");
if (strcmp(update->unit_style, "lj") == 0) {
error->all(FLERR,
"Fix dplr does not support unit style lj. Please use other "
"unit styles like metal or real unit instead. You may set it by "
"\"units metal\" or \"units real\"");
}

int iarg = 3;
Expand Down Expand Up @@ -142,6 +143,9 @@ FixDPLR::FixDPLR(LAMMPS *lmp, int narg, char **arg)
if (!pair_deepmd) {
error->all(FLERR, "pair_style deepmd should be set before this fix\n");
}
ener_unit_cvt_factor = pair_deepmd->ener_unit_cvt_factor;
dist_unit_cvt_factor = pair_deepmd->dist_unit_cvt_factor;
force_unit_cvt_factor = pair_deepmd->force_unit_cvt_factor;

int n = atom->ntypes;
std::vector<std::string> type_names = pair_deepmd->type_names;
Expand Down Expand Up @@ -445,16 +449,17 @@ void FixDPLR::pre_force(int vflag) {
dtype[ii] = type_idx_map[type[ii] - 1];
}
// get box
dbox[0] = domain->h[0]; // xx
dbox[4] = domain->h[1]; // yy
dbox[8] = domain->h[2]; // zz
dbox[7] = domain->h[3]; // zy
dbox[6] = domain->h[4]; // zx
dbox[3] = domain->h[5]; // yx
dbox[0] = domain->h[0] / dist_unit_cvt_factor; // xx
dbox[4] = domain->h[1] / dist_unit_cvt_factor; // yy
dbox[8] = domain->h[2] / dist_unit_cvt_factor; // zz
dbox[7] = domain->h[3] / dist_unit_cvt_factor; // zy
dbox[6] = domain->h[4] / dist_unit_cvt_factor; // zx
dbox[3] = domain->h[5] / dist_unit_cvt_factor; // yx
// get coord
for (int ii = 0; ii < nall; ++ii) {
for (int dd = 0; dd < 3; ++dd) {
dcoord[ii * 3 + dd] = x[ii][dd] - domain->boxlo[dd];
dcoord[ii * 3 + dd] =
(x[ii][dd] - domain->boxlo[dd]) / dist_unit_cvt_factor;
}
}
// get lammps nlist
Expand Down Expand Up @@ -523,9 +528,11 @@ void FixDPLR::pre_force(int vflag) {
int res_idx = sel_fwd[idx0];
// int ret_idx = dpl_bwd[res_idx];
for (int dd = 0; dd < 3; ++dd) {
x[idx1][dd] = x[idx0][dd] + tensor[res_idx * 3 + dd];
x[idx1][dd] =
x[idx0][dd] + tensor[res_idx * 3 + dd] * dist_unit_cvt_factor;
// res_buff[idx1 * odim + dd] = tensor[res_idx * odim + dd];
dipole_recd[idx0 * 3 + dd] = tensor[res_idx * 3 + dd];
dipole_recd[idx0 * 3 + dd] =
tensor[res_idx * 3 + dd] * dist_unit_cvt_factor;
}
}
// cout << "-------------------- fix/dplr: pre force " << endl;
Expand Down Expand Up @@ -568,17 +575,18 @@ void FixDPLR::post_force(int vflag) {
for (int ii = 0; ii < nall; ++ii) {
dtype[ii] = type_idx_map[type[ii] - 1];
}
dbox[0] = domain->h[0]; // xx
dbox[4] = domain->h[1]; // yy
dbox[8] = domain->h[2]; // zz
dbox[7] = domain->h[3]; // zy
dbox[6] = domain->h[4]; // zx
dbox[3] = domain->h[5]; // yx
dbox[0] = domain->h[0] / dist_unit_cvt_factor; // xx
dbox[4] = domain->h[1] / dist_unit_cvt_factor; // yy
dbox[8] = domain->h[2] / dist_unit_cvt_factor; // zz
dbox[7] = domain->h[3] / dist_unit_cvt_factor; // zy
dbox[6] = domain->h[4] / dist_unit_cvt_factor; // zx
dbox[3] = domain->h[5] / dist_unit_cvt_factor; // yx
// get coord
double **x = atom->x;
for (int ii = 0; ii < nall; ++ii) {
for (int dd = 0; dd < 3; ++dd) {
dcoord[ii * 3 + dd] = x[ii][dd] - domain->boxlo[dd];
dcoord[ii * 3 + dd] =
(x[ii][dd] - domain->boxlo[dd]) / dist_unit_cvt_factor;
}
}
// revise force according to efield
Expand All @@ -599,7 +607,7 @@ void FixDPLR::post_force(int vflag) {
for (int ii = 0; ii < nlocal; ++ii) {
double tmpf[3];
for (int dd = 0; dd < 3; ++dd) {
tmpf[dd] = q[ii] * efield[dd];
tmpf[dd] = q[ii] * efield[dd] * force->qe2f;
}
for (int dd = 0; dd < 3; ++dd) {
dfele[ii * 3 + dd] += tmpf[dd];
Expand Down Expand Up @@ -632,8 +640,17 @@ void FixDPLR::post_force(int vflag) {
vector<FLOAT_PREC> dfcorr, dvcorr;
// compute
try {
for (int ii = 0; ii < nlocal * 3; ++ii) {
dfele[ii] /= force_unit_cvt_factor;
}
dtm.compute(dfcorr, dvcorr, dcoord, dtype, dbox, valid_pairs, dfele, nghost,
lmp_list);
for (int ii = 0; ii < nlocal * 3; ++ii) {
dfcorr[ii] *= force_unit_cvt_factor;
}
for (int ii = 0; ii < 9; ++ii) {
dvcorr[ii] *= ener_unit_cvt_factor;
}
} catch (deepmd_compat::deepmd_exception &e) {
error->one(FLERR, e.what());
}
Expand Down
1 change: 1 addition & 0 deletions source/lmp/fix_dplr.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class FixDPLR : public Fix {
void unpack_reverse_comm(int, int *, double *) override;
double compute_scalar(void) override;
double compute_vector(int) override;
double ener_unit_cvt_factor, dist_unit_cvt_factor, force_unit_cvt_factor;

private:
PairDeepMD *pair_deepmd;
Expand Down

0 comments on commit 19c6a68

Please sign in to comment.