function [xdot,y] = v1_DiMeglio_model(x,u,par)

% Simplified model for riser slugging
% By: Florent Di Meglio
% Mines Paristech

% x1: Mass of gas in the elongated bubble (m_G1)
% x2: Mass of gas in the riser (m_G2)
% x3: Mass of liquid in the riser (m_L2)


m_G1 = x(1); 
m_G2 = x(2); 
m_L2 = x(3); 


z = u(1);
w_G_in = u(2);
w_L_in = u(3);


P1 = m_G1*par.R*par.T1/(par.M_G*par.Veb);

V_G2 = par.V2 - (m_L2+par.m_lstill)/par.rho_L;
P2_t = m_G2*par.R*par.T2/(par.M_G*V_G2);
P2_t = max(P2_t,par.P0);

P2_b = P2_t + par.g*sin(par.theta2)*(m_L2+par.m_lstill)/par.A2;

w_G1 = par.C_g*max(0,P1-P2_b);

w_out = par.C_c*z*sqrt(max(0,P2_t-par.P0));
w_L_out = (m_L2/(m_L2+m_G2))*w_out;
w_G_out = (m_G2/(m_L2+m_G2))*w_out;


dx1 = (1 - par.ep)*w_G_in - w_G1;
dx2 = par.ep*w_G_in + w_G1 - w_G_out;
dx3 = w_L_in - w_L_out;

xdot = [dx1;dx2;dx3];

y = [P1/1e5;...     %Y1 
    P2_t/1e5;...    %Y2
    w_out;...       %Y3
    P2_b/1e5;       %Y4
    w_G_out;...     %Y5
    w_L_out;...     %Y6
    w_G1];          %Y7
end