forked from deepmodeling/deepmd-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38bcdd6
commit b49d91d
Showing
2 changed files
with
950 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,288 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
#include <fcntl.h> | ||
#include <gtest/gtest.h> | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
|
||
#include <algorithm> | ||
#include <cmath> | ||
#include <fstream> | ||
#include <vector> | ||
|
||
#include "DeepPot.h" | ||
#include "neighbor_list.h" | ||
#include "test_utils.h" | ||
|
||
// 1e-10 cannot pass; unclear bug or not | ||
#undef EPSILON | ||
#define EPSILON (std::is_same<VALUETYPE, double>::value ? 1e-7 : 1e-4) | ||
|
||
template <class VALUETYPE> | ||
class TestInferDeepPotDpaPt : public ::testing::Test { | ||
protected: | ||
std::vector<VALUETYPE> coord = {12.83, 2.56, 2.18, 12.09, 2.87, 2.74, | ||
00.25, 3.32, 1.68, 3.36, 3.00, 1.81, | ||
3.51, 2.51, 2.60, 4.27, 3.22, 1.56}; | ||
std::vector<int> atype = {0, 0, 0, 0, 0, 0}; | ||
std::vector<VALUETYPE> box = {13., 0., 0., 0., 13., 0., 0., 0., 13.}; | ||
std::vector<VALUETYPE> expected_e = { | ||
-92.764905689986, -186.094047026640, -186.076093742681, -92.655789598697, | ||
-185.718629663486}; | ||
std::vector<VALUETYPE> expected_f = { | ||
-6.014285454688, -0.117415862256, 0.909976406194, -0.058755844443, | ||
-0.589966016072, -0.235278171532, 0.483870488636, 0.338301034169, | ||
-0.538259022526, 5.229560416553}; | ||
std::vector<VALUETYPE> expected_v = { | ||
-10.354386415497, 0.920140248533, 6.499718669934, -16.258335886103, | ||
0.067297469207, -0.575195264098, 4.392295016061, -0.338241970141, | ||
-0.835516051852, -14.851643816437, 0.033098659297, -1.061263863927, | ||
2.374008409869, -0.016149388418, 0.170055485103, 8.600268264426, | ||
-0.027869254689, 0.691937433336, 6.169225730997, 0.244831791298, | ||
0.034831047240, 12.137933736936, 0.115779703815, 0.522653171179, | ||
-10.043154308105, -0.213524051977, -0.159862489647, 27.897569062860, | ||
0.850893105913, -7.167280545513, 6.502654913608, 0.019020688297, | ||
-0.452832699108, -8.392512895479, 0.032993026742, 0.099452385425, | ||
4.585903488998, -0.087008504902, 0.195331013424, -4.461738874082, | ||
-0.410332961015, 0.302227770306, 6.852403864252, 0.649592712208, | ||
-0.468543024604, 5.650128346142, -0.619456389363, 1.329581340420, | ||
1.047976610548, -0.169940826454, 0.360848206024, -1.578382279577, | ||
0.224806207264, -0.471006970793}; | ||
int natoms; | ||
double expected_tot_e; | ||
std::vector<VALUETYPE> expected_tot_v; | ||
|
||
deepmd::DeepPot dp; | ||
|
||
void SetUp() override { | ||
dp.init("../../tests/infer/deeppot_dpa.pth"); | ||
|
||
natoms = expected_e.size(); | ||
EXPECT_EQ(natoms * 3, expected_f.size()); | ||
EXPECT_EQ(natoms * 9, expected_v.size()); | ||
expected_tot_e = 0.; | ||
expected_tot_v.resize(9); | ||
std::fill(expected_tot_v.begin(), expected_tot_v.end(), 0.); | ||
for (int ii = 0; ii < natoms; ++ii) { | ||
expected_tot_e += expected_e[ii]; | ||
} | ||
for (int ii = 0; ii < natoms; ++ii) { | ||
for (int dd = 0; dd < 9; ++dd) { | ||
expected_tot_v[dd] += expected_v[ii * 9 + dd]; | ||
} | ||
} | ||
}; | ||
|
||
void TearDown() override { remove("deeppot.pb"); }; | ||
}; | ||
|
||
TYPED_TEST_SUITE(TestInferDeepPotDpaPt, ValueTypes); | ||
|
||
TYPED_TEST(TestInferDeepPotDpaPt, cpu_build_nlist) { | ||
using VALUETYPE = TypeParam; | ||
std::vector<VALUETYPE>& coord = this->coord; | ||
std::vector<int>& atype = this->atype; | ||
std::vector<VALUETYPE>& box = this->box; | ||
std::vector<VALUETYPE>& expected_e = this->expected_e; | ||
std::vector<VALUETYPE>& expected_f = this->expected_f; | ||
std::vector<VALUETYPE>& expected_v = this->expected_v; | ||
int& natoms = this->natoms; | ||
double& expected_tot_e = this->expected_tot_e; | ||
std::vector<VALUETYPE>& expected_tot_v = this->expected_tot_v; | ||
deepmd::DeepPot& dp = this->dp; | ||
double ener; | ||
std::vector<VALUETYPE> force, virial; | ||
dp.compute(ener, force, virial, coord, atype, box); | ||
|
||
EXPECT_EQ(force.size(), natoms * 3); | ||
EXPECT_EQ(virial.size(), 9); | ||
|
||
EXPECT_LT(fabs(ener - expected_tot_e), EPSILON); | ||
for (int ii = 0; ii < natoms * 3; ++ii) { | ||
EXPECT_LT(fabs(force[ii] - expected_f[ii]), EPSILON); | ||
} | ||
for (int ii = 0; ii < 3 * 3; ++ii) { | ||
EXPECT_LT(fabs(virial[ii] - expected_tot_v[ii]), EPSILON); | ||
} | ||
} | ||
|
||
TYPED_TEST(TestInferDeepPotDpaPt, cpu_build_nlist_atomic) { | ||
using VALUETYPE = TypeParam; | ||
std::vector<VALUETYPE>& coord = this->coord; | ||
std::vector<int>& atype = this->atype; | ||
std::vector<VALUETYPE>& box = this->box; | ||
std::vector<VALUETYPE>& expected_e = this->expected_e; | ||
std::vector<VALUETYPE>& expected_f = this->expected_f; | ||
std::vector<VALUETYPE>& expected_v = this->expected_v; | ||
int& natoms = this->natoms; | ||
double& expected_tot_e = this->expected_tot_e; | ||
std::vector<VALUETYPE>& expected_tot_v = this->expected_tot_v; | ||
deepmd::DeepPot& dp = this->dp; | ||
double ener; | ||
std::vector<VALUETYPE> force, virial, atom_ener, atom_vir; | ||
dp.compute(ener, force, virial, atom_ener, atom_vir, coord, atype, box; | ||
|
||
EXPECT_EQ(force.size(), natoms * 3); | ||
EXPECT_EQ(virial.size(), 9); | ||
EXPECT_EQ(atom_ener.size(), natoms); | ||
EXPECT_EQ(atom_vir.size(), natoms * 9); | ||
|
||
EXPECT_LT(fabs(ener - expected_tot_e), EPSILON); | ||
for (int ii = 0; ii < natoms * 3; ++ii) { | ||
EXPECT_LT(fabs(force[ii] - expected_f[ii]), EPSILON); | ||
} | ||
for (int ii = 0; ii < 3 * 3; ++ii) { | ||
EXPECT_LT(fabs(virial[ii] - expected_tot_v[ii]), EPSILON); | ||
} | ||
for (int ii = 0; ii < natoms; ++ii) { | ||
EXPECT_LT(fabs(atom_ener[ii] - expected_e[ii]), EPSILON); | ||
} | ||
for (int ii = 0; ii < natoms * 9; ++ii) { | ||
EXPECT_LT(fabs(atom_vir[ii] - expected_v[ii]), EPSILON); | ||
} | ||
} | ||
|
||
TYPED_TEST(TestInferDeepPotDpaPt, cpu_lmp_nlist) { | ||
using VALUETYPE = TypeParam; | ||
std::vector<VALUETYPE>& coord = this->coord; | ||
std::vector<int>& atype = this->atype; | ||
std::vector<VALUETYPE>& box = this->box; | ||
std::vector<VALUETYPE>& expected_e = this->expected_e; | ||
std::vector<VALUETYPE>& expected_f = this->expected_f; | ||
std::vector<VALUETYPE>& expected_v = this->expected_v; | ||
int& natoms = this->natoms; | ||
double& expected_tot_e = this->expected_tot_e; | ||
std::vector<VALUETYPE>& expected_tot_v = this->expected_tot_v; | ||
deepmd::DeepPot& dp = this->dp; | ||
float rc = dp.cutoff(); | ||
int nloc = coord.size() / 3; | ||
std::vector<VALUETYPE> coord_cpy; | ||
std::vector<int> atype_cpy, mapping; | ||
std::vector<std::vector<int> > nlist_data; | ||
_build_nlist<VALUETYPE>(nlist_data, coord_cpy, atype_cpy, mapping, coord, | ||
atype, box, rc); | ||
int nall = coord_cpy.size() / 3; | ||
std::vector<int> ilist(nloc), numneigh(nloc); | ||
std::vector<int*> firstneigh(nloc); | ||
deepmd::InputNlist inlist(nloc, &ilist[0], &numneigh[0], &firstneigh[0]); | ||
convert_nlist(inlist, nlist_data); | ||
|
||
double ener; | ||
std::vector<VALUETYPE> force_, virial; | ||
dp.compute(ener, force_, virial, coord_cpy, atype_cpy, box, nall - nloc, | ||
inlist, 0); | ||
std::vector<VALUETYPE> force; | ||
_fold_back<VALUETYPE>(force, force_, mapping, nloc, nall, 3); | ||
|
||
EXPECT_EQ(force.size(), natoms * 3); | ||
EXPECT_EQ(virial.size(), 9); | ||
|
||
EXPECT_LT(fabs(ener - expected_tot_e), EPSILON); | ||
for (int ii = 0; ii < natoms * 3; ++ii) { | ||
EXPECT_LT(fabs(force[ii] - expected_f[ii]), EPSILON); | ||
} | ||
for (int ii = 0; ii < 3 * 3; ++ii) { | ||
EXPECT_LT(fabs(virial[ii] - expected_tot_v[ii]), EPSILON); | ||
} | ||
|
||
ener = 0.; | ||
std::fill(force_.begin(), force_.end(), 0.0); | ||
std::fill(virial.begin(), virial.end(), 0.0); | ||
dp.compute(ener, force_, virial, coord_cpy, atype_cpy, box, nall - nloc, | ||
inlist, 1); | ||
_fold_back<VALUETYPE>(force, force_, mapping, nloc, nall, 3); | ||
|
||
EXPECT_EQ(force.size(), natoms * 3); | ||
EXPECT_EQ(virial.size(), 9); | ||
|
||
EXPECT_LT(fabs(ener - expected_tot_e), EPSILON); | ||
for (int ii = 0; ii < natoms * 3; ++ii) { | ||
EXPECT_LT(fabs(force[ii] - expected_f[ii]), EPSILON); | ||
} | ||
for (int ii = 0; ii < 3 * 3; ++ii) { | ||
EXPECT_LT(fabs(virial[ii] - expected_tot_v[ii]), EPSILON); | ||
} | ||
} | ||
|
||
TYPED_TEST(TestInferDeepPotDpaPt, cpu_lmp_nlist_atomic) { | ||
using VALUETYPE = TypeParam; | ||
std::vector<VALUETYPE>& coord = this->coord; | ||
std::vector<int>& atype = this->atype; | ||
std::vector<VALUETYPE>& box = this->box; | ||
std::vector<VALUETYPE>& expected_e = this->expected_e; | ||
std::vector<VALUETYPE>& expected_f = this->expected_f; | ||
std::vector<VALUETYPE>& expected_v = this->expected_v; | ||
int& natoms = this->natoms; | ||
double& expected_tot_e = this->expected_tot_e; | ||
std::vector<VALUETYPE>& expected_tot_v = this->expected_tot_v; | ||
deepmd::DeepPot& dp = this->dp; | ||
float rc = dp.cutoff(); | ||
int nloc = coord.size() / 3; | ||
std::vector<VALUETYPE> coord_cpy; | ||
std::vector<int> atype_cpy, mapping; | ||
std::vector<std::vector<int> > nlist_data; | ||
_build_nlist<VALUETYPE>(nlist_data, coord_cpy, atype_cpy, mapping, coord, | ||
atype, box, rc); | ||
int nall = coord_cpy.size() / 3; | ||
std::vector<int> ilist(nloc), numneigh(nloc); | ||
std::vector<int*> firstneigh(nloc); | ||
deepmd::InputNlist inlist(nloc, &ilist[0], &numneigh[0], &firstneigh[0]); | ||
convert_nlist(inlist, nlist_data); | ||
|
||
double ener; | ||
std::vector<VALUETYPE> force_, atom_ener_, atom_vir_, virial; | ||
std::vector<VALUETYPE> force, atom_ener, atom_vir; | ||
dp.compute(ener, force_, virial, atom_ener_, atom_vir_, coord_cpy, atype_cpy, | ||
box, nall - nloc, inlist, 0); | ||
_fold_back<VALUETYPE>(force, force_, mapping, nloc, nall, 3); | ||
_fold_back<VALUETYPE>(atom_ener, atom_ener_, mapping, nloc, nall, 1); | ||
_fold_back<VALUETYPE>(atom_vir, atom_vir_, mapping, nloc, nall, 9); | ||
|
||
EXPECT_EQ(force.size(), natoms * 3); | ||
EXPECT_EQ(virial.size(), 9); | ||
EXPECT_EQ(atom_ener.size(), natoms); | ||
EXPECT_EQ(atom_vir.size(), natoms * 9); | ||
|
||
EXPECT_LT(fabs(ener - expected_tot_e), EPSILON); | ||
for (int ii = 0; ii < natoms * 3; ++ii) { | ||
EXPECT_LT(fabs(force[ii] - expected_f[ii]), EPSILON); | ||
} | ||
for (int ii = 0; ii < 3 * 3; ++ii) { | ||
EXPECT_LT(fabs(virial[ii] - expected_tot_v[ii]), EPSILON); | ||
} | ||
for (int ii = 0; ii < natoms; ++ii) { | ||
EXPECT_LT(fabs(atom_ener[ii] - expected_e[ii]), EPSILON); | ||
} | ||
for (int ii = 0; ii < natoms * 9; ++ii) { | ||
EXPECT_LT(fabs(atom_vir[ii] - expected_v[ii]), EPSILON); | ||
} | ||
|
||
ener = 0.; | ||
std::fill(force_.begin(), force_.end(), 0.0); | ||
std::fill(virial.begin(), virial.end(), 0.0); | ||
std::fill(atom_ener_.begin(), atom_ener_.end(), 0.0); | ||
std::fill(atom_vir_.begin(), atom_vir_.end(), 0.0); | ||
dp.compute(ener, force_, virial, atom_ener_, atom_vir_, coord_cpy, atype_cpy, | ||
box, nall - nloc, inlist, 1); | ||
_fold_back<VALUETYPE>(force, force_, mapping, nloc, nall, 3); | ||
_fold_back<VALUETYPE>(atom_ener, atom_ener_, mapping, nloc, nall, 1); | ||
_fold_back<VALUETYPE>(atom_vir, atom_vir_, mapping, nloc, nall, 9); | ||
|
||
EXPECT_EQ(force.size(), natoms * 3); | ||
EXPECT_EQ(virial.size(), 9); | ||
EXPECT_EQ(atom_ener.size(), natoms); | ||
EXPECT_EQ(atom_vir.size(), natoms * 9); | ||
|
||
EXPECT_LT(fabs(ener - expected_tot_e), EPSILON); | ||
for (int ii = 0; ii < natoms * 3; ++ii) { | ||
EXPECT_LT(fabs(force[ii] - expected_f[ii]), EPSILON); | ||
} | ||
for (int ii = 0; ii < 3 * 3; ++ii) { | ||
EXPECT_LT(fabs(virial[ii] - expected_tot_v[ii]), EPSILON); | ||
} | ||
for (int ii = 0; ii < natoms; ++ii) { | ||
EXPECT_LT(fabs(atom_ener[ii] - expected_e[ii]), EPSILON); | ||
} | ||
for (int ii = 0; ii < natoms * 9; ++ii) { | ||
EXPECT_LT(fabs(atom_vir[ii] - expected_v[ii]), EPSILON); | ||
} | ||
} |
Oops, something went wrong.