Skip to content

Commit a02316b

Browse files
committed
bfgs_trad
1 parent ca6e24e commit a02316b

File tree

9 files changed

+61
-40
lines changed

9 files changed

+61
-40
lines changed

source/module_relax/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ add_library(
66

77
relax_new/relax.cpp
88
relax_new/line_search.cpp
9-
relax_new/bfgs.cpp
10-
9+
10+
relax_old/bfgs.cpp
1111
relax_old/relax_old.cpp
1212
relax_old/bfgs_basic.cpp
1313
relax_old/ions_move_basic.cpp

source/module_relax/relax_driver.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ void Relax_Driver::relax_driver(ModuleESolver::ESolver* p_esolver)
1616

1717
if (PARAM.inp.calculation == "relax" || PARAM.inp.calculation == "cell-relax" )
1818
{
19-
if(PARAM.inp.relax_method == "bfgs_trad")
19+
/*if(PARAM.inp.relax_method=="bfgs_trad")
2020
{
21-
bfgs_trad.init_relax(GlobalC::ucell.nat,GlobalC::ucell,PARAM.inp.relax_bfgs_rmax);
21+
bfgs_trad.allocate(GlobalC::ucell.nat);
2222
}
23-
else if (!PARAM.inp.relax_new)
23+
else*/ if (!PARAM.inp.relax_new)
2424
{
2525
rl_old.init_relax(GlobalC::ucell.nat);
2626
}
@@ -82,12 +82,12 @@ void Relax_Driver::relax_driver(ModuleESolver::ESolver* p_esolver)
8282

8383
if (PARAM.inp.calculation == "relax" || PARAM.inp.calculation == "cell-relax")
8484
{
85-
if(PARAM.inp.relax_method == "bfgs_trad")
85+
/*if(PARAM.inp.relax_method == "bfgs_trad")
8686
{
8787
stop=bfgs_trad.relax_step(force,GlobalC::ucell);
8888
}
8989
90-
else if (PARAM.inp.relax_new)
90+
else*/ if (PARAM.inp.relax_new)
9191
{
9292
stop = rl.relax_step(force, stress, this->etot);
9393
}

source/module_relax/relax_driver.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "module_esolver/esolver_ks.h"
66
#include "relax_new/relax.h"
77
#include "relax_old/relax_old.h"
8-
#include "relax_new/bfgs.h"
8+
#include "relax_old/bfgs.h"
99
class Relax_Driver
1010
{
1111

@@ -28,6 +28,8 @@ class Relax_Driver
2828
Relax_old rl_old;
2929

3030
BFGS bfgs_trad;
31+
32+
3133
};
3234

3335
#endif

source/module_relax/relax_new/bfgs.cpp renamed to source/module_relax/relax_old/bfgs.cpp

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
#include "module_hamilt_pw/hamilt_pwdft/global.h"
33
#include "module_base/matrix3.h"
44
#include "module_relax/relax_old/ions_move_basic.h"
5+
#include "module_parameter/parameter.h"
6+
#include "ions_move_basic.h"
57

6-
void BFGS::init_relax(const int _size,UnitCell& ucell,double _maxstep) // initialize H0、H、pos0、force0、force
8+
9+
void BFGS::allocate(const int _size) // initialize H0、H、pos0、force0、force
710
{
811
alpha=70;//relax_scale_force
9-
maxstep=_maxstep;
12+
maxstep=PARAM.inp.relax_bfgs_rmax;
1013
if(maxstep==0)
1114
{
1215
maxstep=0.8;
@@ -23,13 +26,19 @@ void BFGS::init_relax(const int _size,UnitCell& ucell,double _maxstep) // initia
2326
force0 = std::vector<double>(3*size, 0.0);
2427
force = std::vector<std::vector<double>>(size, std::vector<double>(3, 0.0));
2528
steplength = std::vector<double>(size, 0.0);
26-
this->GetPos(ucell,pos);
29+
this->GetPos(GlobalC::ucell,pos);
30+
2731

2832
}
2933

30-
bool BFGS::relax_step(ModuleBase::matrix _force,UnitCell& ucell)
34+
void BFGS::relax_step(ModuleBase::matrix _force,UnitCell& ucell)
3135
{
3236
std::cout<<"enter Step"<<std::endl;
37+
if(sign)
38+
{
39+
40+
}
41+
std::cout<<"enter Step1"<<std::endl;
3342
ucell.ionic_position_updated = true;
3443
for(int i = 0; i < _force.nr; i++)
3544
{
@@ -59,21 +68,21 @@ bool BFGS::relax_step(ModuleBase::matrix _force,UnitCell& ucell)
5968
}*/
6069
this->UpdatePos(ucell);
6170
//std::cout<<"enter Step3"<<std::endl;
62-
return this->IsRestrain(dpos);
71+
this->IsRestrain(dpos);
6372
}
6473

6574
void BFGS::GetPos(UnitCell& ucell,std::vector<std::vector<double>>& pos)
6675
{
6776
int k=0;
68-
for(int i=0;i<GlobalC::ucell.ntype;i++)
77+
for(int i=0;i<ucell.ntype;i++)
6978
{
70-
for(int j=0;j<GlobalC::ucell.atoms[i].na;j++)
79+
for(int j=0;j<ucell.atoms[i].na;j++)
7180
{
72-
pos[k+j][0]=GlobalC::ucell.atoms[i].tau[j].x*ModuleBase::BOHR_TO_A*GlobalC::ucell.lat0;
73-
pos[k+j][1]=GlobalC::ucell.atoms[i].tau[j].y*ModuleBase::BOHR_TO_A*GlobalC::ucell.lat0;
74-
pos[k+j][2]=GlobalC::ucell.atoms[i].tau[j].z*ModuleBase::BOHR_TO_A*GlobalC::ucell.lat0;
81+
pos[k+j][0]=ucell.atoms[i].tau[j].x*ModuleBase::BOHR_TO_A*ucell.lat0;
82+
pos[k+j][1]=ucell.atoms[i].tau[j].y*ModuleBase::BOHR_TO_A*ucell.lat0;
83+
pos[k+j][2]=ucell.atoms[i].tau[j].z*ModuleBase::BOHR_TO_A*ucell.lat0;
7584
}
76-
k+=GlobalC::ucell.atoms[i].na;
85+
k+=ucell.atoms[i].na;
7786
}
7887
}
7988

@@ -86,14 +95,14 @@ void BFGS::PrepareStep(std::vector<std::vector<double>>& force,std::vector<std::
8695
//std::cout<<"enter prepareStep1"<<std::endl;
8796
this->Update(changedpos, changedforce,H);
8897
//std::cout<<"enter prepareStep2"<<std::endl;
89-
for(int i = 0; i < 3*size; i++)
98+
/*for(int i = 0; i < 3*size; i++)
9099
{
91100
for(int j = 0; j < 3*size; j++)
92101
{
93102
std::cout<<H[i][j]<<' ';
94103
}
95104
std::cout<<std::endl;
96-
}
105+
}*/
97106
//call dysev
98107
//std::cout<<size<<std::endl;
99108
std::vector<double> omega(3*size);
@@ -226,20 +235,20 @@ void BFGS::UpdatePos(UnitCell& ucell)
226235

227236
//convert unit
228237
ModuleBase::Vector3<double> move_ion_cart;
229-
move_ion_cart.x = dpos[iat][0] / ModuleBase::BOHR_TO_A / GlobalC::ucell.lat0;
230-
move_ion_cart.y = dpos[iat][1] / ModuleBase::BOHR_TO_A / GlobalC::ucell.lat0;
231-
move_ion_cart.z = dpos[iat][2] / ModuleBase::BOHR_TO_A / GlobalC::ucell.lat0;
238+
move_ion_cart.x = dpos[iat][0] / ModuleBase::BOHR_TO_A / ucell.lat0;
239+
move_ion_cart.y = dpos[iat][1] / ModuleBase::BOHR_TO_A / ucell.lat0;
240+
move_ion_cart.z = dpos[iat][2] / ModuleBase::BOHR_TO_A / ucell.lat0;
232241

233242
//convert to Direct coordinate
234243
//note here the old GT is used
235244

236245
//convert pos
237-
ModuleBase::Vector3<double> move_ion_dr = move_ion_cart * GlobalC::ucell.GT;
246+
ModuleBase::Vector3<double> move_ion_dr = move_ion_cart * ucell.GT;
238247

239248

240-
int it = GlobalC::ucell.iat2it[iat];
241-
int ia = GlobalC::ucell.iat2ia[iat];
242-
Atom* atom = &GlobalC::ucell.atoms[it];
249+
int it = ucell.iat2it[iat];
250+
int ia = ucell.iat2ia[iat];
251+
Atom* atom = &ucell.atoms[it];
243252

244253
if(atom->mbl[ia].x == 1)
245254
{
@@ -254,11 +263,11 @@ void BFGS::UpdatePos(UnitCell& ucell)
254263
move_ion[iat * 3 + 2] = move_ion_dr.z ;
255264
}
256265
}
257-
GlobalC::ucell.update_pos_taud(move_ion);
266+
ucell.update_pos_taud(move_ion);
258267
pos = this->MAddM(pos, dpos);
259268
}
260269

261-
bool BFGS::IsRestrain(std::vector<std::vector<double>>& dpos)
270+
void BFGS::IsRestrain(std::vector<std::vector<double>>& dpos)
262271
{
263272
double a=0;
264273
for(int i=0;i<size;i++)
@@ -281,7 +290,7 @@ bool BFGS::IsRestrain(std::vector<std::vector<double>>& dpos)
281290
}
282291
}
283292
std::cout<<a<<std::endl;
284-
return a<0.00001;
293+
Ions_Move_Basic::converged = a<0.00001;
285294
}
286295
// matrix methods
287296

source/module_relax/relax_new/bfgs.h renamed to source/module_relax/relax_old/bfgs.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#include<algorithm>
77
#include<cmath>
88
#include"module_base/lapack_connector.h"
9-
#include "module_cell/unitcell.h"
9+
1010
#include "module_base/matrix.h"
1111
#include "module_base/matrix3.h"
12+
#include "module_cell/unitcell.h"
13+
1214

1315

1416
class BFGS
@@ -28,10 +30,10 @@ class BFGS
2830
std::vector<std::vector<double>> pos;
2931
std::vector<std::vector<double>> dpos;
3032

31-
void init_relax(const int _size,UnitCell& ucell,double _maxstep);//initialize parameters
32-
bool relax_step(ModuleBase::matrix _force,UnitCell& ucell);//
33+
void allocate(const int _size);//initialize parameters
34+
void relax_step(ModuleBase::matrix _force,UnitCell& ucell);//
3335
void 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);
34-
bool IsRestrain(std::vector<std::vector<double>>& dpos);
36+
void IsRestrain(std::vector<std::vector<double>>& dpos);
3537

