% Golgi model with vesicular transport between cisternae
clear; 

                                 % definition of cisternae:
dc=1.5;                          % diameter in micrometers
hc=.03;                          % heigth of cisternae in micrometers
Vc=pi*(dc/2)^2*hc;               % volume
Sc=2*pi*(dc/2)^2+2*(dc/2)*pi*hc; % surface

                                 % definition of transport vesicles:
dv=.15;                          % diameter
Vv=(4/3)*pi*(dv/2)^3;            % volume
Sv=4*pi*(dv/2)^2;                % surface

                                 % number of cisternae:
NC=6;
                                 % starting concentration in first cisterna:
N_0=zeros(1,NC);                 % empty cisternae at time t=0 ; N_0 is a vector with NC fields
N_0(1)=1000;                     % number of proteins in the 1st cisterna at t=0; initial condition

                                 % ratio of volumes:
a=Vv/Vc; 
disp(a);

                                 % transport mode: 
                                 %   m = 1: anterogrde+retrograde w/o self-fusion; 2 vesicles from middle
                                 %           cist. (Fig. A) 
                                 %   m = 2: only anterograde, constant flux into 1st (1st cist. const), flux out
                                 %           from the last (Fig. B)
                                 % 
                                 %   value of m is hard-coded in this script; set value of m in next line
m=1; 

c_crit=0.90;                     % stop when 90% equilibrium is reached
n_disp=100;                      % display results every n_disp steps

                                 % save data
s=1;                             % set s=1 to save data
                                 % define "fpath" in next line where output file should be saved
fpath='/home/vhelms/matlab/vesicular/vesicular';
n_save=100;                      % save every n_save steps
if s==1, M_save(1,:)=[0 N_0]; n_s=1; end

N(1,:)=N_0;                      % N counts the particles in each cisterna;
n=1;                             % n counts the iteration steps 
c=0;                             % c is the concentration in the last cisterna

while c<c_crit;
    Nv=N(n,:)*a;                 % fill vesicles; protein concentration in vesicle as in cisterna
    N(n+1,:)=N(n,:)-Nv;          % vesicles detatch and carry Nv proteins to vesicle on the right
    n=n+1;
    switch m                     % each of the following blocks performs one transport iteration on all cisternae
                    
        case 1                   % this corresponds to Fig. S9 a)
            N(n,1)=N(n,1)+Nv(2);

            for i=2:NC-1 % only need to model transport to vesicle on the left
                N(n,i)=N(n,i)-Nv(i)+Nv(i-1)+Nv(i+1);            
            end
            N(n,NC)=N(n,NC)+Nv(NC-1);
            c=1-abs((N(n,1)-N(n,NC))/2)/((N(n,1)+N(n,NC))/2);
            
        case 2                    % this corresponds to Fig. S9 b)
            N(n,1)=N(n,1)+Nv(1);
            for i=1:NC-1
                N(n,i+1)=N(n,i+1)+Nv(i);
            end
            c=abs(N(n,NC))/N(1,1);
                       
    end
%     disp(Nv(:));
%     disp(N(n,:));
%     disp(N(n,1)+N(n,2)+N(n,3)+N(n,4)+N(n,5)+N(n,6));
     
    if mod(n,n_disp)==0, disp([num2str(n) '    ' num2str(N(n,:))]); end
    if mod(n,n_save)==0, n_s=n_s+1; M_save(n_s,:)=[n N(n,:)]; end
end
disp([num2str(n) ' steps needed to arrive at ' num2str(c_crit*100) '%% equilibrium']); 

t=1:n;
figure;
for i=1:NC; hold on; plot(t,N(:,i)); end

% save
if s==1
    fname=[fpath 'NC' num2str(NC) '_mode' num2str(m) '.dat'];
 %   fM=[t' N];
    save(fname,'M_save','-ascii');
end

