function Sol = ILS(X,IEN,Conv,nu,f,c,Accd1,bccd1,T,s,beta,dt,nstep)
% Sol = ILS(X,IEN,Conv,nu,f,c,Accd1,bccd1, T,s,beta,dt,nstep)
% This function computes solution for a transient convection-diffusion equation
% at different time instants.
% Least-squares method is used to perform spatial discretization.
%
% Input:
%   X: nodal coordinates
%   IEN: connectivities
%   Conv: velocity field
%   nu: diffusion
%   f: source term
%   c: initial condition
%   Accd1, bccd1: matrices to impose boundary conditions using Lagrange multipliers
%   T,s,beta: matrices for the time-integration scheme
%   dt: time-step 
%   nstep: number of time steps 
%


% Number of points used in the discretization
npoin = size(X,1);

% COMPUTATION OF THE MATRICES 
% Number of Gauss points (numerical quadrature)
ngaus = 4;
% Quadrature
[pospg,wpg] = Quadrature(ngaus);
% Shape Functions
[N,Nxi,Neta] = ShapeFunc(pospg);

% Matrices obtained by discretizing the Galerkin weak-form
disp(' ')
disp ('Computation of mass matrix M, obtained by discretizing the term (w,u)')
M = CreMassMat(X,IEN,pospg,wpg,N,Nxi,Neta);
disp ('Computation of convection matrix C, obtained by discretizing the term (w,a·grad(u))')
C = CreConvMat(X,IEN,Conv,pospg,wpg,N,Nxi,Neta);
disp ('Computation of stiffness matrix K, obtained by discretizing the term (grad(w),grad(u))')
K = CreStiffMat(X,IEN,pospg,wpg,N,Nxi,Neta);
disp ('Computation of matrix K2, obtained by discretizing the term (a·grad(w),a·grad(u))')
K2 = CreK2Mat(X,IEN,Conv,pospg,wpg,N,Nxi,Neta);


% Integration matrix
[n,m] = size(T);
Id = eye(n,m);
T2 = T'*T;
Ts = T'*s;

% Computation of the matrix necessary to obtain solution at each time-step: A du = F
disp(' ')
disp('Computation of total matrices for the time-integration scheme')
Kt  = C + nu*K;
A = [];
for i = 1:n
    row = [];
    for j = 1:m
        row = [row, Id(i,j)*M  + dt*T(i,j)*Kt + ...
               dt*T(j,i)*Kt' + dt^2*T2(i,j)*K2];
    end
    A = [A; row];
end

nccd = size(Accd1,1);
cero = zeros(nccd,npoin);
Accd = []; bccd = [];
for i = 1:n
    row = [];
    for j = 1:m
        row = [row, Id(i,j)*Accd1];
    end
    Accd = [Accd; row];
    bccd = [bccd; bccd1];
end

nccd = n*nccd;
Atot = [A Accd'; Accd zeros(nccd)];
Atot = sparse(Atot);

% Factorization of matrix Atot
disp(' ')
disp('Factorization of the matrices for the time-integration scheme')
[L,U] = lu(Atot);
L = sparse(L);
U = sparse(U);

% Source term
Mf = M*f;
Ktf = Kt'*f;
% Initial condition
Sol = c;
% Loop to compute the transient solution
disp(' ')
disp('Transient analysis: computation of the solution at each time step')
for i=1:nstep
    t_i = (i-1)*dt;
    source_t = exp(-(t_i^10));    
    aux1 = -dt*(Kt*c - source_t*Mf);
    aux2 = -dt^2*(K2*c - source_t*Ktf);
    F = [];
    dF = zeros(m*npoin,1);
    for i =1:n
        t0 = beta(i)*dt;
        t1 = beta(i+1)*dt;
        df1 = (exp(-(t1^10)) - exp(-(t0^10)))*Mf;
        df2 = (exp(-(t1^10)) - exp(-(t0^10)))*Ktf;
        for j=1:m
            dF(j:j+npoin-1) = T(i,j)*df1 + T2(i,j)*df2;
        end
        F = [F; s(i)*aux1 + Ts(i)*aux2];
    end
    F = F + dF;
    F = [F;bccd*0];    
    dc  = U\(L\F);
    dc = reshape(dc(1:n*npoin),npoin,n);
    c = c + sum(dc,2);
    Sol = [Sol c];   
end








