% Continuation of main file for unsteady convection problems.
% Input of a vector can be a scalar or matlab expresion involving x and y.
% Example: 4*x+3*y.

addpath('Functions/steady')

nPt   = size(X,1);
nEl   = size(T,1);

fprintf('\nDefine equation parameters:\n')
ax    = cinput('Enter x component of velocity >> ',-1.d-3);
ay    = cinput('Enter y component of velocity >> ',0.d-0);
av    = compVel(X,[ax,ay]);
nu    = cinput('Enter diffusion parameter >> ',1.d-3);
sigma = cinput('Enter reaction parameter >> ' ,0.d-0);
s     = source(X,1);

a     = sqrt(ax^2+ay^2);
h     = max(max(X(:,1))/nx,max(X(:,2))/ny);
Pe    = a*h/(2*nu);

fprintf('\nSelect stabilization method:\n')
xDisc = cinput('[1] No stabilization\n[2] SUPG method\n[3] GLS method\n>> ',1);

% System creation
u         = zeros(nPt,1);
[M,K,C,f] = femMatrices(X,T,av,s,elem);
fn        = prescFlow  (X,T,TB,bType,bVal,elem);
aux       = prescBound (XB,bType,bVal,u);

Ks = C+nu*K+sigma*M;
fs = f+fn;

% Spatial discretization
switch xDisc
    case 1
        ST       = zeros(nPt);
        fST      = zeros(nPt,1);
    case 2
%         tau      = cinput('Enter stabilization parameter >> ',h/(2*a)/(1+1/Pe+h/(2*a)*sigma));
        tau      = cinput('Enter stabilization parameter >> ',h/(2*a)/(coth(Pe)-1/Pe));
        [fST,ST] = supgMethod(X,T,av,nu,sigma,s,tau,elem);
    case 3
%         tau      = cinput('Enter stabilization parameter >> ',h/(2*a)/(1+1/Pe+h/(2*a)*sigma));
        tau      = cinput('Enter stabilization parameter >> ',h/(2*a)/(coth(Pe)-1/Pe));
        [fST,ST] = glsMethod(X,T,av,nu,sigma,s,tau,elem);
end

nBd           = length(aux);
A             = zeros(nBd,nPt);
A(:,aux(:,1)) = eye(nBd);
b             = aux(:,2);

Kss = [Ks+ST transpose(A); A zeros(nBd)];
fss = [fs;b];

us  = Kss\fss;
u   = us(1:nPt);

% Plotting of the results
if (eDeg == 2)
    if (eType == 1);        TP = [T(:,1) T(:,2) T(:,3)];
    elseif (eType == 0);    TP = [T(:,1) T(:,2) T(:,3) T(:,4)];
    end
else
    TP = T;
end

figure
patch('faces',TP,'vertices',[X(:,1) X(:,2) u],'facevertexcdata',u,...
      'FaceColor','interp','EdgeColor',[0.5,0.5,0.5]);
colormap(jet(100));
zlim([min(u) max(u)])
xlabel('x')
ylabel('y')
view(3)