function [x,b,exflg,Uout] = SampSep_MolFlow(initSep,Uin,opt,par,scl,con,ther,def,itg)

% This function defines the separation section for the simple ammonia cycle
% in the case of sampling. It is using the inputs as molar flows instead of
% mole fractions and samples the in the vector A defined independent variables. 
%
% Written by Julian Straus, Trondheim 09.12.2018
%
% The needed input for this function are
%   initSep:    Solution of the system around the nominal point
%   Uin:        A column vector defining the input values for the sampling
%               of the variables.
%   opt:        Options for fsolve, defined for all iterations of the
%               sampling in the same way.
%
% The output is defined as
%  	x:          States of the system, calculated via the non-linear system
%               of equations.
%   b:      	Residual in the Newton-Raphson approach
%   exflg:      Exitflag of fsolve.
%
% The necessary 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 factor for the temperature
%               .W      Scaling factor for the power
%
%   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

% Redefinition of the screening values
Uout = Uin(end,:)';
Q = sum(Uout(3:itg.k-1));
def.FSep = [Q; Uout(1:2); Uout(3:itg.k-1)/Q];
def.HEx6(1:3) = Uout(8:10);
par{3}.ratio = [0; Uout(11)];
par{9}.Tref = Uout(12);
par{11}.Tref = Uout(13);
par{15}.Tref = Uout(14);
Uout = Uout';

def.fun = @(nvar,into,colind) DefSep(nvar,into,colind,scl,def);
ConAnSep = @(x) ConSep(x,par,scl,con,ther,def,itg);
[x,b,exflg] = fsolve(ConAnSep,initSep',opt);
