Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: qe input blocks not seperated by empty lines #724

Merged
merged 6 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions dpdata/qe/scf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,32 @@
bohr2ang = 0.52917721067
kbar2evperang3 = 1e3 / 1.602176621e6

_QE_BLOCK_KEYWORDS = [
"ATOMIC_SPECIES",
"ATOMIC_POSITIONS",
"K_POINTS",
"ADDITIONAL_K_POINTS",
"CELL_PARAMETERS",
"CONSTRAINTS",
"OCCUPATIONS",
"ATOMIC_VELOCITIES",
"ATOMIC_FORCES",
"SOLVENTS",
"HUBBARD",
]


def get_block(lines, keyword, skip=0):
ret = []
for idx, ii in enumerate(lines):
if keyword in ii:
blk_idx = idx + 1 + skip
while len(lines[blk_idx]) == 0:
while len(lines[blk_idx].split()) == 0:
blk_idx += 1
while len(lines[blk_idx]) != 0 and blk_idx != len(lines):
while (
len(lines[blk_idx].split()) != 0
and (lines[blk_idx].split()[0] not in _QE_BLOCK_KEYWORDS)
) and blk_idx != len(lines):
ret.append(lines[blk_idx])
blk_idx += 1
break
Expand Down Expand Up @@ -111,6 +128,8 @@ def get_energy(lines):

def get_force(lines, natoms):
blk = get_block(lines, "Forces acting on atoms", skip=1)
if len(blk) == 0:
raise RuntimeError("no force found in the output file.")
ret = []
blk = blk[0 : sum(natoms)]
for ii in blk:
Expand All @@ -123,7 +142,7 @@ def get_force(lines, natoms):
def get_stress(lines):
blk = get_block(lines, "total stress")
if len(blk) == 0:
return
return None
ret = []
for ii in blk:
ret.append([float(jj) for jj in ii.split()[3:6]])
Expand Down
45 changes: 45 additions & 0 deletions tests/test_qe_pw_scf.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,50 @@ def setUp(self):
)


class TestNa(unittest.TestCase):
def test(self):
ss = dpdata.LabeledSystem("qe.scf/na.out", fmt="qe/pw/scf")
b2a = dpdata.unit.LengthConversion("bohr", "angstrom").value()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the unused variable b2a.

The local variable b2a is assigned the value of dpdata.unit.LengthConversion("bohr", "angstrom").value() but it is not used anywhere else in the test method.

Apply this diff to remove the unused variable:

-        b2a = dpdata.unit.LengthConversion("bohr", "angstrom").value()
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
b2a = dpdata.unit.LengthConversion("bohr", "angstrom").value()
Tools
Ruff

194-194: Local variable b2a is assigned to but never used

Remove assignment to unused variable b2a

(F841)

print(ss.data.keys())
self.assertEqual(ss["atom_numbs"], [3])
self.assertEqual(ss["atom_names"], ["Na"])
self.assertEqual(ss.get_nframes(), 1)
np.testing.assert_array_equal(ss["atom_types"], [0, 0, 0])
np.testing.assert_allclose(
ss["coords"][0],
np.array(
[
0.940587444301534,
0.397635863676890,
0.059472381901808,
0.059413515648061,
0.602364552456546,
0.559472465518034,
0.602364619812068,
0.059413062489401,
0.059472381901808,
]
).reshape(3, 3)
@ ss["cells"][0],
)
np.testing.assert_allclose(
ss["cells"][0],
np.array(
[
7.171683039200000,
0.000000000000000,
0.000000000000000,
-4.270578118300000,
5.761527588200000,
0.000000000000000,
-0.000000045600000,
0.000000023000000,
12.826457854999999,
]
).reshape(3, 3),
)
np.testing.assert_allclose(ss["forces"][0], np.zeros([3, 3]))


if __name__ == "__main__":
unittest.main()
Loading