function [XP,TP] = CreatePresNod(elem,nenP,X,T,npx,npy)
% [XP,TP] = CreatePresNod(elem,nenP,X,T,npx,npy)
% Creates a mesh for the pressure field with nenP nodes per element,
% based on a velocity mesh (X,T)
%
% Input:    
%   elem:       element type (0 for quadrilaterals, 1 for triangles)
%   nenP:       number of element nodes
%   X,T:        nodal coordinates and connectivities of the velocity mesh
%   npx,npy:    number of nodes on each direction 
%
% Output:   
%   XP:         nodal coordinates forthe pressure mesh
%   TP:         connectivities of the pressure mesh
% 


[numel,nen] = size(T); 
numnp = size(X,1);

nx = npx-1;  ny = npy-1;

if elem == 0  % Quadrilateral
    if nenP == 1  % P0
        XP = zeros(numel,2); TP = zeros(numel,nenP);
        for ielem=1:numel
            XP(ielem,:) = mean(X(T(ielem,:),:));
            TP(ielem)   = ielem;
        end        
    elseif nenP == 4 
        if nen == 4  % Q1Q1
            XP = X; TP = T;
        elseif  nen==9 % Q2Q1
            % Only for structured meshes!!!
            npx=nx/2+1; npy=ny/2+1;
            XP = zeros(npx*npy,2); TP = zeros(numel,nenP);
            for irow=1:npy
                irowodd = 2*(irow-1)+1;
                XP((irow-1)*npx+1:irow*npx,:) = X((irowodd-1)*(nx+1)+1:2:irowodd*(nx+1),:);
            end
            for i=1:ny/2
                for j=1:nx/2
                    ielem = (i-1)*nx/2+j;
                    inode = (i-1)*(npx)+j;
                    TP(ielem,:) = [inode inode+1 inode+(npx+1) inode+(npx)];
                end   
            end
        end
    elseif nenP==9 & nen==9 % Q2Q2
        XP = X; TP = T;
    end
elseif elem == 1   % Triangles
    if nenP == 1 %P0
        XP = zeros(numel,2); TP = zeros(numel,nenP);
        for ielem=1:numel
            XP(ielem,:) = mean(X(T(ielem,:),:));
            TP(ielem)   = ielem;
        end
    elseif nenP == 3
        if nen == 3  % P1P1
            XP = X; 
            TP = T;
        elseif nen == 4  % MINI (P1+P1)
            XP = X(1:(nx+1)*(ny+1),:);
            TP = zeros(nx*ny,3); TP = T(:,1:3);
        elseif nen == 6  % P2P1
            npx=nx/2+1; npy=ny/2+1;
            XP = zeros(npx*npy,2);
            TP = zeros(nx*ny/4,3);
            for irow=1:npy
                irowodd = 2*(irow-1)+1;
                XP((irow-1)*npx+1:irow*npx,:) = X((irowodd-1)*(nx+1)+1:2:irowodd*(nx+1),:);
            end
            for i=1:ny/2
                for j=1:nx/2
                    ielem = 2*((i-1)*(nx/2)+j)-1;
                    inode = (i-1)*(npx)+j;
                    TP(ielem,:) = [inode   inode+1   inode+(npx)];
                    TP(ielem+1,:) = [inode+1   inode+1+npx   inode+npx];
                end
            end
            TP(1,:) = [1  npx+2   npx+1];
            TP(2,:) = [1     2    npx+2];
            aux = size(TP,1);
            TP(aux,:) =     [npx*(npy-1)-1   npx*(npy-1)    npx*npy];
            TP(aux-1,:)   = [npx*(npy-1)-1     npx*npy     npx*npy-1];
        end
    elseif nenP == 6 & nen == 6  % P2P2
        XP = X;
        TP = T;
    end
end
