1010// ! initialize H0、H、pos0、force0、force
1111void BFGS::allocate (const int _size)
1212{
13+ assert (_size > 0 );
1314 alpha=70 ;// default value in ase is 70
1415 maxstep=PARAM.inp .relax_bfgs_rmax ;
1516 size=_size;
16- sign =true ;
17+ largest_grad=0.0 ;
18+ sign=true ;
1719 H = std::vector<std::vector<double >>(3 *size, std::vector<double >(3 *size, 0.0 ));
1820
1921 for (int i = 0 ; i < 3 *size; ++i)
@@ -37,6 +39,7 @@ void BFGS::relax_step(const ModuleBase::matrix& _force,UnitCell& ucell)
3739 GetPos (ucell,pos);
3840 GetPostaud (ucell,pos_taud);
3941 ucell.ionic_position_updated = true ;
42+ assert (_force.nr == force.size () && _force.nc == force[0 ].size ());
4043 for (int i = 0 ; i < _force.nr ; i++)
4144 {
4245 for (int j=0 ;j<_force.nc ;j++)
@@ -65,7 +68,7 @@ void BFGS::relax_step(const ModuleBase::matrix& _force,UnitCell& ucell)
6568 k+=ucell.atoms [i].na ;
6669 }
6770
68- this ->PrepareStep (force,pos,H,pos0,force0,steplength,dpos,ucell);
71+ this ->PrepareStep (force,pos,H,pos0,force0,steplength,dpos,size, ucell);
6972 this ->DetermineStep (steplength,dpos,maxstep);
7073 this ->UpdatePos (ucell);
7174 this ->CalculateLargestGrad (_force,ucell);
@@ -76,6 +79,12 @@ void BFGS::relax_step(const ModuleBase::matrix& _force,UnitCell& ucell)
7679
7780void BFGS::GetPos (UnitCell& ucell,std::vector<std::vector<double >>& pos)
7881{
82+ int total_atoms = 0 ;
83+ for (int i = 0 ; i < ucell.ntype ; i++)
84+ {
85+ total_atoms += ucell.atoms [i].na ;
86+ }
87+ assert (pos.size () == total_atoms);
7988 int k=0 ;
8089 for (int i=0 ;i<ucell.ntype ;i++)
8190 {
@@ -92,6 +101,12 @@ void BFGS::GetPos(UnitCell& ucell,std::vector<std::vector<double>>& pos)
92101void BFGS::GetPostaud (UnitCell& ucell,
93102 std::vector<std::vector<double >>& pos_taud)
94103{
104+ int total_atoms = 0 ;
105+ for (int i = 0 ; i < ucell.ntype ; i++)
106+ {
107+ total_atoms += ucell.atoms [i].na ;
108+ }
109+ assert (pos_taud.size () == total_atoms);
95110 int k=0 ;
96111 for (int i=0 ;i<ucell.ntype ;i++)
97112 {
@@ -112,6 +127,7 @@ void BFGS::PrepareStep(std::vector<std::vector<double>>& force,
112127 std::vector<double >& force0,
113128 std::vector<double >& steplength,
114129 std::vector<std::vector<double >>& dpos,
130+ int & size,
115131 UnitCell& ucell)
116132{
117133 std::vector<double > changedforce = ReshapeMToV (force);
@@ -144,6 +160,7 @@ void BFGS::PrepareStep(std::vector<std::vector<double>>& force,
144160 std::vector<double > a=DotInMAndV2 (V, changedforce);
145161 for (int i = 0 ; i < a.size (); i++)
146162 {
163+ assert (std::abs (omega[i]) > 1e-8 );
147164 a[i]/=std::abs (omega[i]);
148165 }
149166 std::vector<double > tmpdpos = DotInMAndV1 (V, a);
@@ -243,6 +260,7 @@ void BFGS::DetermineStep(std::vector<double>& steplength,
243260{
244261 std::vector<double >::iterator maxsteplength = max_element (steplength.begin (), steplength.end ());
245262 double a = *maxsteplength;
263+ assert (a > 1e-10 );
246264 if (a >= maxstep)
247265 {
248266 double scale = maxstep / a;
@@ -311,8 +329,8 @@ void BFGS::UpdatePos(UnitCell& ucell)
311329
312330void BFGS::IsRestrain (std::vector<std::vector<double >>& dpos)
313331{
314- Ions_Move_Basic::converged = Ions_Move_Basic:: largest_grad
315- * ModuleBase::Ry_to_eV / 0.529177 <PARAM.inp .force_thr_ev ;
332+ Ions_Move_Basic::converged = largest_grad
333+ * ModuleBase::Ry_to_eV / ModuleBase::BOHR_TO_A <PARAM.inp .force_thr_ev ;
316334}
317335
318336void BFGS::CalculateLargestGrad (const ModuleBase::matrix& _force,UnitCell& ucell)
@@ -334,20 +352,20 @@ void BFGS::CalculateLargestGrad(const ModuleBase::matrix& _force,UnitCell& ucell
334352 ++iat;
335353 }
336354 }
337- Ions_Move_Basic:: largest_grad = 0.0 ;
355+ largest_grad = 0.0 ;
338356 for (int i = 0 ; i < 3 *size; i++)
339357 {
340- if (Ions_Move_Basic:: largest_grad < std::abs (grad[i]))
358+ if (largest_grad < std::abs (grad[i]))
341359 {
342- Ions_Move_Basic:: largest_grad = std::abs (grad[i]);
360+ largest_grad = std::abs (grad[i]);
343361 }
344362 }
363+ assert (ucell.lat0 != 0 );
345364 Ions_Move_Basic::largest_grad /= ucell.lat0 ;
346365 if (PARAM.inp .out_level == " ie" )
347366 {
348- std::cout << " LARGEST GRAD (eV/Angstrom) : " << Ions_Move_Basic:: largest_grad
349- * ModuleBase::Ry_to_eV / 0.5291772109
367+ std::cout << " LARGEST GRAD (eV/Angstrom) : " << largest_grad
368+ * ModuleBase::Ry_to_eV / ModuleBase::BOHR_TO_A
350369 << std::endl;
351370 }
352-
353371}
0 commit comments