@@ -80,12 +80,9 @@ void ModuleIO::write_grid(
8080 for (int i = 0 ;i < 2 ;++i) { std::getline (ss, comment[i]); }
8181
8282 double fac = ucell->lat0 ;
83- std::vector<std::vector<double >> axis_vecs =
84- {
85- {fac * ucell->latvec .e11 / double (nx), fac * ucell->latvec .e12 / double (nx), fac * ucell->latvec .e13 / double (nx)},
86- {fac * ucell->latvec .e21 / double (ny), fac * ucell->latvec .e22 / double (ny), fac * ucell->latvec .e23 / double (ny)},
87- {fac * ucell->latvec .e31 / double (nz), fac * ucell->latvec .e32 / double (nz), fac * ucell->latvec .e33 / double (nz)}
88- };
83+ std::vector<double > dx = { fac * ucell->latvec .e11 / double (nx), fac * ucell->latvec .e12 / double (nx), fac * ucell->latvec .e13 / double (nx) };
84+ std::vector<double > dy = { fac * ucell->latvec .e21 / double (ny), fac * ucell->latvec .e22 / double (ny), fac * ucell->latvec .e23 / double (ny) };
85+ std::vector<double > dz = { fac * ucell->latvec .e31 / double (nz), fac * ucell->latvec .e32 / double (nz), fac * ucell->latvec .e33 / double (nz) };
8986
9087 std::string element = " " ;
9188 std::vector<int > atom_type;
@@ -125,7 +122,7 @@ void ModuleIO::write_grid(
125122 atom_pos.push_back ({ fac * ucell->atoms [it].tau [ia].x , fac * ucell->atoms [it].tau [ia].y , fac * ucell->atoms [it].tau [ia].z });
126123 }
127124 }
128- write_cube (fn, comment, ucell->nat , { 0.0 , 0.0 , 0.0 }, { nx, ny, nz }, axis_vecs , atom_type, atom_charge, atom_pos, data_xyz_full, precision);
125+ write_cube (fn, comment, ucell->nat , { 0.0 , 0.0 , 0.0 }, nx, ny, nz, dx, dy, dz , atom_type, atom_charge, atom_pos, data_xyz_full, precision);
129126 end = time (NULL );
130127 ModuleBase::GlobalFunc::OUT_TIME (" write_grid" , start, end);
131128 }
@@ -135,30 +132,46 @@ void ModuleIO::write_grid(
135132
136133void ModuleIO::write_cube (const std::string& file,
137134 const std::vector<std::string>& comment,
138- const int natom,
139- const std::vector<double >& cel_pos,
140- const std::vector<int >& nvoxel,
141- const std::vector<std::vector<double >>& axis_vecs,
135+ const int & natom,
136+ const std::vector<double >& origin,
137+ const int & nx,
138+ const int & ny,
139+ const int & nz,
140+ const std::vector<double >& dx,
141+ const std::vector<double >& dy,
142+ const std::vector<double >& dz,
142143 const std::vector<int >& atom_type,
143144 const std::vector<double >& atom_charge,
144145 const std::vector<std::vector<double >>& atom_pos,
145146 const std::vector<double >& data,
146147 const int precision,
147148 const int ndata_line)
148149{
150+ assert (comment.size () >= 2 );
151+ for (int i = 0 ;i < 2 ;++i) { assert (!comment[i].find (" \n " )); }
152+ assert (origin.size () >= 3 );
153+ assert (dx.size () >= 3 );
154+ assert (dy.size () >= 3 );
155+ assert (dz.size () >= 3 );
156+ assert (atom_type.size () >= natom);
157+ assert (atom_charge.size () >= natom);
158+ assert (atom_pos.size () >= natom);
159+ for (int i = 0 ;i < natom;++i) { assert (atom_pos[i].size () >= 3 ); }
160+ assert (data.size () >= nx * ny * nz);
161+
149162 std::ofstream ofs (file);
150163
151164 for (int i = 0 ;i < 2 ;++i) { ofs << comment[i] << " \n " ; }
152165
153166 ofs << std::fixed;
154167 ofs << std::setprecision (1 ); // as before
155- ofs << natom << " " << cel_pos[0 ] << " " << cel_pos[1 ] << " " << cel_pos[2 ] << " \n " ;
168+
169+ ofs << natom << " " << origin[0 ] << " " << origin[1 ] << " " << origin[2 ] << " \n " ;
156170
157171 ofs << std::setprecision (6 ); // as before
158- for (int i = 0 ;i < 3 ;++i)
159- {
160- ofs << nvoxel[i] << " " << axis_vecs[i][0 ] << " " << axis_vecs[i][1 ] << " " << axis_vecs[i][2 ] << " \n " ;
161- }
172+ ofs << nx << " " << dx[0 ] << " " << dx[1 ] << " " << dx[2 ] << " \n " ;
173+ ofs << ny << " " << dy[0 ] << " " << dy[1 ] << " " << dy[2 ] << " \n " ;
174+ ofs << nz << " " << dz[0 ] << " " << dz[1 ] << " " << dz[2 ] << " \n " ;
162175
163176 for (int i = 0 ;i < natom;++i)
164177 {
@@ -168,8 +181,7 @@ void ModuleIO::write_cube(const std::string& file,
168181 ofs.unsetf (std::ofstream::fixed);
169182 ofs << std::setprecision (precision);
170183 ofs << std::scientific;
171- const int nxy = nvoxel[0 ] * nvoxel[1 ];
172- const int nz = nvoxel[2 ];
184+ const int nxy = nx * ny;
173185 for (int ixy = 0 ; ixy < nxy; ++ixy)
174186 {
175187 for (int iz = 0 ;iz < nz;++iz)
0 commit comments