function [CineqC,CeqC,JacineqC,JaceqC,Ceq,Jaceq,dCeqdx,into,x,colind] = ConMakeUp_min(a,par,scl,con,~,def,itg)

% This function defines the flowsheet of the reactant makeup section of the
% simplified ammonia loop. It can be used to define the surrogate model of
% this part of the plant. It consists of the compressors and the mixer
% including the streams coming from the recycle section of the plant.
%
% 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.
%
%       colind: Column indices for the Jacobians 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. The assignment is given in par.MakeUpSec.
%
%       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 and:
%               .sizeMat The size of the cell array par.MakeUpSec.
%
%       itg:    Intiger values, which should include
%               .k      Total number of variables per stream
%               .j      Total number of components in each stream

% Reassignment of the solution vector
x = cell(def.sizeMat(1)+1,1);
into = cell(def.sizeMat);
Ceq = into;
dCeqdx = into;
rowlen = into;
colind = into;
ctvar = into;
x{end} = a;

% Predefinition of the Jacobian of the System
Jaceq = into;%spalloc(itg.nvar,itg.nvar,6*itg.nvar);

%% Compressor 1
% Reassignment of the variables for the compressor 1
k = 1;
x{k} = a(1:18);
a = a(19:end);
ctvar{k} = itg.M;

% Definition of the input values for the first compressor
into{k} = [x{k}(1:8);...        Inlet flow, pressure, and temperature
%            par{k}.S;...         RPM of the Compressor
           x{1}(9);...          RPM of the Compressor
           x{k}(10);...         Compressor Power
%            x{k}(10:17)];...    	Outlet flow, pressure, and temperature
           x{k}(11:18)];...     Outlet flow, pressure, and temperature
          
% Calculation of the equality conditions given by compressor 1
[Ceq{k},dCeqdx{k}] = CompSim(into{k},par{k},scl{k},con,[],itg);

% Calculation of the parts of the Jacobian defined by compressor 1
rowlen{k} = length(Ceq{k});                         % Number of the rows
colind{k} = ctvar{k}+1:ctvar{k}+length(x{k});      	% Indexes of the columns
Jaceq{k} = sparse(rowlen{k},itg.nvar);
Jaceq{k}(:,colind{k}) = dCeqdx{k};
% Jac{k}(:,colind{k}) = dCeqdx{k}(:,[1:8 10:end]);

%% Heat Exchanger 1
% Reassignment of the variables for heat exchanger 1
x{k+2} = a(1:24);
a = a(25:end);
ctvar{k+2} = ctvar{k}+numel(x{k});
k = k+2;

% Definition of the input values for heat exchanger 1
into{k} = [into{k-2}(11:18);... Hot inlet flow, pressure, and temperature 
           x{k}(1:8);...        Hot outlet flow, pressure, and temperature 
           x{k}(9:16);...       Cold inlet flow, pressure, and temperature 
           x{k}(17:24)];...     Cold outlet flow, pressure, and temperature 

% Calculation of the equality constraints given by heat exchanger 1
[Ceq{k},dCeqdx{k}] = HExeNTU(into{k},par{k},scl{k},con,[],itg);

% Calculation of the parts of the Jacobian defined by heat exchanger 1
rowlen{k} = length(Ceq{k});                         % Number of the rows
colind{k} = [colind{k-2}(11:18) ...                 % Input p and T Cold Stream
             ctvar{k}+1:ctvar{k}+length(x{k})];     % Indexes of the columns
Jaceq{k} = sparse(rowlen{k},itg.nvar);
Jaceq{k}(:,colind{k}) = dCeqdx{k}(:,1:32);


%% Compressor 2
% Reassignment of the variables for compressor 2
x{k+2} = a(1:9);
a = a(10:end);
ctvar{k+2} = ctvar{k}+numel(x{k});
k = k+2;

