function Sol = GalerkinG(X,IEN,Conv,nu,f,c,Accd1,bccd1,T,s,beta,dt,nstep,SigG,sol,desol,SigGF)
% Sol = Galerkin(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.
% Galerkin 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);


% Integration matrix

[n,m] = size(T);
Id = eye(n,m);

% 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 + SigF*M;  %%% SigF*M adds the reaction term from usteady con-dif page 28
Kt  = nu*K + SigG*M ;  %%% SigF*M adds the reaction term from usteady con-dif page 28
A = [];
D = [];
for i = 1:n
    row = [];
    r2 = [];
    for j = 1:m
        row = [row, Id(i,j)*M  + dt*T(i,j)*Kt];
        r2 = [r2,  dt*T(i,j)*M*SigGF];
    end
    A = [A; row];
    D = [D; r2];  %FOR r2
end
    
nccd = size(Accd1,1);
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 = SigGF*M*sol;
% Initial condition
Sol = c;
% Loop to compute the transient solution
%desol =[]; 
disp(' ')
disp('Transient analysis: computation of the solution at each time step')
for i=1:nstep
    aux = dt*(-Kt*c + Mf(:,i)); % small w
    F = []; %source vector
    for i =1:n   %From Mateo 1 D code.
        F = [F; s(i)*aux] ;   
    end
    F = F + D*desol(:,i);     % Addition of the desol(F)
    F = [F;bccd*0];    
    dc  = U\(L\F); 
    %desol= [desol dc(1:n*npoin)];  % desol is derivative of the source solution
    dc = reshape(dc(1:n*npoin),npoin,n);
    c = c + sum(dc,2);
    Sol = [Sol c];   
end






