function out = GasCooler(x, aux)
% Steady state CO2 gas cooler model
% Jørgen Bauck Jensen
% 04.01.2005
% Inputs:       x - States
%               u - Inputs
%               d - Data struct
% Outputs:      out.res - residual
u = aux.u;
d = aux.d;
sc = aux.sc;
x = x.*sc.x;

N = x(1:d.ngco);
T = x(d.ngco+1:2*d.ngco);
Q = x(2*d.ngco+1:3*d.ngco);
Tair = x(3*d.ngco+1:4*d.ngco);
nair = x(4*d.ngco+1);
P = x(end-1);
A = x(end);

hin = u(1);
n = u(2);
Tair_in = [Tair(2:end); u(3)];
Pt=[];h=[];
for i=1:d.ngco
  Atmp = co2_sw([T(i); d.Vgco/d.ngco; N(i)]);
  Pt = [Pt; -Atmp.g(2)];
  h = [h; h_given_TVN([T(i); d.Vgco/d.ngco; N(i)])]; 
end
hin = [hin;h(1:end-1)];
hout = h(1:end);
out = [(hin-hout)*n+Q;                  % Energy balance
       Q-d.Ugco*A/d.ngco*(Tair-T); % Heat transfer
       Q+nair*d.Cp_air*(Tair-Tair_in);
       P-Pt]./sc.res;
