Skip to content

Commit e794931

Browse files
committed
<fix>
Now out_chg is the parameter deciding if to output charge and out_freq_elec and out_freq_ion decide the frequency.
1 parent 3f7cd57 commit e794931

File tree

9 files changed

+81
-45
lines changed

9 files changed

+81
-45
lines changed

source/input.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ void Input::Default(void)
252252
printe = 100; // must > 0
253253
init_chg = "atomic";
254254
chg_extrap = "atomic"; // xiaohui modify 2015-02-01
255+
out_freq_elec = 0;
256+
out_freq_ion = 0;
255257
out_chg = 0;
256258
out_dm = 0;
257259

@@ -932,6 +934,14 @@ bool Input::Read(const std::string &fn)
932934
{
933935
read_value(ifs, chg_extrap); // xiaohui modify 2015-02-01
934936
}
937+
else if (strcmp("out_freq_elec", word) == 0)
938+
{
939+
read_value(ifs, out_freq_elec);
940+
}
941+
else if (strcmp("out_freq_ion", word) == 0)
942+
{
943+
read_value(ifs, out_freq_ion);
944+
}
935945
else if (strcmp("out_chg", word) == 0)
936946
{
937947
read_value(ifs, out_chg);
@@ -1914,6 +1924,8 @@ void Input::Bcast()
19141924
Parallel_Common::bcast_int(printe);
19151925
Parallel_Common::bcast_string(init_chg);
19161926
Parallel_Common::bcast_string(chg_extrap); // xiaohui modify 2015-02-01
1927+
Parallel_Common::bcast_int(out_freq_elec);
1928+
Parallel_Common::bcast_int(out_freq_ion);
19171929
Parallel_Common::bcast_int(out_chg);
19181930
Parallel_Common::bcast_int(out_dm);
19191931

source/input.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ class Input
197197
int mem_saver; // 1: save psi when nscf calculation.
198198

199199
int printe; // mohan add 2011-03-16
200-
int out_chg; // output charge density.
200+
int out_freq_elec; // the frequency ( >= 0) of electronic iter to output charge density and wavefunction. 0: output only when converged
201+
int out_freq_ion; // the frequency ( >= 0 ) of ionic step to output charge density and wavefunction. 0: output only when ion steps are finished
202+
int out_chg; // output charge density. 0: no; 1: yes
201203
int out_dm; // output density matrix.
202204
int out_pot; // yes or no
203205
int out_wfc_pw; // 0: no; 1: txt; 2: dat

source/input_conv.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ void Input_Conv::Convert(void)
423423
//----------------------------------------------------------
424424
// wavefunction / charge / potential / (2/4)
425425
//----------------------------------------------------------
426+
GlobalV::OUT_FREQ_ELEC = INPUT.out_freq_elec;
427+
GlobalV::OUT_FREQ_ION = INPUT.out_freq_ion;
426428
GlobalC::pot.init_chg = INPUT.init_chg;
427429
GlobalC::pot.chg_extrap = INPUT.chg_extrap; // xiaohui modify 2015-02-01
428430
GlobalC::CHR.out_chg = INPUT.out_chg;

source/module_base/global_variable.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ double PRESS3 = 0.0;
4343
double PRESSURE = 0.0;
4444
std::string RELAX_METHOD = "bfgs";
4545
std::string OUT_LEVEL = "ie";
46+
int OUT_FREQ_ELEC = 0;
47+
int OUT_FREQ_ION = 0;
4648
int RELAX_NMAX = 20;
4749
int MD_NSTEP = 20;
4850
int SCF_NMAX = 50;

source/module_base/global_variable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ extern double PRESS3;
4444
extern double PRESSURE;
4545
extern std::string RELAX_METHOD;
4646
extern std::string OUT_LEVEL;
47+
extern int OUT_FREQ_ELEC;
48+
extern int OUT_FREQ_ION;
4749

4850
extern int RELAX_NMAX; // 8.3
4951
extern int SCF_NMAX; // 8.4

source/module_esolver/esolver_ks.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,31 @@ namespace ModuleESolver
1515

1616
ESolver_KS::ESolver_KS()
1717
{
18+
classname = "ESolver_KS";
1819
basisname = "PLEASE ADD BASISNAME FOR CURRENT ESOLVER.";
1920
diag_ethr = GlobalV::PW_DIAG_THR;
2021
scf_thr = GlobalV::SCF_THR;
2122
drho = 0.0;
2223
maxniter = GlobalV::SCF_NMAX;
2324
niter = maxniter;
24-
25-
25+
out_freq_elec = GlobalV::OUT_FREQ_ELEC;
2626
}
2727

2828
void ESolver_KS:: hamilt2density(int istep, int iter, double ethr)
2929
{
30+
ModuleBase::timer:: tick(this->classname,"hamilt2density");
3031
//Temporarily, before HSolver is constructed, it should be overrided by
3132
//LCAO, PW, SDFT and TDDFT.
3233
//After HSolver is constructed, LCAO, PW, SDFT should delete their own
3334
//hamilt2density() and use:
3435
//this->phsol->solve(this->phamilt, this->pes, this->wf, ETHR);
35-
36+
ModuleBase::timer:: tick(this->classname,"hamilt2density");
3637
}
3738

3839

3940
void ESolver_KS:: Run(int istep, UnitCell_pseudo& cell)
4041
{
41-
ModuleBase::timer:: tick("ESolver_KS","run");
42+
ModuleBase::timer:: tick(this->classname,"Run");
4243

4344
this->printhead(); //print the headline on the screen.
4445
this->beforeiter(); //Something else to do before the iter loop
@@ -67,14 +68,7 @@ void ESolver_KS:: Run(int istep, UnitCell_pseudo& cell)
6768
// The error of HSolver is larger than drho, so a more precise HSolver should be excuted.
6869
if(hsover_error > drho)
6970
{
70-
GlobalV::ofs_running << " Notice: Threshold on eigenvalues was too large.\n";
71-
ModuleBase::WARNING("scf","Threshold on eigenvalues was too large.");
72-
GlobalV::ofs_running << " hsover_error=" << hsover_error << " > drho=" << drho << std::endl;
73-
74-
GlobalV::ofs_running << " Origin diag_ethr = " << this->diag_ethr << std::endl;
75-
this->diag_ethr =0.1 * drho / GlobalC::CHR.nelec;
76-
GlobalV::ofs_running << " New diag_ethr = " << this->diag_ethr << std::endl;
77-
71+
reset_diagethr(GlobalV::ofs_running, hsover_error);
7872
this->hamilt2density(istep, iter, this->diag_ethr);
7973
drho = GlobalC::CHR.get_drho();
8074
}
@@ -92,7 +86,7 @@ void ESolver_KS:: Run(int istep, UnitCell_pseudo& cell)
9286
// Hamilt should be used after it is constructed.
9387
// this->phamilt->update(conv_elec);
9488
updatepot(conv_elec);
95-
eachiterfinish(iter);
89+
eachiterfinish(iter,conv_elec);
9690
iterend = std::clock();
9791
double duration = double(iterend-iterstart) / CLOCKS_PER_SEC;
9892
printiter(conv_elec, iter, drho, duration, diag_ethr);
@@ -104,11 +98,11 @@ void ESolver_KS:: Run(int istep, UnitCell_pseudo& cell)
10498
}
10599
afteriter(conv_elec);
106100

107-
ModuleBase::timer:: tick("ESolver_KS","run");
101+
ModuleBase::timer:: tick(this->classname,"Run");
108102
return;
109103
};
110104

