function res = Heatpump(x, aux)
sc = aux.sc;
d = aux.d;
u = aux.u;
Tgco_air = u(1);
Tvap_air = u(2);

i_com = length(sc.com.x);
i_gco = length(sc.gco.x);
i_val = length(sc.val.x);
i_vap = length(sc.vap.x);
i_ihx = length(sc.ihx.x);
ycom = x(1:i_com).*sc.com.x;
ygco = x(i_com+1:i_com+i_gco).*sc.gco.x;
yval = x(i_com+i_gco+1:i_com+i_gco+i_val).*sc.val.x;
yvap = x(i_com+i_gco+i_val+1:i_com+i_gco+i_val+i_vap).*sc.vap.x;
yihx = x(i_com+i_gco+i_val+i_vap+1:end).*sc.ihx.x;

n = yval(2);
Ph = ygco(end-1);
Pl = yvap(end);
A = yvap(end-1);
Vtmp = 1;
Ntmp = N_given_TVP([yvap(1); Vtmp; Pl], 'v');
hvap_out = h_given_TVN([yvap(1); Vtmp; Ntmp]);
hcom_out = ycom(2);
hgco_out = h_given_TVN([ygco(2*d.ngco); d.Vgco/d.ngco; ygco(d.ngco)]);
hval_out = yval(end);
hihx1_out = h_given_TVN([yihx(d.nihx); d.Vihx1/d.nihx; yihx(2*d.nihx)]);
hihx2_out = h_given_TVN([yihx(3*d.nihx+1); d.Vihx2/d.nihx; yihx(4*d.nihx+1)]);
sihx2_out = s_given_TVN([yihx(3*d.nihx+1); d.Vihx2/d.nihx; yihx(4*d.nihx+1)]);
rho_to_valve = yihx(2*d.nihx)/(d.Vihx1/d.nihx);

aux.d = d;
xcom = x(1:i_com);
xgco = x(i_com+1:i_com+i_gco);
xval = x(i_com+i_gco+1:i_com+i_gco+i_val);
xvap = x(i_com+i_gco+i_val+1:i_com+i_gco+i_val+i_vap);
xihx = x(i_com+i_gco+i_val+i_vap+1:end);

% Compressor
u = [hihx2_out; sihx2_out; Ph];
aux.u = u;
aux.sc = sc.com;
com_res = compressor(xcom, aux);
% Gas Cooler
u = [hcom_out; n; Tgco_air];
aux.u=u;
aux.sc = sc.gco;
gco_res = GasCooler(xgco, aux);
% Valve
u = [hihx1_out; rho_to_valve; Pl; Ph];
aux.u = u;
aux.sc = sc.val;
val_res = valve(xval, aux);
% Evaporator
u = [hval_out; n; Tvap_air];
aux.u = u;
aux.sc = sc.vap;
vap_res = evaporator(xvap, aux);
% Internal heat exchanger
u = [hgco_out; Ph; hvap_out; Pl; n];
aux.u = u;
aux.sc = sc.ihx;
ihx_res = InternalHX(xihx, aux);
% Return residuals
res = [com_res; gco_res; val_res; vap_res; ihx_res];
