Skip to content

Commit 705dad7

Browse files
Refactor IO cube (#5362)
* remove the redundant info in read_cube * remove redundant code in write_cube * make Pgrid a serial-or-parallel grid class * Pgrid contains nxyz * [pre-commit.ci lite] apply automatic fixes * rename reduce * rewrite io_cube * rename previous read/write_cube as read/write_grid * remove CubeInfo and xyz-zxy permutation * minor interface changes * revert the call in rho_sym to fix SDFT failure * rename r/w_grid as r/w_vdata_palgrid * [pre-commit.ci lite] apply automatic fixes * add rank parameter to pgrid.bcast to fix SDFT --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 4e53045 commit 705dad7

File tree

21 files changed

+424
-898
lines changed

21 files changed

+424
-898
lines changed

source/module_elecstate/module_charge/charge_init.cpp

Lines changed: 22 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -53,61 +53,37 @@ void Charge::init_rho(elecstate::efermi& eferm_iout,
5353
{
5454
std::stringstream ssc;
5555
ssc << PARAM.globalv.global_readin_dir << "SPIN" << is + 1 << "_CHG.cube";
56-
double& ef_tmp = eferm_iout.get_ef(is);
57-
if (ModuleIO::read_cube(
58-
#ifdef __MPI
59-
& (GlobalC::Pgrid),
60-
#endif
56+
if (ModuleIO::read_vdata_palgrid(GlobalC::Pgrid,
6157
(PARAM.inp.esolver_type == "sdft" ? GlobalV::RANK_IN_STOGROUP : GlobalV::MY_RANK),
62-
is,
6358
GlobalV::ofs_running,
64-
PARAM.inp.nspin,
6559
ssc.str(),
6660
this->rho[is],
67-
this->rhopw->nx,
68-
this->rhopw->ny,
69-
this->rhopw->nz,
70-
ef_tmp,
71-
& (GlobalC::ucell),
72-
this->prenspin))
61+
GlobalC::ucell.nat))
7362
{
7463
GlobalV::ofs_running << " Read in the charge density: " << ssc.str() << std::endl;
7564
}
76-
else if (is > 0)
65+
else if (is > 0) // nspin=2 or 4
7766
{
78-
if (prenspin == 1)
67+
if (is == 1) // failed at the second spin
7968
{
80-
GlobalV::ofs_running << " Didn't read in the charge density but autoset it for spin " << is + 1
81-
<< std::endl;
82-
for (int ir = 0; ir < this->rhopw->nrxx; ir++)
83-
{
84-
this->rho[is][ir] = 0.0;
85-
}
69+
std::cout << "Incomplete charge density file!" << std::endl;
70+
read_error = true;
71+
break;
8672
}
87-
//
88-
else if (prenspin == 2)
89-
{ // read up and down , then rearrange them.
90-
if (is == 1)
91-
{
92-
std::cout << "Incomplete charge density file!" << std::endl;
93-
read_error = true;
94-
break;
95-
}
96-
else if (is == 2)
97-
{
98-
GlobalV::ofs_running << " Didn't read in the charge density but would rearrange it later. "
99-
<< std::endl;
100-
}
101-
else if (is == 3)
73+
else if (is == 2) // read 2 files when nspin=4
74+
{
75+
GlobalV::ofs_running << " Didn't read in the charge density but would rearrange it later. "
76+
<< std::endl;
77+
}
78+
else if (is == 3) // read 2 files when nspin=4
79+
{
80+
GlobalV::ofs_running << " rearrange charge density " << std::endl;
81+
for (int ir = 0; ir < this->rhopw->nrxx; ir++)
10282
{
103-
GlobalV::ofs_running << " rearrange charge density " << std::endl;
104-
for (int ir = 0; ir < this->rhopw->nrxx; ir++)
105-
{
106-
this->rho[3][ir] = this->rho[0][ir] - this->rho[1][ir];
107-
this->rho[0][ir] = this->rho[0][ir] + this->rho[1][ir];
108-
this->rho[1][ir] = 0.0;
109-
this->rho[2][ir] = 0.0;
110-
}
83+
this->rho[3][ir] = this->rho[0][ir] - this->rho[1][ir];
84+
this->rho[0][ir] = this->rho[0][ir] + this->rho[1][ir];
85+
this->rho[1][ir] = 0.0;
86+
this->rho[2][ir] = 0.0;
11187
}
11288
}
11389
}
@@ -127,22 +103,12 @@ void Charge::init_rho(elecstate::efermi& eferm_iout,
127103
GlobalV::ofs_running << " try to read kinetic energy density from file : " << ssc.str()
128104
<< std::endl;
129105
// mohan update 2012-02-10, sunliang update 2023-03-09
130-
if (ModuleIO::read_cube(
131-
#ifdef __MPI
132-
& (GlobalC::Pgrid),
133-
#endif
106+
if (ModuleIO::read_vdata_palgrid(GlobalC::Pgrid,
134107
(PARAM.inp.esolver_type == "sdft" ? GlobalV::RANK_IN_STOGROUP : GlobalV::MY_RANK),
135-
is,
136108
GlobalV::ofs_running,
137-
PARAM.inp.nspin,
138109
ssc.str(),
139110
this->kin_r[is],
140-
this->rhopw->nx,
141-
this->rhopw->ny,
142-
this->rhopw->nz,
143-
eferm_iout.ef,
144-
& (GlobalC::ucell),
145-
this->prenspin))
111+
GlobalC::ucell.nat))
146112
{
147113
GlobalV::ofs_running << " Read in the kinetic energy density: " << ssc.str() << std::endl;
148114
}

source/module_elecstate/module_charge/symmetry_rho.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void Symmetry_rho::psymm(double* rho_part,
9797
rhotot.resize(rho_basis->nxyz);
9898
ModuleBase::GlobalFunc::ZEROS(rhotot.data(), rho_basis->nxyz);
9999
}
100-
Pgrid.reduce_to_fullrho(rhotot.data(), rho_part);
100+
Pgrid.reduce(rhotot.data(), rho_part);
101101

102102
// (2)
103103
if (GlobalV::MY_RANK == 0)
@@ -127,24 +127,7 @@ void Symmetry_rho::psymm(double* rho_part,
127127
}
128128

129129
// (3)
130-
const int ncxy = rho_basis->nx * rho_basis->ny;
131-
std::vector<double> zpiece(ncxy);
132-
for (int iz = 0; iz < rho_basis->nz; iz++)
133-
{
134-
ModuleBase::GlobalFunc::ZEROS(zpiece.data(), ncxy);
135-
if (GlobalV::MY_RANK == 0)
136-
{
137-
for (int ix = 0; ix < rho_basis->nx; ix++)
138-
{
139-
for (int iy = 0; iy < rho_basis->ny; iy++)
140-
{
141-
const int ir = ix * rho_basis->ny + iy;
142-
zpiece[ir] = rhotot[ix * rho_basis->ny * rho_basis->nz + iy * rho_basis->nz + iz];
143-
}
144-
}
145-
}
146-
Pgrid.zpiece_to_all(zpiece.data(), iz, rho_part);
147-
}
130+
Pgrid.bcast(rhotot.data(), rho_part, GlobalV::MY_RANK);
148131
#endif
149132
return;
150133
}

source/module_esolver/esolver_fp.cpp

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -156,43 +156,25 @@ void ESolver_FP::after_scf(const int istep)
156156
this->pw_rhod->real2recip(this->pelec->charge->rho_save[is], this->pelec->charge->rhog_save[is]);
157157
}
158158
std::string fn =PARAM.globalv.global_out_dir + "/SPIN" + std::to_string(is + 1) + "_CHG.cube";
159-
ModuleIO::write_cube(
160-
#ifdef __MPI
161-
this->pw_big->bz,
162-
this->pw_big->nbz,
163-
this->pw_rhod->nplane,
164-
this->pw_rhod->startz_current,
165-
#endif
159+
ModuleIO::write_vdata_palgrid(GlobalC::Pgrid,
166160
data,
167161
is,
168162
PARAM.inp.nspin,
169163
istep,
170164
fn,
171-
this->pw_rhod->nx,
172-
this->pw_rhod->ny,
173-
this->pw_rhod->nz,
174165
this->pelec->eferm.get_efval(is),
175166
&(GlobalC::ucell),
176167
PARAM.inp.out_chg[1],
177168
1);
178169
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
179170
{
180171
fn =PARAM.globalv.global_out_dir + "/SPIN" + std::to_string(is + 1) + "_TAU.cube";
181-
ModuleIO::write_cube(
182-
#ifdef __MPI
183-
this->pw_big->bz,
184-
this->pw_big->nbz,
185-
this->pw_rhod->nplane,
186-
this->pw_rhod->startz_current,
187-
#endif
172+
ModuleIO::write_vdata_palgrid(GlobalC::Pgrid,
188173
this->pelec->charge->kin_r_save[is],
189174
is,
190175
PARAM.inp.nspin,
191176
istep,
192177
fn,
193-
this->pw_rhod->nx,
194-
this->pw_rhod->ny,
195-
this->pw_rhod->nz,
196178
this->pelec->eferm.get_efval(is),
197179
&(GlobalC::ucell));
198180
}
@@ -224,21 +206,12 @@ void ESolver_FP::after_scf(const int istep)
224206
{
225207
std::string fn =PARAM.globalv.global_out_dir + "/SPIN" + std::to_string(is + 1) + "_POT.cube";
226208

227-
ModuleIO::write_cube(
228-
#ifdef __MPI
229-
this->pw_big->bz,
230-
this->pw_big->nbz,
231-
this->pw_rhod->nplane,
232-
this->pw_rhod->startz_current,
233-
#endif
209+
ModuleIO::write_vdata_palgrid(GlobalC::Pgrid,
234210
this->pelec->pot->get_effective_v(is),
235211
is,
236212
PARAM.inp.nspin,
237213
istep,
238214
fn,
239-
this->pw_rhod->nx,
240-
this->pw_rhod->ny,
241-
this->pw_rhod->nz,
242215
0.0, // efermi
243216
&(GlobalC::ucell),
244217
3, // precision

source/module_esolver/esolver_ks_lcao.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,43 +1029,25 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(int& iter)
10291029
data = this->pelec->charge->rho_save[is];
10301030
}
10311031
std::string fn = PARAM.globalv.global_out_dir + "/tmp_SPIN" + std::to_string(is + 1) + "_CHG.cube";
1032-
ModuleIO::write_cube(
1033-
#ifdef __MPI
1034-
this->pw_big->bz,
1035-
this->pw_big->nbz,
1036-
this->pw_rhod->nplane,
1037-
this->pw_rhod->startz_current,
1038-
#endif
1032+
ModuleIO::write_vdata_palgrid(GlobalC::Pgrid,
10391033
data,
10401034
is,
10411035
PARAM.inp.nspin,
10421036
0,
10431037
fn,
1044-
this->pw_rhod->nx,
1045-
this->pw_rhod->ny,
1046-
this->pw_rhod->nz,
10471038
this->pelec->eferm.get_efval(is),
10481039
&(GlobalC::ucell),
10491040
3,
10501041
1);
10511042
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
10521043
{
10531044
fn = PARAM.globalv.global_out_dir + "/tmp_SPIN" + std::to_string(is + 1) + "_TAU.cube";
1054-
ModuleIO::write_cube(
1055-
#ifdef __MPI
1056-
this->pw_big->bz,
1057-
this->pw_big->nbz,
1058-
this->pw_rhod->nplane,
1059-
this->pw_rhod->startz_current,
1060-
#endif
1045+
ModuleIO::write_vdata_palgrid(GlobalC::Pgrid,
10611046
this->pelec->charge->kin_r_save[is],
10621047
is,
10631048
PARAM.inp.nspin,
10641049
0,
10651050
fn,
1066-
this->pw_rhod->nx,
1067-
this->pw_rhod->ny,
1068-
this->pw_rhod->nz,
10691051
this->pelec->eferm.get_efval(is),
10701052
&(GlobalC::ucell));
10711053
}

source/module_esolver/esolver_ks_pw.cpp

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -239,21 +239,12 @@ void ESolver_KS_PW<T, Device>::before_scf(const int istep)
239239
{
240240
std::stringstream ss;
241241
ss << PARAM.globalv.global_out_dir << "SPIN" << is + 1 << "_CHG_INI.cube";
242-
ModuleIO::write_cube(
243-
#ifdef __MPI
244-
this->pw_big->bz,
245-
this->pw_big->nbz,
246-
this->pw_rhod->nplane,
247-
this->pw_rhod->startz_current,
248-
#endif
242+
ModuleIO::write_vdata_palgrid(GlobalC::Pgrid,
249243
this->pelec->charge->rho[is],
250244
is,
251245
PARAM.inp.nspin,
252246
istep,
253247
ss.str(),
254-
this->pw_rhod->nx,
255-
this->pw_rhod->ny,
256-
this->pw_rhod->nz,
257248
this->pelec->eferm.ef,
258249
&(GlobalC::ucell));
259250
}
@@ -266,21 +257,12 @@ void ESolver_KS_PW<T, Device>::before_scf(const int istep)
266257
{
267258
std::stringstream ss;
268259
ss << PARAM.globalv.global_out_dir << "SPIN" << is + 1 << "_POT_INI.cube";
269-
ModuleIO::write_cube(
270-
#ifdef __MPI
271-
this->pw_big->bz,
272-
this->pw_big->nbz,
273-
this->pw_rhod->nplane,
274-
this->pw_rhod->startz_current,
275-
#endif
260+
ModuleIO::write_vdata_palgrid(GlobalC::Pgrid,
276261
this->pelec->pot->get_effective_v(is),
277262
is,
278263
PARAM.inp.nspin,
279264
istep,
280265
ss.str(),
281-
this->pw_rhod->nx,
282-
this->pw_rhod->ny,
283-
this->pw_rhod->nz,
284266
0.0, // efermi
285267
&(GlobalC::ucell),
286268
11, // precsion
@@ -480,43 +462,25 @@ void ESolver_KS_PW<T, Device>::iter_finish(int& iter)
480462
data = this->pelec->charge->rho_save[is];
481463
}
482464
std::string fn = PARAM.globalv.global_out_dir + "/tmp_SPIN" + std::to_string(is + 1) + "_CHG.cube";
483-
ModuleIO::write_cube(
484-
#ifdef __MPI
485-
this->pw_big->bz,
486-
this->pw_big->nbz,
487-
this->pw_rhod->nplane,
488-
this->pw_rhod->startz_current,
489-
#endif
465+
ModuleIO::write_vdata_palgrid(GlobalC::Pgrid,
490466
data,
491467
is,
492468
PARAM.inp.nspin,
493469
0,
494470
fn,
495-
this->pw_rhod->nx,
496-
this->pw_rhod->ny,
497-
this->pw_rhod->nz,
498471
this->pelec->eferm.get_efval(is),
499472
&(GlobalC::ucell),
500473
3,
501474
1);
502475
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
503476
{
504477
fn = PARAM.globalv.global_out_dir + "/tmp_SPIN" + std::to_string(is + 1) + "_TAU.cube";
505-
ModuleIO::write_cube(
506-
#ifdef __MPI
507-
this->pw_big->bz,
508-
this->pw_big->nbz,
509-
this->pw_rhod->nplane,
510-
this->pw_rhod->startz_current,
511-
#endif
478+
ModuleIO::write_vdata_palgrid(GlobalC::Pgrid,
512479
this->pelec->charge->kin_r_save[is],
513480
is,
514481
PARAM.inp.nspin,
515482
0,
516483
fn,
517-
this->pw_rhod->nx,
518-
this->pw_rhod->ny,
519-
this->pw_rhod->nz,
520484
this->pelec->eferm.get_efval(is),
521485
&(GlobalC::ucell));
522486
}

source/module_esolver/io_npz.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void ESolver_KS_LCAO<TK, TR>::read_mat_npz(std::string& zipname, hamilt::HContai
7373
const int it = GlobalC::ucell.iat2it[iat];
7474
const int ia = GlobalC::ucell.iat2ia[iat];
7575

76-
//get atomic number (copied from write_cube.cpp)
76+
//get atomic number (copied from write_vdata_palgrid.cpp)
7777
std::string element = "";
7878
element = GlobalC::ucell.atoms[it].label;
7979
std::string::iterator temp = element.begin();
@@ -368,7 +368,7 @@ void ESolver_KS_LCAO<TK, TR>::output_mat_npz(std::string& zipname, const hamilt:
368368
const int it = GlobalC::ucell.iat2it[iat];
369369
const int ia = GlobalC::ucell.iat2ia[iat];
370370

371-
//get atomic number (copied from write_cube.cpp)
371+
//get atomic number (copied from write_vdata_palgrid.cpp)
372372
std::string element = "";
373373
element = GlobalC::ucell.atoms[it].label;
374374
std::string::iterator temp = element.begin();

0 commit comments

Comments
 (0)