
% This program solves a convection-diffusion-reaction problem 
% in a domain [0,2]x[0,3] 

% clear; clc
dom = [0,2,0,3];
switch method
    case 0
        methd= ['Case ' num2str(prob) ': Galerkin'];
        filename= ['Case' num2str(prob) 'Galerkin'];
    case 1
        methd= ['Case ' num2str(prob) ': GLS'];
        filename= ['Case' num2str(prob) 'GLS'];
    case 2
        methd= ['Case ' num2str(prob) ': SUPG'];
        filename= ['Case' num2str(prob) 'SUPG'];
end
disp(methd)
disp('This program solves a convection-diffusion-reaction equation on [0,1]x[0,1]')
% disp(' ')
% disp('No source term is considered');
% PDE coefficients
%disp('Convection velocity is');
% a_in = input('enter the velocity or the norm of velocity==');
velo = [a_in,0];
disp(velo);
a = norm(velo);
% nu = cinput('Diffusion coefficient', 0.01);
% disp(' ')
% sigma=input('enter the value of sigma =');
% source = cinput('source',0);


example.dom = dom; 
example.velo = velo; 
example.a = a; 
example.nu = nu; 
example.sigma = sigma; 

% Element type
elem = 0; p = 1; 
referenceElement = SetReferenceElement(elem,p); 

% nx = input('Number of elements in each direction (multiple of 5)= ');
% ny = nx; 

nx = ceil((dom(2)-dom(1))/hx);
ny = ceil((dom(4)-dom(3))/hy);
hx = (dom(2)-dom(1))/nx ;
hy = (dom(4)-dom(3))/ny ;
h  = max(hx,hy) ;

[X,T] = CreateMesh(dom,nx,ny,referenceElement);
% PlotMesh(T,X,'b-');
Pe = a*h/(2*nu);
disp(' ')
disp(strcat('Peclet number: ',num2str(Pe)))

% disp(' ')
% disp ('The following methods can be used:');
% disp ('     [0] Galerkin');
% disp ('     [1] GLS');
% disp ('     [2] SUPG');
% method = input ('Choose a method for solving the problem: ');
example.method = method; 
example.source = source; 
% SYSTEM RESULTING OF DISCRETIZING THE WEAK FORM
[K,f] = FEM_system(X,T,referenceElement,example);


% BOUNDARY CONDITIONS 
% Boundary conditions are imposed using Lagrange multipliers
nodes_y0 = [1:nx+1]';                           % Nodes on the boundary y=0
nodes_x1 = [2*(nx+1):nx+1:(ny+1)*(nx+1)]' ;     % Nodes on the boundary x=2
nodes_y1 = [ny*(nx+1)+nx:-1:ny*(nx+1)+1]' ;     % Nodes on the boundary y=3
nodes_x0 = [(ny-1)*(nx+1)+1:-(nx+1):nx+2]';     % Nodes on the boundary x=0
nodes = 1:max(max(T));
%%(NEUMANN and DIRECHLIT BC)%%
% nodes on which solution is u=1
nodesDir1 = nodes(X(:,2) > dom(4)/2 & (X(:,1) == dom(1) ))'; 
% nodes on which solution is u=0
nodesDir0 = nodes(X(:,1) == dom(2))';


%%%%      Boundary condition matrix  %%%%%

%%%%%% rho=1 in Bound.set=2 and rho=0 in Bound.set=4%%%%%%
 %C = [nodesDir1, ones(length(nodesDir1),1);
 % nodesDir0, zeros(length(nodesDir0),1)];

% % %%%%%% rho=2 in Bound.set=2 and rho=1 in Bound.set=4%%%%%%
%   C = [nodesDir1, 2*ones(length(nodesDir1),1);
%        nodesDir0, ones(length(nodesDir0),1)];
 
%%%%%% %%%% NEUMANN BC on Bound.set=4 %%%%%%%%%%%%%
 C = [nodesDir1, 2*ones(length(nodesDir1),1)];
 N = nodesDir0;

%(flux Calculated from Direchlit BC and imposed on nodes where Neumann Bound.set=4 )%
 known_flux = [  -0.0292
   -0.0583
   -0.0583
   -0.0584
   -0.0584
   -0.0584
   -0.0584
   -0.0584
   -0.0585
   -0.0585
   -0.0585
   -0.0586
   -0.0586
   -0.0587
   -0.0587
   -0.0588
   -0.0589
   -0.0589
   -0.0590
   -0.0591
   -0.0592
   -0.0593
   -0.0594
   -0.0595
   -0.0596
   -0.0596
   -0.0597
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0598
   -0.0299];

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

f(N) = f(N) + known_flux;


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

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


% POSTPROCESS
figure(), clf
xx  = reshape(X(:,1), nx+1, ny+1); 
yy  = reshape(X(:,2), nx+1, ny+1); 
sol = reshape(u, nx+1, ny+1);  
surface(xx,yy,sol,'FaceColor','interp');
set(gca, 'xTick',0:0.5:2, 'yTick',0:1:3, 'FontSize',14)
xlabel('x','FontSize',14); 
ylabel('y','FontSize',14); 
title(methd,'FontSize',16)
zlabel('u','FontSize',14);
grid on; view(3)
set(gcf,'color','white')

if sv == 1
    disp('Saving images')
    saveas(gcf,[filename,'.pdf']) ;
end