%Initial values einzelner Stufen
Xv0   = [0.3*10^6 ,0.3*10^6    ,0.3*10^6    ,0.3*10^6    ,0.4*10^6    ,0.4*10^6    ,0.25*10^6   ,0.3*10^6    ,0.38*10^6  ,0.38*10^6];
Xd0   = [6*10^3   ,6*10^3      ,6*10^3      ,6*10^3      ,6*10^3      ,6*10^3      ,6*10^3      ,6*10^3      ,6*10^3     ,6*10^3];% ,0.3*10^6];
cGlc0 = [6.8      ,6.8         ,6.8         ,6.8         ,6.8         ,6.8         ,6.8         ,6.8         ,6.8        ,6.8];    
cLac0 = [0.3      ,0.3         ,0.3         ,0.3         ,0.3         ,0.3         ,0.3         ,0.3         ,0.3        ,0.3];    
cGln0 = [0.9      ,0.9         ,0.9         ,0.9         ,0.9         ,0.9         ,0.9         ,0.9         ,0.9        ,0.9];      
cAmm0 = [1        ,1           ,1           ,1           ,1           ,1           ,1           ,1           ,1          ,1];    
v0    = [0.25     ,0.5         ,1           ,2           ,2           ,10          ,50          ,80          ,400        ,2000];    
%%
%Parameters()
Parameters(1) = 0.055;     % my_max   [h^-1]          Maximum Specific Growth Rate  
Parameters(2) = 0.03;      % my_d_max [h^-1]          Maximum Specific Death Rate  
Parameters(3) = 0.002;     % my_d_min [h^-1]          Minimum Specific Death Rate                  
Parameters(4) = 1.8*10^-8; % qGlc_max [mmol/cells *h] Maximum Cell-specific Glucose Uptake Rate
Parameters(5) = 0.8*10^-8; % qGln_max [mmol/cells *h] Maximum Cell-specific Glutamine Uptake Rate
Parameters(6) = 1.2*10^-12;% qLac_max [mmol/cells *h] Maximum Cell-specific Lactate Uptake Rate
Parameters(7) = 0.19;      % kGlc     [mmol/L]        Monod Kinetic Constant for Glucose Uptake
Parameters(8) = 0.3;       % kGln     [mmol/L]        Monod Kinetic Constant for Glutamine Uptake
Parameters(9) = 0.03;      % KsGlc    [mmol/L]        Monod Kinetic Constant for Glucose          
Parameters(10) = 0.03;     % KsGln    [mmol/L]        Monod Kinetic Constant for Glutamine  
 %%
%constants
%Constants(1) = 0.01;       % KLys        [h^-1]          Constant for Cell Lysing    
Constants(2) = 4*10^-12;   % qAmm_uptake [mmol/cells *h] Maximum Cell-specific Ammonia Uptake Rate  
Constants(3) = 0.4;        % YAmm/Gln    [mmol/mmol]     Kinetic production Constant for Ammonia by consuming Glutamine  
Constants(4) = 1.6;        %YLac/Glc     [mmol/mmol]     Kinetic production Constant for Lactate by consuming Glucose  
Constants(5) = 0;          %             [L]             Sample Volume
Constants(6) = 0.5;        %                             Correction Factor for Ammonia uptake
%%
n = 10; %Anzahl an Stufen
%Laufdauer
t=0;
y=[0,0,0,0,0,0,0];
Laufdauer =[0 72;72 144 ;144 216;216 288;288 360;360 432;432 504;504 576;576 648;648 1059]; %Laufdauer der einzelnen Stufen
for k = 1:n
    tRange= Laufdauer(k,:);  
    c0 = [ Xv0(k); Xd0(k); cGlc0(k); cLac0(k);  cGln0(k);  cAmm0(k); v0(k)]; %Vector that contains all the Initial values  
%%
%ODE45
[tSpeicher,ySpeicher] = ode15s(@(t,c) Model_BI130_1(t,c,Parameters,Constants), tRange, c0);
 
 t = vertcat(t,tSpeicher);
 y= vertcat(y,ySpeicher);
end
%Total Cell Density
Xv = y (:,1);
Xd = y (:,2);
Xt = Xv + Xd;
%Viability
Viability = (Xv./Xt)*100;

[x,fval,exitflag,output]  = particleswarm(@(c)Model_BI130_1([],c,Parameters,Constants),10,[],[])