-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.m
118 lines (115 loc) · 3.51 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
% This code solves the incompressible transient 2D Navier-Stokes equation on
% a uniform staggered grid --> pressure is stored at the cell center and
% velocities on the faces; this allows for the solution to have a tight
% coupling
% staggered grid
%--------------------------------------
% -p- -u- -p- -u- -p- -u- -p- -u- -p-
% -v- -v- -v- -v- -v-
% -p- -u- -p- -u- -p- -u- -p- -u- -p-
% -v- -v- -v- -v- -v-
% -p- -u- -p- -u- -p- -u- -p- -u- -p-
% -v- -v- -v- -v- -v-
% -p- -u- -p- -u- -p- -u- -p- -u- -p-
% -v- -v- -v- -v- -v-
% -p- -u- -p- -u- -p- -u- -p- -u- -p-
%---------------------------------------
clear; close all; clc;
%% set input parameters
nu = 0.001; %kinematic viscosity
rho = 1; %density
nx = 20; % node points excluding boundaries
ny = 20;
Lx = 2; %domain length over x
Ly = 2; %domain height over y
dt = 0.01; % timestep
time = 1; %end time (s)
actualtime = 0;
% Create mesh sizes
hx=Lx/nx;
hy=Ly/ny;
%% Memory allocation to matrices
utemp = zeros(nx+2,ny+2);
vtemp = zeros(nx+2,ny+2);
u = zeros(nx+2,ny+2);
v = zeros(nx+2,ny+2);
p = zeros(nx+2,ny+2);
R = zeros(nx*ny,1);
L=zeros(nx*ny,nx*ny);
%% Laplacian operator
L = Laplacian(L,nx,ny,hx,hy);
% Set pressure BC
%L(1,:)=0; L(1,1)=1;
%% time loop warning for too large timestep
if (max(max(u)))*dt/hx >= 1 || (max(max(v)))*dt/hy >= 1
disp ('timestep too large, divergence may occur')
else
%% time loop
while actualtime<=time
%% setting of Boundary Conditions
u(:,1) = 1; %First column is inlet --> not calculated later on
v(:,1) = 0;
% BC wall
u(1,:)=0;
v(1,:)=0;
u(nx+2,:)=0;
v(nx+2,:)=0;
%% temporary velocity calculation (predictor step)
[utemp,vtemp]=predictor(utemp,vtemp,u,v,nx,ny,hx,hy,nu,dt);
%Gaussian Layer
u(:,nx+1) = u(:,nx);
v(:,nx+1) = v(:,nx);
%% compute RHS
n = 0;
for j = 2:ny+1
for i = 2:nx+1
n = n+1;
R(n) = -rho/dt*((utemp(i+1,j)-utemp(i,j))*hx ...
+(vtemp(i,j+1)-vtemp(i,j))*hy);
end
end
%% solve pressure
% pressure solve with Laplacian pv = L\RHS
% pv is a vector including the pressure value of every cell
pv = L\R;
%convert pv into pressure field
n = 0;
for j = 2:ny
for i = 2:nx
n = n+1;
p(i,j) = pv(n);
end
end
p(:,nx+1) = p(:,nx);
%% velocity correction (corrector step)
[u,v] = corrector(u,v,utemp,vtemp,dt,rho,p,hx,hy,nx,ny);
%Gaussian Layer
u(:,nx+1) = u(:,nx);
v(:,nx+1) = v(:,nx);
%% plot
% figure(1); clf(1);
% title('Velocity & Pressure Field','fontsize',20);
% xlim([0,Lx]);
% ylim([0,Ly]);
% hold on
%
x (2:nx+2)=linspace(0,Lx,nx+1);
y (2:ny+2)=linspace(0,Ly,ny+1);
%
% contourf(x(2:nx+1),y(2:ny+1),p(2:nx+1,2:ny+1)');
% quiver(x(2:nx+1),y(2:ny+1),...
% u(2:nx+1,2:ny+1)',v(2:nx+1,2:ny+1)','filled', 'k');
% xmax=Lx-hx;
% ymax=Ly-hy;
% % Set Limits on X & Y Axis
% xlim([0 xmax]);
% ylim([0 ymax]);
% col = colorbar;
% col.Label.String = 'Pressure (Pa)';
% drawnow
plot (u(10,:),y)
pause(0.01)
%% timestep end
actualtime = actualtime + dt;
end
end % delta t criterion check end