/
poiscell_2d_init.m
141 lines (107 loc) · 2.51 KB
/
poiscell_2d_init.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
% McDermott
% 10-14-2015
% poiscell_2d_init.m
% First, create a 3 x 3 array of cells with a nontrivial solenoidal velocity field.
Lx = 2*pi;
Ly = 2*pi;
Sx = rand(1)*Lx; % random phase shift in x
Sy = rand(1)*Ly; % random phase shift in y
nx = 3;
ny = 3;
B = 2;
% mesh spacing
dx = Lx/nx;
dy = Ly/ny;
% staggered face locations
x = [0:nx]*dx + Sx;
y = [0:ny]*dy + Sy;
% cell center locations
xp = x(1:nx) + 0.5*dx;
yp = y(1:ny) + 0.5*dy;
for i=1:nx+1
for j=1:ny
uhat(i,j) = 1 - B*cos(x(i))*sin(yp(j));
end
end
for i=1:nx
for j=1:ny+1
vhat(i,j) = 1 + B*sin(xp(i))*cos(y(j));
end
end
% build A matrix (periodic)
A = sparse(nx*ny,nx*ny);
for i=1:nx
for j=1:ny
ip1=i+1;
im1=i-1;
jp1=j+1;
jm1=j-1;
if ip1>nx; ip1=ip1-nx; end
if jp1>ny; jp1=jp1-ny; end
if im1<1; im1=im1+nx; end
if jm1<1; jm1=jm1+ny; end
% lexicographical ordering
np = (j-1)*nx + i;
east = (j-1)*nx + ip1;
west = (j-1)*nx + im1;
north = (jp1-1)*nx + i;
south = (jm1-1)*nx + i;
A(np,np ) = -(2/dx^2 + 2/dy^2);
A(np,east ) = 1/dx^2;
A(np,west ) = 1/dx^2;
A(np,north) = 1/dy^2;
A(np,south) = 1/dy^2;
end
end
% build right hand side of Poisson equation
for i=1:nx
for j=1:ny
ip1=i+1;
jp1=j+1;
if ip1>nx; ip1=ip1-nx; end
if jp1>ny; jp1=jp1-ny; end
np = (j-1)*nx + i;
b(np) = (uhat(ip1,j)-uhat(i,j))/dx + (vhat(i,jp1)-vhat(i,j))/dy;
end
end
% solve Poisson equation for "condensed" system
n_cells = nx*ny;
Ac = A(1:n_cells-1,1:n_cells-1);
bc = b(1:n_cells-1);
pc = Ac\bc';
pvec = [pc;0];
% map solution vector to computational indices
for i=1:nx
for j=1:ny
np = (j-1)*nx + i;
p(i,j) = pvec(np);
end
end
% project velocities
for i=1:nx
for j=1:ny
im1=i-1;
jm1=j-1;
if im1<1; im1=im1+nx; end
if jm1<1; jm1=jm1+ny; end
u(i,j) = uhat(i,j) - ( p(i,j) - p(im1,j) )/dx;
v(i,j) = vhat(i,j) - ( p(i,j) - p(i,jm1) )/dy;
end
end
% check divergence
for i=1:nx
for j=1:ny
ip1=i+1;
jp1=j+1;
if ip1>nx; ip1=ip1-nx; end
if jp1>ny; jp1=jp1-ny; end
div(i,j) = (u(ip1,j)-u(i,j))/dx + (v(i,jp1)-v(i,j))/dy;
end
end
display( ['max divergence = ',num2str(max(max(abs(div))))] )
for j=1:ny
u(nx+1,j) = u(1,j);
end
for i=1:nx
v(i,ny+1) = v(i,1);
end