-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
516 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
% Copyright 2020 The MathWorks, Inc. | ||
function xu = getIntermediateU_xyCNAB(u, b, dt, Re, nx, ny, dx, dy) | ||
|
||
persistent A4u dtRe_old L4u | ||
|
||
if isempty(dtRe_old) | ||
dtRe_old = dt/Re; | ||
end | ||
|
||
if isempty(A4u) || any(size(L4u) ~= [(nx-1)*ny,(nx-1)*ny]) || dtRe_old ~= dt/Re | ||
dtRe_old = dt/Re; | ||
maskU = false(nx+1,ny+2); | ||
maskU(2:end-1,2:end-1) = true; | ||
L4u = getL4u(nx,ny,dx,dy,maskU); | ||
A4u = eye(size(L4u),'like',L4u)-dt/(2*Re)*L4u; | ||
A4u = decomposition(A4u); | ||
end | ||
|
||
% x0 = u(2:end-1,2:end-1); | ||
% [xu,flag] = cgs(A4u,b(:),[],[],[],[],x0(:)); | ||
xu = A4u\b(:); | ||
xu = reshape(xu,[nx-1,ny]); | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
% Copyright 2020 The MathWorks, Inc. | ||
function xu = getIntermediateU_xyRK3(u, b, dt, Re, nx, ny, dx, dy, id) | ||
|
||
persistent A4u1 A4u2 A4u3 dtRe_old L4u | ||
|
||
if isempty(dtRe_old) | ||
dtRe_old = dt/Re; | ||
end | ||
|
||
if isempty(A4u1) || any(size(L4u) ~= [(nx-1)*ny,(nx-1)*ny]) || dtRe_old ~= dt/Re | ||
dtRe_old = dt/Re; | ||
maskU = false(nx+1,ny+2); | ||
maskU(2:end-1,2:end-1) = true; | ||
L4u = getL4u(nx,ny,dx,dy,maskU); | ||
A4u1 = eye(size(L4u),'like',L4u)-(8/15)*dt/(Re)*L4u; | ||
A4u2 = eye(size(L4u),'like',L4u)-(2/15)*dt/(Re)*L4u; | ||
A4u3 = eye(size(L4u),'like',L4u)-(2/6)*dt/(Re)*L4u; | ||
A4u1 = decomposition(A4u1); | ||
A4u2 = decomposition(A4u2); | ||
A4u3 = decomposition(A4u3); | ||
|
||
end | ||
|
||
% x0 = u(2:end-1,2:end-1); | ||
% [xu,flag] = cgs(A4u,b(:),[],[],[],[],x0(:)); | ||
switch id | ||
case 1 | ||
xu = A4u1\b(:); | ||
case 2 | ||
xu = A4u2\b(:); | ||
case 3 | ||
xu = A4u3\b(:); | ||
end | ||
|
||
xu = reshape(xu,[nx-1,ny]); | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
% Copyright 2020 The MathWorks, Inc. | ||
function xv = getIntermediateV_xyCNAB(v, b, dt, Re, nx, ny, dx, dy) | ||
|
||
persistent A4v dtRe_old L4v | ||
|
||
if isempty(dtRe_old) | ||
dtRe_old = dt/Re; | ||
end | ||
|
||
if isempty(A4v) || any(size(L4v) ~= [nx*(ny-1),nx*(ny-1)]) || dtRe_old ~= dt/Re | ||
dtRe_old = dt/Re; | ||
maskV = false(nx+2,ny+1); | ||
maskV(2:end-1,2:end-1) = true; | ||
L4v = getL4v(nx,ny,dx,dy,maskV); | ||
A4v = eye(size(L4v),'like',L4v)-dt/(2*Re)*L4v; | ||
A4v = decomposition(A4v); | ||
end | ||
|
||
% x0 = v(2:end-1,2:end-1); | ||
% [xv,flag] = cgs(A4v,b(:),[],[],[],[],x0(:)); | ||
xv = A4v\b(:); | ||
xv = reshape(xv,[nx,ny-1]); | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
% Copyright 2020 The MathWorks, Inc. | ||
function xv = getIntermediateV_xyRK3(v, b, dt, Re, nx, ny, dx, dy,id) | ||
|
||
persistent A4v1 A4v2 A4v3 dtRe_old L4v | ||
|
||
if isempty(dtRe_old) | ||
dtRe_old = dt/Re; | ||
end | ||
|
||
if isempty(A4v1) || any(size(L4v) ~= [nx*(ny-1),nx*(ny-1)]) || dtRe_old ~= dt/Re | ||
dtRe_old = dt/Re; | ||
maskV = false(nx+2,ny+1); | ||
maskV(2:end-1,2:end-1) = true; | ||
L4v = getL4v(nx,ny,dx,dy,maskV); | ||
A4v1 = eye(size(L4v),'like',L4v)-(8/15)*dt/(Re)*L4v; | ||
A4v2 = eye(size(L4v),'like',L4v)-(2/15)*dt/(Re)*L4v; | ||
A4v3 = eye(size(L4v),'like',L4v)-(2/6)*dt/(Re)*L4v; | ||
A4v1 = decomposition(A4v1); | ||
A4v2 = decomposition(A4v2); | ||
A4v3 = decomposition(A4v3); | ||
|
||
end | ||
|
||
switch id | ||
case 1 | ||
xv = A4v1\b(:); | ||
case 2 | ||
xv = A4v2\b(:); | ||
case 3 | ||
xv = A4v3\b(:); | ||
end | ||
xv = reshape(xv,[nx,ny-1]); | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
% Copyright 2020 The MathWorks, Inc. | ||
function L4u = getL4u(nx,ny,dx,dy,mask) | ||
|
||
% Note: Boundary condition effects y derivative only | ||
% y-derivatives | ||
% ub = (u0+u1)/2 => u0 = 2*ub - u1; | ||
% Lu_1 = (u2 - 2*u1 + u0)/dy^2 | ||
% = (u2 - 2*u1 + 2*ub - u1)/dy^2 | ||
% = (u2 - 3*u1)/dx^2 + 2*ub/dy^2 | ||
% | ||
% x-derivatives | ||
% Lu_1 = (u2 - 2*u1 + ub)/dx^2 | ||
% = (u2 - 2*u1)/dx^2 + ub/dx^2 | ||
|
||
matrixSize = [nx-1,ny]; | ||
|
||
% 3 | ||
%152 | ||
% 4 | ||
coefx = 1/dx^2; | ||
coefy = 1/dy^2; | ||
|
||
i = zeros(nx*ny*5,1); | ||
j = zeros(nx*ny*5,1); | ||
v = zeros(nx*ny*5,1); | ||
index1 = 1; | ||
index = 1; | ||
for jj=1:ny | ||
for ii=1:nx-1 | ||
idx = [mask(ii+2,jj+1),mask(ii,jj+1),mask(ii+1,jj+2),mask(ii+1,jj),mask(ii+1,jj+1)]; | ||
stencils = [ii+1,jj | ||
ii-1,jj | ||
ii,jj+1 | ||
ii,jj-1 | ||
ii,jj]; | ||
stencils = stencils(idx,:); | ||
|
||
tmpx = 2; | ||
tmpy = 2 + sum(~idx(3:4)); | ||
|
||
coeffs = [coefx | ||
coefx | ||
coefy | ||
coefy | ||
-tmpx*coefx-tmpy*coefy]; | ||
coeffs = coeffs(idx); | ||
linearIdx = sub2ind(matrixSize, stencils(:,1), stencils(:,2)); | ||
|
||
i(index1:index1+length(linearIdx)-1) = index; | ||
j(index1:index1+length(linearIdx)-1) = linearIdx; | ||
v(index1:index1+length(linearIdx)-1) = coeffs; | ||
index1 = index1 + length(linearIdx); | ||
index = index + 1; | ||
end | ||
end | ||
|
||
i(i==0) = []; | ||
j(j==0) = []; | ||
v(v==0) = []; | ||
L4u = sparse(i,j,v,(nx-1)*ny,(nx-1)*ny); | ||
L4u(:,~any(L4u,1)) = []; | ||
L4u(~any(L4u,2),:) = []; | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
% Copyright 2020 The MathWorks, Inc. | ||
function L4v = getL4v(nx,ny,dx,dy,mask) | ||
|
||
% Note: Boundary condition effects x derivative only | ||
% x-derivatives | ||
% vb = (v0+v1)/2 => v0 = 2*vb - v1; | ||
% Lv_1 = (v2 - 2*v1 + v0)/dx^2 | ||
% = (v2 - 2*v1 + 2*vb - u1)/dx^2 | ||
% = (v2 - 3*v1)/dx^2 + 2*vb/dx^2 | ||
% | ||
% y-derivatives | ||
% Lv_1 = (v2 - 2*v1 + vb)/dy^2 | ||
% = (v2 - 2*v1)/dy^2 + vb/dy^2 | ||
|
||
matrixSize = [nx,ny-1]; | ||
|
||
% 3 | ||
%152 | ||
% 4 | ||
coefx = 1/dx^2; | ||
coefy = 1/dy^2; | ||
|
||
i = zeros(nx*ny*5,1); | ||
j = zeros(nx*ny*5,1); | ||
v = zeros(nx*ny*5,1); | ||
index1 = 1; | ||
index = 1; | ||
for jj=1:ny-1 | ||
for ii=1:nx | ||
idx = [mask(ii+2,jj+1),mask(ii,jj+1),mask(ii+1,jj+2),mask(ii+1,jj),mask(ii+1,jj+1)]; | ||
stencils = [ii+1,jj | ||
ii-1,jj | ||
ii,jj+1 | ||
ii,jj-1 | ||
ii,jj]; | ||
stencils = stencils(idx,:); | ||
|
||
tmpx = 2 + sum(~idx(1:2)); | ||
tmpy = 2; | ||
|
||
coeffs = [coefx | ||
coefx | ||
coefy | ||
coefy | ||
-tmpx*coefx-tmpy*coefy]; | ||
coeffs = coeffs(idx); | ||
linearIdx = sub2ind(matrixSize, stencils(:,1), stencils(:,2)); | ||
|
||
i(index1:index1+length(linearIdx)-1) = index; | ||
j(index1:index1+length(linearIdx)-1) = linearIdx; | ||
v(index1:index1+length(linearIdx)-1) = coeffs; | ||
index1 = index1 + length(linearIdx); | ||
index = index + 1; | ||
end | ||
end | ||
|
||
i(i==0) = []; | ||
j(j==0) = []; | ||
v(v==0) = []; | ||
L4v = sparse(i,j,v,nx*(ny-1),nx*(ny-1)); | ||
L4v(:,~any(L4v,1)) = []; | ||
L4v(~any(L4v,2),:) = []; | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
% Copyright 2020 The MathWorks, Inc. | ||
function Ax = operatorAu_CNAB(x,dt,Re,nx,ny,dx,dy) | ||
% Getting A for u velocity (Crank-Nicolson) | ||
% A = [I - dt/(2*Re)*Lxy]; | ||
|
||
x = reshape(x,nx-1,ny); | ||
xbig = zeros(nx+1,ny+2); | ||
xbig(2:end-1,2:end-1) = x; | ||
|
||
% Addting boudanry value | ||
% ub = (u0+u1)/2 => u0 = 2*ub - u1; | ||
xbig(:,1) = - xbig(:,2); | ||
xbig(:,end) = - xbig(:,end-1); | ||
|
||
Ax = x - dt/(2*Re)*(xbig(1:end-2,2:end-1)-2*xbig(2:end-1,2:end-1)+xbig(3:end,2:end-1))/dx^2; % nx-1 * ny | ||
Ax = Ax - dt/(2*Re)*(xbig(2:end-1,1:end-2)-2*xbig(2:end-1,2:end-1)+xbig(2:end-1,3:end))/dy^2; % nx-1 * ny | ||
Ax = Ax(:); | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
% Copyright 2020 The MathWorks, Inc. | ||
function Ax = operatorAv_CNAB(x,dt,Re,nx,ny,dx,dy) | ||
% Getting A for v velocity (Crank-Nicolson) | ||
% A = [I - dt/(2*Re)*Lxy]; | ||
|
||
x = reshape(x,nx,ny-1); | ||
xbig = zeros(nx+2,ny+1); | ||
xbig(2:end-1,2:end-1) = x; | ||
|
||
% Addting boudanry value | ||
% ub = (u0+u1)/2 => u0 = 2*ub - u1; | ||
xbig(1,:) = - xbig(2,:); | ||
xbig(end,:) = - xbig(end-1,:); | ||
|
||
Ax = x - dt/(2*Re)*(xbig(1:end-2,2:end-1)-2*xbig(2:end-1,2:end-1)+xbig(3:end,2:end-1))/dx^2; % nx-1 * ny | ||
Ax = Ax - dt/(2*Re)*(xbig(2:end-1,1:end-2)-2*xbig(2:end-1,2:end-1)+xbig(2:end-1,3:end))/dy^2; % nx-1 * ny | ||
Ax = Ax(:); | ||
end | ||
|
Oops, something went wrong.