I have Matlab code that simulates something called the 2-D Lid Driven Cavity Flow. In this code I have the following structure:
for i = 1:timeStep
%Lets call this Part 1
for a1 = 1:N
for b1 = 1:N
%calculates things
end
end
%Lets call this Part 2
for a2 = 1:N
for b2 = 1:N
%calculates things
end
end
%Lets call this Part 3
for a3 = 1:N
for b3 = 1:N
%calculates things
end
end
end
Since Part 1, Part 2, and Part 3 are independent pf each other I would like to compute them in parallel, or multi thread them, every time there is a timeStep (every iteration of primary for loop). Is there any way I can achieve this? Thanks!
I include my code to to reference:
Nx = 50;
Ny = 50;
numTimesteps = 10000;
reynoldsNum = 1000;
dt = 0.0025;
numIter = 100000;
Beta = 1.5;
maxErr = 0.001;
ds = 1/(Nx + 1);
x1 = 0:ds:1;
x2 = 0:ds:1;
time = 0;
boundarySpeed = 1;
PHI = zeros(Nx+2, Ny+2);
OMEGA = zeros(Nx+2, Ny+2);
U = zeros(Nx+2, Ny+2);
V = zeros(Nx+2, Ny+2);
x2d = zeros(Nx+2, Ny+2);
y2d = zeros(Nx+2, Ny+2);
PRESSURE = zeros(Nx+2, Ny+2);
B = zeros(Nx+2, Ny+2);
pressureOLD = zeros(Nx+2, Ny+2);
W = zeros(Nx+2, Ny+2);
for i = 1:Nx+2
for j = 1:Ny+2
x2d(i,j) = x1(i);
y2d(i,j) = x2(j);
end
end
for timeStep = 1:numTimesteps
if(mod(timeStep,10000) == 0)
disp(timeStep);
end
OLDPHI = PHI;
OLDOMEGA = OMEGA;
OLDPRESSURE = PRESSURE;
parfor parJob = 1:4
switch parJob
%{
----------------------------------
STREAM FUNCTION CALCULATION
----------------------------------
%}
case 1
for iter = 1:numIter
ERRMATRIX = OLDPHI;
for i = 2:Nx+1
for j = 2:Ny+1
PHI(i,j) = (1/4) * Beta * (OLDPHI(i+1,j) + OLDPHI(i-1,j) + OLDPHI(i,j+1) + OLDPHI(i,j-1) + ...
ds * ds * OLDOMEGA(i,j)) + (1 - Beta) * OLDPHI(i,j);
end
end
Err = 0;
for i = 1:Nx+2
for j = 1:Ny+2
Err = Err + abs(ERRMATRIX(i,j) - PHI(i,j));
end
end
if (Err <= maxErr)
break;
end
OLDPHI = PHI;
end
%{
----------------------------------
BOUNDARY CONDITIONS FOR VORTICITY
----------------------------------
%}
case 2
for i = 2:Nx+1
for j = 2:Ny+1
OMEGA(i,1) = -2 * OLDPHI(i,2) / (ds * ds); % bottom wall
OMEGA(i,Ny+2) = -2 * OLDPHI(i,Ny+1) / (ds * ds) - 2 * boundarySpeed / ds; % top wall
OMEGA(1,j) = -2 * OLDPHI(2,j) / (ds * ds); % right wall
OMEGA(Nx+2,j) = -2 * OLDPHI(Nx+1,j) / (ds * ds); % left wall
end
end
%{
----------------------------------
VORTICITY CALCULATIONS
----------------------------------
%}
for i = 2:Nx+1
for j = 2:Ny+1
W(i,j) = -(1 / 4) * ((OLDPHI(i,j+1) - OLDPHI(i,j-1)) * (OLDOMEGA(i+1,j) - OLDOMEGA(i-1,j)) ...
- (OLDPHI(i+1,j) - OLDPHI(i-1,j)) * (OLDOMEGA(i,j+1) - OLDOMEGA(i,j-1))) / (ds * ds) ...
+(1 / reynoldsNum) * (OLDOMEGA(i+1,j) + OLDOMEGA(i-1,j) + OLDOMEGA(i,j+1) + ...
OLDOMEGA(i,j-1) - 4 * OLDOMEGA(i,j)) / (ds * ds);
end
end
OMEGA(2:Nx+1,2:Ny+1) = OLDOMEGA(2:Nx+1,2:Ny+1) + dt * W(2:Nx+1,2:Ny+1);
time = time + dt;
for i = 1:Nx
for j = 1:Ny
x2d(i,j) = x1(i);
y2d(i,j) = x2(j);
end
end
%{
----------------------------------
U AND V CALCULATIONS
----------------------------------
%}
case 3
for i = 2:Nx+1
for j = 2:Ny+1
U(i,j) = (OLDPHI(i,j+1) - OLDPHI(i,j)) / (2 * ds);
V(i,j) = -(OLDPHI(i+1,j) - OLDPHI(i,j)) / (2 * ds);
U(:,Ny+2) = 1;
V(Nx+2,:) = 0.0;
end
end
%{
----------------------------------
PRESSURE CALCULATIONS
----------------------------------
%}
otherwise
for i = 2:Nx+1
for j = 2:Ny+1
PRESSURE(i,j) = (1/4) * (pressureOLD(i+1,j) + pressureOLD(i-1,j) + pressureOLD(i,j+1) ...
+ pressureOLD(i,j-1)) - (1/2) * (((((OLDPHI(i-1,j) - 2 * OLDPHI(i,j) + ...
OLDPHI(i+1,j)) / (ds^2)) * ((OLDPHI(i,j-1) - 2 * OLDPHI(i,j) + OLDPHI(i,j+1)) / (ds^2))) ...
- (OLDPHI(i+1,j+1) - OLDPHI(i+1,j-1) - OLDPHI(i-1,j+1) + OLDPHI(i-1,j-1)) / (4 * (ds^2))) * ds^2);
end
pressureOLD = PRESSURE;
end
end
end