Skip to content

Commit 8dabb3f

Browse files
committed
Refactor ModuleIO::write_cube_core()
1 parent ee516c5 commit 8dabb3f

File tree

2 files changed

+28
-36
lines changed

2 files changed

+28
-36
lines changed

source/module_io/cube_io.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ extern void write_cube_core(
7878
const int startz_current,
7979
#endif
8080
const double*const data,
81-
const int nx,
82-
const int ny,
83-
const int nz);
81+
const int nxy,
82+
const int nz,
83+
const int n_data_newline);
8484

8585
/**
8686
* @brief The trilinear interpolation method

source/module_io/write_cube.cpp

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ void ModuleIO::write_cube(
127127
}
128128

129129
#ifdef __MPI
130-
ModuleIO::write_cube_core(ofs_cube, bz, nbz, nplane, startz_current, data, nx, ny, nz);
130+
ModuleIO::write_cube_core(ofs_cube, bz, nbz, nplane, startz_current, data, nx*ny, nz, 6);
131131
#else
132-
ModuleIO::write_cube_core(ofs_cube, data, nx, ny, nz);
132+
ModuleIO::write_cube_core(ofs_cube, data, nx*ny, nz, 6);
133133
#endif
134134

135135
if (my_rank == 0)
@@ -154,15 +154,14 @@ void ModuleIO::write_cube_core(
154154
const int startz_current,
155155
#endif
156156
const double*const data,
157-
const int nx,
158-
const int ny,
159-
const int nz)
157+
const int nxy,
158+
const int nz,
159+
const int n_data_newline)
160160
{
161161
ModuleBase::TITLE("ModuleIO", "write_cube_core");
162162

163163
#ifdef __MPI
164164

165-
166165
const int my_rank = GlobalV::MY_RANK;
167166
const int my_pool = GlobalV::MY_POOL;
168167
const int rank_in_pool = GlobalV::RANK_IN_POOL;
@@ -172,7 +171,7 @@ void ModuleIO::write_cube_core(
172171
if (my_pool == 0)
173172
{
174173
/// for cube file
175-
const int nxyz = nx * ny * nz;
174+
const int nxyz = nxy * nz;
176175
std::vector<double> data_cube(nxyz, 0.0);
177176

178177
// num_z: how many planes on processor 'ip'
@@ -212,7 +211,6 @@ void ModuleIO::write_cube_core(
212211
}
213212

214213
int count = 0;
215-
const int nxy = nx * ny;
216214
std::vector<double> zpiece(nxy, 0.0);
217215

218216
// save the rho one z by one z.
@@ -227,21 +225,21 @@ void ModuleIO::write_cube_core(
227225
// case 1: the first part of rho in processor 0.
228226
if (which_ip[iz] == 0 && rank_in_pool == 0)
229227
{
230-
for (int ir = 0; ir < nxy; ir++)
228+
for (int ixy = 0; ixy < nxy; ixy++)
231229
{
232230
// mohan change to rho_save on 2012-02-10
233231
// because this can make our next restart calculation lead
234232
// to the same scf_thr as the one saved.
235-
zpiece[ir] = data[ir * nplane + iz - startz_current];
233+
zpiece[ixy] = data[ixy * nplane + iz - startz_current];
236234
}
237235
}
238236
// case 2: > first part rho: send the rho to
239237
// processor 0.
240238
else if (which_ip[iz] == rank_in_pool)
241239
{
242-
for (int ir = 0; ir < nxy; ir++)
240+
for (int ixy = 0; ixy < nxy; ixy++)
243241
{
244-
zpiece[ir] = data[ir * nplane + iz - startz_current];
242+
zpiece[ixy] = data[ixy * nplane + iz - startz_current];
245243
}
246244
MPI_Send(zpiece.data(), nxy, MPI_DOUBLE, 0, tag, POOL_WORLD);
247245
}
@@ -256,9 +254,9 @@ void ModuleIO::write_cube_core(
256254
if (my_rank == 0)
257255
{
258256
/// for cube file
259-
for (int ir = 0; ir < nxy; ir++)
257+
for (int ixy = 0; ixy < nxy; ixy++)
260258
{
261-
data_cube[ir + iz * nxy] = zpiece[ir];
259+
data_cube[ixy * nz + iz] = zpiece[ixy];
262260
}
263261
/// for cube file
264262
}
@@ -267,41 +265,35 @@ void ModuleIO::write_cube_core(
267265
// for cube file
268266
if (my_rank == 0)
269267
{
270-
for (int ix = 0; ix < nx; ix++)
268+
for (int ixy = 0; ixy < nxy; ixy++)
271269
{
272-
for (int iy = 0; iy < ny; iy++)
270+
for (int iz = 0; iz < nz; iz++)
273271
{
274-
for (int iz = 0; iz < nz; iz++)
272+
ofs_cube << " " << data_cube[ixy * nz + iz];
273+
if ((iz % n_data_newline == n_data_newline-1) && (iz != nz - 1))
275274
{
276-
ofs_cube << " " << data_cube[iz * nx * ny + ix * ny + iy];
277-
if (iz % 6 == 5 && iz != nz - 1)
278-
{
279-
ofs_cube << "\n";
280-
}
275+
ofs_cube << "\n";
281276
}
282-
ofs_cube << "\n";
283277
}
278+
ofs_cube << "\n";
284279
}
285280
}
286281
/// for cube file
287282
}
288283
MPI_Barrier(MPI_COMM_WORLD);
289284
#else
290-
for (int i = 0; i < nx; i++)
285+
for (int ixy = 0; ixy < nxy; ixy++)
291286
{
292-
for (int j = 0; j < ny; j++)
287+
for (int iz = 0; iz < nz; iz++)
293288
{
294-
for (int k = 0; k < nz; k++)
289+
ofs_cube << " " << data[iz * nxy + ixy];
290+
// ++count_cube;
291+
if ((iz % n_data_newline == n_data_newline-1) && (iz != nz - 1))
295292
{
296-
ofs_cube << " " << data[k * nx * ny + i * ny + j];
297-
// ++count_cube;
298-
if (k % 6 == 5 && k != nz - 1)
299-
{
300-
ofs_cube << "\n";
301-
}
293+
ofs_cube << "\n";
302294
}
303-
ofs_cube << "\n";
304295
}
296+
ofs_cube << "\n";
305297
}
306298
#endif
307299
}

0 commit comments

Comments
 (0)