
%Solver of a convect-diff-react equation; domain [0,2]x[0,3] , linear
%elements in 2D

clear, close all, home


global velo  diffusion  reaction  h  method

disp(' ')
disp('This program solves a convection-diffusion-reaction equation on [0,2]x[0,3]')
disp(' ')
disp('Source term is constant and can be changed in the function SourceTermn');
disp('Velocity is skew to the mesh and its modulus can be chosen, and direction'); 
disp('can also be chosen. It came as default with direction [cos(pi/3),sin(pi/3)]*a. It has been done using that default value. ')
a = input('Velocity module =  ');

velo = [cos(pi/3),sin(pi/3)]*a;
disp(velo)
diffusion = input('Diffusion coefficient =  ');
reaction = input('Reaction coefficient =  ');

disp(' we have 10 elements in x direction and 15 in y direction. It can be changed in-code ')
% nx = input('Elements in each direction = ');
% ny = nx; 
%h=0.2/1;
nx=10;
ny=15;
h = 2/nx;
%h=0.002;
Pe= a*h/(2*diffusion);
disp(' ')
disp(strcat('Peclet number: ',num2str(Pe)))

disp(' ')
disp ('The following methods can be used:');
disp ('     [1] Galerkin');
disp ('     [2] Streamline Upwind Petrov-Galerkin (SUPG)');
disp ('     [3] Galerkin Least-Squares (GLS)');
% disp ('     [4] SGS');
method = input ('Choose a method for solving the problem: ');


% MESH GENERATION
% Nodal coordinates and connectivities
[X,T] = CreateMesh(0,2,0,3,nx,ny);

% NUMERICAL INTEGRATION
% Gauss points. Available options: 4 gauss points. There are other codes
% with other gauss points implementations. We use 4 as default.
    ngaus = 4;
% Quadrature - only n=4
    [pospg,wpg] = Quadrature(ngaus);
% Shape functions. Only linear elements are available. Other codes
% available have the quadratic elements available. (9 nodes quadratic elements)
    [N,Nxi,Neta] = ShapeFunc(pospg);

% SYSTEM OBTAINED DISCRETIZING THE WEAK FORM. Depending on the method this
% will 
    [K,f] = CreateMatrix (X,T,pospg,wpg,N,Nxi,Neta);

% BOUNDARY CONDITIONS
% Essential boundary conditions are imposed on the whole boundary
% 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=1
nodes_y1 = [ny*(nx+1)+nx:-1:ny*(nx+1)+1]';      % Nodes on the boundary y=1
nodes_x0 = [(ny-1)*(nx+1)+1:-(nx+1):nx+2]';     % Nodes on the boundary x=0
%we require the division so we can have the different specified areas of
%boundary conditions.
nodes_x0_1 = nodes_x0(1:length(nodes_x0)/2);     % Nodes on the boundary x=0
nodes_x0_2 = nodes_x0(length(nodes_x0)/2:end);     % Nodes on the boundary x=0

% nodes_DC = [nodes_y0; nodes_x1; nodes_y1; nodes_x0];
nodes_DC=nodes_x1;
%new
nodes_DC_1=nodes_x0_1;
%
nodes=[nodes_DC;nodes_DC_1];
x1 = X(nodes_DC,1); y1 = X(nodes_DC,2);
x2 = X(nodes_DC_1,1); y2 = X(nodes_DC_1,2);
c=[nodes_DC,0.5*x1];

d=[nodes_DC_1,2*ones(length(x2),1)];
C=[c;d];

x = X(nodes,1); y = X(nodes,2);
% x = X(nodes_DC,1); y = X(nodes_DC,2);
% C = [nodes_DC,0*x];

%new


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(1),clf
%colormap (gray);
[xx,yy,sol] = MatSol(X,nx,ny,Temp);
surface(xx,yy,sol,-sol.^2,'FaceColor','interp');
view([-15,30])
xlabel('x','FontSize',12); 
ylabel('y','FontSize',12); 
zlabel('u','FontSize',12);
grid on;
set(gca,'FontSize',12)




