% David O. Lignell 12/29/03
% u hat is basically the rhs (all but unsteady term) of the mom eqn, without pressure, 
%   and with the old velocity
% e.g. u(n+1) = uhat(n) + dt/rho/dx*(Pw-Pe)
% So, the algorithm gets uhat, then plugs the above into continuity and solves P, 
%    then solves the above for u(n+1)
%
% This techniqe is very simple and straight forward.
% Every cell in FLOW domain has the relevant face velocities and derivatives computed
% For boundaries, just make sure the face value of vel and dv/dx are correct
% The outlet is treated by assuming u is 0, and v is the adjacent value (upstream) + a velocity
%    Correction to force mass conseration.  Note that this is done immediately at time = 0.
% Alternately, we could extrapolate the velocity some way, then apply a correction.
% Note that this is not the same as assuming the gradient of velocity is zero

v_vel(2:nx-1,ny) = v_vel(2:nx-1,ny-1);               % Set the v-vel at the outlet
u_vel(3:nx-1,ny) = 0.0;
mass_out = sum(v_vel(2:nx-1,ny));
v_corr = (mass_in-mass_out)/(nx-2);
v_vel(2:nx-1,ny) = v_vel(2:nx-1,ny)+v_corr;
mass_out = sum(v_vel(2:nx-1,ny));
if abs((mass_in-mass_out)/mass_in) > 1.0e-6;
    fprintf('error in outlet velocity');
end

for i=3:nx-1                               % For u cells
    for j=2:ny-1
        
        ue    = 0.5*(u_vel(i+1,j)+u_vel(i,j));
        uw    = 0.5*(u_vel(i-1,j)+u_vel(i,j));
        un    = 0.5*(u_vel(i,j+1)+u_vel(i,j));
        us    = 0.5*(u_vel(i,j-1)+u_vel(i,j));
        vn    = 0.5*(v_vel(i,j+1)+v_vel(i-1,j+1));
        vs    = 0.5*(v_vel(i-1,j)+v_vel(i,j));
        dudxe = (u_vel(i+1,j)-u_vel(i,j))/dx;
        dudxw = (u_vel(i,j)-u_vel(i-1,j))/dx;
        dudyn = (u_vel(i,j+1)-u_vel(i,j))/dy;
        dudys = (u_vel(i,j)-u_vel(i,j-1))/dy;
        dvdxn = (v_vel(i,j+1)-v_vel(i-1,j+1))/dx;
        dvdxs = (v_vel(i,j)-v_vel(i-1,j))/dx;
        
        if j == ny-1
            un    = u_vel(i,j+1);
            dudyn = dudyn * 2.0;
        elseif j == 2
            us    = u_vel(i,j-1);
            dudys = dudys*2.0;
        end
        
        
        u_hat(i,j) = ( rho*dy*(uw*uw-ue*ue) + rho*dx*(us*vs-un*vn) + ...
                     2.0*mu*dy*(dudxe-dudxw) + mu*dx*(dudyn-dudys) + ...
                     mu*dx*(dvdxn-dvdxs) + ...
                     rho*gx*dx*dy ) * dt/(rho*dx*dy) + u_vel(i,j);
                 
    end
end
  
for i=2:nx-1                                          % v cells
    for j=3:ny-1
        
        vn    = 0.5*(v_vel(i,j+1)+v_vel(i,j));
        vs    = 0.5*(v_vel(i,j-1)+v_vel(i,j));
        ve    = 0.5*(v_vel(i+1,j)+v_vel(i,j));
        vw    = 0.5*(v_vel(i-1,j)+v_vel(i,j));
        ue    = 0.5*(u_vel(i+1,j)+u_vel(i+1,j-1));
        uw    = 0.5*(u_vel(i,j)+u_vel(i,j-1));
        dvdyn = (v_vel(i,j+1)-v_vel(i,j))/dy;            % is zero at j=ny-1
        dvdys = (v_vel(i,j)-v_vel(i,j-1))/dy;
        dvdxe = (v_vel(i+1,j)-v_vel(i,j))/dx;
        dvdxw = (v_vel(i,j)-v_vel(i-1,j))/dx;
        dudye = (u_vel(i+1,j)-u_vel(i+1,j-1))/dy;
        dudyw = (u_vel(i,j)-u_vel(i,j-1))/dy;
   
        if i == 2
            vw    = v_vel(i-1,j);
            dvdxw = dvdxw * 2.0;
        elseif i == nx-1
            ve    = v_vel(i+1,j);
            dvdxe = dvdxe * 2.0;
        end

        v_hat(i,j) = v_vel(i,j) + dt/(rho*dx*dy) * ...
            ( rho*dx*(vs*vs-vn*vn) + rho*dy*(vw*uw-ve*ue) + ...
            2.0*mu*dx*(dvdyn-dvdys) + mu*dy*(dvdxe-dvdxw) + ...
            mu*dy*(dudye-dudyw) + ...
            rho*gy*dx*dy );
    end
end
