function [int,UOVec,Par,Q]= ReassigFlex(x,fun,name,scl,itg)

% Reassignment of variables
% Written by Julian Straus, Trondheim 16.09.2015

% This function reassign and scales the results of the optimization/solving
% procedure. If more units area added, it can be updated similar to the
% Constraints flowsheet. it is multifunctional and can work independently
% of the 

% The needed input is defined as
%       x:      Solution vector
%       fun:    fun@(x) giving the into values at third position
%       name:   Cell containing the names of the unit operations

% The necessary structures are defined as
%       scl:    Scaling factors of the system, must include:
%               .M2   	Scaling matrix for 2 streams
%               .M3     Scaling matrix for 3 streams
%               .M4     Scaling matrix for 4 streams

% Calculation of the into values based on the function
[~,~,~,~,~,~,~,into_C,x_C] = fun(x);

% Predefinition of the cells and values used
n = length(name);
Vec = cell(n,1);
dum = Vec;
pdum = Vec;
Qdum = Vec;
emptyCell = false(1,n);
emptyp = false(1,n);
emptyQ = false(1,n);

% Redefinition of the names
nameVec = name;
namep = name;
nameQ = name;

% Checking, if combined or not
if iscell(into_C{1})
    into = [into_C{1};into_C{3};into_C{5}];
    x    = [x_C{1};x_C{3}; x_C{5}];
else
    into = into_C;
    x    = x_C;
end

% Scanning and Scaling of the values
for k = 1:n
    if iscell(into_C{1})
       if k <= numel(into_C{1})
           sclU = scl.MR;
       elseif k <= numel(into_C{1})+numel(into_C{3})
           sclU = scl.RR;
       else
           sclU = scl.SR;
       end
    else
        sclU = scl;
    end
    if isempty(name{k}) || strcmp(name{k}(end-2:end),'Def')
    
    elseif strcmp(name{k}(1:3),'HEx') && any(strcmp(name{k}(4:end),{'5'; '8'; '9'; '10'}))
        dum{k} = reshape(into{k}([1:8 10:33]),itg.k,[]).*sclU.M4;
        Vec{k} = dum{k}(1:8,:)';
        pdum{k} = into{k}(9);
        Qdum{k} = into{k}(34);
        
    elseif strcmp(name{k}(1:3),'HEx')
        dum{k} = reshape(into{k}(1:32),itg.k,[]).*sclU.M4;
        Vec{k} = dum{k}(1:8,:)';
        if mod(numel(x{k}),itg.k) ~= 0
            pdum{k} = x{k}(end-mod(numel(x{k}),itg.k)+1:end)*sclU.Kpd;
        end
        
    elseif strcmp(name{k}(1:3),'Mix')
        dum{k} = reshape(into{k},itg.k,[]).*(sclU.M1*ones(1,length(into{k})/itg.k));
        Vec{k} = dum{k}(1:6,:)';
        
    elseif strcmp(name{k}(1:3),'Sep')
        dum{k} = reshape(into{k},itg.k,[]).*sclU.M3;
        Vec{k} = dum{k}';
        
    elseif strcmp(name{k}(1:3),'Val')
        dum{k} = reshape(into{k},itg.k,[]).*sclU.M2;
        Vec{k} = dum{k}(1:3,:)';
        if mod(numel(x{k}),itg.k) ~= 0
            pdum{k} = x{k}(end-mod(numel(x{k}),itg.k)+1:end)*sclU.Kpd;
        end
        
    elseif strcmp(name{k}(1:4),'Comp')
        dum{k} = reshape(into{k}([1:8 11:18]),itg.k,[]).*sclU.M2;
        Vec{k} = dum{k}(1:3,:)';
        Qdum{k} = into{k}(10);
        pdum{k} = into{k}(9);
                
    elseif strcmp(name{k}(1:4),'Reac')
        dum{k} = reshape(into{k},itg.k,[]).*(sclU.M1*ones(1,length(into{k})/itg.k));
        Vec{k} = dum{k}(1:6,[1 end])';
        
    elseif strcmp(name{k}(1:5),'Split')
        nratio = rem(numel(into{k}),itg.k);
        dum{k} = reshape(into{k}(1:end-nratio),itg.k,[]).*(sclU.M1*ones(1,(length(into{k})-nratio)/itg.k));
        Vec{k} = dum{k}(1:3,:)';
        pdum{k} = into{k}(end-nratio+1:end);

    elseif strcmp(name{k}(1:6),'NN_Sep')
        dum{k} = reshape(into{k}(1:40),itg.k,[]).*sclU.M5;
        Vec{k} = dum{k}(:,[1 2 4 3 5])';
        pdum{k} = into{k}(41:44);
        Qdum{k} = into{k}(45:47);
    elseif strcmp(name{k}(1:7),'NN_Reac')
        dum{k} = reshape(into{k}(1:32),itg.k,[]).*sclU.M4;
        Vec{k} = dum{k}(:,2:3)';
        pdum{k} = into{k}(33:35);
        Qdum{k} = into{k}(36);
    end
    emptyCell(k) = isempty(Vec{k});
    emptyp(k) = isempty(pdum{k});
    emptyQ(k) = isempty(Qdum{k});
end

% Removal of empty fiels given in the definition fields
Vec(emptyCell) = [];
dum(emptyCell) = [];
pdum(emptyp) = [];
Qdum(emptyQ) = [];
nameVec(emptyCell) = [];
namep(emptyp) = [];
nameQ(emptyQ) = [];


% Redefinition of the values as structures
int = cell2struct(dum,nameVec);
UOVec = cell2struct(Vec,nameVec);
Par = cell2struct(pdum,namep);
Q = cell2struct(Qdum,nameQ);
