function[initPipe,     ConAnPipe,...
         initMakeUp,   ConAnMakeUp,...
         initSep,      ConAnSep,...
         initReact,    ConAnReact,...
         initComb,     ConAnComb]...
          = InitSamp(par,scl,con,ther,def,itg,dirmain,Ver)

% This function returns initial values and annonymous functions for the 
% submodels.

% Written by Julian Straus, Trondheim 09.12.2018

% The input structures are defined as
%       par:    Parameters used in the calculation, output of the system
%       scl:    Scaling factors of the system, must include:
%               .Q      Scaling factor for the flow
%               .p      Scaling factor for the pressure
%               .T      Scaling facto for the temperature
%               .W      Scaling factor for the pwer
%       con:    Physical constants, must contain
%               .R      Ideal Gas constant
%       ther:   Thermodynamical values to calculate henry coefficients of
%               the supercritical components, must include:
%               .Hi    	Henry coefficient, i = 1-3
%       def:    Defined stream values, e.g pressure and composition for all
%               inlets and the pressure of the outlets or the flow of the
%               inlets
%       itg:    Intiger values, which should include
%               .k    	Total number of variables per stream
%               .j     	Total number of components in each stream

% Load of the initial values from the flowsheet
load([dirmain,'\InitTot_FO_MakeUpSec.mat'])         % Solution for the MakeUp Section
load([dirmain,'\InitTot_FO_ReacSec_n_12=10.mat'])   % Solution for the Reaction Section
load([dirmain,'\InitTot_FO_SepSec.mat'])            % Solution for the Separation Section

% Addition of the path of the reduced order unit operations
UO = [dirmain,'\Util4. Unit Operations\Simulation'];
addpath(UO)

%% Definition of the anonymous functions for the definition files
def.P.fun = @(nvar,into,colind) DefPipe(nvar,into,colind,scl.P,def.P,itg);
def.M.fun = @(nvar,into,colind) DefMakeUp(nvar,into,colind,scl.M,def.M);
def.R.fun = @(nvar,into,colind) DefReact(nvar,into,colind,scl.R,def.R);
def.S.fun = @(nvar,into,colind) DefSep(nvar,into,colind,scl.S,def.S);

%% Definition of the anonymous functions for the full order models
ConAnPipe   = @(a) ConPipe(a,par.P,scl.P,con,[],def.P,itg);
ConAnMakeUp = @(x) ConMakeUp(x,par.M,scl.M,con,[],def.M,itg); 
ConAnReact  = @(x) ConReact(x,par.R,scl.R,con,[],def.R,itg);
ConAnSep    = @(x) ConSep(x,par.S,scl.S,con,ther,def.S,itg);

%% Reassignment of the variables

% Calculation of the values for the MakeUp section
x.Comp1 = initMakeUpSaved(1:16);
x.HEx1  = initMakeUpSaved(17:40);
x.Comp2 = initMakeUpSaved(41:48);
x.Mix1  = initMakeUpSaved(49:64);
x.Comp3 = initMakeUpSaved(65:72);

% Calculation of the values for the Reaction section
x.HEx2   = initReactSaved(1:32);
x.Split1 = initReactSaved(33:56);
x.Val1   = initReactSaved(57:65);
x.HEx3   = initReactSaved(66:90);
x.HEx4   = initReactSaved(91:114);
x.Mix2   = initReactSaved(115:122);
x.Reac1  = initReactSaved(123:194);
x.Reac2  = initReactSaved(195:266);
x.HEx5   = initReactSaved(267:282);

% Calculation of the values for the Separation section
x.HEx6   = initSepSaved(1:32);
x.Split2 = initSepSaved(33:48);
x.HEx7   = initSepSaved(49:73);
x.Split3 = initSepSaved(74:89);
x.HEx8   = initSepSaved(90:113);
x.HEx9   = initSepSaved(114:137);
x.Mix3   = initSepSaved(138:145);
x.HEx10  = initSepSaved(146:169);
x.Sep    = initSepSaved(170:177);

%% Definition of the initial values for the different sections using the
% full order models

initPipe_In   = [def.P.FPipe; def.P.FPipe];
initMakeUp_In = [x.Comp1; x.HEx1; x.Comp2; x.Mix1; x.Comp3];
initReact_In  = [x.HEx2; x.Split1; x.Val1; x.HEx3; x.HEx4; x.Mix2; x.Reac1; x.Reac2; x.HEx5];
initSep_In    = [x.HEx6; x.Split2; x.HEx7; x.Split3; x.HEx8; x.HEx9; x.Mix3; x.HEx10; x.Sep];
initComb_In   = [initMakeUp_In; initReact_In; initSep_In];


%% Definition of the initial values and anonymous function of the MakeUp section
% depending on the chosen scaling

% Rescaling of the initial values
if Ver.model(1:3) == 'Pip'
    initPipe = initPipe_In./scl.P{1}.Var';
else
    initPipe = [];
end
initMakeUp = initMakeUp_In./scl.M{11}';
initReact  = initReact_In./scl.R{19}';
initSep    = initSep_In./scl.S{19}';
initComb   = initComb_In./[scl.M{11} scl.R{19} scl.S{19}]';

%% Definition of the anonymous functions for the combined flowsheet
def.M.fun = @(nvar,into,colind) DefMakeUp_Comb(nvar,into,colind,scl.M,def.M);
def.R.fun = @(nvar,into,colind) DefReact_Comb(nvar,into,colind,scl.R,def.R);
def.S.fun = @(nvar,into,colind) DefSep_Comb(nvar,into,colind,scl.S,def.S);
itg.nvar = numel(initComb);
ConAnComb = @(x) ConComb(x,par,scl,con,ther,def,itg);
