%DP_CALC calculates the pressure difference, individual flow rates, and
%pump duty at Viken's side of the Brobekk incineration plant. The syntax is
%
%   y = dp_calc(u)
%
%where u is a vector containing the bypass valve position, the valve
%positions of the other two valves, inlet temperature [°C], and total flow 
%rate [kg/s]. The user must also check the parameters used in this script.
%
%Helge Smedsrud
%IKP, NTNU
%2008

function [y] = dp_calc(u)

% Flow coefficient calculation [m3/h*bar]
Kv_CB = Kv_Viken(u(1));
Kv_CI1 = Kv_Viken(u(2));
Kv_CI2 = Kv_Viken(u(3));

% Densities [kg/m3]
rho_c = 959.049;
rho = 1000;

% Defined by user
Tc = u(4);              % [°C]
wc = u(5)*3600/rho_c;   % [m3/h]

% Pump efficiency [-]
eta_p = 0.9;

% Other parameters
A1 = 1;         % [m2]
A2 = A1;        % [m2]
g = 9.80665;    % [m/s2]
dz = 0;         % [m]

% Pressure difference [bar] and flow rate [kg/s] calculation
dp = ((wc/(Kv_CB+Kv_CI1+Kv_CI2))^2)*(rho_c/rho);
wCB = Kv_CB*sqrt(dp*(rho/rho_c))*(rho_c/3600);
wCI1 = Kv_CI1*sqrt(dp*(rho/rho_c))*(rho_c/3600);
wCI2 = Kv_CI2*sqrt(dp*(rho/rho_c))*(rho_c/3600);

% Calculating the pump duty [W]
P = ((dp*u(5)*1e5/rho_c) + ((u(5)^2)*((1/(A2^2))-(1/(A1^2)))/(2*rho_c)) + ...
    (rho_c*g*dz))/eta_p;

y = [dp;wCB;wCI1;wCI2;Tc;P];