function [Accd, bccd] =  BC(nx,ny,nunk,X,p)
% % [Accd, bccd] =  BC(nx,ny,nunk)
% % This function creates matrices Accd and bccd to impose homogeneous 
% % Dirichlet boudary conditions using Lagrange multipliers method.
% % 
% %   nx,ny:  number of elements on each direction
% %   nunk:   number of degrees of freedom for the velocity field
% 
% 
% % Nodes on the Dirichlet boundary
% nodesDy0 = [1 : nx+1]';                             % y = 0
% nodesDx1 = [2*(nx+1) : nx+1 : (ny+1)*nx]';          % x = 1
% nodesDx0 = [(ny-1)*(nx+1)+1 : -(nx+1) : nx+2]';     % x = 0
% nodesDy1 = [(ny+1)*(nx+1) : -1 : ny*(nx+1)+1]';     % y = 1
% nodesD   = [nodesDy0;nodesDx1;nodesDx0;nodesDy1];
% 
% % Imposed boundary conditions
% C = [nodesD, zeros(size(nodesD))];
% 
% % Boundary conditions' matrix
% nDir = size(C,1);
% Accd = zeros(nDir,nunk);
% Accd(:,C(:,1)) = eye(nDir);
% % Boundary conditions' vector
% bccd = C(:,2);
if p==1

    h=2/nx;
    % nodes_y0 = [1:nx+1]'                          ; % Nodes on the boundary y=0
    % nodes_x1 = [2*(nx+1):nx+1:(ny+1)*(nx+1)]'     ;% Nodes on the boundary x=1
    % nodes_y1 = [ny*(nx+1)+nx:-1:ny*(nx+1)+1]'     ;% Nodes on the boundary y=1
    % nodes_x0 = [(ny-1)*(nx+1)+1:-(nx+1):nx+2]'     ;% Nodes on the boundary x=0


    nodes_y0 = [2:nx]'                          ; % Nodes on the boundary y=0
    nodes_x1 = [(nx+1):nx+1:(ny+1)*(nx+1)]'     ;% Nodes on the boundary x=1
    nodes_y1 = [ny*(nx+1)+nx:-1:ny*(nx+1)+2]'     ;% Nodes on the boundary y=1
    nodes_x0 = [(ny)*(nx+1)+1:-(nx+1):1]'     ;% Nodes on the boundary x=0

    %we require the division so we can have the different specified areas of
    %boundary conditions.
    nodes_x0_1 = nodes_x0(1:length(nodes_x0)/2) ;    % Nodes on the boundary x=0
    nodes_x0_2 = nodes_x0(length(nodes_x0)/2+1:end);     % Nodes on the boundary x=0
    % nodes_DC = [nodes_y0; nodes_x1; nodes_y1; nodes_x0];
    nodes_DC=nodes_x1;
    %new
    nodes_DC_1=nodes_x0_1;
    %
    nodes=[nodes_DC;nodes_DC_1];
    x1 = X(nodes_DC,1); y1 = X(nodes_DC,2);
    x2 = X(nodes_DC_1,1); y2 = X(nodes_DC_1,2);
    c=[nodes_DC,0*x1];

    d=[nodes_DC_1,ones(length(x2),1)];

    C=[c;d];
    % 
    % x = X(nodes,1); y = X(nodes,2);
    % x = X(nodes_DC,1); y = X(nodes_DC,2);
    % C = [nodes_DC,0*x];
    % Boundary conditions' matrix
    nDir = size(C,1);
    Accd = zeros(nDir,nunk);
    Accd(:,C(:,1)) = eye(nDir);
    % Boundary conditions' vector
    bccd = C(:,2);
elseif p==2
%     nodes_y0 = [2:2*nx]'                          ; % Nodes on the boundary y=0
    nodes_x1 = [(2*nx+1):2*nx+1:(2*ny+1)*(2*nx+1)]'     ;% Nodes on the boundary x=1
%     nodes_y1 = [ny*(nx+1)+nx:-1:ny*(nx+1)+2]'     ;% Nodes on the boundary y=1
    nodes_x0 = [(ny)*(nx+1)+1:-(nx+1):1]'     ;% Nodes on the boundary x=0
    nodes_x0_1 = nodes_x0(1:length(nodes_x0)/2) ;    % Nodes on the boundary x=0
    nodes_x0_2 = nodes_x0(length(nodes_x0)/2+1:end);     % Nodes on the boundary x=0
    % nodes_DC = [nodes_y0; nodes_x1; nodes_y1; nodes_x0];
    nodes_DC=nodes_x1;
    %new
    nodes_DC_1=nodes_x0_1;
    %
    nodes=[nodes_DC;nodes_DC_1];
    x1 = X(nodes_DC,1); y1 = X(nodes_DC,2);
    x2 = X(nodes_DC_1,1); y2 = X(nodes_DC_1,2);
    c=[nodes_DC,0*x1];

    d=[nodes_DC_1,ones(length(x2),1)];

    C=[c;d];
    nDir = size(C,1);
    Accd = zeros(nDir,nunk);
    Accd(:,C(:,1)) = eye(nDir);
    % Boundary conditions' vector
    bccd = C(:,2);
end








% h=0.1/1;
% nx=10*1;
% ny=15*1;
% 
% nodes_y0 = [1:nx+1]';                           % Nodes on the boundary y=0
% nodes_x1 = [2*(nx+1):nx+1:(ny+1)*(nx+1)]';      % Nodes on the boundary x=1
% nodes_y1 = [ny*(nx+1)+nx:-1:ny*(nx+1)+1]';      % Nodes on the boundary y=1
% nodes_x0 = [(ny-1)*(nx+1)+1:-(nx+1):nx+2]';     % Nodes on the boundary x=0
% %we require the division so we can have the different specified areas of
% %boundary conditions.
% nodes_x0_1 = nodes_x0(1:length(nodes_x0)/2);     % Nodes on the boundary x=0
% nodes_x0_2 = nodes_x0(length(nodes_x0)/2:end);     % Nodes on the boundary x=0
% 
% % nodes_DC = [nodes_y0; nodes_x1; nodes_y1; nodes_x0];
% nodes_DC=nodes_x1;
% %new
% nodes_DC_1=nodes_x0_1;
% %
% nodes=[nodes_DC;nodes_DC_1];
% x1 = X(nodes_DC,1); y1 = X(nodes_DC,2);
% x2 = X(nodes_DC_1,1); y2 = X(nodes_DC_1,2);
% c=[nodes_DC,0*x1];
% 
% d=[nodes_DC_1,ones(length(x2),1)];
% C=[c;d];
% 
% x = X(nodes,1); y = X(nodes,2);
% % x = X(nodes_DC,1); y = X(nodes_DC,2);
% % C = [nodes_DC,0*x];
% 
% %new
% 
% nDir = size(C,1); 
% ndir = size(C,1);
% neq  = size(X,1);
% A = zeros(ndir,neq);
% A(:,C(:,1)) = eye(ndir);
% b = C(:,2);

