Skip to content

Commit c212bdf

Browse files
authored
Feature: Support outputting real space wave functions in Gaussian CUBE format (#5140)
* Fix a minor timer bug in TDDFT * Add parameters out_wfc_norm and out_pchg, with other necessary changes * Delete GlobalV::MYRANK in get_pchg and modify output * Refactor write_wfc_r * Refactor get_wf and get_pchg files * Add re im code * Support output re and im of wfc * Modify write_wfc_r.cpp by clang-format * Modify and change some annotation * Optimize the expression form of standard output * Explain what is mode 1 and 2 in select_bands * Delete mode = 3 in get_pchg_lcao * Fix some naming convention * Fix naming of PW_Basis instance * Change some naming in write_wfc_r.cpp * Add annotation about using write_chg_r_1 in write_psi_r_1 * Add warnings at the front of file write_wfc_r.cpp
1 parent c234dd3 commit c212bdf

File tree

10 files changed

+738
-528
lines changed

10 files changed

+738
-528
lines changed

source/module_esolver/lcao_others.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,12 @@ void ESolver_KS_LCAO<TK, TR>::others(const int istep)
110110
this->pw_big->nbz,
111111
PARAM.globalv.gamma_only_local,
112112
PARAM.inp.nbands_istate,
113-
PARAM.inp.bands_to_print,
113+
PARAM.inp.out_pchg,
114114
PARAM.inp.nbands,
115115
PARAM.inp.nelec,
116116
PARAM.inp.nspin,
117117
PARAM.globalv.nlocal,
118118
PARAM.globalv.global_out_dir,
119-
GlobalV::MY_RANK,
120119
GlobalV::ofs_warning,
121120
&GlobalC::ucell,
122121
&GlobalC::GridD,
@@ -140,13 +139,12 @@ void ESolver_KS_LCAO<TK, TR>::others(const int istep)
140139
this->pw_big->nbz,
141140
PARAM.globalv.gamma_only_local,
142141
PARAM.inp.nbands_istate,
143-
PARAM.inp.bands_to_print,
142+
PARAM.inp.out_pchg,
144143
PARAM.inp.nbands,
145144
PARAM.inp.nelec,
146145
PARAM.inp.nspin,
147146
PARAM.globalv.nlocal,
148147
PARAM.globalv.global_out_dir,
149-
GlobalV::MY_RANK,
150148
GlobalV::ofs_warning,
151149
&GlobalC::ucell,
152150
&GlobalC::GridD,
@@ -174,7 +172,8 @@ void ESolver_KS_LCAO<TK, TR>::others(const int istep)
174172
this->kv,
175173
PARAM.inp.nelec,
176174
PARAM.inp.nbands_istate,
177-
PARAM.inp.bands_to_print,
175+
PARAM.inp.out_wfc_norm,
176+
PARAM.inp.out_wfc_re_im,
178177
PARAM.inp.nbands,
179178
PARAM.inp.nspin,
180179
PARAM.globalv.nlocal,
@@ -193,7 +192,8 @@ void ESolver_KS_LCAO<TK, TR>::others(const int istep)
193192
this->kv,
194193
PARAM.inp.nelec,
195194
PARAM.inp.nbands_istate,
196-
PARAM.inp.bands_to_print,
195+
PARAM.inp.out_wfc_norm,
196+
PARAM.inp.out_wfc_re_im,
197197
PARAM.inp.nbands,
198198
PARAM.inp.nspin,
199199
PARAM.globalv.nlocal,

source/module_io/get_pchg_lcao.cpp

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ IState_Charge::~IState_Charge()
2626
{
2727
}
2828

29-
// for gamma only
29+
// For gamma_only
3030
void IState_Charge::begin(Gint_Gamma& gg,
3131
double** rho,
3232
const ModuleBase::matrix& wg,
@@ -41,36 +41,35 @@ void IState_Charge::begin(Gint_Gamma& gg,
4141
const int bigpw_nbz,
4242
const bool gamma_only_local,
4343
const int nbands_istate,
44-
const std::vector<int>& out_band_kb,
44+
const std::vector<int>& out_pchg,
4545
const int nbands,
4646
const double nelec,
4747
const int nspin,
4848
const int nlocal,
4949
const std::string& global_out_dir,
50-
const int my_rank,
5150
std::ofstream& ofs_warning,
5251
const UnitCell* ucell_in,
5352
Grid_Driver* GridD_in,
5453
const K_Vectors& kv)
5554
{
5655
ModuleBase::TITLE("IState_Charge", "begin");
5756

58-
std::cout << " Perform |psi(i)|^2 for selected bands (band-decomposed charge densities, gamma only)." << std::endl;
57+
std::cout << " Calculate |psi(i)|^2 for selected bands (band-decomposed charge densities, gamma only)."
58+
<< std::endl;
5959

60+
// Determine the mode based on the input parameters
6061
int mode = 0;
61-
if (nbands_istate > 0 && static_cast<int>(out_band_kb.size()) == 0)
62+
// mode = 1: select bands below and above the Fermi surface using parameter `nbands_istate`
63+
if (nbands_istate > 0 && static_cast<int>(out_pchg.size()) == 0)
6264
{
6365
mode = 1;
6466
}
65-
else if (static_cast<int>(out_band_kb.size()) > 0)
67+
// mode = 2: select bands directly using parameter `out_pchg`
68+
else if (static_cast<int>(out_pchg.size()) > 0)
6669
{
67-
// If out_band_kb (bands_to_print) is not empty, set mode to 2
70+
// If out_pchg is not empty, set mode to 2
6871
mode = 2;
69-
std::cout << " Notice: INPUT parameter `nbands_istate` overwritten by `bands_to_print`!" << std::endl;
70-
}
71-
else
72-
{
73-
mode = 3;
72+
std::cout << " Notice: INPUT parameter `nbands_istate` overwritten by `out_pchg`!" << std::endl;
7473
}
7574

7675
// if ucell is odd, it's correct,
@@ -81,7 +80,7 @@ void IState_Charge::begin(Gint_Gamma& gg,
8180
std::cout << " number of occupied bands = " << fermi_band << std::endl;
8281

8382
// Set this->bands_picked_ according to the mode
84-
select_bands(nbands_istate, out_band_kb, nbands, nelec, mode, fermi_band);
83+
select_bands(nbands_istate, out_pchg, nbands, nelec, mode, fermi_band);
8584

8685
for (int ib = 0; ib < nbands; ++ib)
8786
{
@@ -174,13 +173,12 @@ void IState_Charge::begin(Gint_k& gk,
174173
const int bigpw_nbz,
175174
const bool gamma_only_local,
176175
const int nbands_istate,
177-
const std::vector<int>& out_band_kb,
176+
const std::vector<int>& out_pchg,
178177
const int nbands,
179178
const double nelec,
180179
const int nspin,
181180
const int nlocal,
182181
const std::string& global_out_dir,
183-
const int my_rank,
184182
std::ofstream& ofs_warning,
185183
UnitCell* ucell_in,
186184
Grid_Driver* GridD_in,
@@ -191,18 +189,18 @@ void IState_Charge::begin(Gint_k& gk,
191189
{
192190
ModuleBase::TITLE("IState_Charge", "begin");
193191

194-
std::cout << " Perform |psi(i)|^2 for selected bands (band-decomposed charge densities, multi-k)." << std::endl;
192+
std::cout << " Calculate |psi(i)|^2 for selected bands (band-decomposed charge densities, multi-k)." << std::endl;
195193

196194
int mode = 0;
197-
if (nbands_istate > 0 && static_cast<int>(out_band_kb.size()) == 0)
195+
if (nbands_istate > 0 && static_cast<int>(out_pchg.size()) == 0)
198196
{
199197
mode = 1;
200198
}
201-
else if (static_cast<int>(out_band_kb.size()) > 0)
199+
else if (static_cast<int>(out_pchg.size()) > 0)
202200
{
203-
// If out_band_kb (bands_to_print) is not empty, set mode to 2
201+
// If out_pchg is not empty, set mode to 2
204202
mode = 2;
205-
std::cout << " Notice: INPUT parameter `nbands_istate` overwritten by `bands_to_print`!" << std::endl;
203+
std::cout << " Notice: INPUT parameter `nbands_istate` overwritten by `out_pchg`!" << std::endl;
206204
}
207205
else
208206
{
@@ -214,7 +212,7 @@ void IState_Charge::begin(Gint_k& gk,
214212
std::cout << " number of occupied bands = " << fermi_band << std::endl;
215213

216214
// Set this->bands_picked_ according to the mode
217-
select_bands(nbands_istate, out_band_kb, nbands, nelec, mode, fermi_band);
215+
select_bands(nbands_istate, out_pchg, nbands, nelec, mode, fermi_band);
218216

219217
for (int ib = 0; ib < nbands; ++ib)
220218
{
@@ -322,13 +320,7 @@ void IState_Charge::begin(Gint_k& gk,
322320
{
323321
rho_save_pointers[i] = rho_save[i].data();
324322
}
325-
srho.begin(is,
326-
rho_save_pointers.data(),
327-
rhog,
328-
ngmc,
329-
nullptr,
330-
rho_pw,
331-
ucell_in->symm);
323+
srho.begin(is, rho_save_pointers.data(), rhog, ngmc, nullptr, rho_pw, ucell_in->symm);
332324
}
333325

334326
std::cout << " Writing cube files...";
@@ -368,7 +360,7 @@ void IState_Charge::begin(Gint_k& gk,
368360
}
369361

370362
void IState_Charge::select_bands(const int nbands_istate,
371-
const std::vector<int>& out_band_kb,
363+
const std::vector<int>& out_pchg,
372364
const int nbands,
373365
const double nelec,
374366
const int mode,
@@ -382,6 +374,7 @@ void IState_Charge::select_bands(const int nbands_istate,
382374
this->bands_picked_.resize(nbands);
383375
ModuleBase::GlobalFunc::ZEROS(bands_picked_.data(), nbands);
384376

377+
// mode = 1: select bands below and above the Fermi surface using parameter `nbands_istate`
385378
if (mode == 1)
386379
{
387380
bands_below = nbands_istate;
@@ -404,33 +397,28 @@ void IState_Charge::select_bands(const int nbands_istate,
404397
}
405398
}
406399
}
400+
// mode = 2: select bands directly using parameter `out_pchg`
407401
else if (mode == 2)
408402
{
409-
// Check if length of out_band_kb is valid
410-
if (static_cast<int>(out_band_kb.size()) > nbands)
403+
// Check if length of out_pchg is valid
404+
if (static_cast<int>(out_pchg.size()) > nbands)
411405
{
412-
ModuleBase::WARNING_QUIT(
413-
"IState_Charge::select_bands",
414-
"The number of bands specified by `bands_to_print` in the INPUT file exceeds `nbands`!");
406+
ModuleBase::WARNING_QUIT("IState_Charge::select_bands",
407+
"The number of bands specified by `out_pchg` in the INPUT file exceeds `nbands`!");
415408
}
416-
// Check if all elements in out_band_kb are 0 or 1
417-
for (int value: out_band_kb)
409+
// Check if all elements in out_pchg are 0 or 1
410+
for (int value: out_pchg)
418411
{
419412
if (value != 0 && value != 1)
420413
{
421-
ModuleBase::WARNING_QUIT(
422-
"IState_Charge::select_bands",
423-
"The elements of `bands_to_print` must be either 0 or 1. Invalid values found!");
414+
ModuleBase::WARNING_QUIT("IState_Charge::select_bands",
415+
"The elements of `out_pchg` must be either 0 or 1. Invalid values found!");
424416
}
425417
}
426-
// Fill bands_picked_ with values from out_band_kb
418+
// Fill bands_picked_ with values from out_pchg
427419
// Remaining bands are already set to 0
428-
const int length = std::min(static_cast<int>(out_band_kb.size()), nbands);
429-
for (int i = 0; i < length; ++i)
430-
{
431-
// out_band_kb rely on function parse_expression
432-
bands_picked_[i] = out_band_kb[i];
433-
}
420+
const int length = std::min(static_cast<int>(out_pchg.size()), nbands);
421+
std::copy(out_pchg.begin(), out_pchg.begin() + length, bands_picked_.begin());
434422

435423
// Check if there are selected bands below the Fermi surface
436424
bool has_below = false;
@@ -485,7 +473,7 @@ void IState_Charge::select_bands(const int nbands_istate,
485473
}
486474

487475
#ifdef __MPI
488-
// for gamma only
476+
// For gamma_only
489477
void IState_Charge::idmatrix(const int& ib,
490478
const int nspin,
491479
const double& nelec,

source/module_io/get_pchg_lcao.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class IState_Charge
2828

2929
~IState_Charge();
3030

31-
// for gamma only
31+
// For gamma_only
3232
void begin(Gint_Gamma& gg,
3333
double** rho,
3434
const ModuleBase::matrix& wg,
@@ -43,13 +43,12 @@ class IState_Charge
4343
const int bigpw_nbz,
4444
const bool gamma_only_local,
4545
const int nbands_istate,
46-
const std::vector<int>& out_band_kb,
46+
const std::vector<int>& out_pchg,
4747
const int nbands,
4848
const double nelec,
4949
const int nspin,
5050
const int nlocal,
5151
const std::string& global_out_dir,
52-
const int my_rank,
5352
std::ofstream& ofs_warning,
5453
const UnitCell* ucell_in,
5554
Grid_Driver* GridD_in,
@@ -72,13 +71,12 @@ class IState_Charge
7271
const int bigpw_nbz,
7372
const bool gamma_only_local,
7473
const int nbands_istate,
75-
const std::vector<int>& out_band_kb,
74+
const std::vector<int>& out_pchg,
7675
const int nbands,
7776
const double nelec,
7877
const int nspin,
7978
const int nlocal,
8079
const std::string& global_out_dir,
81-
const int my_rank,
8280
std::ofstream& ofs_warning,
8381
UnitCell* ucell_in,
8482
Grid_Driver* GridD_in,
@@ -92,14 +90,14 @@ class IState_Charge
9290
* @brief Set this->bands_picked_ according to the mode, and process an error if the mode is not recognized.
9391
*
9492
* @param nbands_istate INPUT parameter nbands_istate.
95-
* @param out_band_kb Calculated from INPUT parameter bands_to_print, vector.
93+
* @param out_pchg INPUT parameter out_pchg, vector.
9694
* @param nbands INPUT parameter nbands.
9795
* @param nelec Total number of electrons.
9896
* @param mode Selected mode.
9997
* @param fermi_band Calculated Fermi band.
10098
*/
10199
void select_bands(const int nbands_istate,
102-
const std::vector<int>& out_band_kb,
100+
const std::vector<int>& out_pchg,
103101
const int nbands,
104102
const double nelec,
105103
const int mode,

0 commit comments

Comments
 (0)