/
PoiseuilleDeveloping_solution.m
248 lines (192 loc) · 6.22 KB
/
PoiseuilleDeveloping_solution.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
clear all
clc
tic
% Developing Poiseuille flow
% Lattice parameters
weights=[4/9 1/9 1/9 1/9 1/9 1/36 1/36 1/36 1/36];
cx=[0 1 0 -1 0 1 -1 -1 1];
cy=[0 0 1 0 -1 1 1 -1 -1];
% Numerical parameters
NX=22; % Number of grids points along x
NY=22; % Number of grid points along y
NPOP=9; % Number of populations used in velocity space discretization
NSTEPS=2000; % Number of time steps/iterations
% Simulation parameters
y_bottom=1; % location of bottom wall
y_top=NY; % location of top wall
Re=10; % Reynolds number
omega=0.9; % Relaxation frequency
kvisc=1/3*(1/omega-0.5); % Kinematic viscosity
umax=Re*kvisc/((y_top-y_bottom)) ;% umax=0.001; % Mach number (can be understood as a CFL number)
% Macroscopic parameters
rho=ones(NX,NY);
ux=zeros(NX,NY);
uy=zeros(NX,NY);
forcex=8.*umax*kvisc./((y_top-y_bottom).^2);
forcey=0;
% Analytical solution
y_plot=y_bottom:y_top;
ux_analy=-1/(2*kvisc).*forcex.*(y_plot-y_bottom).*(y_plot-y_top);
ux_in=1/12*(y_bottom^2-2*y_bottom*y_top+y_top^2)*forcex/kvisc;
% Initialize populations with rho=1 and (ux,uy)=(0,0)
feq=zeros(NPOP);
f1=zeros(NPOP,NX,NY);
f2=zeros(NPOP,NX,NY);
forcepop=zeros(NPOP);
for y=1:NY
for x=1:NX
dense=rho(x,y);
vx=ux(x,y);
vy=uy(x,y);
for k=1:NPOP
feq(k)=weights(k)*(dense+(3*(vx*cx(k)+vy*cy(k)) ...
+9/2*(cx(k)*vx+cy(k)*vy)^2-3/2*(vx^2+vy^2)));
f1(k,x,y)=feq(k);
f2(k,x,y)=feq(k);
end
end
end
% Main algorithm
for counter=1:NSTEPS
% Macroscopic parameters computed through velocity moments of
% populations f1
for y=1:NY
for x=1:NX
dense=0;
vx=0;
vy=0;
for k=1:NPOP
dense=dense+f1(k,x,y);
vx=vx+cx(k)*f1(k,x,y);
vy=vy+cy(k)*f1(k,x,y);
end
rho(x,y)=dense;
ux(x,y)=vx;
uy(x,y)=vy;
for k=1:NPOP
% Compute the populations equilibrium value
feq(k)=weights(k).*(dense+3*(vx*cx(k)+vy*cy(k)) ...
+9/2*((cx(k)*cx(k)-1/3)*vx*vx+2*cx(k)*cy(k)*vx*vy+(cy(k)*cy(k)-1/3)*vy*vy));
% Collision step
f1(k,x,y)=f1(k,x,y)*(1-omega)+feq(k)*omega;
% Streaming step
newx=1+mod(x-1+cx(k)+NX,NX);
newy=1+mod(y-1+cy(k)+NY,NY);
f2(k,newx,newy)=f1(k,x,y);
end
end
end
% Zou He Boundary Conditions
x=1; % inlet
for y=2:NY-1
ux(x,y)=ux_in;
uy(x,y)=0;
rho(x,y)=ux(x,y)+(f2(1,x,y)+f2(3,x,y)+f2(5,x,y)+2*(f2(4,x,y)+f2(7,x,y)+f2(8,x,y)));
f2(2,x,y)=f2(4,x,y)+2/3*ux(x,y);
f2(6,x,y)=f2(8,x,y)+1/6*ux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,y);
end
% Corners
%==================================================================
%Bottom left corner
x=1; y=1;
rho(x,y)=rho(x,y+1); %Extrapolation (1st order)
ux(x,y)=0;
uy(x,y)=0;
f2(2,x,y)=f2(4,x,y)+2/3*ux(x,y);
f2(3,x,y)=f2(5,x,y)+2/3*uy(x,y);
f2(6,x,y)=f2(8,x,y)+1/6*(ux(x,y)+uy(x,y));
f2(7,x,y)=1/12*(-ux(x,y)+uy(x,y));
f2(9,x,y)=1/12*(ux(x,y)-uy(x,y));
f2(1,x,y)=0;
f2(1,x,y)=rho(x,y)-sum(f2(:,x,y));
%Top left corner
x=1; y=NY;
rho(x,y)=rho(x,y-1); %Extrapolation (1st order)
ux(x,y)=0;
uy(x,y)=0;
f2(2,x,y)=f2(4,x,y)+2/3*ux(x,y);
f2(5,x,y)=f2(3,x,y)-2/3*uy(x,y);
f2(9,x,y)=f2(7,x,y)+1/6*(ux(x,y)-uy(x,y));
f2(6,x,y)=1/12*(ux(x,y)+uy(x,y));
f2(8,x,y)=-1/12*(ux(x,y)+uy(x,y));
f2(1,x,y)=0;
f2(1,x,y)=rho(x,y)-sum(f2(:,x,y));
%Bottom right corner
x=NX; y=1;
rho(x,y)=rho(x,y+1); %Extrapolation (1st order)
ux(x,y)=0;
uy(x,y)=0;
f2(4,x,y)=f2(2,x,y)-2/3*ux(x,y);
f2(3,x,y)=f2(5,x,y)+2/3*uy(x,y);
f2(7,x,y)=f2(9,x,y)+1/6*(-ux(x,y)+uy(x,y));
f2(6,x,y)=1/12*(ux(x,y)+uy(x,y));
f2(8,x,y)=-1/12*(ux(x,y)+uy(x,y));
f2(1,x,y)=0;
f2(1,x,y)=rho(x,y)-sum(f2(:,x,y));
%Top right corner
x=NX; y=NY;
rho(x,y)=rho(x,y-1); %Extrapolation (1st order)
ux(x,y)=0;
uy(x,y)=0;
f2(4,x,y)=f2(2,x,y)-2/3*ux(x,y);
f2(5,x,y)=f2(3,x,y)-2/3*uy(x,y);
f2(8,x,y)=f2(6,x,y)-1/6*(ux(x,y)+uy(x,y));
f2(7,x,y)=1/12*(-ux(x,y)+uy(x,y));
f2(9,x,y)=1/12*(ux(x,y)-uy(x,y));
f2(1,x,y)=0;
f2(1,x,y)=rho(x,y)-sum(f2(:,x,y));
% Assign new state f1, i.e. f(t+1) to previous state f2, i.e. f(t)
f1=f2;
end
ux_plot=ux;
% % Compare LBM and Analytical solution velocity profiles
% figure('color',[1 1 1])
% hold on
% plot(y_plot./(y_top),ux_analy./umax,'ko--');
% xlabel('y/y_{top}');
% ylabel('u/umax');
% plot(y_plot./(y_top),ux_plot(round(NX),:)./umax,'rs-.');
% legend('ux analy','ux LBM');
% axis tight
% box on
% Calculation of L2 error
sum_num=0;
sum_denom=0;
x=NX;
for y=1:NY
sum_num=sum_num+(ux_plot(x,y)-ux_analy(y)).^2;
sum_denom=sum_denom+ux_analy(y).^2;
end
error=sqrt((sum_num)/(sum_denom));
disp(['L2 relative error = ',num2str(error)]);
toc % Stop time counter