% This program solves a 2D steady convection-diffusion-reaction equation
% in a domain [0,2]x[0,3]
% with Dirichlet boundary conditions
% using the finite element method

clear; close all; clc;

disp(' ')
disp('This program solves a 2D steady convection-diffusion-reaction equation in [0,2]x[0,3]')
dom = [0,2,0,3];

%% Element type
% elem: 0=quadrilateral, 1=triangles
% p: 1=linear, 2=quadratic
disp(' ')
elem = cinput('Element shape: 0=quadrilateral, 1=triangles',0);
p = cinput('Element type: 1=linear, 2=quadratic',1);
referenceElement = SetReferenceElement(elem,p); 

disp(' ')
nx = cinput('Number of elements in x-direction',20);
ny = cinput('Number of elements in y-direction',30);
[X,T] = CreateMesh(dom,nx,ny,referenceElement);
figure; PlotMesh(T,X,'b-');

%% PDE coefficients
% velocity field
disp(' ')
disp('The following velocity fields can be considered');
disp('   [1] v(x,y) = (0.001,0)');
disp('   [2] v(x,y) = (-1,0)');
disp('   [3] v(x,y) = (-0.001,0)'); 
disp('   [4] v(x,y) = (cos(pi/6),sin(pi/6))'); 
c_velo = cinput('Choose the convection field to be used on the computation',1); 

switch c_velo
    case 1
        velo = [0.001,0];
        a = norm(velo);
    case 2
        velo = [-1,0];
        a = norm(velo);
    case 3
        velo = [-0.001,0];
        a = norm(velo);
    case 4
        velo = [cos(pi/6),sin(pi/6)];
        a = norm(velo);
end

% diffusion coefficient
disp(' ')
nu = cinput('Diffusion coefficient', 0.001);

% reaction coefficient
disp(' ')
disp('The following reaction coefficient can be considered');
disp('   [1] sigma = 1');
disp('   [2] sigma = 0.001');
disp('   [3] sigma = 0');
c_sigma = cinput('Choose the reaction coefficient to be used on the computation',1);

switch c_sigma
    case 1
        sigma = 1;
    case 2
        sigma = 0.001;
    case 3
        sigma = 0;
end

% source term
disp(' ')
disp('The following source term can be considered');
disp('   [1] s = 0');
disp('   [2] s = 1');
c_source = cinput('Choose the source term to be used on the computation',1);

switch c_source
    case 1
        source = 0;
    case 2
        source = 1;
end

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

%% Peclet
h = 1/nx;
Pe = a*h/(2*nu);
disp(' ')
disp(strcat('Peclet number: ',num2str(Pe)))

%% method
disp(' ')
disp ('The following methods can be used:');
disp ('     [0] Galerkin');
disp ('     [1] SUPG');
disp ('     [2] GLS');
method = input ('Choose a method for solving the problem: ');
example.method = method; 

%% System resulting of discretizing the weak form
[K,f] = FEM_system(X,T,referenceElement,example);

%% Boundary conditions
% Boundary conditions are imposed using Lagrange multipliers
if p<2
    nodes_x0 = [ny*(nx+1)+1:-(nx+1):1]';     % Nodes on the boundary x=0
    nodes_x2 = [(nx+1):nx+1:(ny+1)*(nx+1)]' ;     % Nodes on the boundary x=2
else
    nodes_x0 = [2*ny*(2*nx+1)+1:-(2*nx+1):1]';     % Nodes on the boundary x=0
    nodes_x2 = [(2*nx+1):2*nx+1:(2*ny+1)*(2*nx+1)]' ;     % Nodes on the boundary x=2
end


% 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_x2;
% 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
if p<2
    rs1=nx+1;
    rs2=ny+1;
else
    rs1=2*nx+1;
    rs2=2*ny+1;
end
xx  = reshape(X(:,1), rs1, rs2)'; 
yy  = reshape(X(:,2), rs1, rs2)'; 
sol = reshape(Temp, rs1, rs2)';  
surface(xx,yy,sol,'FaceColor','interp');
set(gca, 'xTick',0:0.5:2, 'yTick',0:0.5:3, 'FontSize',12)
xlabel('x','FontSize',14); 
ylabel('y','FontSize',14); 
zlabel('u','FontSize',14);
%title('Steady-state Case 3: Galerkin methods with GLS and quadratic element')
grid on; 
view(3)
%view(37.5,30)
%view(-135,-15)