This repository has been archived by the owner on Aug 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultigrid_PCG_C_method.m
486 lines (363 loc) · 12.4 KB
/
multigrid_PCG_C_method.m
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
clear;
clc;
global M_x; % division number of spatial mesh along the x-axis
global M_y; % division number of spatial mesh along the y-axis
global dx; % unitless spatial mesh size for the x-axis
global dy; % unitless spatial mesh size for the y-axis
global x; % unitless spatial array for the x-axis
global y; % unitless spatial array for the y-axis
global xx; % unitless spatial mesh for the x-axis
global yy; % unitless spatial mesh for the y-axis
global xi; % unitless Fourier frequency array with respect to the x-axis
global nu; % unitless Fourier frequency array with respect to the y-axis
global xxi; % unitless Fourier frequency mesh with respect to the x-axis
global nnu; % unitless Fourier frequency mesh with respect to the y-axis
global V; % unitless potential
global E; % unitless energy of state
global phi_n; % unitless wave function
global L; L = 20; % unitless half length of bounded domain
global N; N = (8.1e5)^(4/5); % number of atoms
global g; g = 6.552e-2; % unitless 2D unitless coupling constant
global gamma_y; gamma_y = 1; % omega_y/omega_x
global Omega; Omega = 0.8; % unitless rotation frequency
global initial_wf; initial_wf = 'phi_a'; % initial wave function
global epsilon_energy; epsilon_energy = 1e-12; % stopping criteria value
% for the energy minimization
% for the multi-grid method
p_initial = 6;
p_end = 9;
Ms = 2.^(p_initial:p_end);
CPU_t_initial = cputime; % storing the time value at the beginning
% starting the multigrid method
for M_i = Ms
M_x = M_i;
M_y = M_i;
dx = 2 * L / (M_x);
dy = 2 * L / (M_y);
x = (-M_x / 2:M_x / 2 - 1) * dx;
y = (-M_y / 2:M_y / 2 - 1) * dy;
% if the current division number is not equal to 2^p_initial,
if M_i ~= Ms(1)
% store the last spatial meshes for the interpolation
xx_last = xx;
yy_last = yy;
% calculate new spatial meshes
[xx, yy] = meshgrid(x, y);
% interpolate
phi_n = interp2(xx_last, yy_last, phi_n, xx, yy, 'cubic');
phi_n(isnan(phi_n)) = 0;
else
% calculate new spatial meshes
[xx, yy] = meshgrid(x, y);
% defining the functions of possible initial wave functions
phi_0s.phi_a = calc_phi_a;
phi_0s.phi_b = calc_phi_b;
phi_0s.phi_b_bar = calc_phi_b_bar;
phi_0s.phi_c = calc_phi_c;
phi_0s.phi_c_bar = calc_phi_c_bar;
phi_0s.phi_d = calc_phi_d;
phi_0s.phi_d_bar = calc_phi_d_bar;
phi_0s.phi_e = calc_phi_e;
phi_0s.phi_e_bar = calc_phi_e_bar;
% initialize the wave function with the chosen state
phi_n = phi_0s.(initial_wf)();
end
xi = (-M_x / 2:M_x / 2 - 1) .* 2 * pi / (2 * L);
nu = (-M_y / 2:M_y / 2 - 1) .* 2 * pi / (2 * L);
[xxi, nnu] = meshgrid(xi, nu);
V = 0.5 * (xx.^2 + gamma_y^2 * yy.^2);
phi_n = normalize(phi_n);
E = calc_E(phi_n);
% running the PG_C method once
[mu_n, r_n, p_n, phi_n] = PG_C();
E = calc_E(phi_n);
count = 0;
% run the PCG_C method until energy_err_n is less than epsilon_energy
while true
r_n_minus_1 = r_n;
p_n_minus_1 = p_n;
[mu_n, r_n, p_n, phi_n] = PCG_C(r_n_minus_1, p_n_minus_1);
E_new = calc_E(phi_n);
count = count + 1;
energy_err_n = abs(E-E_new);
E = E_new;
if energy_err_n < epsilon_energy
break
end
end
end
CPU_t_final = cputime; % storing the time value at the end
CPU_t = CPU_t_final - CPU_t_initial; % calculating the CPU time
prob_density = abs(phi_n).^2; % calculating the probability density
hold on
% setting the interpreter of plot to LaTeX
set(gca, 'DefaultTextInterpreter', 'latex');
set(gca, 'defaultAxesTickLabelInterpreter', 'latex');
set(gca, 'defaulttextinterpreter', 'latex');
set(gca, 'defaultLegendInterpreter', 'latex');
set(gca, 'TickLabelInterpreter', 'latex');
% plotting the density
surf(xx, yy, prob_density, 'EdgeColor', 'None', 'facecolor', 'interp')
view(2) % displaying the plot in a 2D view
daspect([1, 1, 1]) % using equal unit lengths along each axis
colormap jet % setting current colormap to jet
% specifying the axis limits
xlim([-L, L - dx])
ylim([-L, L - dy])
% labeling each axis
xlabel('$x$')
ylabel('$y$')
% setting the plot title
title(['$N = $ ', num2str(N),'$, g = $ ', num2str(g), ', $\Omega = $ ', num2str(Omega), ', $E =$ ', num2str(real(E), '%.5f'), ', CPU time: ', num2str(CPU_t), ' s'])
% creating a colorbar
c = colorbar;
c.Label.String = '$|\psi(x,y)|^2$';
c.Label.Interpreter = 'latex';
c.TickLabelInterpreter = 'latex';
hold off
% preparing the filname
Omega_str_wo_dot = num2str(Omega, '%.2f');
Omega_str_wo_dot(Omega_str_wo_dot == '.') = [];
g_str_wo_dot = num2str(g, '%.2f');
g_str_wo_dot(g_str_wo_dot == '.') = [];
filename = ['N_', num2str(N, '%.0f'), '_g_', g_str_wo_dot, '_Omega_', Omega_str_wo_dot];
% saving several variables in .MAT format
save([filename, '.mat'], 'phi_n', 'g', 'Omega', 'gamma_y', 'N', 'L', 'M_x', 'M_y', 'x', 'y', 'mu_n', 'E')
% saving the plot in .EPS format
exportgraphics(gcf, [filename, '.eps'], 'Resolution', 300)
%-------------------------------------------------------------------------%
% FUNCTIONS
function output = allsum(x)
output = sum(x, 'all');
end
function phi_a = calc_phi_a()
% calculating (a) initial condition
global xx;
global yy;
phi_a = exp(-(xx.^2 + yy.^2)/2) / sqrt(pi);
end
function phi_b = calc_phi_b()
% calculating (b) initial condition
global xx;
global yy;
phi_a = calc_phi_a();
phi_b = phi_a .* (xx + 1j * yy);
end
function phi_b_bar = calc_phi_b_bar()
% calculating (b-bar) initial condition
phi_b = calc_phi_b();
phi_b_bar = conj(phi_b);
end
function phi_c = calc_phi_c()
% calculating (c) initial condition
phi_a = calc_phi_a();
phi_b = calc_phi_b();
phi_c = (phi_a + phi_b) ./ 2;
end
function phi_c_bar = calc_phi_c_bar()
% calculating (c-bar) initial condition
phi_c = calc_phi_c();
phi_c_bar = conj(phi_c);
end
function phi_d = calc_phi_d()
% calculating (d) initial condition
global Omega;
phi_a = calc_phi_a();
phi_b = calc_phi_b();
phi_d = (1 - Omega) * phi_a + Omega * phi_b;
end
function phi_d_bar = calc_phi_d_bar()
% calculating (d-bar) initial condition
phi_d = calc_phi_d();
phi_d_bar = conj(phi_d);
end
function phi_e = calc_phi_e()
% calculating (e) initial condition
global Omega;
phi_a = calc_phi_a();
phi_b = calc_phi_b();
phi_e = (1 - Omega) * phi_b + Omega * phi_a;
end
function phi_e_bar = calc_phi_e_bar()
% calculating (d-bar) initial condition
phi_e = calc_phi_e();
phi_e_bar = conj(phi_e);
end
function normalized_state = normalize(state)
% normalizing the given state
global dx;
global dy;
global N;
integral = allsum(abs(state).^2) * dx * dy;
norm_factor = sqrt(N/integral);
normalized_state = norm_factor * state;
end
function H_times_state = H_times_state(state)
% calculating H times the given state
global xx;
global yy;
global xxi
global nnu;
global Omega;
global g;
global V;
global phi_n;
H_1 = 0.5 * xxi.^2 + Omega * yy .* xxi;
state_kp = fftshift(fft(state, [], 2), 2); % FFT along the x-axis
state_kp = H_1 .* state_kp;
H_1_times_state = ifft(ifftshift(state_kp, 2), [], 2); % IFFT along the
% x-axis
H_2 = 0.5 * nnu.^2 - Omega * xx .* nnu;
state_qj = fftshift(fft(state, [], 1), 1); % FFT along the y-axis
state_qj = H_2 .* state_qj;
H_2_times_state = ifft(ifftshift(state_qj, 1), [], 1); % IFFT along the
% y-axis
H_times_state = H_1_times_state + H_2_times_state + (V + g * abs(phi_n).^2) .* state;
end
function E = calc_E(state)
% calculating the energy of given state
global xx;
global yy;
global xxi
global nnu;
global Omega;
global g;
global V;
global dx;
global dy;
global N;
H_1_op = 0.5 * xxi.^2 + Omega * yy .* xxi;
state_kp = fftshift(fft(state, [], 2), 2); % FFT along the x-axis
state_kp = H_1_op .* state_kp;
H_1_times_state = ifft(ifftshift(state_kp, 2), [], 2); % IFFT along the
% x-axis
H_2_op = 0.5 * nnu.^2 - Omega * xx .* nnu;
state_qj = fftshift(fft(state, [], 1), 1); % FFT along the y-axis
state_qj = H_2_op .* state_qj;
H_2_times_state = ifft(ifftshift(state_qj, 1), [], 1); % IFFT along the
% y-axis
H_state = H_1_times_state + H_2_times_state + (V + 0.5 * g * abs(state).^2) .* state;
E = allsum((conj(state).*H_state)) * dx * dy / N;
E = real(E);
end
function mu = calc_mu()
% calculating the chemical energy of phi_n
global phi_n;
global N;
global dx;
global dy;
mu = allsum((conj(phi_n).*H_times_state(phi_n))) * dx * dy / N;
mu = real(mu);
end
function P_C_times_state = calc_P_C_times_state(state)
% calculating the preconditioner P_C times the given state
global xxi
global nnu;
global g;
global V;
global phi_n;
global dx;
global dy;
global N;
phi_n_qp = fftshift(fft2(phi_n));
phi_n_qp = -(xxi.^2 + nnu.^2) .* phi_n_qp;
lap_times_phi_n = ifft2(ifftshift(phi_n_qp));
alpha_Delta = -0.5 * conj(phi_n) .* lap_times_phi_n + V .* abs(phi_n).^2 + g * abs(phi_n).^4;
alpha_Delta = real(sum(sum(alpha_Delta))*dx*dy/N);
alpha_V = alpha_Delta;
P_V = sqrt(alpha_V+V+g*abs(phi_n).^2).^(-1);
P_Delta = (alpha_Delta + 0.5 * (xxi.^2 + nnu.^2)).^(-1);
P_C_times_state = P_V .* state;
P_C_times_state_qp = fftshift(fft2(P_C_times_state));
P_C_times_state_qp = P_Delta .* P_C_times_state_qp;
P_C_times_state = ifft2(ifftshift(P_C_times_state_qp));
P_C_times_state = P_V .* P_C_times_state;
end
function p_n_norm = calc_p_n_norm(p_n)
% calculating the norm of p_n
global dx;
global dy;
p_n_norm = sqrt(allsum(abs(p_n).^2)*dx*dy);
end
function [mu_n, r_n, p_n, phi_n_plus_1] = PG_C()
% finding phi_n_plus_1 by running the PG_C method once
global phi_n;
global dx;
global dy;
global N;
global g;
global E;
mu_n = calc_mu();
r_n = H_times_state(phi_n) - mu_n * phi_n;
d_n = -calc_P_C_times_state(r_n);
p_n = d_n - real(allsum((d_n) .* conj(phi_n))*dx*dy/N) .* phi_n;
p_n_norm = calc_p_n_norm(p_n);
theta_n_numerator = -real(allsum((2*H_times_state(phi_n)) .* conj(p_n))*dx*dy/N) * p_n_norm;
rho_p_phi = real(phi_n.*conj(p_n));
g_n = 2 * g * rho_p_phi .* phi_n;
theta_n_denominator = 2 * sqrt(N) * (allsum(conj(H_times_state(p_n)) .* (p_n) + real(conj(p_n) .* g_n)) * dx * dy / N - mu_n * p_n_norm^2 / N);
% if the denominator of theta_n is positive
if theta_n_denominator > 0
% choose theta_n as theta_opt_n
theta_n = real(theta_n_numerator/theta_n_denominator);
else
% choose theta_n as a small positive value
theta_n = real(theta_n_numerator);
end
% decrease theta_n until the energy starts to decrease or theta_n is less
% than eps (2.2204e-16)
while true
phi_n_plus_1_temp = cos(theta_n) .* phi_n + sin(theta_n) .* p_n * sqrt(N) / p_n_norm;
E_temp = calc_E(phi_n_plus_1_temp);
if E_temp < E || theta_n < eps
break
else
theta_n = theta_n / 2;
end
end
phi_n_plus_1 = cos(theta_n) .* phi_n + sin(theta_n) .* p_n * sqrt(N) / p_n_norm;
phi_n_plus_1 = normalize(phi_n_plus_1);
end
function [mu_n, r_n, p_n, phi_n_plus_1] = PCG_C(r_n_minus_1, p_n_minus_1)
% finding phi_n_plus_1 by running the PCG_C method once
global phi_n;
global dx;
global dy;
global N;
global g;
global E;
mu_n = calc_mu();
r_n = H_times_state(phi_n) - mu_n * phi_n;
% calculate beta_n_PR
beta_n_PR = real((allsum((r_n - r_n_minus_1) .* conj(calc_P_C_times_state(r_n)))*dx*dy/N)/(allsum((r_n_minus_1) .* conj(calc_P_C_times_state(r_n_minus_1))) * dx * dy / N));
% if beta_n_PR is negative, take beta_n as 0
beta_n = max([beta_n_PR, 0]);
d_n = -calc_P_C_times_state(r_n) + beta_n * p_n_minus_1;
p_n = d_n - real(allsum((d_n) .* conj(phi_n))*dx*dy/N) .* phi_n;
p_n_norm = calc_p_n_norm(p_n);
theta_n_numerator = -real(allsum((2*H_times_state(phi_n)) .* conj(p_n))*dx*dy/N) * p_n_norm;
rho_p_phi = real(phi_n.*conj(p_n));
g_n = 2 * g * rho_p_phi .* phi_n;
theta_n_denominator = 2 * sqrt(N) * (allsum(conj(H_times_state(p_n)) .* (p_n) + real(conj(p_n) .* g_n)) * dx * dy / N - mu_n * p_n_norm^2 / N);
% if the numerator of theta_n is negative
if theta_n_numerator < 0
% run the PG_C method once
[mu_n, r_n, p_n, phi_n_plus_1] = PG_C();
else
% choose theta_n as theta_opt_n
theta_n = real(theta_n_numerator/theta_n_denominator);
% decrease theta_n until the energy starts to decrease or theta_n is
% less than eps (2.2204e-16)
while true
phi_n_plus_1_temp = cos(theta_n) .* phi_n + sin(theta_n) .* p_n * sqrt(N) / p_n_norm;
E_temp = calc_E(phi_n_plus_1_temp);
if E_temp < E || theta_n < eps
break
else
theta_n = theta_n / 2;
end
end
phi_n_plus_1 = cos(theta_n) .* phi_n + sin(theta_n) .* p_n * sqrt(N) / p_n_norm;
phi_n_plus_1 = normalize(phi_n_plus_1);
end
end