Skip to content

Commit

Permalink
add UT
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Oct 22, 2024
1 parent 596efaf commit 0a1bb76
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
6 changes: 1 addition & 5 deletions dpdata/lammps/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,7 @@ def get_spin(lines, spin_keys):
blk, head = _get_block(lines, "ATOMS")
heads = head.split()

key1 = ["sp", "spx", "spy", "spz"]

if all(i in heads for i in key1):
key = key1
elif spin_keys is not None and all(i in heads for i in spin_keys):
if spin_keys is not None and all(i in heads for i in spin_keys):
key = spin_keys
else:
return None
Expand Down
52 changes: 52 additions & 0 deletions tests/lammps/traj_partial_spin.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
ITEM: TIMESTEP
0
ITEM: NUMBER OF ATOMS
17
ITEM: BOX BOUNDS xy xz yz pp pp pp
-4.0080511965879438e-02 5.7039029418994556e+00 -5.9179115295410201e-03
1.4436085788922526e-02 5.6674744441011660e+00 -1.1487414836883500e-02
7.8239288740356017e-03 5.6734038274259646e+00 6.8277359008788905e-04
ITEM: ATOMS id type x y z c_spin[1] c_spin[2] c_spin[3] c_spin[4] c_spin[5] c_spin[6] c_spin[7] c_spin[8] c_spin[9] c_spin[10]
1 1 0.00141160 5.64868599 0.01005602 1.54706291 0.00000000 0.00000000 1.00000000 -1.40772100 -2.03739417 -1522.64797384 -0.00397809 -0.00190426 -0.00743976
2 1 5.65283939 5.57449025 2.84281508 1.54412869 0.00000000 0.00000000 1.00000000 7.75304092 6.48949619 -1512.84926162 -0.00637234 -0.00733168 0.00661107
3 1 0.00066480 2.78022036 0.01010716 1.54612979 0.00000000 0.00000000 1.00000000 -0.93618575 1.92206111 -1520.80305011 -0.00316673 0.00177893 -0.00744575
4 1 5.65233666 2.85374747 2.84289453 1.54439093 0.00000000 0.00000000 1.00000000 8.11012818 -6.49922039 -1514.31557088 -0.00569217 0.00741000 0.00640353
5 1 2.82063515 5.64869321 0.01007552 1.54714250 0.00000000 0.00000000 1.00000000 2.49070852 -2.14456666 -1523.53038650 0.00478410 -0.00213962 -0.00751154
6 1 2.89579803 5.57439179 2.84287630 1.54415032 0.00000000 0.00000000 1.00000000 -8.03062338 6.63950296 -1513.41291897 0.00440396 -0.00717185 0.00633657
7 1 2.82151287 2.78010538 0.01016303 1.54619615 0.00000000 0.00000000 1.00000000 2.71859584 1.98482729 -1521.34149633 0.00533453 0.00194532 -0.00745901
8 1 2.89637049 2.85377083 2.84297332 1.54440023 0.00000000 0.00000000 1.00000000 -7.76758760 -6.67134514 -1514.43304618 0.00505040 0.00743195 0.00630302
9 1 1.41106492 1.38817482 1.72302072 1.18134529 0.00000000 0.00000000 1.00000000 0.27170165 -0.00426695 -444.22843899 0.00100237 -0.00002725 -0.03385965
10 1 1.41105247 1.38807861 3.96314606 1.18153407 0.00000000 0.00000000 1.00000000 -0.07722674 0.01368756 -337.08703133 -0.00066982 0.00007487 0.07887183
11 1 1.41105864 4.21395432 1.43987180 1.71989299 0.00000000 0.00000000 1.00000000 -0.01511106 0.00320081 -1653.34500916 0.00010421 0.00007248 0.00634401
12 1 1.41104843 4.21387554 4.24576823 1.71989825 0.00000000 0.00000000 1.00000000 -0.71645898 0.05923960 -1640.68070568 -0.00117959 0.00006676 -0.01467806
13 1 4.27433865 1.38779084 1.43977211 1.72010048 0.00000000 0.00000000 1.00000000 0.45899480 0.03956420 -1653.36356942 0.00051885 0.00002313 0.00911600
14 1 4.27436799 1.38772964 4.24586490 1.72010133 0.00000000 0.00000000 1.00000000 0.38385331 0.07301994 -1642.06086017 -0.00002034 0.00010335 -0.01688908
15 1 4.27435427 4.21452597 1.39359689 1.65590121 0.00000000 0.00000000 1.00000000 -0.01658773 -0.06159007 -1659.12744163 0.00006470 -0.00006420 -0.01342935
16 1 4.27434583 4.21455469 4.29208004 1.65592002 0.00000000 0.00000000 1.00000000 -0.15590720 -0.03252166 -1654.84697132 -0.00066755 -0.00003915 -0.00482188
17 2 1.41096761 1.38958048 0.01029027 0.00000000 0.00000000 0.00000000 1.00000000 0.00000000 0.00000000 0.00000000 0.00048351 -0.00022876 -0.00645195
ITEM: TIMESTEP
10
ITEM: NUMBER OF ATOMS
17
ITEM: BOX BOUNDS xy xz yz pp pp pp
-4.0080511965879438e-02 5.7039029418994556e+00 -5.9179115295410201e-03
1.4436085788922526e-02 5.6674744441011660e+00 -1.1487414836883500e-02
7.8239288740356017e-03 5.6734038274259646e+00 6.8277359008788905e-04
ITEM: ATOMS id type x y z
1 1 0.00037565 5.64900783 0.00994919
2 1 5.65299480 5.57370279 2.84182058
3 1 0.00076668 2.78053566 0.01181481
4 1 5.65188602 2.85285383 2.84413423
5 1 2.82101290 5.64942265 0.01091135
6 1 2.89400594 5.57477971 2.84333235
7 1 2.82260306 2.78052696 0.00917962
8 1 2.89632273 2.85545549 2.84070353
9 1 1.41117683 1.38867159 1.72266429
10 1 1.41176544 1.38873530 3.96470435
11 1 1.41085716 4.21342650 1.43850987
12 1 1.41088045 4.21340876 4.24487134
13 1 4.27525485 1.38812593 1.43912039
14 1 4.27483864 1.38696457 4.24782541
15 1 4.27528588 4.21463764 1.39334117
16 1 4.27407834 4.21327842 4.29226033
17 2 1.40675897 1.38612182 0.01000617
48 changes: 48 additions & 0 deletions tests/test_lammps_spin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@

