%=========================================================== 
%   Stokes Problem Question 2 without Stabilization: Q2Q1
%===========================================================
%============================================================
clear; close all; clc
addpath('Func_ReferenceElement')
%===========================================================
% Definition Material parameters
%===========================================================
mu = 10^3;
%============================================================
%   Mesh settings different for velocity and pressure Q2Q1
%============================================================

Nr = 20;     % Number of elements on Radius direction;
Ntheta = 10; % Number of elements on Theta direction
%-----------------------------------------------------------
[Xv,Tv,theta] = createMeshV(Nr,Ntheta); % Velocity
elem = 0;     %  Quadrilateral elements
figure(1)
plotMeshQ2(Tv,Xv,elem);
%-----------------------------------------------------------
[Xp,Tp] = createMesh(Nr,Ntheta); % Pressure
elem = 0;     % Bilinear quadratic elements
figure(2)
plotMeshQ1(Tp,Xp,elem);
%===========================================================
elemV = 0; degreeV = 2; degreeP = 1; % Q2Q1 element
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 
if confined
   nunkP = ndofP-1;
   disp(' ')
   disp('Confined flow. Pressure on lower left corner is set to zero');
   G(1,:) = [];
else
   nunkP = ndofP;
end

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

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
% Plot velocity component in X
PlotResults(Xv,Tv,velo(:,1),referenceElement.elemV,referenceElement.degreeV)
title('Velocity component X')
% Plot velocity component in Y
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 ===========================







