%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%               Valve module                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculates flow rate q_out based on pressure difference 
% across the valve. Equation for calculating q, suits
% with definition of the used C_v constant. The valve 
% flow coefficient is the number of US gallons per minute
% of 60°F water that will flow through a valve at
% a specified opening with a pressure drop of 1psi 
% across the valve.
%
% q=C_v/(11.7*(SG/dp)^(1/2))
% q-flow rate [m3/h]
% SG-special gravity. (rho_propan/rho_water)
% dp-pressure difference [kPa]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [q_out] = flow_element(u)
global rho;
C_v=u(1);
z=u(2);
p_in=u(3);
p_out=u(4);
q_out=z*C_v/11.7/(rho/1000/((p_in-p_out)/1000))^(1/2);
end
