Professional Documents
Culture Documents
Write the m-file, the severe slug system can be modelled by writing the m-file of the
system to be used by matlab solver. The name of the m-file is two-fluid the m-file is
shown in the appendix. In figure below.
function data=gen_data
% Function defining case spesific data for the two_fluid.m
%
% Written by Espen Storkaas, Jan 20th 2003
% Fluid properties
data.Mw_G=20.6;
data.Z=1.2;
data.rho_L=850;
data.my_L=0.00014;
data.my_G=1.3e-5;
data.T=273+42;
% Physical Constants
data.R=8.314;
data.g=9.81;
% Choke parameters
data.Ac=pi/4*0.075^2;
data.Cd=0.9;
data.C_vG=1.75e3;
data.C_pG=2.15e3;
data.k=data.C_pG/data.C_vG;
data.C_L=1.75e3;
%25/24
% data.dx_m=[500;500;250;250;250;250;250;250;250;250;250;250;...
% 200;150;100;100;100;50;50;50;50;50;100;100;100];
% data.dx_i=[500;375;250;250;250;250;250;250;250;250;250;225;...
% 175;125;100;100;75;50;50;50;50;75;100;100];
% data.theta=[0;0;0;0;0;0;-0.0137;-0.0274;-0.0274;-0.0274;-0.0274;...
% -0.0274;-0.0274;-0.0274;-0.0274;-0.0274;-0.0274;-0.0274;-0.0274;...
% 0.7717;1.571;1.571;1.571;0.7854];
%13/12
%Alt. 1
% data.dx_m=[1000;1000;1000;500;250;250;100;100;50;50;150;150;100];
% data.dx_i=[1000;1000;750;375;250;175;100;75;50;100;150;125];
% data.theta=[0;-0.00875;-0.0175;-0.0175;-0.0175;-0.0175;-0.0175;-0.0175;-
0.0175;0.7766;pi/2;0.7854];
%Alt. 2
data.dx_m=[1500;1000;1500;100;100;50;40;20;40;100;125;50;75];
data.dx_i=[1250;1250;800;100;75;45;30;30;70;112.5;87.5;62.5];
data.theta=[0;-0.0175;-0.0175;-0.0175;-0.0175;-0.0175;-0.0175;pi/2;pi/2;...
pi/2;pi/2;0];
data.D=0.12;
data.dx_m=[1500;1000;1500;100;100;50;40;20;40;100;125;50;75];
data.dx_i=[1250;1250;800;100;75;45;30;30;70;112.5;87.5;62.5];
data.theta=[0;-0.0175;-0.0175;-0.0175;-0.0175;-0.0175;-0.0175;pi/2;pi/2;...
pi/2;pi/2;0];
data.D=0.12;
data.A=pi/4*data.D^2;
% Index vectors
data.I_mL=(1:length(data.dx_m))';
data.I_mG=data.I_mL+max(data.I_mL);
data.I_i=(1:length(data.dx_i))';
data.I_iL=data.I_i+max(data.I_mG);
data.I_iG=data.I_i+max(data.I_iL);
2. The file is saved as an m-file and it contains the protocol in which Simulink can access
information from MATLAB.
3. A s-function is developed for the system and it is saved as s_slug.
function [sys,x0,str,ts] = s_slug(t,x,u,flag)
global data
switch flag
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts]=InitializeSlug;
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1,
sys=real(two_fluid_sim(0,x,u,data,'derivatives'));
%%%%%%%%%%
% Update %
%%%%%%%%%%
case 2,
% sys=mdlUpdate(t,x,u);
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,
sys=two_fluid_sim(0,x,u,data,'measurements');
case 2,
% sys=mdlUpdate(t,x,u);
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,
sys=two_fluid_sim(0,x,u,data,'measurements');
%%%%%%%%%%%%%%%%%%%%%%%
% GetTimeOfNextVarHit %
%%%%%%%%%%%%%%%%%%%%%%%
case 4,
% sys=mdlGetTimeOfNextVarHit(t,x,u);
%%%%%%%%%%%%%
% Terminate %
%%%%%%%%%%%%%
case 9,
% sys=mdlTerminate(t,x,u);
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
The file needs to contain the following :
I. The first line specifies the input and output arguments
a. Input arguments
1. t – the time variable
2. x – the column-vector of state variables
3. u – the column-vector of input variables (whose values would
come from other simulink block)
4. flag -- indicator of which group of information and/or calculation
is being requested by Simulink.
There are six types of request that Simulink performs, each of which I
designated by an integer number.
9 Termination
Additional calculations were done at the end of the
simulation run
b. Output Arguments
1) sys – the main vector of results requested by simulink. Depending on the flag sent by simulink,
this vector will hold different information.
The next set of 3 output are needed by simulink only when flag = 0, otherwise, they are ignored:
3) str – need to be set to null. This is reserved for use in the future versions of Simulink.
4) ts - an array of two column to specify sampling time and time offsets. Since the system here is
a continuous system, this was set to [0 0] during initiation phase.
II. After the first line, the S-function file (s_slug) was split into the different cases
determined by the flag. In the figure below, the bare structure of the ‘container’
for the different cases is shown.
str=[] ;
ts = [0 0] ;
x0 = 770*ones(50,1);
x0(14:25) = 30;
x0(26:end) = 30;
b. A row was created which specifies the number of inputs and output. This was
done by invoking the simsize command. Without arguments simsizes created a
structure variable which can then filled it with the required values;
sizes = simsizes;
sizes.NumContStates = 50;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 8;
sizes.NumInputs = 4;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1
Using the command simsizes again with the structure variable as the command actually translates
the values in the structure, s, into a row vector which gets sent to simulink via sys:
sys = simsizes(sizes);
In the simulink window, the S-function block was dragged dropped from the [User-Define
Functions] subdirectory. The S-function was selected and filled with parameters. The S-function
block was changed to s_slug and also filled with parameters. The input parameters were , (which
is the value of ) as shown in the figure below .