Skip to content

Commit abf77eb

Browse files
19helloFei Yang
andauthored
Add initialization checking in BFGS, and replace the old BFGS with a new one in CG-BFGS. (#7117)
* Optimize install_openmpi.sh script * fix cg-bfgs problem, add initialization in bfgs * fix cg-bfgs problem, add initialization in bfgs * fix cg-bfgs problem, add initialization in bfgs * fix cg-bfgs problem, add initialization in bfgs * fix cg-bfgs problem, add initialization in bfgs * fix cg-bfgs problem, add initialization in bfgs * fix cg-bfgs problem, add initialization in bfgs --------- Co-authored-by: Fei Yang <2501213217@stu.pku.edu.cn>
1 parent d1c9536 commit abf77eb

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

source/source_relax/bfgs.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ void BFGS::allocate(const int _size)
2929
force0 = std::vector<double>(3*size, 0.0);
3030
force = std::vector<ModuleBase::Vector3<double>>(size, ModuleBase::Vector3<double>(0.0, 0.0, 0.0));
3131
steplength = std::vector<double>(size, 0.0);
32+
is_initialized = true;
3233
}
3334

3435

3536
void BFGS::relax_step(const ModuleBase::matrix& _force,UnitCell& ucell)
3637
{
38+
if(!is_initialized)
39+
{
40+
allocate(ucell.nat);
41+
}
3742
GetPos(ucell,pos);
3843
GetPostaud(ucell,pos_taud);
3944
ucell.ionic_position_updated = true;

source/source_relax/bfgs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class BFGS
2424
double maxstep;//every movement smaller than maxstep
2525
double largest_grad;
2626
int size;//number of atoms
27+
bool is_initialized=false;
2728

2829
std::vector<double> steplength;//the length of atoms displacement
2930
std::vector<std::vector<double>> H;//Hessian matrix

source/source_relax/ions_move_cg.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "ions_move_basic.h"
33
#include "source_base/global_function.h"
44
#include "source_base/global_variable.h"
5+
56
using namespace Ions_Move_Basic;
67

78
double Ions_Move_CG::RELAX_CG_THR = -1.0; // default is 0.5
@@ -169,7 +170,7 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
169170
< RELAX_CG_THR) // cg to bfgs by pengfei 13-8-8
170171
{
171172
Ions_Move_Basic::relax_method[0] = "bfgs";
172-
Ions_Move_Basic::relax_method[1] = "2";
173+
Ions_Move_Basic::relax_method[1] = "1";
173174
}
174175
Ions_Move_Basic::best_xxx = steplength;
175176
}

source/source_relax/test/bfgs_test.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,34 @@ TEST_F(BFGSTest, TestAllocate)
5454
EXPECT_EQ(bfgs.largest_grad,0.0);
5555
}
5656

57+
// Test that relax_step will auto-initialize if not already initialized
58+
TEST_F(BFGSTest, RelaxStepAutoInitialize)
59+
{
60+
bfgs.is_initialized = false;
61+
62+
UnitCell ucell;
63+
ucell.nat = 2;
64+
ucell.ntype = 1;
65+
ucell.atoms = new Atom[ucell.ntype];
66+
ucell.atoms[0].na = 2;
67+
ucell.atoms[0].tau = std::vector<ModuleBase::Vector3<double>>(2);
68+
ucell.atoms[0].taud = std::vector<ModuleBase::Vector3<double>>(2);
69+
ucell.atoms[0].mbl = std::vector<ModuleBase::Vector3<int>>(2, {1, 1, 1});
70+
ucell.atoms[0].tau[0].x = 0.0; ucell.atoms[0].tau[0].y = 0.0; ucell.atoms[0].tau[0].z = 0.0;
71+
ucell.atoms[0].tau[1].x = 1.0; ucell.atoms[0].tau[1].y = 0.0; ucell.atoms[0].tau[1].z = 0.0;
72+
ucell.lat0 = 1.0;
73+
74+
ModuleBase::matrix force(2, 3);
75+
force(0, 0) = 0.1; force(0, 1) = 0.0; force(0, 2) = 0.0;
76+
force(1, 0) = -0.1; force(1, 1) = 0.0; force(1, 2) = 0.0;
77+
78+
// Before relax_step, is_initialized should be false
79+
EXPECT_FALSE(bfgs.is_initialized);
80+
bfgs.relax_step(force, ucell);
81+
// After relax_step, is_initialized should be true
82+
EXPECT_TRUE(bfgs.is_initialized);
83+
}
84+
5785
// Test if a dimension less than or equal to 0 results in an assertion error
5886
TEST_F(BFGSTest, TestAllocateWithZeroDimension)
5987
{
@@ -103,10 +131,8 @@ TEST_F(BFGSTest, GetPosAndPostaud)
103131
ucell.atoms[0].mbl = std::vector<ModuleBase::Vector3<int>>(2, {1, 1, 1});
104132

105133
// set coordinates
106-
ucell.atoms[0].tau[0].x = 1.0; ucell.atoms[0].tau[0].y = 2.0; ucell.atoms[0].tau[0].z = 3.0;
107-
ucell.atoms[0].tau[1].x = 2.0; ucell.atoms[0].tau[1].y = 3.0; ucell.atoms[0].tau[1].z = 4.0;
108-
ucell.atoms[0].taud[0].x = 0.1; ucell.atoms[0].taud[0].y = 0.2; ucell.atoms[0].taud[0].z = 0.3;
109-
ucell.atoms[0].taud[1].x = 0.4; ucell.atoms[0].taud[1].y = 0.5; ucell.atoms[0].taud[1].z = 0.6;
134+
ucell.atoms[0].tau[0].x = 0.0; ucell.atoms[0].tau[0].y = 0.0; ucell.atoms[0].tau[0].z = 0.0;
135+
ucell.atoms[0].tau[1].x = 1.0; ucell.atoms[0].tau[1].y = 0.0; ucell.atoms[0].tau[1].z = 0.0;
110136

111137
// allocate mapping arrays
112138
ucell.iat2it = new int[ucell.nat];

0 commit comments

Comments
 (0)