function [sys,x0] = multicomP(t,x,u,flag)
%
%  Simulink interface to multicomA.m     
%
% Inputs:    t    - time in [min].
%            X    - State, the first (NC-1)*NT states are compositions of light
%                   component A with reboiler/bottom stage as X(1) and 
%                   condenser as X((NC-1)*NT). State X((NC-1)*NT+1)is holdup in reboiler/
%                   bottom stage and X(NC*NT) is hold-up in condenser. 
%            U(1) - reflux L,
%            U(2) - boilup V,
%            U(3) - top or distillate product flow D,
%            U(4) - bottom product flow B,
%            U(5) - feed rate F,
%            U(6) - liquid feed fraction, qF.
%            U(7) - feed composition, zF.
%           
%
% Outputs:   sys and x0 as described in the SIMULINK manual.
%            when flag is 0 sys contains sizes and x0 contains 
%            initial condition. 
%            when flag is 1, sys contains the derivatives,
%            and when flag is 3 sys contains outputs; 
%            y(1)    - top composition,
%            y(2)    - bottom composition,
%            y(3)    - condenser holdup,
%            y(4)    - reboiler holdup,
%            y(5:((NC-1)*NT)+4) - tray composition, y(5) is reboiler y((NC-1)*NT+4) is top.
% sys = [....] on flag=0 returns a vector, SIZES, which
% contains the sizes of the state vector and other parameters.
%		SIZES(1) number of states
%		SIZES(2) number of discrete states
%		SIZES(3) number of outputs
%		SIZES(4) number of inputs
%		SIZES(5) number of roots (currently unsupported)
%		SIZES(6) direct feedthrough flag
%		SIZES(7) number of sample times
%	For the definition of other parameters in SIZES, see SFUNC.
%	See also, TRIM, LINMOD, LINSIM, EULER, RK23, RK45, ADAMS, GEAR.
NT = 48;
NC=5;
if abs(flag) == 1
  % Return state derivatives.
  sys = multicAP_sim(t,x,u);
 
elseif abs(flag) == 3
  % Return system outputs.
  %   sys(1,1) = x((NC-1)*NT);       % Top composition.
  sys(1,1) = x(2*NT);       % C3 i Top.
  sys(2,1) = x(NT+1);          %C3 Bottom.
% sys(2,1) = x(1);          % Bottom composition.
  sys(3,1) = x(NC*NT);       % Holdup in condenser.
  sys(4,1) = x((NC-1)*NT+1);     % Holdup in reboiler.
  sys(5:(NC-1)*NT+4,1)= x(1:(NC-1)*NT);   % Compositions.
%%  sys(5:NT+4,1)= x(NT+1:2*NT);   % Stage holdups.
% %sys(:,1)=x(:); % ALL states
%sys=x;
elseif flag == 0
  % Ininitalize the system
  load multicom_init
  x0 = Xinit;
 
 sys = [NC*NT, 0, (NC-1)*NT+4, 6+NC, 0, 0];
 %sys = [NC*NT, 0, NC*NT, 6+NC, 0, 0];
else
  sys = [];  
end
