-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.m
395 lines (328 loc) · 10.1 KB
/
main.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
% Numerical Solution to Navier Stokes Equation in 2D Polar Coordinates
% Second Order accuracy throughout
% Uses Biconjugate Gradient Stabilized scheme for Matrix Solution.
% Author: Prapanj G R
% Department of Mechanical Engineering, Indian Institute of Science,
% Bangalore, India.
% r, t coordinates. r- radial, t - theta.
% t varies from 0 rad (downstream 0 angle) to 2pi radians. Anticlockwise is
% positive
function main()
clear all; clc; format long;
global Us2;
global Vs2;
global rr;
global tt;
global delr;
global delth;
global r_n;
global t_n;
global Re;
global dt;
global k;
delr =0.075;
delth =pi/75; %32
r =[1:delr:20];
t =[0:delth:(2*pi-delth)];
r_size = size(r); r_n= r_size(2);
t_size = size(t); t_n= t_size(2);
[rr,tt] = ndgrid(r,t);
for i=1:r_n
for j=1:t_n
XX(i,j)=rr(i,j)*cos(tt(i,j));
YY(i,j)=rr(i,j)*sin(tt(i,j));
end
end
for i=1:r_n
for j=1:t_n+1
if j==t_n+1
gridX(i,j) = XX(i,1);
gridY(i,j) = YY(i,1);
else
gridX(i,j)=XX(i,j);
gridY(i,j)=YY(i,j);
end
end
end
fid = fopen('grid.xyz','w');
fprintf(fid, '%d \t %d\n', r_n, t_n+1);
fprintf(fid,'%f\n',gridX);
fprintf(fid,'%f\n',gridY);
fclose(fid);
writenum= 50;
% In grid, the first argument is in r direction
% 2nd argument is in theta direction.
% let Ux = 10 at the upstream. Then U = -Ux* cos(pi - t)
% V = -Ux*sin(pi - t)
% Initial U and V
U = zeros(size(XX));
V = zeros(size(XX));
Ux = zeros(size(XX));
Uy = zeros(size(XX));
Us = zeros(size(XX));
Vs = zeros(size(XX));
Us2 = zeros(size(XX));
Vs2 = zeros(size(XX));
G1 = zeros(size(XX));
G2 = zeros(size(XX));
deldotU = zeros(size(XX));
P = zeros(size(XX));
UxBc=1.0;
for j=1:t_n
for i=2:r_n
U(i,j)=UxBc*(1 - (1/r(i))^2)*cos(tt(i,j));
V(i,j)=-UxBc*(1 + (1/r(i))^2)*sin(tt(i,j));
end
end
% Reynolds number
Re=150;
dt = 0.005;
% Time iterations
for k=1:40000
% Convection
for i=2:r_n-1
for j=1:t_n
convU1 = (1/(2*delr))*( U(i+1,j)^2 - U(i-1,j)^2 );
convV1 = (1/(2*delr))*(U(i+1,j)*V(i+1,j) - U(i-1,j)*V(i-1,j) );
convV3 = 2*U(i,j)*V(i,j)/rr(i,j);
convU3 = (U(i,j)^2 - V(i,j)^2)/rr(i,j); %%%%%% edited
if (j == 1) %Theta boundaries
convU2 = (1/rr(i,j))*(1/(2*delth))*(U(i,j+1)*V(i,j+1) - U(i,t_n)*V(i,t_n));
convV2 = (1/rr(i,j))*(1/(2*delth))*(V(i,j+1)^2 - V(i,t_n)^2);
elseif (j==t_n)
convU2 = (1/rr(i,j))*(1/(2*delth))*(U(i,1)*V(i,1) - U(i,j-1)*V(i,j-1));
convV2 = (1/rr(i,j))*(1/(2*delth))*(V(i,1)^2 - V(i,j-1)^2);
else
convU2 = (1/rr(i,j))*(1/(2*delth))*(U(i,j+1)*V(i,j+1) - U(i,j-1)*V(i,j-1));
convV2 = (1/rr(i,j))*(1/(2*delth))*(V(i,j+1)^2 - V(i,j-1)^2);
end
Us(i,j) = U(i,j) - dt*( convU1+convU2+convU3);
Vs(i,j) = V(i,j) - dt*( convV1+convV2 + convV3);
end
end
sum=0;
for j=1:t_n
sum= sum+U(r_n,j);
end
fprintf('\n Sum after convection = %f ',sum);
for j=1:t_n
Us(r_n,j) = U(r_n,j);
Vs(r_n,j) = V(r_n,j);
Us2(r_n,j) = U(r_n,j);
Vs2(r_n,j) = V(r_n,j);
end
solveUs22(Us);
solveVs22(Vs);
for i=2:r_n-1
for j=1:t_n
U(i,j)=Us2(i,j);
V(i,j)=Vs2(i,j);
end
end
sum=0;
for j=1:t_n
sum= sum+U(r_n,j);
end
fprintf('\n Sum after diffusion = %f ',sum);
% U(1,:)=0;V(1,:) =0;
for i = 2: r_n-1
for j = 1:t_n
if (i==2) % wall
term1= (U(i+1,j) - 0)/(2*delr) ;
% term1= (1/(4*delr))*( (rr(i+1,j)+rr(i,j))*(U(i+1,j)+U(i,j)) - (rr(i-1,j) +rr(i,j))*(0 +U(i,j)) );
elseif (i==r_n-1)
term1= (U(i+1,j) - U(i-1,j))/(2*delr) ;
else
term1= (U(i+1,j) - U(i-1,j))/(2*delr) ;
end
if (j==1)
term2= (V(i,j+1)-V(i,t_n))/(2*delth);
elseif (j==t_n)
term2= (V(i,1)-V(i,j-1))/(2*delth);
else
term2= (V(i,j+1)-V(i,j-1))/(2*delth);
end
term3 = U(i,j);
deldotU(i,j) = term1 + (1/r(i))*(term2+term3);
end
end
b=zeros(size(XX));
for i=2:r_n-1 %1 to r_n
for j=1:t_n
b(i,j)=(1/dt)*deldotU(i,j);
end
end
%
%
% %%% b(r_n-3,t_n-3)=0;
%
%
% % Biconjugate Gradient Stabilized method %%%%%%%%%%%%%%%%%%%
% %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% % Vectors for BICGSTAB
% v0=zeros(size(XX));
% d0=zeros(size(XX));
% dd0=zeros(size(XX));
% x0=zeros(size(XX));%zeros(size(XX));
% %x0(10,10)=1;
% % Scalars
% p0=v0;
% alpha =1;
% w0=1;
% rho0=1;
%
% iter = 0;
%
% Ax0=ComputeAx_collocated_pn(x0,rr,tt, delr, delth, r_n, t_n,t,Pneumann);
%
% for i=2:r_n-1
% for j=1:t_n
% dd0(i,j)=b(i,j)-Ax0(i,j);
% d0(i,j)=dd0(i,j);
% end
% end
% %dd0=b-Ax0;
%
% %d0=dd0;
%
%
% Tol=1e-15;
% Res=100;
%
% while (Res>Tol && iter<5000),
% rho_i=innerp(dd0,d0,r_n,t_n);
% if (rho_i==0) break, end
% if (iter>0)
% beta = (rho_i/rho0)*(alpha/w0);
% p_i = d0 + beta.*(p0 - w0.*v0);
%
% else
% p_i=d0;
%
% end
% v_i = ComputeAx_collocated_pn(p_i,rr,tt,delr,delth,r_n,t_n,t,Pneumann);
%
% alpha = rho_i/innerp(dd0,v_i,r_n,t_n);
% s = d0 - alpha.*v_i;
%
% t=ComputeAx_collocated_pn(s,rr,tt,delr,delth,r_n,t_n,t,Pneumann);
%
% w_i=innerp(t,s,r_n,t_n)/innerp(t,t,r_n,t_n);
% x_i=x0+alpha.*p_i + w_i.*s;
%
% d_i=s - w_i.*t;
%
% P= x_i;
% %%%%%%%%
% d0=d_i;
% Res = innerp((x_i - x0 ), (x_i -x0),r_n,t_n);
% Res = sqrt ( Res / ((r_n-2)*(t_n)));
% x0=x_i;
% p0=p_i;
% w0=w_i;
% v0=v_i;
% rho0=rho_i;
%
% iter=iter+1;
%
%
% end
% fprintf('\n Pressure converged at Iter %d, Residual is %0.3g at time step %d', iter, Res, k);
% ------------------------------------------------------------
%b=ones(t_n*r_n,1);
P = BICGSTABIter_p(P,r,r_n,t_n,delr,delth,b);
% Making the plot look good by accounting for Neumann boundary
for j=1:t_n
P(1,j)=P(2,j);
P(r_n,j) =P(r_n-1,j);
end
for i=2:r_n-1
for j=1:t_n
G1(i,j) = (P(i+1,j)-P(i-1,j))/(2*delr);
if (j==1)
G2(i,j) =(1/r(i))*(P(i,j+1)-P(i,t_n))/(2*delth);
elseif (j==t_n)
G2(i,j) =(1/r(i))*(P(i,1)-P(i,j-1))/(2*delth);
else
G2(i,j) =(1/r(i))*(P(i,j+1)-P(i,j-1))/(2*delth);
end
U(i,j)= U(i,j) - dt*G1(i,j);
V(i,j)= V(i,j) - dt*G2(i,j);
end
end
for i=1:r_n
for j=1:t_n+1
if j==t_n+1
Uplot(i,j) = U(i,1);
Vplot(i,j) = V(i,1);
Pplot(i,j) = P(i,1);
else
Uplot(i,j)=U(i,j); Vplot(i,j) = V(i,j);
Pplot(i,j) = P(i,j);
end
end
end
sum=0;
for j=1:t_n
sum= sum+U(r_n,j);
end
fprintf('\n Sum after Pressure correction = %f \n',sum);
if (mod(k,writenum)==0)
%Velocities in XY coordinates for the contours.
for i=1:r_n
for j=1:t_n+1
Umag(i,j)=sqrt(Uplot(i,j)^2 + Vplot(i,j)^2);
if j==t_n+1
Ux(i,j)=Uplot(i,j)*cos(tt(i,1)) - Vplot(i,j)*sin(tt(i,1));
Uy(i,j)=Uplot(i,j)*sin(tt(i,1))+ Vplot(i,j)*cos(tt(i,1));
else
Ux(i,j)=Uplot(i,j)*cos(tt(i,j)) - Vplot(i,j)*sin(tt(i,j));
Uy(i,j)=Uplot(i,j)*sin(tt(i,j))+ Vplot(i,j)*cos(tt(i,j));
end
end
end
% writing the solutions
if k==0
fid2 = fopen('sol_init.xyz','w');
elseif k==1000
fid2 = fopen('sol_1000.xyz','w');
elseif k==2500
fid2 = fopen('sol_2500.xyz','w');
elseif k==5000
fid2 = fopen('sol_5000.xyz','w');
elseif k==8000
fid2 = fopen('sol_8000.xyz','w');
elseif k==11000
fid2 = fopen('sol_11000.xyz','w');
elseif k==14000
fid2 = fopen('sol_14000.xyz','w');
elseif k==17000
fid2 = fopen('sol_17000.xyz','w');
elseif k==20000
fid2 = fopen('sol_20000.xyz','w');
elseif k==25000
fid2 = fopen('sol_25000.xyz','w');
elseif k==30000
fid2 = fopen('sol_30000.xyz','w');
elseif k==35000
fid2 = fopen('sol_35000.xyz','w');
elseif k==40000
fid2 = fopen('sol_40000.xyz','w');
else
fid2 = fopen('sol.xyz','w');
end
fprintf(fid2, '%d \t %d \t %d\n', r_n, t_n+1, 6);
% Pressure
fprintf(fid2,'%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n',Pplot);
fprintf(fid2,'%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n',Uplot);
fprintf(fid2,'%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n',Vplot);
fprintf(fid2,'%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n',Ux);
fprintf(fid2,'%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n',Uy);
fprintf(fid2,'%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n',Umag);
% U
% V
fclose(fid2);
fprintf('\n Wrote solution to file \n');
end
end %time###########################################################3
end