function u = CalcBlock2(x,v,d,p)
% static calculation block to invert transformation for tank 2 and solve for
% T1s = f(v,x,d,p)

V2    = p(2);                                          % [m3] tank 2 volume
A2    = p(6);                    %[1/s] tuning param gen linear transf (vL)
tau02 = p(8);                                % [s] tuning param IFAC. (vL0)
% states
T    = x;                                         % [degC] tank temperature
%disturbances
TD   = d(2);                   % [degC] temperature of flow added to tank 2
qD   = d(3);                                  % [m3/s] flow added to tank 2
q    = d(4);                                    % [kW] heat added to tank 1

% switch variables to change between case
transf = p(9); % switch trasformations: 0=static; 1=lin; 2= IFAC; 3=fb lin

switch transf
     case 0                                         % static transformation
          u = v-qD/q*(TD-T(2));                                 % u=T1s
     case 1                                         % linear trasnformation
          u = T(2) + 1/q*(V2*(v+A2*T(2))-qD*(TD-T(2)));           % u = T1s
     case 2                                                     % IFAC. vL0
          u = T(2) + 1/q*(V2/tau02*(v-T(2))-qD*(TD-T(2)));          % u=T1s
     case 3           % integrating transformation. similar to feedback lin
          u = T(2) + 1/q*(v*V2-qD*(TD-T(2)));                     % u = T1s
     otherwise
end

end
