
%% Nominal values

m1_nom = 5.975; % [kg/s] inlet flow of boiler
m2_nom = m1_nom; % [kg/s] outlet flow of boiler
mbypass_nom = 0.2649; % bypass stream [kg/s]
z_nom = 0.8911;  %  Valve opening 
Qc_nom =  -1.4976e+07; % [J/s] 
mG_nom = 7; % flue gas in HEX [kg/s] 
mG2_nom = 7; % flue gas in reheater [kg/s] 

%% Initial guess

M1_0 = 1000; % [kg]
H1_0 = 2.9319e+09; % [J]
T1_0 = 584.0833; % [K]
Mmix_0 = 253; % [kg]
T2_0 = 802.1171; % [K]
P2_0 = 9.8001e+06; % [Pa]
Mturbine_0 = 1000; % [kg]
P3_0 =  9.2919e+06; % [Pa]
P4_0 = 0.096e+05; % [Pa]
Mc_0 = 983.8729; % [kg] 
T0_0 = 45 + 273.15; % [K]
Ms_0 = 90.109;  % [kg] 
TG2_0 = 272.5266; % [K]
Q1_0 =   1.7516e+07; % [J/s]
TG1_0 =  1.1066e+03; % [K]
Q2_0 =   3.4973e+06; % [J/s]
T2hot_0 =  876.7845; % [K]
T3mid_0 = 613.8158; %[K]
TG3_0 =  1.0975e+03; % [K]
Q3_0 =  3.6894e+06; % [J/s]
P0_0 = 130e5; % Pressure after pump [Pa] 

%% Setpoints for the controllers

M1s = 1000; % [kg]
T2s = 802; % [K]
P1s = 9.9241e+06; % [Pa] 
Ws_s =  -9.722226921410142e+06; %[W] 
T3mid_s = 613.9; % [K]
T4s = 45 + 273.15; % [K]

%% Controller gain

Kc1 = 0.14654*0;
Kcbypass = -0.1082*0;
KcQc = 1.98432e6*0;
KcReheater = 0.28*0;

tc=1;
Kcz = -6.8538e-6*(1/tc)*0;
tauiz = min(3,4*tc); 
KcWs = -2.162e-7*0;

CheckControllerVector = [Kc1 Kcbypass KcQc KcReheater Kcz KcWs]
%% Step in inputs

m1_step = 0.1*m1_nom*0; % [kg/s]
mbypass_step = 0.1*mbypass_nom*0; % [kg/s] 
z_step = 0.1*z_nom*0; % [-]
mG_step = -0.10*mG_nom*0; % [kg/s] 
mG2_step = 0.1*mG2_nom*0;
Qc_step = 0.01*Qc_nom*0; % [J/s] 

m1_step_time = 50; % [s]
mbypass_step_time = 1500; % [s]
z_step_time = 1500; % [s]
mG_step_time = 1500; % [s]
mG2_step_time = 1000; % [s)
Qc_step_time = 1000; % [s]

CheckStepVector = [m1_step mbypass_step z_step mG_step mG2_step Qc_step]

%% Step in setpoints M1s and T2s

M1s_step = 0.1*M1s*0;
M1s_step_time = 1000;

T2s_step = 0.1*T2s*0;
T2s_step_time = 1000;

T3mid_step = 0.1*T3mid_s*0;
T3mid_step_time = 1000;

Ws_s_step = 0.1*Ws_s*0;
Ws_s_step_time = 1000;

P1s_step = 0.1*P1s*0;
P1s_step_time = 1000;

T4s_step = 0.1*T4s*0;
T4s_step_time = 1000;
%% Parameters

Cpg = 2e3; % Heat capacity steam (gas)  [J/kg,K] 
dHvap45 = 2400e3; % Heat of vap. at 45C  [J/kg]
Antoine.A = 5.11564; % Antoine parameters
Antoine.B = 1687.537;
Antoine.C = -42.98;
Mmwater = 18e-3; % Mole weight water [kg/mol]
R = 8.3145; % Gas constant [J/mol,K]
Vmix = 10; % Volume in mixer and turbine holdup
CpgG = 3e3; % Heat capacity methane [J/kg,K]
TG0 = 1000 + 273.15; % Initial temperature gas [K]
UA1 = 1.6605e+05; % [W/m2K]
UA2 = 7.6121e+03; % [W/m2K]
UA3 = 5.1290e+03; % [W/m2K]

Km2 =  4.614084630048991e-05; % Valve coefficient for m2
Km3 =  1.380619362612537e-05; % Valve coefficient for m3
%Km3 =  (m2_nom + mbypass_nom)/(z_nom*(P2_0-P3_0));
%Km4 =  (m2_nom + mbypass_nom)/(P4_0-P0_0); 
Km4 = -4.803026072265384e-07; % Valve coefficient for m4
P = [Cpg dHvap45 Antoine.A Antoine.B Antoine.C P4_0 Km3 Mmwater R Vmix CpgG TG0 UA1 UA2 UA3];

Np = size(P,2);

%Km2 = m2_nom/(AntoineEq(T1_0,P)-P2_0);


%% Gain matrices

Mdyn = [1 0 0; 0 1 0];  
Malg = [0 0 1];