diff --git a/source/lmp/tests/test_lammps_pd.py b/source/lmp/tests/test_lammps_pd.py index c495071551..8ccfe478de 100644 --- a/source/lmp/tests/test_lammps_pd.py +++ b/source/lmp/tests/test_lammps_pd.py @@ -30,6 +30,9 @@ data_type_map_file = Path(__file__).parent / "data_type_map.lmp" md_file = Path(__file__).parent / "md.out" +ATOL = 1e-7 +RTOL = 1e-6 + # this is as the same as python and c++ tests, test_deeppot_a.py expected_ae = np.array( [ @@ -334,7 +337,7 @@ def test_pair_deepmd_virial(lammps): for ii in range(9): assert np.array( lammps.variables[f"virial{ii}"].value - ) / constants.nktv2p == pytest.approx(expected_v[idx_map, ii], 1e-6, 1e-9) + ) / constants.nktv2p == pytest.approx(expected_v[idx_map, ii], RTOL, ATOL) def test_pair_deepmd_model_devi(lammps): @@ -376,30 +379,30 @@ def test_pair_deepmd_model_devi_virial(lammps): "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, 1e-6, 1e-9) + assert lammps.eval("pe") == pytest.approx(expected_e, RTOL, ATOL) for ii in range(6): assert lammps.atoms[ii].force == pytest.approx( - expected_f[lammps.atoms[ii].id - 1], 1e-6, 1e-9 + expected_f[lammps.atoms[ii].id - 1], RTOL, ATOL ) idx_map = lammps.lmp.numpy.extract_atom("id") - 1 for ii in range(9): assert np.array( lammps.variables[f"virial{ii}"].value - ) / constants.nktv2p == pytest.approx(expected_v[idx_map, ii], 1e-6, 1e-9) + ) / constants.nktv2p == pytest.approx(expected_v[idx_map, ii], RTOL, ATOL) # 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, 1e-6, 1e-9) - assert md[4] == pytest.approx(np.max(expected_md_f), 1e-6, 1e-9) - assert md[5] == pytest.approx(np.min(expected_md_f), 1e-6, 1e-9) - assert md[6] == pytest.approx(np.mean(expected_md_f), 1e-6, 1e-9) + assert md[7:] == pytest.approx(expected_md_f, RTOL, ATOL) + assert md[4] == pytest.approx(np.max(expected_md_f), RTOL, ATOL) + assert md[5] == pytest.approx(np.min(expected_md_f), RTOL, ATOL) + assert md[6] == pytest.approx(np.mean(expected_md_f), RTOL, ATOL) 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), 1e-6, 1e-9) - assert md[2] == pytest.approx(np.min(expected_md_v), 1e-6, 1e-9) + assert md[1] == pytest.approx(np.max(expected_md_v), RTOL, ATOL) + assert md[2] == pytest.approx(np.min(expected_md_v), RTOL, ATOL) assert md[3] == pytest.approx( - np.sqrt(np.mean(np.square(expected_md_v))), 1e-6, 1e-9 + np.sqrt(np.mean(np.square(expected_md_v))), RTOL, ATOL ) @@ -504,14 +507,14 @@ def test_pair_deepmd_virial_real(lammps_real): lammps_real.run(0) assert lammps_real.eval("pe") == pytest.approx( expected_e * constants.ener_metal2real, - 1e-6, - 1e-9, + RTOL, + ATOL, ) for ii in range(6): assert lammps_real.atoms[ii].force == pytest.approx( expected_f[lammps_real.atoms[ii].id - 1] * constants.force_metal2real, - 1e-6, - 1e-9, + RTOL, + ATOL, ) idx_map = lammps_real.lmp.numpy.extract_atom("id") - 1 for ii in range(9): @@ -519,8 +522,8 @@ def test_pair_deepmd_virial_real(lammps_real): lammps_real.variables[f"virial{ii}"].value ) / constants.nktv2p_real == pytest.approx( expected_v[idx_map, ii] * constants.ener_metal2real, - 1e-6, - 1e-9, + RTOL, + ATOL, ) @@ -569,14 +572,14 @@ def test_pair_deepmd_model_devi_virial_real(lammps_real): lammps_real.run(0) assert lammps_real.eval("pe") == pytest.approx( expected_e * constants.ener_metal2real, - 1e-6, - 1e-9, + RTOL, + ATOL, ) for ii in range(6): assert lammps_real.atoms[ii].force == pytest.approx( expected_f[lammps_real.atoms[ii].id - 1] * constants.force_metal2real, - 1e-6, - 1e-9, + RTOL, + ATOL, ) idx_map = lammps_real.lmp.numpy.extract_atom("id") - 1 for ii in range(9): @@ -584,37 +587,37 @@ def test_pair_deepmd_model_devi_virial_real(lammps_real): lammps_real.variables[f"virial{ii}"].value ) / constants.nktv2p_real == pytest.approx( expected_v[idx_map, ii] * constants.ener_metal2real, - 1e-6, - 1e-9, + RTOL, + ATOL, ) # 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, 1e-6, 1e-9 + expected_md_f * constants.force_metal2real, RTOL, ATOL ) assert md[4] == pytest.approx( - np.max(expected_md_f) * constants.force_metal2real, 1e-6, 1e-9 + np.max(expected_md_f) * constants.force_metal2real, RTOL, ATOL ) assert md[5] == pytest.approx( - np.min(expected_md_f) * constants.force_metal2real, 1e-6, 1e-9 + np.min(expected_md_f) * constants.force_metal2real, RTOL, ATOL ) assert md[6] == pytest.approx( - np.mean(expected_md_f) * constants.force_metal2real, 1e-6, 1e-9 + np.mean(expected_md_f) * constants.force_metal2real, RTOL, ATOL ) 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, 1e-6, 1e-9 + np.max(expected_md_v) * constants.ener_metal2real, RTOL, ATOL ) assert md[2] == pytest.approx( - np.min(expected_md_v) * constants.ener_metal2real, 1e-6, 1e-9 + np.min(expected_md_v) * constants.ener_metal2real, RTOL, ATOL ) assert md[3] == pytest.approx( np.sqrt(np.mean(np.square(expected_md_v))) * constants.ener_metal2real, - 1e-6, - 1e-9, + RTOL, + ATOL, )