-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffusionSpectralLineExact2.m
More file actions
267 lines (190 loc) · 7.28 KB
/
DiffusionSpectralLineExact2.m
File metadata and controls
267 lines (190 loc) · 7.28 KB
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
function E = DiffusionSpectralLineExact2()
%Runs an adaptive version of a diffusion equation with exact BCs.
close all;
N = 50;
nn = 1000;
xx = linspace(-1,1,nn)';
mult = 0.85;
initMap = @tanmap;
initArgs = [20 0 -1 1];
tol = 10^-0.5;
c1 = 1-tol;
c2 = 1+tol;
args = initArgs;
ChebPoints = chebx(N);
% [y,Int] = ChebyshevPoints(N-1);
x = initMap(ChebPoints, args);
D0 = 1; % diffusion coefficient
t0 = 0.0001; % initial time
initT = t0;
rho_ic = ExactSolution(x,t0);
% plot(x, rho_ic, '.');
% figure();
y0 = rho_ic;
argnew = argfinderBetaFixed(x, y0, N, nn, args, 1/t0);
args = argnew;
disp(['Found starting args = ', num2str(args)]);
x = initMap(ChebPoints, args);
Dy = diffm(N, initMap, args);
rho_ic = ExactSolution(x, t0);
y0 = rho_ic;
initNorm = sobnorm(y0, initMap, args);
disp(['Init norm = ', num2str(initNorm)]);
tMax = 1;
nTimes = 80;
mM = ones(size(x));
% mM(1) = 0; % make the equation a DAE with algebraic ones at 1 and N
mM([1,N+1]) = 0; % make the equation a DAE with algebraic ones at 1 and N
opts = odeset('RelTol',10^-9,'AbsTol',10^-9,'Mass',diag(mM), 'event', @event);
% opts = odeset('RelTol',10^-9,'AbsTol',10^-9,'Mass',diag(mM));
%ADAPTIVE GRID--------------------------------------
Rho_out = [];
Times_out = [];
teout = [];
yeout = [];
ieout = [];
xout=[];
count= 0;
timestep = (tMax-t0)/nTimes;
outTimes = t0:timestep:tMax;
tic
while t0 < tMax-timestep
count = count+1;
[t,Rho_t, te, ye, ie] = ode15s(@rhs,outTimes,y0,opts);
nt = length(t);
% disp(['Nt = ', num2str(nt)]);
Rho_out = [Rho_out;Rho_t(1:nt-1,:)];
Times_out = [Times_out;t(1:nt-1)];
teout = [teout;te];
yeout = [yeout;ye];
ieout = [ieout; ie];
X = repmat(x', nt-1, 1);
xout = [xout;X]; %Store the grids used for each timestep, for plotting purposes.
% disp(X);
y1 = Rho_t(nt,:)';
% disp(['Y = ']); disp([num2str(y1)]);
args(1) = mult*args(1);
% argnew = argfinderBetaFixed(x, y1, N, nn, 1, args,1/t0);
% % argnew = argfinderBetaFixed(x, y1, N, nn, 1, args);
% args = argnew;
disp(['Break ', num2str(count), ', Time reached = ', num2str(t(nt)), ', New args found = ', num2str(args)]);
args(1) = abs(args(1));
xnew = tanmap(ChebPoints, args);
InterpM = baryM(x, xnew);
ynew = InterpM*y1;
initNorm = sobnorm(ynew, @tanmap, args);
x = xnew;
y0 = ynew;
Dy = diffm(N, @tanmap, args);
t0 = t(nt);
outTimes = t0:timestep:tMax;
end
toc
%FIXED GRID---------------------------------
% disp(size(Times_out));
Nfixed = N;
Dy2 = diffm(Nfixed, @linmap, [-1,1]);
x2 = chebx(Nfixed);
rho_ic2 = ExactSolution(x2, initT);
mM = ones(size(x2));
mM([1,Nfixed+1]) = 0; % make the equation a DAE with algebraic ones at 1 and N
opts = odeset('RelTol',10^-9,'AbsTol',10^-9,'Mass',diag(mM));
tic
[t2, Rho_t2] = ode15s(@rhs2, Times_out, rho_ic2, opts);
toc
% disp([size(Times_out);size(t2)])
%%%PLOTTING--------------------------------
errors1=[];
errors2=[];
errors1i = [];
errors2i = [];
figure('Position', [50, 200, 600, 500])
IntFix = baryM(x2, xx);
for iTimes = 1:length(t2)
Time = Times_out(iTimes);
xT = xout(iTimes, :)';
rhot = Rho_out(iTimes,:)';
rhot2 = Rho_t2(iTimes,:)';
%rhoE = ExactSolution(xT,outTimesorig(iTimes));
IntM = baryM(xT, xx);
rhoE = ExactSolution(xT,Time);
rhoE2 = ExactSolution(x2, Time);
yy = ExactSolution(xx, Time);
hold off
plot(xT,rhot,'b.');
hold on
plot(xx, IntM*rhot, 'b');
plot(x2, rhot2, 'r.');
plot(xx, IntFix*rhot2, 'r');
plot(xx,yy,'--k');
% err(iTimes) = norm((abs(rhot - rhoE)) / (abs(rhoE)),1);
legend('Adaptive Solution', 'Adaptive Interpolation', 'Fixed Solution', 'Fixed Interpolation', 'Exact Solution');
e1 = norm(rhot - rhoE, 1);
errors1 = [errors1;e1];
e2 = norm(rhot2 - rhoE2, 1);
errors2 = [errors2;e2];
e1i = norm(IntM*rhot - yy, 1);
errors1i = [errors1i;e1i];
e2i = norm(IntFix*rhot2 - yy,1);
errors2i = [errors2i;e2i];
ylim([-0.2,1.1]);
xlim([-1,1]);
grid on;
xlabel('x');
ylabel('u(x)');
title('Solution of Diffusion Equation');
text(-0.8, 0.3, ['Time = ', num2str(Time), 's']);
pause(initT/Time)
end
figure('Position', [800 200 600 500]);
loglog(t2, errors1, 'b');
hold on
loglog(t2, errors2, 'r');
loglog(t2, errors1i, 'b--');
loglog(t2, errors2i, 'r--');
grid on
legend('Adaptive Grid', 'Fixed Grid', 'Adaptive Interpolation', 'Fixed Interpolation');
xlabel('t');
ylabel('Error');
title('Error in diffusion equation')
% disp(Times_out);
E = log10(max(errors1i));
%----------------------------------------------------------------------
function dydt = rhs(t,rho)
flux = getFlux(rho); % -d_x \rho
dydt = - Dy*flux; % d_xx \rho
% Neumann problem
dydt(N+1) = rho(N+1) - ExactSolution(x(N+1),t); % match to exact boundary condition
dydt(1) = rho(1) - ExactSolution(x(1),t); % match to exact boundary condition
% dydt(N+1) = rho(N+1) - ExactSolution(x(N+1),t-delay); % match to exact boundary condition
% dydt(1) = rho(1) - ExactSolution(x(1),t-delay); % match to exact boundary condition
dydt = dydt(:);
end
function f = getFlux(rho)
f = - D0*Dy*rho;
end
function dydt = rhs2(t, rho)
flux = getFlux2(rho);
dydt = -Dy2*flux;
% Neumann problem
dydt(Nfixed+1) = rho(Nfixed+1) - ExactSolution(x2(Nfixed+1), t); % make d_x \rho = 0 on right endpoint
dydt(1) = rho(1) - ExactSolution(x2(1), t); % make d_x \rho = 0 on left endpoint
% Dirichlet problem
% dydt(N) = rho(N) - 0; % make \rho = 0 on right endpoint
% dydt(1) = rho(1) - 0; % make \rho = 0 on left endpoint
end
function f = getFlux2(rho)
f = -D0*Dy2*rho;
end
function [value, isterminal, direction] = event(~,y)
Norm = sobnorm(y, @tanmap, args);
ratio = Norm/initNorm;
value = (ratio>c1) & (ratio<c2);
isterminal = 1;
direction = 0;
end
function rho = ExactSolution(y,t)
% exact solution to diffusion equation on unbounded domain
rho = sqrt(initT/t)*exp(-y.^2/(4*D0*t));
end
end