-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.m
executable file
·426 lines (342 loc) · 13.4 KB
/
driver.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
addpath('functions/')
% driver
clc, clear all, close all
% Setup
dealias = true
restart = false
xi = 0.5;
eta = 0.5;
Ulid = 1;
beta = sqrt(5.0);
Revec = [1000]%, 100, 400, 1000];
Nvec = [8];
maxstep = 1000000;
steadytol = 1e-6;
statstride = 50;
step0 = 1;
for r = 1:length(Revec)
Re=Revec(r)
for nv = 1:length(Nvec)
Nx = Nvec(nv)
Ny = Nvec(nv);
% Choose start guess
P = zeros(Ny+1,Nx+1); dP = zeros(Ny+1,Nx+1);
U = zeros(Ny+1,Nx+1); dU = zeros(Ny+1,Nx+1);
V = zeros(Ny+1,Nx+1); dV = zeros(Ny+1,Nx+1);
if restart
% load selected variables
load(sprintf('data/Re%i_Nx%i_Ny%i.mat',Re,Nx,Ny),...
'U','V','P','step','steadyhist','statstride')
Umax = max(abs(U(:)));
Vmax = max(abs(V(:)));
step0 = step + 1;
steadyhist = [steadyhist, zeros(2,floor((maxstep-step0+1)/statstride),1)];
else
% smooth bc
U(Ny+1,3:Nx-1) = Ulid; U(Ny+1,2) = Ulid/2; U(Ny+1,Nx) = Ulid/2;
% U(Ny+1,1:Nx+1) = Ulid; [PSF, center] = psfGauss([Ny+1,Nx+1],2);
% U = conv2(U,PSF,'same');
Umax = Ulid;
Vmax = 0;
steadyhist = zeros(2,maxstep/statstride,1);
end
tstart = tic;
% Generate grid and operators
[X, Y, GradX_vel, GradY_vel, Lap_vel, GradX_pres, GradY_pres, Mx, My] ...
= operators(Nx, Ny, xi, eta);
N = (Nx+1)*(Ny+1);
dxmin = min(diff(X(1,:)));
dymin = min(diff(Y(:,1)));
%% DEALIASING
% rescale coarse axis to [-1,1]
x1 = X(1,:)/xi; y1 = Y(:,1)/eta;
% generate fine grid axis
[x2, wx2] = JacobiGQ(0,0,2*Nx);
[y2, wy2] = JacobiGQ(0,0,2*Ny);
[X2,Y2] = meshgrid(x2,y2);
% coarse to fine pol. interpolation matrix (kopriva algorithm)
% 1D
Imatx = PolynomialInterpolationMatrix(x1,x2);
Imaty = PolynomialInterpolationMatrix(y1,y2);
% 2D
ImatXY = kron(Imatx,Imaty);
% allocate
U2 = zeros(2*Ny+1, 2*Nx+1);
V2 = zeros(2*Ny+1, 2*Nx+1);
Uprod2 = zeros(2*Ny+1, 2*Nx+1);
Vprod2 = zeros(2*Ny+1, 2*Nx+1);
Uprodtx2 = zeros(2*Ny+1, 2*Nx+1);
Vprodtx2 = zeros(2*Ny+1, 2*Nx+1);
Uprodtxy2 = zeros(2*Ny+1, 2*Nx+1);
Vprodtxy2 = zeros(2*Ny+1, 2*Nx+1);
Uprodtx1 = zeros(Ny+1, Nx+1);
Vprodtx1 = zeros(Ny+1, Nx+1);
Uprod1 = zeros(Ny+1, Nx+1);
Vprod1 = zeros(Ny+1, Nx+1);
%% Pseudo time stepping
w = [1/4,1/3,1/2,1]; % RK weights
for step = step0:maxstep
% Choose time step (conservatively)
lambda1 = (Umax + sqrt(Umax^2+beta^2))/dxmin + 1/(Re*dxmin^2);
lambda2 = (Vmax + sqrt(Vmax^2+beta^2))/dymin + 1/(Re*dymin^2);
dt = 0.9/(lambda1+lambda2);
Pold = P;
Uold = U;
Vold = V;
for s = 1:4
if dealias
%% Compute dealiased convection (Uprod1, Vprod1)
% interp vel and grad_vel to fine grid and compute product
U2(:) = ImatXY*U(:);
V2(:) = ImatXY*V(:);
Uprod2(:) = - U2(:).*( ImatXY*(GradX_vel*U(:)) ) ...
- V2(:).*( ImatXY*(GradY_vel*U(:)) );
Vprod2(:) = - U2(:).*( ImatXY*(GradX_vel*V(:)) ) ...
- V2(:).*( ImatXY*(GradY_vel*V(:)) );
% discrete polynomial transform
% transforms first direction
for n = 0:2*Nx
nn = n + 1;
Pn = NormJacobiPol(x2,0,0,n); % column
Uprodtx2(:, nn) = Uprod2*(Pn.*wx2);
Vprodtx2(:, nn) = Vprod2*(Pn.*wx2);
end
% transform other direction (tranpose)
for m = 0:2*Ny
mm = m + 1;
Pm = NormJacobiPol(y2,0,0,m); % column
Uprodtxy2(mm, :) = (Uprodtx2'*(Pm.*wy2))';
Vprodtxy2(mm, :) = (Vprodtx2'*(Pm.*wy2))';
end
% projection (truncated polynomial evaluated on coarse grid)
% transforms first direction
Uprodtx1(:,:) = 0; Vprodtx1(:,:) = 0;
for n = 0:Nx
nn = n + 1;
Pn = NormJacobiPol(x1,0,0,n); % column
Uprodtx1 = Uprodtx1 + Uprodtxy2(1:Ny+1, nn)*Pn';
Vprodtx1 = Vprodtx1 + Vprodtxy2(1:Ny+1, nn)*Pn';
end
% transform other direction (tranpose)
Uprod1(:,:) = 0; Vprod1(:,:) = 0;
for m = 0:Ny
mm = m + 1;
Pm = NormJacobiPol(y1,0,0,m); % column
Uprod1 = Uprod1 + (Uprodtx1(mm, 1:Nx+1)'*Pm')';
Vprod1 = Vprod1 + (Vprodtx1(mm, 1:Nx+1)'*Pm')';
end
else
Uprod1 = - U(:).*(GradX_vel*U(:)) - V(:).*(GradY_vel*U(:));
Vprod1 = - U(:).*(GradX_vel*V(:)) - V(:).*(GradY_vel*V(:));
end
%% pseudo time stage derivatives
dP(:) = -(beta^2)*GradX_vel*U(:) ...
-(beta^2)*GradY_vel*V(:);
dU(:) = -GradX_pres*P(:)...
+ (1/Re*Lap_vel)*U(:)...
+ Uprod1(:);
dV(:) = -GradY_pres*P(:)...
+ (1/Re*Lap_vel)*V(:)...
+ Vprod1(:);
% advance pressure
P = Pold + w(s)*dt*dP;
% advance momentum
U(2:Ny,2:Nx) = Uold(2:Ny,2:Nx) + w(s)*dt*dU(2:Ny,2:Nx);
V(2:Ny,2:Nx) = Vold(2:Ny,2:Nx) + w(s)*dt*dV(2:Ny,2:Nx);
end
dU = (U - Uold);
dV = (V - Vold);
changeU = norm(dU(:),2)/norm(Uold(:),2);
changeV = norm(dV(:),2)/norm(Vold(:),2);
Umax = max(abs(U(:)));
Vmax = max(abs(V(:)));
if max(changeU,changeV) < steadytol
break
end
if mod(step, statstride) == 0
step
changeU
changeV
dt;
steadyhist(1,step/statstride) = changeU;
steadyhist(2,step/statstride) = changeV;
end
end
telapsed = toc(tstart);
%% Post processing
% calc vorticity
VORT = zeros(Ny+1,Nx+1);
VORT(:) = GradX_vel*V(:) - GradY_vel*U(:);
DVORTx = zeros(Ny+1,Nx+1);
DVORTy = zeros(Ny+1,Nx+1);
DVORTx(:) = GradX_vel*VORT(:);
DVORTy(:) = GradY_vel*VORT(:);
% Kinetic Energy
E = 0.5*integrate2d(U.^2,Nx,Ny,Mx,My)
% Enstrophy
Z = 0.5*integrate2d(VORT.^2,Nx,Ny,Mx,My)
% Palinenstrophy
Pa = 0.5*integrate2d(DVORTx.^2+DVORTy.^2,Nx,Ny,Mx,My)
% Save entire work space
save(sprintf('data/Dealiased%i_Re%i_Nx%i_Ny%i.mat',dealias,Re,Nx,Ny))
%% plots
close all
fig1 = figure(1); clf
subplot(1,1,1)
surf(X,Y,U)
xlabel('x')
ylabel('y')
view([0 90])
colorbar
axis equal
% % use latex legends and labels, set fontsize/name
% legend({'$y=x$'},...
% 'FontUnits','points',...
% 'interpreter','latex',...
% 'FontSize',18,...
% 'FontName','Computer Modern Roman',...
% 'Location','EO');
% % legend boxoff % if not outside plot?
set(gca,'FontSize',18)
xlabel('$x$',...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Times')
ylabel('$y$',...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Times')
print(fig1,'-depsc',sprintf('fig/Dealiased%i_U_Re%i_Nx%i_Ny%i',dealias,Re,Nx,Ny))
fig2 = figure(2); clf
subplot(1,1,1)
surf(X,Y,V)
xlabel('x')
ylabel('y')
view([0 90])
colorbar
axis equal
% % use latex legends and labels, set fontsize/name
% legend({'$y=x$'},...
% 'FontUnits','points',...
% 'interpreter','latex',...
% 'FontSize',18,...
% 'FontName','Computer Modern Roman',...
% 'Location','EO');
% % legend boxoff % if not outside plot?
set(gca,'FontSize',18)
xlabel('$x$',...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Times')
ylabel('$y$',...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Times')
print(fig2,'-depsc',sprintf('fig/Dealiased%i_V_Re%i_Nx%i_Ny%i',dealias,Re,Nx,Ny))
fig3 = figure(3); clf
subplot(1,1,1)
surf(X(2:Ny,2:Nx),Y(2:Ny,2:Nx),P(2:Ny,2:Nx))
xlabel('x')
ylabel('y')
view([0 90])
colorbar
axis equal
% % use latex legends and labels, set fontsize/name
% legend({'$y=x$'},...
% 'FontUnits','points',...
% 'interpreter','latex',...
% 'FontSize',18,...
% 'FontName','Computer Modern Roman',...
% 'Location','EO');
% % legend boxoff % if not outside plot?
set(gca,'FontSize',18)
xlabel('$x$',...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Times')
ylabel('$y$',...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Times')
print(fig3,'-depsc',sprintf('fig/Dealiased%i_P_Re%i_Nx%i_Ny%i',dealias,Re,Nx,Ny))
fig4 = figure(4); clf
subplot(1,1,1)
surf(X,Y,VORT)
xlabel('x')
ylabel('y')
view([0 90])
colorbar
axis equal
% % use latex legends and labels, set fontsize/name
% legend({'$y=x$'},...
% 'FontUnits','points',...
% 'interpreter','latex',...
% 'FontSize',18,...
% 'FontName','Computer Modern Roman',...
% 'Location','EO');
% % legend boxoff % if not outside plot?
set(gca,'FontSize',18)
xlabel('$x$',...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Times')
ylabel('$y$',...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Times')
print(fig4,'-depsc',sprintf('fig/Dealiased%i_VORT_Re%i_Nx%i_Ny%i',dealias,Re,Nx,Ny))
fig5 = figure(5); clf
loglog((1:length(steadyhist))*statstride, steadyhist)
% use latex legends and labels, set fontsize/name
legend({'$u$', '$v$'},...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Computer Modern Roman',...
'Location','EO');
% legend boxoff % if not outside plot?
set(gca,'FontSize',18)
xlabel('$n$',...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Times')
ylabel('$R$',...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Times')
print(fig5,'-depsc',sprintf('fig/Dealiased%i_Change_Re%i_Nx%i_Ny%i',dealias,Re,Nx,Ny))
fig6 = figure(6); clf
streamslice(X,Y,U,V), axis tight
% use latex legends and labels, set fontsize/name
legend({'$u$', '$v$'},...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Computer Modern Roman',...
'Location','EO');
% legend boxoff % if not outside plot?
set(gca,'FontSize',18)
xlabel('$x$',...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Times')
ylabel('$y$',...
'FontUnits','points',...
'interpreter','latex',...
'FontSize',18,...
'FontName','Times')
print(fig6,'-depsc',sprintf('fig/Dealiased%i_STREAMSLICE_Re%i_Nx%i_Ny%i',dealias,Re,Nx,Ny))
%%
end
end