From 2f69bb045cc822ccacb13fc5772965d6bee6d408 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 20 Sep 2025 14:24:34 +0000 Subject: [PATCH 1/3] Initial plan From 7fabecff0beb5e4c22e6bf89359d9641b91730da Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 20 Sep 2025 14:32:22 +0000 Subject: [PATCH 2/3] fix(qe): use ndmin=2 in np.loadtxt to handle single-line .evp files Co-authored-by: njzjz <9496702+njzjz@users.noreply.github.com> --- dpdata/qe/traj.py | 2 +- tests/test_qe_single_line_evp.py | 85 ++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tests/test_qe_single_line_evp.py diff --git a/dpdata/qe/traj.py b/dpdata/qe/traj.py index 00901e37..aa12ebb8 100644 --- a/dpdata/qe/traj.py +++ b/dpdata/qe/traj.py @@ -169,7 +169,7 @@ def load_data(fname: FileType, natoms, begin=0, step=1, convert=1.0): def load_energy(fname, begin=0, step=1): - data = np.loadtxt(fname) + data = np.loadtxt(fname, ndmin=2) steps = [] for ii in data[begin::step, 0]: steps.append("%d" % ii) # noqa: UP031 diff --git a/tests/test_qe_single_line_evp.py b/tests/test_qe_single_line_evp.py new file mode 100644 index 00000000..2091954a --- /dev/null +++ b/tests/test_qe_single_line_evp.py @@ -0,0 +1,85 @@ +from __future__ import annotations + +import os +import tempfile +import unittest + +import dpdata + + +class TestQESingleLineEvp(unittest.TestCase): + """Test for QE CP trajectory with single-line .evp file (issue #899).""" + + def setUp(self): + # Create a temporary directory for test files + self.test_dir = tempfile.mkdtemp() + + # Create a simple input file + self.input_content = """&CONTROL + calculation = 'cp' + prefix = 'test' +/ +&SYSTEM + ibrav = 1 + celldm(1) = 10.0 + nat = 2 + ntyp = 1 +/ +&ELECTRONS +/ +&IONS +/ +ATOMIC_SPECIES +H 1.0 H.pbe-rrkjus_psl.1.0.0.UPF +ATOMIC_POSITIONS {alat} +H 0.0 0.0 0.0 +H 0.5 0.5 0.5 +""" + + # Create a single-line .evp file (this used to cause errors) + self.evp_content = "# comments here\n 195 9.433649E-03 1.290500E-05 0.000000E+00 1.057182E-02 -1100.03076389 -1100.03076389 -1100.03075434 -1100.03073209 1.300606E+04 -3.31610" + + # Create a single-line .for file + self.for_content = " 195\n0.0 0.0 0.0\n0.0 0.0 0.0" + + # Create a single-line .pos file + self.pos_content = " 195\n0.0 0.0 0.0\n1.0 1.0 1.0" + + # Create a .cel file + self.cel_content = " 195\n10.0 0.0 0.0\n0.0 10.0 0.0\n0.0 0.0 10.0" + + # Write files + self.prefix = os.path.join(self.test_dir, "test") + with open(self.prefix + ".in", "w") as f: + f.write(self.input_content) + with open(self.prefix + ".evp", "w") as f: + f.write(self.evp_content) + with open(self.prefix + ".for", "w") as f: + f.write(self.for_content) + with open(self.prefix + ".pos", "w") as f: + f.write(self.pos_content) + with open(self.prefix + ".cel", "w") as f: + f.write(self.cel_content) + + def tearDown(self): + # Clean up temporary files + import shutil + shutil.rmtree(self.test_dir) + + def test_single_line_evp_load(self): + """Test that single-line .evp files can be loaded without errors.""" + # This should not raise an exception with the fix + system = dpdata.LabeledSystem(self.prefix, fmt="qe/cp/traj") + + # Verify basic properties + self.assertEqual(system.get_nframes(), 1) + self.assertEqual(len(system["energies"]), 1) + + # The energy should be converted properly + # -1100.03076389 Hartree * energy_convert factor + # Let's check the actual value first and use a more reasonable tolerance + self.assertAlmostEqual(system["energies"][0], -29933.361998680055, places=3) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file From e038715b5b7941e1c64440cd5e14081bd4256992 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 20 Sep 2025 14:35:48 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_qe_single_line_evp.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/tests/test_qe_single_line_evp.py b/tests/test_qe_single_line_evp.py index 2091954a..5bd675c2 100644 --- a/tests/test_qe_single_line_evp.py +++ b/tests/test_qe_single_line_evp.py @@ -13,7 +13,7 @@ class TestQESingleLineEvp(unittest.TestCase): def setUp(self): # Create a temporary directory for test files self.test_dir = tempfile.mkdtemp() - + # Create a simple input file self.input_content = """&CONTROL calculation = 'cp' @@ -35,19 +35,19 @@ def setUp(self): H 0.0 0.0 0.0 H 0.5 0.5 0.5 """ - + # Create a single-line .evp file (this used to cause errors) self.evp_content = "# comments here\n 195 9.433649E-03 1.290500E-05 0.000000E+00 1.057182E-02 -1100.03076389 -1100.03076389 -1100.03075434 -1100.03073209 1.300606E+04 -3.31610" - - # Create a single-line .for file + + # Create a single-line .for file self.for_content = " 195\n0.0 0.0 0.0\n0.0 0.0 0.0" - + # Create a single-line .pos file self.pos_content = " 195\n0.0 0.0 0.0\n1.0 1.0 1.0" - + # Create a .cel file self.cel_content = " 195\n10.0 0.0 0.0\n0.0 10.0 0.0\n0.0 0.0 10.0" - + # Write files self.prefix = os.path.join(self.test_dir, "test") with open(self.prefix + ".in", "w") as f: @@ -64,22 +64,23 @@ def setUp(self): def tearDown(self): # Clean up temporary files import shutil + shutil.rmtree(self.test_dir) def test_single_line_evp_load(self): """Test that single-line .evp files can be loaded without errors.""" # This should not raise an exception with the fix system = dpdata.LabeledSystem(self.prefix, fmt="qe/cp/traj") - + # Verify basic properties self.assertEqual(system.get_nframes(), 1) self.assertEqual(len(system["energies"]), 1) - - # The energy should be converted properly + + # The energy should be converted properly # -1100.03076389 Hartree * energy_convert factor # Let's check the actual value first and use a more reasonable tolerance self.assertAlmostEqual(system["energies"][0], -29933.361998680055, places=3) if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main()