% s_ngventil.m

function [sys,x0,str,ts] = s_ventil(t,x,u,flag)
% u = [T p] i siste kontrollvolum til PC
global b
switch flag


  % INITALIZATION
  % -------------
  case 0,
    [sys,x0,str,ts]=Initialize;

  
  % OUTPUTS 
  % -------
  case 3,
    pinn= u(1);
    rhoinn= u(2);
    Hinn= u(3);     % [J/mol]
    z = u(4);
%     Tut = u(5);
    put = u(5);

    w = z*b.Cv_ngventil*rhoinn*sqrt(abs(pinn-put)/rhoinn);
    Hut = Hinn*w/b.MW_pc(2);        %[J/s]
    sys = [w Hut]';
    
  case { 1, 2, 4, 9 }
    sys=[];

  % UNEXPECTED FLAGS
  % ----------------
  otherwise
    error(['Unhandled flag = ',num2str(flag)]);

end

% end sfuntmpl

%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%

function [sys,x0,str,ts]=Initialize
global b
%
% call simsizes for a sizes structure, fill it in and convert it to a
% sizes array.
%
% Note that in this example, the values are hard coded.  This is not a
% recommended practice as the characteristics of the block are typically
% defined by the S-function parameters.
%
sizes = simsizes;

sizes.NumContStates  = 0;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 2; % w H
sizes.NumInputs      = 5; % pinn,rho,H, z, p og T etter ventil
sizes.DirFeedthrough = 1; % 0 hvis utgang ikke direkte funksjon av inngang, ellers 1
sizes.NumSampleTimes = 1; % at least one sample time is needed

sys = simsizes(sizes);

% initialize the initial conditions
x0 = []; % temp
%
% str is always an empty matrix
str = [];

% initialize the array of sample times
ts  = [0 0];
%data=initialize;
% end mdlInitializeSizes

