%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  This program allows to solve the convection-diffusion-reaction problem %
%                                                                         %
%     a.grad(u) - div(nu grad(u)) + sigma u = s in a reactangular domain  %
%                                                                         %
%     Omega = (0,2)x(0,3) sucha that                                      % 
%                                                                         %
%                             Gamma 3 = (0,2) x (3,3)                     %
%                                xxxxxxxxxxxxxxxx                         %
%                                o              |                         %
%                                o              |                         %
%     Gamma 2 = (0,0) x (3/2,3)  o              |                         %
%                                o              |                         %
%                                o              | Gamma 4 = (2,2) x (0,3) %
%                                /              |                         %
%                                /              |                         %
%     Gamma 1 = (0,0) x (0,3/2)  /              |                         %
%                                /              |                         %
%                                /              |                         %
%                                \\\\\\\\\\\\\\\\                         %
%                             Gamma 5 = (0,2) x (0,0)                     %
%                                                                         %
%    wih boundary conditions:                                             %
%            u = 1 on Gamma 2                                             %
%            u = 0 on Gamma 4                                             %
%                                                                         %
%    The user can also choose to solve the transient version of the       %
%    previous problem on the same domain.                                 %
%                                                                         %
%        u_t + a.grad(u) - div(nu grad(u)) + sigma u = s                  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Preamble
clear; close all; clc
disp('------------------------------------------------------------------------------------------')
disp('This program allows to solve a transient or steady convection-diffusion-reaction problem');
disp('on a rectangular domain (0,2) x (0,3) with Dirichlet and homogeneous Neumann boundary conditions.')
disp('-------------------------------------------------------------------------------------------')

%% Geometry
fprintf('\n')
disp('INTRODUCE GEOMETRIC DATA');

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

% Number of elements in each direction
nx = cinput('Number of elements on X direction',20); % default is 20 elements
ny = cinput('Number of elements on Y direction',20); % default is 20 elements

% Element type
elem = cinput('Type of element (0: quadrilateral, 1: triangles)',0); 
p = cinput('Interpolation degree',1); % Degree of interpolation
Element = SetReferenceElement(elem,p); %Obtain Gauss points, shape functions, etc.

% Matrices of nodal coordinates and connectivities
[X,T] = CreateMesh(dom,nx,ny,Element);
figure; PlotMesh(T,X,'b-');

%% Convection velocity
% Choose a velocity field
fprintf('\n')
disp('CONVECTION VELOCITY')
disp('------------------------------------------------------ ')
disp('Available velocity fields a = (a_x,a_y): ')
disp('[1] a = (-1,0) m/s')
disp('[2] a = (-10e-3,0) m/s ')
disp('[3] a = (10e-3,0) m/s')
a = cinput('Choose the convection velocity field to be used:  ',1);

%% Diffusion coefficient
fprintf('\n')
disp('DIFFUSION COEFFICIENT')
disp('--------------------------------------------------------')
disp('The only diffusion coefficient considered is nu = 10e-3')
ProblemData.nu = 1e-3;

%% Reaction coefficient
fprintf('\n')
disp('REACTION COEFFICIENT')
disp('------------------------------------------------------ ')
disp('Available reaction coefficients: ')
disp('[1] sigma = 0')
disp('[2] sigma = 1')
disp('[3] sigma = 10e-3')
reaction_coeff = input ('Choose a reaction coefficient:');
switch reaction_coeff
    case 1
        ProblemData.sigma = 0;
    case 2
        ProblemData.sigma = 1;
    case 3
        ProblemData.sigma = 1e-3;
end
%% Source term
fprintf('\n')
disp('SOURCE TERM')
disp('------------------------------------------------------ ')
disp('Choose a source term: ')
disp('[1] s = 0')
disp('[2] s = 1')
ProblemData.s = input('Choose a source term:');

%% Problem to solve
fprintf('\n')
disp('PROBLEM TO SOLVE')
disp('------------------------------------------------------ ')
disp('Which problem do you want to solve? ')
disp('[1] Transient')
disp('[2] Steady')
problem = input('Choose the problem to solve:');
if problem == 1
    run('TransientProblem')  
else
    run('SteadyProblem')
end