%=========================================================== 
%   Stokes Problem Question 2 with Stabilization:  Q1Q1
%===========================================================
%============================================================
clear; close all; clc
addpath('Func_ReferenceElement')
%===========================================================
% Definition Material parameters
%===========================================================
mu = 10^3;
%============================================================
%   Mesh settings different for velocity and pressure Q2Q1
%============================================================
Ntheta = 10;     % Number of elements on Theta direction
Nr = 20;         % Number of elements on Radius direction;
%-----------------------------------------------------------
[Xv,Tv,theta] = createMeshs(Nr,Ntheta);
elem = 0;        %  Quadrilateral elements
figure(1)
plotMeshQ2(Tv,Xv,elem);
%-----------------------------------------------------------
[Xp,Tp] = createMeshs(Nr,Ntheta); % Pressure
elem = 0;        % Bilinear quadratic elements
figure(2)
plotMeshQ1(Tp,Xp,elem);
%=============================================================
elemV = 0; degreeV = 1; degreeP = 1;
elemP = elemV; 
referenceElement = SetReferenceElementStokes(elemV,degreeV,elemP,degreeP);
%==========We are using Div.Sigman so use StokesSystem2=====
[K,G,f] = StokesSystem2(Xv,Tv,Xp,Tp,referenceElement);
K = mu*K; 
[ndofP,ndofV] = size(G); 

% Prescribed velocity degrees of freedom
nx = Ntheta;
ny = Nr;
% ==============Apply the boundary conditions==========================
[A_DirBC, b_DirBC, nDir, confined] = BC(nx,ny,ndofV,degreeV,theta);

% Total system of equations
if confined
   nunkP = ndofP-1;
   disp(' ')
   disp('Confined flow. Pressure on lower left corner is set to zero');
   G(1,:) = [];
else
   nunkP = ndofP;
end
%================================================
%               GLS stabilization 
%================================================
% GLS = cinput('Use GLS stabilization? (1:Yes,0:No)',0);
h=sqrt(2/nx^2);
GLS = 1;
if GLS == 1
    glsq = 'yes';
    tau = (0.01*(h^2/(4*mu))); 
    [TL,TR] = GLSMethod(Xp,Tp,tau,referenceElement);
    if confined
        TL = TL(2:end,2:end);
        TR = TR(2:end);
    end
else
    glsq = 'no';
    TL = zeros(nunkP);
    TR = zeros(nunkP,1);
end

Atot = [K   A_DirBC'  G'; 
     A_DirBC zeros(nDir,nDir) zeros(nDir,nunkP)
     G   zeros(nunkP,nDir)  -TL]; 
 
btot = [f ; b_DirBC; -TR];

sol = Atot\btot; 

velo = reshape(sol(1:ndofV),2,[])'; 

if confined
    pres = [0; sol(ndofV+nDir+1:ndofV+nDir+nunkP)]; 
else
	pres = sol(ndofV+nDir+1:ndofV+nDir+nunkP); 
end
%===============Post -Processing ===============================

nPt = size(Xv,1); 
figure; 
quiver(Xv(1:nPt,1),Xv(1:nPt,2),velo(1:nPt,1),velo(1:nPt,2));
hold on 
axis equal; axis tight

% PlotStreamlines(X,velo,dom); 

PlotResults(Xv,Tv,velo(:,1),referenceElement.elemV,referenceElement.degreeV)
title('Velocity component X')
PlotResults(Xv,Tv,velo(:,2),referenceElement.elemV,referenceElement.degreeV)
title('Velocity component Y')
if degreeP == 0
    PlotResults(Xp,Tp,pres,referenceElement.elemP,referenceElement.degreeP)
else
    PlotResults(Xp,Tp,pres,referenceElement.elemP,referenceElement.degreeP)
end
%=============== End of Post -Processing ==============================







