% This file defines the parameters and scaling values of the different 
% unit operations of the of the simplified ammonia cycle. In addition, it 
% adds the directories to the path for the evaluation using Splinter, the
% saved BSplines and the sampling functions. The core of this function is
% similar to the one in "Prog1. Adaptive Sampling", however, minor changes 
% are present.

% Written by Julian Straus, Trondheim 09.12.2018

% The necessary structures are defined as

%       par:    Parameters used in the calculation, output of the system
%       ther:   Constants used n thermodynamical values
%       scl:    Scaling factors and matrices
%       con:    Thermodynamic constants

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition of the current path folder
FileName = mfilename('fullpath');
[directory,~,~] = fileparts(FileName);
[dirmain,~,~] = fileparts(directory);
rmpath(genpath(dirmain))
clc

% Definition of the direction of Splinter and the bsplines for the
% compressor curves
addpath(genpath([dirmain,'\Util1. Splinter 3.0']))  
dircompcurv = [dirmain,'\Util2. Bsplines and Datatables\Compressor Curves 3.0'];    
mHvalues = [dirmain,'\Util2. Bsplines and Datatables\Mass Enthalpy 3.0'];       

% Addition of the sampling functions
addpath(genpath([dirmain,'\Util3. Sampling functions']))

% Loading of Splinter
if ~Splinter.get_instance.is_loaded
    setup
end

