Skip to content

Commit

Permalink
update rtol and atol for python unitest
Browse files Browse the repository at this point in the history
  • Loading branch information
HydrogenSulfate committed Jan 10, 2025
1 parent 58711c7 commit d265094
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions source/lmp/tests/test_lammps_pd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
)


Expand Down Expand Up @@ -504,23 +507,23 @@ 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):
assert np.array(
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,
)


Expand Down Expand Up @@ -569,52 +572,52 @@ 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):
assert np.array(
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,
)


Expand Down

0 comments on commit d265094

Please sign in to comment.