3638
private:
3739
bool sign;

source/module_relax/relax_old/ions_move_methods.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ void Ions_Move_Methods::allocate(const int &natom)
3232
this->cg.allocate();
3333
this->bfgs.allocate(); // added by pengfei 13-8-8
3434
}
35+
else if(Ions_Move_Basic::relax_method == "bfgs_trad")
36+
{
37+
this->bfgs_trad.allocate(natom);
38+
39+
}
3540
else
3641
{
3742
ModuleBase::WARNING("Ions_Move_Methods::init", "the parameter Ions_Move_Basic::relax_method is not correct.");
@@ -70,6 +75,11 @@ void Ions_Move_Methods::cal_movement(const int &istep,
7075
{
7176
cg.start(ucell, f, etot); // added by pengfei 13-8-10
7277
}
78+
else if(Ions_Move_Basic::relax_method == "bfgs_trad")
79+
{
80+
bfgs_trad.relax_step(f,ucell);
81+
82+
}
7383
else
7484
{
7585
ModuleBase::WARNING("Ions_Move_Methods::init", "the parameter Ions_Move_Basic::relax_method is not correct.");

source/module_relax/relax_old/ions_move_methods.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "ions_move_bfgs.h"
66
#include "ions_move_cg.h"
77
#include "ions_move_sd.h"
8+
#include "bfgs.h"
89

910
class Ions_Move_Methods
1011
{
@@ -45,5 +46,6 @@ class Ions_Move_Methods
4546
Ions_Move_BFGS bfgs;
4647
Ions_Move_CG cg;
4748
Ions_Move_SD sd;
49+
BFGS bfgs_trad;
4850
};
4951
#endif

source/module_relax/relax_old/test/CMakeLists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ AddTest(
2929
SOURCES bfgs_basic_test.cpp ../bfgs_basic.cpp
3030
)
3131

32-
AddTest(
33-
TARGET ions_move_methods_test
34-
LIBS parameter ${math_libs} base device
35-
SOURCES ions_move_methods_test.cpp ../ions_move_methods.cpp ../ions_move_basic.cpp ../ions_move_bfgs.cpp ../ions_move_cg.cpp ../ions_move_sd.cpp ../bfgs_basic.cpp
36-
)
32+
3733

3834
AddTest(
3935
TARGET ions_move_basic_test

0 commit comments

Comments
 (0)