-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathslabcc_math.cpp
287 lines (245 loc) · 9.66 KB
/
slabcc_math.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (c) 2018-2023, University of Bremen, M. Farzalipour Tabriz
// Copyrights licensed under the 2-Clause BSD License.
// See the accompanying LICENSE.txt file for terms.
#include "slabcc_math.hpp"
arma::cube interp3(const arma::rowvec &x, const arma::rowvec &y,
const arma::rowvec &z, const arma::cube &v,
const arma::rowvec &xi, const arma::rowvec &yi,
const arma::rowvec &zi) {
arma::cube v_x = arma::zeros(xi.n_elem, y.n_elem, z.n_elem);
arma::cube v_xy = arma::zeros(xi.n_elem, yi.n_elem, z.n_elem);
arma::cube v_xyz = arma::zeros(xi.n_elem, yi.n_elem, zi.n_elem);
arma::vec vec_i = arma::zeros<arma::vec>(x.n_elem);
for (arma::uword i = 0; i < y.n_elem; ++i) {
for (arma::uword j = 0; j < z.n_elem; ++j) {
vec_i = arma::vectorise(v(arma::span(), arma::span(i), arma::span(j)));
Spline<double> sp(x, vec_i);
v_x(arma::span(), arma::span(i), arma::span(j)) = sp.interpolate(xi);
}
}
vec_i.set_size(y.n_elem);
for (arma::uword i = 0; i < xi.n_elem; ++i) {
for (arma::uword j = 0; j < z.n_elem; ++j) {
vec_i = vectorise(v_x(arma::span(i), arma::span(), arma::span(j)));
Spline<double> sp(y, vec_i);
v_xy(arma::span(i), arma::span(), arma::span(j)) = sp.interpolate(yi);
}
}
vec_i.set_size(z.n_elem);
for (arma::uword i = 0; i < xi.n_elem; ++i) {
for (arma::uword j = 0; j < yi.n_elem; ++j) {
vec_i = vectorise(v_xy(arma::span(i), arma::span(j), arma::span()));
Spline<double> sp(z, vec_i);
v_xyz(arma::span(i), arma::span(j), arma::span()) = sp.interpolate(zi);
}
}
return v_xyz;
}
arma::cube interp3(const arma::cube &v, const arma::rowvec &xi,
const arma::rowvec &yi, const arma::rowvec &zi) {
return interp3(arma::regspace<arma::rowvec>(1, v.n_rows),
arma::regspace<arma::rowvec>(1, v.n_cols),
arma::regspace<arma::rowvec>(1, v.n_slices), v, xi, yi, zi);
}
std::tuple<arma::cube, arma::cube, arma::cube>
ndgrid(const arma::rowvec &v1, const arma::rowvec &v2, const arma::rowvec &v3) {
arma::cube x(v1.n_elem, v2.n_elem, v3.n_elem, arma::fill::ones), y = x, z = x;
arma::mat s_mat(v1.n_elem, v2.n_elem, arma::fill::zeros);
s_mat.each_col() = v1.t();
x.each_slice() = s_mat;
s_mat.each_row() = v2;
y.each_slice() = s_mat;
for (arma::uword i = 0; i < v3.n_elem; ++i) {
z.slice(i) *= v3(i);
}
return std::make_tuple(x, y, z);
}
std::tuple<arma::cube, arma::cube, arma::cube>
meshgrid(const arma::rowvec &v1, const arma::rowvec &v2,
const arma::rowvec &v3) {
arma::cube x2, y2, z2;
std::tie(y2, x2, z2) = ndgrid(v2, v1, v3);
return std::make_tuple(x2, y2, z2);
}
arma::cube shift(arma::cube cube_in, arma::rowvec3 shifts) {
if (cube_in.is_empty()) {
return {};
}
shifts = arma::round(arma::rowvec3(SizeVec(cube_in) % shifts));
for (arma::uword i = 0; i < 3; ++i) {
cube_in = shift(cube_in, shifts(i), i);
}
return cube_in;
}
arma::vec planar_average(const arma::uword &direction,
const arma::cube &cube_in) {
const arma::uword dim_size = arma::size(cube_in)(direction);
arma::vec average = arma::vec(dim_size);
for (arma::uword i = 0; i < dim_size; ++i) {
switch (direction) {
case 0:
average(i) =
arma::accu(cube_in(arma::span(i), arma::span(), arma::span()));
break;
case 1:
average(i) =
arma::accu(cube_in(arma::span(), arma::span(i), arma::span()));
break;
case 2:
average(i) =
arma::accu(cube_in(arma::span(), arma::span(), arma::span(i)));
break;
}
}
return average;
}
arma::cx_vec fft(arma::vec X) {
arma::cx_vec out(X.n_elem);
fftw_plan plan = fftw_plan_dft_r2c_1d(
X.n_elem, X.memptr(), reinterpret_cast<fftw_complex *>(out.memptr()),
FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
for (arma::uword i = out.n_elem / 2 + 1; i < out.n_elem; ++i)
out(i) = std::conj(out(X.n_rows - i));
return out;
}
arma::cx_vec fft(arma::cx_vec X) {
arma::cx_vec out(X.n_elem);
fftw_plan plan =
fftw_plan_dft_1d(X.n_elem, reinterpret_cast<fftw_complex *>(X.memptr()),
reinterpret_cast<fftw_complex *>(out.memptr()),
FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
return out;
}
arma::cx_cube fft(arma::cube X) {
arma::cx_cube out(X.n_rows / 2 + 1, X.n_cols, X.n_slices);
fftw_plan plan = fftw_plan_dft_r2c_3d(
X.n_slices, X.n_cols, X.n_rows, X.memptr(),
reinterpret_cast<fftw_complex *>(out.memptr()), FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
out.resize(X.n_rows, X.n_cols, X.n_slices);
for (arma::uword i = X.n_rows / 2 + 1; i < X.n_rows; ++i) {
out(i, 0, 0) = std::conj(out(X.n_rows - i, 0, 0));
for (arma::uword j = 1; j < X.n_cols; ++j) {
out(i, j, 0) = std::conj(out(X.n_rows - i, X.n_cols - j, 0));
}
for (arma::uword k = 1; k < X.n_slices; ++k) {
out(i, 0, k) = std::conj(out(X.n_rows - i, 0, X.n_slices - k));
}
for (arma::uword j = 1; j < X.n_cols; ++j) {
for (arma::uword k = 1; k < X.n_slices; ++k) {
out(i, j, k) =
std::conj(out(X.n_rows - i, X.n_cols - j, X.n_slices - k));
}
}
}
return out;
}
arma::cx_cube fft(arma::cx_cube X) {
arma::cx_cube fft(X.n_rows, X.n_cols, X.n_slices);
fftw_plan plan =
fftw_plan_dft_3d(X.n_slices, X.n_cols, X.n_rows,
reinterpret_cast<fftw_complex *>(X.memptr()),
reinterpret_cast<fftw_complex *>(fft.memptr()),
FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
return fft;
}
arma::cx_vec ifft(arma::cx_vec X) {
arma::cx_vec out(X.n_elem);
fftw_plan plan =
fftw_plan_dft_1d(X.n_elem, reinterpret_cast<fftw_complex *>(X.memptr()),
reinterpret_cast<fftw_complex *>(out.memptr()),
FFTW_BACKWARD, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
return out / out.n_elem;
}
arma::cx_cube ifft(arma::cx_cube X) {
arma::cx_cube ifft(X.n_rows, X.n_cols, X.n_slices);
fftw_plan plan =
fftw_plan_dft_3d(X.n_slices, X.n_cols, X.n_rows,
reinterpret_cast<fftw_complex *>(X.memptr()),
reinterpret_cast<fftw_complex *>(ifft.memptr()),
FFTW_BACKWARD, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
return ifft / X.n_elem;
}
arma::SizeCube as_size(const arma::urowvec3 &vec) {
return arma::SizeCube(vec(0), vec(1), vec(2));
}
arma::SizeMat as_size(const arma::urowvec2 &vec) {
return arma::SizeMat(vec(0), vec(1));
}
arma::mat fmod(arma::mat mat_in, const double &denom) noexcept {
mat_in.for_each([&denom](double &val) noexcept { val = fmod(val, denom); });
return mat_in;
}
arma::mat fmod_p(arma::mat mat_in, const double &denom) noexcept {
mat_in.for_each([&denom](double &val) noexcept { val = fmod_p(val, denom); });
return mat_in;
}
double fmod_p(double num, const double &denom) noexcept {
num = fmod(num, denom);
if (num < 0)
num += std::abs(denom);
return num;
}
arma::cx_cube poisson_solver_3D(const arma::cx_cube &rho, arma::mat diel,
arma::rowvec3 lengths,
arma::uword normal_direction) {
auto n_points = SizeVec(rho);
if (normal_direction != 2) {
n_points.swap_cols(normal_direction, 2);
lengths.swap_cols(normal_direction, 2);
diel.swap_cols(normal_direction, 2);
}
const arma::rowvec Gs = 2.0 * PI / lengths;
arma::rowvec Gx0 = arma::ceil(arma::regspace<arma::rowvec>(
-0.5 * n_points(0), 0.5 * n_points(0) - 1)) *
Gs(0);
arma::rowvec Gy0 = arma::ceil(arma::regspace<arma::rowvec>(
-0.5 * n_points(1), 0.5 * n_points(1) - 1)) *
Gs(1);
arma::rowvec Gz0 = arma::ceil(arma::regspace<arma::rowvec>(
-0.5 * n_points(2), 0.5 * n_points(2) - 1)) *
Gs(2);
Gx0 = ifftshift(Gx0);
Gy0 = ifftshift(Gy0);
Gz0 = ifftshift(Gz0);
// 4PI is for the atomic units
const auto rhok = fft(arma::cx_cube(4.0 * PI * rho));
const arma::cx_mat dielsG = fft(diel);
const arma::cx_mat eps11 = arma::circ_toeplitz(dielsG.col(0)) / Gz0.n_elem;
const arma::cx_mat eps22 = arma::circ_toeplitz(dielsG.col(1)) / Gz0.n_elem;
const arma::cx_mat eps33 = arma::circ_toeplitz(dielsG.col(2)) / Gz0.n_elem;
const arma::mat GzGzp = Gz0.t() * Gz0;
const arma::cx_mat Az = eps33 % GzGzp;
arma::cx_cube Vk(arma::size(rhok));
#pragma omp parallel for firstprivate(Az, eps11, eps22, rhok)
for (arma::uword k = 0; k < Gx0.n_elem; ++k) {
const arma::cx_mat eps11_Gx0k2 = eps11 * square(Gx0(k));
for (arma::uword m = 0; m < Gy0.n_elem; ++m) {
std::vector<arma::span> spans = {arma::span(k), arma::span(m),
arma::span()};
std::swap(spans[normal_direction], spans[2]);
arma::cx_mat AG = Az + eps11_Gx0k2 + eps22 * square(Gy0(m));
if ((k == 0) && (m == 0)) {
AG(0, 0) = 1;
}
Vk(spans[0], spans[1], spans[2]) =
arma::solve(AG, arma::vectorise(rhok(spans[0], spans[1], spans[2])));
}
}
// 0,0,0 in k-space corresponds to a constant in the real space: average
// potential over the supercell.
Vk(0, 0, 0) = 0;
const arma::cx_cube V = ifft(Vk);
return V;
}