From da58ef9dd47fbdd04a6882708691afb74b9f4842 Mon Sep 17 00:00:00 2001 From: Fei Yang <2501213217@stu.pku.edu.cn> Date: Thu, 18 Jun 2026 14:13:08 +0800 Subject: [PATCH 1/3] fix bfgs bug --- source/source_relax/ions_move_bfgs2.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/source/source_relax/ions_move_bfgs2.cpp b/source/source_relax/ions_move_bfgs2.cpp index ee136cac69..c043d77712 100644 --- a/source/source_relax/ions_move_bfgs2.cpp +++ b/source/source_relax/ions_move_bfgs2.cpp @@ -239,7 +239,12 @@ void Ions_Move_BFGS2::Update(std::vector& pos, } } double threshold=1e-7; - if(*max_element(dpos.begin(), dpos.end()) < threshold) + double max_abs_dpos = 0.0; + for (const double value : dpos) + { + max_abs_dpos = std::max(max_abs_dpos, std::abs(value)); + } + if (max_abs_dpos < threshold) { return; } From 2c61bb96210b3cfe034f573da613d045815a71b4 Mon Sep 17 00:00:00 2001 From: Fei Yang <2501213217@stu.pku.edu.cn> Date: Thu, 18 Jun 2026 15:48:52 +0800 Subject: [PATCH 2/3] fix bfgs bug --- source/source_relax/test/bfgs_test.cpp | 31 +++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/source/source_relax/test/bfgs_test.cpp b/source/source_relax/test/bfgs_test.cpp index 4a8ddcf119..fdeaf04e8c 100644 --- a/source/source_relax/test/bfgs_test.cpp +++ b/source/source_relax/test/bfgs_test.cpp @@ -118,6 +118,35 @@ TEST_F(BFGSTest, DetermineStepScaling) EXPECT_NEAR(dpos[1][2], 0.05, 1e-12); } +TEST_F(BFGSTest, UpdateUsesAbsoluteDisplacementThreshold) +{ + UnitCell ucell; + ucell.ntype = 1; + ucell.nat = 1; + ucell.lat0 = 1.0; + ucell.latvec.Identity(); + ucell.atoms = new Atom[ucell.ntype]; + ucell.atoms[0].na = 1; + ucell.atoms[0].mbl = std::vector>(1, {1, 1, 1}); + ucell.iat2it = new int[ucell.nat]; + ucell.iat2ia = new int[ucell.nat]; + ucell.iat2it[0] = 0; + ucell.iat2ia[0] = 0; + + bfgs.allocate(ucell.nat); + bfgs.sign = false; + bfgs.pos_taud[0].x = -1.0e-6; + bfgs.force0 = {0.0, 0.0, 0.0}; + + std::vector pos = {0.0, 0.0, 0.0}; + std::vector force = {1.0, 0.0, 0.0}; + const double initial_h00 = bfgs.H[0][0]; + + bfgs.Update(pos, force, bfgs.H, ucell); + + EXPECT_NE(bfgs.H[0][0], initial_h00); +} + // Test GetPos and GetPostaud without creating extra helper class TEST_F(BFGSTest, GetPosAndPostaud) { @@ -245,4 +274,4 @@ TEST_F(BFGSTest, RelaxStepBasic) // Check that positions are updated (not equal to initial) EXPECT_NEAR(bfgs.pos[0][0], 0.0, 1e-12); EXPECT_NE(bfgs.pos[1][0], 1.0); -} \ No newline at end of file +} From a9f6a6cb4444be7c362c955f1ba70121c0485ccb Mon Sep 17 00:00:00 2001 From: Fei Yang <2501213217@stu.pku.edu.cn> Date: Thu, 18 Jun 2026 16:01:02 +0800 Subject: [PATCH 3/3] fix bfgs bug --- source/source_relax/ions_move_bfgs2.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/source/source_relax/ions_move_bfgs2.cpp b/source/source_relax/ions_move_bfgs2.cpp index c043d77712..6df3b72e42 100644 --- a/source/source_relax/ions_move_bfgs2.cpp +++ b/source/source_relax/ions_move_bfgs2.cpp @@ -80,6 +80,13 @@ bool Ions_Move_BFGS2::relax_step(const ModuleBase::matrix& _force,UnitCell& ucel this->UpdatePos(ucell); this->CalculateLargestGrad(_force,ucell); bool converged = this->IsRestrain(); + if (converged) + { + ofs_running << "\n Largest force is " << largest_grad * ModuleBase::Ry_to_eV / ModuleBase::BOHR_TO_A + << " eV/Angstrom while threshold is " + << PARAM.inp.force_thr_ev << " eV/Angstrom" << std::endl; + ofs_running << "\n Ion relaxation is converged!" << std::endl; + } // print out geometry information during bfgs_trad relax unitcell::print_tau(ucell.atoms,ucell.Coordinate,ucell.ntype,ucell.lat0,ofs_running); return converged;