Skip to content

Commit e29cf06

Browse files
committed
minor interface changes
1 parent 5f168ad commit e29cf06

File tree

3 files changed

+72
-48
lines changed

3 files changed

+72
-48
lines changed

source/module_io/cube_io.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ namespace ModuleIO
3030
bool read_cube(const std::string& file,
3131
std::vector<std::string>& comment,
3232
int& natom,
33-
std::vector<double>& cel_pos,
34-
std::vector<int>& nvoxel,
35-
std::vector<std::vector<double>>& axis_vecs,
33+
std::vector<double>& origin,
34+
int& nx,
35+
int& ny,
36+
int& nz,
37+
std::vector<double>& dx,
38+
std::vector<double>& dy,
39+
std::vector<double>& dz,
3640
std::vector<int>& atom_type,
3741
std::vector<double>& atom_charge,
3842
std::vector<std::vector<double>>& atom_pos,
@@ -41,10 +45,14 @@ namespace ModuleIO
4145
/// write a cube file
4246
void write_cube(const std::string& file,
4347
const std::vector<std::string>& comment,
44-
const int natom,
45-
const std::vector<double>& cel_pos,
46-
const std::vector<int>& nvoxel,
47-
const std::vector<std::vector<double>>& axis_vecs,
48+
const int& natom,
49+
const std::vector<double>& origin,
50+
const int& nx,
51+
const int& ny,
52+
const int& nz,
53+
const std::vector<double>& dx,
54+
const std::vector<double>& dy,
55+
const std::vector<double>& dz,
4856
const std::vector<int>& atom_type,
4957
const std::vector<double>& atom_charge,
5058
const std::vector<std::vector<double>>& atom_pos,

source/module_io/read_cube.cpp

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,22 @@ bool ModuleIO::read_grid(
3636
{
3737
std::vector<std::string> comment;
3838
int natom = 0;
39-
std::vector<double> cel_pos;
39+
std::vector<double> origin;
4040
std::vector<int> nvoxel;
41+
int nx_read = 0;
42+
int ny_read = 0;
43+
int nz_read = 0;
44+
std::vector<double> dx(3);
45+
std::vector<double> dy(3);
46+
std::vector<double> dz(3);
4147
std::vector<std::vector<double>> axis_vecs;
4248
std::vector<int> atom_type;
4349
std::vector<double> atom_charge;
4450
std::vector<std::vector<double>> atom_pos;
4551
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);
4752

48-
const int& nx_read = nvoxel[0];
49-
const int& ny_read = nvoxel[1];
50-
const int& nz_read = nvoxel[2];
53+
// we've already checked the file existence, so we don't need the returned value here
54+
ModuleIO::read_cube(fn, comment, natom, origin, nx_read, ny_read, nz_read, dx, dy, dz, atom_type, atom_charge, atom_pos, data_read);
5155

5256
// if mismatch, trilinear interpolate
5357
if (nx == nx_read && ny == ny_read && nz == nz_read)
@@ -143,9 +147,13 @@ void ModuleIO::trilinear_interpolate(
143147
bool ModuleIO::read_cube(const std::string& file,
144148
std::vector<std::string>& comment,
145149
int& natom,
146-
std::vector<double>& cell_pos,
147-
std::vector<int>& nvoxel,
148-
std::vector<std::vector<double>>& axis_vecs,
150+
std::vector<double>& origin,
151+
int& nx,
152+
int& ny,
153+
int& nz,
154+
std::vector<double>& dx,
155+
std::vector<double>& dy,
156+
std::vector<double>& dz,
149157
std::vector<int>& atom_type,
150158
std::vector<double>& atom_charge,
151159
std::vector<std::vector<double>>& atom_pos,
@@ -159,29 +167,25 @@ bool ModuleIO::read_cube(const std::string& file,
159167
for (auto& c : comment) { std::getline(ifs, c); }
160168

161169
ifs >> natom;
162-
cell_pos.resize(3);
163-
for (auto& cp : cell_pos) { ifs >> cp; }
170+
origin.resize(3);
171+
for (auto& cp : origin) { ifs >> cp; }
164172

165-
nvoxel.resize(3);
166-
axis_vecs.resize(0);
167-
for (int i = 0;i < 3;++i)
168-
{
169-
std::vector<double> vec(3);
170-
ifs >> nvoxel[i] >> vec[0] >> vec[1] >> vec[2];
171-
axis_vecs.push_back(vec);
172-
}
173+
dx.resize(3);
174+
dy.resize(3);
175+
dz.resize(3);
176+
ifs >> nx >> dx[0] >> dx[1] >> dx[2];
177+
ifs >> ny >> dy[0] >> dy[1] >> dy[2];
178+
ifs >> nz >> dz[0] >> dz[1] >> dz[2];
173179

174180
atom_type.resize(natom);
175181
atom_charge.resize(natom);
176-
atom_pos.resize(0);
182+
atom_pos.resize(natom, std::vector<double>(3));
177183
for (int i = 0;i < natom;++i)
178184
{
179-
std::vector<double> apos(3);
180-
ifs >> atom_type[i] >> atom_charge[i] >> apos[0] >> apos[1] >> apos[2];
181-
atom_pos.push_back(apos);
185+
ifs >> atom_type[i] >> atom_charge[i] >> atom_pos[i][0] >> atom_pos[i][1] >> atom_pos[i][2];
182186
}
183187

184-
const int nxyz = nvoxel[0] * nvoxel[1] * nvoxel[2];
188+
const int nxyz = nx * ny * nz;
185189
data.resize(nxyz);
186190
for (int i = 0;i < nxyz;++i) { ifs >> data[i]; }
187191

source/module_io/write_cube.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

136133
void 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") == std::string::npos); }
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

Comments
 (0)