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

clear; close all; clc


disp(' ')
disp('This program solves a convection-diffusion equation on [0,2]x[0,3]')
disp(' ')
disp('No source term is considered');

% PDE coefficients
%disp('Convection velocity is');
a_in = cinput('enter the velocity or the norm of velocity==',1);
velo = [a_in,0];
disp(velo);
a = norm(velo);
nu = cinput('Diffusion coefficient', 0.001);
disp(' ')
h =cinput('enter size of element',0.2);%%%size of element
dom = [0,2,0,3]; %%%this is specified domain [0,2]x[0,3]

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

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

%nx = input('Number of elements in each direction (multiple of 5)= ');
nx = abs(dom(1)-dom(2))/h;
%ny = (3*nx)/2; %for square elements, original nx=ny 
ny = abs(dom(3)-dom(4))/h;
[X,T] = CreateMesh(dom,nx,ny,referenceElement);
PlotMesh(T,X,'b-');
%h = 0.2; %%spatial discretization as given original =1/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] Artificial diffusion');
disp ('     [1] SUPG');
disp ('     [2] GLS')
method = cinput ('Choose a method for solving the problem: ',0);
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_gamma5 = [1:nx+1]';
% nodes_gamma4 = [2*(nx+1):nx+1:(ny+1)*(nx+1)]';
% nodes_gamma3 = [(ny+1)*(nx+1):-1:ny*(nx+1)+1]';
% nodes_gamma2 = [ny*(nx+1)+1:-(nx+1):((nx+1)*(ny+1)/2)+1]';
% nodes_gamma1 = [1+(ny-1)*ceil((nx+1)/2):-(nx+1):1]';

nodes_gamma1 = find(X(:,1)==0 & X(:,2) < 1.5);
nodes_gamma2 = find(X(:,1)==0 & X(:,2) >= 1.5);
nodes_gamma3 = find(X(:,2)==3);
nodes_gamma4 = find(X(:,1)==2);
nodes_gamma5 = find(X(:,2)==0);


% nodes on which solution is u=1     , 2
nodesDir1 = unique([nodes_gamma2]); 
% nodes on which solution is u=0      ,1
nodesDir0 = [nodes_gamma4];

% Boundary condition matrix
%   C = [nodesDir1, ones(length(nodesDir1),1);
%        nodesDir0, zeros(length(nodesDir0),1)];
%  C = [nodesDir1, 2*ones(length(nodesDir1),1);
%        nodesDir0, ones(length(nodesDir0),1)];

%% For part d) to impose Neumann BC for same solutions
 C = [nodesDir1, 2*ones(length(nodesDir1),1)];
 node=nodesDir0;
flux=[-0.0971125788
-0.1943035245
-0.1945386598
-0.194930644
-0.1954796927
-0.1961857481
-0.1970503551
-0.198068115
-0.1989760503
-0.1992181574
-0.1992220023
-0.1992211603
-0.1992213431
-0.1992213038
-0.1992213122
-0.0996106551];

f(node)=f(node)+flux;

%%
% C = [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), nx+1, ny+1); 
yy  = reshape(X(:,2), nx+1, ny+1); 
sol = reshape(Temp, nx+1, ny+1);  
surface(xx,yy,sol,'FaceColor','interp');
set(gca, 'xTick',0:0.25:1, 'yTick',0:0.25:1, 'FontSize',12)
xlabel('x','FontSize',14); 
ylabel('y','FontSize',14); 
zlabel('u','FontSize',14);
grid on; view(3)

