% Steady convection-diffussion-reaction problem

% Compute convection velocity
 switch a
     case 1
         ProblemData.ConvVel = [-1,0];
     case 2
         ProblemData.ConvVel = [-1e-3, 0];
     case 3
         ProblemData.ConvVel = [1e-3, 0];
 end
 
fprintf('\n')
disp ('The following methods can be used:');
disp ('     [1] Galerkin ');
disp ('     [2] Streamline Upwind Petrov-Galerkin (SUPG)');
disp ('     [3] Galerkin Least-Squares (GLS)');
ProblemData.method = input ('Choose a method for solving the problem: ');
disp('------------ END INPUT DATA --------------')

%% Obtain linear systema to solve
% Matrices
[K,f] = ComputeSystem (X,T,Element,ProblemData);

% Boundary conditions
nodes_y0 = (1: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+1)*(nx+1)+nx:-1:(ny+1)*(nx+1)+1)' ;  
%nodes_y1 = [ny*(nx+1)+nx:-1:ny*(nx+1)+1]';     % Nodes on the boundary y=1
nodes_x0 = (ny*(nx+1)+1:-(nx+1):nx+2)';     % Nodes on the boundary x=0

% Nodes on which solution is u=1
nodesDir1 = nodes_x0(X(nodes_x0,2) > 1.5 );

% Nodes on which solution is u=0
nodesDir0 = nodes_x1;

% Boundary condition matrix for Lagrange multipliers method
C = [nodesDir0, zeros(length(nodesDir0),1);
     nodesDir1, ones(length(nodesDir1),1)];

ndir = size(C,1);
neq  = size(f,1);
A = zeros(ndir,neq);
A(:,C(:,1)) = eye(ndir);
b = C(:,2);

%% SOLUTION OF THE LINEAR SYSTEM
% Entire matrix
Ktot = [K A';A zeros(ndir,ndir)];
ftot = [f;b];

sol = Ktot\ftot;
Temp = sol(1:neq);
multip = sol(neq+1:end);


%% POSTPROCESS
figure(2), clf
[xx,yy,sol] = MatSol(X,nx,ny,Temp);
surface(xx,yy,sol,-sol.^2,'FaceColor','interp');
view([-15,30])
xlabel('x','FontSize',14); 
ylabel('y','FontSize',14); 
zlabel('u','FontSize',14);
grid on;
set(gca,'FontSize',12)