22#include < limits>
33#include " module_hamilt_pw/hamilt_pwdft/parallel_grid.h"
44// #include "module_base/global_variable.h" // GlobalV reference removed
5- void ModuleIO::xyz2zxy (const double * const xyz, const int nx, const int ny, const int nz, double * const zxy)
6- {
7- for (int ix = 0 ; ix < nx; ix++)
8- {
9- for (int iy = 0 ; iy < ny; iy++)
10- {
11- for (int iz = 0 ; iz < nz; iz++)
12- {
13- zxy[(iz * nx + ix) * ny + iy] = xyz[(ix * ny + iy) * nz + iz];
14- }
15- }
16- }
17- }
185
196bool ModuleIO::read_grid (
207 const Parallel_Grid& pgrid,
@@ -44,34 +31,40 @@ bool ModuleIO::read_grid(
4431 const int & ny = pgrid.ny ;
4532 const int & nz = pgrid.nz ;
4633 const int & nxyz = nx * ny * nz;
47- std::vector<double > data_zxy_full (nxyz, 0.0 ); // [iz][ix][iy]
34+ std::vector<double > data_xyz_full (nxyz, 0.0 );
4835 if (my_rank == 0 )
4936 {
50- const CubeInfo& cube_info = ModuleIO::read_cube (fn);
51-
52- const int & nx_read = cube_info.nvoxel [0 ];
53- const int & ny_read = cube_info.nvoxel [1 ];
54- const int & nz_read = cube_info.nvoxel [2 ];
37+ std::vector<std::string> comment;
38+ int natom = 0 ;
39+ std::vector<double > cel_pos;
40+ std::vector<int > nvoxel;
41+ std::vector<std::vector<double >> axis_vecs;
42+ std::vector<int > atom_type;
43+ std::vector<double > atom_charge;
44+ std::vector<std::vector<double >> atom_pos;
45+ std::vector<double > data_read;
46+ bool valid = ModuleIO::read_cube (fn, comment, natom, cel_pos, nvoxel, axis_vecs, atom_type, atom_charge, atom_pos, data_read);
47+
48+ const int & nx_read = nvoxel[0 ];
49+ const int & ny_read = nvoxel[1 ];
50+ const int & nz_read = nvoxel[2 ];
5551
5652 // if mismatch, trilinear interpolate
5753 if (nx == nx_read && ny == ny_read && nz == nz_read)
5854 {
59- ModuleIO::xyz2zxy (cube_info .data .data (), nx, ny, nz, data_zxy_full. data ( ));
55+ std::memcpy (data_xyz_full .data (), data_read .data (), nxyz * sizeof ( double ));
6056 }
6157 else
6258 {
63- std::vector<double > data_xyz_full (nxyz, 0.0 );
64- trilinear_interpolate (cube_info.data .data (), nx_read, ny_read, nz_read, nx, ny, nz, data_xyz_full.data ());
65- ModuleIO::xyz2zxy (data_xyz_full.data (), nx, ny, nz, data_zxy_full.data ());
59+ trilinear_interpolate (data_read.data (), nx_read, ny_read, nz_read, nx, ny, nz, data_xyz_full.data ());
6660 }
6761 }
6862
6963 // distribute
7064#ifdef __MPI
71- const int nxy = nx * ny;
72- for (int iz = 0 ;iz < nz;++iz) { pgrid.zpiece_to_all (data_zxy_full.data () + iz * nxy, iz, data); }
65+ pgrid.bcast (data_xyz_full.data (), data);
7366#else
74- std::memcpy (data, data_zxy_full .data (), nxyz * sizeof (double ));
67+ std::memcpy (data, data_xyz_full .data (), nxyz * sizeof (double ));
7568#endif
7669 return true ;
7770}
@@ -147,43 +140,51 @@ void ModuleIO::trilinear_interpolate(
147140 delete[] read_rho;
148141}
149142
150- ModuleIO::CubeInfo ModuleIO::read_cube (const std::string& file)
143+ bool ModuleIO::read_cube (const std::string& file,
144+ std::vector<std::string>& comment,
145+ int & natom,
146+ std::vector<double >& cell_pos,
147+ std::vector<int >& nvoxel,
148+ std::vector<std::vector<double >>& axis_vecs,
149+ std::vector<int >& atom_type,
150+ std::vector<double >& atom_charge,
151+ std::vector<std::vector<double >>& atom_pos,
152+ std::vector<double >& data)
151153{
152154 std::ifstream ifs (file);
153155
154- if (!ifs) { return ModuleIO::CubeInfo ({}, 0 , {}, {}, {}, {}, {}, {}, {}, false ) ; }
156+ if (!ifs) { return false ; }
155157
156- std::vector<std::string> comment (2 );
158+ comment. resize (2 );
157159 for (auto & c : comment) { std::getline (ifs, c); }
158160
159- int natom;
160161 ifs >> natom;
161- std::vector< double > cell_pos (3 );
162+ cell_pos. resize (3 );
162163 for (auto & cp : cell_pos) { ifs >> cp; }
163164
164- std::vector< int > nvoxel (3 );
165- std::vector<std::vector< double >> axis_vecs ( 3 );
165+ nvoxel. resize (3 );
166+ axis_vecs. resize ( 0 );
166167 for (int i = 0 ;i < 3 ;++i)
167168 {
168169 std::vector<double > vec (3 );
169170 ifs >> nvoxel[i] >> vec[0 ] >> vec[1 ] >> vec[2 ];
170171 axis_vecs.push_back (vec);
171172 }
172173
173- std::vector< int > itype (natom);
174- std::vector< double > charge (natom);
175- std::vector<std::vector< double >> atom_pos (natom );
174+ atom_type. resize (natom);
175+ atom_charge. resize (natom);
176+ atom_pos. resize ( 0 );
176177 for (int i = 0 ;i < natom;++i)
177178 {
178179 std::vector<double > apos (3 );
179- ifs >> itype [i] >> charge [i] >> apos[0 ] >> apos[1 ] >> apos[2 ];
180+ ifs >> atom_type [i] >> atom_charge [i] >> apos[0 ] >> apos[1 ] >> apos[2 ];
180181 atom_pos.push_back (apos);
181182 }
182183
183184 const int nxyz = nvoxel[0 ] * nvoxel[1 ] * nvoxel[2 ];
184- std::vector< double > data (nxyz);
185+ data. resize (nxyz);
185186 for (int i = 0 ;i < nxyz;++i) { ifs >> data[i]; }
186187
187188 ifs.close ();
188- return ModuleIO::CubeInfo (comment, natom, cell_pos, nvoxel, axis_vecs, itype, charge, atom_pos, std::move (data), true ) ;
189+ return true ;
189190}
0 commit comments