-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_exact_solution_muGaussNewton_E0_mov.m
346 lines (297 loc) · 9.78 KB
/
main_exact_solution_muGaussNewton_E0_mov.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Charge transport by discrete breathers in 1D model
% written by Janis Bajars, February 2023
% Damped Gauss-Newton algorithm to obtain exact moving polarobreather
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all
clearvars
clc
set(0,'DefaultAxesFontSize',16)
set(groot,'DefaultAxesTickLabelInterpreter','latex');
set(groot,'DefaultTextInterpreter','latex');
set(groot,'DefaultLegendInterpreter','latex');
disp('Start of the computation!')
tic;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Add path
addpath('Functions')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load approximate moving polarobreather simulation data
load('SavedData/Approx_SimData_Mov_G06_E0shift_N64Tau001.mat',...
'parm','U','P','A','B')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Redefine some parameter values in structure <parm>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Number of time steps
parm.Nsteps = 979;
% Final simulation time
parm.Tend = parm.h*parm.Nsteps;
% Time grid points
parm.t = 0:parm.h:parm.Tend;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define simulation variables in structure <vars>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Define_Variables;
% Pick an initial seed (solution)
Nn = 36; % gamma=0.6
vars.u = U(:,Nn);
vars.a = A(:,Nn);
vars.p = P(:,Nn);
vars.b = B(:,Nn);
% Move the localized solution to the center of the lattice
L = floor(parm.N/2);
[~, idx] = max(abs(vars.u));
L = L - idx;
if L<0
L = parm.N - L;
else
L = -L;
end
shift_L = mod(parm.x+L,parm.N)+1;
vars.u = vars.u(shift_L);
vars.a = vars.a(shift_L);
vars.p = vars.p(shift_L);
vars.b = vars.b(shift_L);
vars.b(end) = 0;
% Make sure that the charge constraint is satisfied
C2 = sum(vars.a.^2 + vars.b.^2);
vars.a = vars.a/sqrt(C2)*parm.tau_f;
vars.b = vars.b/sqrt(C2)*parm.tau_f;
% Define step s and shift vector
parm.s = 3;
parm.shift_s = mod(parm.x+parm.s,parm.N)+1;
% Save initial values
u_n = vars.u;
a_n = vars.a;
p_n = vars.p;
b_n = vars.b;
% Initial E0 value
E0 = parm.E0;
format long
disp(E0)
format short
% Max number of iterations
Iter_max = 100;
% Save iteration and error values in structure <parm>
parm.iter_max = Iter_max;
parm.iter = zeros(Iter_max,1);
parm.error_p = zeros(Iter_max,1);
parm.error_b = zeros(Iter_max,1);
parm.error_C2 = zeros(Iter_max,1);
parm.error_f = zeros(Iter_max,1);
parm.values_E0 = zeros(Iter_max,1);
parm.mu = zeros(Iter_max,1);
% Errors
Error_f = Inf;
Error_C2 = Inf;
% Tolerance
Tol = 1e-14;
% Numerical Jacobian computation
delta = 1e-6;
df = zeros(4*parm.N,4*parm.N+1);
% Number of constraints
parm.M = 2;
% Jacobian matrix for the total charge probability and b_N=0
dg = zeros(parm.M,4*parm.N+1);
dg(end,end-1) = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Time integration to find f and the objective function F_old
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for n=1:parm.Nsteps
vars = Method_Im_PQDWimDQP(parm,vars,parm.h);
end
% Shift values back s step
U_n = vars.u(parm.shift_s);
A_n = vars.a(parm.shift_s);
P_n = vars.p(parm.shift_s);
B_n = vars.b(parm.shift_s);
% Compute function value f
f = [U_n-u_n; A_n-a_n; P_n-p_n; B_n-b_n];
% Objective function
F_old = (f'*f)/2;
iter = 1;
while Error_f > Tol || Error_C2 > Tol
disp(iter)
% % % % % % Optional: plot solutions at different iterations
% % % % % figure(iter)
% % % % % set(gcf, 'Position', [600, 150, 1200, 800])
% % % % %
% % % % % subplot(221)
% % % % % hold on
% % % % % plot(parm.x,u_n,'-k','linewidth',1.5)
% % % % % plot(parm.x,U_n,'--r','linewidth',1.5)
% % % % % axis on
% % % % % box on
% % % % % grid on
% % % % % xlabel('$n$')
% % % % % ylabel('$u_n$')
% % % % % axis([0 parm.N-1 -0.15 0.1501])
% % % % %
% % % % % subplot(222)
% % % % % hold on
% % % % % plot(parm.x,a_n,'-k','linewidth',1.5)
% % % % % plot(parm.x,A_n,'--r','linewidth',1.5)
% % % % % plot(parm.x,b_n,'-b','linewidth',1.5)
% % % % % plot(parm.x,B_n,'--c','linewidth',1.5)
% % % % % axis on
% % % % % box on
% % % % % grid on
% % % % % xlabel('$n$')
% % % % % ylabel('$a_n$, $b_n$')
% % % % % axis([0 parm.N-1 -0.04 0.04])
% % % % %
% % % % % subplot(223)
% % % % % hold on
% % % % % plot(parm.x,p_n,'-k','linewidth',1.5)
% % % % % plot(parm.x,P_n,'--r','linewidth',1.5)
% % % % % axis on
% % % % % box on
% % % % % grid on
% % % % % xlabel('$n$')
% % % % % ylabel('$p_n$')
% % % % % axis([0 parm.N-1 -0.15 0.1501])
% % % % %
% % % % % subplot(224)
% % % % % hold on
% % % % % plot(parm.x,a_n.^2+b_n.^2,'-k','linewidth',1.5)
% % % % % plot(parm.x,A_n.^2+B_n.^2,'--r','linewidth',1.5)
% % % % % axis on
% % % % % box on
% % % % % grid on
% % % % % xlabel('$n$')
% % % % % ylabel('$|c_n|^2$')
% % % % % axis([0 parm.N-1 -0.0001 0.001])
% % % % % drawnow
% Numerical Jacobian computation
X = [u_n; a_n; p_n; b_n; E0];
for j=1:4*parm.N+1
% Modify one of the entries of X
XX = X;
temp = X(j);
h = delta*abs(temp);
if h < 1e-8
h = delta;
end
XX(j) = temp+h;
h = XX(j)-temp;
% Initial conditions for time integration
vars.u = XX(1:parm.N);
vars.a = XX(parm.N+1:2*parm.N);
vars.p = XX(2*parm.N+1:3*parm.N);
vars.b = XX(3*parm.N+1:4*parm.N);
parm.E0 = XX(end);
parm.E0_tau = parm.E0/parm.tau;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Time integration with new u, p, a, b, and E0 values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for n=1:parm.Nsteps
vars = Method_Im_PQDWimDQP(parm,vars,parm.h);
end
% Shift values back s steps
vars.u = vars.u(parm.shift_s);
vars.a = vars.a(parm.shift_s);
vars.p = vars.p(parm.shift_s);
vars.b = vars.b(parm.shift_s);
% Compute function value g
g = [vars.u-XX(1:parm.N);...
vars.a-XX(parm.N+1:2*parm.N);...
vars.p-XX(2*parm.N+1:3*parm.N);...
vars.b-XX(3*parm.N+1:4*parm.N)];
% Numerical Jacobian entry
df(:,j) = (g-f)/h;
end
% Compute constraint Jacobian matrix dg
dg(parm.M-1,parm.N+1:2*parm.N) = a_n';
dg(parm.M-1,3*parm.N+1:4*parm.N) = b_n';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Damped Gauss-Newton algorithm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Regularization parameter mu
if iter==1
mu = 1e-6*max(diag(df'*df));
end
% Vector G = df'*f
G = df'*f;
% System matrix M
M = [df'*df+mu*eye(4*parm.N+1,4*parm.N+1) dg'; dg zeros(parm.M,parm.M)];
% Right-hand side
Y = -[G; (a_n'*a_n + b_n'*b_n)/2-parm.tau; b_n(end)];
% Solve the linear system of equations
dX = M\Y;
dX = dX(1:4*parm.N+1);
% Update variables
X = X + dX;
% Initial conditions for the next iteration and time integration
vars.u = X(1:parm.N);
vars.a = X(parm.N+1:2*parm.N);
vars.p = X(2*parm.N+1:3*parm.N);
vars.b = X(3*parm.N+1:4*parm.N);
parm.E0 = X(end);
parm.E0_tau = parm.E0/parm.tau;
% Save initial values
u_n = vars.u;
a_n = vars.a;
p_n = vars.p;
b_n = vars.b;
% Value of E0
format long
E0 = parm.E0;
disp(E0)
format short
parm.values_E0(iter,1) = E0;
disp('_______________________________________________________________')
% Errors
Error_p = max(abs(vars.p));
parm.error_p(iter,1) = Error_p;
disp(Error_p)
Error_b = abs(vars.b(end));
parm.error_b(iter,1) = Error_b;
disp(Error_b)
Error_C2 = abs(sum(vars.a.^2 + vars.b.^2)/2/parm.tau-1);
parm.error_C2(iter,1) = Error_C2;
disp(Error_C2)
Error_f = max(abs(f));
parm.error_f(iter,1) = Error_f;
disp(Error_f)
parm.mu(iter,1) = mu;
disp(mu)
disp('---------------------------------------------------------------')
parm.iter(iter,1) = iter;
iter = iter + 1;
if iter > Iter_max
disp('Reached the maximum number of iterations!')
break
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Time integration to find f and the objective function F_new
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for n=1:parm.Nsteps
vars = Method_Im_PQDWimDQP(parm,vars,parm.h);
end
% Shift values back s step
U_n = vars.u(parm.shift_s);
A_n = vars.a(parm.shift_s);
P_n = vars.p(parm.shift_s);
B_n = vars.b(parm.shift_s);
% Compute function value f
f = [U_n-u_n; A_n-a_n; P_n-p_n; B_n-b_n];
% Objective function
F_new = (f'*f)/2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute the gain ratio rho and update the mu value
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rho = 2*(F_old-F_new)/(dX'*(mu*dX-G));
F_old = F_new;
if rho < 0.25
mu = 2*mu;
elseif rho > 0.75
mu = mu/3;
end
end
% Save simulation data
save('SavedData/ExactSol_Mov_G06_E0shift_N64Tau001.mat','parm','vars')
toc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Remove path
rmpath('Functions')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%