Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 13 additions & 1 deletion source/source_relax/ions_move_bfgs2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -239,7 +246,12 @@ void Ions_Move_BFGS2::Update(std::vector<double>& 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)
Comment thread
mohanchen marked this conversation as resolved.
{
return;
}
Expand Down
31 changes: 30 additions & 1 deletion source/source_relax/test/bfgs_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModuleBase::Vector3<int>>(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<double> pos = {0.0, 0.0, 0.0};
std::vector<double> 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)
{
Expand Down Expand Up @@ -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);
}
}
Loading