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

%
% Written by Julian Straus, Trondheim 09.12.2018
% 
% This function defines the definition values in the reactant makeup
% section of the simple ammonia loop. It can be modified to define the
% flowsheet differently. The definitions in this file are given by:
%
%   Inlet Comp1         p, T, and xi
%   Cold inlet HEx1     Q, p, T, and xi
%   Inlet 2 Mix1        Q, p, t, and xi
%
% Hence, the inlet flow is defined via the recycle pressure and not the
% inlet flow.
%
% 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(def.sizeMat);
Ceqdef = Jacdef;

%% Definitions in Compressor 1
k = 1;
% Calculation of the equality constraints for the definition of compressor 1
Ceqdef{k+1} = (into{k}(2:9).*scl{k}.Var(2:9)'-def.FMake(2:end)).*scl{k}.DefEq;

% Calculation of the parts of the Jacobian defined by the definition of
% compressor 1
colind{k+1} = colind{k}(2:8);
Jacdef{k+1} = sparse(1:numel(Ceqdef{k+1}),colind{k+1},scl{k}.DefEq'.*scl{k}.Var(2:8),...
                     numel(Ceqdef{k+1}),nvar);
                                  
%% Definitions in Heat Exchanger 1

k = 3;
% Calculation of the equality constraints for the definition of heat exchanger 1
Ceqdef{k+1} = (into{k}(17:24).*scl{k}.Var(17:24)'-def.HEx1).*scl{k}.DefEq;

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

%% Definitions in Mixer 1

k = 7;
% Calculation of the equality constraints for the definition of mixer 1
Ceqdef{k+1} = (into{k}(9:16).*scl{k}.Var(9:16)'-def.FMix(1:end)).*scl{k}.DefEq;

% Calculation of the parts of the Jacobian defined by the definition of
% mixer 1
colind{k+1} = colind{k}(9:16);
Jacdef{k+1} = sparse(1:numel(Ceqdef{k+1}),colind{k+1},scl{k}.DefEq'.*scl{k}.Var(9:16),...
                     numel(Ceqdef{k+1}),nvar);