function [U,x,OnN1,nsamp] = CalSep(U,x,ind,par,scl,con,opt,Ver,On1j)

% Calculation file - separation section
%
% This function calculations the sampling domain in other variables than its
% initial definition. Furthermore, it extracts the dependent variables from
% the solution vector of the nonlinear system of equations and calculates
% dependent variables which are not included in the nonlinear system.
%
% Written by Julian Straus, Trondheim 09.12.2018
%
% The main input and output is defined as
%       U:      Structure containing the sampling domain calculated using
%               several possible approaches for independent variables 
%               definition. The output includes as well the whole input.
%
%       x:      Structure containing the state variables and new calculated
%               variables which are not calculated inside the system of
%               nonlinear equations. The output includes as well the whole
%               input.
%
% The seconday output for post analysis is given:
%       OnN1:   Vector of ones with size nsamp x 1
%
%       nsamp:  Number of sampled points.
%
%
% The necessary input is defined as
%       ind:    Vector containing the indidces of the dependent variables
%               in x.
%
%       par:    Cell structure containing the parameters used in the 
%               calculation, output of the system.
%
%       scl:    Cell structure containing the scaling factors for each unit
%               operations. The assignment is given in par.ReactSec.
%
%       con:    Physical constants, must contain
%               .R      Ideal Gas constant
%       
%       opt:    Structure containing options.
%
%       Ver:    Structure containing options.
%
%       On1j:   Vector of ones with size 1 x j (Number of chemical
%               components.

% Calculation of the number of sampling points and of the independent 
% variables which are varied for the validation space
nsamp = size(U.tot,1);
OnN1  = ones(nsamp,1);

% Calculation of the total molar flow
Q = sum(U.tot(:,3:7),2);

% Calculation of the varied independent variables
U.samp = U.tot(:,ind.in);

% Calculation of the partial pressures and mole fractions
if isfield(opt,'dep')
    U.samp(:,4) = U.samp(:,3)./U.samp(:,4);
    U.Molfrac   = [Q U.tot(:,1:2) U.tot(:,3)./U.tot(:,4) U.tot(:,5:7)./(Q*ones(1,3)) ...
                   U.tot(:,8:end)];
    U.PartPress = [Q U.tot(:,1:2) U.tot(:,3)./U.tot(:,4) U.tot(:,5:7).*((U.tot(:,1)./Q)*ones(1,3)) ...
                   U.tot(:,8:end)];
else
    U.Molfrac   = [Q U.tot(:,1:2) U.tot(:,4:7)./(Q*ones(1,4)) ...
                   U.tot(:,8:end)];
    U.PartPress = [Q U.tot(:,2) U.tot(:,3:7).*((U.tot(:,1)./Q)*On1j) ...
                   U.tot(:,8:end)];
end

% Calculation of the component molar flowrates 
x.out = x.tot(:,ind.out([4:8 12:16])).*   ...
       [x.tot(:,ind.out(1))*On1j*scl{19}(ind.out(1)) ...
        x.tot(:,ind.out(9))*On1j*scl{19}(ind.out(9))];

% Calculation of the inlet and outlet flow of the heat exchanger
x.HEx8in   = x.tot(:,ind.HEx8in)  .*(OnN1*scl{19}(ind.HEx8in));
x.HEx8out  = x.tot(:,ind.HEx8out) .*(OnN1*scl{19}(ind.HEx8out));
x.HEx9in   = x.tot(:,ind.HEx9in)  .*(OnN1*scl{19}(ind.HEx9in));
x.HEx9out  = x.tot(:,ind.HEx9out) .*(OnN1*scl{19}(ind.HEx9out));
x.HEx10in  = x.tot(:,ind.HEx10in) .*(OnN1*scl{19}(ind.HEx10in));
x.HEx10out = x.tot(:,ind.HEx10out).*(OnN1*scl{19}(ind.HEx10out));

% Calculation of the separation factor for each component
alphU.gas = x.out(:,1:5) ./U.tot(:,3:7);
alphU.liq = x.out(:,6:10)./U.tot(:,3:7);

% Calculation of the heat duties of the heat exchanger
W(:,1) = x.tot(:,ind.HEx(1))*scl{19}(ind.HEx(1)).* ...	Outlet molar flow
        (U.tot(:,3:7)./(Q*On1j)*con.M').* ...          	Molar mass
        (par{9}.Hin.eval(x.HEx8in(:,ind.mH))- ...       Mass enthalpy inlet
         par{9}.Hout.eval(x.HEx8out(:,ind.mH)));...     Mass enthalpy outlet
W(:,2) = x.tot(:,ind.HEx(2))*scl{19}(ind.HEx(2)).* ...	Outlet molar flow
        (U.tot(:,3:7)./(Q*On1j)*con.M').* ...          	Molar mass
        (par{11}.Hin.eval(x.HEx9in(:,ind.mH))- ...      Mass enthalpy inlet
         par{11}.Hout.eval(x.HEx9out(:,ind.mH)));...    Mass enthalpy outlet
W(:,3) = x.tot(:,ind.HEx(3))*scl{19}(ind.HEx(3)).* ...	Outlet molar flow
        (U.tot(:,3:7)./(Q*On1j)*con.M').* ...          	Molar mass
        (par{15}.Hin.eval(x.HEx10in(:,ind.mH))- ...     Mass enthalpy inlet
         par{15}.Hout.eval(x.HEx10out(:,ind.mH)));...   Mass enthalpy outlet

% Combination of the outlet values
if Ver.diff
    x.red = [U.tot(:,[1:2 1:2])-x.tot(:,ind.out(:,[2:3 10:11])).*(OnN1*scl{19}(ind.out(:,[2:3 10:11]))) ...
             U.tot(:,10)-x.tot(:,ind.out(:,17)).*(OnN1*scl{19}(ind.out(:,17))) ...
             alphU.gas W];
else
    x.red = [x.tot(:,ind.out(:,[2:3 10:11])).*(OnN1*scl{19}(ind.out(:,[2:3 10:11]))) ...
             x.tot(:,ind.out(:,17)).*(OnN1*scl{19}(ind.out(:,17))) ...
             alphU.gas W];
end