% Definition of the input values for compressor 2
into{k} = [into{k-2}(9:16);...  Inlet flow, pressure, and temperature
%            par{k}.S;...         RPM of the Compressor
           x{1}(9);...          RPM of the Compressor
           x{k}(1);...          Compressor Power
           x{k}(2:9)];...       Outlet flow, pressure, and temperature

% Calculation of the equality conditions given by compressor 2
[Ceq{k},dCeqdx{k}] = CompSim(into{k},par{k},scl{k},con,[],itg);

% Calculation of the parts of the Jacobian defined by compressor 2
rowlen{k} = length(Ceq{k});                         % Number of the rows
colind{k} = [colind{k-2}(9:16) ...                  % Input Q, p, and T
             colind{1}(9) ...                       % Compressor RPM
             ctvar{k}+1:ctvar{k}+length(x{k})];     % Indexes of the columns
Jaceq{k} = sparse(rowlen{k},itg.nvar);
Jaceq{k}(:,colind{k}) = dCeqdx{k};
% Jac{k}(:,colind{k}) = dCeqdx{k}(:,[1:8 10:end]);

%% Mixer 1
% Reassignment of the variables for mixer 1
x{k+2} = a(1:16);
a = a(17:end);
ctvar{k+2} = ctvar{k}+numel(x{k});
k = k+2;

% Definition of the input values for mixer 1
into{k} = [into{k-2}(11:18);...    	Inlet 1 flow, pressure, temperature, and composition
           x{k}(1:8);...            Inlet 2 flow, pressure, temperature, and composition
           x{k}(9:16)];           % Outlet flow, pressure, temperature, and composition

% Calculation of the equality conditions given by mixer 1
[Ceq{k},dCeqdx{k}] = Mix(into{k},par{k},scl{k},[],[],itg);

% Calculation of the parts of the Jacobian defined by mixer 1
rowlen{k} = length(Ceq{k});                      	% Number of the rows
colind{k} = [colind{k-2}(11:18) ...                 % Input Q, p, and T
             ctvar{k}+1:ctvar{k}+length(x{k})];     % Indexes of the columns
Jaceq{k} = sparse(rowlen{k},itg.nvar);
Jaceq{k}(:,colind{k}) = dCeqdx{k};

%% Compressor 3
% Reassignment of the variables for compressor 3
x{k+2} = a(1:9);
a = a(10:end);
ctvar{k+2} = ctvar{k}+numel(x{k});
k = k+2;

% Definition of the input values for compressor 3
into{k} = [into{k-2}(17:24);...	Inlet flow, pressure, and temperature
%            par{k}.S;...         RPM of the Compressor
           x{1}(9);...          RPM of the Compressor
           x{k}(1);...          Compressor Power
           x{k}(2:9)];...       Outlet flow, pressure, and temperature

% Calculation of the equality conditions given by compressor 3
[Ceq{k},dCeqdx{k}] = CompSim(into{k},par{k},scl{k},con,[],itg);

% Calculation of the parts of the Jacobian defined by compressor 3
rowlen{k} = length(Ceq{k});                             % Number of the rows
colind{k} = [colind{k-2}(17:24) ...                     
             colind{1}(9) ...                           % Compressor RPM
             ctvar{k}+1:ctvar{k}+length(x{k})];      	% Indexes of the columns
Jaceq{k} = sparse(rowlen{k},itg.nvar);
Jaceq{k}(:,colind{k}) = dCeqdx{k};
% Jac{k}(:,colind{k}) = dCeqdx{k}(:,[1:8 10:end]);

%% Call and reassignment of the definitions within the submodels
[Ceqdef,Jacdef] = def.fun(itg.nvar,into,colind);

Ceq(2:2:end) = Ceqdef(2:2:end);
Jaceq(2:2:end) = Jacdef(2:2:end);

%% Combination of the equality constraints
CeqC   = cell2mat(Ceq);
JaceqC = cell2mat(Jaceq)';

%% Assignment and combination of the inequality constraints
[Cineq,Jacineq] = def.ineqfun(itg.nvar,into,colind);

CineqC   = cell2mat(Cineq);
JacineqC = cell2mat(Jacineq)';