From 07d1d860c3df369d9843718d11377ff674ef624c Mon Sep 17 00:00:00 2001 From: MoseyQAQ Date: Tue, 4 Aug 2026 12:48:35 +0800 Subject: [PATCH 1/3] fix eig occ txt write mode --- .../ASE_interface/abacuslite/io/latestio.py | 9 ++++----- source/source_io/module_energy/write_eig_occ.cpp | 4 +++- source/source_io/test/write_eig_occ_test.cpp | 15 ++++++++++++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/interfaces/ASE_interface/abacuslite/io/latestio.py b/interfaces/ASE_interface/abacuslite/io/latestio.py index fbd6cc8bb3..39581fdffe 100644 --- a/interfaces/ASE_interface/abacuslite/io/latestio.py +++ b/interfaces/ASE_interface/abacuslite/io/latestio.py @@ -443,9 +443,6 @@ def read_abacus_out(fileobj, # read the eigenvalues (nframe, nk, nbnd) elecstate = read_band_from_eig_occ(fileobj.parent / 'eig_occ.txt') - # FIXME: remove thw following line till the eig_occ.txt is not written - # in the append mode - (fileobj.parent / 'eig_occ.txt').unlink() # read the atomic forces (nframe, nat, 3) forces = read_forces_from_running_log(abacus_lines) @@ -682,14 +679,16 @@ def test_read_pw_symm0_nspin4_gamma_md(self): # make files ready shutil.copy(self.testfiles / 'pw-symm0-nspin4-gamma-md', self.testfiles / 'running_md.log') + eig_occ_file = self.testfiles / 'eig_occ.txt' shutil.copy(self.testfiles / 'nspin4-gamma-eigocc', - self.testfiles / 'eig_occ.txt') + eig_occ_file) shutil.copy(self.testfiles / 'nspin4-gamma-mddump', self.testfiles / 'MD_dump') res = read_abacus_out(self.testfiles / 'running_md.log') self.assertIsNotNone(res) self.assertEqual(len(res), 2) # two frames + self.assertTrue(eig_occ_file.is_file()) for atoms in res: self.assertIsInstance(atoms, Atoms) @@ -701,7 +700,7 @@ def test_read_pw_symm0_nspin4_gamma_md(self): # remove the files (self.testfiles / 'running_md.log').unlink() - # (self.testfiles / 'eig_occ.txt').unlink() + eig_occ_file.unlink() (self.testfiles / 'MD_dump').unlink() def test_read_iter_header_from_running_log(self): diff --git a/source/source_io/module_energy/write_eig_occ.cpp b/source/source_io/module_energy/write_eig_occ.cpp index e589a66524..5a318d803f 100644 --- a/source/source_io/module_energy/write_eig_occ.cpp +++ b/source/source_io/module_energy/write_eig_occ.cpp @@ -212,8 +212,10 @@ void ModuleIO::write_eig_file(const ModuleBase::matrix &ekb, if (GlobalV::MY_RANK == 0) { std::ofstream ofs_eig0; + const bool append = istep > 0 + || (PARAM.inp.calculation == "md" && PARAM.inp.mdp.md_restart); - if(PARAM.inp.out_app_flag==true) + if (append) { ofs_eig0.open(filename.c_str(), std::ios::app); } diff --git a/source/source_io/test/write_eig_occ_test.cpp b/source/source_io/test/write_eig_occ_test.cpp index b7139ba093..e758601a87 100644 --- a/source/source_io/test/write_eig_occ_test.cpp +++ b/source/source_io/test/write_eig_occ_test.cpp @@ -97,9 +97,14 @@ TEST_F(IstateInfoTest, OutIstateInfoS1) ++i; } - // write eigenvalues and occupations - const int istep_in = -1; - ModuleIO::write_eig_file(ekb, wg, *kv, istep_in); + { + std::ofstream stale_file("eig_occ.txt"); + stale_file << "stale calculation" << std::endl; + } + + // A new calculation truncates stale output, then later ionic steps append. + ModuleIO::write_eig_file(ekb, wg, *kv, 0); + ModuleIO::write_eig_file(ekb, wg, *kv, 1); // check the output files std::ifstream ifs; @@ -108,7 +113,11 @@ TEST_F(IstateInfoTest, OutIstateInfoS1) EXPECT_THAT(str, testing::HasSubstr("Electronic state energy (eV) and occupations")); EXPECT_THAT(str, testing::HasSubstr("spin=1 k-point=1/10 Cartesian=0.0000000 0.0000000 0.0000000 (299 plane wave)")); EXPECT_THAT(str, testing::HasSubstr("1 2.040854700000000 0.000000000000000")); + EXPECT_THAT(str, testing::Not(testing::HasSubstr("stale calculation"))); + EXPECT_THAT(str, testing::HasSubstr("1 # ionic step")); + EXPECT_THAT(str, testing::HasSubstr("2 # ionic step")); ifs.close(); + remove("eig_occ.txt"); } From 175920f535790b49450d8732760cd29a2d932335 Mon Sep 17 00:00:00 2001 From: MoseyQAQ Date: Tue, 4 Aug 2026 12:53:10 +0800 Subject: [PATCH 2/3] update test --- interfaces/ASE_interface/abacuslite/io/latestio.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/interfaces/ASE_interface/abacuslite/io/latestio.py b/interfaces/ASE_interface/abacuslite/io/latestio.py index 39581fdffe..2b5e4ac39a 100644 --- a/interfaces/ASE_interface/abacuslite/io/latestio.py +++ b/interfaces/ASE_interface/abacuslite/io/latestio.py @@ -679,16 +679,14 @@ def test_read_pw_symm0_nspin4_gamma_md(self): # make files ready shutil.copy(self.testfiles / 'pw-symm0-nspin4-gamma-md', self.testfiles / 'running_md.log') - eig_occ_file = self.testfiles / 'eig_occ.txt' shutil.copy(self.testfiles / 'nspin4-gamma-eigocc', - eig_occ_file) + self.testfiles / 'eig_occ.txt') shutil.copy(self.testfiles / 'nspin4-gamma-mddump', self.testfiles / 'MD_dump') res = read_abacus_out(self.testfiles / 'running_md.log') self.assertIsNotNone(res) self.assertEqual(len(res), 2) # two frames - self.assertTrue(eig_occ_file.is_file()) for atoms in res: self.assertIsInstance(atoms, Atoms) @@ -700,7 +698,7 @@ def test_read_pw_symm0_nspin4_gamma_md(self): # remove the files (self.testfiles / 'running_md.log').unlink() - eig_occ_file.unlink() + (self.testfiles / 'eig_occ.txt').unlink() (self.testfiles / 'MD_dump').unlink() def test_read_iter_header_from_running_log(self): From 0ecf662656994598ad0b27546f9045c49dea64b9 Mon Sep 17 00:00:00 2001 From: MoseyQAQ Date: Tue, 4 Aug 2026 13:01:35 +0800 Subject: [PATCH 3/3] update to make check pass --- source/source_io/module_energy/write_eig_occ.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/source_io/module_energy/write_eig_occ.cpp b/source/source_io/module_energy/write_eig_occ.cpp index 5a318d803f..ff5d9ea4cd 100644 --- a/source/source_io/module_energy/write_eig_occ.cpp +++ b/source/source_io/module_energy/write_eig_occ.cpp @@ -212,8 +212,9 @@ void ModuleIO::write_eig_file(const ModuleBase::matrix &ekb, if (GlobalV::MY_RANK == 0) { std::ofstream ofs_eig0; + const auto& inp = PARAM.inp; const bool append = istep > 0 - || (PARAM.inp.calculation == "md" && PARAM.inp.mdp.md_restart); + || (inp.calculation == "md" && inp.mdp.md_restart); if (append) {