66void BFGS::init_relax (const int _size,UnitCell& ucell,double _maxstep) // initialize H0、H、pos0、force0、force
77{
88 alpha=70 ;// relax_scale_force
9- maxstep=_maxstep;
9+ maxstep=_maxstep;
10+ if (maxstep==0 )
11+ {
12+ maxstep=0.8 ;
13+ }
1014 size=_size;
1115 sign =true ;
1216 H = std::vector<std::vector<double >>(3 *size, std::vector<double >(3 *size, 0.0 ));
@@ -40,18 +44,19 @@ bool BFGS::relax_step(ModuleBase::matrix _force,UnitCell& ucell)
4044 // std::cout<<"enter Step0"<<std::endl;
4145 // std::cout<<size<<std::endl;
4246 // GetPos(ucell);
43- this ->PrepareStep (force,pos,H,pos0,force0,steplength);
47+ this ->PrepareStep (force,pos,H,pos0,force0,steplength,dpos );
4448 // std::cout<<"enter Step1"<<std::endl;
4549 this ->DetermineStep (steplength,dpos,maxstep);
50+
4651 std::cout<<" enter Step2" <<std::endl;
47- for (int i=0 ;i<size;i++)
52+ /* for(int i=0;i<size;i++)
4853 {
4954 for(int j=0;j<3;j++)
5055 {
5156 std::cout<<dpos[i][j]<<' ';
5257 }
5358 std::cout<<std::endl;
54- }
59+ }*/
5560 this ->UpdatePos (ucell);
5661 // std::cout<<"enter Step3"<<std::endl;
5762 return this ->IsRestrain (dpos);
@@ -73,7 +78,7 @@ void BFGS::GetPos(UnitCell& ucell,std::vector<std::vector<double>>& pos)
7378}
7479
7580
76- void BFGS::PrepareStep (std::vector<std::vector<double >>& force,std::vector<std::vector<double >>& pos,std::vector<std::vector<double >>& H,std::vector<double >& pos0,std::vector<double >& force0,std::vector<double >& steplength)
81+ void BFGS::PrepareStep (std::vector<std::vector<double >>& force,std::vector<std::vector<double >>& pos,std::vector<std::vector<double >>& H,std::vector<double >& pos0,std::vector<double >& force0,std::vector<double >& steplength,std::vector<std::vector< double >>& dpos )
7782{
7883 // std::cout<<"enter prepareStep0"<<std::endl;
7984 std::vector<double > changedforce = this ->ReshapeMToV (force);
@@ -124,13 +129,29 @@ void BFGS::PrepareStep(std::vector<std::vector<double>>& force,std::vector<std::
124129 std::vector<double > a=this ->DotInMAndV2 (V, changedforce);
125130 for (int i = 0 ; i < a.size (); i++)
126131 {
127- a[i] /= abs (omega[i]);
132+ if (omega[i]>0 )
133+ {
134+ a[i] /= omega[i];
135+ }
136+ else if (omega[i]<0 )
137+ {
138+ a[i] /= (-omega[i]);
139+ }
140+
128141 }
129142
130143 std::vector<double > tmpdpos = this ->DotInMAndV1 (V, a);
131144
132145
133146 dpos = this ->ReshapeVToM (tmpdpos);
147+ /* for(int i=0;i<size;i++)
148+ {
149+ for(int j=0;j<3;j++)
150+ {
151+ std::cout<<dpos[i][j]<<' ';
152+ }
153+ std::cout<<std::endl;
154+ }*/
134155 for (int i = 0 ; i < size; i++)
135156 {
136157 double k = 0 ;
@@ -176,7 +197,7 @@ void BFGS::Update(std::vector<double> pos, std::vector<double> force,std::vector
176197 H = this ->MSubM (H, this ->MPlus (this ->OuterVAndV (dg, dg), b));
177198}
178199
179- void BFGS::DetermineStep (std::vector<double > steplength,std::vector<std::vector<double >>& dpos,int maxstep)
200+ void BFGS::DetermineStep (std::vector<double > steplength,std::vector<std::vector<double >>& dpos,double maxstep)
180201{
181202 auto maxsteplength = max_element (steplength.begin (), steplength.end ());
182203 double a = *maxsteplength;
0 commit comments