% This program solves a one-dimensional transient convection equation
%        u_t + a u_x = 0
% with Dirichlet boundary conditions using the finite element method.

clear; close all; clc


disp(' ')
disp('This program solves a transient convection equation')
disp('         u_t + a u_x = 0')

% Reference element: numerical quadrature and shape functions
p = 1; 
referenceElement = SetRefereceElement(p); 

% Spatial discretization
dom = [0,1]; 
example.dom = dom;  % Computational domain
disp(' ')
nElem = cinput('Number of elements',50);  
nPt = nElem + 1;
if p ~= 1
    warning('This program is only working for linear elements'); 
end
h = (dom(2) - dom(1))/nElem;
X = (dom(1):h:dom(2))'; 
T = [1:nPt-1; 2:nPt]';   

% Initial condition u0
problem = 1;  
u0 = InitialCondition(X,problem);
    

% Time discretization
tEnd = cinput('End time', 0.3);
nStep = cinput('Numnber of time-steps', 100);
dt = tEnd / nStep; 
Courant = 0.5;
a=Courant*h/dt;
disp(['Courant number: ', num2str(Courant)]); 


% Method used for solving the problem
disp(' ')
disp ('The problem can be solved using one of the following methods: ');
disp ('    [1] Lax-Wendroff + Galerkin')
disp ('    [2] Lax-Wendroff with lumped mass matrix + Galerkin')
disp ('    [3] Crank-Nicolson + Galerkin')
disp ('    [4] Crank-Nicolson with lumped mass matrix + Galerkin')
disp ('    [5] Two step - Third order Taylor-Galerkin + Galerkin')
method = input('Method = ');

[M,K,C] = FEM_matrices(X,T,referenceElement); 

[A,B,methodName] = System(method,M,K,C,a,dt); 

% Reduced system to impose du(0) = 0: 
ind_unk = 2:nPt; 
A = A(ind_unk,ind_unk); 
if problem == 4
    f = [B(2,1); zeros(nPt-2,1)];
else
    f = zeros(nPt-1,1); 
end
B = B(ind_unk,ind_unk); 

% Solution at each time step
u = zeros(nPt,nStep+1); 
u(:,1) = u0;
if problem == 4
    u(1,:) = 1; 
end

for n = 1:nStep
    Du = A\(B*u(ind_unk,n) + f); 
    u(ind_unk,n+1) = u(ind_unk,n) + Du; 
end


figure(1); clf;
plot(X,u(:,1),'k--', X,u(:,nStep+1),'r','LineWidth',1.5);
l = legend('Initial condition', [methodName, ' solution at t=',num2str(tEnd)]); 
set(l,'FontSize',24,'Location','NorthWest')
if problem == 1 || problem == 4
    axis([dom(1), dom(2),-0.25,1.25])
    set(gca,'FontSize',16,'YTick',-0.25:0.25:1.25)
else
    axis([dom(1), dom(2),-1.25,1.25])
    set(gca,'FontSize',16,'YTick',-1:0.5:1)
end
set(gcf,'color',[1,1,1])


figure(2); clf;
xx = linspace(X(1),X(end),2*nElem+1); 
uu = InitialCondition(xx - a*tEnd,problem);
plot(xx,uu,'k--', X,u(:,nStep+1),'r','LineWidth',1.5);
l = legend('Exact solution', [methodName, ' solution']); 
set(l,'FontSize',24,'Location','NorthWest')
if problem == 1 || problem == 4
    axis([dom(1), dom(2),-0.25,1.25])
    set(gca,'FontSize',16,'YTick',-0.25:0.25:1.25)
    v = axis; 
    text(v(1)+0.15,v(3)+0.15,['C=',num2str(Courant)],'FontSize',32)
else
    axis([dom(1), dom(2),-1.25,1.25])
    set(gca,'FontSize',16,'YTick',-1:0.5:1)
    v = axis; 
    text(v(1)+0.15,v(3)+0.25,['C=',num2str(Courant,'%3.1f')],'FontSize',32)
end
set(gcf,'color',[1,1,1])
title('Solution at t=0.3') 

figure(3); clf;
xx = linspace(X(1),X(end),2*nElem+1); 
uu = InitialCondition(xx - a*tEnd,problem);
plot(xx,uu,'k--', X,u(:,1),'r','LineWidth',1.5);
l = legend('Exact solution', [methodName, ' solution']); 
set(l,'FontSize',24,'Location','NorthWest')
if problem == 1 || problem == 4
    axis([dom(1), dom(2),-0.25,1.25])
    set(gca,'FontSize',16,'YTick',-0.25:0.25:1.25)
    v = axis; 
    text(v(1)+0.15,v(3)+0.15,['C=',num2str(Courant)],'FontSize',32)
else
    axis([dom(1), dom(2),-1.25,1.25])
    set(gca,'FontSize',16,'YTick',-1:0.5:1)
    v = axis; 
    text(v(1)+0.15,v(3)+0.25,['C=',num2str(Courant,'%3.1f')],'FontSize',32)
end
set(gcf,'color',[1,1,1])
title('Solution at t=0')

figure(4); clf;
xx = linspace(X(1),X(end),2*nElem+1); 
uu = InitialCondition(xx - a*tEnd,problem);
plot(xx,uu,'k--', X,u(:,(nStep/2)),'r','LineWidth',1.5);
l = legend('Exact solution', [methodName, ' solution']); 
set(l,'FontSize',24,'Location','NorthWest')
if problem == 1 || problem == 4
    axis([dom(1), dom(2),-0.25,1.25])
    set(gca,'FontSize',16,'YTick',-0.25:0.25:1.25)
    v = axis; 
    text(v(1)+0.15,v(3)+0.15,['C=',num2str(Courant)],'FontSize',32)
else
    axis([dom(1), dom(2),-1.25,1.25])
    set(gca,'FontSize',16,'YTick',-1:0.5:1)
    v = axis; 
    text(v(1)+0.15,v(3)+0.25,['C=',num2str(Courant,'%3.1f')],'FontSize',32)
end
set(gcf,'color',[1,1,1])
title('Solution at t=0.15')