% Addition of the reduced order, the constraint, and the definition files
% pathes.
addpath('Constraint Files\')
addpath('Definition Files\')
addpath('Inequality Files\')

% Definition of a local safe directory
dirsave = 'LocalSave\';

% Load of the neural networks used for analysis
% load([dirsave,'Rea_Net_20180901_Addsamp_dep_diff_all_SOB.mat'])
% load([dirsave,'Sep_Net_20180901_Addsamp_dep_diff_all_SOB.mat'])
% load([dirsave,'Rea_Net_20180901_Addsamp_dep_diff_all_RND.mat'])
% load([dirsave,'Sep_Net_20180901_Addsamp_dep_diff_all_RND.mat'])
load([dirsave,'Rea_Net_20180902_Addsamp_dep_diff_all_LHSaug.mat'])
load([dirsave,'Sep_Net_20180902_Addsamp_dep_diff_all_LHSaug.mat'])

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition of thermodynamical values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      
% Definition of the Henry constant polynomial's
ther.H1 = [-3.68607;...
           -2.29337;...
           -0.79410;...
           -1.67010];

ther.H2 = [0.596736;...
          0.529474;...
          0.447247;...
          0.440558]*10^4;

ther.H3 = [-0.642828;...
          -0.521881;...
          -0.509281;...
          -0.482973]*10^6;
          
% Definition of intiger values used in the calculations 
itg.j = 5;
itg.k = itg.j+3;
itg.M = 0;
itg.R = 0;
itg.S = 0;
itg.fig = 1;
k1 = itg.k;
k2 = itg.k*2;
k3 = itg.k*3;
k4 = itg.k*4;
      
% Definition of auxiliary variables
On1j = ones(1,itg.j);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition of the parameters for the different unit operations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition of the parameters for the MakeUp Section 

% Predefinition of the cells

% Names of the different unit
name.MakeUpSec = {'Comp1';'Comp1Def';'HEx1';'HEx1Def';'Comp2';'Comp2Def';...
                  'Mix1';'Mix1Def';'Comp3';'Comp3Def'};
def.M.sizeMat = size(name.MakeUpSec);
par.M = cell(def.M.sizeMat);

% Parameters of the Compressor 1 
par.M{1}.W  = BSpline([dircompcurv,'\C1W.BSpline']); 
par.M{1}.polyeff = BSpline([dircompcurv,'\C1eff.BSpline']);
par.M{1}.n  = BSpline([dircompcurv,'\C1exp.BSpline']);
par.M{1}.S  = 10288; 

% Parameters for the Interstage Heat Exchanger
par.M{3}.UA = 179390;
par.M{3}.cpHot = 29.4;          % Heat capacity hot stream in [J/mol/K]
par.M{3}.cpCold = 75.5;         % Heat capacity cold stream in [J/mol/K]
par.M{3}.kHot = 1.2873e-3;      % Pressure drop coefficient in [mol/s/Pa]
par.M{3}.kCold = 1.30e-2;       % Pressure drop coefficient in [mol/s/Pa]
par.M{3}.cond = 0;              % Adjustment for condensation in [MW]

% Parameters of the Compressor 2
par.M{5}.W  = BSpline([dircompcurv,'\C2W.BSpline']); 
par.M{5}.polyeff = BSpline([dircompcurv,'\C2eff.BSpline']);
par.M{5}.n  = BSpline([dircompcurv,'\C2exp.BSpline']);
par.M{5}.S  = par.M{1}.S; 

% Parameter for the Mixer 1
par.M{7}.n = 2;
par.M{7}.Conc = 'y';

% Parameters of the Compressor 3
par.M{9}.W  = BSpline([dircompcurv,'\C3W.BSpline']); 
par.M{9}.polyeff = BSpline([dircompcurv,'\C3eff.BSpline']);
par.M{9}.n  = BSpline([dircompcurv,'\C3exp.BSpline']);
par.M{9}.S  = par.M{1}.S; 
       

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition of the parameters for the Reaction Section 

% Names of the different unit
name.ReactSec = {'HEx2';'HEx2Def';'Split1';'Split1Def';...
                 'Val1';'Val1Def';'HEx3';'HEx3Def';'HEx4';'HEx4Def';'Mix2';'Mix2Def';....
                 'Reac1';'Reac1Def';'Reac2';'Reac2Def';'HEx5';'HEx5Def'};
name.NNReactSec = {'NN_Reac';'NN_ReacDef'};
def.R.sizeMat = size(name.ReactSec);
par.R = cell(def.R.sizeMat);

% Parameters for the Recycle Heat Exchanger
par.R{1}.UA = 2.84e6;
par.R{1}.cpHot = 31;            % Heat capacity hot stream in [J/mol/K]
par.R{1}.cpCold = 31;           % Heat capacity cold stream in [J/mol/K]
par.R{1}.kHot = 5.39*1e-3;      % Pressure drop coefficient in [mol/s/Pa]
par.R{1}.kCold = 4.4198*1e-3;   % Pressure drop coefficient in [mol/s/Pa]
par.R{1}.cond = 0;              % Adjustment for condensation in [MW]

% Parameters of the Split 1
par.R{3}.ratio = [9.7109; 56.6694; 0];
par.R{3}.Num = 1;

% Parameters for Valve 1
par.R{5}.K = 7.2211e-04; %+7.337609477690828e-04; %

% Parameters for the Pre/Post Reactor Heat Exchanger
par.R{7}.UA = 3.6e5;
par.R{7}.cpHot = 33.3;         	% Heat capacity hot stream in [J/mol/K]
par.R{7}.cpCold = 30.6;         % Heat capacity cold stream in [J/mol/K]
par.R{7}.kHot = 6.5*1e-3;       % Pressure drop coefficient in [mol/s/Pa]
par.R{7}.kCold = 4.214*1e-3;    % Pressure drop coefficient in [mol/s/Pa]
par.R{7}.cond = 0;              % Adjustment for condensation in [MW]

% Parameters for the interstage Reactor Heat Exchanger
par.R{9}.UA = 1.27e5;
par.R{9}.cpHot = 32.9;          % Heat capacity hot stream in [J/mol/K]
par.R{9}.cpCold = 30.6;         % Heat capacity cold stream in [J/mol/K]
par.R{9}.kHot = 8.428*1e-3;     % Pressure drop coefficient in [mol/s/Pa]
par.R{9}.kCold = 2.5*1e-3;      % Pressure drop coefficient in [mol/s/Pa]
par.R{9}.cond = 0;              % Adjustment for condensation in [MW]

% Parameter for the Mixer 2
par.R{11}.n = 3;
par.R{11}.Conc = 'n';

% Parameters for Reactor 1
par.R{13}.n = Reac1_n;          % Number of sequential CSTRs in #
par.R{13}.Mr = 20.4;            % Reactor holdup in [m^3]
par.R{13}.cool  = 5e6;          % Cooling in [MW]
% Definition of the forward reaction constants
par.R{13}.for.A = 6.9195/1e5*400;
par.R{13}.for.Ea = 11200;       % Activation Energy in [J/mol]
par.R{13}.for.b = -1;
% Definition of the backward reaction constants
par.R{13}.bac.A = 6.1530e12*1e5*400;
par.R{13}.bac.Ea = 116180;      % Activation Energy in [J/mol]
par.R{13}.bac.b = -1;
par.R{13}.stoi = [-3 -1 2 0 0]';% Stoichometric coefficients of the reaction
% Definition of the parameters for the CSTR cascade
par.R{13}.cp = 33;              % Cp of the mixture in [J/mol/K]
par.R{13}.dH = -91420;          % Reaction enthalpy in [J/mol]
par.R{13}.k = 0.0332/sqrt(20);  % Pressure drop coefficient in [mol/s/Pa]

% Parameters for Reactor 2
par.R{15}.n = Reac2_n;          % Number of sequential CSTRs in #
par.R{15}.Mr = 47.72;           % Reactor holdup in [m^3]
par.R{15}.cool  = 5e6;          % Cooling in [MW]
% Definition of the forward reaction constants
par.R{15}.for.A = 2.3843/1e5*900;
par.R{15}.for.Ea = 11200;       % Activation Energy in [J/mol]
par.R{15}.for.b = -1;
% Definition of the backward reaction constants
par.R{15}.bac.A = 2.1131*1e12*1e5*900;
par.R{15}.bac.Ea = 116180;      % Activation Energy in [J/mol]
par.R{15}.bac.b = -1;
par.R{15}.stoi = [-3 -1 2 0 0]';% Stoichometric coefficients of the reaction
% Definition of the parameters for the CSTR cascade
par.R{15}.cp = 33;              % Cp of the mixture in [J/mol/K]
par.R{15}.dH = -91420;          % Reaction enthalpy in [J/mol]
par.R{15}.k = 0.03405/sqrt(20); % Pressure drop coefficient in [mol/s/Pa]

% Parameters for the primary cooling for steam production (HEx5)
par.R{17}.Tref = 160.0;         % Reference temperature in [C]
par.R{17}.dH = 439.5100;        % Mass enthalpy hot stream in [J/g]
par.R{17}.Hin = BSpline([mHvalues,'\MassEnth1.BSpline']);
par.R{17}.Hout = BSpline([mHvalues,'\MassEnth2.BSpline']);
par.R{17}.kHot = 2.156*1e-3;    % Pressure drop coefficient in [mol/s/Pa]

% Parameters for the aritifical neural networks
par.R{21}.stoi  = [-3 -1 2 0 0]';% Stoichometric coefficients of the reaction
par.R{21}.net   = Rea.combnet;  % Neural Network for the dependent variables
par.R{21}.xload = Rea.mult;     % PLSR loads for the dependent variables
par.R{21}.diff  = Rea.Ver.diff; % Variable for difference or not
par.R{21}.dep   = Rea.Ver.dep;  % Variable for dependency or not

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition of the parameters for the Separation Section 

% Names of the different unit
name.SepSec = {'HEx6';'HEx6Def';'Split2';'Split2Def';'HEx7';'HEx7Def';'Split3';'Split3Def';...
               'HEx8';'HEx8Def';'HEx9';'HEx9Def';'Mix3';'Mix3Def';'HEx10';'HEx10Def';...
               'Sep';'SepDef'};
name.NNSepSec = {'NN_Sep';'NN_SepDef';'';'';'';'';'Split3';'Split3Def'};
def.S.sizeMat = size(name.SepSec);
par.S = cell(def.S.sizeMat);

% Parameters for the precooling of the separation section
par.S{1}.UA = 3.05e5;
par.S{1}.cpHot = 33;            % Heat capacity hot stream in [J/mol/K]
par.S{1}.cpCold = 75.4;         % Heat capacity cold stream in [J/mol/K]
par.S{1}.kHot = 5.88e-3;        % Pressure drop coefficient in [mol/s/Pa]
par.S{1}.kCold = 5e-2;          % Pressure drop coefficient in [mol/s/Pa]
par.S{1}.cond = 0;0.8;            % Adjustment for condensation in [MW]

% Parameters of the Split 2
par.S{3}.ratio = [0;53.3784];
par.S{3}.Num = 2;

% Parameters for the recycle heat integration
par.S{5}.UA = 1.1762e+06;
par.S{5}.cpHot = 34;            % Heat capacity hot stream in [J/mol/K]
par.S{5}.cpCold = 31;           % Heat capacity cold stream in [J/mol/K]
par.S{5}.kHot = 2.3226e-3;      % Pressure drop coefficient in [mol/s/Pa]
par.S{5}.kCold = 5.88e-3;       % Pressure drop coefficient in [mol/s/Pa]
par.S{5}.cond = 0;2.6957;         % Adjustment for condensation in [MW]

% Parameters of the Split 3
par.S{7}.ratio = [0; 1.7174];   % Purge in [%]
par.S{7}.Num = 3;

% Definition of the ratio between the pressure drop given by HEx8 and HEx9
S.r = .6;
S.A = 2.0286e-3;
S.B = 1/sqrt(S.r/S.A^2);
S.C = 1/sqrt((1-S.r)/S.A^2);

% Parameters for the parallel heat exchanger
par.S{9}.Tref = 13.54;          % Reference temperature in [C]
par.S{9}.dH = 121.4;            % Mass enthalpy hot stream in [J/g]
par.S{9}.Hin = BSpline([mHvalues,'\MassEnth3.BSpline']);
par.S{9}.Hout = BSpline([mHvalues,'\MassEnth4.BSpline']);
par.S{9}.kHot = S.B; 2.6189e-3; % Pressure drop coefficient in [mol/s/Pa]

% Parameters for the parallel heat exchanger # 2
par.S{11}.Tref = -3.542;        % Reference temperature in [C]
par.S{11}.dH = 108.5;           % Mass enthalpy hot stream in [J/g]
par.S{11}.Hin = BSpline([mHvalues,'\MassEnth4.BSpline']);
par.S{11}.Hout = BSpline([mHvalues,'\MassEnth5.BSpline']);
par.S{11}.kHot = S.C; 3.2075;   % Pressure drop coefficient in [mol/s/Pa]

% Parameter for the Mixer 3
par.S{13}.n = 2;
par.S{13}.Conc = 'n';

% Parameters for the final heat exchanger
par.S{15}.Tref = -17.10;        % Reference temperature in [C]
par.S{15}.dH = 92.89;           % Mass enthalpy hot stream in [J/g]
par.S{15}.Hin = BSpline([mHvalues,'\MassEnth5.BSpline']);
par.S{15}.Hout = BSpline([mHvalues,'\MassEnth6.BSpline']);
par.S{15}.kHot = 4.9e-3;        % Pressure drop coefficient in [mol/s/Pa]

% Parameters for the aritifical neural networks
par.S{21}.net   = Sep.combnet;  % Neural Network for the dependent variables
par.S{21}.xload = Sep.mult;     % PLSR loads for the dependent variables
par.S{21}.diff  = Sep.Ver.diff; % Variable for difference or not
par.S{21}.dep   = Sep.Ver.dep;  % Variable for dependency or not

% Defining all pressure drop coefficients in a vector
par.pdc = [par.M{3}.kHot;...
           par.R{1}.kHot;...
           par.R{1}.kCold;...
           par.R{7}.kHot;...
           par.R{7}.kCold;...
           par.R{9}.kHot;...
           par.R{9}.kCold;...
           par.R{17}.kHot;...
           par.S{1}.kHot;...
           par.S{5}.kHot;...
           par.S{5}.kCold;...
           par.S{9}.kHot;...
           par.S{11}.kHot;...
           par.S{15}.kHot]; 

% Manipulating the value
par.pdc = par.pdc*1;

% Reassignment of the pressure drop 
par.M{3}.kHot   = par.pdc(1);
par.R{1}.kHot   = par.pdc(2);
par.R{1}.kCold  = par.pdc(3);
par.R{7}.kHot   = par.pdc(4);
par.R{7}.kCold  = par.pdc(5);
par.R{9}.kHot   = par.pdc(6);
par.R{9}.kCold  = par.pdc(7);
par.R{17}.kHot   = par.pdc(8);
par.S{1}.kHot   = par.pdc(9);
par.S{5}.kHot   = par.pdc(10);
par.S{5}.kCold  = par.pdc(11);
par.S{9}.kHot   = par.pdc(12);
par.S{11}.kHot   = par.pdc(13);
par.S{15}.kHot  = par.pdc(14);

% Defintion of natural constants
con.R  = 8.314;                 % in [J/mol/k]
con.M = [2 28 17 40 16];        % in [g/mol]
pow = 2;
     
% Name of the overall flowsheet units
name.Comb = [name.MakeUpSec(:); name.ReactSec(:); name.SepSec(:)];
name.CombNN = [name.MakeUpSec(:); name.NNReactSec(:); name.NNSepSec(:)];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition of scaling factors for the calculation, scaling matrices for
% the reassignment files, and natural constants
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Definition of scaling factors
scl.safe.W = 10^6;              % Scale Power to MW
scl.safe.p = 10^7;              % Scale pressure to hbar
scl.safe.Q = 10^4;              % Scale flowrate to 10 kgmole/s
scl.safe.T = 10^3;              % Scale temperature to kC
scl.safe.Kpd = 1e-3;           	% Scale factor for pressure drop coefficients
scl.safe.pin = scl.safe.p/10^5;
scl.safe.Stream = [scl.safe.Q; scl.safe.p; scl.safe.T; ones(itg.j,1)];  % Total scaling vector

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition of scaling vectors for the MakeUp Section

scl.p = 1.5e6;                  % Scale pressure to 1.5 dabar
scl.Q = 8e3;                    % Scale flowrate to 10 kgmole/s
scl.T = 1e2;                    % Scale temperature to kC
scl.Kpd = 1e-3;                 % Scale factor for pressure drop coefficients
scl.pin = scl.p/10^5;
scl.W = 1e7;                    % Scale factor for compressor power
scl.Sp = 1e4;                   % Scale factor for compressor speed
scl.V = [scl.Q scl.p scl.T ones(1,itg.j)];

% Definition of scaling matrices for the reassignment of variables for the
% makeup section
scl.MR.M1 = [scl.Q; scl.pin; scl.T; 100*ones(5,1)];% Total scaling vector
scl.MR.M2 = [scl.MR.M1 scl.MR.M1];       	% Scaling vector for 2 streams
scl.MR.M3 = [scl.MR.M2 scl.MR.M1];          % Scaling vector for 3 streams
scl.MR.M4 = [scl.MR.M2 scl.MR.M2];          % Scaling vector for 4 streams
scl.MR.S  = par.M{1}.S;                     % Scaling Factor for compressor speed
scl.MR.W  = scl.W;                          % Scaling Factor for compressor duty

scl.M{1}.Var  = [scl.V scl.Sp scl.W scl.V];
scl.M{1}.Eq   = 1./[scl.Q; scl.p*10; scl.T; scl.W; scl.Q*ones(5,1)];
scl.M{1}.DefEq = 1./[scl.Q; scl.p; scl.T; ones(5,1); scl.Sp];
scl.M{1}.InEq = 1./[scl.Sp; scl.Sp];

scl.M{3}.Var  = [scl.M{1}.Var(end-k1+1:end) ...
                 scl.V scl.V scl.V scl.Kpd scl.Kpd];
scl.M{3}.Eq   = 1./[scl.Q; scl.Q^pow; scl.Q*scl.T; scl.Q*ones(5,1);...
                    scl.Q; scl.Q^pow*10; scl.Q*scl.T*10; scl.Q*ones(5,1)];
scl.M{3}.DefEq = 1./[scl.Q; scl.p; scl.T; ones(5,1)];

scl.M{5}.Var  = [scl.M{3}.Var(k1+1:k2) ...
                 scl.Sp scl.W ...
                 scl.V];
scl.M{5}.Eq   = 1./[scl.Q; scl.p*10; scl.T; scl.W; scl.Q*ones(5,1)];
scl.M{5}.DefEq = 1./scl.Sp;

scl.M{7}.Var  = [scl.M{5}.Var(end-k1+1:end) ...
                 scl.V scl.V];
scl.M{7}.Eq   = 1./[scl.Q; scl.p; scl.p; scl.T*scl.Q; scl.Q*ones(4,1); 1];
scl.M{7}.DefEq = 1./[scl.Q; scl.p; scl.T; ones(5,1)];

scl.M{9}.Var  = [scl.M{3}.Var(k2+1:k3) ...
                 scl.Sp scl.W ...
                 scl.V];
scl.M{9}.Eq   = 1./[scl.Q; scl.p*10; scl.T; scl.W; scl.Q*ones(5,1)];
scl.M{9}.DefEq = 1./scl.Sp;

scl.M{11} = [scl.M{1}.Var([1:end]) scl.M{3}.Var(k1+1:32) scl.M{5}.Var(k1+2:end) ...
             scl.M{7}.Var(k1+1:end) scl.M{9}.Var(k1+2:end)];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition of scaling vectors for the Reaction Section

scl.p = 1e6;                    % Scale pressure to 1.5 dabar
scl.Q = 1e4;                    % Scale flowrate to 10 kgmole/s
scl.T = 1e3;                    % Scale temperature to kC
scl.Kpd = 1e-2;                 % Scale factor for pressure drop coefficients
scl.pin = scl.p/10^5;
scl.Qt = 1e7;                   % Scale power to W
scl.V = [scl.Q scl.p scl.T ones(1,itg.j)];

% Definition of scaling matrices for the reassignment of variables for the
% makeup section
scl.RR.M1 = [scl.Q; scl.pin; scl.T; 100*ones(5,1)];% Total scaling vector
scl.RR.M2 = [scl.RR.M1 scl.RR.M1];       	% Scaling vector for 2 streams
scl.RR.M3 = [scl.RR.M2 scl.RR.M1];          % Scaling vector for 3 streams
scl.RR.M4 = [scl.RR.M2 scl.RR.M2];          % Scaling vector for 4 streams
scl.RR.Kpd = scl.Kpd;

scl.R{1}.Var = [scl.V scl.V scl.V scl.V scl.Kpd scl.Kpd];
scl.R{1}.Eq  = 1./[scl.Q; scl.Q^pow; scl.Q*scl.T*10; scl.Q*ones(5,1);...
                   scl.Q; scl.Q^pow; scl.Q*scl.T*10; scl.Q*ones(5,1)];
scl.R{1}.DefEq = 1./scl.V';

scl.R{3}.Var = [scl.R{1}.Var(k3+1:k4) ...
                scl.V scl.V scl.V ...
                100 100 100];
scl.R{3}.Eq  = 1./[scl.Q; scl.Q; scl.Q; scl.p; scl.T; ones(5,1);...
                   scl.p; scl.T; ones(5,1);...
                   scl.p; scl.T; ones(5,1)];
scl.R{3}.DefEq = 1./[100; 100];
scl.R{3}.InEq  = 1./[100; 100; 100; 100];
scl.R{3}.InEq_Un = 1./[100; 100; 100];

scl.R{5}.Var  = [scl.R{3}.Var(k1+1:k2) ...
                 scl.V scl.Kpd];
scl.R{5}.Eq   = 1./[scl.Q; scl.Q^pow; scl.T/scl.p; scl.Q*ones(5,1)];

scl.R{7}.Var = [scl.R{3}.Var(k2+1:k3) ...
                scl.V scl.V scl.V scl.Kpd scl.Kpd];
scl.R{7}.Eq  = 1./[scl.Q; scl.Q^pow*8; scl.Q*scl.T*10; scl.Q*ones(5,1);...
                   scl.Q; scl.Q^pow; scl.Q*scl.T*10; scl.Q*ones(5,1)];

scl.R{9}.Var = [scl.R{3}.Var(k3+1:k4) ...
                scl.V scl.V scl.V scl.Kpd scl.Kpd];
scl.R{9}.Eq  = 1./[scl.Q; scl.Q^pow*8; scl.Q*scl.T*10; scl.Q*ones(5,1);...
                   scl.Q; scl.Q^pow; scl.Q*scl.T*10; scl.Q*ones(5,1)];
scl.R{9}.DefEq = 1./scl.T;

scl.R{11}.Var = [scl.R{5}.Var(k1+1:k2) ...
                 scl.R{7}.Var(k3+1:k4) ...
                 scl.R{9}.Var(k3+1:k4) ...
                 scl.V];
scl.R{11}.Eq  = 1./[scl.Q; scl.p; scl.p; scl.p; scl.T*scl.Q; scl.Q*ones(4,1); 1];
scl.R{11}.DefEq = 1./scl.T;

scl.R{13}.Var = [scl.R{11}.Var(k3+1:k4) ...
                 reshape(scl.V'*ones(1,par.R{13}.n-1),1,[]) ...
                 scl.R{9}.Var(1:k1)];
scl.R{13}.Eq  = 1./reshape([scl.Q^2*100; scl.Q; scl.Q*scl.T*10; scl.Q*ones(5,1)]*....
                            ones(1,par.R{13}.n),[],1);
scl.R{13}.DefEq = 1./scl.T;

scl.R{15}.Var = [scl.R{9}.Var(k1+1:k2) ...
                 reshape(scl.V'*ones(1,par.R{13}.n-1),1,[]) ...
                 scl.R{7}.Var(1:k1)];
scl.R{15}.Eq  = 1./reshape([scl.Q^2*100; scl.Q; scl.Q*scl.T*10; scl.Q*ones(5,1)]*....
                            ones(1,par.R{15}.n),[],1);
scl.R{15}.DefEq = 1./scl.T;

scl.R{17}.Var = [scl.R{7}.Var(k1+1:k2) scl.T...
                 scl.R{1}.Var(1:k1) ...
                 scl.V scl.V scl.Qt scl.Kpd scl.Kpd];
scl.R{17}.Eq  = 1./[scl.Q; scl.Q^pow; scl.T; scl.Q*ones(5,1);...
                    scl.Q; scl.p; scl.T; ones(5,1);...
                    scl.Q; scl.p; scl.T; ones(5,1);...
                    scl.Qt];
scl.R{17}.DefEq = 1./scl.T;
scl.R{17}.InEq  = 1./[scl.T; scl.T];

scl.R{19} = [scl.R{1}.Var(1:k4) scl.R{3}.Var(k1+1:end-1) scl.R{5}.Var(k1+1:end) ....
             scl.R{7}.Var([k1+1:k4 end]) scl.R{9}.Var(k1+1:k4) scl.R{11}.Var(25:end) ...
             scl.R{13}.Var(k1+1:end-k1) scl.R{15}.Var(k1+1:end-k1) scl.R{17}.Var([9 18:k4+2])];

% Scaling values for the neural networks
scl.R{21}.U  = Rea.Var.U;
scl.R{21}.x  = Rea.Var.x;
scl.R{21}.Eq  = 1./[scl.Q; scl.p; scl.T; 0.8*scl.Q*ones(5,1); scl.Qt];
scl.R{21}.Var = [ones(1,8) scl.R{1}.Var(k1+1:k3) ones(1,8) scl.R{3}.Var(k4+1:end-1) scl.T scl.Qt];
scl.R{21}.DefEq = [scl.R{1}.DefEq; scl.R{3}.DefEq; scl.R{17}.DefEq];
scl.R{21}.InEq  = [scl.R{3}.InEq; scl.R{17}.InEq];

% Scaling values for the neural networks combined with the split
scl.R{23} = [scl.R{21}.Var([9:24 33:34]) scl.T scl.Qt];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition of the sacling factors for the Reaction Section using the HYSYS
% model

scl.RHy.in = [1e3 1 1e3*On1j 100 100 60 1e3 1];
scl.RHy.out = [1e3 1 1e3 1];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition of scaling vectors for the Separation Section

scl.p = 7e5;                    % Scale pressure to 7 bar
scl.Q = 1e4;                    % Scale flowrate to 10 kgmole/s
scl.T = 1e2;                    % Scale temperature to kC
scl.Kpd = 1e-2;                 % Scale factor for pressure drop coefficients
scl.pin = scl.p/10^5;
scl.Qt = 1e7;                   % Scale power to W
scl.V = [scl.Q scl.p scl.T ones(1,itg.j)];

% Definition of scaling matrices for the reassignment of variables for the
% makeup section
scl.SR.M1 = [scl.Q; scl.pin; scl.T; 100*ones(5,1)];% Total scaling vector
scl.SR.M2 = [scl.SR.M1 scl.SR.M1];       	% Scaling vector for 2 streams
scl.SR.M3 = [scl.SR.M2 scl.SR.M1];          % Scaling vector for 3 streams
scl.SR.M4 = [scl.SR.M2 scl.SR.M2];          % Scaling vector for 4 streams
scl.SR.M5 = [scl.SR.M4 scl.SR.M1];          % Scaling vector for 4 streams
scl.SR.Kpd = scl.Kpd;

scl.S{1}.Var = [scl.V scl.V scl.V scl.V scl.Kpd scl.Kpd];
scl.S{1}.Eq  = 1./[scl.Q; scl.Q^pow; scl.Q*scl.T*10; scl.Q*ones(5,1);...
                   scl.Q; scl.Q^pow; scl.Q*scl.T*10; scl.Q*ones(5,1)];
scl.S{1}.DefEq = 1./[scl.V'; scl.V'];

scl.S{3}.Var = [scl.S{1}.Var(k1+1:k2) ...
                scl.V scl.V 100 100];
scl.S{3}.Eq  = 1./[scl.Q; scl.Q; scl.p; scl.T; ones(5,1);...
                   scl.p; scl.T; ones(5,1)];
scl.S{3}.DefEq = 1./[100];
scl.S{3}.InEq  = 1./[100; 100];

scl.S{5}.Var = [scl.S{3}.Var(k2+1:k3) ...s
                scl.V scl.V scl.V scl.Kpd scl.Kpd];
scl.S{5}.Eq  = 1./[scl.Q; scl.Q^pow; scl.Q*scl.T*10; scl.Q*ones(5,1);...
                   scl.Q; scl.Q^pow; scl.Q*scl.T*10; scl.Q*ones(5,1)];

scl.S{7}.Var = [scl.S{5}.Var(k3+1:k4) ...
                scl.V scl.V 100 100];
scl.S{7}.Eq  = 1./[scl.Q; scl.Q; scl.p; scl.T; ones(5,1);...
                   scl.p; scl.T; ones(5,1)];
scl.S{7}.DefEq = 1./[100];
scl.S{7}.InEq  = 1./[100];

scl.S{9}.Var = [scl.S{3}.Var(k1+1:k2) ...
                scl.T scl.V scl.V scl.V scl.Qt scl.Kpd scl.Kpd];
scl.S{9}.Eq  = 1./[scl.Q; scl.Q^pow; scl.T; scl.Q*ones(5,1);...
                   scl.V'; scl.V'; scl.Qt];
scl.S{9}.DefEq = 1./scl.T;
scl.S{9}.InEq  = 1./[scl.T; scl.T];

scl.S{11}.Var = [scl.S{9}.Var(k1+1+1:k2+1) ...
                 scl.T scl.V scl.V scl.V scl.Qt scl.Kpd scl.Kpd];
scl.S{11}.Eq  = 1./[scl.Q; scl.Q^pow; scl.T; scl.Q*ones(5,1);...
                    scl.V'; scl.V'; scl.Qt];
scl.S{11}.DefEq = 1./scl.T;
scl.S{11}.InEq  = 1./[scl.T; scl.T];
 
scl.S{13}.Var = [scl.S{5}.Var(k1+1:k2) ...
                 scl.S{9}.Var(k1+1+1:k2+1) ...
                 scl.V];
scl.S{13}.Eq  = 1./[scl.Q; scl.p; scl.p; scl.Q*scl.T; scl.Q*ones(4,1); 1];

scl.S{15}.Var = [scl.S{13}.Var(k2+1:k3) ...
                 scl.T scl.V scl.V scl.V scl.Qt scl.Kpd scl.Kpd];
scl.S{15}.Eq  = 1./[scl.Q; scl.Q^pow; scl.T; scl.Q*ones(5,1);...
                    scl.V'; scl.V'; scl.Qt];
scl.S{15}.DefEq = 1./scl.T;
scl.S{15}.InEq  = 1./[scl.T; scl.T];
                  
scl.S{17}.Var = [scl.S{15}.Var(k1+1+1:k2+1) ...
                 scl.S{5}.Var(k2+1:k3) ...
                 scl.V];
scl.S{17}.Eq  = 1./[scl.Q; scl.p; scl.T; scl.p; scl.T;...
                    scl.Q*ones(4,1); [1000;1000;10;100;100]*scl.p; 1; 1];

scl.S{19} = [scl.S{1}.Var(1:k4) scl.S{3}.Var(9:end-1) scl.S{5}.Var(9:k4+1) ....
             scl.S{7}.Var(9:end-1) scl.S{9}.Var(9:k4+2) scl.S{11}.Var(9:k4+2) ...
             scl.S{13}.Var(17:end) scl.S{15}.Var(9:k4+2) scl.S{17}.Var(17:end)];

% Scaling values for the neural networks
scl.S{21}.U  = Sep.Var.U;
scl.S{21}.x  = Sep.Var.x;
scl.S{21}.Eq  = 1./[scl.Q; scl.p; scl.T; scl.Q*ones(5,1); ...
                    scl.Q; scl.p; scl.T; scl.Q*ones(5,1); ...
                    scl.Q; scl.p; scl.T; scl.Q*ones(5,1); ...
                    scl.Qt; scl.Qt; scl.Qt];
scl.S{21}.Var = [scl.S{1}.Var(1:8) scl.S{17}.Var(17:24) scl.S{1}.Var(17:24) ....
                 scl.S{5}.Var(25:32) scl.S{1}.Var(25:32) 100 scl.T scl.T scl.T scl.Qt scl.Qt scl.Qt];
scl.S{21}.DefEq = [scl.S{1}.DefEq; scl.S{3}.DefEq; scl.S{9}.DefEq; scl.S{11}.DefEq; scl.S{15}.DefEq];
scl.S{21}.InEq  = [scl.S{3}.InEq; scl.S{9}.InEq; scl.S{11}.InEq; scl.S{15}.InEq];

% Scaling values for the neural networks combined with the split
scl.S{23} = [scl.S{21}.Var(1:47) scl.S{7}.Var(9:end-1)];