function [Ceqdef,Jacdef] = DefReact_NN(nvar,into,colind,scl,def)

%
% Written by Julian Straus, Trondheim 09.12.2018
% 
% This function defines the definition values in the reaction
% section of the simple ammonia loop. It can be modified to define the
% flowsheet differently. The definitions in this file are given by:
%
%   Cold inlet HEx2     Q, p, T, and xi
%
% The input of this function is given by
%       nvar:   Total number of variables.
%
%       into:   Input values to the unit operation defined in the
%               flowsheet.
%
%       colind: Indexes of the columns i the Jacobian.
%
% The main output is defined as
%       Ceqdef: Cells containing the equality constraints of the
%               definitions.
%
%       Jacdef: Cells containing the rows of the Jacobian defined by the
%               definitions.
%
% The necessary structures and cells are defined as
%       scl:    Scaling factors of the system defined in cells representing
%               the unit operations defined in par.ReactSec.
%
%       def:    Defined stream values, e.g pressure and composition for all
%               inlets and the pressure of the outlets or the flow of the
%               inlets.

% Predefinition of the returning cells.
Jacdef = cell(size(into));
Ceqdef = Jacdef;
                                  
%% Definitions in the neural network

k = 1;
% Calculation of the equality constraints for the definition of heat exchanger 2
Ceqdef{k+1} = [into{k}(17:24).*scl{k+20}.Var(17:24)'-def.FReact;...
               into{k}(33:34).*scl{k+20}.Var(33:34)'-def.Sratio{1}(1:2);...
               into{k}(35).*scl{k+20}.Var(35)'-def.Tref].*scl{k+20}.DefEq;

% Calculation of the parts of the Jacobian defined by the definition of
% heat exchanger 2
colind{k+1} = colind{k}([17:24 33:35]);
Jacdef{k+1} = sparse(1:numel(Ceqdef{k+1}),colind{k+1},scl{k+20}.DefEq'.*scl{k+20}.Var([17:24 33:35]),...
                     numel(Ceqdef{k+1}),nvar);
