function [CeqC,JacC,Ceq,dCeqdx,into,x] = ConPipe(a,par,scl,con,~,def,itg)

% This function defines the flowsheet of the simple model.
%
% 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.
%       into:   Input vector to the unitoperation files.
%
%       Ceq:    Equality contraints.
%
%       dCeqdx: Full Jacobians.
%
%       x:      Reassigned states of the subunits.
%
%       ctvar:  Counting variable to determine the overall index of a state
%
%       cteq:   Counting variable to determine the overall index of an
%               equality constraint.
%
%       rowind: Row indices for the Jacobian.
%
%       colind: Column indices for the Jacobian.
%
% The necessary structures are defined as
%       par:    Parameters used in the calculation, output of the system
%
%       scl:    Scaling factors of the system, must include:
%               .Q      Scaling factor for the flow
%               .p      Scaling factor for the pressure
%               .T      Scaling factor for the temperature
%               .W      Scaling factor for the pwer
%
%       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
%
%       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;
nvar = numel(a);

% Definition of auxiliary variables based on itg.j and itg.k
k1 = itg.k;
k2 = 2*itg.k;

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

%% Pipe 1
% Reassignment of the variables for the compressor 1
k = 1;
x{k} = a(1:k2);
a = a(k2+1:end);
ctvar{k} = 0;

% Definition of the input values for the first compressor
into{k} = [x{k}(1:k1);...   	Inlet flow, pressure, and temperature
           x{k}(k1+1:end)];   % Outlet flow, pressure, and temperature
          
% Calculation of the equality conditions given by compressor 1
[Ceq{k},dCeqdx{k}] = PipeSimp(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
Jac{k} = sparse(rowlen{k},nvar);
Jac{k}(:,colind{k}) = dCeqdx{k};


%% Call and reassignment of the definitions within the submodels
[Ceqdef,Jacdef] = def.fun(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);