% David O. Lignell
% 12/27/03
% Constant density, 2-D, laminar flow solver
% Constant viscosity, constant x and y grid
% Time accurate, explicit solver.  (or at least time advance to steady state)
% Explicit Euler is used
% Pressure solve, not pressure correction
% Allow for wall, inlet, outlet BC
% See make grid for setup

clc;
clear;

make_grid;

Re = 500;
mu = 1.0e-3;
nu = 1.0e-6;
rho = mu/nu;
v_in = Re*nu/W;
u_in = 0.0;
mass_in = (v_in^2+u_in^2)^0.5 * 4;       % grid specific
gx = 0.0;
gy = 0.0;

%Simulation control                      (stolen from Jeremy's soln)
ttime = 0.1*W^2/nu;                      %total time iterations
CFL = 1.0;                               %CFL constant
dtc = CFL*0.5*dx/v_in;                   %time step, convective
dtd = CFL*0.5*dx^2/nu;	    		     %time step, diffusive
dt = min(dtc,dtd)/1.0;                   %choose a dt
rtol = 1.0e-6;

ttime = ttime / 2;
% Create and initialize field variables

u_vel     = zeros(nx,ny);
v_vel     = u_vel;
pressure  = u_vel;
u_cc      = zeros(nx,ny);
v_cc      = u_cc;
v_vel(nx/2-1:nx/2+2,1:2) = v_in;

u_hat = zeros(nx,ny);
v_hat = zeros(nx,ny);

Ap_init = 1;                               % Flag to calc Ap in get_Ap once only
time = 0.0;
titer = 0;

while time < ttime
    time = time + dt;
    titer = titer + 1;
    
    fprintf('------------------------------------\n')
    fprintf('       Time = ')
    fprintf('%6.2f\n',time)
    fprintf('Iteration # = ')
    fprintf('%6.0i\n',titer)
    
    get_uv_hat2;
    get_Ap;                                    % Get pressure equation Ap * P = Bp matricies
    P = Ap\Bp;
    P = reshape(P,nx-2,ny-2);                  % P size here is nx-2, ny-2
    %P = P - sum(sum(P))/((nx-2)*(ny-2));      % Uncomment if Ap, Bp don't force a reference P
                                               % Code works with uncommented and Ap, Bp general, not sure why (walls?)
    % get u and v velocities at n+1
    
    for i=3:nx-1
        u_vel(i,2:ny-1) = u_hat(i,2:ny-1) + dt/rho/dx*( P(i-1-1,1:ny-2) - P(i-1,1:ny-2) );
    end        
    for j=3:ny-1
        v_vel(2:nx-1,j) = v_hat(2:nx-1,j) + dt/rho/dy*( P(1:nx-2,j-1-1) - P(1:nx-2,j-1) );
    end
    
    get_max_divergence;                       % Test continuity and print to screen
    
    for i=2:nx-1
        u_cc(i,2:ny-1) = 0.5*( u_vel(i,2:ny-1)+u_vel(i+1,2:ny-1) );
    end
    for j=2:ny-1
        v_cc(2:nx-1,j) = 0.5*( v_vel(2:nx-1,j)+v_vel(2:nx-1,j+1) );
    end
    KE(titer,1) = sum(sum( (u_cc.*u_cc + v_cc.*v_cc).^0.5));   % total KE as convergence indicator
    fprintf('KE = ');
    fprintf('%6.2e\n', KE(titer));
    
    %plot the stuff:            ( this part stolen from Jeremy's soln)
    if (mod(titer,30) == 0) 
        close all
        subplot(221), quiver(x',y',u_cc(2:nx-1,2:ny-1),v_cc(2:nx-1,2:ny-1));title('vel vectors')
        axis image
        subplot(222), plot(KE); title('Kinetic Energy');
        subplot(223), streamslice(u_cc',v_cc');title('stream lines')
        axis image
        subplot(224), contourf(v_cc',10);title('u-vel contour');
        axis fill
        pause(0.5)
        
    end
end

