function f = Crevect_phi(X,IEN,elem,velo) 
% f = Crevect_phi(X,IEN,elem,velo) 
% This function computes the vorticity vector v 
% necessary to plot streamlines
% Input:    X:    matrix of nodal coordinates 
%           IEN:  connectivities
%           elem: type of element (0: quadrilateral, 1:triangle) 
%           velo: velocity field
%


% Number of elements (numel) and
% number of element nodes (nen)
[numel,nen] = size(IEN); 

% Total number of nodes (numnp) and degrees of freedom (nunk)
if elem == 1 & nen == 4         % MINI element
    numnp = size(X,1) + numel;  
else
    numnp = size(X,1);     
end
nunk  = 2*numnp; ndofn = 2*nen;

% Number of Gauss points (ngaus) and
% number of points describing geometry (ngeom)
if elem == 0
    if nen == 4
        ngaus = 4; ngeom = 4;
    elseif nen == 9
        ngaus = 9; ngeom = 9;
    end
else
    if nen == 3
        ngaus = 4; ngeom = 3;
    elseif nen == 6 
        ngaus = 12; ngeom = 6;
    elseif nen == 4 
        ngaus = 12; ngeom = 3;
    end
end

% Numerical quadrature and shape functions
[pospg,wpg] = Quadrature(elem,ngaus);
[N,Nxi,Neta] = ShapeFunc(elem,nen,pospg);

f = zeros(numnp,1); 

% Loop on elements
for ielem = 1:numel 
    Te = IEN(ielem,:);
    Xe = X(IEN(ielem,1:ngeom),:); 
    Ve = velo(IEN(ielem,:),:); 
    fe = zeros(nen,1);
    % loop of Gauss points
    for igaus = 1:ngaus
        N_igaus    = N(igaus,:);  
        Nxi_igaus  = Nxi(igaus,:);   
        Neta_igaus = Neta(igaus,:);
        
        Jacob = [Nxi_igaus(1:ngeom)*(Xe(:,1))	Nxi_igaus(1:ngeom)*(Xe(:,2))   
                 Neta_igaus(1:ngeom)*(Xe(:,1))	Neta_igaus(1:ngeom)*(Xe(:,2))];
        
        dvolu = wpg(igaus)*det(Jacob); 
        
        res = Jacob\[Nxi_igaus;Neta_igaus]; 
        Nx = res(1,:); Ny = res(2,:);
        
        vy_x = Nx*Ve(:,2);
        vx_y = Ny*Ve(:,1);
        f_igaus = vx_y - vy_x;
        fe = fe + (N_igaus)'*f_igaus*dvolu;    
    end
    % Assembly
    f(Te) = f(Te) + fe; 
    clear fe;
end 

