Skip to content

Commit

Permalink
add unittest for virial and pe/atom (#4013)
Browse files Browse the repository at this point in the history
Addresses #3943 

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit



- **New Features**
- Enhanced testing functionality for pressure calculations alongside
virial computations in the simulation framework.

- **Bug Fixes**
- Improved reliability and accuracy of test assertions for pressure
values, ensuring better validation of simulation results.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Yi-FanLi and pre-commit-ci[bot] authored Jul 24, 2024
1 parent a010ae9 commit 5dd0062
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion source/lmp/tests/test_lammps.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
]
)

expected_v = -np.array(
expected_v = -np.array( # This minus sign comes from the definition of the compute centroid/stress/atom command in LAMMPS. See https://docs.lammps.org/compute_stress_atom.html
[
-2.912234126853306959e-01,
-3.800610846612756388e-02,
Expand Down Expand Up @@ -321,7 +321,13 @@ def test_pair_deepmd(lammps):
def test_pair_deepmd_virial(lammps):
lammps.pair_style(f"deepmd {pb_file.resolve()}")
lammps.pair_coeff("* *")
lammps.compute("peatom all pe/atom pair")
lammps.compute("pressure all pressure NULL pair")
lammps.compute("virial all centroid/stress/atom NULL pair")
lammps.variable("eatom atom c_peatom")
for ii in range(9):
jj = [0, 4, 8, 3, 6, 7, 1, 2, 5][ii]
lammps.variable(f"pressure{jj} equal c_pressure[{ii+1}]")
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}]")
Expand All @@ -335,6 +341,17 @@ def test_pair_deepmd_virial(lammps):
expected_f[lammps.atoms[ii].id - 1]
)
idx_map = lammps.lmp.numpy.extract_atom("id") - 1
assert np.array(lammps.variables["eatom"].value) == pytest.approx(
expected_ae[idx_map]
)
vol = box[1] * box[3] * box[5]
for ii in range(6):
jj = [0, 4, 8, 3, 6, 7, 1, 2, 5][ii]
assert np.array(
lammps.variables[f"pressure{jj}"].value
) / constants.nktv2p == pytest.approx(
-expected_v[idx_map, jj].sum(axis=0) / vol
)
for ii in range(9):
assert np.array(
lammps.variables[f"virial{ii}"].value
Expand Down Expand Up @@ -372,7 +389,13 @@ def test_pair_deepmd_model_devi_virial(lammps):
f"deepmd {pb_file.resolve()} {pb_file2.resolve()} out_file {md_file.resolve()} out_freq 1 atomic"
)
lammps.pair_coeff("* *")
lammps.compute("peatom all pe/atom pair")
lammps.compute("pressure all pressure NULL pair")
lammps.compute("virial all centroid/stress/atom NULL pair")
lammps.variable("eatom atom c_peatom")
for ii in range(9):
jj = [0, 4, 8, 3, 6, 7, 1, 2, 5][ii]
lammps.variable(f"pressure{jj} equal c_pressure[{ii+1}]")
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}]")
Expand All @@ -386,6 +409,17 @@ def test_pair_deepmd_model_devi_virial(lammps):
expected_f[lammps.atoms[ii].id - 1]
)
idx_map = lammps.lmp.numpy.extract_atom("id") - 1
assert np.array(lammps.variables["eatom"].value) == pytest.approx(
expected_ae[idx_map]
)
vol = box[1] * box[3] * box[5]
for ii in range(6):
jj = [0, 4, 8, 3, 6, 7, 1, 2, 5][ii]
assert np.array(
lammps.variables[f"pressure{jj}"].value
) / constants.nktv2p == pytest.approx(
-expected_v[idx_map, jj].sum(axis=0) / vol
)
for ii in range(9):
assert np.array(
lammps.variables[f"virial{ii}"].value
Expand Down

0 comments on commit 5dd0062

Please sign in to comment.