% This program solves a convection-diffusion problem 

src = cinput('Enter the source ',0);%Source term defined by user.
example.source=src;

% SYSTEM RESULTING OF DISCRETIZING THE WEAK FORM
[K,f] = FEM_system_steady(X,T,referenceElement,example,src);


    % BOUNDARY CONDITIONS 
% Boundary conditions are imposed using Lagrange multipliers
nodes_y0 = [1:npx+1]';                           % Nodes on the boundary y=0
nodes_x1 = [2*(npx+1):npx+1:(npy+1)*(npx+1)]' ;     % Nodes on the boundary x=1
nodes_y1 = [npy*(npx+1)+npx:-1:npy*(npx+1)+1]' ;     % Nodes on the boundary y=1
nodes_x0 = [(npy-1)*(npx+1)+1:-(npx+1):npx+2]';     % Nodes on the boundary x=0

% nodes on which solution is u=1
nodesDir1 = [nodes_y1(end);nodes_x0( X(nodes_x0,2) > 1.5) ; nodes_x0( X(nodes_x0,2) == 1.5)] ; 

% nodes on which solution is u=0
nodesDir0 = [nodes_y0(end) ; nodes_x1];
% Boundary condition matrix
C = [nodesDir1, ones(length(nodesDir1),1);
     nodesDir0, zeros(length(nodesDir0),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  = reshape(X(:,1), npy+1, npx+1)'; 
yy  = reshape(X(:,2), npy+1, npx+1)'; 
sol = reshape(Temp, npy+1, npx+1)';  
surface(xx,yy,sol,'FaceColor','interp');
set(gca, 'xTick',0:0.25:2, 'yTick',0:0.25:3, 'FontSize',12)
xlabel('x','FontSize',14); 
ylabel('y','FontSize',14); 
zlabel('u','FontSize',14);
grid on; view(3)