111-
105+
//<Temporary> It should be a function of Diag_H class in the future.
112106
void ESolver_KS:: set_ethr(int istep, int iter)
113107
{
114108
//It is too complex now and should be modified.
@@ -191,6 +185,16 @@ void ESolver_KS:: writehead(std::ofstream &ofs_running, int istep, int iter)
191185
<< "--------------------------------\n";
192186
}
193187

188+
void ESolver_KS:: reset_diagethr(std::ofstream &ofs_running, double hsover_error)
189+
{
190+
ofs_running << " Notice: Threshold on eigenvalues was too large.\n";
191+
ModuleBase::WARNING("scf","Threshold on eigenvalues was too large.");
192+
ofs_running << " hsover_error=" << hsover_error << " > DRHO=" << drho << std::endl;
193+
ofs_running << " Origin diag_ethr = " << this->diag_ethr << std::endl;
194+
this->diag_ethr =0.1 * drho / GlobalC::CHR.nelec;
195+
ofs_running << " New diag_ethr = " << this->diag_ethr << std::endl;
196+
}
197+
194198
int ESolver_KS:: getniter()
195199
{
196200
return this->niter;

source/module_esolver/esolver_ks.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ESolver_KS: public ESolver_FP
1818
double drho; // the difference between rho_in (before HSolver) and rho_out (After HSolver)
1919
int maxniter; // maximum iter steps for scf
2020
int niter; // iter steps actually used in scf
21+
int out_freq_elec;// frequency for output
2122

2223
virtual void Run(int istep, UnitCell_pseudo& cell) override;
2324

@@ -32,7 +33,7 @@ class ESolver_KS: public ESolver_FP
3233
// Something to do before hamilt2density function in each iter loop.
3334
virtual void eachiterinit(int iter){};
3435
// Something to do after hamilt2density function in each iter loop.
35-
virtual void eachiterfinish(int iter){};
36+
virtual void eachiterfinish(int iter, bool conv){};
3637
// Something to do after the iter loop when scf is converged or comes to the max iter step.
3738
virtual void afteriter(bool){};
3839
// <Temporary> It should be replaced by a function in Hamilt Class
@@ -44,14 +45,15 @@ class ESolver_KS: public ESolver_FP
4445
// Set ethr for hsolver
4546
void set_ethr(int istep, int iter);
4647
// Print the headline on the screen:
47-
// ITER ETOT(eV) EDIFF(eV) SCF_THR TIME(s)
48+
// ITER ETOT(eV) EDIFF(eV) DRHO TIME(s)
4849
void printhead();
4950
// Print inforamtion in each iter
5051
// G1 -3.435545e+03 0.000000e+00 3.607e-01 2.862e-01
5152
void printiter(bool conv, int iter, double drho, double duration, double ethr);
5253
// Write the headline in the running_log file
5354
// "PW/LCAO" ALGORITHM --------------- ION= 1 ELEC= 1--------------------------------
5455
void writehead(std::ofstream &ofs_running,int istep, int iter);
56+
void reset_diagethr(std::ofstream &ofs_running, double hsover_error);
5557

5658

5759

source/module_esolver/esolver_ks_pw.cpp

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -305,45 +305,55 @@ void ESolver_KS_PW:: updatepot(bool conv_elec)
305305
GlobalC::pot.set_vr_eff();
306306
}
307307

