forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_op.c
213 lines (167 loc) · 5.17 KB
/
split_op.c
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
#include <complex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// Using fftw3 library.
#include <fftw3.h>
struct params {
double xmax;
unsigned int res;
double dt;
unsigned int timesteps;
double dx;
double *x;
double dk;
double *k;
bool im_time;
};
struct operators {
size_t size;
double complex *v;
double complex *pe;
double complex *ke;
double complex *wfc;
};
void fft(double complex *x, size_t n, bool inverse) {
double complex y[n];
memset(y, 0, sizeof(y));
fftw_plan p;
if (inverse) {
p = fftw_plan_dft_1d((int)n, (fftw_complex*)x, (fftw_complex*)y,
FFTW_BACKWARD, FFTW_ESTIMATE);
} else {
p = fftw_plan_dft_1d((int)n, (fftw_complex*)x, (fftw_complex*)y,
FFTW_FORWARD, FFTW_ESTIMATE);
}
fftw_execute(p);
fftw_destroy_plan(p);
for (size_t i = 0; i < n; ++i) {
x[i] = y[i] / sqrt((double)n);
}
}
void init_params(struct params *par, double xmax, unsigned int res, double dt,
unsigned int timesteps, bool im) {
par->xmax = xmax;
par->res = res;
par->dt = dt;
par->timesteps = timesteps;
par->dx = 2.0 * xmax / res;
par->x = malloc(sizeof(double) * res);
par->dk = M_PI / xmax;
par->k = malloc(sizeof(double) * res);
par->im_time = im;
for (size_t i = 0; i < res; ++i) {
par->x[i] = xmax / res - xmax + (double)i * (2.0 * xmax / res);
if (i < res / 2) {
par->k[i] = (double)i * M_PI / xmax;
} else {
par->k[i] = ((double)i - res) * M_PI / xmax;
}
}
}
void init_operators(struct operators *opr, struct params par, double voffset,
double wfcoffset) {
opr->size = par.res;
opr->v = malloc(sizeof(double complex) * par.res);
opr->pe = malloc(sizeof(double complex) * par.res);
opr->ke = malloc(sizeof(double complex) * par.res);
opr->wfc = malloc(sizeof(double complex) * par.res);
for (size_t i = 0; i < par.res; ++i) {
opr->v[i] = 0.5 * cpow(par.x[i] - voffset, 2);
opr->wfc[i] = cexp(-cpow(par.x[i] - wfcoffset, 2) / 2.0);
if (par.im_time) {
opr->ke[i] = cexp(-0.5 * par.dt * cpow(par.k[i], 2));
opr->pe[i] = cexp(-0.5 * par.dt * opr->v[i]);
} else {
opr->ke[i] = cexp(-0.5 * par.dt * cpow(par.k[i], 2) * I);
opr->pe[i] = cexp(-0.5 * par.dt * opr->v[i] * I);
}
}
}
void split_op(struct params par, struct operators opr) {
double density[opr.size];
for (size_t i = 0; i < par.timesteps; ++i) {
for (size_t j = 0; j < opr.size; ++j) {
opr.wfc[j] *= opr.pe[j];
}
fft(opr.wfc, opr.size, false);
for (size_t j = 0; j < opr.size; ++j) {
opr.wfc[j] *= opr.ke[j];
}
fft(opr.wfc, opr.size, true);
for (size_t j = 0; j < opr.size; ++j) {
opr.wfc[j] *= opr.pe[j];
}
for (size_t j = 0; j < opr.size; ++j) {
density[j] = pow(cabs(opr.wfc[j]), 2);
}
if (par.im_time) {
double sum = 0;
for (size_t j = 0; j < opr.size; ++j) {
sum += density[j];
}
sum *= par.dx;
for (size_t j = 0; j < opr.size; ++j) {
opr.wfc[j] /= sqrt(sum);
}
}
// Writing data into a file in the format of:
// index, density, real potential.
char filename[256];
sprintf(filename, "output%lu.dat", i);
FILE *fp = fopen(filename, "w");
for (size_t i = 0; i < opr.size; ++i) {
fprintf(fp, "%ld\t%f\t%f\n", i, density[i], creal(opr.v[i]));
}
fclose(fp);
}
}
double calculate_energy(struct params par, struct operators opr) {
double complex wfc_r[opr.size];
double complex wfc_k[opr.size];
double complex wfc_c[opr.size];
memcpy(wfc_r, opr.wfc, sizeof(wfc_r));
memcpy(wfc_k, opr.wfc, sizeof(wfc_k));
fft(wfc_k, opr.size, false);
for (size_t i = 0; i < opr.size; ++i) {
wfc_c[i] = conj(wfc_r[i]);
}
double complex energy_k[opr.size];
double complex energy_r[opr.size];
for (size_t i = 0; i < opr.size; ++i) {
energy_k[i] = wfc_k[i] * cpow(par.k[i] + 0.0*I, 2);
}
fft(energy_k, opr.size, true);
for (size_t i = 0; i < opr.size; ++i) {
energy_k[i] *= 0.5 * wfc_c[i];
energy_r[i] = wfc_c[i] * opr.v[i] * wfc_r[i];
}
double energy_final = 0;
for (size_t i = 0; i < opr.size; ++i) {
energy_final += creal(energy_k[i] + energy_r[i]);
}
return energy_final * par.dx;
}
void free_params(struct params par) {
free(par.x);
free(par.k);
}
void free_operators(struct operators opr) {
free(opr.v);
free(opr.pe);
free(opr.ke);
free(opr.wfc);
}
int main() {
struct params par;
struct operators opr;
init_params(&par, 5.0, 256, 0.05, 100, true);
init_operators(&opr, par, 0.0, -1.0);
split_op(par, opr);
printf("the energy is %f\n", calculate_energy(par, opr));
free_params(par);
free_operators(opr);
return 0;
}