-
Notifications
You must be signed in to change notification settings - Fork 1
/
OpticalVortices.m
221 lines (183 loc) · 6.2 KB
/
OpticalVortices.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
clearvars, clearvars –global
clear all, clear global
% parallel computign setup
delete(gcp('nocreate'))
parpool('local')
UseParallel = true;
% ========== variables ===========
global dim R m P_0 basis;
% copy and paste this box to "plottingpad.m"
% ========= copy to plot ========
filename = 'eigenrmp9.mat';
dim = 20;
R_range = [20];
m_range = [1 2 3 4 5];
P_0_range = logspace(-1, 4, 50);
% %===============================
basis = cell(dim, 3);
nr = length(R_range);
nm = length(m_range);
np = length(P_0_range);
%{
eigen values stored in a rank 4 tensor with
axis 1: index for R
axis 2: index for m
axis 3: index for P_0
axis 4: value and corresponding error and solution vector.
with 1 being eigenvalue, and
2 being its corresponding error, and
3 to 2 + dim being the solution to variational vector
%}
eigen_mat = NaN(nr, nm, np, 2 + dim);
%{
cache is used to set initial point in fmincon
solution of variational vector of previous iteration
is set to to be initial point. set to (x01,0,0,...) if
r is changed
%}
cache = zeros(1, dim);
x01 = 1;
% index used for for-loop. Can be used to pick up half finished code
m_ind = 1;
p_ind = 1;
r_ind = 1;
for R = R_range(r_ind:end)
% ======= creating basis ===========
for n = 1:1:dim
basis{n, 1} = chebfun(@(r) sin(r * n * pi ./ R ), [0, R]);
gram_schmidt(n);
basis{n, 2} = diff(basis{n, 1});
basis{n, 3} = diff(basis{n, 2});
end
cache(1) = x01; % initial value
for m = m_range(m_ind:end)
for P_0 = P_0_range(p_ind:end)
t = cputime;
disp(['dim: ',num2str(dim), ...
' R: ',num2str(R), ...
' m: ',num2str(m), ...
' P_0: ',num2str(P_0) ...
])
% ============ problem setup ================
c = optimvar('c', dim);
obj = fcn2optimexpr(@I, c);
prob = optimproblem('Objective', obj);
cons1 = sum(c .* c) == P_0; % beam power constraints
prob.Constraints.cons1 = cons1;
options = optimoptions(@fmincon,...
'Algorithm','active-set',...
'MaxFunEvals', 1e+05, ...
'MaxIter', 1000,...
'TolCon',1e-8,...
'TolFun',1e-8);
x0.c = cache;
% ============== optimization ==============='
try
[sol,fval,exitflag,output] = solve(prob, x0 ,'options', options);
% display success/fail
disp(exitflag > 0)
if exitflag > 0 % if successful optimization
% plotting
u = lin_comb(sol.c);
cache = sol.c;
% compute eigenvalue
eigen = compute_eigen(sol.c, P_0);
eigen_mat(r_ind, m_ind, p_ind, 1) = eigen;
disp(['Yields a good eigenvalue:', num2str(eigen)])
% compute error
error = compute_error(sol.c, eigen);
eigen_mat(r_ind, m_ind, p_ind, 2) = error;
disp(['error:', num2str(error)])
eigen_mat(r_ind, m_ind, p_ind, 3:2+dim) = sol.c;
save(filename, 'eigen_mat')
end
catch
disp('some error')
end
disp(['Time it took:', num2str(cputime-t)])
p_ind = p_ind + 1;
end
p_ind = 1;
m_ind = m_ind + 1;
cache = zeros(1, dim);
cache(1) = x01;
end
r_ind = r_ind + 1;
p_ind = 1;
m_ind = 1;
end
save(filename, 'eigen_mat')
% ================== functions ===================
% Linear Combination of variational vector and basis (global)
function u = lin_comb(vec)
global R dim basis;
u = chebfun(0, [0 R]);
for n = 1:dim
u = u + vec(n) * basis{n, 1};
end
end
function u_r = lin_comb_r(vec)
global R dim basis;
u_r = chebfun(0, [0 R]);
for n = 1:dim
u_r = u_r + vec(n) * basis{n, 2};
end
end
function u_rr = lin_comb_rr(vec)
global R dim basis;
u_rr = chebfun(0, [0 R]);
for n = 1:dim
u_rr = u_rr + vec(n) * basis{n, 3};
end
end
% inner product defined as: 2 pi * int_0^R r*f(r)*g(r) dr
function prod = inner_product(func1, func2)
global R;
prod = func1 * func2 * chebfun(@(r) r, [0, R]);
prod = 2 * pi * sum(prod);
end
% gram_schmidt process on n th basis
function gram_schmidt(n)
global basis;
basis{n, 1} = basis{n, 1} / sqrt(inner_product(basis{n, 1}, basis{n, 1}));
for i = 1 : n-1
basis{n, 1} = basis{n, 1} - projection(basis{i, 1}, basis{n, 1});
end
basis{n, 1} = basis{n, 1} / sqrt(inner_product(basis{n, 1}, basis{n, 1}));
function proj = projection(fun_i, fun_n)
proj = inner_product(fun_i,fun_n) * basis{i, 1} / inner_product(fun_i,fun_i);
end
end
% I: action functional
function integral = I(vec)
global R m;
u = lin_comb(vec);
u_r = lin_comb_r(vec);
r = chebfun(@(r) r, [0, R]);
integrand = r * u_r^2;
integrand = integrand + m^2 * u^2 / r;
integrand = integrand - u * u_r * r^2 / (1 + u^2);
integral = sum(integrand) / 2;
end
function eigen = compute_eigen(vec, p_0)
global R m;
u = lin_comb(vec);
u_r = lin_comb_r(vec);
r = chebfun(@(r) r, [0, R]);
ut = r * u_r^2;
ut = ut + m^2 * u^2 / r;
ut = ut + r*u^2 / (1 + u^2);
eigen = -2 * pi * sum(ut)/ p_0;
end
% compute error intergrating (1)
function error = compute_error(vec, b)
global R m;
u = lin_comb(vec);
u_r = lin_comb_r(vec);
r = chebfun(@(r) r, [0, R]);
ut = b * r * u;
ut = ut - u_r - r * lin_comb_rr(vec);
ut = ut + m^2 * u / r ;
ut = ut + r * u / (1 + u^2);
error = sum(ut^2);
end