@@ -112,29 +112,61 @@ bool ModuleIO::read_cube(
112112 }
113113 }
114114
115+ const bool flag_read_rank = (my_rank == 0 || (esolver_type == " sdft" && rank_in_stogroup == 0 ));
115116#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+ if (nx == nx_read && ny == ny_read && nz == nz_read)
118+ ModuleIO::read_cube_core_match (ifs, Pgrid, flag_read_rank, data, nx*ny, nz);
119+ else
120+ ModuleIO::read_cube_core_mismatch (ifs, Pgrid, flag_read_rank, data, nx, ny, nz, nx_read, ny_read, nz_read);
117121#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);
122+ ofs_running << " Read SPIN = " << is + 1 << " charge now." << std::endl;
123+ if (nx == nx_read && ny == ny_read && nz == nz_read)
124+ ModuleIO::read_cube_core_match (ifs, data, nx*ny, nz);
125+ else
126+ ModuleIO::read_cube_core_mismatch (ifs, data, nx, ny, nz, nx_read, ny_read, nz_read);
119127#endif
120128
121- if (my_rank == 0 || (esolver_type == " sdft" && rank_in_stogroup == 0 ))
122- ifs.close ();
123129 return true ;
124130}
125131
126- void ModuleIO::read_cube_core (
132+ void ModuleIO::read_cube_core_match (
127133 std::ifstream &ifs,
128134#ifdef __MPI
129135 const Parallel_Grid*const Pgrid,
136+ const bool flag_read_rank,
130137#endif
131- const int my_rank,
132- const std::string esolver_type,
133- const int rank_in_stogroup,
138+ double *const data,
139+ const int nxy,
140+ const int nz)
141+ {
134142#ifdef __MPI
143+ if (flag_read_rank)
144+ {
145+ std::vector<std::vector<double >> read_rho (nz, std::vector<double >(nxy));
146+ for (int ixy = 0 ; ixy < nxy; ixy++)
147+ for (int iz = 0 ; iz < nz; iz++)
148+ ifs >> read_rho[iz][ixy];
149+ for (int iz = 0 ; iz < nz; iz++)
150+ Pgrid->zpiece_to_all (read_rho[iz].data (), iz, data);
151+ }
152+ else
153+ {
154+ std::vector<double > zpiece (nxy);
155+ for (int iz = 0 ; iz < nz; iz++)
156+ Pgrid->zpiece_to_all (zpiece.data (), iz, data);
157+ }
135158#else
136- const int is,
137- std::ofstream& ofs_running,
159+ for (int ixy = 0 ; ixy < nxy; ixy++)
160+ for (int iz = 0 ; iz < nz; iz++)
161+ ifs >> data[iz * nxy + ixy];
162+ #endif
163+ }
164+
165+ void ModuleIO::read_cube_core_mismatch (
166+ std::ifstream &ifs,
167+ #ifdef __MPI
168+ const Parallel_Grid*const Pgrid,
169+ const bool flag_read_rank,
138170#endif
139171 double *const data,
140172 const int nx,
@@ -144,83 +176,23 @@ void ModuleIO::read_cube_core(
144176 const int ny_read,
145177 const int nz_read)
146178{
147- const bool same = (nx == nx_read && ny == ny_read && nz == nz_read) ? true : false ;
148-
149179#ifdef __MPI
150180 const int nxy = nx * ny;
151- double * zpiece = nullptr ;
152- double ** read_rho = nullptr ;
153- if (my_rank == 0 || (esolver_type == " sdft" && rank_in_stogroup == 0 ))
181+ if (flag_read_rank)
154182 {
155- read_rho = new double *[nz];
183+ std::vector<std::vector<double >> read_rho (nz, std::vector<double >(nxy));
184+ ModuleIO::trilinear_interpolate (ifs, nx_read, ny_read, nz_read, nx, ny, nz, read_rho);
156185 for (int iz = 0 ; iz < nz; iz++)
157- {
158- read_rho[iz] = new double [nxy];
159- }
160- if (same)
161- {
162- for (int ix = 0 ; ix < nx; ix++)
163- {
164- for (int iy = 0 ; iy < ny; iy++)
165- {
166- for (int iz = 0 ; iz < nz; iz++)
167- {
168- ifs >> read_rho[iz][ix * ny + iy];
169- }
170- }
171- }
172- }
173- else
174- {
175- ModuleIO::trilinear_interpolate (ifs, nx_read, ny_read, nz_read, nx, ny, nz, read_rho);
176- }
186+ Pgrid->zpiece_to_all (read_rho[iz].data (), iz, data);
177187 }
178188 else
179189 {
180- zpiece = new double [nxy];
181- ModuleBase::GlobalFunc::ZEROS (zpiece, nxy);
182- }
183-
184- for (int iz = 0 ; iz < nz; iz++)
185- {
186- if (my_rank == 0 || (esolver_type == " sdft" && rank_in_stogroup == 0 ))
187- {
188- zpiece = read_rho[iz];
189- }
190- Pgrid->zpiece_to_all (zpiece, iz, data);
191- } // iz
192-
193- if (my_rank == 0 || (esolver_type == " sdft" && rank_in_stogroup == 0 ))
194- {
190+ std::vector<double > zpiece (nxy);
195191 for (int iz = 0 ; iz < nz; iz++)
196- {
197- delete[] read_rho[iz];
198- }
199- delete[] read_rho;
200- }
201- else
202- {
203- delete[] zpiece;
192+ Pgrid->zpiece_to_all (zpiece.data (), iz, data);
204193 }
205194#else
206- ofs_running << " Read SPIN = " << is + 1 << " charge now." << std::endl;
207- if (same)
208- {
209- for (int i = 0 ; i < nx; i++)
210- {
211- for (int j = 0 ; j < ny; j++)
212- {
213- for (int k = 0 ; k < nz; k++)
214- {
215- ifs >> data[k * nx * ny + i * ny + j];
216- }
217- }
218- }
219- }
220- else
221- {
222- ModuleIO::trilinear_interpolate (ifs, nx_read, ny_read, nz_read, nx, ny, nz, data);
223- }
195+ ModuleIO::trilinear_interpolate (ifs, nx_read, ny_read, nz_read, nx, ny, nz, data);
224196#endif
225197}
226198
@@ -232,7 +204,7 @@ void ModuleIO::trilinear_interpolate(std::ifstream& ifs,
232204 const int & ny,
233205 const int & nz,
234206#ifdef __MPI
235- double ** data
207+ std::vector<std::vector< double >> & data
236208#else
237209 double * data
238210#endif
0 commit comments