% Author: Elsa W. Birch, ewbirch@stanford.edu, Covert Lab - Stanford Bioengineering
% Date: July 27th 2012
% Version: associated with production of the figures for Birch et al. 2012
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
% Summary: Overall function to do bookkepping such as naming the file, listing date, 
%			then running setup, and finally calling to run the simulation itsel fthen saving results.
% 
%
% Inputs: 	simulation - simulation structure that is primarily empty except for the setup type variables
%			folderPath - folder path for savig output
%			nUpdates - integer specifiyng if text outputs are to be written, how many durring the simulation durration itself
%			nameFile - name of file if a specific designation is to be applied to simulation, possible to overwrite, if too small then automatically generated
%			showProgress - logical if text outputs should be written for proscesses like inialization, reffernce runs, and simulation progress
%
% Outputs: growth - growth rate of organism (typcially not used, but convivnet for fitting needs)
%
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
% Notes: 
%
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function [ growth ] = FBA_EB_simulationPrepRunSave(simulation, folderPath, nUpdates, nameFile, showProgress)

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

simulation = FBA_EB_simulationSetup(simulation);

simulation = FBA_EB_simulationMedia(simulation);

simulation = FBA_EB_simulationSetMethods(simulation);

simulation = rmfield( simulation, 'setup' );

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


% construct simulation ID 
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

% data and time information 
timeVect = clock;
% prepare month string
if timeVect(2) < 10
    monthStr = ['0',num2str(timeVect(2))];
else
    monthStr = num2str(timeVect(2));
end
% prepare day string
if timeVect(3) < 10
    dayStr = ['0',num2str(timeVect(3))];
else
    dayStr = num2str(timeVect(3));
end
% prepare date string
dateStr = [ num2str(timeVect(1)), monthStr, dayStr ];
% prepare hour string from military
if timeVect(4) > 12
    hourStr = num2str( timeVect(4) - 12 );
else
    hourStr = num2str( timeVect(4) );
end % end if afternoon
% prepare am/pm string
if timeVect(4) > 11
    apStr = 'p';
else
    apStr = 'a';
end % end if afternoon
% pad hour string
if length(hourStr) < 2
    hourStr = [ '0', hourStr];
end
% pad min string
if timeVect(5) < 10
    minStr = ['0',num2str(timeVect(5))];
else
    minStr = num2str(timeVect(5));
end
% prepare time string
timeStr = [ hourStr, minStr, apStr ];

% organism information (currently assumes only one organism - not multiple in media)
organismsStr = simulation.organismList{1};

% timstep information
timestepStr = [num2str( round( simulation.tStep * 60 * 60 ) ), 's'];

% method infomration
if simulation.(simulation.organismList{ 1 }).infectionFlag
    viralConstraintStr = func2str(simulation.(simulation.organismList{ 1 }).methods.viralConstraintHandle);
    switch viralConstraintStr
        case 'FBA_EB_limitViralMetToMaxViralFluxes'
            methodStr = 'VM2VF';
        case 'FBA_EB_limitViralMetToMaxHostBiomass'
            methodStr = 'VM2HB';
        case 'FBA_EB_limitViralMetToMaxViralPseudoBiomass'
            methodStr = 'VM2VPB';
        case 'FBA_EB_limitViralFluxesToMaxViralPseudoBiomass'
            methodStr = 'VF2VPB';
        case 'FBA_EB_limitViralFluxesToMaxViralFluxes'
            methodStr = 'VF2VF';
        case 'FBA_EB_limitViralMetToMaxViralFluxesAfterMaxVPB'
            methodStr = 'VM2VFAVPB';    
        case 'FBA_EB_limitViralMetToMaxHostBiomassAndDNTPs'
            methodStr = 'VM2HBAD';
        case 'FBA_EB_limitViralMetToMaxDNTPsAfterMaxHostBiomass'
            methodStr = 'VM2MDAHB';
        otherwise
            methodStr = 'UNKNWN';
    end % swtich viralConstraintStr
    
else
    % uninfected
    methodStr = 'UNINF';

end % if infected

if simulation.(simulation.organismList{ 1 }).infectionFlag
    viralConstraintStr = func2str(simulation.(simulation.organismList{ 1 }).methods.modularSingleStepHandle);
    switch viralConstraintStr
        case 'FBA_EB_modularSingleStepPremtiveLimit'
            methodStrMod = '_PL';
        case 'FBA_EB_modularSingleStep'
            methodStrMod = '_RL';
        otherwise
            methodStrMod = '_UNKNWN';
    end % swtich viralConstraintStr

    methodStr = [methodStr, methodStrMod];
    
end % if infected

if simulation.(simulation.organismList{ 1 }).infectionFlag
    viralConstraintStr = func2str(simulation.(simulation.organismList{ 1 }).methods.viralRequestHandle);
    switch viralConstraintStr
        case 'FBA_EB_viralRequestAverage'
            methodStrMod = '_RA';
        case 'FBA_EB_viralRequestMax'
            methodStrMod = '_RM';
        case 'FBA_EB_viralRequestStart'
            methodStrMod = '_RS';
        otherwise
            methodStrMod = '_UNKNWN';
    end % swtich viralConstraintStr

    methodStr = [methodStr, methodStrMod];
    
end % if infected

koStr = simulation.(simulation.organismList{ 1 }).methods.koString;

simulation.simulationID = [ dateStr, '_', timeStr, '_', organismsStr, '_', ...
    simulation.mediaID,'_', timestepStr, '_', methodStr,koStr ];

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
if showProgress 
    if length(nameFile) < 2
        disp(['Simulation: ',simulation.simulationID])
    else
        disp(['Simulation: ',nameFile{end}])
    end
end

% initialize simulation organisms
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

%initialize organisms
if showProgress 
    disp('|  Begining Initialization to Media')
end
simulation = FBA_EB_simulationInitialize(simulation);

% run viral references
if showProgress && simulation.(simulation.organismList{1}).infectionFlag
    disp('|  Begining Viral Reference Run')
end
simulation = FBA_EB_viralRefernceRun( simulation ); 

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


% run simulation
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
if showProgress
    disp('|  Begining Integrated Simulation')
end
simulation = FBA_EB_simulationRun(simulation, nUpdates);

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

growth = simulation.(simulation.organismList{1}).simulation.growth(2);


% save simulation
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

if isempty(nameFile)
    save( fullfile(folderPath,simulation.simulationID) , 'simulation')
elseif length(nameFile) == 1
    save( fullfile(nameFile{:},simulation.simulationID) , 'simulation')
else
    save( fullfile(nameFile{:}) , 'simulation')
end

if showProgress 
    disp('|_Simulation Saved')
end
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
