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

% This function defines the reaction section for the simple ammonia cycle
% in the case of sampling. It is using the inputs as molar flows instead of
% mole fractions in comparison to the function "SampReact.m". It samples
% the in the vector A defined independent variables. 
%
% Written by Julian Straus, Trondheim 09.12.2018
%
% The needed input for this function are
%   initReact:  Solution of the system around the nominal point
%
%   U:          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.FReact = [Q; Uout(1:2); Uout(3:itg.k-1)/Q];
par{3}.ratio = [Uout(8:9); 0];
par{17}.Tref = Uout(10);
Uout = Uout';

def.fun = @(nvar,into,colind) DefReact(nvar,into,colind,scl,def);
ConAnReact = @(x) ConReact(x,par,scl,con,ther,def,itg);
[x,b,exflg,c,d] = fsolve(ConAnReact,initReact',opt);
A = 0; 