% 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: Runs the viral ODEs without integration with FBA for control comparison to integrated simulation.
%			Becuase parameters such as timestep and solver choice have the potential to impact output, 
% 			it is convieninet to have matching timesteps, and to assure that the same model definition version is being compared 
%			this function is used for each integrated simulation even though this will likely lead to some redundant simulation.
% 
% Inputs: simulation - simulation structure
%
% Outputs: simulation - simulation structure, updated with the viral reference run included
%
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
% Notes: 
%
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function simulation = FBA_EB_viralRefernceRun( simulation )

try % this try is just to assue path gets restored in case of error
    
    for iOrg = 1 : simulation.nOrganism

        if simulation.(simulation.organismList{ iOrg }).infectionFlag && ...
                simulation.(simulation.organismList{ iOrg }).methods.viralReferenceRunFlag

            % no limits on viral ODEs 
            viralODElimits = {false(simulation.(simulation.organismList{ iOrg }).modelInfected.nViralODEs, 1) zeros(simulation.(simulation.organismList{ iOrg }).modelInfected.nViralODEs, 1) };
            
            % prepare function handle
            viralODEfn = simulation.(simulation.organismList{ iOrg }).modelInfected.handleViralODEs;

            % prepare time variable range
            tViral = simulation.(simulation.organismList{ iOrg }).simulation.tViralSubStep * 60 * 60; % hour to sec (FBA time to virus time)
            % viral initial conditions
            cViralInitial = simulation.(simulation.organismList{ iOrg }).simulation.viralConcentrations( : , 1); % zeros
            % viral ODE simulation
            odeSolver = simulation.(simulation.organismList{ iOrg }).methods.viralODEsolverHandle;
            [~, viralReferenceRun] = odeSolver( viralODEfn, tViral, cViralInitial, [], ...
                simulation.(simulation.organismList{ iOrg }).modelInfected.viralVarargin, viralODElimits ); % sec, (ode)met

            
            % transpose viral reference run output if needed and store results
            if size( viralReferenceRun ) == size( simulation.(simulation.organismList{ iOrg }).simulation.viralConcentrations )
                simulation.(simulation.organismList{ iOrg }).simulation.viralReferenceRun = viralReferenceRun;
            else
                simulation.(simulation.organismList{ iOrg }).simulation.viralReferenceRun = viralReferenceRun';
            end
            
        end % if infected and flag to do reference run

    end % for iOrg


catch exceptionViralRefRun
    
    
    rethrow( exceptionViralRefRun )
       
end % try catch to make sure path is restored

