Skip to content

Commit ee516c5

Browse files
committed
Refactor ModuleIO::read/write_cube()
1 parent 7fa03ca commit ee516c5

File tree

5 files changed

+189
-102
lines changed

5 files changed

+189
-102
lines changed

source/module_hamilt_pw/hamilt_pwdft/parallel_grid.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void Parallel_Grid::z_distribution(void)
182182

183183

184184
#ifdef __MPI
185-
void Parallel_Grid::zpiece_to_all(double *zpiece, const int &iz, double *rho)
185+
void Parallel_Grid::zpiece_to_all(double *zpiece, const int &iz, double *rho) const
186186
{
187187
if(PARAM.inp.esolver_type == "sdft")
188188
{
@@ -256,7 +256,7 @@ void Parallel_Grid::zpiece_to_all(double *zpiece, const int &iz, double *rho)
256256
#endif
257257

258258
#ifdef __MPI
259-
void Parallel_Grid::zpiece_to_stogroup(double *zpiece, const int &iz, double *rho)
259+
void Parallel_Grid::zpiece_to_stogroup(double *zpiece, const int &iz, double *rho) const
260260
{
261261
assert(allocate);
262262
//TITLE("Parallel_Grid","zpiece_to_all");

source/module_hamilt_pw/hamilt_pwdft/parallel_grid.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class Parallel_Grid
2121
const int &nczp, const int &nrxx, const int &nbz, const int &bz); //LiuXh add 20180606
2222

2323
#ifdef __MPI
24-
void zpiece_to_all(double *zpiece, const int &iz, double *rho);
25-
void zpiece_to_stogroup(double *zpiece, const int &iz, double *rho); //qainrui add for sto-dft 2021-7-21
24+
void zpiece_to_all(double *zpiece, const int &iz, double *rho) const;
25+
void zpiece_to_stogroup(double *zpiece, const int &iz, double *rho) const; //qainrui add for sto-dft 2021-7-21
2626

2727
void reduce_to_fullrho(double *rhotot, double *rhoin);
2828
#endif

source/module_io/cube_io.h

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,79 @@
88

99
namespace ModuleIO
1010
{
11-
bool read_cube(
11+
extern bool read_cube(
1212
#ifdef __MPI
13-
Parallel_Grid* Pgrid,
13+
const Parallel_Grid*const Pgrid,
1414
#endif
15-
int my_rank,
16-
std::string esolver_type,
17-
int rank_in_stogroup,
18-
const int& is,
15+
const int my_rank,
16+
const std::string esolver_type,
17+
const int rank_in_stogroup,
18+
const int is,
1919
std::ofstream& ofs_running,
20-
const int& nspin,
20+
const int nspin,
2121
const std::string& fn,
22-
double* data,
23-
const int& nx,
24-
const int& ny,
25-
const int& nz,
22+
double*const data,
23+
const int nx,
24+
const int ny,
25+
const int nz,
2626
double& ef,
27-
const UnitCell* ucell,
27+
const UnitCell*const ucell,
2828
int& prenspin,
29-
const bool& warning_flag = true);
29+
const bool warning_flag = true);
3030

31-
void write_cube(
31+
extern void read_cube_core(
32+
std::ifstream &ifs,
3233
#ifdef __MPI
33-
const int& bz,
34-
const int& nbz,
35-
const int& nplane,
36-
const int& startz_current,
34+
const Parallel_Grid*const Pgrid,
3735
#endif
38-
const double* data,
39-
const int& is,
40-
const int& nspin,
41-
const int& iter,
36+
const int my_rank,
37+
const std::string esolver_type,
38+
const int rank_in_stogroup,
39+
#ifdef __MPI
40+
#else
41+
const int is,
42+
std::ofstream& ofs_running,
43+
#endif
44+
double*const data,
45+
const int nx,
46+
const int ny,
47+
const int nz,
48+
const int nx_read,
49+
const int ny_read,
50+
const int nz_read);
51+
52+
extern void write_cube(
53+
#ifdef __MPI
54+
const int bz,
55+
const int nbz,
56+
const int nplane,
57+
const int startz_current,
58+
#endif
59+
const double*const data,
60+
const int is,
61+
const int nspin,
62+
const int iter,
4263
const std::string& fn,
43-
const int& nx,
44-
const int& ny,
45-
const int& nz,
46-
const double& ef,
47-
const UnitCell* ucell,
48-
const int& precision = 11,
49-
const int& out_fermi = 1); // mohan add 2007-10-17
64+
const int nx,
65+
const int ny,
66+
const int nz,
67+
const double ef,
68+
const UnitCell*const ucell,
69+
const int precision = 11,
70+
const int out_fermi = 1); // mohan add 2007-10-17
71+
72+
extern void write_cube_core(
73+
std::ofstream &ofs_cube,
74+
#ifdef __MPI
75+
const int bz,
76+
const int nbz,
77+
const int nplane,
78+
const int startz_current,
79+
#endif
80+
const double*const data,
81+
const int nx,
82+
const int ny,
83+
const int nz);
5084

5185
/**
5286
* @brief The trilinear interpolation method
@@ -78,7 +112,7 @@ void write_cube(
78112
* @param nz the dimension of grids along z
79113
* @param data the interpolated results
80114
*/
81-
void trilinear_interpolate(std::ifstream& ifs,
115+
extern void trilinear_interpolate(std::ifstream& ifs,
82116
const int& nx_read,
83117
const int& ny_read,
84118
const int& nz_read,

source/module_io/read_cube.cpp

Lines changed: 69 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,44 @@
33

44
bool ModuleIO::read_cube(
55
#ifdef __MPI
6-
Parallel_Grid* Pgrid,
6+
const Parallel_Grid*const Pgrid,
77
#endif
8-
int my_rank,
9-
std::string esolver_type,
10-
int rank_in_stogroup,
11-
const int& is,
8+
const int my_rank,
9+
const std::string esolver_type,
10+
const int rank_in_stogroup,
11+
const int is,
1212
std::ofstream& ofs_running,
13-
const int& nspin,
13+
const int nspin,
1414
const std::string& fn,
15-
double* data,
16-
const int& nx,
17-
const int& ny,
18-
const int& nz,
15+
double*const data,
16+
const int nx,
17+
const int ny,
18+
const int nz,
1919
double& ef,
20-
const UnitCell* ucell,
20+
const UnitCell*const ucell,
2121
int& prenspin,
22-
const bool& warning_flag)
22+
const bool warning_flag)
2323
{
2424
ModuleBase::TITLE("ModuleIO","read_cube");
2525
std::ifstream ifs(fn.c_str());
26-
if (!ifs)
27-
{
28-
std::string tmp_warning_info = "!!! Couldn't find the charge file of ";
29-
tmp_warning_info += fn;
30-
ofs_running << tmp_warning_info << std::endl;
31-
return false;
32-
}
33-
else
34-
{
35-
ofs_running << " Find the file, try to read charge from file." << std::endl;
36-
}
26+
if (!ifs)
27+
{
28+
std::string tmp_warning_info = "!!! Couldn't find the charge file of ";
29+
tmp_warning_info += fn;
30+
ofs_running << tmp_warning_info << std::endl;
31+
return false;
32+
}
33+
else
34+
{
35+
ofs_running << " Find the file, try to read charge from file." << std::endl;
36+
}
3737

38-
bool quit=false;
38+
bool quit=false;
3939

40-
ifs.ignore(300, '\n'); // skip the header
40+
ifs.ignore(300, '\n'); // skip the header
4141

42-
if(nspin != 4)
43-
{
42+
if(nspin != 4)
43+
{
4444
int v_in;
4545
ifs >> v_in;
4646
if (v_in != nspin)
@@ -49,16 +49,16 @@ bool ModuleIO::read_cube(
4949
return false;
5050
}
5151
}
52-
else
53-
{
54-
ifs >> prenspin;
55-
}
56-
ifs.ignore(150, ')');
52+
else
53+
{
54+
ifs >> prenspin;
55+
}
56+
ifs.ignore(150, ')');
5757

58-
ifs >> ef;
59-
ofs_running << " read in fermi energy = " << ef << std::endl;
58+
ifs >> ef;
59+
ofs_running << " read in fermi energy = " << ef << std::endl;
6060

61-
ifs.ignore(150, '\n');
61+
ifs.ignore(150, '\n');
6262

6363
ModuleBase::CHECK_INT(ifs, ucell->nat);
6464
ifs.ignore(150, '\n');
@@ -93,8 +93,6 @@ bool ModuleIO::read_cube(
9393
ifs >> temp >> temp >> temp;
9494
}
9595

96-
const bool same = (nx == nx_read && ny == ny_read && nz == nz_read) ? true : false;
97-
9896
for (int it = 0; it < ucell->ntype; it++)
9997
{
10098
for (int ia = 0; ia < ucell->atoms[it].na; ia++)
@@ -114,6 +112,40 @@ bool ModuleIO::read_cube(
114112
}
115113
}
116114

115+
#ifdef __MPI
116+
ModuleIO::read_cube_core(ifs, Pgrid, my_rank, esolver_type, rank_in_stogroup, data, nx, ny, nz, nx_read, ny_read, nz_read);
117+
#else
118+
ModuleIO::read_cube_core(ifs, my_rank, esolver_type, rank_in_stogroup, is, ofs_running, data, nx, ny, nz, nx_read, ny_read, nz_read);
119+
#endif
120+
121+
if (my_rank == 0 || (esolver_type == "sdft" && rank_in_stogroup == 0))
122+
ifs.close();
123+
return true;
124+
}
125+
126+
void ModuleIO::read_cube_core(
127+
std::ifstream &ifs,
128+
#ifdef __MPI
129+
const Parallel_Grid*const Pgrid,
130+
#endif
131+
const int my_rank,
132+
const std::string esolver_type,
133+
const int rank_in_stogroup,
134+
#ifdef __MPI
135+
#else
136+
const int is,
137+
std::ofstream& ofs_running,
138+
#endif
139+
double*const data,
140+
const int nx,
141+
const int ny,
142+
const int nz,
143+
const int nx_read,
144+
const int ny_read,
145+
const int nz_read)
146+
{
147+
const bool same = (nx == nx_read && ny == ny_read && nz == nz_read) ? true : false;
148+
117149
#ifdef __MPI
118150
const int nxy = nx * ny;
119151
double* zpiece = nullptr;
@@ -190,10 +222,6 @@ bool ModuleIO::read_cube(
190222
ModuleIO::trilinear_interpolate(ifs, nx_read, ny_read, nz_read, nx, ny, nz, data);
191223
}
192224
#endif
193-
194-
if (my_rank == 0 || (esolver_type == "sdft" && rank_in_stogroup == 0))
195-
ifs.close();
196-
return true;
197225
}
198226

199227
void ModuleIO::trilinear_interpolate(std::ifstream& ifs,

0 commit comments

Comments
 (0)