308-
void ESolver_KS_PW:: eachiterfinish(int iter)
308+
void ESolver_KS_PW:: eachiterfinish(int iter, bool conv_elec)
309309
{
310310
//print_eigenvalue(GlobalV::ofs_running);
311311
GlobalC::en.calculate_etot();
312-
if(GlobalC::CHR.out_chg != 0 && iter % GlobalC::CHR.out_chg == 0)
312+
//We output it for restarting the scf.
313+
bool print = false;
314+
if(this->out_freq_elec == 0 )
313315
{
314-
for(int is=0; is<GlobalV::NSPIN; is++)
316+
if(conv_elec) print = true;
317+
}
318+
else
319+
{
320+
if(iter % this->out_freq_elec == 0 || conv_elec) print = true;
321+
}
322+
323+
if(print)
324+
{
325+
if(GlobalC::CHR.out_chg > 0)
326+
{
327+
for(int is=0; is<GlobalV::NSPIN; is++)
328+
{
329+
std::stringstream ssc;
330+
std::stringstream ss1;
331+
ssc << GlobalV::global_out_dir << "tmp" << "_SPIN" << is + 1 << "_CHG";
332+
GlobalC::CHR.write_rho(GlobalC::CHR.rho_save[is], is, iter, ssc.str(), 3);//mohan add 2007-10-17
333+
ss1 << GlobalV::global_out_dir << "tmp" << "_SPIN" << is + 1 << "_CHG.cube";
334+
GlobalC::CHR.write_rho_cube(GlobalC::CHR.rho_save[is], is, ssc.str(), 3);
335+
}
336+
}
337+
//output wavefunctions
338+
if(GlobalC::wf.out_wfc_pw == 1 || GlobalC::wf.out_wfc_pw == 2)
315339
{
316-
std::stringstream ssc;
317-
std::stringstream ss1;
318-
ssc << GlobalV::global_out_dir << "tmp" << "_SPIN" << is + 1 << "_CHG";
319-
GlobalC::CHR.write_rho(GlobalC::CHR.rho_save[is], is, iter, ssc.str(), 3);//mohan add 2007-10-17
320-
ss1 << GlobalV::global_out_dir << "tmp" << "_SPIN" << is + 1 << "_CHG.cube";
321-
GlobalC::CHR.write_rho_cube(GlobalC::CHR.rho_save[is], is, ssc.str(), 3);
340+
std::stringstream ssw;
341+
ssw << GlobalV::global_out_dir << "WAVEFUNC";
342+
//WF_io::write_wfc( ssw.str(), GlobalC::wf.evc );
343+
// mohan update 2011-02-21
344+
//qianrui update 2020-10-17
345+
WF_io::write_wfc2( ssw.str(), GlobalC::wf.evc, GlobalC::pw.gcar);
346+
//ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running,"write wave functions into file WAVEFUNC.dat");
322347
}
348+
323349
}
324350

351+
325352
}
326353

