Skip to content

Commit d1ec60e

Browse files
Update bfgs.cpp (#5656)
Co-authored-by: Liang Sun <[email protected]>
1 parent 6d26194 commit d1ec60e

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

source/module_relax/relax_old/bfgs.cpp

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44
#include "module_parameter/parameter.h"
55
#include "ions_move_basic.h"
66

7-
void BFGS::allocate(const int _size) // initialize H0、H、pos0、force0、force
7+
//! initialize H0、H、pos0、force0、force
8+
void BFGS::allocate(const int _size)
89
{
910
alpha=70;
1011
maxstep=PARAM.inp.relax_bfgs_rmax;
1112
size=_size;
1213
sign =true;
14+
1315
H = std::vector<std::vector<double>>(3*size, std::vector<double>(3*size, 0.0));
14-
for (int i = 0; i < 3*size; ++i) {
16+
17+
for (int i = 0; i < 3*size; ++i)
18+
{
1519
H[i][i] = alpha;
1620
}
21+
1722
pos = std::vector<std::vector<double>> (size, std::vector<double>(3, 0.0));
1823
pos0 = std::vector<double>(3*size, 0.0);
1924
pos_taud = std::vector<std::vector<double>> (size, std::vector<double>(3, 0.0));
@@ -24,7 +29,8 @@ void BFGS::allocate(const int _size) // initialize H0、H、pos0、force0、forc
2429
steplength = std::vector<double>(size, 0.0);
2530
}
2631

27-
void BFGS::relax_step(ModuleBase::matrix _force,UnitCell& ucell)
32+
void BFGS::relax_step(ModuleBase::matrix _force,
33+
UnitCell& ucell)
2834
{
2935
GetPos(ucell,pos);
3036
GetPostaud(ucell,pos_taud);
@@ -36,6 +42,7 @@ void BFGS::relax_step(ModuleBase::matrix _force,UnitCell& ucell)
3642
force[i][j]=_force(i,j)*ModuleBase::Ry_to_eV/ModuleBase::BOHR_TO_A;
3743
}
3844
}
45+
3946
int k=0;
4047
for(int i=0;i<ucell.ntype;i++)
4148
{
@@ -56,8 +63,10 @@ void BFGS::relax_step(ModuleBase::matrix _force,UnitCell& ucell)
5663
}
5764
k+=ucell.atoms[i].na;
5865
}
66+
5967
this->PrepareStep(force,pos,H,pos0,force0,steplength,dpos,ucell);
6068
this->DetermineStep(steplength,dpos,maxstep);
69+
6170
/*std::cout<<"force"<<std::endl;
6271
for(int i=0;i<size;i++)
6372
{
@@ -85,6 +94,7 @@ void BFGS::relax_step(ModuleBase::matrix _force,UnitCell& ucell)
8594
}
8695
std::cout<<std::endl;
8796
}*/
97+
8898
this->UpdatePos(ucell);
8999
this->CalculateLargestGrad(_force,ucell);
90100
this->IsRestrain(dpos);
@@ -105,7 +115,8 @@ void BFGS::GetPos(UnitCell& ucell,std::vector<std::vector<double>>& pos)
105115
}
106116
}
107117

108-
void BFGS::GetPostaud(UnitCell& ucell,std::vector<std::vector<double>>& pos_taud)
118+
void BFGS::GetPostaud(UnitCell& ucell,
119+
std::vector<std::vector<double>>& pos_taud)
109120
{
110121
int k=0;
111122
for(int i=0;i<ucell.ntype;i++)
@@ -121,27 +132,30 @@ void BFGS::GetPostaud(UnitCell& ucell,std::vector<std::vector<double>>& pos_taud
121132
}
122133

123134
void BFGS::PrepareStep(std::vector<std::vector<double>>& force,
124-
std::vector<std::vector<double>>& pos,
125-
std::vector<std::vector<double>>& H,
126-
std::vector<double>& pos0,
127-
std::vector<double>& force0,
128-
std::vector<double>& steplength,
129-
std::vector<std::vector<double>>& dpos,
130-
UnitCell& ucell)
135+
std::vector<std::vector<double>>& pos,
136+
std::vector<std::vector<double>>& H,
137+
std::vector<double>& pos0,
138+
std::vector<double>& force0,
139+
std::vector<double>& steplength,
140+
std::vector<std::vector<double>>& dpos,
141+
UnitCell& ucell)
131142
{
132143
std::vector<double> changedforce = this->ReshapeMToV(force);
133144
std::vector<double> changedpos = this->ReshapeMToV(pos);
134145
this->Update(changedpos, changedforce,H,ucell);
135-
//call dysev
146+
147+
//! call dysev
136148
std::vector<double> omega(3*size);
137149
std::vector<double> work(3*size*3*size);
138150
int lwork=3*size*3*size;
139151
int info=0;
140152
std::vector<double> H_flat;
153+
141154
for(const auto& row : H)
142155
{
143156
H_flat.insert(H_flat.end(), row.begin(), row.end());
144-
}
157+
}
158+
145159
int value=3*size;
146160
int* ptr=&value;
147161
dsyev_("V","U",ptr,H_flat.data(),ptr,omega.data(),work.data(),&lwork,&info);
@@ -183,7 +197,10 @@ UnitCell& ucell)
183197
force0 = this->ReshapeMToV(force);
184198
}
185199