import numpy as np
from context import dpdata
from dpdata.lammps.dump import get_spin

TRAJ_NO_ID = """ITEM: TIMESTEP
0
ITEM: NUMBER OF ATOMS
17
ITEM: BOX BOUNDS xy xz yz pp pp pp
-4.0080511965879438e-02 5.7039029418994556e+00 -5.9179115295410201e-03
1.4436085788922526e-02 5.6674744441011660e+00 -1.1487414836883500e-02
7.8239288740356017e-03 5.6734038274259646e+00 6.8277359008788905e-04
ITEM: ATOMS type x y z c_spin[1] c_spin[2] c_spin[3] c_spin[4] c_spin[5] c_spin[6] c_spin[7] c_spin[8] c_spin[9] c_spin[10]
1 0.00141160 5.64868599 0.01005602 1.54706291 0.00000000 0.00000000 1.00000000 -1.40772100 -2.03739417 -1522.64797384 -0.00397809 -0.00190426 -0.00743976
1 5.65283939 5.57449025 2.84281508 1.54412869 0.00000000 0.00000000 1.00000000 7.75304092 6.48949619 -1512.84926162 -0.00637234 -0.00733168 0.00661107
1 0.00066480 2.78022036 0.01010716 1.54612979 0.00000000 0.00000000 1.00000000 -0.93618575 1.92206111 -1520.80305011 -0.00316673 0.00177893 -0.00744575
1 5.65233666 2.85374747 2.84289453 1.54439093 0.00000000 0.00000000 1.00000000 8.11012818 -6.49922039 -1514.31557088 -0.00569217 0.00741000 0.00640353
1 2.82063515 5.64869321 0.01007552 1.54714250 0.00000000 0.00000000 1.00000000 2.49070852 -2.14456666 -1523.53038650 0.00478410 -0.00213962 -0.00751154
1 2.89579803 5.57439179 2.84287630 1.54415032 0.00000000 0.00000000 1.00000000 -8.03062338 6.63950296 -1513.41291897 0.00440396 -0.00717185 0.00633657
1 2.82151287 2.78010538 0.01016303 1.54619615 0.00000000 0.00000000 1.00000000 2.71859584 1.98482729 -1521.34149633 0.00533453 0.00194532 -0.00745901
1 2.89637049 2.85377083 2.84297332 1.54440023 0.00000000 0.00000000 1.00000000 -7.76758760 -6.67134514 -1514.43304618 0.00505040 0.00743195 0.00630302
1 1.41106492 1.38817482 1.72302072 1.18134529 0.00000000 0.00000000 1.00000000 0.27170165 -0.00426695 -444.22843899 0.00100237 -0.00002725 -0.03385965
1 1.41105247 1.38807861 3.96314606 1.18153407 0.00000000 0.00000000 1.00000000 -0.07722674 0.01368756 -337.08703133 -0.00066982 0.00007487 0.07887183
1 1.41105864 4.21395432 1.43987180 1.71989299 0.00000000 0.00000000 1.00000000 -0.01511106 0.00320081 -1653.34500916 0.00010421 0.00007248 0.00634401
1 1.41104843 4.21387554 4.24576823 1.71989825 0.00000000 0.00000000 1.00000000 -0.71645898 0.05923960 -1640.68070568 -0.00117959 0.00006676 -0.01467806
1 4.27433865 1.38779084 1.43977211 1.72010048 0.00000000 0.00000000 1.00000000 0.45899480 0.03956420 -1653.36356942 0.00051885 0.00002313 0.00911600
1 4.27436799 1.38772964 4.24586490 1.72010133 0.00000000 0.00000000 1.00000000 0.38385331 0.07301994 -1642.06086017 -0.00002034 0.00010335 -0.01688908
1 4.27435427 4.21452597 1.39359689 1.65590121 0.00000000 0.00000000 1.00000000 -0.01658773 -0.06159007 -1659.12744163 0.00006470 -0.00006420 -0.01342935
1 4.27434583 4.21455469 4.29208004 1.65592002 0.00000000 0.00000000 1.00000000 -0.15590720 -0.03252166 -1654.84697132 -0.00066755 -0.00003915 -0.00482188
2 1.41096761 1.38958048 0.01029027 0.00000000 0.00000000 0.00000000 1.00000000 0.00000000 0.00000000 0.00000000 0.00048351 -0.00022876 -0.00645195"""


class TestLmp(unittest.TestCase):
Expand Down Expand Up @@ -99,3 +127,23 @@ def test_read_dump_spin(self):

if os.path.isdir("lammps/dump"):
shutil.rmtree("lammps/dump")

def test_read_dump_partial_spin(self):
# test if dpdata can read the spins when the spin data is not complete
with self.assertWarns(UserWarning) as cm:
tmp_system = dpdata.System(
"lammps/traj_partial_spin.dump",
fmt="lammps/dump",
type_map=["O", "H"],
input_file="lammps/in.lmp",
)
self.assertTrue("spins" not in tmp_system.data)

self.assertIn("Warning: spin info is not found in frame", str(cm.warning))

def test_get_spin_failed(self):
with self.assertWarns(UserWarning) as cm:
spin = get_spin(TRAJ_NO_ID.split("\n"),["c_spin[1]", "c_spin[2]", "c_spin[3]", "c_spin[4]"])
self.assertTrue(spin is None)

self.assertIn("Error processing spin data:", str(cm.warning))

0 comments on commit 0a1bb76

Please sign in to comment.