diff --git a/source/api_cc/tests/test_deeppot_dpa2_pt.cc b/source/api_cc/tests/test_deeppot_dpa2_pt.cc index 65829c55c8..951d88e018 100644 --- a/source/api_cc/tests/test_deeppot_dpa2_pt.cc +++ b/source/api_cc/tests/test_deeppot_dpa2_pt.cc @@ -209,6 +209,42 @@ TYPED_TEST(TestInferDeepPotDpa2PtNoPbc, cpu_build_nlist) { } } +TYPED_TEST(TestInferDeepPotDpa2PtNoPbc, cpu_build_nlist_atomic) { + using VALUETYPE = TypeParam; + std::vector& coord = this->coord; + std::vector& atype = this->atype; + std::vector& box = this->box; + std::vector& expected_e = this->expected_e; + std::vector& expected_f = this->expected_f; + std::vector& expected_v = this->expected_v; + int& natoms = this->natoms; + double& expected_tot_e = this->expected_tot_e; + std::vector& expected_tot_v = this->expected_tot_v; + deepmd::DeepPot& dp = this->dp; + double ener; + std::vector 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(TestInferDeepPotDpa2PtNoPbc, cpu_lmp_nlist) { using VALUETYPE = TypeParam; std::vector& coord = this->coord; @@ -242,3 +278,48 @@ TYPED_TEST(TestInferDeepPotDpa2PtNoPbc, cpu_lmp_nlist) { EXPECT_LT(fabs(virial[ii] - expected_tot_v[ii]), EPSILON); } } + +TYPED_TEST(TestInferDeepPotDpa2PtNoPbc, cpu_lmp_nlist_atomic) { + using VALUETYPE = TypeParam; + std::vector& coord = this->coord; + std::vector& atype = this->atype; + std::vector& box = this->box; + std::vector& expected_e = this->expected_e; + std::vector& expected_f = this->expected_f; + std::vector& expected_v = this->expected_v; + int& natoms = this->natoms; + double& expected_tot_e = this->expected_tot_e; + std::vector& expected_tot_v = this->expected_tot_v; + deepmd::DeepPot& dp = this->dp; + double ener; + std::vector force, virial, atom_ener, atom_vir; + + std::vector > nlist_data = { + {1, 2, 3, 4, 5}, {0, 2, 3, 4, 5}, {0, 1, 3, 4, 5}, + {0, 1, 2, 4, 5}, {0, 1, 2, 3, 5}, {0, 1, 2, 3, 4}}; + std::vector ilist(natoms), numneigh(natoms); + std::vector firstneigh(natoms); + deepmd::InputNlist inlist(natoms, &ilist[0], &numneigh[0], &firstneigh[0]); + convert_nlist(inlist, nlist_data); + dp.compute(ener, force, virial, atom_ener, atom_vir, coord, atype, box, 0, + inlist, 0); + + 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); + } +} diff --git a/source/api_cc/tests/test_deeppot_dpa_pt.cc b/source/api_cc/tests/test_deeppot_dpa_pt.cc deleted file mode 100644 index 04f42ba94a..0000000000 --- a/source/api_cc/tests/test_deeppot_dpa_pt.cc +++ /dev/null @@ -1,399 +0,0 @@ -// SPDX-License-Identifier: LGPL-3.0-or-later -#include -#include -#include -#include - -#include -#include -#include -#include - -#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::value ? 1e-10 : 1e-4) - -template -class TestInferDeepPotDpaPt : public ::testing::Test { - protected: - std::vector 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 atype = {0, 1, 1, 0, 1, 1}; - std::vector box = {13., 0., 0., 0., 13., 0., 0., 0., 13.}; - // Generated by the following Python code: - // import numpy as np - // from deepmd.infer import DeepPot - // coord = np.array([ - // 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 - // ]).reshape(1, -1) - // atype = np.array([0, 1, 1, 0, 1, 1]) - // box = np.array([13., 0., 0., 0., 13., 0., 0., 0., 13.]).reshape(1, -1) - // dp = DeepPot("deeppot_dpa.pth") - // e, f, v, ae, av = dp.eval(coord, box, atype, atomic=True) - // np.set_printoptions(precision=16) - // print(f"{e.ravel()=} {v.ravel()=} {f.ravel()=} {ae.ravel()=} - // {av.ravel()=}") - - std::vector expected_e = { - -94.37720733019096, -187.43155959873033, -187.37830241580824, - -94.34880710985752, -187.38869830422271, -187.33919952642458}; - std::vector expected_f = { - 5.402355596838843, -1.263284191331685, -0.697693239979719, - -1.025144852453706, 0.6554396369933394, 0.8817286288078215, - 0.4364579972147229, 1.2150079148857598, -0.6778076371985796, - -6.939243547937094, 0.1571084862688049, -0.9017435514431825, - 0.3597967524845581, -1.328808718007412, 2.0974306454214653, - 1.7657780538526762, 0.5645368711911929, -0.7019148456078053}; - std::vector expected_v = { - 9.5175137906314511e-01, -2.0801835688892991e+00, 4.6860789988973117e-01, - -6.0178723966859824e+00, 1.2556002911926123e-01, 4.7887097832213565e-02, - 5.6216590124464116e-01, 1.7071246159044051e-01, 8.4990129293690209e-02, - -1.2558035496847255e+00, -3.1123763096053136e-02, -4.4100135935181761e-01, - 6.4707184007995455e-01, 1.5574441384822924e-01, 3.2409058144551339e-01, - 2.8631311270672963e+00, -3.0375434485037031e-04, 3.9533024424985619e-01, - 3.2722174727830535e+00, 1.1867224518409690e-01, -2.2250901443705223e-01, - 5.0337980348311300e+00, 6.0517723355290898e-01, -5.5204995585567707e-01, - -3.8335680797875722e+00, -2.3083403461022087e-01, 3.1281970616476651e-01, - -1.0733902445454071e+01, -2.7634498084191517e-01, 1.5720135955951031e+00, - -2.9262906180354680e+00, 1.0845127764896278e-01, -1.1142053272645919e-01, - 3.6066832583682209e+00, -1.9002351752094526e-01, 3.1875602887687587e-01, - 3.6971839777382898e-01, -2.7352380159430506e-02, 1.0670299036230046e-01, - 1.8155828042674422e+00, 4.9170982983933986e-01, -6.7166291183351579e-01, - -2.9003369690467395e+00, -7.6647630459927585e-01, 1.0566933380800889e+00, - -4.8620953903555858e-01, 4.0440213825136057e-01, -6.5227187264812003e-01, - -4.4421997400831864e-01, 1.4811202361724179e-01, -2.4354470120979710e-01, - 5.3346700156430571e-01, -1.8977527286286849e-01, 3.1383559345422440e-01}; - int natoms; - double expected_tot_e; - std::vector expected_tot_v; - - deepmd::DeepPot dp; - - void SetUp() override { -#ifndef BUILD_PYTORCH - GTEST_SKIP() << "Skip because PyTorch support is not enabled."; -#endif - 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 {}; -}; - -TYPED_TEST_SUITE(TestInferDeepPotDpaPt, ValueTypes); - -TYPED_TEST(TestInferDeepPotDpaPt, cpu_build_nlist) { - using VALUETYPE = TypeParam; - std::vector& coord = this->coord; - std::vector& atype = this->atype; - std::vector& box = this->box; - std::vector& expected_e = this->expected_e; - std::vector& expected_f = this->expected_f; - std::vector& expected_v = this->expected_v; - int& natoms = this->natoms; - double& expected_tot_e = this->expected_tot_e; - std::vector& expected_tot_v = this->expected_tot_v; - deepmd::DeepPot& dp = this->dp; - double ener; - std::vector 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& coord = this->coord; - std::vector& atype = this->atype; - std::vector& box = this->box; - std::vector& expected_e = this->expected_e; - std::vector& expected_f = this->expected_f; - std::vector& expected_v = this->expected_v; - int& natoms = this->natoms; - double& expected_tot_e = this->expected_tot_e; - std::vector& expected_tot_v = this->expected_tot_v; - deepmd::DeepPot& dp = this->dp; - double ener; - std::vector 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); - } -} - -template -class TestInferDeepPotDpaPtNopbc : public ::testing::Test { - protected: - std::vector 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 atype = {0, 1, 1, 0, 1, 1}; - std::vector box = {}; - // Generated by the following Python code: - // import numpy as np - // from deepmd.infer import DeepPot - // coord = np.array([ - // 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 - // ]).reshape(1, -1) - // atype = np.array([0, 1, 1, 0, 1, 1]) - // box = None - // dp = DeepPot("deeppot_dpa.pth") - // e, f, v, ae, av = dp.eval(coord, box, atype, atomic=True) - // np.set_printoptions(precision=16) - // print(f"{e.ravel()=} {v.ravel()=} {f.ravel()=} {ae.ravel()=} - // {av.ravel()=}") - - std::vector expected_e = { - -95.13216447995296, -188.10146505781867, -187.74742451023172, - -94.73864717001219, -187.76956603003393, -187.76904550434332}; - std::vector expected_f = { - 0.7486830600282869, -0.240322915088127, -0.3943366458127905, - -0.1776248813665344, 0.2359143394202788, 0.4210018319063822, - -0.2368532809002255, 0.0291156803500336, -0.0219651427265617, - -1.407280069394403, 0.4932116549421467, -0.9482072853582465, - -0.1501958909452974, -0.9720722611839484, 1.5128172910814666, - 1.2232710625781733, 0.4541535015596165, -0.569310049090249}; - std::vector expected_v = { - 1.4724482801774368e+00, -1.8952544175284314e-01, -2.0502896614522359e-01, - -2.0361724110178425e-01, 5.4221646102123211e-02, 8.7963957026666373e-02, - -1.3233356224791937e-01, 8.3907068051133571e-02, 1.6072164570432412e-01, - 2.2913216241740741e+00, -6.0712170533586352e-02, 1.2802395909429765e-01, - 6.9581050483420448e-03, 2.0894022035588655e-02, 4.3408316864598340e-02, - -1.4144392402206662e-03, 3.6852652738654124e-02, 7.7149761552687490e-02, - 5.6814285976509526e-01, -7.0738211182030164e-02, 5.4514470128648518e-02, - -7.1339324275474125e-02, 9.8158535704203354e-03, -8.3431069537701560e-03, - 5.4072790262097083e-02, -8.1976736911977682e-03, 7.6505804915597275e-03, - 1.6869950835783332e-01, 2.1880432930426963e-02, 1.0308234746703970e-01, - 9.1015395953307099e-02, 7.1788910181538768e-02, -1.4119552688428305e-01, - -1.4977320631771729e-01, -1.0982955047012899e-01, 2.3324521962640055e-01, - 8.1569862372597679e-01, 6.2848559999917952e-02, -4.5341405643671506e-02, - -3.9134119664198064e-01, 4.1651372430088562e-01, -5.8173709994663803e-01, - 6.6155672230934037e-01, -6.4774042800560672e-01, 9.0924772156749301e-01, - 2.0503134548416586e+00, 1.9684008914564011e-01, -3.1711040533580070e-01, - 5.2891751962511613e-01, 8.7385258358844808e-02, -1.5487618319904839e-01, - -7.1396830520028809e-01, -1.0977171171532918e-01, 1.9792085656111236e-01}; - int natoms; - double expected_tot_e; - std::vector expected_tot_v; - - deepmd::DeepPot dp; - - void SetUp() override { -#ifndef BUILD_PYTORCH - GTEST_SKIP() << "Skip because PyTorch support is not enabled."; -#endif - 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 {}; -}; - -TYPED_TEST_SUITE(TestInferDeepPotDpaPtNopbc, ValueTypes); - -TYPED_TEST(TestInferDeepPotDpaPtNopbc, cpu_build_nlist) { - using VALUETYPE = TypeParam; - std::vector& coord = this->coord; - std::vector& atype = this->atype; - std::vector& box = this->box; - std::vector& expected_e = this->expected_e; - std::vector& expected_f = this->expected_f; - std::vector& expected_v = this->expected_v; - int& natoms = this->natoms; - double& expected_tot_e = this->expected_tot_e; - std::vector& expected_tot_v = this->expected_tot_v; - deepmd::DeepPot& dp = this->dp; - double ener; - std::vector 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(TestInferDeepPotDpaPtNopbc, cpu_build_nlist_atomic) { - using VALUETYPE = TypeParam; - std::vector& coord = this->coord; - std::vector& atype = this->atype; - std::vector& box = this->box; - std::vector& expected_e = this->expected_e; - std::vector& expected_f = this->expected_f; - std::vector& expected_v = this->expected_v; - int& natoms = this->natoms; - double& expected_tot_e = this->expected_tot_e; - std::vector& expected_tot_v = this->expected_tot_v; - deepmd::DeepPot& dp = this->dp; - double ener; - std::vector 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(TestInferDeepPotDpaPtNopbc, cpu_lmp_nlist) { - using VALUETYPE = TypeParam; - std::vector& coord = this->coord; - std::vector& atype = this->atype; - std::vector& box = this->box; - std::vector& expected_e = this->expected_e; - std::vector& expected_f = this->expected_f; - std::vector& expected_v = this->expected_v; - int& natoms = this->natoms; - double& expected_tot_e = this->expected_tot_e; - std::vector& expected_tot_v = this->expected_tot_v; - deepmd::DeepPot& dp = this->dp; - double ener; - std::vector force, virial; - - std::vector > nlist_data = { - {1, 2, 3, 4, 5}, {0, 2, 3, 4, 5}, {0, 1, 3, 4, 5}, - {0, 1, 2, 4, 5}, {0, 1, 2, 3, 5}, {0, 1, 2, 3, 4}}; - std::vector ilist(natoms), numneigh(natoms); - std::vector firstneigh(natoms); - deepmd::InputNlist inlist(natoms, &ilist[0], &numneigh[0], &firstneigh[0]); - convert_nlist(inlist, nlist_data); - dp.compute(ener, force, virial, coord, atype, box, 0, inlist, 0); - - 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(TestInferDeepPotDpaPtNopbc, cpu_lmp_nlist_atomic) { - using VALUETYPE = TypeParam; - std::vector& coord = this->coord; - std::vector& atype = this->atype; - std::vector& box = this->box; - std::vector& expected_e = this->expected_e; - std::vector& expected_f = this->expected_f; - std::vector& expected_v = this->expected_v; - int& natoms = this->natoms; - double& expected_tot_e = this->expected_tot_e; - std::vector& expected_tot_v = this->expected_tot_v; - deepmd::DeepPot& dp = this->dp; - double ener; - std::vector force, virial, atom_ener, atom_vir; - - std::vector > nlist_data = { - {1, 2, 3, 4, 5}, {0, 2, 3, 4, 5}, {0, 1, 3, 4, 5}, - {0, 1, 2, 4, 5}, {0, 1, 2, 3, 5}, {0, 1, 2, 3, 4}}; - std::vector ilist(natoms), numneigh(natoms); - std::vector firstneigh(natoms); - deepmd::InputNlist inlist(natoms, &ilist[0], &numneigh[0], &firstneigh[0]); - convert_nlist(inlist, nlist_data); - dp.compute(ener, force, virial, atom_ener, atom_vir, coord, atype, box, 0, - inlist, 0); - - 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); - } -} diff --git a/source/lmp/tests/test_lammps_dpa_pt.py b/source/lmp/tests/test_lammps_dpa2_pt.py similarity index 90% rename from source/lmp/tests/test_lammps_dpa_pt.py rename to source/lmp/tests/test_lammps_dpa2_pt.py index 2c93096556..860a62b631 100644 --- a/source/lmp/tests/test_lammps_dpa_pt.py +++ b/source/lmp/tests/test_lammps_dpa2_pt.py @@ -12,6 +12,9 @@ import constants import numpy as np import pytest +from expected_ref import ( + read_expected_ref, +) from lammps import ( PyLammps, ) @@ -25,7 +28,10 @@ pbtxt_file2 = ( Path(__file__).parent.parent.parent / "tests" / "infer" / "deeppot-1.pbtxt" ) -pb_file = Path(__file__).parent.parent.parent / "tests" / "infer" / "deeppot_dpa.pth" +pb_file = Path(__file__).parent.parent.parent / "tests" / "infer" / "deeppot_dpa2.pth" +ref_file = ( + Path(__file__).parent.parent.parent / "tests" / "infer" / "deeppot_dpa2.expected" +) pb_file2 = Path(__file__).parent / "graph2.pb" system_file = Path(__file__).parent.parent.parent / "tests" data_file = Path(__file__).parent / "data.lmp" @@ -33,40 +39,15 @@ data_type_map_file = Path(__file__).parent / "data_type_map.lmp" md_file = Path(__file__).parent / "md.out" -# this is as the same as python and c++ tests, test_deeppot_a.py -expected_ae = np.array( - [ - -94.37720733019096, - -187.43155959873033, - -187.37830241580824, - -94.34880710985752, - -187.38869830422271, - -187.33919952642458, - ] -) -expected_e = np.sum(expected_ae) -expected_f = np.array( - [ - 5.402355596838843, - -1.263284191331685, - -0.697693239979719, - -1.025144852453706, - 0.6554396369933394, - 0.8817286288078215, - 0.4364579972147229, - 1.2150079148857598, - -0.6778076371985796, - -6.939243547937094, - 0.1571084862688049, - -0.9017435514431825, - 0.3597967524845581, - -1.328808718007412, - 2.0974306454214653, - 1.7657780538526762, - 0.5645368711911929, - -0.7019148456078053, - ] -).reshape(6, 3) +# Reference values written by source/tests/infer/gen_dpa2.py. +# Guarded with try/except because gen_dpa2.py only runs when PyTorch is built; +# matrices that disable PyTorch still load this file at pytest collection. +try: + _ref = read_expected_ref(ref_file)["pbc"] + expected_e = float(np.sum(_ref["expected_e"])) + expected_f = _ref["expected_f"].reshape(6, 3) +except FileNotFoundError: + expected_e = expected_f = None expected_f2 = np.array( [ @@ -79,64 +60,11 @@ ] ) -expected_v = -np.array( - [ - 9.5175137906314511e-01, - -2.0801835688892991e00, - 4.6860789988973117e-01, - -6.0178723966859824e00, - 1.2556002911926123e-01, - 4.7887097832213565e-02, - 5.6216590124464116e-01, - 1.7071246159044051e-01, - 8.4990129293690209e-02, - -1.2558035496847255e00, - -3.1123763096053136e-02, - -4.4100135935181761e-01, - 6.4707184007995455e-01, - 1.5574441384822924e-01, - 3.2409058144551339e-01, - 2.8631311270672963e00, - -3.0375434485037031e-04, - 3.9533024424985619e-01, - 3.2722174727830535e00, - 1.1867224518409690e-01, - -2.2250901443705223e-01, - 5.0337980348311300e00, - 6.0517723355290898e-01, - -5.5204995585567707e-01, - -3.8335680797875722e00, - -2.3083403461022087e-01, - 3.1281970616476651e-01, - -1.0733902445454071e01, - -2.7634498084191517e-01, - 1.5720135955951031e00, - -2.9262906180354680e00, - 1.0845127764896278e-01, - -1.1142053272645919e-01, - 3.6066832583682209e00, - -1.9002351752094526e-01, - 3.1875602887687587e-01, - 3.6971839777382898e-01, - -2.7352380159430506e-02, - 1.0670299036230046e-01, - 1.8155828042674422e00, - 4.9170982983933986e-01, - -6.7166291183351579e-01, - -2.9003369690467395e00, - -7.6647630459927585e-01, - 1.0566933380800889e00, - -4.8620953903555858e-01, - 4.0440213825136057e-01, - -6.5227187264812003e-01, - -4.4421997400831864e-01, - 1.4811202361724179e-01, - -2.4354470120979710e-01, - 5.3346700156430571e-01, - -1.8977527286286849e-01, - 3.1383559345422440e-01, - ] -).reshape(6, 9) +# LAMMPS uses opposite sign convention for virial vs DeepPot atom_virial. +try: + expected_v = -_ref["expected_v"].reshape(6, 9) +except (NameError, KeyError): + expected_v = None expected_v2 = -np.array( [ [ diff --git a/source/lmp/tests/test_lammps_dpa_pt_nopbc.py b/source/lmp/tests/test_lammps_dpa2_pt_nopbc.py similarity index 90% rename from source/lmp/tests/test_lammps_dpa_pt_nopbc.py rename to source/lmp/tests/test_lammps_dpa2_pt_nopbc.py index 0edd3ed089..3b8b60ea46 100644 --- a/source/lmp/tests/test_lammps_dpa_pt_nopbc.py +++ b/source/lmp/tests/test_lammps_dpa2_pt_nopbc.py @@ -12,6 +12,9 @@ import constants import numpy as np import pytest +from expected_ref import ( + read_expected_ref, +) from lammps import ( PyLammps, ) @@ -23,7 +26,10 @@ ) pbtxt_file2 = Path(__file__).parent.parent.parent / "tests" / "infer" / "deeppot.pbtxt" -pb_file = Path(__file__).parent.parent.parent / "tests" / "infer" / "deeppot_dpa.pth" +pb_file = Path(__file__).parent.parent.parent / "tests" / "infer" / "deeppot_dpa2.pth" +ref_file = ( + Path(__file__).parent.parent.parent / "tests" / "infer" / "deeppot_dpa2.expected" +) pb_file2 = Path(__file__).parent / "graph.pb" system_file = Path(__file__).parent.parent.parent / "tests" data_file = Path(__file__).parent / "data.lmp" @@ -31,40 +37,13 @@ data_type_map_file = Path(__file__).parent / "data_type_map.lmp" md_file = Path(__file__).parent / "md.out" -# this is as the same as python and c++ tests, test_deeppot_a.py -expected_ae = np.array( - [ - -95.13216447995296, - -188.10146505781867, - -187.74742451023172, - -94.73864717001219, - -187.76956603003393, - -187.76904550434332, - ] -) -expected_e = np.sum(expected_ae) -expected_f = np.array( - [ - 0.7486830600282869, - -0.240322915088127, - -0.3943366458127905, - -0.1776248813665344, - 0.2359143394202788, - 0.4210018319063822, - -0.2368532809002255, - 0.0291156803500336, - -0.0219651427265617, - -1.407280069394403, - 0.4932116549421467, - -0.9482072853582465, - -0.1501958909452974, - -0.9720722611839484, - 1.5128172910814666, - 1.2232710625781733, - 0.4541535015596165, - -0.569310049090249, - ] -).reshape(6, 3) +# Reference values written by source/tests/infer/gen_dpa2.py. +try: + _ref = read_expected_ref(ref_file)["nopbc"] + expected_e = float(np.sum(_ref["expected_e"])) + expected_f = _ref["expected_f"].reshape(6, 3) +except FileNotFoundError: + expected_e = expected_f = None expected_f2 = np.array( [ @@ -89,64 +68,11 @@ ] ).reshape(6, 3) -expected_v = -np.array( - [ - 1.4724482801774368e00, - -1.8952544175284314e-01, - -2.0502896614522359e-01, - -2.0361724110178425e-01, - 5.4221646102123211e-02, - 8.7963957026666373e-02, - -1.3233356224791937e-01, - 8.3907068051133571e-02, - 1.6072164570432412e-01, - 2.2913216241740741e00, - -6.0712170533586352e-02, - 1.2802395909429765e-01, - 6.9581050483420448e-03, - 2.0894022035588655e-02, - 4.3408316864598340e-02, - -1.4144392402206662e-03, - 3.6852652738654124e-02, - 7.7149761552687490e-02, - 5.6814285976509526e-01, - -7.0738211182030164e-02, - 5.4514470128648518e-02, - -7.1339324275474125e-02, - 9.8158535704203354e-03, - -8.3431069537701560e-03, - 5.4072790262097083e-02, - -8.1976736911977682e-03, - 7.6505804915597275e-03, - 1.6869950835783332e-01, - 2.1880432930426963e-02, - 1.0308234746703970e-01, - 9.1015395953307099e-02, - 7.1788910181538768e-02, - -1.4119552688428305e-01, - -1.4977320631771729e-01, - -1.0982955047012899e-01, - 2.3324521962640055e-01, - 8.1569862372597679e-01, - 6.2848559999917952e-02, - -4.5341405643671506e-02, - -3.9134119664198064e-01, - 4.1651372430088562e-01, - -5.8173709994663803e-01, - 6.6155672230934037e-01, - -6.4774042800560672e-01, - 9.0924772156749301e-01, - 2.0503134548416586e00, - 1.9684008914564011e-01, - -3.1711040533580070e-01, - 5.2891751962511613e-01, - 8.7385258358844808e-02, - -1.5487618319904839e-01, - -7.1396830520028809e-01, - -1.0977171171532918e-01, - 1.9792085656111236e-01, - ] -).reshape(6, 9) +# LAMMPS uses opposite sign convention for virial vs DeepPot atom_virial. +try: + expected_v = -_ref["expected_v"].reshape(6, 9) +except (NameError, KeyError): + expected_v = None expected_v2 = -np.array( [ -7.042445481792056761e-01, diff --git a/source/lmp/tests/test_lammps_dpa_sel_pt.py b/source/lmp/tests/test_lammps_dpa_sel_pt.py deleted file mode 100644 index 27baee3432..0000000000 --- a/source/lmp/tests/test_lammps_dpa_sel_pt.py +++ /dev/null @@ -1,765 +0,0 @@ -# SPDX-License-Identifier: LGPL-3.0-or-later -import importlib -import os -import shutil -import subprocess as sp -import sys -import tempfile -from pathlib import ( - Path, -) - -import constants -import numpy as np -import pytest -from lammps import ( - PyLammps, -) -from model_convert import ( - ensure_converted_pb, -) -from write_lmp_data import ( - write_lmp_data, -) - -pbtxt_file2 = ( - Path(__file__).parent.parent.parent / "tests" / "infer" / "deeppot-1.pbtxt" -) -# large repinit sel but small repformer sel -pb_file = ( - Path(__file__).parent.parent.parent / "tests" / "infer" / "deeppot_dpa_sel.pth" -) -pb_file2 = Path(__file__).parent / "graph2.pb" -system_file = Path(__file__).parent.parent.parent / "tests" -data_file = Path(__file__).parent / "data.lmp" -data_file_si = Path(__file__).parent / "data.si" -data_type_map_file = Path(__file__).parent / "data_type_map.lmp" -md_file = Path(__file__).parent / "md.out" - -# this is as the same as python and c++ tests, test_deeppot_a.py -expected_ae = np.array( - [ - -94.40466356082422, - -188.20655580528742, - -188.172650838896, - -94.3984730612324, - -188.18804200217326, - -188.20912570390797, - ] -) -expected_e = np.sum(expected_ae) -expected_f = np.array( - [ - -0.5269430960718773, - 0.09443722477575306, - -0.018996127144558193, - 0.07511784469939177, - -0.004636423045215931, - -0.06042882995560078, - -0.11356148928265902, - -0.14249867913062475, - 0.11471641225723211, - 0.48857267799774884, - 0.029274479383282204, - 0.0018077032375469655, - 0.14145328669603485, - 0.061307914850956956, - -0.08774313950622735, - -0.06463922403863916, - -0.03788451683415152, - 0.050643981111607235, - ] -).reshape(6, 3) - -expected_f2 = np.array( - [ - [-0.6454949, 1.72457783, 0.18897958], - [1.68936514, -0.36995299, -1.36044464], - [-1.09902692, -1.35487928, 1.17416702], - [1.68426111, -0.50835585, 0.98340415], - [0.05771758, 1.12515818, -1.77561531], - [-1.686822, -0.61654789, 0.78950921], - ] -) - -expected_v = -np.array( - [ - 0.9071749098850648, - 0.06394291002323482, - -0.045778841699466444, - 0.05724095081080198, - -0.04587607140012173, - 0.03338900821751993, - -0.08821876554631314, - 0.028921736412500003, - -0.0016941267234178055, - 0.0328481028525373, - 0.011077847594560757, - 0.05737319258976218, - 0.03033379636209457, - -0.007106204143787434, - -0.008933706230224273, - 0.08706716158937683, - -0.007590237086934508, - -0.0465897822325519, - 0.005288635023633567, - -0.04363673459933623, - 0.040896766225094555, - -0.0776988217139129, - -0.04503884467345057, - 0.034987399918229245, - 0.06527106015783832, - 0.036805235933779795, - -0.03289891755994384, - 0.9956154345592723, - 0.11963562102541159, - -0.11601555180804074, - 0.12681453991319047, - 0.01822615751480253, - -0.020439753868777312, - -0.10614750448436672, - -0.018079989970225654, - 0.021624509802219784, - 0.29066664335998216, - -0.017510677635950628, - 0.040767419279345324, - 0.0019631746760569863, - -0.029874379170659604, - 0.047763391313415365, - 0.016942327234135898, - 0.04694015440808539, - -0.07381327572535609, - -0.0318765521505375, - -0.03129174028722404, - 0.04031856154341752, - -0.036436413927534876, - -0.015255351334518573, - 0.020292770612590032, - 0.04264726717944152, - 0.0200622102655479, - -0.026746995544407036, - ] -).reshape(6, 9) -expected_v2 = -np.array( - [ - [ - -0.70008436, - -0.06399891, - 0.63678391, - -0.07642171, - -0.70580035, - 0.20506145, - 0.64098364, - 0.20305781, - -0.57906794, - ], - [ - -0.6372635, - 0.14315552, - 0.51952246, - 0.04604049, - -0.06003681, - -0.02688702, - 0.54489318, - -0.10951559, - -0.43730539, - ], - [ - -0.25090748, - -0.37466262, - 0.34085833, - -0.26690852, - -0.37676917, - 0.29080825, - 0.31600481, - 0.37558276, - -0.33251064, - ], - [ - -0.80195614, - -0.10273138, - 0.06935364, - -0.10429256, - -0.29693811, - 0.45643496, - 0.07247872, - 0.45604679, - -0.71048816, - ], - [ - -0.03840668, - -0.07680205, - 0.10940472, - -0.02374189, - -0.27610266, - 0.4336071, - 0.02465248, - 0.4290638, - -0.67496763, - ], - [ - -0.61475065, - -0.21163135, - 0.26652929, - -0.26134659, - -0.11560267, - 0.15415902, - 0.34343952, - 0.1589482, - -0.21370642, - ], - ] -).reshape(6, 9) - -box = np.array([0, 13, 0, 13, 0, 13, 0, 0, 0]) -coord = np.array( - [ - [12.83, 2.56, 2.18], - [12.09, 2.87, 2.74], - [0.25, 3.32, 1.68], - [3.36, 3.00, 1.81], - [3.51, 2.51, 2.60], - [4.27, 3.22, 1.56], - ] -) -type_OH = np.array([1, 2, 2, 1, 2, 2]) -type_HO = np.array([2, 1, 1, 2, 1, 1]) - - -def setup_module() -> None: - if os.environ.get("ENABLE_PYTORCH", "1") != "1": - pytest.skip( - "Skip test because PyTorch support is not enabled.", - ) - if os.environ.get("ENABLE_TENSORFLOW", "1") == "1": - ensure_converted_pb(pbtxt_file2, pb_file2) - - write_lmp_data(box, coord, type_OH, data_file) - write_lmp_data(box, coord, type_HO, data_type_map_file) - write_lmp_data( - box * constants.dist_metal2si, - coord * constants.dist_metal2si, - type_OH, - data_file_si, - ) - - -def teardown_module() -> None: - os.remove(data_file) - os.remove(data_type_map_file) - - -def _lammps(data_file, units="metal") -> PyLammps: - lammps = PyLammps() - lammps.units(units) - lammps.boundary("p p p") - lammps.atom_style("atomic") - if units == "metal" or units == "real": - lammps.neighbor("2.0 bin") - elif units == "si": - lammps.neighbor("2.0e-10 bin") - else: - raise ValueError("units should be metal, real, or si") - lammps.neigh_modify("every 10 delay 0 check no") - lammps.read_data(data_file.resolve()) - if units == "metal" or units == "real": - lammps.mass("1 16") - lammps.mass("2 2") - elif units == "si": - lammps.mass("1 %.10e" % (16 * constants.mass_metal2si)) - lammps.mass("2 %.10e" % (2 * constants.mass_metal2si)) - else: - raise ValueError("units should be metal, real, or si") - if units == "metal": - lammps.timestep(0.0005) - elif units == "real": - lammps.timestep(0.5) - elif units == "si": - lammps.timestep(5e-16) - else: - raise ValueError("units should be metal, real, or si") - lammps.fix("1 all nve") - return lammps - - -@pytest.fixture -def lammps(): - lmp = _lammps(data_file=data_file) - yield lmp - lmp.close() - - -@pytest.fixture -def lammps_type_map(): - lmp = _lammps(data_file=data_type_map_file) - yield lmp - lmp.close() - - -@pytest.fixture -def lammps_real(): - lmp = _lammps(data_file=data_file, units="real") - yield lmp - lmp.close() - - -@pytest.fixture -def lammps_si(): - lmp = _lammps(data_file=data_file_si, units="si") - yield lmp - lmp.close() - - -def test_pair_deepmd(lammps) -> None: - lammps.pair_style(f"deepmd {pb_file.resolve()}") - lammps.pair_coeff("* *") - lammps.run(0) - assert lammps.eval("pe") == pytest.approx(expected_e) - for ii in range(6): - assert lammps.atoms[ii].force == pytest.approx( - expected_f[lammps.atoms[ii].id - 1] - ) - lammps.run(1) - - -def test_pair_deepmd_virial(lammps) -> None: - lammps.pair_style(f"deepmd {pb_file.resolve()}") - lammps.pair_coeff("* *") - lammps.compute("virial all centroid/stress/atom NULL pair") - for ii in range(9): - jj = [0, 4, 8, 3, 6, 7, 1, 2, 5][ii] - lammps.variable(f"virial{jj} atom c_virial[{ii + 1}]") - lammps.dump( - "1 all custom 1 dump id " + " ".join([f"v_virial{ii}" for ii in range(9)]) - ) - lammps.run(0) - assert lammps.eval("pe") == pytest.approx(expected_e) - for ii in range(6): - assert lammps.atoms[ii].force == pytest.approx( - expected_f[lammps.atoms[ii].id - 1] - ) - idx_map = lammps.lmp.numpy.extract_atom("id")[: coord.shape[0]] - 1 - for ii in range(9): - assert np.array( - lammps.variables[f"virial{ii}"].value - ) / constants.nktv2p == pytest.approx(expected_v[idx_map, ii]) - - -@pytest.mark.skipif( - os.environ.get("ENABLE_TENSORFLOW", "1") != "1", - reason="Skip test because TensorFlow support is not enabled.", -) -def test_pair_deepmd_model_devi(lammps) -> None: - lammps.pair_style( - f"deepmd {pb_file.resolve()} {pb_file2.resolve()} out_file {md_file.resolve()} out_freq 1 atomic" - ) - lammps.pair_coeff("* *") - lammps.run(0) - assert lammps.eval("pe") == pytest.approx(expected_e) - for ii in range(6): - assert lammps.atoms[ii].force == pytest.approx( - expected_f[lammps.atoms[ii].id - 1] - ) - # load model devi - md = np.loadtxt(md_file.resolve()) - expected_md_f = np.linalg.norm(np.std([expected_f, expected_f2], axis=0), axis=1) - assert md[7:] == pytest.approx(expected_md_f) - assert md[4] == pytest.approx(np.max(expected_md_f)) - assert md[5] == pytest.approx(np.min(expected_md_f)) - assert md[6] == pytest.approx(np.mean(expected_md_f)) - expected_md_v = ( - np.std([np.sum(expected_v, axis=0), np.sum(expected_v2, axis=0)], axis=0) / 6 - ) - assert md[1] == pytest.approx(np.max(expected_md_v)) - assert md[2] == pytest.approx(np.min(expected_md_v)) - assert md[3] == pytest.approx(np.sqrt(np.mean(np.square(expected_md_v)))) - - -@pytest.mark.skipif( - os.environ.get("ENABLE_TENSORFLOW", "1") != "1", - reason="Skip test because TensorFlow support is not enabled.", -) -def test_pair_deepmd_model_devi_virial(lammps) -> None: - lammps.pair_style( - f"deepmd {pb_file.resolve()} {pb_file2.resolve()} out_file {md_file.resolve()} out_freq 1 atomic" - ) - lammps.pair_coeff("* *") - lammps.compute("virial all centroid/stress/atom NULL pair") - for ii in range(9): - jj = [0, 4, 8, 3, 6, 7, 1, 2, 5][ii] - lammps.variable(f"virial{jj} atom c_virial[{ii + 1}]") - lammps.dump( - "1 all custom 1 dump id " + " ".join([f"v_virial{ii}" for ii in range(9)]) - ) - lammps.run(0) - assert lammps.eval("pe") == pytest.approx(expected_e) - for ii in range(6): - assert lammps.atoms[ii].force == pytest.approx( - expected_f[lammps.atoms[ii].id - 1] - ) - idx_map = lammps.lmp.numpy.extract_atom("id")[: coord.shape[0]] - 1 - for ii in range(9): - assert np.array( - lammps.variables[f"virial{ii}"].value - ) / constants.nktv2p == pytest.approx(expected_v[idx_map, ii]) - # load model devi - md = np.loadtxt(md_file.resolve()) - expected_md_f = np.linalg.norm(np.std([expected_f, expected_f2], axis=0), axis=1) - assert md[7:] == pytest.approx(expected_md_f) - assert md[4] == pytest.approx(np.max(expected_md_f)) - assert md[5] == pytest.approx(np.min(expected_md_f)) - assert md[6] == pytest.approx(np.mean(expected_md_f)) - expected_md_v = ( - np.std([np.sum(expected_v, axis=0), np.sum(expected_v2, axis=0)], axis=0) / 6 - ) - assert md[1] == pytest.approx(np.max(expected_md_v)) - assert md[2] == pytest.approx(np.min(expected_md_v)) - assert md[3] == pytest.approx(np.sqrt(np.mean(np.square(expected_md_v)))) - - -@pytest.mark.skipif( - os.environ.get("ENABLE_TENSORFLOW", "1") != "1", - reason="Skip test because TensorFlow support is not enabled.", -) -def test_pair_deepmd_model_devi_atomic_relative(lammps) -> None: - relative = 1.0 - lammps.pair_style( - f"deepmd {pb_file.resolve()} {pb_file2.resolve()} out_file {md_file.resolve()} out_freq 1 atomic relative {relative}" - ) - lammps.pair_coeff("* *") - lammps.run(0) - assert lammps.eval("pe") == pytest.approx(expected_e) - for ii in range(6): - assert lammps.atoms[ii].force == pytest.approx( - expected_f[lammps.atoms[ii].id - 1] - ) - # load model devi - md = np.loadtxt(md_file.resolve()) - norm = np.linalg.norm(np.mean([expected_f, expected_f2], axis=0), axis=1) - expected_md_f = np.linalg.norm(np.std([expected_f, expected_f2], axis=0), axis=1) - expected_md_f /= norm + relative - assert md[7:] == pytest.approx(expected_md_f) - assert md[4] == pytest.approx(np.max(expected_md_f)) - assert md[5] == pytest.approx(np.min(expected_md_f)) - assert md[6] == pytest.approx(np.mean(expected_md_f)) - expected_md_v = ( - np.std([np.sum(expected_v, axis=0), np.sum(expected_v2, axis=0)], axis=0) / 6 - ) - assert md[1] == pytest.approx(np.max(expected_md_v)) - assert md[2] == pytest.approx(np.min(expected_md_v)) - assert md[3] == pytest.approx(np.sqrt(np.mean(np.square(expected_md_v)))) - - -@pytest.mark.skipif( - os.environ.get("ENABLE_TENSORFLOW", "1") != "1", - reason="Skip test because TensorFlow support is not enabled.", -) -def test_pair_deepmd_model_devi_atomic_relative_v(lammps) -> None: - relative = 1.0 - lammps.pair_style( - f"deepmd {pb_file.resolve()} {pb_file2.resolve()} out_file {md_file.resolve()} out_freq 1 atomic relative_v {relative}" - ) - lammps.pair_coeff("* *") - lammps.run(0) - assert lammps.eval("pe") == pytest.approx(expected_e) - for ii in range(6): - assert lammps.atoms[ii].force == pytest.approx( - expected_f[lammps.atoms[ii].id - 1] - ) - md = np.loadtxt(md_file.resolve()) - expected_md_f = np.linalg.norm(np.std([expected_f, expected_f2], axis=0), axis=1) - assert md[7:] == pytest.approx(expected_md_f) - assert md[4] == pytest.approx(np.max(expected_md_f)) - assert md[5] == pytest.approx(np.min(expected_md_f)) - assert md[6] == pytest.approx(np.mean(expected_md_f)) - expected_md_v = ( - np.std([np.sum(expected_v, axis=0), np.sum(expected_v2, axis=0)], axis=0) / 6 - ) - norm = ( - np.abs( - np.mean([np.sum(expected_v, axis=0), np.sum(expected_v2, axis=0)], axis=0) - ) - / 6 - ) - expected_md_v /= norm + relative - assert md[1] == pytest.approx(np.max(expected_md_v)) - assert md[2] == pytest.approx(np.min(expected_md_v)) - assert md[3] == pytest.approx(np.sqrt(np.mean(np.square(expected_md_v)))) - - -def test_pair_deepmd_type_map(lammps_type_map) -> None: - lammps_type_map.pair_style(f"deepmd {pb_file.resolve()}") - lammps_type_map.pair_coeff("* * H O") - lammps_type_map.run(0) - assert lammps_type_map.eval("pe") == pytest.approx(expected_e) - for ii in range(6): - assert lammps_type_map.atoms[ii].force == pytest.approx( - expected_f[lammps_type_map.atoms[ii].id - 1] - ) - lammps_type_map.run(1) - - -def test_pair_deepmd_real(lammps_real) -> None: - lammps_real.pair_style(f"deepmd {pb_file.resolve()}") - lammps_real.pair_coeff("* *") - lammps_real.run(0) - assert lammps_real.eval("pe") == pytest.approx( - expected_e * constants.ener_metal2real - ) - for ii in range(6): - assert lammps_real.atoms[ii].force == pytest.approx( - expected_f[lammps_real.atoms[ii].id - 1] * constants.force_metal2real - ) - lammps_real.run(1) - - -def test_pair_deepmd_virial_real(lammps_real) -> None: - lammps_real.pair_style(f"deepmd {pb_file.resolve()}") - lammps_real.pair_coeff("* *") - lammps_real.compute("virial all centroid/stress/atom NULL pair") - for ii in range(9): - jj = [0, 4, 8, 3, 6, 7, 1, 2, 5][ii] - lammps_real.variable(f"virial{jj} atom c_virial[{ii + 1}]") - lammps_real.dump( - "1 all custom 1 dump id " + " ".join([f"v_virial{ii}" for ii in range(9)]) - ) - lammps_real.run(0) - assert lammps_real.eval("pe") == pytest.approx( - expected_e * constants.ener_metal2real - ) - for ii in range(6): - assert lammps_real.atoms[ii].force == pytest.approx( - expected_f[lammps_real.atoms[ii].id - 1] * constants.force_metal2real - ) - idx_map = lammps_real.lmp.numpy.extract_atom("id")[: coord.shape[0]] - 1 - for ii in range(9): - assert np.array( - lammps_real.variables[f"virial{ii}"].value - ) / constants.nktv2p_real == pytest.approx( - expected_v[idx_map, ii] * constants.ener_metal2real - ) - - -@pytest.mark.skipif( - os.environ.get("ENABLE_TENSORFLOW", "1") != "1", - reason="Skip test because TensorFlow support is not enabled.", -) -def test_pair_deepmd_model_devi_real(lammps_real) -> None: - lammps_real.pair_style( - f"deepmd {pb_file.resolve()} {pb_file2.resolve()} out_file {md_file.resolve()} out_freq 1 atomic" - ) - lammps_real.pair_coeff("* *") - lammps_real.run(0) - assert lammps_real.eval("pe") == pytest.approx( - expected_e * constants.ener_metal2real - ) - for ii in range(6): - assert lammps_real.atoms[ii].force == pytest.approx( - expected_f[lammps_real.atoms[ii].id - 1] * constants.force_metal2real - ) - # load model devi - md = np.loadtxt(md_file.resolve()) - expected_md_f = np.linalg.norm(np.std([expected_f, expected_f2], axis=0), axis=1) - assert md[7:] == pytest.approx(expected_md_f * constants.force_metal2real) - assert md[4] == pytest.approx(np.max(expected_md_f) * constants.force_metal2real) - assert md[5] == pytest.approx(np.min(expected_md_f) * constants.force_metal2real) - assert md[6] == pytest.approx(np.mean(expected_md_f) * constants.force_metal2real) - expected_md_v = ( - np.std([np.sum(expected_v, axis=0), np.sum(expected_v2, axis=0)], axis=0) / 6 - ) - assert md[1] == pytest.approx(np.max(expected_md_v) * constants.ener_metal2real) - assert md[2] == pytest.approx(np.min(expected_md_v) * constants.ener_metal2real) - assert md[3] == pytest.approx( - np.sqrt(np.mean(np.square(expected_md_v))) * constants.ener_metal2real - ) - - -@pytest.mark.skipif( - os.environ.get("ENABLE_TENSORFLOW", "1") != "1", - reason="Skip test because TensorFlow support is not enabled.", -) -def test_pair_deepmd_model_devi_virial_real(lammps_real) -> None: - lammps_real.pair_style( - f"deepmd {pb_file.resolve()} {pb_file2.resolve()} out_file {md_file.resolve()} out_freq 1 atomic" - ) - lammps_real.pair_coeff("* *") - lammps_real.compute("virial all centroid/stress/atom NULL pair") - for ii in range(9): - jj = [0, 4, 8, 3, 6, 7, 1, 2, 5][ii] - lammps_real.variable(f"virial{jj} atom c_virial[{ii + 1}]") - lammps_real.dump( - "1 all custom 1 dump id " + " ".join([f"v_virial{ii}" for ii in range(9)]) - ) - lammps_real.run(0) - assert lammps_real.eval("pe") == pytest.approx( - expected_e * constants.ener_metal2real - ) - for ii in range(6): - assert lammps_real.atoms[ii].force == pytest.approx( - expected_f[lammps_real.atoms[ii].id - 1] * constants.force_metal2real - ) - idx_map = lammps_real.lmp.numpy.extract_atom("id")[: coord.shape[0]] - 1 - for ii in range(9): - assert np.array( - lammps_real.variables[f"virial{ii}"].value - ) / constants.nktv2p_real == pytest.approx( - expected_v[idx_map, ii] * constants.ener_metal2real - ) - # load model devi - md = np.loadtxt(md_file.resolve()) - expected_md_f = np.linalg.norm(np.std([expected_f, expected_f2], axis=0), axis=1) - assert md[7:] == pytest.approx(expected_md_f * constants.force_metal2real) - assert md[4] == pytest.approx(np.max(expected_md_f) * constants.force_metal2real) - assert md[5] == pytest.approx(np.min(expected_md_f) * constants.force_metal2real) - assert md[6] == pytest.approx(np.mean(expected_md_f) * constants.force_metal2real) - expected_md_v = ( - np.std([np.sum(expected_v, axis=0), np.sum(expected_v2, axis=0)], axis=0) / 6 - ) - assert md[1] == pytest.approx(np.max(expected_md_v) * constants.ener_metal2real) - assert md[2] == pytest.approx(np.min(expected_md_v) * constants.ener_metal2real) - assert md[3] == pytest.approx( - np.sqrt(np.mean(np.square(expected_md_v))) * constants.ener_metal2real - ) - - -@pytest.mark.skipif( - os.environ.get("ENABLE_TENSORFLOW", "1") != "1", - reason="Skip test because TensorFlow support is not enabled.", -) -def test_pair_deepmd_model_devi_atomic_relative_real(lammps_real) -> None: - relative = 1.0 - lammps_real.pair_style( - f"deepmd {pb_file.resolve()} {pb_file2.resolve()} out_file {md_file.resolve()} out_freq 1 atomic relative {relative * constants.force_metal2real}" - ) - lammps_real.pair_coeff("* *") - lammps_real.run(0) - assert lammps_real.eval("pe") == pytest.approx( - expected_e * constants.ener_metal2real - ) - for ii in range(6): - assert lammps_real.atoms[ii].force == pytest.approx( - expected_f[lammps_real.atoms[ii].id - 1] * constants.force_metal2real - ) - # load model devi - md = np.loadtxt(md_file.resolve()) - norm = np.linalg.norm(np.mean([expected_f, expected_f2], axis=0), axis=1) - expected_md_f = np.linalg.norm(np.std([expected_f, expected_f2], axis=0), axis=1) - expected_md_f /= norm + relative - assert md[7:] == pytest.approx(expected_md_f * constants.force_metal2real) - assert md[4] == pytest.approx(np.max(expected_md_f) * constants.force_metal2real) - assert md[5] == pytest.approx(np.min(expected_md_f) * constants.force_metal2real) - assert md[6] == pytest.approx(np.mean(expected_md_f) * constants.force_metal2real) - expected_md_v = ( - np.std([np.sum(expected_v, axis=0), np.sum(expected_v2, axis=0)], axis=0) / 6 - ) - assert md[1] == pytest.approx(np.max(expected_md_v) * constants.ener_metal2real) - assert md[2] == pytest.approx(np.min(expected_md_v) * constants.ener_metal2real) - assert md[3] == pytest.approx( - np.sqrt(np.mean(np.square(expected_md_v))) * constants.ener_metal2real - ) - - -@pytest.mark.skipif( - os.environ.get("ENABLE_TENSORFLOW", "1") != "1", - reason="Skip test because TensorFlow support is not enabled.", -) -def test_pair_deepmd_model_devi_atomic_relative_v_real(lammps_real) -> None: - relative = 1.0 - lammps_real.pair_style( - f"deepmd {pb_file.resolve()} {pb_file2.resolve()} out_file {md_file.resolve()} out_freq 1 atomic relative_v {relative * constants.ener_metal2real}" - ) - lammps_real.pair_coeff("* *") - lammps_real.run(0) - assert lammps_real.eval("pe") == pytest.approx( - expected_e * constants.ener_metal2real - ) - for ii in range(6): - assert lammps_real.atoms[ii].force == pytest.approx( - expected_f[lammps_real.atoms[ii].id - 1] * constants.force_metal2real - ) - md = np.loadtxt(md_file.resolve()) - expected_md_f = np.linalg.norm(np.std([expected_f, expected_f2], axis=0), axis=1) - assert md[7:] == pytest.approx(expected_md_f * constants.force_metal2real) - assert md[4] == pytest.approx(np.max(expected_md_f) * constants.force_metal2real) - assert md[5] == pytest.approx(np.min(expected_md_f) * constants.force_metal2real) - assert md[6] == pytest.approx(np.mean(expected_md_f) * constants.force_metal2real) - expected_md_v = ( - np.std([np.sum(expected_v, axis=0), np.sum(expected_v2, axis=0)], axis=0) / 6 - ) - norm = ( - np.abs( - np.mean([np.sum(expected_v, axis=0), np.sum(expected_v2, axis=0)], axis=0) - ) - / 6 - ) - expected_md_v /= norm + relative - assert md[1] == pytest.approx(np.max(expected_md_v) * constants.ener_metal2real) - assert md[2] == pytest.approx(np.min(expected_md_v) * constants.ener_metal2real) - assert md[3] == pytest.approx( - np.sqrt(np.mean(np.square(expected_md_v))) * constants.ener_metal2real - ) - - -def test_pair_deepmd_si(lammps_si) -> None: - lammps_si.pair_style(f"deepmd {pb_file.resolve()}") - lammps_si.pair_coeff("* *") - lammps_si.run(0) - assert lammps_si.eval("pe") == pytest.approx(expected_e * constants.ener_metal2si) - for ii in range(6): - assert lammps_si.atoms[ii].force == pytest.approx( - expected_f[lammps_si.atoms[ii].id - 1] * constants.force_metal2si - ) - lammps_si.run(1) - - -@pytest.mark.skipif( - shutil.which("mpirun") is None, reason="MPI is not installed on this system" -) -@pytest.mark.skipif( - importlib.util.find_spec("mpi4py") is None, reason="mpi4py is not installed" -) -@pytest.mark.parametrize( - ("balance_args",), - [(["--balance"],), ([],)], -) -@pytest.mark.skipif( - os.environ.get("ENABLE_TENSORFLOW", "1") != "1", - reason="Skip test because TensorFlow support is not enabled.", -) -def test_pair_deepmd_mpi(balance_args: list) -> None: - with tempfile.NamedTemporaryFile() as f: - sp.check_call( - [ - "mpirun", - "-n", - "2", - sys.executable, - Path(__file__).parent / "run_mpi_pair_deepmd.py", - data_file, - pb_file, - pb_file2, - md_file, - f.name, - *balance_args, - ] - ) - arr = np.loadtxt(f.name, ndmin=1) - pe = arr[0] - - relative = 1.0 - assert pe == pytest.approx(expected_e) - # load model devi - md = np.loadtxt(md_file.resolve()) - norm = np.linalg.norm(np.mean([expected_f, expected_f2], axis=0), axis=1) - expected_md_f = np.linalg.norm(np.std([expected_f, expected_f2], axis=0), axis=1) - expected_md_f /= norm + relative - assert md[7:] == pytest.approx(expected_md_f) - assert md[4] == pytest.approx(np.max(expected_md_f)) - assert md[5] == pytest.approx(np.min(expected_md_f)) - assert md[6] == pytest.approx(np.mean(expected_md_f)) - expected_md_v = ( - np.std([np.sum(expected_v, axis=0), np.sum(expected_v2, axis=0)], axis=0) / 6 - ) - assert md[1] == pytest.approx(np.max(expected_md_v)) - assert md[2] == pytest.approx(np.min(expected_md_v)) - assert md[3] == pytest.approx(np.sqrt(np.mean(np.square(expected_md_v)))) diff --git a/source/tests/infer/deeppot_dpa.pth b/source/tests/infer/deeppot_dpa.pth deleted file mode 100644 index db236953e1..0000000000 Binary files a/source/tests/infer/deeppot_dpa.pth and /dev/null differ diff --git a/source/tests/infer/deeppot_dpa_sel.pth b/source/tests/infer/deeppot_dpa_sel.pth deleted file mode 100644 index 09127c73ff..0000000000 Binary files a/source/tests/infer/deeppot_dpa_sel.pth and /dev/null differ