clear, close all, 

disp(' ')
disp('This program solves the 1st Assignment question')
disp('Using Crank-Nicolson for time and Galerkin in space')
% Material paramters for F and G
D_F = 5; SigF = 0.25; D_G=15; SigG= 2; SigGF= 0.5;

% GEOMETRY
% Number of elements on each direction
nx = 10; 
ny = 20;
% Matrices of nodal coordinates and connectivities
[X,T] = createMesh(ny,nx); % nx=Theta ny =Nr
plotMesh(T,X,0)
%a = 0.5; % Not objective 
% numnp = size(X,1); 
% h = sqrt(2/nx^2);
% nu=1;
% if nu ~= 0    
%     Pe = a*h/(2*nu);
%     disp(' ')
%     disp(strcat('   P�clet number: ',num2str(Pe)));  
% end

% DATA FOR THE TRANSIENT ANALYSIS
disp(' ')
C = 1;
dt = input(sprintf('Time step (default: %g) = ',C*h/a));
if (isempty(dt)), dt = C*h/a; end
endt= input('End time (default: pi) =  ');
if isempty(endt), endt = pi; end
nstep = ceil(endt/dt);

Courant = a*dt/h;
disp(' ')
disp(strcat('   Courant number: ',num2str(Courant)));  

% CONVECTION VELOCITY
% The velocity inlet condition
Conv = zeros(size(X));
cont_k= -1/1500;
rad = X(:,1).^2 + X(:,2).^2;
r = sqrt(rad);
Conv(:,1) = r.*cont_k.* X(:,1);
Conv(:,2) = r.*cont_k.* X(:,2);


% SOURCE TERM
f = SourceTerm(X);

% INITIAL CONDITION
cF = 80 *((r-15)/(25-15));   % For F
c = zeros(numnp,1);          % For G


% BOUNDARY CONDITIONS  (Using LAGRANGE MULTIPLIERS METHOD)
[Accd1, bccd1] = BCF(nx,ny,numnp); %  Set for F

[Accd1G, bccd1G] = BCG(nx,ny,numnp); % for G

% Initial condition.
% SOLUTION

d_temp = 0;    % Time integration;
d_esp = 0;     %spatial discretization   

% Matrices for the time integration
if d_temp == 0
    method = 'CN + ';
    W = 1/2;
    w = 1;    
    beta = [0,1];
else
    error('Unavailable time integration scheme')
end

if d_esp == 0
    method = strcat(method,' Galerkin');
    disp(' ')
    disp(method)
else
    error('Unavailable spatial discretization method')
end    
% Solution
if d_esp == 0
   [solf desol] = GalerkinF(X,T,Conv,D_F,f,cF,Accd1,bccd1,W,w,beta,dt,nstep,SigF);% F solution
    %%% Transfer the solution F to G
    Sol = GalerkinG(X,T,Conv,D_G,f,c,Accd1G,bccd1G,W,w,beta,dt,nstep,SigG,solf,desol,SigGF); %G solution
end   
  
% POST PROCESS for F
% Solution at final time
figure(1); clf;
xxf = reshape(X(:,1),nx+1,ny+1)';
yyf = reshape(X(:,2),nx+1,ny+1)';
solF = reshape(solf(:,nstep+1), nx+1, ny+1)';
colorbar
surface(xxf,yyf,solF);
view(3)
grid on;
title('Actin Density F')
% figure(2); clf;
% contour(xxf,yyf,solF,[-0.01,0.01,0.1:0.1:0.5]);
% axis equal tight
% colorbar
% Movie
figure(10); clf;
peliF = moviein(nstep+1);
axis manual

for n=1:nstep+1
    xxf = reshape(X(:,1),nx+1,ny+1)';
    yyf = reshape(X(:,2),nx+1,ny+1)';
    solF = reshape(solf(:,n), nx+1, ny+1)';
    surf(xxf,yyf,solF); view(3);
    peliF(:,n) = getframe;
    pause(0.1)
    title('Actin Density F Movie')
end
colorbar
% POST PROCESS for G
% Solution at final time
figure(11); clf;
xx = reshape(X(:,1),nx+1,ny+1)';
yy = reshape(X(:,2),nx+1,ny+1)';
sol = reshape(Sol(:,nstep+1), nx+1, ny+1)';
colorbar
surface(xx,yy,sol);
view(3)
grid on;
title('Monomer Density G')
% figure(12); clf;
% contour(xx,yy,sol,[-0.01,0.01,0.1:0.1:0.5]);
% axis equal tight
% colorbar
% Movie
figure(14); clf;
peli = moviein(nstep+1);
axis manual

for n=1:nstep+1
    xx = reshape(X(:,1),nx+1,ny+1)';
    yy = reshape(X(:,2),nx+1,ny+1)';
    sol = reshape(Sol(:,n), nx+1, ny+1)';
    surf(xxf,yyf,sol); view(3);
    peli(:,n) = getframe;
    pause(0.1)
end
colorbar

