1 function Mmat=mat_flux(X)
3 % Compute wind flux matrix for a hexa grid assuming all sides in the y
4 % direction are straight vertical while horizontal sides may be slanted
6 % X{l}(i,j,k) coordinate l of node (i,j,k), if l=1,2 does not depend on k
8 % Mmat matrix computing fluxes in normal directions on mesh
12 x = X{1}; y = X{2}; z = X{3};
13 % Q: this assumes that we are working on a cube based on the X direction?
14 [nx1, ny1, nz1] = size(x);
15 nx = nx1 - 1; ny = ny1 - 1; nz = nz1 - 1;
16 % TODO: error check to make sure we are only working in 3 dimensions?
17 n = nx * ny * nz1 + nx * ny1 * nz + nx1 * ny * nz;
19 % continue one layer up
20 s.dz_at_u(:,:,nz+1)=s.dz_at_u(:,:,nz);
21 s.dz_at_v(:,:,nz+1)=s.dz_at_v(:,:,nz);
24 % slope in x and y directions
25 dzdx = zeros(nx,ny+1,nz+1);
26 dzdy = zeros(nx+1,ny,nz+1);
30 dzdx(i,j,k) = ((z(i+1,j,k)-z(i,j,k))/(x(i+1,j,k)-x(i,j,k)));
35 dzdy(i,j,k) = ((z(i,j+1,k)-z(i,j,k))/(y(i,j+1,k)-y(i,j,k)));
48 row_offset = nx1 * ny * nz + ny1 * nx * nz;
51 % Size of the grid for the current direction
52 grid_size = [nx,ny,nz];
53 grid_size(ux) = grid_size(ux) + 1;
54 X_len = grid_size(1); Y_len = grid_size(2); Z_len = grid_size(3);
61 M(M_entry) = s.area_u(ix,jx,kx);
64 M_entry = M_entry + 1;
67 is_top_level = (kx == Z_len);
68 is_low_level = (kx == 1);
69 is_first_row = (ix == 1);
70 is_last_row = (ix == X_len);
74 M(M_entry) = -s.area_w(ix - 1, jx, kx + 1) * ...
75 0.5 * (dzdx(ix - 1, jx, kx + 1) + dzdx(ix - 1, jx + 1, kx + 1)) * ...
76 0.5 * s.dz_at_u(ix - 1, jx, kx) / (s.dz_at_u(ix - 1, jx, kx + 1) + s.dz_at_u(ix - 1, jx, kx));
77 I(M_entry) = row_offset + (ix - 1) + (jx - 1) * nx + kx * nx * ny;
79 M_entry = M_entry + 1;
82 M(M_entry) = -s.area_w(ix, jx, kx + 1) * ...
83 0.5 * (dzdx(ix, jx, kx + 1) + dzdx(ix, jx + 1, kx + 1)) * ...
84 0.5 * s.dz_at_u(ix, jx, kx) / (s.dz_at_u(ix, jx, kx + 1) + s.dz_at_u(ix, jx, kx));
85 I(M_entry) = row_offset + ix + (jx - 1) * nx + kx * nx * ny;
87 M_entry = M_entry + 1;
92 M(M_entry) = -s.area_w(ix - 1, jx, kx) * ...
93 0.5 * (dzdx(ix - 1, jx, kx) + dzdx(ix - 1, jx + 1, kx)) * ...
94 0.5 * s.dz_at_u(ix - 1, jx, kx) / (s.dz_at_u(ix - 1, jx, kx - 1) + s.dz_at_u(ix - 1, jx, kx));
95 I(M_entry) = row_offset + (ix - 1) + (jx - 1) * nx + (kx - 1) * nx * ny;
97 M_entry = M_entry + 1;
100 M(M_entry) = -s.area_w(ix, jx, kx) * ...
101 0.5 * (dzdx(ix, jx, kx) + dzdx(ix, jx + 1, kx)) * ...
102 0.5 * s.dz_at_u(ix, jx, kx) / (s.dz_at_u(ix, jx, kx - 1) + s.dz_at_u(ix, jx, kx));
103 I(M_entry) = row_offset + ix + (jx - 1) * nx + (kx - 1) * nx * ny;
105 M_entry = M_entry + 1;
110 M(M_entry) = -s.area_w(ix - 1, jx, kx) * ...
111 0.5 * (dzdx(ix - 1, jx, kx) + dzdx(ix - 1, jx + 1, kx)) * ...
112 0.5 * s.dz_at_u(ix - 1, jx, kx) / (s.dz_at_u(ix - 1, jx, kx) + s.dz_at_u(ix - 1, jx, kx - 1));
113 I(M_entry) = row_offset + (ix - 1) + (jx - 1) * nx + (kx - 1) * nx * ny;
115 M_entry = M_entry + 1;
116 M(M_entry) = -s.area_w(ix - 1, jx, kx + 1) * ...
117 0.25 * (dzdx(ix - 1, jx, kx + 1) + dzdx(ix - 1, jx + 1, kx + 1));
118 I(M_entry) = row_offset + (ix - 1) + (jx - 1) * nx + kx * nx * ny;
120 M_entry = M_entry + 1;
123 M(M_entry) = -s.area_w(ix, jx, kx) * ...
124 0.5 * (dzdx(ix, jx, kx) + dzdx(ix, jx + 1, kx)) * ...
125 0.5 * (s.dz_at_u(ix, jx, kx) / (s.dz_at_u(ix, jx, kx - 1) + s.dz_at_u(ix, jx, kx)));
126 I(M_entry) = row_offset + ix + (jx - 1) * nx + (kx - 1) * nx * ny;
128 M_entry = M_entry + 1;
129 M(M_entry) = -s.area_w(ix, jx, kx + 1) * ...
130 0.25 * (dzdx(ix, jx, kx + 1) + dzdx(ix, jx + 1, kx + 1));
131 I(M_entry) = row_offset + ix + (jx - 1) * nx + kx * nx * ny;
133 M_entry = M_entry + 1;
138 M(M_entry) = s.area_v(ix,jx,kx);
141 M_entry = M_entry + 1;
144 is_top_level = (kx == Z_len);
145 is_low_level = (kx == 1);
146 is_first_row = (jx == 1);
147 is_last_row = (jx == Y_len);
151 M(M_entry) = -s.area_w(ix, jx - 1, kx + 1) * ...
152 0.5 * (dzdy(ix, jx - 1, kx + 1) + dzdy(ix + 1, jx - 1, kx + 1)) * ...
153 0.5 * s.dz_at_v(ix, jx - 1, kx) / (s.dz_at_v(ix, jx - 1, kx + 1) + s.dz_at_v(ix, jx - 1, kx));
154 I(M_entry) = row_offset + ix + (jx - 2) * nx + kx * nx * ny;
156 M_entry = M_entry + 1;
159 M(M_entry) = -s.area_w(ix, jx, kx + 1) * ...
160 0.5 * (dzdy(ix, jx, kx + 1) + dzdy(ix + 1, jx, kx + 1)) * ...
161 0.5 * s.dz_at_v(ix, jx, kx) / (s.dz_at_v(ix, jx, kx + 1) + s.dz_at_v(ix, jx, kx));
162 I(M_entry) = row_offset + ix + (jx - 1) * nx + kx * nx * ny;
164 M_entry = M_entry + 1;
169 M(M_entry) = -s.area_w(ix, jx - 1, kx) * ...
170 0.5 * (dzdy(ix, jx - 1, kx) + dzdy(ix + 1, jx - 1, kx)) * ...
171 0.5 * s.dz_at_v(ix, jx - 1, kx) / (s.dz_at_v(ix, jx - 1, kx - 1) + s.dz_at_v(ix, jx - 1, kx));
172 I(M_entry) = row_offset + ix + (jx - 2) * nx + (kx - 1) * nx * ny;
174 M_entry = M_entry + 1;
177 M(M_entry) = -s.area_w(ix, jx, kx) * ...
178 0.5 * (dzdy(ix, jx, kx) + dzdy(ix + 1, jx, kx)) * ...
179 0.5 * s.dz_at_v(ix, jx, kx) / (s.dz_at_v(ix, jx, kx - 1) + s.dz_at_v(ix, jx, kx));
180 I(M_entry) = row_offset + ix + (jx - 1) * nx + (kx - 1) * nx * ny;
182 M_entry = M_entry + 1;
187 M(M_entry) = -s.area_w(ix, jx - 1, kx) * ...
188 0.5 * (dzdy(ix, jx - 1, kx) + dzdy(ix + 1, jx - 1, kx)) * ...
189 0.5 * s.dz_at_v(ix, jx - 1, kx) / (s.dz_at_v(ix, jx - 1, kx) + s.dz_at_v(ix, jx - 1, kx - 1));
190 I(M_entry) = row_offset + ix + (jx - 2) * nx + (kx - 1) * nx * ny;
192 M_entry = M_entry + 1;
194 M(M_entry) = -s.area_w(ix, jx - 1, kx + 1) * ...
195 0.25 * (dzdy(ix, jx - 1, kx + 1) + dzdy(ix + 1, jx - 1, kx + 1));
196 I(M_entry) = row_offset + ix + (jx - 2) * nx + kx * nx * ny;
198 M_entry = M_entry + 1;
201 M(M_entry) = -s.area_w(ix, jx, kx) * ...
202 0.5 * (dzdy(ix, jx, kx) + dzdy(ix + 1, jx, kx)) * ...
203 0.5 * (s.dz_at_v(ix, jx, kx) / (s.dz_at_v(ix, jx, kx - 1) + s.dz_at_v(ix, jx, kx)));
204 I(M_entry) = row_offset + ix + (jx - 1) * nx + (kx - 1) * nx * ny;
206 M_entry = M_entry + 1;
208 M(M_entry) = -s.area_w(ix, jx, kx + 1) * ...
209 0.25 * (dzdy(ix, jx, kx + 1) + dzdy(ix + 1, jx, kx + 1));
210 I(M_entry) = row_offset + ix + (jx - 1) * nx + kx * nx * ny;
212 M_entry = M_entry + 1;
215 elseif ux == 3 && kx ~= 1
217 M(M_entry) = s.area_w(ix, jx, kx);
220 M_entry = M_entry + 1;
228 Mmat = sparse(I,J,M,n,n);