% 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: function to evaluate viral ODEs and pass the maximum of any reaction as an initial demand or request to metabolism FBA
%
% Inputs: 	tStep - timestep in hours
%  			iSim - iteration of simulation
%			organism - organism structure that includes an infected organism
%
% Outputs: odeRequest - a vector with the units of (ode)met/sec matching the viral FBA formating order
%
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%
% Notes: (alternates explored and can modularly replace this one, but not included in code provided for paper)
%
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function [odeRequest] = FBA_EB_viralRequestMax( tStep, iSim, organism )

viralODElimits = {false(organism.modelInfected.nViralODEs, 1) zeros(organism.modelInfected.nViralODEs, 1) };

% prepare function handle
viralODEfn = organism.modelInfected.handleViralODEs;
% prepare time variable range
tViralLim = ((iSim-2)*tStep: tStep/organism.simulation.nViralSubSteps :(iSim-1)*tStep) * 60 * 60; % hour to sec (FBA time to virus time)
% viral initial conditions
cViralInitial = organism.simulation.viralConcentrations( : , iSim - 1 ); % (ode)met
% viral ODE simulation
odeSolver = organism.methods.viralODEsolverHandle;
[~, cViral] = odeSolver( viralODEfn, tViralLim, cViralInitial, [], organism.modelInfected.viralVarargin, viralODElimits ); % sec, (ode)met

fViralApprox = diff(cViral,1,1) / ( tStep/organism.simulation.nViralSubSteps * 60 * 60 );
fViralMax = max( fViralApprox, [], 1 );

odeRequest = fViralMax; % (ode)met/sec


