/
channel_ibm.m
349 lines (303 loc) · 9.09 KB
/
channel_ibm.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
% McDermott
% 8-26-2021
% channel_ibm.m
%
% Simple staggered primitive variable treatment channel flow
% using a simple FE time integrator.
close all
clear all
Lx = 2; % channel dimension in x
Ly = 1; % channel dimension in y
nx = 32; % number of pcells in x
ny = 16; % number of pcells in y
dx = Lx/nx; % uniform grid spacing in x
dy = Ly/ny; % uniform grid spacing in y
dxdx = dx^2;
dydy = dy^2;
U_INLET = 1; % inlet velocity
nu = 1e-3; % kinematic viscosity (1/Re)
Fo = 0.5; % Fourier number
CFL = 0.5; % Courant number
T = 10; % total simulation time
% For the CC_IBM test, let's blank out cells on the top and bottom
% of the channel.
dy_cc = floor(0.2/dy)*dy;
ncc = floor(dy_cc/dy);
% STAGGERED GRID ARRANGEMENT:
%
% Let this represent the bottom left pcell control volume.
% Velocities are stored on their respective faces. Normal stresses
% and normal advective fluxes are stored at the pcell center.
% Off-diagonal stresses and advective fluxes are stored at vertices
% marked by the Xs. The bottom left most element is prescribed the
% indices (1,1). Because of this, some of the differencing
% below looks confusing, but this seems somewhat unavoidable.
%
% omega3(1,2) omega3(2,2)
% tau12(1,2) ^ v(1,2) tau12(2,2)
% X-------------------|--------------------X
% | |
% | |
% | |
% | |
% | |
% | |
% | |
% | |
% ---> u(1,1) O ---> u(2,1)
% | H(1,1) |
% | tau11(1,1) |
% | tau22(1,1) |
% | |
% | |
% | |
% | |
% | ^ v(1,1) |
% X-------------------|--------------------X
% omega3(1,1) omega3(2,1)
% tau12(1,1) tau12(2,1)
%
% initialization
u = zeros(nx+1,ny);
v = zeros(nx,ny+1);
u_hat = zeros(nx+1,ny);
v_hat = zeros(nx,ny+1);
up = zeros(nx,ny);
vp = zeros(nx,ny);
kres = zeros(nx,ny);
tau11 = zeros(nx,ny);
tau22 = zeros(nx,ny);
tau12 = zeros(nx+1,ny+1);
omega3 = zeros(nx+1,ny+1);
H = zeros(nx,ny);
b_vec = zeros(nx*ny,1);
HNXP1 = zeros(1,ny);
% cell-centered grid
x = linspace(dx/2,Lx-dx/2,nx);
y = linspace(dy/2,Ly-dy/2,ny);
for j = 1:ny
X(:,j) = x;
end
for i = 1:nx
Y(i,:) = y;
end
celltype = ones(nx+1,ny+1); % pad this array for later use with pcolor output
facextype = ones(nx+1,ny);
faceytype = ones(nx,ny+1);
if ncc>0
celltype(:,1:ncc) = 0;
celltype(:,(ny-ncc+1):ny+1) = 0;
end
for i=1:nx
for j=1:ny
if celltype(i,j)==0
facextype(i,j) = 0; facextype(i+1,j) = 0;
faceytype(i,j) = 0; faceytype(i,j+1) = 0;
end
end
end
% set inlet bc
U0 = U_INLET*ones(1,ny);
for j=1:ny
if facextype(1,j)==0
U0(j) = 0;
end
end
% node-centered grid
xf = linspace(0,Lx,nx+1);
yf = linspace(0,Ly,ny+1);
for j = 1:ny+1
XF(:,j) = xf;
end
for i = 1:nx+1
YF(i,:) = yf;
end
% Build A
A = build_sparse_matrix_mixed([nx ny],[dx dy],[0 2 0 0]);
% Main time step loop
t = 0;
while t<T
dt_dif = Fo/(nu*(1/dxdx+1/dydy)) % time step based on Fourier number
velmax = max([max(abs(U0)),max(max(abs(u))),max(max(abs(v)))])
dt_adv = CFL*min(dx,dy)/velmax % time step based on CFL
dt = min([dt_dif,dt_adv]) % time step used in Forward Euler integrator
t = t+dt
% compute normal stresses
for j = 1:ny
for i = 1:nx
tau11(i,j) = -2*nu*( u(i+1,j)-u(i,j) )/dx;
tau22(i,j) = -2*nu*( v(i,j+1)-v(i,j) )/dy;
kres(i,j) = 0.5*(up(i,j)^2 + vp(i,j)^2);
end
end
% compute interior vorticity and off-diagonal stresses
for j = 2:ny
for i = 2:nx
omega3(i,j) = (v(i,j)-v(i-1,j))/dx - (u(i,j)-u(i,j-1))/dy;
tau12(i,j) = -nu*( (u(i,j)-u(i,j-1))/dy + (v(i,j)-v(i-1,j))/dx );
end
end
% left wall stress and vorticity
i = 1;
for j = 2:ny
dvdx_wall = 0;
tau12(i,j) = -nu*dvdx_wall;
omega3(i,j) = dvdx_wall;
end
% right wall stress and vorticity
i = nx+1;
for j = 2:ny
dvdx_wall = 0;
tau12(i,j) = -nu*dvdx_wall;
omega3(i,j) = dvdx_wall;
end
% bottom wall stress and vorticity
j = 1;
for i = 1:nx+1
dudy_wall = 2*u(i,j)/dy;
tau12(i,j) = -nu*dudy_wall;
omega3(i,j) = -dudy_wall;
end
% top wall stress and vorticity
j = ny+1;
for i = 1:nx
dudy_wall = -2*u(i,j-1)/dy;
tau12(i,j) = -nu*dudy_wall;
omega3(i,j) = -dudy_wall;
end
% u momentum -- predictor
for j = 1:ny
for i = 2:nx
Fu_visc = -( (tau11(i,j)-tau11(i-1,j))/dx + (tau12(i,j+1)-tau12(i,j))/dy );
vn = 0.5*( v(i-1,j+1) + v(i,j+1) );
vs = 0.5*( v(i-1,j) + v(i,j) );
Fx = -0.5*( vs*omega3(i,j) + vn*omega3(i,j+1) );
% immersed boundary forcing
if facextype(i,j)==0
Fu_visc = 0;
Fx = (0 - u(i,j))/dt - ( H(i,j)-H(i-1,j) )/dx;
end
u_hat(i,j) = u(i,j) + dt*(Fx + Fu_visc);
end
% inlet boundary forcing
Fx = (U0(j) - u(1,j))/dt;
u_hat(1,j) = u(1,j) + dt*Fx;
% outlet boundary forcing
if facextype(i,j)==0
Fu_visc = 0;
Fx = (0 - u(nx+1,j))/dt - ( HNXP1(j)-H(nx,j) )/dx;
else
Fu_visc = 0;
vn = v(nx,j+1);
vs = v(nx,j);
Fx = -0.5*( vs*omega3(nx,j) + vn*omega3(nx,j+1) );
end
u_hat(nx+1,j) = u(nx+1,j) + dt*(Fx + Fu_visc);
end
% v momentum -- predictor
for j = 2:ny
for i = 1:nx
Fv_visc = -( (tau12(i+1,j)-tau12(i,j))/dx + (tau22(i,j)-tau22(i,j-1))/dy );
ue = 0.5*( u(i+1,j-1) + u(i+1,j) );
uw = 0.5*( u(i,j-1) + u(i,j) );
Fy = 0.5*( uw*omega3(i,j) + ue*omega3(i+1,j) );
% immersed boundary forcing
if faceytype(i,j)==0
Fv_visc = 0;
Fy = (0 - v(i,j))/dt - ( H(i,j)-H(i,j-1) )/dy;
end
v_hat(i,j) = v(i,j) + dt*(Fy + Fv_visc);
end
end
% build source (Poisson right-hand-side)
for j = 1:ny
for i = 1:nx
b(i,j) = ( ( u_hat(i+1,j)-u_hat(i,j) )/dx + ( v_hat(i,j+1)-v_hat(i,j) )/dy ) / dt;
end
% apply Dirichlet bcs to outflow
if u(nx+1,j)>0 & facextype(nx+1,j)>0
b(nx,j) = b(nx,j) - 2*kres(nx,j)/dxdx;
end
end
% map b to source vector
for j = 1:ny
for i = 1:nx
p = (j-1)*nx+i;
b_vec(p) = b(i,j);
end
end
% % subtract mean for discrete compatibility condition
% b_vec = b_vec - mean(b_vec);
% solve for H vector
H_vec = A\b_vec;
% % subtract mean for arbitrary solution
% H_vec = H_vec - mean(H_vec);
H_min = min(H_vec);
H_max = max(H_vec);
% map H_vec to grid
for j = 1:ny
for i = 1:nx
p = (j-1)*nx+i;
H(i,j) = H_vec(p);
end
end
% project velocities
for j = 1:ny
for i = 2:nx
u(i,j) = u_hat(i,j) - dt*( H(i,j)-H(i-1,j) )/dx;
end
% apply inflow bc
u(1,j) = u_hat(1,j);
% apply outflow bc
if u(nx+1,j)>0 & facextype(nx+1,j)>0
HNXP1(j) = 2*kres(nx,j) - H(nx,j);
else
HNXP1(j) = -H(nx,j);
end
u(nx+1,j) = u_hat(nx+1,j) - dt*( HNXP1(j)-H(nx,j) )/dx;
end
for j = 2:ny
for i = 1:nx
v(i,j) = v_hat(i,j) - dt*( H(i,j)-H(i,j-1) )/dy;
end
end
% check divergence
for j = 1:ny
for i = 1:nx
b(i,j) = ( u(i+1,j)-u(i,j) )/dx + ( v(i,j+1)-v(i,j) )/dy;
end
end
% u
% b
% return
display(['max stage 1 velocity divergence = ',num2str( max(max(abs(b))) )])
% interpolate velocities to cell centers
for j = 1:ny
for i = 1:nx
up(i,j) = 0.5*( u(i+1,j)+u(i,j) );
vp(i,j) = 0.5*( v(i,j+1)+v(i,j) );
end
end
subplot(3,1,1)
pcolor(XF,YF,celltype)
colorbar
title('Cutcell Mesh')
axis([0 Lx 0 Ly])
set(gca,'PlotBoxAspectRatio',[Lx Ly 1])
subplot(3,1,2)
velmag = sqrt( up.*up + vp.*vp );
V = linspace(0,1,20);
contourf(X,Y,velmag,V); hold on
colorbar
quiver(X,Y,up,vp); hold off
title('velocity vectors')
axis([0 Lx 0 Ly])
set(gca,'PlotBoxAspectRatio',[Lx Ly 1])
subplot(3,1,3)
V = linspace(H_min,H_max,20);
contourf(X,Y,H,V)
title('pressure contours')
colorbar
set(gca,'PlotBoxAspectRatio',[Lx Ly 1])
pause(0.001)
end