327354

328355
void ESolver_KS_PW::afteriter(bool conv_elec)
329356
{
330-
//output wavefunctions
331-
if(GlobalC::wf.out_wfc_pw == 1 || GlobalC::wf.out_wfc_pw == 2)
332-
{
333-
std::stringstream ssw;
334-
ssw << GlobalV::global_out_dir << "WAVEFUNC";
335-
//WF_io::write_wfc( ssw.str(), GlobalC::wf.evc );
336-
// mohan update 2011-02-21
337-
//qianrui update 2020-10-17
338-
WF_io::write_wfc2( ssw.str(), GlobalC::wf.evc, GlobalC::pw.gcar);
339-
//ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running,"write wave functions into file WAVEFUNC.dat");
340-
}
341-
342-
343-
//--------------------------------------
344-
// output charge density for converged,
345-
// 0 means don't need to consider iter,
346-
//--------------------------------------
347357
#ifdef __LCAO
348358
if(GlobalC::chi0_hilbert.epsilon) // pengfei 2016-11-23
349359
{

source/module_esolver/esolver_ks_pw.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ESolver_KS_PW: public ESolver_KS
2424
virtual void eachiterinit(int iter) override;
2525
virtual void hamilt2density(int istep, int iter, double ethr) override;
2626
virtual void updatepot(bool conv) override;
27-
virtual void eachiterfinish(int iter) override;
27+
virtual void eachiterfinish(int iter, bool conv) override;
2828
virtual void afteriter(bool) override;
2929

3030
// <Temporary> Get wavefunctions and eigen energies.

0 commit comments

Comments
 (0)