function [CeqC,JacC,Ceq,Jac,dCeqdx,into,x] = ConComb(a,par,scl,con,ther,def,itg)

% This function defines the flowsheet of the simple ammonia cycle. If new
% unit operations have to be added, only the input values to the subsequent
% unit operation has to be changed. However, the initial values have to be
% adjusted.
%
% Written by Julian Straus, Trondheim 09.12.2018
%  
% The input of this function is given by
%       a:      States of the system, length(a) is depending on the
%               investigated model.
%
% The main output is defined as
%       CeqC:   Equality constraints of the non-linear system of equations.
%
%       JacC:   Jacobian of the system.
%
% The seconday output for post analysis is given by the following
% structures. Each structure includes values for each subunit.
%       Ceq:    Cell structure containing the equality contraints of the
%               UOs.
%
%       Jac:    Cell structure containing the full Jacobians of the UOs at
%               the right position.
%
%       dCeqdx: Cell structure containing the full jacobians of the UOs.
%
%       into:   Cell structure containing the Input vector to the UOs.
%
%       x:      Cell structure containing the reassigned states of the UOs.
%
% The necessary structures are defined as
%       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.
%
%       con:    Physical constants, must contain
%               .R      Ideal Gas constant
%
%       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

% Prdefinition of the arrays
x_in = cell(4,1);
Ceq = cell(6,1);
Jac = Ceq;
dCeqdx = Ceq;
into = Ceq;
Ceq_a = Ceq;
Jac_a = Ceq;
colind = Ceq;

% Reassignment of the solution vector
x_in{1} = a(1:76);
x_in{2} = a(77:end-185);
x_in{3} = a(end-184:end);
x_in{4} = a;

% Call of the solutions given by the MakeUp section
itg.M = 0;
[Ceq_a{1},Jac_a{1},Ceq{1},Jac{1},dCeqdx{1},into{1},x{1},colind{1}] = ConMakeUp(x_in{1},par.M,scl.M,con,[],def.M,itg);

% Call of the constraints defined in the Reaction section
itg.R = numel(x_in{1});
[Ceq_a{3},Jac_a{3},Ceq{3},Jac{3},dCeqdx{3},into{3},x{3},colind{3}] = ConReact(x_in{2},par.R,scl.R,con,ther,def.R,itg);

% % Call of the constraints defined in the Separation section
itg.S = numel(x_in{1}) + numel(x_in{2});
[Ceq_a{5},Jac_a{5},Ceq{5},Jac{5},dCeqdx{5},into{5},x{5},colind{5}] = ConSep(x_in{3},par.S,scl.S,con,ther,def.S,itg);

% Definition of the equality constraints between Makeup and Reaction
% section
Ceq_a{2}  = (into{1}{9}(11:18).*scl.M{9}.Var(11:18)' - into{3}{1}(17:24).*scl.R{1}.Var(17:24)')./scl.V';
colind{2} = [colind{1}{9}(11:18) colind{3}{1}(17:24)];
Jac_a{2}  = sparse([1:8 1:8],colind{2},...
                   [scl.M{9}.Var(11:18) -scl.R{1}.Var(17:24)]./[scl.V scl.V],...
                    8,itg.nvar);

% Definition of the equality constraints between Reaction and Separation
% section
Ceq_a{4}  = (into{3}{1}(9:16).*scl.R{1}.Var(9:16)' - into{5}{1}(1:8).*scl.S{1}.Var(1:8)')./scl.V';
colind{4} = [colind{3}{1}(9:16) colind{5}{1}(1:8)];
Jac_a{4}  = sparse([1:8 1:8],colind{4},...
                   [scl.R{1}.Var(9:16) -scl.S{1}.Var(1:8)]./[scl.V scl.V],...
                    8,itg.nvar);

% Definition of the equality constraints between Makeup and Separation
% section  _a{6} {5}{8}
Ceq_a{6}  = (into{1}{7}(9:16).*scl.M{7}.Var(9:16)' - into{5}{7}(9:16).*scl.S{7}.Var(9:16)')./scl.V';
colind{6} = [colind{1}{7}(9:16) colind{5}{7}(9:16)];
Jac_a{6}  = sparse([1:8 1:8],colind{6},...
                   [scl.M{7}.Var(9:16) -scl.S{7}.Var(9:16)]./[scl.V scl.V],...
                    8,itg.nvar);


% Definition of the overall equality constraints
CeqC = cell2mat(Ceq_a);

% Definition of the overall Jacobian
JacC = cell2mat(Jac_a);