function sf_multicomA(block)

% ******* Program written by Antonio Araujo in March, 2008 **********
% In order to know more on Level-2 M-file S-function, refer to the MatLab documentation.

setup(block);

function setup(block)

% Register number of dialog parameters
block.NumDialogPrms = 0;

% Register number of ports
parameters
nu = 7; ny = 2 + 2; % Output = [composition at both ends (2 vectors)] + [MD and MB (2)]
block.NumInputPorts  = nu;
block.NumOutputPorts = ny;

% Setup port properties to be inherited or dynamic
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;

for i = 1:nu
    block.InputPort(i).Dimensions = 1;
    block.InputPort(i).DirectFeedthrough = true;
end
block.InputPort(3).Dimensions = NC; % Feed composition

for i = 1:ny
    block.OutputPort(i).Dimensions = 1;
end
block.OutputPort(1).Dimensions = NC-1;
block.OutputPort(2).Dimensions = NC-1;

block.SampleTimes = [0 0];

% Setup Dwork
block.NumContStates = NT*NC;

block.RegBlockMethod('InitializeConditions',    @InitializeConditions);
block.RegBlockMethod('Outputs',                 @Outputs);
block.RegBlockMethod('Derivatives',             @Derivatives);
block.RegBlockMethod('SetInputPortSamplingMode',@SetInpPortFrameData);


function SetInpPortFrameData(block, idx, fd)
  
block.InputPort(idx).SamplingMode = fd;
for i = 1:block.NumOutputPorts
    block.OutputPort(i).SamplingMode = fd;
end


function InitializeConditions(block)

load steadystate % y0
block.ContStates.Data = y0;


function Outputs(block)

parameters
% State variables
X = block.ContStates.Data;
x = X(1:(NC-1)*NT);                    % Liquid compositions from btm to top
M = X((NC-1)*NT+1:NC*NT);              % Liquid hold up from btm to top

% Rearrange elements of composition vector (x) for later use
Iu = reshape(1:(NC-1)*NT,NT,NC-1);
x  = x(Iu);

% Disturbances
F   = block.InputPort(1).Data + Fbias;
qF  = block.InputPort(2).Data + qFbias;
zF  = block.InputPort(3).Data' + zFbias;

% Manipulation
LT  = block.InputPort(4).Data + LTbias;
VB  = block.InputPort(5).Data + VBbias;
D   = block.InputPort(6).Data + Dbias;
B   = block.InputPort(7).Data + Bbias;

% Output variables
yout = [x(NT,:) x(1,:) M(NT) M(1)] - ybias;
block.OutputPort(1).Data = yout(1:NC-1);
block.OutputPort(2).Data = yout(NC:2*(NC-1));
block.OutputPort(3).Data = yout(2*(NC-1)+1);
block.OutputPort(4).Data = yout(2*(NC-1)+2);


function Derivatives(block)

parameters
% State variables
X = block.ContStates.Data;
x = X(1:(NC-1)*NT);                    % Liquid compositions from btm to top
M = X((NC-1)*NT+1:NC*NT);              % Liquid hold up from btm to top

% Rearrange elements of composition vector (x) for later use
Iu = reshape(1:(NC-1)*NT,NT,NC-1);
x  = x(Iu);                          

% Disturbances
F   = block.InputPort(1).Data + Fbias;
qF  = block.InputPort(2).Data + qFbias;
zF  = block.InputPort(3).Data' + zFbias;

% Manipulation
LT  = block.InputPort(4).Data + LTbias;
VB  = block.InputPort(5).Data + VBbias;
D   = block.InputPort(6).Data + Dbias;
B   = block.InputPort(7).Data + Bbias;

% Non-linear model
model

block.Derivatives.Data = [dxdt; dMdt];

