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

clear; close all; clc


disp(' ')
disp('This program solves a convection-diffusion-reaction equation on [0,2]x[0,3]')

% PDE coefficients
a = input('Velocity module =  ');
velo = [1,0]*a;
disp(velo)
nu = cinput('Diffusion coefficient', 0.01);
disp(' ')
sigma = cinput('Reaction coefficient', 1);
disp(' ')

dom = [0,2,0,3]; 

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 x direction (multiple of 5)= ');
ny = input('Number of elements in y direction (multiple of 5)= ');
[X,T] = CreateMesh(dom,nx,ny,referenceElement);
PlotMesh(T,X,'b-');

h = 2/nx;
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] 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
nodes_y0 = [1:nx+1]';                           % Nodes on the boundary y=0
nodes_x2 = [2*(nx+1):nx+1:(ny+1)*(nx+1)]' ;     % Nodes on the boundary x=2
nodes_y3 = [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

%If we want to obtain the same solution applying Neumann and Dirichlet
%boundary conditions, first we have to obtain the reactions 
%applying Dirichlet boundary conditions.

BC = input ('Boundary condition on Gamma4 (1:Neumann, 2: Dirichlet):  ');
if BC == 1   
% nodes on Gamma2
nodesDir1 = [nodes_x0( X(nodes_x0,2) > 1.5); nodes_y3(length(nodes_y3))];
% nodes on Gamma4
nodesDir0 = [nodes_x2; nodes_y0(length(nodes_y0))];

% Boundary condition matrix
C = [nodesDir1, 2*ones(length(nodesDir1),1)];
% Neumman boundary condition 
BC_Neumann=load('BC_Neumann.txt');
f(nodesDir0) = f(nodesDir0) - BC_Neumann;

elseif BC == 2
    % nodes on Gamma2
    nodesDir1 = [nodes_x0( X(nodes_x0,2) > 1.5 ); nodes_y3(length(nodes_y3))];
    % nodes on Gamma4
    nodesDir0 = [nodes_x2; nodes_y0(length(nodes_y0))];
    % Boundary condition matrix
    %For problem 1b
    C = [nodesDir1, ones(length(nodesDir1),1); 
         nodesDir0, zeros(length(nodesDir0),1)];
     
    %For problem 1d
    %C = [nodesDir1, 2*ones(length(nodesDir1),1); 
         %nodesDir0, ones(length(nodesDir0),1)];
end

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);

if BC == 2
  fi = fopen('BC_Neumann.txt', 'w'); 
  for k=1:length(nodesDir0)
    fprintf(fi, '%f \n', multip(length(nodesDir1)+k)); 
  end 

  fclose(fi);
end

% POSTPROCESS
figure(2), clf
[xx,yy,sol] = MatSol(X,nx,ny,Temp);
% xx  = reshape(X(:,1), ny+1, nx+1)'; 
% yy  = reshape(X(:,2), ny+1, nx+1)'; 
% sol = reshape(Temp, ny+1, nx+1)';  
surface(xx,yy,sol,-sol.^2,'FaceColor','interp');
view([-15,30])
%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)


