function u = CalcBlock(x,v,d,p)
% static calculation block to solve for u = f(v,x,d,p)

V    = p(1);                                             % [m3] tank volume
rho  = p(2);                                              % [m3/kg] density
cp   = p(3);                                      % [kJ/kg/C] specific heat
A    = p(4);                     %[1/s] tuning param gen linear transf (vL)
tau0 = p(5);                                 % [s] tuning param IFAC. (vL0) 

T    = x;                                            % [C] tank temperature
T0   = d(1);                                       % inlet flow temperature

% switch variables to change between case
MV     = p(6);                           % switch between MVs; 1:u=q; 2=u=Q
transf = p(7);           % switch trasformations: 0=static; 1=lin; 2=fb lin

switch MV                                          % select if q or Q is MV
    case 1                                               % q is MV, Q is DV
        Q = d(2);                                     % [kW] heat flow rate
        switch transf                          % select transformation type
            case 0                           % static transformation
                u = Q/(rho*cp*(v-T0));                              % u = q
            case 1                                  % linear trasnformation
                u = (V*(v+A*T)-Q/(rho*cp))/(T0-T);                  % u = q
            case 2                                              % IFAC. vL0
                u = (V/tau0*(v-T)-Q/(rho*cp))/(T0-T);                 % u=q
            case 3    % integrating transformation. similar to feedback lin
                u = (V*v-Q/(rho*cp))/(T0-T);                        % u = q
            otherwise
        end
        
    case 2                                               % q is DV, Q is MV
        q = d(2);                                        % [m3/s] flow rate
        switch transf
            case 0                                  % static transformation
                u = q*rho*cp*(v-T0);                                  % u=Q
            case 1                                  % linear trasnformation
                u = rho*cp*(V*(v+A*T)-q*(T0-T));                    % u = Q
            case 2                                              % IFAC. vL0
                u = rho*cp*(V/tau0*(v-T)-q*(T0-T));                   % u=Q
            case 3    % integrating transformation. similar to feedback lin
                u = rho*cp*(V*v-q*(T0-T));                          % u = Q
            otherwise
        end
    otherwise
end
