function [CineqC,CeqC,JacineqC,JacC,Ceq,Jac,dCeqdx,into,x,colind] = ConSep_NN_min(a,par,scl,~,~,def,itg)

% This function defines the flowsheet of the separation section of the
% simplified ammonia cycle. It can be used to define the surrogate model of
% this part of the plant. It consists of the heat exchanger network and the
% separator used in the simplified model. It can be easily extended to more
% complicated models if needed.
%
% 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.SepSec.
%
%       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 and:
%               .sizeMat The size of the cell array par.SepSec.
%
%       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(9,1);      % cell(def.sizeMat(1)+1,1);
into = cell(8,1);   % cell(def.sizeMat);
Ceq = into;
dCeqdx = into;
rowlen = into;
colind = into;
ctvar = into;
x{end} = a;

% Predefinition of the Jacobian of the System
Jac = into;

%% Separation section via Neural Networks
% Reassignment of the variables
k = 1;
x{k} = a(1:47);
a = a(48:end);
ctvar{k} = itg.S;

% Definition of the input values
into{k} = [x{k}(1:8);...                    Inlet 
           x{k}(17:24);...                  Liquid outlet 
           x{k}(9:16);...                   Heat Exchanger inlet
           x{k}(25:32);...                  Gas outlet 
           x{k}(33:40);...                  Heat Exchanger outlet
           x{k}(41:44);...                  Parameters (Variable
%            par{3}.ratio(2); par{9}.Tref;... Parameters (Fixed)
%            par{11}.Tref; par{15}.Tref;... 	Parameters (Fixed)
           x{k}(45:47)];...               Heat Exchanger Duties
             
% Calculation of the equality constraints given by the neural network
[Ceq{k},dCeqdx{k}] = SepNN(into{k},par{k+20},scl{k+20},[],[],itg);

% Calculation of the parts of the Jacobian defined by the neural network
rowlen{k} = length(Ceq{k});...                  Number of the rows
colind{k} = [ctvar{k}+1:ctvar{k}+8 ...          Inlet
             ctvar{k}+17:ctvar{k}+24 ...        Liquid Outlet
             ctvar{k}+9:ctvar{k}+16 ...         Heat Exchanger inlet
             ctvar{k}+25:ctvar{k}+32 ...     	Gas outlet
             ctvar{k}+33:ctvar{k}+40 ...        Heat Exchanger outlet
             ctvar{k}+41:ctvar{k}+44 ...        Parameters
             ctvar{k}+45:ctvar{k}+47]; ...      Heat Exchanger duties
Jac{k} = sparse(rowlen{k},itg.nvar);
Jac{k}(:,colind{k}) = dCeqdx{k}(:,1:47);

%% Split 3 - Recycle Split
k = 7;
% Reassignment of the variables for split 3
x{k} = a(1:17);
a = a(18:end);
ctvar{k} = ctvar{1}+numel(x{1});

% Definition of the input values for split 3
into{k} = [into{1}(25:32);...   Inlet flow
           x{k}(1:8);...        Outlet 1 flow
           x{k}(9:16);...      	Outlet 2
           0; x{k}(17)];...     Split ratios

% Calculation of the equality constraints given the split 3
[Ceq{k},dCeqdx{k}] = Split(into{k},par{k},scl{k},[],[],itg);

% Calculation of the parts of the Jacobian defined by split 3 (only flows)
rowlen{k} = length(Ceq{k});...                      Number of the rows
colind{k} = [colind{1}(25:32) ...                   Indexes of the inlet columns
             ctvar{k}+1:ctvar{k}+length(x{k})];...  Indexes of the outlet columns
Jac{k} = sparse(rowlen{k},itg.nvar);
Jac{k}(:,colind{k}) = dCeqdx{k}(:,[1:24 26]);

%% 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);
Jac(2:2:end) = Jacdef(2:2:end);

%% Combination of the equality constraints
CeqC = cell2mat(Ceq);
JacC = cell2mat(Jac)';

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

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