% 
% Author: Elsa W. Birch, ewbirch@stanford.edu, Covert Lab - Stanford Bioengineering
% Date: 4/24/2011
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
% Summary: Loops through organisms running initialization function as specified in
%          simulation structure.
% 
%
% 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: Loops through organisms running initialization functions as specified in
%          simulation structure. ** THIS IS THE VERSION USED WHEN FLUX BOUNDS ARE SPECIFICLALY PURTURBED TO EXPLORE THE LIMITING FACTORS OF VIRAL REPLICATION **
% 
% Inputs: simulation - simulation structure
%
% Outputs: simulationInitialized - initialized with state of fluxes and regulation in media
%
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
% Notes: 	> ** THIS IS THE VERSION USED WHEN FLUX BOUNDS ARE SPECIFICLALY PURTURBED TO EXPLORE THE LIMITING FACTORS OF VIRAL REPLICATION **
% 			> The criterion (if any) for sucsessful inialization is determined in the function that is called here, not this function
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function simulationInitialized = FBA_EB_simulationInitialize_fluxBoundSet(simulation)

% disp('begin initialization ...')
for iOrgInit = 1 : simulation.nOrganism
    
    % host initialization
    
    hInitializerFn = simulation.(simulation.organismList{iOrgInit}).methods.initializerHandle;
    
    [simulation.(simulation.organismList{iOrgInit}).simulation, initializationWarnings] = ...
                    hInitializerFn(simulation.(simulation.organismList{iOrgInit}),...
                    simulation.media(:,1),simulation.tStep,...
                    simulation.(simulation.organismList{iOrgInit}).simulation.stimuli(:,1));
                
    % viral initialization
    if simulation.(simulation.organismList{ iOrgInit }).infectionFlag && ...
            simulation.(simulation.organismList{ iOrgInit }).modelInfected.viralInitializerFlag
        
        % save orrigional path in case of modification
        
        try
            hViralInitializerFn = simulation.(simulation.organismList{ iOrgInit }).modelInfected.viralInitializerHandle;
        
            simulation.(simulation.organismList{ iOrgInit }) = hViralInitializerFn(simulation.(simulation.organismList{ iOrgInit }));

        catch exceptionSimulation 

            rethrow( exceptionSimulation )

        end % try running simulation
        
    
    end % if infected and initializer flag
   
end % for iOrgInit
% disp('... finished initialization')

% (initialization is taken as first iteration)
simulation.iSim = 1;

simulationInitialized = simulation;