Skip to content

Commit b09725d

Browse files
committed
Merge branch 'pw' into pw-merge
2 parents 58a66d8 + 032dc13 commit b09725d

30 files changed

+369
-502
lines changed

docs/advanced/input_files/input-main.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,16 +1664,19 @@ These variables are used to control the output of properties.
16641664
### out_wfc_pw
16651665

16661666
- **Type**: Integer
1667-
- **Availability**: Plane wave basis or get_wf calculation in numerical atomic orbital basis
1668-
- **Description**:
1669-
- 1: Output the coefficients of wave functions into text files named `OUT.${suffix}/WAVEFUNC${K}.txt`, where ${K} is the index of k points.
1670-
- 2: results are stored in binary files named `OUT.${suffix}/WAVEFUNC${K}.dat`.
1667+
- **Availability**: Output electronic wave functions in plane wave basis, or transform the real-space electronic wave function into plane wave basis (see get_wf option in [calculation](#calculation) with NAO basis)
1668+
- **Description**: Whether to output the electronic wavefunction coefficients into files and store them in the folder `OUT.${suffix}`. The files are named as `wf{s}{spin index}{k}{k-point index}{_pw} + {".txt"/".dat"}`. Here, 's' refers to spin, where s1 means spin up channel while s2 means spin down channel, and 's12' refer to spinor wave functions that contains both spin channels with spin-orbital coupling or noncollinear calculations enabled.
1669+
- 0: no output
1670+
- 1: (txt format)
1671+
- non-gamma-only: `wfs1k1_pw.txt` or `wfs1k2_pw.txt`, ...;
1672+
- 2: (binary format)
1673+
- non-gamma-only: `wfs1k1_pw.dat` or `wfs1k2_Pw.dat`, ....
16711674
- **Default**: 0
16721675

16731676
### out_wfc_r
16741677

16751678
- **Type**: Boolean
1676-
- **Availability**: Plane wave basis or get_wf calculation in numerical atomic orbital basis
1679+
- **Availability**: Output electronic wave functions in plane wave basis, or transform the real-space electronic wave function into plane wave basis (see get_wf option in [calculation](#calculation) with NAO basis)
16771680
- **Description**: Whether to output real-space wave functions into `OUT.suffix/wfc_realspace/wfc_realspace_${K}_${B}`, where `${K}` is the index of k points, `${B}` is the index of bands.
16781681
- **Default**: False
16791682

source/module_cell/unitcell.cpp

100755100644
File mode changed.

source/module_elecstate/module_charge/charge_init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ void Charge::init_rho(elecstate::efermi& eferm_iout,
247247
const K_Vectors* kv = reinterpret_cast<const K_Vectors*>(klist);
248248
const int nkstot = kv->get_nkstot();
249249
const std::vector<int>& isk = kv->isk;
250-
ModuleIO::read_wf2rho_pw(pw_wfc, symm, kv->ik2iktot.data(), nkstot, isk, *this);
250+
ModuleIO::read_wf2rho_pw(pw_wfc, symm, kv->ik2iktot, nkstot, isk, *this);
251251
}
252252
}
253253

source/module_esolver/esolver_ks_pw.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,10 @@ void ESolver_KS_PW<T, Device>::iter_finish(UnitCell& ucell, const int istep, int
650650
{
651651
if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || conv_esolver)
652652
{
653-
std::stringstream ssw;
654-
ssw << PARAM.globalv.global_out_dir << "WAVEFUNC";
655-
ModuleIO::write_wfc_pw(ssw.str(), this->psi[0], this->kv, this->pw_wfc);
653+
ModuleIO::write_wfc_pw(
654+
PARAM.inp.out_wfc_pw,
655+
PARAM.globalv.global_out_dir,
656+
this->psi[0], this->kv, this->pw_wfc);
656657
}
657658
}
658659

@@ -712,10 +713,10 @@ void ESolver_KS_PW<T, Device>::after_scf(UnitCell& ucell, const int istep, const
712713
//------------------------------------------------------------------
713714
if (PARAM.inp.out_wfc_pw == 1 || PARAM.inp.out_wfc_pw == 2)
714715
{
715-
std::stringstream ssw;
716-
ssw << PARAM.globalv.global_out_dir << "WAVEFUNC";
717-
ModuleIO::write_wfc_pw(ssw.str(), this->psi[0], this->kv, this->pw_wfc);
718-
}
716+
ModuleIO::write_wfc_pw(PARAM.inp.out_wfc_pw,
717+
PARAM.globalv.global_out_dir,
718+
this->psi[0], this->kv, this->pw_wfc);
719+
}
719720

720721
//------------------------------------------------------------------
721722
// 5) calculate band-decomposed (partial) charge density in pw basis

source/module_io/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ list(APPEND objects
3333
para_json.cpp
3434
parse_args.cpp
3535
orb_io.cpp
36+
filename.cpp
3637
)
3738

3839
list(APPEND objects_advanced

source/module_io/filename.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include <set>
2+
#include "filename.h"
3+
#include "module_base/tool_quit.h"
4+
5+
namespace ModuleIO
6+
{
7+
8+
std::string filename_output(
9+
const std::string &directory,
10+
const std::string &property,
11+
const std::string &basis,
12+
const int ik,
13+
const std::vector<int> &ik2iktot,
14+
const int nspin,
15+
const int nkstot,
16+
const int out_type,
17+
const bool out_app_flag,
18+
const bool gamma_only,
19+
const int istep)
20+
{
21+
// output filename = "{PARAM.globalv.global_out_dir}/property{s}{spin index}
22+
// {k(optinal)}{k-point index}{g(optional)}{geometry index1}{_basis(nao|pw)}
23+
// + {".txt"/".dat"}"
24+
25+
std::set<std::string> valid_properties = {"wf", "chg", "h", "s"};
26+
if (valid_properties.find(property) == valid_properties.end())
27+
{
28+
ModuleBase::WARNING_QUIT("ModuleIO::filename_output", "unknown property");
29+
}
30+
31+
std::set<std::string> valid_basis = {"pw", "nao"};
32+
if (valid_basis.find(basis) == valid_basis.end())
33+
{
34+
ModuleBase::WARNING_QUIT("ModuleIO::filename_output", "unknown basis");
35+
}
36+
37+
assert(ik>=0);
38+
assert(ik2iktot.size() == nkstot);
39+
assert(nspin>0);
40+
41+
// spin index
42+
int is0 = -1;
43+
// ik0 is the k-point index, starting from 1
44+
int ik0 = ik2iktot[ik];
45+
46+
if(nspin == 1)
47+
{
48+
is0 = 1;
49+
}
50+
else if(nspin == 2)
51+
{
52+
const int half_k = nkstot/2;
53+
if(ik0 >= half_k)
54+
{
55+
is0 = 2;
56+
ik0 -= half_k;
57+
}
58+
else
59+
{
60+
is0 = 1;
61+
}
62+
}
63+
else if(nspin==4)
64+
{
65+
is0 = 12;
66+
}
67+
68+
// spin part
69+
std::string spin_block;
70+
spin_block = "s" + std::to_string(is0);
71+
72+
// k-point part
73+
std::string kpoint_block;
74+
if(gamma_only)
75+
{
76+
// do nothing;
77+
}
78+
else
79+
{
80+
kpoint_block = "k" + std::to_string(ik0+1);
81+
}
82+
83+
std::string istep_block
84+
= (istep >= 0 && (!out_app_flag))
85+
? "g" + std::to_string(istep + 1)
86+
: ""; // only when istep >= 0 and out_app_flag is false will write each wfc to a separate file
87+
88+
std::string suffix_block;
89+
if (out_type == 1)
90+
{
91+
suffix_block = ".txt";
92+
}
93+
else if (out_type == 2)
94+
{
95+
suffix_block = ".dat";
96+
}
97+
else
98+
{
99+
std::cout << "WARNING: the type of output wave function is not 1 or 2, so 1 is chosen." << std::endl;
100+
suffix_block = ".txt";
101+
}
102+
103+
std::string fn_out
104+
= directory + property + spin_block + kpoint_block
105+
+ istep_block + "_" + basis + suffix_block;
106+
107+
return fn_out;
108+
}
109+
110+
}

source/module_io/filename.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef FILENAME_H
2+
#define FILENAME_H
3+
#include <vector>
4+
#include <string>
5+
6+
namespace ModuleIO
7+
{
8+
9+
/**
10+
* Generates the filename for the output files
11+
* @param directory: directory of the file
12+
* @param property: wave function (wf), charge density (chg) or matrix (mat)
13+
* @param basis: nao or pw
14+
* @param ik: index of the k-point, and starting from 0.
15+
* @param ik2iktot: map from ik to iktot
16+
* @param nspin: number of spin channels, 1,2 or 4
17+
* @param nkstot: number of total k-points
18+
* @param out_type: two types of output file format, 1 for .txt and 2 for .dat (binary)
19+
* @param out_app_flag: whether to append to existing file.
20+
* @param gamma_only: gamma_only algorithm or not.
21+
* @param istep: index of the ion step starting from 0. If < 0, the step number is not included in the file name.
22+
* @return The generated filename.
23+
*/
24+
std::string filename_output(
25+
const std::string &directory,
26+
const std::string &property,
27+
const std::string &basis,
28+
const int ik,
29+
const std::vector<int> &ik2iktot,
30+
const int nspin,
31+
const int nkstot,
32+
const int out_type,
33+
const bool out_app_flag,
34+
const bool gamma_only,
35+
const int istep=-1);
36+
37+
}
38+
#endif

source/module_io/get_wf_lcao.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,12 @@ void Get_wf_lcao::begin(const UnitCell& ucell,
243243

244244
if (out_wfc_pw)
245245
{
246-
std::stringstream ssw;
247-
ssw << global_out_dir << "WAVEFUNC";
248-
std::cout << " Write G-space wave functions into \"" << global_out_dir << "/" << ssw.str() << "\" files."
249-
<< std::endl;
250-
ModuleIO::write_wfc_pw(ssw.str(), psi_g, kv, pw_wfc);
246+
ModuleIO::write_wfc_pw(
247+
out_wfc_pw,
248+
PARAM.globalv.global_out_dir,
249+
psi_g,
250+
kv,
251+
pw_wfc);
251252
}
252253

253254
if (out_wfc_r)
@@ -275,7 +276,7 @@ void Get_wf_lcao::begin(const UnitCell& ucell,
275276
const Parallel_Grid& pgrid,
276277
const Parallel_Orbitals& para_orb,
277278
Gint_k& gk,
278-
const int& out_wf,
279+
const int& out_wf_pw,
279280
const int& out_wf_r,
280281
const K_Vectors& kv,
281282
const double nelec,
@@ -311,7 +312,7 @@ void Get_wf_lcao::begin(const UnitCell& ucell,
311312

312313
// for pw_wfc in G space
313314
psi::Psi<std::complex<double>> psi_g;
314-
if (out_wf || out_wf_r)
315+
if (out_wf_pw || out_wf_r)
315316
{
316317
psi_g.resize(nks, nbands, pw_wfc->npwk_max);
317318
}
@@ -401,7 +402,7 @@ void Get_wf_lcao::begin(const UnitCell& ucell,
401402
3,
402403
1);
403404

404-
if (out_wf || out_wf_r)
405+
if (out_wf_pw || out_wf_r)
405406
{
406407
psi_g.fix_k(ik);
407408
this->set_pw_wfc(pw_wfc, ik, ib, nspin, pes_->charge->rho, psi_g);
@@ -410,15 +411,11 @@ void Get_wf_lcao::begin(const UnitCell& ucell,
410411
}
411412
}
412413

413-
if (out_wf || out_wf_r)
414+
if (out_wf_pw || out_wf_r)
414415
{
415-
if (out_wf)
416+
if (out_wf_pw)
416417
{
417-
std::stringstream ssw;
418-
ssw << global_out_dir << "WAVEFUNC";
419-
std::cout << " write G-space wave functions into \"" << global_out_dir << "/" << ssw.str() << "\" files."
420-
<< std::endl;
421-
ModuleIO::write_wfc_pw(ssw.str(), psi_g, kv, pw_wfc);
418+
ModuleIO::write_wfc_pw(out_wf_pw, global_out_dir, psi_g, kv, pw_wfc);
422419
}
423420
if (out_wf_r)
424421
{

source/module_io/read_wf2rho_pw.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
#include "module_elecstate/module_charge/symmetry_rho.h"
77
#include "module_parameter/parameter.h"
88
#include "module_elecstate/kernels/elecstate_op.h"
9+
#include "module_io/filename.h"
910

1011
void ModuleIO::read_wf2rho_pw(const ModulePW::PW_Basis_K* pw_wfc,
1112
ModuleSymmetry::Symmetry& symm,
12-
const int* ik2iktot,
13+
const std::vector<int> &ik2iktot,
1314
const int nkstot,
14-
const std::vector<int>& isk,
15+
const std::vector<int> &isk,
1516
Charge& chg)
1617
{
1718
ModuleBase::TITLE("ModuleIO", "read_wf2rho_pw");
@@ -78,11 +79,20 @@ void ModuleIO::read_wf2rho_pw(const ModulePW::PW_Basis_K* pw_wfc,
7879
{
7980
is = isk[ik];
8081
}
81-
std::stringstream filename;
8282
const int ikstot = ik2iktot[ik];
83-
filename << PARAM.globalv.global_readin_dir << "WAVEFUNC" << ikstot + 1 << ".dat";
84-
ModuleIO::read_wfc_pw(filename.str(), pw_wfc, ik, ikstot, nkstot, wfc_tmp);
85-
if (PARAM.inp.nspin == 4)
83+
84+
// mohan add 2025-05-17
85+
// .dat file
86+
const int out_type = 2;
87+
const bool out_app_flag = false;
88+
const bool gamma_only = false;
89+
const int istep = -1;
90+
91+
std::string fn = filename_output(PARAM.globalv.global_readin_dir,"wf","pw",ik,ik2iktot,nspin,nkstot,
92+
out_type,out_app_flag,gamma_only,istep);
93+
94+
ModuleIO::read_wfc_pw(fn, pw_wfc, ik, ikstot, nkstot, wfc_tmp);
95+
if (nspin == 4)
8696
{
8797
std::vector<std::complex<double>> rho_tmp2(nrxx);
8898
for (int ib = 0; ib < nbands; ++ib)

source/module_io/read_wf2rho_pw.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "module_basis/module_pw/pw_basis_k.h"
55
#include "module_elecstate/module_charge/charge.h"
66

7+
#include <vector>
78
#include <string>
89

910
namespace ModuleIO
@@ -19,9 +20,9 @@ namespace ModuleIO
1920
*/
2021
void read_wf2rho_pw(const ModulePW::PW_Basis_K* pw_wfc,
2122
ModuleSymmetry::Symmetry& symm,
22-
const int* ik2iktot,
23+
const std::vector<int> &ik2iktot,
2324
const int nkstot,
24-
const std::vector<int>& isk,
25+
const std::vector<int> &isk,
2526
Charge& chg);
2627

2728
} // namespace ModuleIO

0 commit comments

Comments
 (0)