%% Load initial values
initial;
%% Choice of the control type and configuration

Case=0; %0:valve control, 1:flow control

PIControl=0; %0:off, 1:on
MPControl=0; %0:off, 1:on
Filters=0;   %0:off, 1:on

if MPControl ==1 %avoid errors, MPC not to be used with PIs or filters
    Filters=0;
    PIControl=0;
end

%Activation time for controllers
tPIControl=2000;
tMPControl=2000;

%% Controllers tuning

if Case==0
    load('MPC6010.mat'); %MPC
    if Filters==1        % PIs
        SIMCtuning;
    else
        SIMCtuningnf;
    end
else
    load('MPC6010flow.mat');
    if Filters==1;
        SIMCtuningflow;
    else
        SIMCtuningflownf;
    end
end

%% Filters time constants [seconds]

tm=5; %temperature sensor time constant

if Filters==1 
   taT0=200; 
   taF1in=20; 
   taF2in=240; 
   taEin=40;
   taAin=100; 
   taBin=240;
   taCin=200; 
   taD1in=160; 
   taD2in=320; 
    tfF=30;
    tfA=30;
    tfB=30;
    tfC=10;
    tfD=10;
    tfE=100;
else
   taT0=0;
   taF1in=0;
   taF2in=0;
   taEin=0;
   taAin=0;
   taBin=0;
   taCin=0;
   taD1in=0;
   taD2in=0;
    tfF=0;
    tfA=0;
    tfB=0;
    tfC=0;
    tfD=0;
    tfE=0;
end

%% Initial values for Self-optimizing control
%Overwriting of the Mongstad initial values 

if PIControl+MPControl == 1

zAin=0.321952537153837;
zBin=0.567990778724905;
zCin=0.523955679372974;
zDin=0.802799870251506;
zEin=0.442765317157139;
zin=[zAin zBin zCin zDin zEin];

ztotin=zAin+zBin+zCin+zDin+zEin+zFin;
mAin=0.088007924682618*m0;
mBin=0.155273650881162*m0;
mCin=0.143229693127898*m0;
mDin=0.219459994392951*m0;
mEin=0.121049941148732*m0;

end

%% Run Simulink

if Case == 0
    sim('Valvecontrol') %Simulink file for valve control
else
    sim('Flowcontrol') % Simulink file for flow control
end