function [A, b, nDir,confined] = BC(X,dom,n,nx,ny,alpha)
% [A, b, nDir, confined] = BC(X,dom,n)
% Matrices to impose Dirichlet boundary conditions using Lagrange
% multipliers on a rectangular domain
% Input: 
%    X: nodal coordinates
%    dom: domain description [x1,x2,y1,y2]
%    n: number of velocity degrees of freedom
% Output:
%    A,b: matrix and r.h.s. vector to impose the boundary conditions using
%         Lagrange multipliers
%    nDir: number of prescribed degrees of freedom
%    confined: 
% nx = 10    ny = 20
% ------------------  Diego Monster --------------------------------------
% nX = 2*nx;  nY = 2*ny;
% 
% %tol = 1e-6; 
% %nodesX1 = ;
% %nodesX2 = find(abs(X(:,1)-x2) < tol); 
% nodesY1 = 1:nX+1; 
% nodesY2 = 1+(nX+1)*nY : (nY+1)*(nX+1); 
% 
% %nodesDirBC = unique([nodesX1; nodesX2; nodesY1; nodesY2]); 
% 
% nodesDirBC = unique([nodesY1; nodesY2]); 
% % confined flow (velocity is imposed for all the nodes in the boundary)
% 
% confined = 0; 
% 
% % number of prescribed degrees of freedom
% nDir = 2*length(nodesDirBC); 
% 
% % Degrees of freedom where the velocity is prescribed
% % (horizontal and vertical component)
% C = [2*nodesDirBC - 1; 2*nodesDirBC];
% %--------------------------------------------------------------------------
% %A = zeros(nDir,n); 
% %A(:,C) = eye(nDir); 
% 
% 
% C1=reshape(C,nDir/2,2); % Zaria piece
% C2 =reshape(C1',nDir,1);  %%number of components of dirichlet velocity % Zaria piece
% 
% A = zeros(nDir,n); % Zaria piece
% A(:,C2) = eye(nDir); %Zaria piexe 
% 
% 
% 
% %--------------------------------------------------------------------------
% % Imposed value (velocity is set to zero on the boundary).
%  
% 
% V = zeros(nDir,1);  
% % for Radius 15 (for 1 to 42)
% %V(1:2:2*(nX+1)-1,1) = -0.15*cos(atan(X(1:nX+1,2)./X(1:nX+1,1))+pi);
% 
% % for x (r = 15)
% V(1:2:2*(nx+1)-1,1) = -0.15*cos(atan(X(1:nx+1,2)./X(1:nx+1,1))+pi);
% V(2*(nx+1)+1:2:2*(nX+1)-1,1) = -0.15*cos(atan(X(nx+2:2*(nx+1)-1,2)./X(nx+2:2*(nx+1)-1,1)));
% % for y (r= 15)
% V(2:2:2*(nx+1),1)   = -0.15*sin(atan(X(1:nx+1,2)./X(1:nx+1,1))+pi);
% V(2*(nx+1)+2:2:2*(nX+1)+1,1) = -0.15*sin(atan(X(nx+2:2*(nx+1)-1,2)./X(nx+2:2*(nx+1)-1,1)));
% 
% % for Radius 25 (for 43 to end)
% n_s = 1+(nX+1)*nY;  n_int = n_s+(nx);  n_end = (nY+1)*(nX+1);
% 
% 
% %V(2*(nX+1)+1:2:end-1,1) = -0.30*cos(atan(X(1:nX+1,2)./X(1:nX+1,1))+pi);
% V(2*(nX+1)+1:2:2*(nX+1)+1+nX,1) = -0.30*cos(atan(X(n_s:n_int,2)./X(n_s:n_int,1))+pi);
% V(2*(nX+1)+3+nX:2:end-1,1) = -0.30*cos(atan(X(n_int+1:n_end,2)./X(n_int+1:n_end,1)));
% 
% 
% V(2*(nX+1)+2:2:2*(nX+1)+2+nX,1) = -0.30*sin(atan(X(n_s:n_int,2)./X(n_s:n_int,1))+pi);
% V(2*(nX+1)+4+nX:2:end,1) = -0.30*sin(atan(X(n_int+1:n_end,2)./X(n_int+1:n_end,1)));
% 
% %V(2*(nX+1)+2:2:end,1) = -0.30*sin(atan(X(1:nX+1,2)./X(1:nX+1,1))+pi);
% V(21,1)=0; V(22,1)=-0.15; V(63,1)=0; V(64,1)=-0.3;
% b = V;
%--------------------------------------------------------------------------
degree=2;
% confined flow (velocity is imposed for all the nodes in the boundary)
confined = 1; 
% number of prescribed degrees of freedom
Ntheta = nx;  Nr = ny;
nptheta = degree*Ntheta + 1;
npr = degree*Nr + 1;
% npt = nptheta*npr;

nodesDy0 = [1 : nptheta]';
% nodesDx1 = [2*(nptheta) : (nptheta) : (npr)*(nptheta-1)]';
% nodesDy1 = [(npr)*(nptheta) : -1 : (npr-1)*(nptheta)+1]';
nodesDy1 = [(npr-1)*(nptheta)+1:1:(npr)*(nptheta)]';
% nodesDx0 = [(npr-2)*(nptheta)+1 : -(nptheta) : nptheta+1]';
nodesDirBC = [nodesDy0; nodesDy1]; 
nDiry0 = 2*length(nodesDy0);
nDiry1 = 2*length(nodesDy1);

nDir = 2*length(nodesDirBC); 
% Degrees of freedom where the velocity is prescribed
% (horizontal and vertical component)
C = [2*nodesDirBC - 1; 2*nodesDirBC];
% A = zeros(nDir,n); 
% A(:,C) = eye(nDir); 

C1=reshape(C,nDir/2,2);
C2 =reshape(C1',nDir,1);  %%number of components of dirichlet velocity

A = zeros(nDir,n); 
A(:,C2) = eye(nDir); 



% Imposed value (velocity is set to zero on the boundary).
val_ut0 = 0;
val_ur0 = -0.15;
val_ut1 = 0;
val_ur1 = -0.3;

% b = [val_ut0*ones(nDiry0/2,1);...
%     val_ur0*ones(nDiry0/2,1);...
%     val_ut1*ones(nDiry1/2,1);...
%     val_ur1*ones(nDiry1/2,1)];
% vx0 = val_ut0*sin(pi/2-alpha) + val_ur0*cos(pi/2-alpha);
% vy0 = -val_ut0*cos(pi/2-alpha) + val_ur0*sin(pi/2-alpha);
% vx1 = val_ut1*sin(pi/2-alpha) + val_ur1*cos(pi/2-alpha);
% vy1 = -val_ut1*cos(pi/2-alpha) + val_ur1*sin(pi/2-alpha);

vx0 = val_ut0*cos(alpha) + val_ur0*sin(alpha);
vy0 = -val_ut0*sin(alpha) + val_ur0*cos(alpha);
vx1 = val_ut1*cos(alpha) + val_ur1*sin(alpha);
vy1 = -val_ut1*sin(alpha) + val_ur1*cos(alpha);

% b = [vx0'; vy0';...
%     vx1'  ; vy1'];
% b = [vx0'; vx1';...
%     vy0'  ; vy1'];
b1 = [vx0' vy0';...
    vx1'   vy1'];
b =reshape(b1',nDir,1);

