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

% Calculation file - reaction 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)./(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(2:3)).*(OnN1*scl{19}(ind.out(2:3))) ...
         x.tot(:,ind.out(4:8)).*(x.tot(:,ind.out(1))*scl{19}(ind.out(1))*On1j)];
     
% Calculation of the inlet and outlet flow of the heat exchanger
x.HEx5in  = x.tot(:,ind.HEx5in).*(OnN1*scl{19}(ind.HEx5in));
x.HEx5out = x.tot(:,ind.HEx5out).*(OnN1*scl{19}(ind.HEx5out));

% Calculation of the differences in molar flow rate and the extent of
% reaction for the sampling space as well as 
Diff   = x.out(:,3:7)-U.tot(:,3:7);
ex_o_R = Diff.*(OnN1*(1./par{13}.stoi'));

% Calculation of the heat duties of the heat exchanger
W = x.tot(:,ind.out(1))*scl{19}(ind.out(1)).* ...	Outlet molar flow
    (x.tot(:,ind.out(4:8))*con.M').* ...            Molar mass
    (par{17}.Hin.eval(x.HEx5in(:,ind.mH))- ...      Mass enthalpy inlet
     par{17}.Hout.eval(x.HEx5out(:,ind.mH)));...    Mass enthalpy outlet
%    par{17}.dH;...                                 Mass enthalpy

% min(abs(par{17}.Hin.eval(x.HEx5in(:,ind.mH))))
% min(abs(par{17}.Hout.eval(x.HEx5out(:,ind.mH))))

% Combination of the outlet values
if Ver.diff
    x.red  = [U.tot(:,1:2)-x.out(:,1:2) ex_o_R(:,1) W];
else
    x.red  = [x.out(:,1:2) ex_o_R(:,1) W];
end