Repository navigation
Expand file tree
/
Copy pathread_cube.cpp
More file actions
194 lines (174 loc) · 6.21 KB
/
Copy pathread_cube.cpp
File metadata and controls
194 lines (174 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "source_io/cube_io.h"
#include <limits>
#include "source_pw/module_pwdft/parallel_grid.h"
#include <cstring> // use std::memcpy
bool ModuleIO::read_vdata_palgrid(
const Parallel_Grid& pgrid,
const int my_rank,
std::ofstream& ofs_running,
const std::string& fn,
double* const data,
const int natom)
{
ModuleBase::TITLE("ModuleIO", "read_vdata_palgrid");
// check if the file exists
std::ifstream ifs(fn.c_str());
if (!ifs)
{
std::string tmp_warning_info = "!!! Couldn't find the file: " + fn;
ofs_running << tmp_warning_info << std::endl;
return false;
}
else
{
ofs_running << " Find the file " << fn << " , try to read it." << std::endl;
}
// read the full grid data
const int& nx = pgrid.nx;
const int& ny = pgrid.ny;
const int& nz = pgrid.nz;
const int& nxyz = nx * ny * nz;
std::vector<double> data_xyz_full(nxyz, 0.0);
if (my_rank == 0)
{
std::vector<std::string> comment;
int natom = 0;
std::vector<double> origin;
std::vector<int> nvoxel;
int nx_read = 0;
int ny_read = 0;
int nz_read = 0;
std::vector<double> dx(3);
std::vector<double> dy(3);
std::vector<double> dz(3);
std::vector<std::vector<double>> axis_vecs;
std::vector<int> atom_type;
std::vector<double> atom_charge;
std::vector<std::vector<double>> atom_pos;
std::vector<double> data_read;
// we've already checked the file existence, so we don't need the returned value here
ModuleIO::read_cube(fn, comment, natom, origin, nx_read, ny_read, nz_read, dx, dy, dz, atom_type, atom_charge, atom_pos, data_read);
// if mismatch, trilinear interpolate
if (nx == nx_read && ny == ny_read && nz == nz_read)
{
std::memcpy(data_xyz_full.data(), data_read.data(), nxyz * sizeof(double));
}
else
{
trilinear_interpolate(data_read.data(), nx_read, ny_read, nz_read, nx, ny, nz, data_xyz_full.data());
}
}
// distribute
#ifdef __MPI
pgrid.bcast(data_xyz_full.data(), data, my_rank);
#else
std::memcpy(data, data_xyz_full.data(), nxyz * sizeof(double));
#endif
return true;
}
void ModuleIO::trilinear_interpolate(
const double* const data_in,
const int& nx_read,
const int& ny_read,
const int& nz_read,
const int& nx,
const int& ny,
const int& nz,
double* data_out)
{
ModuleBase::TITLE("ModuleIO", "trilinear_interpolate");
double** read_rho = new double*[nz_read];
for (int iz = 0; iz < nz_read; iz++)
{
read_rho[iz] = new double[nx_read * ny_read];
}
for (int ix = 0; ix < nx_read; ix++)
{
for (int iy = 0; iy < ny_read; iy++)
{
for (int iz = 0; iz < nz_read; iz++)
{
read_rho[iz][ix * ny_read + iy] = data_in[(ix * ny_read + iy) * nz_read + iz];
}
}
}
for (int ix = 0; ix < nx; ix++)
{
double fracx = 0.5 * (static_cast<double>(nx_read) / nx * (1.0 + 2.0 * ix) - 1.0);
fracx = std::fmod(fracx, nx_read);
int lowx = static_cast<int>(fracx);
double dx = fracx - lowx;
int highx = (lowx == nx_read - 1) ? 0 : lowx + 1; // the point nz_read is the same as 0
for (int iy = 0; iy < ny; iy++)
{
double fracy = 0.5 * (static_cast<double>(ny_read) / ny * (1.0 + 2.0 * iy) - 1.0);
fracy = std::fmod(fracy, ny_read);
int lowy = static_cast<int>(fracy);
double dy = fracy - lowy;
int highy = (lowy == ny_read - 1) ? 0 : lowy + 1;
for (int iz = 0; iz < nz; iz++)
{
double fracz = 0.5 * (static_cast<double>(nz_read) / nz * (1.0 + 2.0 * iz) - 1.0);
fracz = std::fmod(fracz, nz_read);
int lowz = static_cast<int>(fracz);
double dz = fracz - lowz;
int highz = (lowz == nz_read - 1) ? 0 : lowz + 1;
double result = read_rho[lowz][lowx * ny_read + lowy] * (1 - dx) * (1 - dy) * (1 - dz)
+ read_rho[lowz][highx * ny_read + lowy] * dx * (1 - dy) * (1 - dz)
+ read_rho[lowz][lowx * ny_read + highy] * (1 - dx) * dy * (1 - dz)
+ read_rho[highz][lowx * ny_read + lowy] * (1 - dx) * (1 - dy) * dz
+ read_rho[lowz][highx * ny_read + highy] * dx * dy * (1 - dz)
+ read_rho[highz][highx * ny_read + lowy] * dx * (1 - dy) * dz
+ read_rho[highz][lowx * ny_read + highy] * (1 - dx) * dy * dz
+ read_rho[highz][highx * ny_read + highy] * dx * dy * dz;
data_out[(ix * ny + iy) * nz + iz] = result; // x > y > z order, consistent with the cube file
}
}
}
for (int iz = 0; iz < nz_read; iz++)
{
delete[] read_rho[iz];
}
delete[] read_rho;
}
bool ModuleIO::read_cube(const std::string& file,
std::vector<std::string>& comment,
int& natom,
std::vector<double>& origin,
int& nx,
int& ny,
int& nz,
std::vector<double>& dx,
std::vector<double>& dy,
std::vector<double>& dz,
std::vector<int>& atom_type,
std::vector<double>& atom_charge,
std::vector<std::vector<double>>& atom_pos,
std::vector<double>& data)
{
std::ifstream ifs(file);
if (!ifs) { return false; }
comment.resize(2);
for (auto& c : comment) { std::getline(ifs, c); }
ifs >> natom;
origin.resize(3);
for (auto& cp : origin) { ifs >> cp; }
dx.resize(3);
dy.resize(3);
dz.resize(3);
ifs >> nx >> dx[0] >> dx[1] >> dx[2];
ifs >> ny >> dy[0] >> dy[1] >> dy[2];
ifs >> nz >> dz[0] >> dz[1] >> dz[2];
atom_type.resize(natom);
atom_charge.resize(natom);
atom_pos.resize(natom, std::vector<double>(3));
for (int i = 0;i < natom;++i)
{
ifs >> atom_type[i] >> atom_charge[i] >> atom_pos[i][0] >> atom_pos[i][1] >> atom_pos[i][2];
}
const int nxyz = nx * ny * nz;
data.resize(nxyz);
for (int i = 0;i < nxyz;++i) { ifs >> data[i]; }
ifs.close();
return true;
}