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

% This function defines the neural network equality constraints of the 
% reactor section of the simplified ammonia cycle. The original model 
% consists of the recycle heat exchanger, the heat exchanger network for 
% heating the reactor and the two reactors itself. 
%
% The original underlying model is defined in "ConReact.m" in the same 
% folder.
%
% 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.ReactSec.
%
%       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.ReactSec.
%
%       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(3,1);      % cell(def.sizeMat(1)+1,1);
into = cell(2,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;

%% Reactor section via Neural Networks
% Reassignment of the variables
k        = 1;
ctvar{k} = itg.R;
x{k}     = a;

% Definition of the input variables
into{k} = [zeros(8,1);...       Hot inlet dummy variables
           x{k}(1:8);...        Connection to the separation section
           x{k}(9:16);...       Connection to the MakeUp section
           zeros(8,1);...       Cold outlet dummy variables
           x{k}(17:19);...      Parameters (Variable)
           x{k}(20)];...        Heat generated in HEx5

% Calculation of the equality constraints given by heat exchanger 6
[Ceq{k},dCeqdx{k}] = ReacNN(into{k},par{20+k},scl{20+k},[],[],itg);

% Calculation of the parts of the Jacobian defined by the reactor section
% model
rowlen{k} = length(Ceq{k});...                      Number of the rows
colind{k} = [zeros(1,8) ...                         Hot inlet dummy variables
             ctvar{k}+1:ctvar{k}+8 ...              Connection to the separation section
             ctvar{k}+9:ctvar{k}+16 ...             Connection to the MakeUp section
             zeros(1,8) ...                         Cold outlet dummy variables
             ctvar{k}+17:ctvar{k}+19 ...            Parameters
             ctvar{k}+20]; ....                     Heat generated in HEx5
Jac{k} = sparse(rowlen{k},itg.nvar);
Jac{k}(:,colind{k}([9:24 33:36])) = dCeqdx{k}(:,[9:24 33:36]);

%% 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)';