186-
void BFGS::Update(std::vector<double>& pos, std::vector<double>& force,std::vector<std::vector<double>>& H,UnitCell& ucell)
200+
void BFGS::Update(std::vector<double>& pos,
201+
std::vector<double>& force,
202+
std::vector<std::vector<double>>& H,
203+
UnitCell& ucell)
187204
{
188205
if(sign)
189206
{
@@ -261,10 +278,12 @@ void BFGS::Update(std::vector<double>& pos, std::vector<double>& force,std::vect
261278
{
262279
return;
263280
}
281+
264282
std::vector<double> dforce = this->VSubV(force, force0);
265283
double a = this->DotInVAndV(dpos, dforce);
266284
std::vector<double> dg = this->DotInMAndV1(H, dpos);
267285
double b = this->DotInVAndV(dpos, dg);
286+
268287
/*std::cout<<"a"<<std::endl;
269288
std::cout<<a<<std::endl;
270289
std::cout<<"b"<<std::endl;
@@ -277,7 +296,9 @@ void BFGS::Update(std::vector<double>& pos, std::vector<double>& force,std::vect
277296
H = this->MSubM(H, term4);
278297
}
279298

280-
void BFGS::DetermineStep(std::vector<double>& steplength,std::vector<std::vector<double>>& dpos,double& maxstep)
299+
void BFGS::DetermineStep(std::vector<double>& steplength,
300+
std::vector<std::vector<double>>& dpos,
301+
double& maxstep)
281302
{
282303
auto maxsteplength = max_element(steplength.begin(), steplength.end());
283304
double a = *maxsteplength;
@@ -332,7 +353,6 @@ void BFGS::UpdatePos(UnitCell& ucell)
332353
//convert pos
333354
ModuleBase::Vector3<double> move_ion_dr = move_ion_cart* ucell.GT;
334355
335-
336356
int it = ucell.iat2it[iat];
337357
int ia = ucell.iat2ia[iat];
338358
Atom* atom = &ucell.atoms[it];
@@ -356,7 +376,8 @@ void BFGS::UpdatePos(UnitCell& ucell)
356376

357377
void BFGS::IsRestrain(std::vector<std::vector<double>>& dpos)
358378
{
359-
Ions_Move_Basic::converged = Ions_Move_Basic::largest_grad * ModuleBase::Ry_to_eV / 0.529177<PARAM.inp.force_thr_ev;
379+
Ions_Move_Basic::converged = Ions_Move_Basic::largest_grad
380+
* ModuleBase::Ry_to_eV / 0.529177<PARAM.inp.force_thr_ev;
360381
}
361382

362383
void BFGS::CalculateLargestGrad(ModuleBase::matrix& _force,UnitCell& ucell)
@@ -389,7 +410,8 @@ void BFGS::CalculateLargestGrad(ModuleBase::matrix& _force,UnitCell& ucell)
389410
Ions_Move_Basic::largest_grad /= ucell.lat0;
390411
if (PARAM.inp.out_level == "ie")
391412
{
392-
std::cout << " LARGEST GRAD (eV/A) : " << Ions_Move_Basic::largest_grad * ModuleBase::Ry_to_eV / 0.5291772109
413+
std::cout << " LARGEST GRAD (eV/A) : " << Ions_Move_Basic::largest_grad
414+
* ModuleBase::Ry_to_eV / 0.5291772109
393415
<< std::endl;
394416
}
395417

@@ -407,7 +429,8 @@ std::vector<double> BFGS::ReshapeMToV(std::vector<std::vector<double>>& matrix)
407429
return result;
408430
}
409431

410-
std::vector<std::vector<double>> BFGS::MAddM(std::vector<std::vector<double>>& a, std::vector<std::vector<double>>& b)
432+
std::vector<std::vector<double>> BFGS::MAddM(std::vector<std::vector<double>>& a,
433+
std::vector<std::vector<double>>& b)
411434
{
412435
std::vector<std::vector<double>> result = std::vector<std::vector<double>>(a.size(), std::vector<double>(a[0].size(), 0.0));
413436
for(int i = 0; i < a.size(); i++)
@@ -515,4 +538,4 @@ std::vector<std::vector<double>> BFGS::MSubM(std::vector<std::vector<double>>& a
515538
}
516539
}
517540
return result;
518-
}
541+
}

0 commit comments

Comments
 (0)